3

Can I have anonymous function with "static" variables in Python?

For example

lambda x: re.compile(r'foobar').match(x)

is not so great, because it may recompile every time it is called (if re runs out of cache - thank you responders for pointing out the cache mechanism).

I can do this without recompiling:

def f(line):
    try:
        f.c
    except:
        f.c = re.compile(r'foobar')
    return f.c.match(line)

How to do it with a lambda, without recompiling?

And well, I don't want to use a helper function, to use inside the lambda. The whole point of using lambdas is "anonymity". So yes the lambda is anonymous, and self-contained.

9
  • 1
    re library has a caching mechanism, so your regex should not be compiled each time function is called. Commented Apr 23, 2015 at 13:47
  • 1
    In practice I wouldn't worry about that. The re module internally caches 100 regexes, so if you aren't using more than 100 patterns you won't see any significant performance gain from doing this Commented Apr 23, 2015 at 13:47
  • 2
    why would the second example not recompile? Commented Apr 23, 2015 at 13:48
  • 2
    I'm mean if we assume that re is not caching (I think the OP just used re.compile as an example) Commented Apr 23, 2015 at 13:52
  • 1
    after you import re, you could set the MAXCACHE to whatever you like Commented Apr 23, 2015 at 23:51

1 Answer 1

11

The usual trick is to provide a default value for an argument you don't intend to supply.

lambda x, regexobject=re.compile(r'foobar'): regexobject.match(x)

The default value is evaluated when the lambda is defined, not each time it is called.


Rather than using the lambda, though, I would just define your regular expressions explicitly

regex1 = re.compile(r'foobar')
regex2 = re.compile(r'bazquux')
# etc

then pass the bound method around where needed. That is, instead of

somefunction(lambda x, regexobject=re.compile(r'foobar'): regexobject.match(x))

use

somefunction(regex1.match)

The use case for an anonymous function is one that will only be called once, so there is no sense in binding a name to it. The fact that you are concerned about re.compile being called multiple times indicates that the functions will be called several times.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for the suggestion just to use the bound method. I see no advantage for trying to apply lambdas here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.