dynamic functions

Brian Quinlan brian at sweetapp.com
Mon May 13 20:45:52 EDT 2002


> d = lambda x: x < 10
> d = lambda x: d(x) and (x %2 == 0)
> 
> won't work and will end up in an infinite recursion (as far as I can
> figure out).

I really don't understand your objective, so my advice isn't as
strategic as it could be...

Two (untested) ways of doing this immediately occur. The most obvious
is:

c = lambda x: x < 10
d = lambda x: c(x) and (x %2 == 0)

Another way is:

d = lambda x: x < 10
def d(x, d=d): return d(x) and (x % 2 == 0)

> How do you do something like that in python?

The basic problem with your original approach is that you were assuming
that name resolution is done at compile time, when it is actually done
at runtime.

Cheers,
Brian






More information about the Python-list mailing list