lambda - strange behavior

Kasper Guldmann gmane at kalleguld.dk
Fri Sep 20 11:21:20 EDT 2013


I was playing around with lambda functions, but I cannot seem to fully grasp
them. I was running the script below in Python 2.7.5, and it doesn't do what
I want it to. Are lambda functions really supposed to work that way. How do
I make it work as I intend?

f = []
for n in range(5):
    f.append( lambda x: x*n )

assert( f[4](2) == 8 )
assert( f[3](3) == 9 )
assert( f[2](2) == 4 )
assert( f[1](8) == 8 )
assert( f[0](2) == 0 )




More information about the Python-list mailing list