0

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.

6
  • For multiple inheritance to be practical, at least one of the parents has to actually be designed for it. It doesn't look like either A or B is designed for it. Commented Aug 31, 2016 at 16:45
  • you should read about MRO in python stackoverflow.com/questions/1848474/… Commented Aug 31, 2016 at 16:48
  • @giaosudau I did, actually; conceptually I get it but syntactically I don't understand how to implement what I'm trying to achieve. Commented Aug 31, 2016 at 16:49
  • Actually your question is about init parent class mean Class A or B right? If so just create like normal A("hello") with B(). Commented Aug 31, 2016 at 16:55
  • @giaosudau It's a nice idea but B is totally unrelated to A; these are mixins designed to pass a suite of functionality to classes that inherit from them. Commented Aug 31, 2016 at 17:01

1 Answer 1

1

You can call the __init__ of the parent classes manually.

class C(A, B):
     def __init__(arg1):
          A.__init__(self,arg1)
          B.__init__(self)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.