python 2.3's lambda behaves old fashioned
Michele Simionato
michele.simionato at poste.it
Thu Apr 29 13:30:58 EDT 2004
Uwe Schmitt <uwe.schmitt at procoders.net> wrote in message news:<c6r3qo$1574k$1 at 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&lr=&ie=UTF-8&oe=UTF-8&threadm=95aa1afa.0402272327.29828430%40posting.google.com&rnum=1&prev=/groups%3Fhl%3Dit%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26q%3Dsimionato%2Bscope%2Brules%26meta%3Dgroup%253Dcomp.lang.python.*
Michele Simionato
More information about the Python-list
mailing list