SoFunction
Updated on 2024-12-20

One of the weird things about Python's multiple inheritance is that it's both Father and Grandfather.

As we know, inheritance is a very important concept in object-oriented programming. Subclasses can use the methods and attributes of the parent class.

For example, the following code:

class Father:
    def __init__(self):
         = 'Shanghai'

    def say(self):
        print('I'm Dad')

class Son(Father):
    def __init__(self):
        super().__init__()

    def say(self):
        print('I am the son')

son = Son()
print()

The running effect is shown below:

As you can see from the figure, the subclasses do not have property, but when we print it directly, it doesn't report an error, it automatically uses the parent class'saddress Properties.

Obviously, if an attribute, which is also not available in the subclass and not available in the parent class, will definitely report an error, theAs shown in the figure below:

We also know thatPython is supporting multiple inheritance, a subclass can have more than one parent. So, let's see the following code:

class GrandFather:
    def __init__(self):
         = 'Shanghai'

    def say(self):
        print('I'm Dad')

class Father:
    def __init__(self):
         = 100
    
    def where(self):
        print('I now live in:', )

class Son(GrandFather, Father):
    def __init__(self):
        super().__init__()

    def say(self):
        print('I am the son')

son = Son()
()

The running effect is shown below:

If you look closely, you will notice that the code is a bit strange. I'm calling the () method, and since the Son class doesn't have it, it looks in its two parents. So it finds it in Father. So it executes thewhere() method, no problems so far.

But then it's not right, .where() Inside the method, the call to the Properties. But the problem is thatFather This class doesn't have an .address property! AndFather There is no parent class either, so this .address Where do the attributes come from?

Could it be that, inside the hidden corners that the developers don't know about, theGrandFather Classes have quietly becomeFather of the parent class? In this way, theGrandFather Wouldn't it be the parent class of C again, and the parent class of C's parent class?GrandFather Both dad and grandpa?

In reality, there is not such a confusing relationship. To explain this phenomenon, we have to look at theself This thing talks about it.

We know that the attributes of a class all begin withself begins with the first parameter of the method, which is alsoself So what is this self? So what is this self thing? Let's see what it is with a little code:

class A:
    def get_self(self):
        return self

test = A()
what_is_self = test.get_self()

test is what_is_self

The running effect is shown below:

As you can see from inside the figure, theself It's actually an instance of this class. Let's look again at the case where there is inheritance:

class A:
    def get_self(self):
        return self

class B(A):
    def __init__(self):
        ...

test = B()
what_is_self = test.get_self()

print(what_is_self)

As you can see from the graph, while I'm in the A category of the.get_self() method returns theself But thisself It is actually an instance of class B. This is because I have been initializing only class B since the beginning and not class A. A is the parent class of class B though. But the parent class ofself are turned into instances of subclasses.

Once this is understood, the previous question is well explained, the我们多打印一些信息:

大家注意画红线的地方, self Always. Son Instances of the class。the reason why,Initialization at the beginning .address is the initialized instance of Son. .address Attributes. The attributes that appear later in the.where Inside the call .address When the time comes, it is also the time to read theSon of the instance of .address Properties. Therefore, there is noFather class to readGrandFather The case of the Son class. From start to finish, it is instances of the Son class that are performing the various operations.

So, in this example, when inheritance is used, all the properties and methods of the parent class, if the child class has the same name, then the child class's will prevail. If the subclass is undefined, then the properties and methods of the parent class will actually run into the subclass.All operations that appear to be performed by the parent class are actually performed by the child class . The code above can even be approximated as equivalent:

due tosay The method is defined in the subclass, so the subclass overrides the parent. The say method of the subclass prevails.where respond in singingaddress Since the subclass is not defined, theFather classwhere methodology andGrandFather inneraddress attributes, are run directly into the subclass.

This article on Python multiple inheritance in a weird phenomenon both Father and grandfather of the article is introduced to this, more related Python multiple inheritance in a weird phenomenon content please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!