Help with lambda

D'Arcy J.M. Cain darcy at druid.net
Thu Feb 18 13:56:03 CET 2010


On Thu, 18 Feb 2010 04:28:00 -0800 (PST)
lallous <elias.bachaalany at gmail.com> wrote:
> def make_power(n):
>     return lambda x: x ** n

Hint: type(make_power(2))

Did you expect that to return "int"?

> # 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)]

The result of make_power(n) is a function that raises it's argument to
the power of n.  I don't know what you are trying to do.  Maybe this?

g = [make_power(n)(2) for n in xrange(2, 5)]
or
g = [make_power(2)(n) for n in xrange(2, 5)]

-- 
D'Arcy J.M. Cain <darcy at druid.net>         |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.



More information about the Python-list mailing list