1

If a class has 2 or more parents, how can I use super(), or any equivalent, to make reference to each of them? For example here:

class A:
    def __init__(self, x): self.a = x
class B:
    def __init__(self, y): self.b = y
class C(A,B):
    def __init__(self, x, y):
        super().__init__(x)
        B.__init__(self,y)  # I would to like to use super() here too

ObjetoC = C(4,3);
print (ObjetoC.a, ObjetoC.b) # It works fine

PD: I understand the MROrder. I just wonder if there is a way to reach a non-priority parent with super() or equivalent. Or if there is another elegant way to do that I have already done by using B.__init__(self,y)

Ali Rojas
  • 549
  • 1
  • 3
  • 12
  • Does this help? https://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance – bb1950328 Jun 08 '20 at 05:44
  • @Sadap There they are using Python2, and I don't understand the parameters inside super(). I hope those don't exist in Python3 – Ali Rojas Jun 08 '20 at 05:57
  • I think it works in python 3 too – bb1950328 Jun 08 '20 at 06:03
  • @Sadap I understand the MROrder but I wonder if there is a way to reach a non-priority parent with super() or equivalent. Or if there is another elegant way to do that I have already done by using `B.__init__(self,y)` – Ali Rojas Jun 08 '20 at 06:17
  • Does this answer your question? [How does Python's super() work with multiple inheritance?](https://stackoverflow.com/questions/3277367/how-does-pythons-super-work-with-multiple-inheritance) – Pynchia Jun 08 '20 at 07:15
  • No, this isn't how `super` works. If you want to use cooperative multiple inheritance, you need to call super in all the classes involved – juanpa.arrivillaga Jun 08 '20 at 08:20
  • @juanpa.arrivillaga I just want to call to both my parents without having to use their names (using `super()` or whatever). If It os imposible I would like to know too – Ali Rojas Jun 08 '20 at 15:13

0 Answers0