【发布时间】:2013-11-05 15:38:11
【问题描述】:
在 Python 3.x 中,super() 可以不带参数调用:
class A(object):
def x(self):
print("Hey now")
class B(A):
def x(self):
super().x()
>>> B().x()
Hey now
为了完成这项工作,需要执行一些编译时魔法,其结果之一是以下代码(将 super 重新绑定到 super_)失败:
super_ = super
class A(object):
def x(self):
print("No flipping")
class B(A):
def x(self):
super_().x()
>>> B().x()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 3, in x
RuntimeError: super(): __class__ cell not found
为什么super() 在没有编译器帮助的情况下无法在运行时解析超类?是否存在这种行为或其根本原因可能会咬到粗心的程序员的实际情况?
...并且,作为一个附带问题:Python 中是否有任何其他函数、方法等示例可以通过将它们重新绑定到不同的名称来破坏?
【问题讨论】:
标签: python python-3.x super