Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Mix lambda and list comprehension? (http://www.velocityreviews.com/forums/t319693-mix-lambda-and-list-comprehension.html)

Peter Barth 07-15-2003 07:02 AM

Mix lambda and list comprehension?
 
Hi,
trying to mix lambda expresions and list comprehension
doesn't seem to work.
---
>>> [lambda x:x+y for y in range(10)][9](2)

11
>>> [lambda x:x+y for y in range(10)][4](2)

11
---
I expected the second expression to return 6.
What did I do wrong? Any hints?
Thanks
- Peter

Raymond Hettinger 07-15-2003 07:09 AM

Re: Mix lambda and list comprehension?
 
Try this:

>>> [lambda x, y=y:x+y for y in range(10)][4](2)

6


It is important to bind y in a closure at the time
the lambda is defined. Otherwise, y remains unbound
until you invoke the function call. At that time, the most
recent value of y is the last value in the range loop (namely, 9).


Raymond Hettinger



"Peter Barth" <peter.barth@t-online.de> wrote in message
news:6f5b3f88.0307142302.1a1531f3@posting.google.c om...
> Hi,
> trying to mix lambda expresions and list comprehension
> doesn't seem to work.
> ---
> >>> [lambda x:x+y for y in range(10)][9](2)

> 11
> >>> [lambda x:x+y for y in range(10)][4](2)

> 11
> ---
> I expected the second expression to return 6.
> What did I do wrong? Any hints?
> Thanks
> - Peter




Michele Simionato 07-15-2003 12:41 PM

Re: Mix lambda and list comprehension?
 
peter.barth@t-online.de (Peter Barth) wrote in message news:<6f5b3f88.0307142302.1a1531f3@posting.google. com>...
> Hi,
> trying to mix lambda expresions and list comprehension
> doesn't seem to work.
> ---
> >>> [lambda x:x+y for y in range(10)][9](2)

> 11
> >>> [lambda x:x+y for y in range(10)][4](2)

> 11
> ---
> I expected the second expression to return 6.
> What did I do wrong? Any hints?
> Thanks
> - Peter


It is a scope issue. The last value for y is used for all
the created lambdas. All lambdas users are bitten by that,
soon or later. The solution is to make y local to the
lambda function, with the optional argument trick:

>>> [lambda x,y=y:x+y for y in range(10)][4](2)

6

Michele

Peter Barth 07-15-2003 03:32 PM

Re: Mix lambda and list comprehension?
 
Thanks a lot, works fine.
However, the solution does not really feel "pythonesque".
Is it considered a usability bug or fine as is?
- Peter

"Raymond Hettinger" <vze4rx4y@verizon.net> wrote in message news:<hMNQa.1134$I4.740@nwrdny01.gnilink.net>...
> Try this:
>
> >>> [lambda x, y=y:x+y for y in range(10)][4](2)

> 6
>
>
> It is important to bind y in a closure at the time
> the lambda is defined. Otherwise, y remains unbound
> until you invoke the function call. At that time, the most
> recent value of y is the last value in the range loop (namely, 9).
>
>
> Raymond Hettinger
>
>
>
> "Peter Barth" <peter.barth@t-online.de> wrote in message
> news:6f5b3f88.0307142302.1a1531f3@posting.google.c om...
> > Hi,
> > trying to mix lambda expresions and list comprehension
> > doesn't seem to work.
> > ---
> > >>> [lambda x:x+y for y in range(10)][9](2)

> 11
> > >>> [lambda x:x+y for y in range(10)][4](2)

> > 11
> > ---
> > I expected the second expression to return 6.
> > What did I do wrong? Any hints?
> > Thanks
> > - Peter


Michele Simionato 07-16-2003 11:20 PM

Re: Mix lambda and list comprehension?
 
bokr@oz.net (Bengt Richter) wrote in message news:<bf3pjm$gsl$0@216.39.172.122>...
> On 15 Jul 2003 05:41:02 -0700, mis6@pitt.edu (Michele Simionato) wrote:
>
> >peter.barth@t-online.de (Peter Barth) wrote in message news:<6f5b3f88.0307142302.1a1531f3@posting.google. com>...
> >> Hi,
> >> trying to mix lambda expresions and list comprehension
> >> doesn't seem to work.
> >> ---
> >> >>> [lambda x:x+y for y in range(10)][9](2)

> 11
> >> >>> [lambda x:x+y for y in range(10)][4](2)
> >> 11
> >> ---
> >> I expected the second expression to return 6.
> >> What did I do wrong? Any hints?
> >> Thanks
> >> - Peter

> >
> >It is a scope issue. The last value for y is used for all
> >the created lambdas. All lambdas users are bitten by that,
> >soon or later. The solution is to make y local to the
> >lambda function, with the optional argument trick:
> >
> >>>> [lambda x,y=y:x+y for y in range(10)][4](2)

> >6

>
> or you could capture y as constants in the lambdas ;-)
>
> >>> [eval('lambda x:x+%s'%y) for y in range(10)][4](2)

> 6
>
>
> Regards,
> Bengt Richter


Thanks to God, there is a smile in your post!!

;)

Michele


All times are GMT. The time now is 04:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.