Message220375
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. |
|
Date |
User |
Action |
Args |
2014-06-12 19:51:52 | r.david.murray | set | recipients:
+ r.david.murray, docs@python, krichter |
2014-06-12 19:51:51 | r.david.murray | set | messageid: <1402602711.96.0.407581429057.issue21739@psf.upfronthosting.co.za> |
2014-06-12 19:51:51 | r.david.murray | link | issue21739 messages |
2014-06-12 19:51:51 | r.david.murray | create | |
|