On Dec 23, 2:39 pm, "Terry Reedy" <[EMAIL PROTECTED]> wrote:
> "John Machin" <[EMAIL PROTECTED]> wrote in message
>
> news:[EMAIL PROTECTED]
> | On Dec 23, 5:38 am, "Terry Reedy" <[EMAIL PROTECTED]> 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.
>
Semi-joking; I thought that your offering of this:
def searcher(rex):
crex = re.compile(rex)
def _(txt):
return crex.search(txt)
return _
foo_searcher = searcher('foo')
was somewhat over-complicated, and possibly slower than already-
mentioned alternatives. The standard idiom etc etc it may be, but the
OP was interested in getting overhead out of his re searching loop.
Let's trim it a bit.
step 1:
def searcher(rex):
crexs = re.compile(rex).search
def _(txt):
return crexs(txt)
return _
foo_searcher = searcher('foo')
step 2:
def searcher(rex):
return re.compile(rex).search
foo_searcher = searcher('foo')
step 3:
foo_searcher = re.compile('foo').search
HTH,
John
--
http://mail.python.org/mailman/listinfo/python-list