[docs] [issue13094] Need Programming FAQ entry for the behavior of closures

Ezio Melotti report at bugs.python.org
Mon Oct 3 17:03:19 CEST 2011


Ezio Melotti <ezio.melotti at gmail.com> added the comment:

To understand better what's going on, try to change the value of 'each' after the 3 prints and then call again the 3 methods: you will see that they now return the new value of each.  This is because the lambdas refer to global 'each' (that at the end of the loop is set to 'baz').
If you do setattr(x, each, lambda each=each: each), the each will be local to the lambda, and it will then work as expected.

An entry in the FAQ would be useful, I thought it was there already but apparently it's not (I'm pretty sure I saw this already somewhere in the doc, but I can't seem to find where).

----------
assignee: docs at python -> 
components: +None -Documentation
nosy: +ezio.melotti
stage: needs patch -> 
type: behavior -> 
versions:  -Python 3.2, Python 3.3

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue13094>
_______________________________________


More information about the docs mailing list