[issue45899] NameError on if clause of class-level list comprehension

2021-11-25 Thread Matthew Barnett


Matthew Barnett  added the comment:

It's not just in the 'if' clause:

>>> class Foo:
... a = ['a', 'b']
... b = ['b', 'c']
... c = [b for x in a]
...
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 4, in Foo
  File "", line 4, in 
NameError: name 'b' is not defined

--
nosy: +mrabarnett

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45899] NameError on if clause of class-level list comprehension

2021-11-25 Thread Mark Dickinson

Mark Dickinson  added the comment:

This is expected behaviour. See the docs here: 
https://docs.python.org/3.9/reference/executionmodel.html#resolution-of-names

> The scope of names defined in a class block is limited to the class block; it 
> does not extend to the code blocks of methods – this includes comprehensions 
> and generator expressions since they are implemented using a function scope.

--
nosy: +mark.dickinson
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45899] NameError on if clause of class-level list comprehension

2021-11-25 Thread ThiefMaster


Change by ThiefMaster :


--
nosy: +ThiefMaster

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue45899] NameError on if clause of class-level list comprehension

2021-11-25 Thread jhpfjyne


New submission from jhpfjyne :

Accessing an attribute defined at class-level in the if clause of a list 
comprehension at class-level throws a NameError.

>>> class Foo:
... a = ['a', 'b']
... b = ['b', 'c']
... c = [x for x in a if x not in b]
...
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 4, in Foo
  File "", line 4, in 
NameError: name 'b' is not defined

--
components: Interpreter Core
messages: 407002
nosy: jhpfjyne
priority: normal
severity: normal
status: open
title: NameError on if clause of class-level list comprehension
type: behavior
versions: Python 3.10

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com