I have a function that iterates over a text file to extract certain information by using regular expressions to find the information. But, the program goes over multiple files and so calls this function several times.
Currently, I have the regular expressions compile as the first steps of the function. But I started wondering if this is good design from a programming perspective since the function gets called several times.
Is the interpreter smart enough to see that these do not change and cache them away between runs? Alternatively I considered compiling them as global variables so that it would always be available only compiled once, but that separates the regex from the place it will be used which makes it harder to read. The other option I looked at was creating the function as a closure with regex values passed in when it is created, but that seemed unnecessarily complex.
In short, What is the most efficient way to compile the RE's (or any other value which is calculated once) that is still readable and pythonic?
Thank you.
Python's regex module does cache the compiled versions of recently-used regexes, so you can probably remove the explicit compiles and not have an issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With