473,594 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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*****@procode rs.net "A service to open source is a service to mankind."
Jul 18 '05 #1
4 1259
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*********@pr ocoders.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*********@pr ocoders.net> wrote in message
news:c6******** ****@hades.rz.u ni-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
2829
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 Vainio http://www.students.tut.fi/~vainio24
14
2842
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" in the subject of any post to this newsgroup. Please ignore the old thread. I've also updated my webpage http://ece.arizona.edu/~edatools/Python Anyone with some good ideas for "Python 3" is welcome to contribute. I hope GvR won't sue me for...
21
1604
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 download it, kick the tires and let us know of any problems you find, but it is not suitable for production usage. http://www.python.org/2.4/ In this release we have a number of new modules, a number of existing
48
2730
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 suggestions?
26
2698
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 locally-scoped-together code, in Ruby a "block" defines a closure (anonymous function). To avoid confusion let's call them Ruby block-closures. I see them as just a syntax for defining
852
28240
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 my general education. Mark
0
4548
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 ( -8) / 278 closed (+12) / 527 total ( +4) New / Reopened Patches ______________________
3
4233
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 csv. I've got the results coming back fine, but since I have *no* experience with python I'm wondering what would be the preferred "pythonic" way of parsing the whois string into a csv record. Tips/thoughts/examples more than welcome!
0
7874
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8246
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8368
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8000
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8231
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5738
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3895
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1205
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.