By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,714 Members | 750 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,714 IT Pros & Developers. It's quick & easy.

python 2.3's lambda behaves old fashioned

P: n/a

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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.