Something is rotten in Denmark...
Ian Kelly
ian.g.kelly at gmail.com
Tue May 31 18:24:27 EDT 2011
On Tue, May 31, 2011 at 2:18 PM, harrismh777 <harrismh777 at charter.net> wrote:
> If I'm understanding that correctly, then that means lambda is working as
> designed, and that there are very subtle nuances to be aware of. In my
> little case
>
> (lambda n: i + n)
>
> ... if the i goes out of scope before the anonymous function gets called
> then we have a problem... or if i as a reference is mutable or refers to
> a different object before the anonymous function is called then we have a
> problem?
Actually, if i merely goes out of scope, there is no problem. It just
creates a closure. It's only when the i within that scope is modified
that we run into problems.
In fact, in Python 3 the scope of a list comprehension variable is the
list comprehension itself, so in your original post i was already out
of scope by the time you started calling the lambda functions.
Cheers,
Ian
More information about the Python-list
mailing list