I keep seeing a lot of code around that does this:
import re _MATCH_DIGITS_RE = re.compile('[0-9]+')
and then uses this regular expression only in one place, e.g. like this:
def func(): numbers = _MATCH_DIGITS_RE.findall(input) ...
re module actually uses expression caching internally, so
it's very unlikely that this is any faster in practice than just
def func(): numbers = re.findall('[0-9]+', input) ...
Which is a shorter and much more straight forward way to write what's going on. Now, for longer and more complex regular expressions, this can actually get out of hand and it does help to give them a readable name. However, all-upper-case constant names tend to be pretty far from readable. So, I always wonder why people don't just write this using a bound method:
_find_numbers = re.compile('[0-9]+').findall def func(): numbers = _find_numbers(input) ...
I find this much clearer to read. And it nicely abstracts the code
that uses the function-like callable
_find_numbers() from the
underlying implementation, which (in case you really want to know)
happens to be a method of a compiled regular expression object.