0

We have a two classes declared as follows

Class A

class A: 
   def __init__(self,a)
       self.a = a

and class B

class B: 
   def __init__(self,B)
       self.b = b

We want to create a class C that inherits both classes A and B

class C(A,B): 
   def __init__(self,a,b)
       pass

I want when creating C to use the constructors of both A and B at the same time from the body of C. How can I use the constructors of both parent classes to set values of a and b from the constructor of the child class that inherits both. All solution I see speak about just about the scenario that calls only one constructor using super(C,self).__init__ Even it is not so true, I speak about a scenario like this

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

I want to pass arguments separately to the constructors of the super classes. What is the best Pythonic way to do this ?

pentanol
  • 330
  • 2
  • 7
  • 18
  • 1
    Is this answered here: [Calling parent class __init__ with multiple inheritance, what's the right way?](https://stackoverflow.com/questions/9575409/calling-parent-class-init-with-multiple-inheritance-whats-the-right-way) – Mark May 25 '22 at 18:36
  • 1
    In `C.__init__()` call `A.__init__(self, a)` and `B.__init__(self, b)`. – martineau May 25 '22 at 18:38
  • @Mark, This is not what I mean. I read the answer you provided on the link before writing this answer. I am not asking about the order of resolution here. Could you read the question carefully ? – pentanol May 25 '22 at 18:38
  • @pentanol the selected answer on that question (from Raymond Hettinger) says: `Both ways work fine…In the direct call approach, C.__init__ can call both A.__init__ and B.__init__` Is that not exactly what you are proposing? – Mark May 25 '22 at 18:45
  • @DylanLee. It does not answer. I am asking about how to target parent constructors separately with different sets of parameters from a child class constructor. – pentanol May 25 '22 at 18:45
  • @Mark I used that as an example and mentioned that it is not true. I used that just to illustrate what I want. Python prints out a TypeError error if you use that syntax. TypeError: __init__() missing 1 required positional argument: 'a' – pentanol May 25 '22 at 18:57
  • 2
    You need to pass `self` into the `__init__()` like: `A.__init__(self, a)` which you will see they are doing if you read that question carefully. – Mark May 25 '22 at 19:01
  • Thank you that works. Is there an equivalent way to specifically target the same constructors using super()? My question was exactly about this. – pentanol May 25 '22 at 19:04
  • 1
    That post also answers this: `When using super(), the classes need to be designed for cooperative multiple inheritance where C calls super, which invokes A's code which will also call super which invokes B's code.` So, no, not unless you design the base classes for multiple inheritance. – Mark May 25 '22 at 19:07
  • 1
    @pentanol the [highest rated answer to the linke dudplicate, the one linked by Mark](https://stackoverflow.com/a/50465583/5014455) is precisely an answer to your question, and all the followup questions you've asked here in the comments – juanpa.arrivillaga May 25 '22 at 19:25
  • 1
    Here's a blog post that covers exactly this question: https://rhettinger.wordpress.com/2011/05/26/super-considered-super/ – Raymond Hettinger May 25 '22 at 19:48
  • Thank you all for your comments and replies. – pentanol May 25 '22 at 20:09

0 Answers0