In what order does a class inherit from its superclasses in python?

3

Be the code below:

class B(A):
    def __init__(self):
        self.c  = 16
    def y(self):
        print("B.y")
    def get_c(self):
        return self.c

class C(object):
    def __init__(self):
        self.c = 5
    def y(self):
        print("C.y")
    def get_c(self):
        return self.c

class D(C, B):
    def __init__(self):
        C.__init__(self)
        B.__init__(self)

var = D()

When calling var.y() the result is C.y because D(C, B) inherits its methods from its superclasses following a left-to-right order. However, when calling var.get_c () the result is 16. Why?

    
asked by anonymous 18.02.2017 / 21:42

1 answer

2

According to the official documentation about Python classes, we see in the multiple inheritance section the definition:

  

For most purposes, in the simplest cases, you can think of the search for attributes inherited from a parent class as depth-first, left-to-right, not searching twice in the same class where there is an overlap in the hierarchy.

That is, using your example, if a method / attribute is not found in the D class, Python will search by its definition in the C class, first left. If it did not find it, it would go deeper, for the base classes of C . As it does not have, it returns to class B and, if it does not, it goes to class A , if it does not, for the base classes of A . If you still can not find the definition, throw the exception.

Therefore, when running var.y() , the definition of y in D, C, B, A , in this order is searched. It finds in C , so it returns C.y . When executed var.get_c() , the same thing is also found in C , returning the value of self.c , but what happens here is that in the initializer of class D , you execute the initialization of class B after% of class C and in both the self.c property is set. That is, when executed C.__init__() , the value of self.c will be 5, but when executed B.__init__() , the value of self.c becomes 16, which explains why var.get_c() returns the value 16-

    
18.02.2017 / 21:57