Python super class reflection

前端 未结 4 1200
感动是毒
感动是毒 2021-02-03 21:30

If I have Python code

class A():
    pass
class B():
    pass
class C(A, B):
    pass

and I have class C, is there a way to iterat

4条回答
  •  心在旅途
    2021-02-03 21:45

    The inspect module was a good start, use the getmro function:

    Return a tuple of class cls’s base classes, including cls, in method resolution order. No class appears more than once in this tuple. ...

    >>> class A: pass
    >>> class B: pass
    >>> class C(A, B): pass
    >>> import inspect
    >>> inspect.getmro(C)[1:]
    (, )
    

    The first element of the returned tuple is C, you can just disregard it.

提交回复
热议问题