Re: Help with lambda

2010-02-24 Thread News123
Jonathan Gardner wrote: > On Feb 18, 4:28 am, lallous wrote: >> f = [lambda x: x ** n for n in xrange(2, 5)] > > This is (pretty much) what the above code does. > f = [] n = 2 f.append(lambda x: x**n) n = 3 f.append(lambda x: x**n) n = 4 f.append(lambda x: x**n

Re: Help with lambda

2010-02-18 Thread Jonathan Gardner
On Feb 18, 4:28 am, lallous wrote: > > f = [lambda x: x ** n for n in xrange(2, 5)] This is (pretty much) what the above code does. >>> f = [] >>> n = 2 >>> f.append(lambda x: x**n) >>> n = 3 >>> f.append(lambda x: x**n) >>> n = 4 >>> f.append(lambda x: x**n) >>> n = 5 >>> f.append(lambda x: x**

Re: Help with lambda

2010-02-18 Thread lallous
On Feb 18, 1:56 pm, "D'Arcy J.M. Cain" wrote: > On Thu, 18 Feb 2010 04:28:00 -0800 (PST) > > lallous wrote: > > def make_power(n): > >     return lambda x: x ** n > > Hint: type(make_power(2)) > > Did you expect that to return "int"? > No, I expect to see a specialized function. > > # Create a

Re: Help with lambda

2010-02-18 Thread D'Arcy J.M. Cain
On Thu, 18 Feb 2010 04:28:00 -0800 (PST) lallous 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

Re: Help with lambda

2010-02-18 Thread lallous
Yes it should be listed somewhere, now I get it. Thanks Arnaud. -- Elias On Feb 18, 1:47 pm, Arnaud Delobelle wrote: > lallous writes: > > 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 la

Re: Help with lambda

2010-02-18 Thread Arnaud Delobelle
lallous writes: > 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

Re: Help with lambda

2010-02-18 Thread Xavier Ho
I'm looking at your code and was thinking ... why writing code that is difficult to understand? To answer your question though, they're different because in case "f", your lambda experssion is only evaluated once. That means the variable 'n' is ever only created once, and replaced repeatedly. In t

Help with lambda

2010-02-18 Thread lallous
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[