super()不是用于staticmethods吗?super()和@staticmethod交互
当我尝试像
class First(object):
@staticmethod
def getlist():
return ['first']
class Second(First):
@staticmethod
def getlist():
l = super(Second).getlist()
l.append('second')
return l
a = Second.getlist()
print a
我得到如果我改变staticmethods到classmethods并通过类的实例,以超(以下错误
Traceback (most recent call last):
File "asdf.py", line 13, in <module>
a = Second.getlist()
File "asdf.py", line 9, in getlist
l = super(Second).getlist()
AttributeError: 'super' object has no attribute 'getlist'
),东西很好地工作。我在这里调用超级(类型)不正确还是有东西我失踪?
在Python 3中,这是否有任何不同,其中没有任何参数的'super()'是常规方法中调用它的常用方法?调用'super()。foo()'时,我在Python3中遇到了同样的问题。 – gerrit 2016-09-22 14:24:03
@gerrit:Python 3的零参数'super()'仅适用于类或实例方法。这是由于它用来确定它定义哪个类的魔力。在静态方法中(就像在常规的模块级函数中一样),仍然需要两个显式参数。 (在Python 3中,单参数形式仍然未被绑定。) – 2016-09-22 22:13:30