closures and dynamic binding

Michele Simionato michele.simionato at gmail.com
Mon Sep 29 10:33:01 EDT 2008


On Sep 28, 6:43 am, "Aaron \"Castironpi\" Brady"
<castiro... at gmail.com> wrote:
> Hello all,
>
> To me, this is a somewhat unintuitive behavior.  I want to discuss the
> parts of it I don't understand.
>
> >>> f= [ None ]* 10
> >>> for n in range( 10 ):
>
> ...     f[ n ]= lambda: n
> ...>>> f[0]()
> 9
> >>> f[1]()
>
> 9
>

You may want to read this old thread that goes in detail explaining
scoping issues
with closures in for loops:

http://groups.google.com/group/comp.lang.python/browse_frm/thread/d691240a5cfebcdf/93503c5b9c66226e?hl=en&lnk=gst&q=simionato+jacek#93503c5b9c66226e




More information about the Python-list mailing list