Jonathan Gardner wrote:
On Feb 18, 4:28 am, lallous elias.bachaal...@gmail.com 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 =
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),
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
lallous elias.bachaal...@gmail.com 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 =
Yes it should be listed somewhere, now I get it. Thanks Arnaud.
--
Elias
On Feb 18, 1:47 pm, Arnaud Delobelle arno...@googlemail.com wrote:
lallous elias.bachaal...@gmail.com writes:
Hello,
I am still fairly new to Python. Can someone explain to me why there
is a difference in f and g:
On Thu, 18 Feb 2010 04:28:00 -0800 (PST)
lallous elias.bachaal...@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 =
On Feb 18, 1:56 pm, D'Arcy J.M. Cain da...@druid.net wrote:
On Thu, 18 Feb 2010 04:28:00 -0800 (PST)
lallous elias.bachaal...@gmail.com 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
On Feb 18, 4:28 am, lallous elias.bachaal...@gmail.com 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**n)