2

I am wondering why a method of a class doesn't look into its enclosing scope, if a name is not defined.

def test_scope_function():
    var = 5
    def print_var():
        print(var) # finds var from __test_scope_function__
    print_var()


globalvar = 5
class TestScopeGlobal:
    var = globalvar # finds globalvar from __main__

    @staticmethod
    def print_var():
        print(TestScopeGlobal.var)


class TestScopeClass():
    var = 5

    @staticmethod
    def print_var():
        print(var) # Not finding var, raises NameError

test_scope_function()
TestScopeGlobal.print_var()
TestScopeClass.print_var()

I would expect TestScopeClass.print_var() to print 5 since it can read classvar in the TestScopeClass body.

Why this behavior? And what should i read in the docs to get to know about it.

Finn
  • 1,999
  • 2
  • 24
  • 29

2 Answers2

6

Scopes are searched as follows:

  • the innermost scope, which is searched first, contains the local names
  • the scopes of any enclosing functions, which are searched starting with the nearest enclosing scope, contains non-local, but also non-global names
  • the next-to-last scope contains the current module’s global names
  • the outermost scope (searched last) is the namespace containing built-in names

(emphasis added). Enclosing classes are not searched, because they are not listed. This behavior is deliberate, because otherwise the descriptor protocol, which among other things provides the self argument to methods, would not have a chance to fire.

Community
  • 1
  • 1
Kevin
  • 28,963
  • 9
  • 62
  • 81
3

Per the Python documentation (emphasis mine):

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

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437