[issue7423] nested generator expression produces strange results

R. David Murray report at bugs.python.org
Tue Feb 26 21:38:42 CET 2013


R. David Murray added the comment:

I hear what you are saying, but the "generalization" does not mean that they work exactly the same way.  The whole point of generators is that execution is lazy, which is what leads to the difference in behavior.  

The generator function *is* closed over the free variables.  That's what leads to the difference in behavior: the generator uses the value the free variable has when the generator executes.  I don't believe there is any practical way to implement what you are suggesting, even if we wanted to...which we would not be, since it would constitute a backward incompatible change in behavior.

Note also that this behavior of closures is not unique to Python.  See for example http://www.javascriptkit.com/javatutors/closures2.shtml.

----------

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


More information about the Python-bugs-list mailing list