◐ Shell
reader mode source ↗

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author mark.dickinson
Recipients Alex Mashianov, mark.dickinson
Date 2018-08-27.10:00:14
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1535364014.92.0.56676864532.issue34517@psf.upfronthosting.co.za>
In-reply-to
Content
[I wrote this before Serhiy posted his reply; sending it anyway, in case the doc links are useful.]

This is documented, by-design behaviour. Essentially name resolution skips class scopes.

See https://docs.python.org/3/reference/executionmodel.html#resolution-of-names, and particularly the example at the end of that section, which is very similar to your example.

See also: https://docs.python.org/3/reference/expressions.html#displays-for-lists-sets-and-dictionaries, and in particular this part:

> However, aside from the iterable expression in the leftmost for clause,
> the comprehension is executed in a separate implicitly nested scope.
> This ensures that names assigned to in the target list don’t “leak” into
> the enclosing scope.

That "leftmost for clause" bit explains why, in the following example, the first comprehension is evaluated successfully, but the second raises a `NameError`.

>>> class A:
...     a = 3
...     b = [x+y for x in range(a) for y in range(3)]  # okay
...     c = [x+y for x in range(3) for y in range(a)]  # raises
History
Date User Action Args
2018-08-27 10:00:14mark.dickinsonsetrecipients: + mark.dickinson, Alex Mashianov
2018-08-27 10:00:14mark.dickinsonsetmessageid: <1535364014.92.0.56676864532.issue34517@psf.upfronthosting.co.za>
2018-08-27 10:00:14mark.dickinsonlinkissue34517 messages
2018-08-27 10:00:14mark.dickinsoncreate