problem with lambda / closures

Terry Reedy tjreedy at udel.edu
Mon Nov 30 17:53:38 EST 2009


Benjamin Kaplan wrote:

> I don't know if anyone considers python's incomplete implementation of
> closures a "feature" but it's documented so it's not really a bug
> either.

I believe Python's implementation of closures is quite complete in 3.x. 
In what way do you consider it otherwise? One just has to use the right 
syntax. Closures in Python are created by nested function definitions. 
Lambda expressions create functions just like def statements and are not 
closures and do not create closure unless nested within another function 
definition. Thinking otherwise is the OP's mistake.

> I believe there is a trick with default arguments to get this
> to work, but I don't use lambdas enough to remember it.

One can simulate closures by giving an un-nested function a default 
argument. This has nothing to do with whether the function is defined by 
a lambda expression or a def statement.

Terry Jan Reedy





More information about the Python-list mailing list