Lambda added to release 2.2 ?

Martin v. Löwis loewis at informatik.hu-berlin.de
Sun Apr 14 13:03:58 EDT 2002


Fabien Hénon <ffjhenon at club-internet.fr> writes:

> When I use the lines below :
> >    insertmenumaterial.add_command(label="interior", command = lambda :
> self.insert_example("interior.txt"))

I assume your lines look more like

  def foo(self):
    insertmenumaterial.add_command(label="interior", 
         command = lambda : self.insert_example("interior.txt"))

i.e. the insertmenumaterial is inside a method.

Notice that the lambda function itself does not take any arguments.

> >>> NameError: global name 'self' is not defined

That is the traditional way of Python: since the lambda is a function,
it has only access to its local and global variables. self is neither
local nor global, hence 'self' is not a name of any variable.

> I don't have any problem under W2000 ( Python 2.2)
> Is lambda function a new add-on to 2.2 ? I thought it was added ages
> ago.

In Python 2.2, nested function scopes where added, so that the nested
lambda function can access the variables of the outer function. This
is not directly available in Python 2.1. Add

from __future__ import nested_scopes

to the top of the file to make it work correctly with Python 2.1, and
fail immediately with Python 2.0 and earlier.

Regards,
Martin



More information about the Python-list mailing list