Title: generator expression doesn't find subclass
Type: Stage: resolved
Components: Versions: Python 3.7
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 2019-07-19 18:37 by serhiy.storchaka. 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 "", line 6, in <module>
    class A:
  File "", line 11, in A
    l = [B(i) for i in range(10)]
  File "", 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 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.
Date User Action Args
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