17

I'm not entirely sure this is for stackoverflow, so please correct me if not.

i.e. say we have t.py with contents:

class A(object):
    pass
print("A:", A)

class B(object):
    print("From B: A:", A)


class OuterClass(object):
    class AA(object):
        pass
    print("AA:", AA)

    class BB(object):
        print("From BB: AA:", AA)

And now we execute it: $ python3 t.py

A: <class '__main__.A'>
From B: A: <class '__main__.A'>
AA: <class '__main__.AA'>
From BB: AA:
Traceback (most recent call last):
  File "t.py", line 9, in <module>
    class OuterClass(object):
  File "t.py", line 14, in OuterClass
    class BB(object):
  File "t.py", line 15, in BB
    print "From BB: AA:", AA
NameError: name 'AA' is not defined

From the docs:

A class definition is an executable statement. It first evaluates the inheritance list, if present. Each item in the inheritance list should evaluate to a class object or class type which allows subclassing. The class’s suite is then executed in a new execution frame (see section Naming and binding), using a newly created local namespace and the original global namespace. (Usually, the suite contains only function definitions.) When the class’s suite finishes execution, its execution frame is discarded but its local namespace is saved. [4] A class object is then created using the inheritance list for the base classes and the saved local namespace for the attribute dictionary. The class name is bound to this class object in the original local namespace.

So I understand the behaviour but not the rationale behind making the scope not be lexical like everywhere else. It goes against "Special cases aren't special enough to break the rules." Why does class behave differently than, say, def?

Is this a "practicality beats purity" case? If so, what's the justification? I initially thought it might be an artefact of python 2.x, but as you can see above, the behaviour is also present in python 3.3.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
Tim Kersten
  • 579
  • 3
  • 13
  • 1
    possible duplicate of [Python nested classes scope](http://stackoverflow.com/questions/1765677/python-nested-classes-scope) – Wooble Dec 18 '12 at 17:06
  • 1
    @Wooble don't think so. That question explains the behaviour, (like the docs) but not the rational behind it. – Tim Kersten Dec 18 '12 at 17:14
  • 3
    http://python-history.blogspot.com/2009/03/how-everything-became-executable.html may help. In any case, your title is misleading; your problem is that the inner class definition *does* create a new scope, not that it doesn't. – Wooble Dec 18 '12 at 17:15
  • 2
    @aaron-digulla why did you rename the question? I originally asked "What's the rational to not scope class body lexically in python class definitions?" which is rather different from the "Why doesn't “class” start a new scope like “def” does?" that it was changed to. – Tim Kersten Dec 18 '12 at 17:20
  • 2
    "rationale" is the correct word, not "rational", just FYI. – jgritty Dec 18 '12 at 17:24
  • @TimKersten: The new title is less complicated but asks the same thing: Why does class behave differently than, say, def? – Aaron Digulla Dec 18 '12 at 17:24
  • @Wooble that link goes a long way to explaining it, if not outright answering the question. Thanks. I guess Guido's not fixed it for classes yet :( – Tim Kersten Dec 18 '12 at 17:24
  • 1
    I doubt it will be since there's no particularly good reason to nest classes in Python IMO. – Wooble Dec 18 '12 at 17:26
  • @jgritty yes, should have been "rationale", sorry – Tim Kersten Dec 18 '12 at 17:26
  • 1
    @Wooble: A class starts a scope in the sense that the methods go into the class but the scope isn't used to look up names. It's more like a dictionary than a scope or name space. – Aaron Digulla Dec 18 '12 at 17:26
  • @AaronDigulla it does start a scope though, just that said scope isn't searched. Enclosing functions' scope is searched though, so why not also search onclosing class's scope? – Tim Kersten Dec 18 '12 at 17:31

1 Answers1

12

As Wooble noted in a comment, the class block does create a new scope. The problem is that names in a class block scope are not accessible to scopes nested within that scope. This is mentioned in the documentation:

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 – this includes comprehensions and generator expressions since they are implemented using a function scope.

I can't find the source of this right now, but somewhere (I think on a StackOverflow question) I found one plausible rationale for this: if the class def were accessible inside nested blocks, method names would shadow global functions, including builtins. This would make it awkward to create classes that have methods with short, simple names, but that also make use of builtin functions of the same name. For instance, you couldn't do this:

class SomeDataStructure(object):
    def sum(self):
        return sum(self._data)
    def otherCalc(self):
        return sum(item for item in somethingElse)

If the class block were in scope within methods, this would cause an infinite recursion, since the inner sum call would have access to the method name and would call that method instead of the builtin sum function. Likewise, other methods such as the the otherCalc method would no longer have access to the global sum function for their own use, but would always be getting the method. An answer to another SO question describes this by saying "it's because you're supposed to use self to access methods in Python".

Now, that argument really only makes sense for functions inside classes, because function bodies aren't executed when the def is, but class bodies are executed when the class statement is. However, I think if you combine what I said above with the notions from this blog post, you get a reasonable rationale. Namely, nested classes weren't --- and still aren't --- considered a style that's worth supporting. Functions inside classes, though, are of course commonplace. The simplest way to handle the function-inside-a-class use case was to make the class block not pass on its names to any nested scopes. It would arguably be better if it passed on its names to nested classes but not nested functions, but that would be a more complex rule, and no one cares enough about supporting nested classes to make it worthwhile.

Community
  • 1
  • 1
BrenBarn
  • 242,874
  • 37
  • 412
  • 384