Jonline Jonline - 3 months ago 12
Python Question

How to handle multiple inheritance in python when parent init functions accept different numbers of arguments

I have a class that inherits from 2 other classes, one of which has accepts an init argument. How do I properly initialize the parent classes?

So far I have:

class A(object):
def __init__(self, arg1):
self.arg1 = arg1

class B(object):
def __init__(self):
pass

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


But this throws a
TypeError
as
B
doesn't receive an argument.

In context,
B
is the proper parent of
C
, whilst
A
is a mixin that many classes in the project inherit functionality from.

Answer

You can call the __init__ of the parent classes manually.

class C(A, B):
     def __init__(arg1):
          A.__init__(self,arg1)
          B.__init__(self)