python多继承

python支持多继承

通过__mro__可查找继承列表,他是根据mro算法排列继承顺序

按照 __mro__ 的输出结果 从左至右 的顺序查找可继承方法

来看一个例子

class Base:
    def __init__(self):
        print("Base")

class Human(Base):
    def __init__(self):
        super().__init__()
        print("Human")

class Job(Base):
    def __init__(self):
        super().__init__()
        print("Job")

class Teacher(Human, Job):
    def __init__(self):
        super().__init__()
        print("Teacher")


if __name__ == "__main__":
    teacher = Teacher()
    print(Teacher.__mro__)

结果:

Base
Job
Human
Teacher
(<class '__main__.Teacher'>, <class '__main__.Human'>, <class '__main__.Job'>, <class '__main__.Base'>, <class 'object'>)

 

你可能感兴趣的:(python多继承)