在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中是否还有其他函数、方法等的例子,可以通过将它们重新绑定到不同的名称来打破它们?