Search code examples
pythoninitsuper

Error: TypeError: __init__() takes 1 positional argument but 2 were given


I am an absolutely Python beginner and don't understand the problem of the following lines.

class Base:
    def __init__(self, x):
        print("Base")
        self.x = x

class A(Base):
    def __init__(self):
        super(A, self).__init__(1)
        print("A")
class B(Base):
    def __init__(self):
        super(B, self).__init__(2)
        print("B")

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

if __name__ == '__main__':
    c = C()

I think super calls looking from C init in the following order: A-B-Base-Base or am I wrong?

Thank you for your answers.


Solution

  • Use the below code:

    class A(Base):
        def __init__(self):
            super(A, self).__init__() #Remove this 1
            print("A")
    

    I hope it may help you.