Closures/Value capture: Difference between revisions
Content deleted Content added
Line 622: | Line 622: | ||
In this case it is also possible to use <code>map()</code> since the function passed to it creates a new scope |
In this case it is also possible to use <code>map()</code> since the function passed to it creates a new scope |
||
<lang python>funcs = map(lambda i: lambda: i * i, range(10)) |
<lang python>funcs = map(lambda i: lambda: i * i, range(10)) |
||
print funcs[3]() # prints 9</lang> |
|||
It is also possible to use <code>eval</code> and eliminate the double function |
|||
<lang python>funcs=[eval("lambda:%s"%i**2)for i in range(10)] |
|||
print funcs[3]() # prints 9</lang> |
print funcs[3]() # prints 9</lang> |
||