How do I dynamically create functions without lambda?
Gary Herron
gherron at islandtraining.com
Fri Jan 27 17:39:17 EST 2006
Russell wrote:
>I want my code to be Python 3000 compliant, and hear
>that lambda is being eliminated. The problem is that I
>want to partially bind an existing function with a value
>"foo" that isn't known until run-time:
>
> someobject.newfunc = lambda x: f(foo, x)
>
>The reason a nested function doesn't work for this is
>that it is, well, dynamic. I don't know how many times
>or with what foo's this will be done.
>
>
That's nonsense! A lambda function *IS* a nested function, and has *NO*
extra capabilities over a nested function. If you can do something with
a lambda, you can also do it with a nested function defined at the same
point. However, a nested function *does* give you several extra
capabilities: (1) it has a name rather than being anonymous like the
lambda, and (2) the body can use statements rather just one expression.
So use a nested function. You'll get the same capabilities, plus a
name, plus more expressive power in the body. You won't lose anything.
and your code will survive any eventual removal of the lambda functionality.
>Now, I am sure there are a half-dozen ways to do this.
>I just want the one, new and shiny, Pythonic way. ;-)
>
>
>
More information about the Python-list
mailing list