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: NameError with genexp in class scope
Type: behavior Stage:
Components: Interpreter Core Versions: Python 2.5
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, cptnwillard, facundobatista, georg.brandl, gvanrossum, rhettinger
Priority: normal Keywords:

Created on 2008-01-17 21:03 by cptnwillard, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (7)
msg60060 - (view) Author: Willard (cptnwillard) Date: 2008-01-17 21:03
The following code does not work like expected, it triggers a NameError.

class C:
    a = 42
    list(a for _ in 'x')

>>> NameError: global name 'a' is not defined 

This issue was discussed on comp.lang.python in the thread "Is this a
bug, or is it me?".
msg60066 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-01-17 22:06
It's a weakness, not a bug.

Just use a different variable name besides 'a'.
msg60068 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-01-17 22:32
I don't follow what you mean.  Can you post a working version of the
code fragment?
msg60092 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2008-01-18 14:02
Don't follow you:

>>> class C:
	a = 42
	list(a for _ in 'x')

>>> 

Works here! (Python 2.5.1 on win32)
msg60094 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-01-18 14:19
Facundo, are your sure that your output starts from a fresh environment?
I get:

C:\Python25>python
Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit
(Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> class C:
...     a = 42
...     list(a for _ in 'x')
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in C
  File "<stdin>", line 3, in <genexpr>
NameError: global name 'a' is not defined

This error is actually approximately "documented" in an obscure sentence
in http://docs.python.org/dev/reference/executionmodel.html#naming

"""
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.
"""

Well, I'm not sure that the genexpr can be considered as a "method", but
it is certainly a nested code block.
msg60100 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2008-01-18 16:33
Yes, something was bad with my test. Now I have the same behaviour.

Sorry for the noise.
msg60101 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-01-18 16:44
Amaury Forgeot d'Arc schrieb:

> Well, I'm not sure that the genexpr can be considered as a "method", but
> it is certainly a nested code block.

Technically it is a method, that's why this happens. I added a note to the
docs in r60051; closing this as "won't fix".
History
Date User Action Args
2022-04-11 14:56:29adminsetgithub: 46171
2008-01-18 16:44:15georg.brandlsetstatus: open -> closed
nosy: + georg.brandl
messages: + msg60101
resolution: wont fix
2008-01-18 16:33:16facundobatistasetmessages: + msg60100
2008-01-18 14:19:56amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg60094
2008-01-18 14:02:18facundobatistasetnosy: + facundobatista
messages: + msg60092
2008-01-17 22:32:03rhettingersetnosy: + rhettinger
messages: + msg60068
2008-01-17 22:06:43gvanrossumsetnosy: + gvanrossum
messages: + msg60066
2008-01-17 21:03:56cptnwillardcreate