Why the difference in handling unbound locals in f

2020-07-03 16:40发布

When referencing global variables, one can see that functions and classes handle this differently. The first is fine and the second causes an error:

x = 10
class Foo():
    x = x + 1
a = foo()

Vs:

x = 10
def faa():
    x = x + 1
faa()

In the Python execution model, this is described as:

A class definition is an executable statement that may use and define names. These references follow the normal rules for name resolution with an exception that unbound local variables are looked up in the global namespace.

But why?

The only other hint I have come across is this bit:

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.

Which still offers no explanation why this should have the consequence that unbound locals are looked up in the global namespace.

Both links are from this answer which does not adress the why in more details though.

1条回答
爷的心禁止访问
2楼-- · 2020-07-03 17:22

As explained in Python FAQ - Why am I getting an UnboundLocalError?

... because when you make an assignment to a variable in a scope, that variable becomes local to that scope and shadows any similarly named variable in the outer scope. Since the last statement in foo assigns a new value to x, the compiler recognizes it as a local variable.

Consequently when earlier attempting to access the uninitialized local variable an error results.

This explains why this:

x = 10
def foo():
    x = x+1  # raises UnboundLocalError
foo()

raises an Exception, but not this:

x = 10
def faa():
    y = x+1  # x stays in global scope, since it is not assigned in local
faa()

For the class foo() it's the same, since python allows assignments of attributes to objects to any given time. The class-code assigns x as new attribute of the object a.

x = 10
class foo():
    x = x+1  # scope of x: class
a = foo()  # x = 10; a.x = foo.x = 11

is the same as:

x = 10
class foo():
    def __init__(self):
        self.x = x+1  # scope of x: instance
a = foo()  # x = 10; a.x = 11

where obviously self.x is assigned instead of x, therefore also staying in global scope. (see also here)

查看更多
登录 后发表回答