【发布时间】:2019-10-15 15:51:54
【问题描述】:
我正在使用以下代码使用超级函数:
class A:
@staticmethod
def hello(string):
print('hello '+string)
class B(A):
@staticmethod
def greeting(string):
super().hello(string)
print('How are you?')
x = B
x.greeting('mate')
但是,上面给出了以下错误: TypeError: super(type, obj): obj 必须是类型的实例或子类型
我已经在 S.O 上寻找与此相关的其他问题,但他们没有解决这个特定问题。
为什么会这样? x 不是类型的子类型吗?
在此先感谢
【问题讨论】:
-
我认为这可能与
@staticmethod装饰器有关。 -
另外,当您尝试创建实例时,不要忘记调用类构造函数。也就是说,您可能想输入
x = B()而不是x = B。
标签: python