0

Say I want to create a class with two parent classes as follows:

class A:
    def __init__(self, a) -> None:
        self.a = a
    
class B:
    def __init__(self, b) -> None:
        self.b = b

class C(A, B):
    def __init__(self, a, b, c) -> None:
        super().__init__(a)
        super().__init__(b)
        self.c = c

Now, how do I make sure a goes to A and b goes to B because if I try to run this:

c = C(1, 2, 3)
print(c.a, c.b, c.c, sep='\n')

It yells AttributeError: 'C' object has no attribute 'b'.

Do I always have to create a sub-class with multiple parents like below?

class C(A, B):
def __init__(self, a, b, c) -> None:
    A.__init__(self, a)
    B.__init__(self, b)
    self.c = c

If so, then what is the use of super?

I've also noticed that if there are no arguments, calling C() will also call __init__ from the first parent i.e. A without doing any super or A.__init__(). But how do I call B here? Again I'm unable to understand the use of super.

It might be an ambiguous question but I'm really finding hard to get answers here. Thanks in advance for any help.

0 Answers0