How to inherit the multiple inheritance constructor from a secondary super-class in python3?

0

How to inherit the multiple inheritance constructor from a secondary super-class in python3?

Greetings I have a question, how can I make the sub-class: "MyClass" inherit the constructor of the super-class "MySuperClase2" in multiple inheritance?

  

Code | Python3:

class MiSuperClase1():
    def __init__(self):
        self.a = "a"
        self.b = "b"

    def metodo_1(self):
        print("metodo_1 metodo llamado")

class MiSuperClase2():
    def __init__(self):
        self.c = "c"
        self.d = "d"

    def metodo_2(self):
        print("metodo_2 metodo llamado")

class MiClase(MiSuperClase1, MiSuperClase2):
    def __init__(self):
        super().__init__()

    def metodo_3(self):
        print("metodo_3 metodo llamado")
        #print(self.c, self.d)

c = MiClase()
c.metodo_3()
  

Doubts:

I have some doubts, thank you for answering them:

How is the super class "MySuperClase2" correctly named when it is the parent class of "MyClass" but it is secondary (so to speak)?

is called: super-class 2, super-secondary class? (I do not understand if ...)

Thank you in advance for your answers !!

    
asked by Julio Cesar 03.01.2019 в 20:10
source

1 answer

0

When you use super() , you get a reference to the first base class in case you inherit several.

If you want super() to refer to MiSuperClase2 you just need to change the order in which you declare the inheritance:

class MiClase(MiSuperClase2, MiSuperClase1):
    def __init__(self):
        super().__init__()

In this case the constructor will call the MiSuperClase2 .

In case you want to access both constructors, for example to call them in sequence, super() is not helpful. But you can always use the names of the classes directly:

class MiClase(MiSuperClase2, MiSuperClase1):
    def __init__(self):
        MiSuperClase2.__init__()
        MiSuperClase1.__init__()

The only problem with this is that if you ever change the class hierarchy, and MiClase stops inheriting these two, you have to remember to change your __init__() .

If you want to make it more automatic (but less and less readable):

class MiClase(MiSuperClase2, MiSuperClase1):
    def __init__(self):
        for clase in self.__class__.__bases__:
            clase.__init__()

The order in which they are listed in __bases__ is the one you put in parentheses in the declaration of MiClase

As far as I know, there is no special name for which class is "main" and which is "secondary", since I do not really see why a base class should be more secondary than another, especially when the class hierarchy it is entangled and you have multiple inheritance of several, which in turn inherit from another (perhaps common), etc. In a diagram of classes UML does not mark graphically of special form the "main".

In the end what matters is to know in what order python will search in that class hierarchy when it has to solve an attribute like self.algo , if the object does not have it. If you want to know that order, you can do Clase.mro() to get the list of all the classes that Clase inherits, directly or indirectly, and in the order in which python will consult them to solve an attribute or method.

    
answered by 03.01.2019 / 21:52
source