News for hackers. Stuff that matters.
Re:The scope of index variables in Python's for loops by rhizome31 on Sunday January 18, 2015 @03:42PM (#8908790)
You can capture the index with a keyword argument default value:

        def foo():
            lst = []
            for i in range(4):
                lst.append(lambda i=i: i)
            print([f() for f in lst])
FWIW I played around with this issue in various languages in this blog post: https://my.smeuh.org/al/blog/lost-in-scope Once you get the difference between block scope and function scope, it's quite easy to see what's happening.

There are no comments on this comment yet.