Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to understand the inheritance of python Classical Class and New Class

Shulou Source: shulou.com Published: 2022-06-01 17:56:38 10月01日 Update

How to understand the inheritance of python classic classes and new classes, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Example 1:

Class a:

Def p (self):

Print "aaaaaaaaaaaa"

Class b (a):

Pass

Class c (a):

Def p (self):

Print "cccccccccccc"

Class d (bmaine c):

Pass

D = d ()

D.P ()

# the result is aaaaaaaaaaa

Example 2:

Class a:

Def p (self):

Print "aaaaaaaaaaaa"

Class b (a):

Pass

Class c (a):

Def p (self):

Print "cccccccccccc"

Class d (c) b):

Pass

D = d ()

D.P ()

# the result is cccccccccccc

Example 3:

Class a:

Pass

Class b (a):

Pass

Class c (a):

Def p (self):

Print "cccccccccccc"

Class d (bmaine c):

Pass

D = d ()

D.P ()

# the result is cccccccccccc

Conclusion: it can be seen from the above that, corresponding to the classical classes that do not inherit object, the case search method adopts the idea of depth first.

As shown in the above example, the p method of D first finds out whether d itself has the method, if not, it goes up to the first parent class to see if it has the method, and if not, to find out whether the first parent class has the method until the root.

If none, the root is found from the second parent class.

Example 4:

Class a (object):

Def p (self):

Print "aaaaaaaaaaaa"

Class b (a):

Pass

Class c (a):

Def p (self):

Print "cccccccccccc"

Class d (bmaine c):

Pass

D = d ()

D.P ()

# the result is cccccccccccc

Conclusion: for the new class inherited from object, the breadth-first idea is adopted in the case search method.

As shown in the above example, the p method of D first finds whether d itself has the method, if not, it goes up to the first parent class to see if it has the method, and if not, it finds out whether the other parent classes of the same level have the method, and then searches for the parent class at a higher level after searching the siblings, until the root.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Methods examples results D.P new classic sibling such as above examples ideas conclusions help search clarity content this breadth articles novices more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Shulou Technology Docker Shulou Tech Info Shulou Information