如何提取super().__ init__来自的类?

时间:2019-06-24 04:48:34

标签: python python-3.x oop multiple-inheritance super

想象一下在多继承层次结构中使用的类 ID Date Amount tmp Checking 0 a 2014-06-13 12:03:56 13 5 True 1 b 2014-06-15 08:11:10 14 5 True 2 a 2014-07-02 13:00:01 15 6 False 3 b 2014-07-19 16:18:41 16 6 True 4 b 2014-08-06 09:39:14 17 7 False 5 c 2014-08-22 11:20:56 18 7 False 。使用MyMixInClass调用某个方法时,是否有某种方法可以检查或深入提取该方法的来源?

示例:

super()

1 个答案:

答案 0 :(得分:0)

对于mro序列中的每个类,您可以检查类`timescale 1s/100ms // 1Hz, with precision of 0.1 seconds ... initial begin clk = 0; #0.5 forever begin #0.5 clk = 1; #0.5 clk = 0; end end中是否存在__init__方法:

__dict__

输出:

class A:
    def __init__(self):
        pass

class B(A):
    def __init__(self):
        super().__init__()

class C(A):
    pass

class D(B, C):
    pass

if __name__ == '__main__':

    for cls in D.__mro__:
        if '__init__' in cls.__dict__:
            print(f'{cls.__name__} has its own init method', end='\n')
        else:
            print(f'{cls.__name__} has no init method', end='\n')

在此输出中,具有D has no init method B has its own init method C has no init method A has its own init method object has its own init method 方法(此处为__init__)的第一类是Bsuper().__init__()中调用的类