When I compile a regex and assign it to a variable or add it to a list, I suspiciously get different behaviour on Python 2.x and 3.x.
import re
z = re.compile('a')
print(z)
Above snippet prints on 2.x
<_sre.SRE_Pattern object at 0x7ff839e57030>
and on 3.x
re.compile('a')
The first one looks like the regex is compiled and ready to go whenever I need it (which is what I want) but the second one still says re.compile
.
Does that mean the regex is compiled on the fly when I need it and even worse recompiled every time I reference z
and do something like z.match('a')
? Or is the described Python 3 behaviour just cosmetic and it also maintains a compiled copy under the hood?
My point is, I (statically) compile my regexes at the beginning of the source file, so can save some time where I repetitively reference them in loops but if this is not happening, then that isn't good.
All this means is that the __repr__
of _sre.SRE_Pattern
has been changed, from the (not terribly helpful) default "<classname object at address>"
to something more useful. Per the data model documentation (emphasis mine):
If at all possible, [the
__repr__
string representation of an object] should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form<...some useful description...>
should be returned.
Compare 2.x:
>>> import re
>>> a = re.compile('a')
>>> a
<_sre.SRE_Pattern object at 0x02654440>
>>> type(a)
<type '_sre.SRE_Pattern'>
>>> repr(a)
'<_sre.SRE_Pattern object at 0x02654440>'
And 3.x:
>>> import re
>>> a = re.compile('a')
>>> a
re.compile('a')
>>> type(a)
<class '_sre.SRE_Pattern'>
>>> repr(a)
"re.compile('a')"
There is no difference in behaviour - the regular expression is still only compiled once (that's the whole point).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.