Code execution in imported modules
Casey McGinty
casey.mcginty at gmail.com
Fri May 30 02:01:29 EDT 2008
On Thu, May 29, 2008 at 2:43 PM, Eric Wertman <ewertman at gmail.com> wrote:
> So I'm working on some file parsing and building up a stack of regular
> expressions that I need to use. I was thinking of dropping them in an
> external module. I was wondering.. if I put them in a file called
> regex.py like so :
>
> import re
>
> re1 = "..
> re2 = "..
>
> and then do:
>
> rgx1 = re.compile(re1)
> rgx2 = re.compile(re2)
>
>
> and, in my script, parse.py py I do:
>
> from regex import *
>
> text = "bunch of stuff......."
>
> m = rgx1.search(text)
>
>
> Does the re get compiled when I import it, or every time I call it?
> Since I'm calling it often, I'd like to compile it once.
>
> Thanks!
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Please correct me if I am wrong ...
but I believe this should work, and rgx's will only be compiled on the first
import. You could also place them in a class and get the same effect when
you instantiate the class.
class MyRegEx( object ):
re1 = "..
re2 = "..
rgx1 = re.compile(re1)
rgx2 = re.compile(re2)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080529/c1073db8/attachment-0001.html>
More information about the Python-list
mailing list