python 2.3's lambda behaves old fashioned
Terry Reedy
tjreedy at udel.edu
Thu Apr 29 13:53:00 EDT 2004
"Uwe Schmitt" <uwe.schmitt at procoders.net> wrote in message
news:c6r3qo$1574k$1 at 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
More information about the Python-list
mailing list