[Python-Dev] Scoping (corner cases)

Tim Peters tim.one@home.com
Sat, 17 Mar 2001 17:31:08 -0500


[Ka-Ping Yee]
> What's going on here?
>
>     Python 2.1b1 (#15, Mar 16 2001, 04:31:43)
>     [GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2
>     Type "copyright", "credits" or "license" for more information.
>     >>> x = 1
>     >>> class Foo:
>     ...     print x
>     ...
>     1
>     >>> class Foo:
>     ...     print x

IMO, this one should have yielded an UnboundLocalError at runtime.  "A class
definition is a code block", and has a local namespace that's supposed to
follow the namespace rules; since x is bound to on the next line, x should be
a local name within the class body.

>     ...     x = 1
>     ...
>     1
>     >>> class Foo:
>     ...     print x

Ditto.

>     ...     x = 2
>     ...     print x
>     ...
>     1
>     2
>     >>> x
>     1
>
> Can we come up with a consistent story on class scopes for 2.1?

The story is consistent but the implementation is flawed <wink>.  Please open
a bug report; I wouldn't consider it high priority, though, as this is
unusual stuff to do in a class definition.