Re: lambda strangeness??

2005-02-28 Thread Alan Gauld
On Sun, 27 Feb 2005 09:39:49 GMT, Roel Schroeven
[EMAIL PROTECTED] wrote:

 adds = [lambda y: (y + n) for n in range(10)]

 You're picking it up not as y but as n, since n in the lambda is
 evaluated when you call the lambde, not when you define it.

  for n in range(10):
   def f(y, n=n): return y+n
   adds.append(f)

 adds = [lambda x, y=n: (x + y) for n in range(10)]

Works perfectly, thanks Roel.
I knew I'd got this to work in the past with a for loop
so I knew it should be possible in a comprehension. I
just forgot the default argument trick! And then confused myself
by coincidentally using n both inside and outside the LC, thus
apparently getting inconsistent results!

Thanks again,

Alan G.

Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lambda strangeness??

2005-02-27 Thread Alan Gauld
On Sun, 27 Feb 2005 09:07:28 + (UTC), Alan Gauld
[EMAIL PROTECTED] wrote:

  adds = [lambda y: (y + n) for n in range(10)]
  adds[0](0)
 9
  for n in range(5): print adds[n](42)
 ...
 42
 43

 the for loop... It seems to somehow be related to the 
 last value in the range(), am I somehow picking that up as y?

Further exploration suggests I'm picking it up as n not y, if
that indeed is what's happening...

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lambda strangeness??

2005-02-27 Thread Roel Schroeven
Alan Gauld wrote:
 I was playing with lambdas and list compregensions and came
 across this unexpected behaviour:
 
 
adds = [lambda y: (y + n) for n in range(10)]
adds[0](0)
 
 9
 
for n in range(5): print adds[n](42)
 
 ...
 42
 43
 44
 45
 46
 
adds[0](0)
 
 4
 
 Can anyone explain the different answers I'm getting?
 FWIW the behaviour I expected was what seems to happen inside 
 the for loop... It seems to somehow be related to the 
 last value in the range(), am I somehow picking that up as y?
 If so why?

You're picking it up not as y but as n, since n in the lambda is
evaluated when you call the lambde, not when you define it.

Or is that just a coincidence? And why did it work
 inside the for loop?

In the loop you are giving n exactly the values you intended it to have
inside the lambda. Check what happens when you use a different loop
variable:

 for i in range(5): print adds[i](0)

9
9
9
9
9

I guess you could something like this instead:

 adds=[]
 for n in range(10):
def f(y, n=n): return y+n
adds.append(f)


 adds[0](0)
0
 adds[0](5)
5
 adds[9](5)
14

-- 
Codito ergo sum
Roel Schroeven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lambda strangeness??

2005-02-27 Thread Roel Schroeven
Alan Gauld wrote:
 On Sun, 27 Feb 2005 09:07:28 + (UTC), Alan Gauld
 [EMAIL PROTECTED] wrote:
 
 
adds = [lambda y: (y + n) for n in range(10)]
adds[0](0)

9

for n in range(5): print adds[n](42)

...
42
43
 
 
the for loop... It seems to somehow be related to the 
last value in the range(), am I somehow picking that up as y?
 
 
 Further exploration suggests I'm picking it up as n not y, if
 that indeed is what's happening...

Your intent is to create lambda's that are equivalent to

adds[0] = lambda y: y + 0
adds[1] = lambda y: y + 1
etc.

but what actually happens is that you get lambda's that are equivalent to

adds[0] = lambda y: y + n
adds[1] = lambda y: y + n
etc.

which obviously depend on the value of n at the moment you call the lambda.

-- 
Codito ergo sum
Roel Schroeven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: lambda strangeness??

2005-02-27 Thread Terry Reedy

Alan Gauld [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 adds = [lambda y: (y + n) for n in range(10)]

To bind the definition-time value of n to the function,
[lambda y,n=n:(y+n) for n in range(10)]

Terry J. Reedy




-- 
http://mail.python.org/mailman/listinfo/python-list