[Tutor] lambda problem

Borgulya Gabor borgulya@pons.sote.hu
Thu, 6 Apr 2000 18:02:09 +0200 (CEST)


Dear Tutors,

Isn't it possible to use a lambda inside a lamda?
I demonstrate what I want to do:

1. without "lambda in lambda":
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> a=map(lambda x:x+1, [30,40])
>>> a
[31, 41]
>>> b=map(lambda x:x+2, [30,40])
>>> b
[32, 42]
>>> c=map(lambda x:x+3, [30,40])
>>> c
[33, 43]
>>> li=[a,b,c]
>>> li
[[31, 41], [32, 42], [33, 43]]


2. With a "lambda in lambda" shortcut:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>> li=map(lambda y:map(lambda x:x+y, [30,40]),[1,2,3])
Traceback (innermost last):
  File "<pyshell#18>", line 1, in ?
    li=map(lambda y:map(lambda x:x+y, [30,40]),[1,2,3])
  File "<pyshell#18>", line 1, in <lambda>
    li=map(lambda y:map(lambda x:x+y, [30,40]),[1,2,3])
  File "<pyshell#18>", line 1, in <lambda>
    li=map(lambda y:map(lambda x:x+y, [30,40]),[1,2,3])
NameError: y

The inner lambda seems not to recognize the variable y defined by the
outer lambda.  Do I want something impossible or do I misunderstand how
lambda is functionning? 

Gabor