Python Multiple Inheritance super().__init__()
Question:
I have two classes, with the same parameter initialized by their __init__
method.
I would like to inherit both classes in class "X". But I will get: TypeError: B.__init__() missing 1 required positional argument: 'my_param'
Reproducible Example:
class A:
def __init__(self, my_param):
super().__init__()
self.my_param = my_param
class B:
def __init__(self, my_param):
super().__init__()
self.my_param = my_param * 2
class X(A, B):
def __init__(self, my_param):
super().__init__(my_param=my_param)
a = X(my_param=1)
print(a.my_param)
A and B are Mixins, they provide additional functionality for Child Classes. They can be used separetly or together. Lets’s say class A provides searhc functionality for search by ID, where class B provides search by value.
Is there a way to set my_param for each of A and B or to set it without getting the error?
Answers:
Since A
and B
share the same fields, I think it makes sense to make one inherit from the other – in this case, B
inherit from A
. That way, you’ll only need to subclass from B
in class X
.
For example:
class A:
def __init__(self, my_param):
self.my_param = my_param
class B(A):
def __init__(self, my_param):
super().__init__(my_param * 2)
# or:
# A.__init__(self, my_param * 2)
class X(B):
def __init__(self, my_param):
super().__init__(my_param=my_param)
a = X(my_param=1)
print(a.my_param) # 2
This can’t be done just using super()
calls, because the superclass of the last class in the chain will be object
, which doesn’t take a my_param
parameter.
See python multiple inheritance passing arguments to constructors using super for more discussion of parameter passing to __init__()
methods with multiple inheritance.
So you need to change X
to call the superclass init methods explicitly, rather than using super()
. And A
and B
shouldn’t call super().__init__()
because they’re subclasses of object
, which doesn’t do anything in its __init__()
.
class A:
def __init__(self, my_param):
self.my_param = my_param
class B:
def __init__(self, my_param):
self.my_param = my_param * 2
class X(A, B):
def __init__(self, my_param):
A.__init__(self, my_param=my_param)
B.__init__(self, my_param=my_param)
I have two classes, with the same parameter initialized by their __init__
method.
I would like to inherit both classes in class "X". But I will get: TypeError: B.__init__() missing 1 required positional argument: 'my_param'
Reproducible Example:
class A:
def __init__(self, my_param):
super().__init__()
self.my_param = my_param
class B:
def __init__(self, my_param):
super().__init__()
self.my_param = my_param * 2
class X(A, B):
def __init__(self, my_param):
super().__init__(my_param=my_param)
a = X(my_param=1)
print(a.my_param)
A and B are Mixins, they provide additional functionality for Child Classes. They can be used separetly or together. Lets’s say class A provides searhc functionality for search by ID, where class B provides search by value.
Is there a way to set my_param for each of A and B or to set it without getting the error?
Since A
and B
share the same fields, I think it makes sense to make one inherit from the other – in this case, B
inherit from A
. That way, you’ll only need to subclass from B
in class X
.
For example:
class A:
def __init__(self, my_param):
self.my_param = my_param
class B(A):
def __init__(self, my_param):
super().__init__(my_param * 2)
# or:
# A.__init__(self, my_param * 2)
class X(B):
def __init__(self, my_param):
super().__init__(my_param=my_param)
a = X(my_param=1)
print(a.my_param) # 2
This can’t be done just using super()
calls, because the superclass of the last class in the chain will be object
, which doesn’t take a my_param
parameter.
See python multiple inheritance passing arguments to constructors using super for more discussion of parameter passing to __init__()
methods with multiple inheritance.
So you need to change X
to call the superclass init methods explicitly, rather than using super()
. And A
and B
shouldn’t call super().__init__()
because they’re subclasses of object
, which doesn’t do anything in its __init__()
.
class A:
def __init__(self, my_param):
self.my_param = my_param
class B:
def __init__(self, my_param):
self.my_param = my_param * 2
class X(A, B):
def __init__(self, my_param):
A.__init__(self, my_param=my_param)
B.__init__(self, my_param=my_param)