a lambda in a function

Fred Clare fred at balzac.scd.ucar.edu
Wed Dec 12 23:12:03 CET 2001


Why does interpreting the five lines:


def func():
  x = 1
  add_one = lambda i: i+x
  j = add_one(100)
func()


Give me:

  Traceback (most recent call last):
    File "test.py", line 6, in ?
      func()
    File "test.py", line 4, in func
      j = add_one(100)
    File "test.py", line 3, in <lambda>
      add_one = lambda i: i+x
  NameError: There is no variable named 'x'


while interpreting the three lines:


x = 1
add_one = lambda i: i+x
j = add_one(100)

works just fine?


---------------------------------------------
Fred Clare     fred at ucar.edu     303-497-1284
Visualization & Enabling Technologies Section
NCAR Scientific Computing Division
---------------------------------------------



More information about the Python-list mailing list