cpython icon indicating copy to clipboard operation
cpython copied to clipboard

NameError in class definition using list comprehension with if statement

Open matesken opened this issue 2 years ago • 0 comments

This code works

class A:
    data = ' '
    remains = [c for c in data]

This code gives us NameError: name 'data' is not defined

class A:
    data = ' '
    remains = [c for c in data if c in data]

Is there any valid reason why this happens (running Python 3.9)?

matesken avatar Nov 09 '22 16:11 matesken