[issue11796] Comprehensions in a class definition mostly cannot access class variable

Raymond Hettinger report at bugs.python.org
Sat Apr 9 02:42:52 CEST 2011


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

> Devs are aware that there is an exception to the general rule
>  for the 'for' clause. There is a technical reason why the 
> exception is possible, though I have forgotten it.

It is best understood when thinking about a gexexp that
gets run long after is created:

   ge = (result_exp(loop_var) for loop_var in iter_exp)
 
The idea was to have the body of the iterator expression, iter_exp, fail early, before the generator is run and while the local context is still set=up and available:

   ge = (1/0 for i in pow('a', 'b'))

We want the TypeError for pow to be raised immediately.  And if a valid expression were evaluated, we would want the body's ZeroDivisionError to be raised only when the generator is invoked using next(ge()).

In the former case, the local context is still available.  In the latter case, it could be long gone.

----------

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


More information about the Python-bugs-list mailing list