Confused about closures and scoping rules

Fernando Perez at
Wed Nov 7 01:59:17 CET 2007

Diez B. Roggisch wrote:

> It's a FAQ. The reason is that the created closures don't capture the
> _value_, but the _name_. Plus of course the locals()-dictionary outside
> the function a to perform the lookup of that name. Which has the value
> bound to it in the last iteration.
> Common cure for this is to create an a-local name that shadows the outer
> variable and is simultaneously bound to the desired value:

Many thanks (also to JP) for the clear explanation.  Greatly appreciated.



More information about the Python-list mailing list