Python: Why can't I use `super` on a class?
为什么我不能用
例子:
1 2 3 4 5 6 7 8 9 10 | Python 3.1.3 >>> class A(object): ... def my_method(self): pass >>> class B(A): ... def my_method(self): pass >>> super(B).my_method Traceback (most recent call last): File"<pyshell#2>", line 1, in <module> super(B).my_method AttributeError: 'super' object has no attribute 'my_method' |
(当然,这是一个很小的例子,我可以只做
根据
super() -> same assuper(__class__,
)
super(type) -> unbound super object
super(type, obj) -> boundsuper
object; requiresisinstance(obj, type) super(type, type2) -> bound super
object; requires issubclass(type2,
type)[non-relevant examples redacted]
看起来您需要B的一个实例作为第二个参数传入。
http://www.artima.com/weblogs/viewpost.jsp?线程=236275
根据这一点,我似乎只需要打电话给
1 2 3 4 | >>> super(B, B).my_method <function my_method at 0x00D51738> >>> super(B, B).my_method is A.my_method True |