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.

Jonline
  • 1,677
  • 2
  • 22
  • 53
  • 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. – user2357112 Aug 31 '16 at 16:45
  • you should read about MRO in python http://stackoverflow.com/questions/1848474/method-resolution-order-mro-in-new-style-python-classes – giaosudau Aug 31 '16 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. – Jonline Aug 31 '16 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(). – giaosudau Aug 31 '16 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. – Jonline Aug 31 '16 at 17:01
  • The standard reference for this sort of thing is (Python core developer) Raymond Hettinger's "[Python's `super()` Considered Super!](https://rhettinger.wordpress.com/2011/05/26/super-considered-super/)", especially the "Practical Advice" and "Complete Example – Just for Fun" sections. – Kevin J. Chase Aug 31 '16 at 17:48

1 Answers1

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)
napuzba
  • 6,033
  • 3
  • 21
  • 32