Lambda?!

klamidad klamidad at wanadoo.es
Tue Sep 10 20:58:48 EDT 2002


Bo M. Maryniuck wrote:

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

Python 2.1.3 (#1, Jul 29 2002, 22:34:51) 
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> class foo:
...     def bar(self, a):
...             return "<%s>" % a
...     def spam(self):
...             return map(lambda rope: self.bar(rope).upper(), 
['test', 'other test'])
... 
<stdin>:4: SyntaxWarning: local name 'self' in 'spam' shadows use of 
'self' as global in nested scope 'lambda'
>>> f = foo()
>>> f.spam()
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<stdin>", line 5, in spam
  File "<stdin>", line 5, in <lambda>
NameError: global name 'self' is not defined

'self' local in 'spam' <---> 'self' global in 'lambda'

Python 2.1.3 (#1, Jul 29 2002, 22:34:51) 
[GCC 2.95.4 20011002 (Debian prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
>>> class foo:
...     def bar(self, a):
...             return "<%s>" % a
...     def spam(self):
...             return map(lambda rope,slf=self: slf.bar(rope).upper(), 
['test', 'other test'])
... 
>>> f = foo()
>>> f.spam()
['<TEST>', '<OTHER TEST>']

Ahh!

 



More information about the Python-list mailing list