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: generator expression doesn't find subclass
Type: Stage: resolved
Components: Versions: Python 3.7
process
Status: closed Resolution: duplicate
Dependencies: Superseder: improper scope in list comprehension, when used in class declaration
View: 3692
Assigned To: Nosy List: serhiy.storchaka, xtreak, Ákos Tompos
Priority: normal Keywords:

Created on 2019-07-19 15:28 by Ákos Tompos, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (3)
msg348181 - (view) Author: Ákos Tompos (Ákos Tompos) Date: 2019-07-19 15:28
The following code does work on python 2.7 but fails on python 3.7. The code seems correct.

class A:
    class B:
        def __init__(self, param):
            self.param = param
        
    l = [B(i) for i in range(10)]

The result on:
3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    class A:
  File "test.py", line 11, in A
    l = [B(i) for i in range(10)]
  File "test.py", line 11, in <listcomp>
    l = [B(i) for i in range(10)]
NameError: name 'B' is not defined

B can be accessed if not inside a generator expression
msg348184 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-07-19 16:52
I think it's about a class being defined inside another class and not about subclass. This seems to be similar to https://bugs.python.org/issue3692 that discusses about class attribute scope for comprehension.
msg348190 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-07-19 18:37
This is a duplicate of issue3692.
History
Date User Action Args
2022-04-11 14:59:18adminsetgithub: 81813
2019-07-19 18:37:02serhiy.storchakasetstatus: open -> closed

superseder: improper scope in list comprehension, when used in class declaration

nosy: + serhiy.storchaka
messages: + msg348190
resolution: duplicate
stage: resolved
2019-07-19 16:52:59xtreaksetnosy: + xtreak
messages: + msg348184
2019-07-19 15:28:48Ákos Tomposcreate