Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Where to compile an RE?

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.

like image 832
TimothyAWiseman Avatar asked Dec 30 '25 01:12

TimothyAWiseman


1 Answers

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.

like image 194
Amber Avatar answered Jan 01 '26 15:01

Amber