Issue 1863: NameError with genexp in class scope (original) (raw)
Issue1863
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) * ![]() |
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) * ![]() |
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) * ![]() |
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) * ![]() |
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 "", line 1, in File "", line 3, in C File "", line 3, in 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) * ![]() |
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) * ![]() |
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:29 | admin | set | github: 46171 |
2008-01-18 16:44:15 | georg.brandl | set | status: open -> closednosy: + georg.brandlmessages: + resolution: wont fix |
2008-01-18 16:33:16 | facundobatista | set | messages: + |
2008-01-18 14:19:56 | amaury.forgeotdarc | set | nosy: + amaury.forgeotdarcmessages: + |
2008-01-18 14:02:18 | facundobatista | set | nosy: + facundobatistamessages: + |
2008-01-17 22:32:03 | rhettinger | set | nosy: + rhettingermessages: + |
2008-01-17 22:06:43 | gvanrossum | set | nosy: + gvanrossummessages: + |
2008-01-17 21:03:56 | cptnwillard | create |