Python super class reflection

前端 未结 4 1193
感动是毒
感动是毒 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条回答
  •  梦毁少年i
    2021-02-03 21:38

    C.__bases__ is an array of the super classes, so you could implement your hypothetical function like so:

    def magicGetSuperClasses(cls):
      return cls.__bases__
    

    But I imagine it would be easier to just reference cls.__bases__ directly in most cases.

提交回复
热议问题