So I've dabbled with Python, and have generally heard it's good form to write __init__()
s for your classes. Python also supports multiple inheritance. However, I've run into an issue where I'm trying to have 2 unrelated classes, both with __init__()
s, and I want to make a subclass of those both. For example:
class A():
def __init__(self, randparam):
self.randparam = randparam
def func(self):
return 2 * self.randparam
class B():
def __init__(self, paramrand):
self.paramrand = paramrand
class C(A, B):
def __init__(self):
super().__init___()
super().__init___()
#this won't result in both of the __init__()s running, obviously...
Here, I'm trying to make sure class C has both the attributes randparam
and paramrand
, in addition to the method func()
. How can I achieve this? Is this even doable in Python, or do I have to account for such issues in advance by making class B a subclass of class A? Or how else can I make sure both __init__()
s for both classes run when I use them both as base classes in a similar case to the code example above?