我已阅读 Why is Python 3.x's super() magic?并了解在方法中使用 super
或 __class__
会自动为该方法创建一个 __class__
单元格变量:
class Demo:
def meth(self):
super().meth()
>>> Demo.meth.__closure__
(<cell at 0x7f4572056138: type object at 0x564bda0e5dd8>,)
>>> Demo.meth.__closure__[0].cell_contents
<class '__main__.Demo'>
据我所知,cell是用来保存闭包变量的,并且可以自由修改:
def outer():
x = 3
def inner():
print(x)
x = 5
return inner
inner = outer()
inner() # output: 5
>>> inner.__closure__
(<cell at 0x7f2183a5e138: int object at 0x7f2184600460>,)
但是尝试重新分配 __class__
单元格的值会使 super
抛出一个奇怪的错误:
class Demo:
def meth(self):
__class__ = Demo
super().meth()
Demo().meth()
Traceback (most recent call last):
File "untitled.py", line 8, in <module>
Demo().meth()
File "untitled.py", line 6, in meth
super().meth()
RuntimeError: super(): __class__ cell not found
为什么会发生这种情况?为什么__class__
不能像其他闭包变量一样重新赋值?
最佳答案
您需要一个 nonlocal
语句来分配给闭包变量,包括神奇的 __class__
闭包变量。在没有 nonlocal
语句的情况下分配给 __class__
会创建一个隐藏魔术闭包变量的局部变量。
您期望 __class__
的行为就像它是 meth
的本地行为一样,但实际上它的行为就像它是一个不可见的伪作用域的本地行为,其中所有方法都在其中Demo
是嵌套的。如果它被视为 meth
本地的,则不需要 nonlocal
。
如果您确实添加了 nonlocal
语句,则实现 actually will allow you重新分配魔术闭包变量:
class Foo:
def meth(self):
nonlocal __class__
__class__ = 3
super()
Foo().meth()
结果:
Traceback (most recent call last):
File "./prog.py", line 7, in <module>
File "./prog.py", line 5, in meth
RuntimeError: super(): __class__ is not a type (int)
关于python - 为什么分配给 __class__ 单元格会中断 `super` ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52429844/