0

I have tried to run the following code:

class A(object):
    def __init__(self, a):
        super(A, self).__init__()
        print('A')
 
 
class B(object):
    def __init__(self, b):
        super(B, self).__init__()
        print('B')
 
 
class C(A, B):
    def __init__(self, a, b):
        A.__init__(self, a)
        B.__init__(self, b)
        print('C')
 
sim = C(a=1,b=2)

And I get the following error message that I don't understand:

Traceback (most recent call last):
  File "<string>", line 19, in <module>
File "<string>", line 15, in __init__
  File "<string>", line 3, in __init__
TypeError: __init__() missing 1 required positional argument: 'b'

In line 3, I call the constructor of the object class that doesn't require any input argument. It seems that Python in line 3 is trying to call the constructor of B. Why?

cheshire
  • 1,109
  • 3
  • 15
  • 37
Simon
  • 21
  • 1
  • 4
  • 1
    Does this answer your question? [How does Python's super() work with multiple inheritance?](https://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance) – Mario Camilleri Nov 21 '20 at 17:22
  • 2
    When you call `super(B, self).__init__()`, you are calling that function with a `C` object. The method resolution order of `C` is `[C, A, B, object]`. Since you are in `A`, it resolves to the *next class in the method resolution order*, i.e. `B`. So you are fundamentally mistaken about "In line 3, I call the constructor of the object class ". That isn't what you are doing. You are calling `super`, which will call *the next method in the method resolution order* – juanpa.arrivillaga Nov 21 '20 at 17:23

0 Answers0