【发布时间】:2011-06-08 20:17:17
【问题描述】:
为什么我不能使用super 来获取类的超类的方法?
例子:
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'
(当然这是一个简单的案例,我可以做A.my_method,但我需要这个来处理钻石继承的案例。)
根据super 的文档,我想要的似乎应该是可能的。这是super 的文档:(强调我的)
super()-> 等同于super(__class__, <first argument>)
super(type)-> 未绑定的超级对象
super(type, obj)-> 绑定super目的;需要isinstance(obj, type)super(type, type2) -> 绑定超级 目的;需要 issubclass(type2, 类型)
[编辑不相关的例子]
【问题讨论】:
-
我删除了我的答案 - 我得到了同样的错误。很奇怪,会再研究一下。
标签: python class python-3.x super