Suppose I have the following class inheriting from classes A
and B
:
class A: def __init__(self): self.x = 2 class B: def __init__(self,u): self.y = u + 2 class C(A,B): def __init__(self): #self.y should be 4 here
How do I initialize B only after initializing A? Using super(C,self).__init__()
doesn’t let me use attributes of A into B.
Advertisement
Answer
You don’t HAVE to use super.
class A: def __init__(self): self.x = 2 class B: def __init__(self,u): self.y = u + 2 class C(A,B): def __init__(self): A.__init__(self) B.__init__(self, self.x)
Now, that does mean some pretty tight coupling, in that C
has to be way too aware of what A.__init__
does.