python 2.3's lambda behaves old fashioned

U

Uwe Schmitt

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.
 
Y

Yermat

Uwe said:
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=f...m=tyfn0696x8w.fsf%40pcepsft001.cern.ch&rnum=2
(search "lambda default group:comp.lang.python" on google, thread of the
6 jan 2004)

or at :
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&th=4bca1bec20119375&rnum=6

hint: this is a matter of scope...
 
M

Michele Simionato

Uwe Schmitt said:
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=i...o+scope+rules&meta=group%3Dcomp.lang.python.*

Michele Simionato
 
T

Terry Reedy

Uwe Schmitt said:
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
 
F

Fredrik Lundh

Uwe said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top