--On 15. Januar 2008 20:21:44 -0700 David Bear <David.Bear@asu.edu> wrote:
In the zope 2.7 book it mentions that an optimization technique for external methods is to compile regular expressions in global scope to the module.
I don't think that this is very specific to Zope but a general recommendation for any Python application. Since compiling regexes can be expensive you want to compile then once. Defining them on the module level is the common solution.
I am wondering if the same holds for lambdas. I created some simple filters using lambdas. I defined them in the functions where I use them. Is there any advantage to defining them globally to the module?
I don't see any relationship between lambda expressions and regex since there is no compilation involved when dealing with lambda expressions - except the byte code compiliation however this takes place when importing a module - not at runtime.
Are there any gottcha's with globally scope objects?
This is a general programming question - no necessarily tied to Python. A general answer might be: define only those things globally that must be globally (aka void global namespace polution). Usually define some constants or something similar globally in order to avoid moving those parameters around..but it depends on the individual usecase. Andreas