R. David Murray added the comment:

In 3.x a list comprehension (like a generator expression in 2.x) *is* a 
separate block:

>>> [x for x in range(3)]
[0, 1, 2]
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

I note that this is not in fact mentioned in the python3 version of the 
tutorial; there it still implies that it is completely equivalent to the 
unrolled if statement, which would imply it *was* in the same scope.

IMO the 2.7 tutorial is complete in the sense that the "is equivalent to" 
example is clearly in the same scope, while the python3 tutorial is missing a 
note that the comprehension is its own scope.  Given the fact that it is 
*different* between the two, I wonder if it would be appropriate to add a note 
(footnote?) to that effect to the 2.7 tutorial.  There is such a footnote in 
the corresponding part of the language reference.

----------
nosy: +r.david.murray
versions: +Python 3.4, Python 3.5

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue21739>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to