Lambda?!

Denis S. Otkidach ods at fep.ru
Tue Aug 20 16:22:50 CEST 2002


On Tue, 20 Aug 2002, Bo M. Maryniuck wrote:

BMM> Python 2.0 (#1, Jan 19 2001, 17:54:27)
BMM> [GCC 2.95.2 19991024 (release)] on linux2
BMM> Type "copyright", "credits" or "license" for more
BMM> information.
BMM> >>> class foo:
BMM> ...     def bar(self, a):
BMM> ...             return "<%s>" % a
BMM> ...     def spam(self):
BMM> ...             return map(lambda rope:
BMM> self.bar(rope).upper(), ['test', 'other test'])
BMM> ...
BMM> >>> f = foo()
BMM> >>> f.spam()
BMM> Traceback (most recent call last):
BMM>   File "<stdin>", line 1, in ?
BMM>   File "<stdin>", line 5, in spam
BMM>   File "<stdin>", line 5, in <lambda>
BMM> NameError: There is no variable named 'self'

"Nested scopes" feature appeared in 2.1 (if "from __future__
import nested_scopes" used).  You should explicitly pass self to
lambda in earlier versions:

return map(lambda rope, self=self: self.bar(rope).upper(),
['test', 'other test'])

-- 
Denis S. Otkidach
http://www.python.ru/      [ru]
http://diveinto.python.ru/ [ru]





More information about the Python-list mailing list