Performance on local constants?

Terry Reedy tjreedy at udel.edu
Sat Dec 22 22:39:05 EST 2007


"John Machin" <sjmachin at lexicon.net> wrote in message 
news:ab88db50-ce4e-4298-bcec-079de67dbcb8 at e25g2000prg.googlegroups.com...
| On Dec 23, 5:38 am, "Terry Reedy" <tjre... at udel.edu> wrote:
| > 'Most flexible' in a different way is
| >
| > def searcher(rex):
| >     crex = re.compile(rex)
| >     def _(txt):
| >         return crex.search(txt)
| >     return _
| >
|
| I see your obfuscatory ante and raise you several dots and
| underscores:

I will presume you are merely joking, but for the benefit of any beginning 
programmers reading this, the closure above is a standard functional idiom 
for partial evaluation of a function (in this this, re.search(crex,txt))

| class Searcher(object):
|    def __init__(self, rex):
|        self.crex = re.compile(rex)
|    def __call__(self, txt):
|        return self.crex.search(txt)

while this is, the equivalent OO version.  Intermdiate Python programmers 
should know both.

tjr






More information about the Python-list mailing list