How to get the parents of a Python class?

后端 未结 6 1830
野趣味
野趣味 2020-11-28 03:49

How can I get the parent class(es) of a Python class?

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 04:30

    If you want all the ancestors rather than just the immediate ones, use inspect.getmro:

    import inspect
    print inspect.getmro(cls)
    

    Usefully, this gives you all ancestor classes in the "method resolution order" -- i.e. the order in which the ancestors will be checked when resolving a method (or, actually, any other attribute -- methods and other attributes live in the same namespace in Python, after all;-).

提交回复
热议问题