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 4 3016
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" <pe*********@t-online.de> wrote in message
news:6f**************************@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 pe*********@t-online.de (Peter Barth) wrote in message news:<6f**************************@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
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" <vz******@verizon.net> wrote in message news:<hM***************@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" <pe*********@t-online.de> wrote in message news:6f**************************@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
bo**@oz.net (Bengt Richter) wrote in message news:<bf**********@216.39.172.122>... On 15 Jul 2003 05:41:02 -0700, mi**@pitt.edu (Michele Simionato) wrote:
pe*********@t-online.de (Peter Barth) wrote in message news:<6f**************************@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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Chas Emerick |
last post by:
In a comment off a post on lambda-the-ultimate, I noticed this little
offhand remark:
'''IIRC GvR is going to kill the various lambda, map,filter & reduce
leaving just generator expressions/list...
|
by: Oliver Fromme |
last post by:
Hi,
I'm trying to write a Python function that parses
an expression and builds a function tree from it
(recursively).
During parsing, lambda functions for the the terms
and sub-expressions...
|
by: Steven Bethard |
last post by:
I thought it might be useful to put the recent lambda threads into
perspective a bit. I was wondering what lambda gets used for in "real"
code, so I grepped my Python Lib directory. Here are some...
|
by: Tom Anderson |
last post by:
Comrades,
During our current discussion of the fate of functional constructs in
python, someone brought up Guido's bull on the matter:
http://www.artima.com/weblogs/viewpost.jsp?thread=98196
...
|
by: Mike Meyer |
last post by:
I know, lambda bashing (and defending) in the group is one of the most
popular ways to avoid writing code. However, while staring at some Oz
code, I noticed a feature that would seem to make both...
|
by: jena |
last post by:
hello,
when i create list of lambdas:
l=]
then l() returns 'C', i think, it should be 'A'
my workaround is to define helper class with __call__ method:
class X:
def __init__(self,s): self.s=s...
|
by: Cameron Laird |
last post by:
QOTW: "... and to my utter surprise it worked." - Andrew Nagel on
his move from wxPython to programming Tkinter in desperation
"Python has more web application frameworks than keywords." - Skip...
|
by: sofeng |
last post by:
I would like to use the following recipe to transpose a list of lists
with different lengths. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/410687
Here is an example of what I would...
|
by: globalrev |
last post by:
i have a rough understanding of lambda but so far only have found use
for it once(in tkinter when passing lambda as an argument i could
circumvent some tricky stuff).
what is the point of the...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |