search

Home  >  Q&A  >  body text

Super function in Python multiple inheritance?

class A:
    def __init__(self):
        print('A')
        
class B():
    def __init__(self):
        print('B')

class C(A, B):
    def __init__(self):
        super(C, self).__init__()
        print('C')
              

obj = C()

You can use the super method to call the method of the parent class, but how to use it in multiple inheritance?
The above code class C will call the __init__ method of class A, but if I want to call class A and class B# at the same time ##How to write the '__init__' method? Or just call the __init__ method of class B?

Of course, this can be achieved using the unbound method.

怪我咯怪我咯2757 days ago579

reply all(2)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-18 10:52:04

    Just call it explicitly, you can choose freely.
    A.__init__(self)A.__init__(self)
    B.__init__(self)B.__init__(self)

    reply
    0
  • 迷茫

    迷茫2017-05-18 10:52:04

    python-super

    reply
    0
  • Cancelreply