473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

python 2.3's lambda behaves old fashioned


Hi,

I just tried (Python 2.3)

li = [ lambda x: x*a for a in range(10) ]

which results in

li[0](1) = 9
...
li[9](1) = 9

In order to achieve the intended result I had to fall back on the
following trick:

li = [ lambda x,a=a: x*a for a in range(10)]

which leads to the expected result.

Any explanations ???

Greetings, Uwe.
--
Dr. rer. nat. Uwe Schmitt http://www.procoders.net
sc*****@procoders.net "A service to open source is a service to mankind."
Jul 18 '05 #1
4 1253
Uwe Schmitt wrote:
Hi,

I just tried (Python 2.3)

li = [ lambda x: x*a for a in range(10) ]

which results in

li[0](1) = 9
...
li[9](1) = 9

In order to achieve the intended result I had to fall back on the
following trick:

li = [ lambda x,a=a: x*a for a in range(10)]

which leads to the expected result.

Any explanations ???

Greetings, Uwe.


exactly like this thread :
http://groups.google.fr/groups?hl=fr....ch%26rnum%3D2
(search "lambda default group:comp.lang.python" on google, thread of the
6 jan 2004)

or at :
http://groups.google.com/groups?hl=e...0119375&rnum=6

hint: this is a matter of scope...

--
Yermat

Jul 18 '05 #2
Uwe Schmitt <uw*********@procoders.net> wrote in message news:<c6************@hades.rz.uni-saarland.de>...
Hi,

I just tried (Python 2.3)

li = [ lambda x: x*a for a in range(10) ]

which results in

li[0](1) = 9
...
li[9](1) = 9

In order to achieve the intended result I had to fall back on the
following trick:

li = [ lambda x,a=a: x*a for a in range(10)]

which leads to the expected result.

Any explanations ???

Greetings, Uwe.


This should be in the FAQ. Here is a recent thread on the subject:

http://groups.google.it/groups?hl=it....lang.python.*

Michele Simionato
Jul 18 '05 #3

"Uwe Schmitt" <uw*********@procoders.net> wrote in message
news:c6************@hades.rz.uni-saarland.de...
I just tried (Python 2.3)
Which did not change anything in this regard...
li = [ lambda x: x*a for a in range(10) ]
which, except for function __name__ and func_name attributes, translates
(unabbreviates) to

li = []
for a in range(10):
def tem(x): return x*a
li.append(tem)
del tem

which is equivalent to

li = []
for a in range(10):
def tem(x): return x*__multiplier
li.append(tem)
__multiplier = a
del a, tem
which results in

li[0](1) = 9
...
li[9](1) = 9


As one would expect from second translation above if not the first.

Its funny how some people expect default parameter objects to be
re-calculated on every call instead of just once, while others sometimes
expect global vars to be evaluated just once instead of on every call ;-)

The question of whether free vars within lambdas within list comps should
get 'early binding' instead of the usual late binding was discussed on
Py-Dev list last fall and some this winter. I believe Guido recently said
no change.

Terry J. Reedy

Jul 18 '05 #4
Uwe Schmitt wrote:
I just tried (Python 2.3)

li = [ lambda x: x*a for a in range(10) ]

which results in

li[0](1) = 9
...
li[9](1) = 9

In order to achieve the intended result I had to fall back on the
following trick:

li = [ lambda x,a=a: x*a for a in range(10)]

which leads to the expected result.

Any explanations ???


repeat after me:

free variables bind to names, default arguments bind to objects.
free variables bind to names, default arguments bind to objects.
free variables bind to names, default arguments bind to objects.
(etc)

</F>


Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville...
14
by: David MacQuigg | last post by:
I am starting a new thread so we can avoid some of the non-productive argument following my earlier post "What is good about Prothon". At Mr. Hahn's request, I will avoid using the name "Prothon"...
21
by: Anthony Baxter | last post by:
On behalf of the Python development team and the Python community, I'm happy to announce the first alpha of Python 2.4. Python 2.4a1 is an alpha release. We'd greatly appreciate it if you could...
48
by: Daniel Crespo | last post by:
Hi! I would like to know how can I do the PHP ternary operator/statement (... ? ... : ...) in Python... I want to something like: a = {'Huge': (quantity>90) ? True : False} Any...
26
by: brenocon | last post by:
Hi all -- Compared to the Python I know and love, Ruby isn't quite the same. However, it has at least one terrific feature: "blocks". Whereas in Python a "block" is just several lines of...
852
by: Mark Tarver | last post by:
How do you compare Python to Lisp? What specific advantages do you think that one has over the other? Note I'm not a Python person and I have no axes to grind here. This is just a question for...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 349 open ( +7) / 3737 closed (+25) / 4086 total (+32) Bugs : 939 open (-12) / 6648 closed (+60) / 7587 total (+48) RFE : 249 open...
3
by: Phillip B Oldham | last post by:
Hi. I'm stretching my boundaries in programming with a little python shell-script which is going to loop through a list of domain names, grab the whois record, parse it, and put the results into a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.