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.

classification
Title: Multidimensional comprehensions cannot access class variables except in the first for
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.5
process
Status: closed Resolution: duplicate
Dependencies: Superseder: Comprehensions in a class definition mostly cannot access class variable
View: 11796
Assigned To: Nosy List: fengyang.wang
Priority: normal Keywords:

Created on 2017-06-05 23:56 by fengyang.wang, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg295220 - (view) Author: Fengyang Wang (fengyang.wang) Date: 2017-06-05 23:56
Reduced reproduction:

class X:
    r = [1, 2, 3]
    z = [(i, j) for i in [4, 5] for j in r]

fails with "NameError: name 'r' is not defined". The expected behavior would be for r to be resolved as the r in the class namespace. Note in contrast that

class Y:
    r = [1, 2, 3]
    z = [(i, j) for j in r for i in [4, 5]]

does not fail.

(Version 3.5.2 on Windows)
msg295221 - (view) Author: Fengyang Wang (fengyang.wang) Date: 2017-06-05 23:57
By the way, in Python 2.7 at least, this was working.
msg295223 - (view) Author: Fengyang Wang (fengyang.wang) Date: 2017-06-06 00:06
It looks like this is a duplicate of http://bugs.python.org/issue11796

Sorry for the noise.
History
Date User Action Args
2022-04-11 14:58:47adminsetgithub: 74762
2017-06-06 00:13:57zach.waresetstatus: open -> closed
superseder: Comprehensions in a class definition mostly cannot access class variable
stage: resolved
2017-06-06 00:06:17fengyang.wangsetresolution: duplicate
messages: + msg295223
2017-06-05 23:57:48fengyang.wangsetmessages: + msg295221
2017-06-05 23:56:16fengyang.wangcreate