Python nonlocal statement in a class definition

孤者浪人 提交于 2019-11-30 16:51:45

问题


I'm trying to perform some analysis of scope in Python 3 source code and I'm stuck with how the nonlocal statement statement works inside a class definition.

As I understand it, the class definition executes its body inside a new namespace (call it dict) and binds the class name to the result of type(name, bases, dict). Nonlocal x should work as long as it refers to a variable that is bound somewhere in the enclosing non-local scope.

From this I expect the following code to compile and run:

class A:
    v = 1
    class B:
        nonlocal v
        v = 2

but this fails with

SyntaxError: no binding for nonlocal 'v' found

while the following code runs perfectly

def A():
    v = 1
    class B:
        nonlocal v
        v = 2

Can anyone explain the difference here between the closure of the function definition and the class definition?


回答1:


Lexical scoping applies only to function namespaces, otherwise methods defined inside a class would be able to "see" the class level attributes (which is by design - those attributes must instead be accessed as attributes of self inside the method).

The same limitations that cause the class level variables to be skipped over by references from methods also keep the nonlocal keyword from working its magic. (global does work though, since that doesn't rely on the lexical scoping machinery)




回答2:


Python handles class and function definitions rather differently. For example, your A.v is not a variable of A but rather an attribute of it. The namespace created by a class is not, therefore, a scope. I am not surprised that nonlocal does not work as you're trying to use it.



来源:https://stackoverflow.com/questions/5466238/python-nonlocal-statement-in-a-class-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!