Closures/Value capture: Difference between revisions

Content added Content deleted
Line 624: Line 624:
print funcs[3]() # prints 9</lang>
print funcs[3]() # prints 9</lang>


It is also possible to use <code>eval</code> and eliminate the double function
It is also possible to use <code>eval</code>.
<lang python>funcs=[eval("lambda:%s"%i**2)for i in range(10)]
<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>