python 2.3's lambda behaves old fashioned

Yermat loic at fejoz.net
Thu Apr 29 11:04:41 EDT 2004


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&lr=&ie=UTF-8&oe=UTF-8&threadm=tyfn0696x8w.fsf%40pcepsft001.cern.ch&rnum=2&prev=/groups%3Fq%3Dlambda%2Bdefault%2Bgroup:comp.lang.python%26hl%3Dfr%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26as_drrb%3Db%26as_mind%3D1%26as_minm%3D1%26as_miny%3D2004%26as_maxd%3D29%26as_maxm%3D4%26as_maxy%3D2004%26selm%3Dtyfn0696x8w.fsf%2540pcepsft001.cern.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=en&lr=&ie=UTF-8&oe=UTF-8&th=4bca1bec20119375&rnum=6

hint: this is a matter of scope...

-- 
Yermat




More information about the Python-list mailing list