
February 22nd, 2004, 06:10 AM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: London, England
|
|
You can get it to work the way you want by passing in i as a default parameter, i.e.:
Code:
>>> closures = [lambda x, i=i: i*x for i in range(10)]
>>> closures[1](2)
2
>>> closures[4](2)
8
This was the standard way to simulate closures in Python prior to 2.1, when it did not have the new scoping rules.
Dave - The Developers' Coach
|