为什么我不能使用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
的文档:(Emphasis mine)
super()
-> same assuper(__class__, <first argument>)
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?thread=236275
据this看来我只需要打电话给
super(B, B).my_method
:相关问题 更多 >
编程相关推荐