[Python-ideas] For-loop variable scope: simultaneous possession and ingestion of cake

Bruce Leban bruce at leapyear.org
Mon Oct 6 06:47:27 CEST 2008

Um, I think this is more complicated. Consider:

    i = 0
    def f():
         i += 1
         return lambda: i

Now lambda: i is bound to i so every time I call f it will return a function
that returns the current value of i not the one at the time f was called. So
I can fix this by immanetization:

    i = 0
    def f():
         i += 1
         return lambda: immanentize i

This will return lambda: 1, then lambda: 2, etc. right? No. It returns
lambda: 0, lambda: 0, etc.

--- Bruce
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20081005/74098674/attachment.html>

More information about the Python-ideas mailing list