What is the reason python handles locals () this way (in pairs)?

Get this simple python code, same mapping to re.compile instance. I noticed that although I use the same value, it creates two instances and repeats them accordingly.

I wonder if the reason for this behavior can be explained,

  • Why does he create a second instance at all?
  • Why only two?
  • And why each time chose a different, and not random?

CLI code:

>>> import re
>>>
>>> rec = re.compile("(?:[-a-z0-9]+\.)+[a-z]{2,6}(?:\s|$)")
>>>
>>> rec.match('www.example.com')
<_sre.SRE_Match object at 0x23cb238>
>>> rec.match('www.example.com')
<_sre.SRE_Match object at 0x23cb1d0>
>>> rec.match('www.example.com')
<_sre.SRE_Match object at 0x23cb238>
>>> rec.match('www.example.com')
<_sre.SRE_Match object at 0x23cb1d0>
>>> rec.match('www.example.com')
<_sre.SRE_Match object at 0x23cb238>
>>> rec.match('www.example.com')
<_sre.SRE_Match object at 0x23cb1d0>

Edit:

As @kimvais answered , the reason lies in _which contains the last assignment. see, if you do not appoint, but print, it is the same all the time.

>>> print rec.match('www.example.com')
<_sre.SRE_Match object at 0x23cb1d0>
>>> print rec.match('www.example.com')
<_sre.SRE_Match object at 0x23cb1d0>
>>> print rec.match('www.example.com')
<_sre.SRE_Match object at 0x23cb1d0>
>>> print rec.match('www.example.com')
<_sre.SRE_Match object at 0x23cb1d0>
>>> print rec.match('www.example.com')
<_sre.SRE_Match object at 0x23cb1d0>
>>> print rec.match('www.example.com')
<_sre.SRE_Match object at 0x23cb1d0>
+5
source share
2 answers

, , (_) python, .. _ <_sre.SRE_Match object at 0x23cb238>, rec.match , , _ , .

+8

, , - . 6 <_sre.SRE_MATCH>.

, , .

0x23cb1d0 - , , , GUID.

, , , , .

+1

All Articles