Get this simple python code, same matching with re.compile instance. I noticed that even though I am using the very same value, it creates two instances, and repeats them accordingly.
I wonder if one can tell the reason for this behavior,
- Why does it create the second instance at all?
- Why only two?
- And why each time picked the other one and not randomly?
the 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 lays in the _
which holds the latest assignment.
see, if you not assigning, rather printing, it is the same one, 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>