我不太清楚在继承时是否需要调用self
类方法中的super
参数,因为我在重复参数定义错误时遇到问题使用它。
代码基本上是:
class MyClass(ParentClass):
def method(self, arg=None):
arg=f(arg)
super(MyClass, self).method(self, arg=None)
在self
电话中super
,我明白了:
TypeError:method()为关键字参数' arg'`
获取了多个值
没有,它似乎有用,但我不知道我是否会错过任何我需要的副作用。是self
语句中的super
吗?
答案 0 :(得分:2)
阅读docs!
您应该如何使用super
:
super(MyClass, self).method(arg=None)
您必须将self
传递给super
来电,而不是方法调用。
此外,如果您使用的是Python 3,它甚至更简单:
super().method(arg=None)
答案 1 :(得分:1)
不要在method
的论据中明确提供自我,不需要。
在python 2中,在super
的参数中你仍然需要它。请注意,在python3中,这只是super()
。