problem with lambda / closures

Marco Mariani marco at sferacarta.com
Mon Nov 30 15:01:51 CET 2009


Louis Steinberg wrote:

> I have run into what seems to be a major bug, but given my short 
> exposure to Python is probably just a feature:


Yes, it works as advertised :-/


> which I would expect.  Can anyone explain this or give me a workaround?  

like this?


> def p(d):
>     print d
> 
> 
> l=[ ]
> for k in [1,2,3]:
>     l.append(lambda k=k: p(k))
> 
> for f in l:
>     f()



More information about the Python-list mailing list