【发布时间】:2019-11-05 20:56:17
【问题描述】:
想象一个在多重继承层次结构中使用的类MyMixInClass。当使用super()调用某个方法时,有没有办法检查或钻取来提取该方法来自的类?
示例:
class MyMixInClass:
def __init__(self):
initfunc = getattr(super(), '__init__')
# can we figure out which class the __init__ came from?
【问题讨论】:
-
在
MyMixInClass的实例上调用mro -
@ReblochonMasque 不准确。
mro中的下一个类可能没有__init__方法 -
是的,
mro返回一个祖先列表;如果他们有初始化,你可以排序。 -
@ReblochonMasque 你能用一个只有一些类有
__init__方法的多重继承示例来证明这一点吗?
标签: python python-3.x oop multiple-inheritance super