Help with lambda

lallous elias.bachaalany at gmail.com
Thu Feb 18 13:28:00 CET 2010


Hello,

I am still fairly new to Python. Can someone explain to me why there
is a difference in f and g:

def make_power(n):
    return lambda x: x ** n

# Create a set of exponential functions
f = [lambda x: x ** n for n in xrange(2, 5)]
g = [make_power(n) for n in xrange(2, 5)]

print f[0](3), f[1](3)
print g[0](3), g[1](3)


I expect to have "f" act same like "g".

Thanks



More information about the Python-list mailing list