closures and dynamic binding

jhermann Juergen.Hermann at 1und1.de
Wed Oct 1 06:43:49 EDT 2008


I didn't see this mentioned in the thread yet: the double-lambda is
unnecessary (and a hack). What you should do when you need early
binding is... early binding. ;)

Namely:

f = [lambda n=n: n for n in range(10)]
print f[0]()
print f[1]()

Note the "n=n", this prints 0 and 1 instead of 9/9.



More information about the Python-list mailing list