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
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**
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
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
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
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
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
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[