On 7/6/07, Phillip J. Eby <[EMAIL PROTECTED]> 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/) _______________________________________________ Python-3000 mailing list [email protected] http://mail.python.org/mailman/listinfo/python-3000 Unsubscribe: http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com
