我想在Python使用超()调用父类的方法2.从子类调用父类方法在Python 2
在Python 3,我想这样的代码吧:
class base:
@classmethod
def func(cls):
print("in base: " + cls.__name__)
class child(base):
@classmethod
def func(cls):
super().func()
print("in child: " + cls.__name__)
child.func()
与此输出:
in base: child
in child: child
但是,我不知道,如何在Python 2。做到这一点。当然,我可以使用base.func()
,但我不喜欢,除了指定的父类名和主要是我得到不想要的结果:
in base: base
in child: child
随着cls
(cls is child
)在super()
函数调用的第一个参数,我得到这个错误:
TypeError: must be type, not classobj
不知道如何使用super()
或类似的功能做在我没有来指定父类的名称?
提示:复制你的问题贴到谷歌搜索 – Dunno