[Python-3000] Change to class construction? (original) (raw)
Guido van Rossum guido at python.org
Sat Jul 7 00:32:15 CEST 2007
- Previous message: [Python-3000] Change to class construction?
- Next message: [Python-3000] Change to class construction?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 7/6/07, Phillip J. Eby <pje at telecommunity.com> wrote:
At 05:00 PM 7/6/2007 +0200, Georg Brandl wrote: >Collin Winter schrieb: > > While experimenting with porting setuptools to py3k (as of r56155), I > > ran into this situation: > > > > class C: > > a = (4, 5) > > b = [c for c in range(2) if a] > > > > results in a "NameError: global name 'a' is not defined" error, while > > > > class C: > > a = (4, 5) > > b = [c for c in a] > > > > works fine. This gives the same error as above: > > > > class C: > > a = (4, 5) > > b = [a for c in range(2)] > > > > Both now-erroneous snippets work in 2.5.1. Was this change intentional? > >It is at least intentional in the sense that in 3k it works the same as with >genexps, which give the same errors in 2.5.
This looks like a bug to me. A list comprehension's local scope should be the locals of the enclosing code, even if its loop indexes aren't exposed to that scope.
It's because the class scope is not made available to the methods. That is intentional. Georg's later example is relevant:
class C: a = 1 def f(self): print(a) # <-- raises NameError for 'a'
This is in turn intentional so that too-clever kids don't develop a habit of referencing class variables without prefixing them with self or C.
The OP's use case is rare enough that I don't think we should do anything about it.
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
- Previous message: [Python-3000] Change to class construction?
- Next message: [Python-3000] Change to class construction?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]