problem with lambda / closures

Jussi Piitulainen jpiitula at ling.helsinki.fi
Tue Dec 1 07:20:58 EST 2009


Terry Reedy writes:

> definitions. Lambda expressions create functions just like def
> statements and are not closures and do not create closure unless
> nested within another function definition. Thinking otherwise is

Seems quite closed in the top level environment to me:

   Python 2.3.4 (#1, Jul 16 2009, 07:03:37) 
   >>> k = 'outer'
   >>> f = lambda : k
   >>> def test():
   ...   k = 'inner'
   ...   return f()
   ... 
   >>> test()
   'outer'
   >>> k = 'thing'
   >>> test()
   'thing'



More information about the Python-list mailing list