In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "case Analysis of Python Diamond inheritance". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Inheritance is an important way of object-oriented programming, through inheritance, subclasses can extend the functionality of the parent class. In python, a class can inherit from more than one parent class, which is called python multiple inheritance (Multiple Inheritance).
Grammar
Class SubclassName (BaseClass1, BaseClass2, BaseClass3,...): pass
Diamond inheritance
When multi-tier inheritance and multi-inheritance are used at the same time, there will be a complex inheritance relationship, multiple inheritance.
Among them, there will be diamond inheritance. This is shown in the following figure. Mark
In this structure, there is confusion in the order of invocation. Which of the following is the order of invocation?
D-> B-> A-> C (depth first) D-> B-> C-> A (breadth first)
Let's answer this question.
Let's take an example:
Class A (): def _ _ init__ (self): print ('init A..') Print ('end A..') class B (A): def _ _ init__ (self): print ('init B..') A.The initableness _ (self) print ('end B..') class C (A): def _ _ init__ (self): print ('init C..') A.The initableness _ (self) print ('end C..') class D (B, C): def _ _ init__ (self): print ('init D.') B.The initableness _ (self) C.The initableness _ (self) print ('end D..') if _ _ name__ = ='_ _ main__': D ()
Output result
Init D... Init B... Init A... End A... End B... Init C... Init A... End A... End C... End D...
From the output, the order of invocation is D-> B-> A-> C-> A. As you can see, B and C both inherit from AMagi An and are called twice. A there is no need to repeat the call twice.
In fact, the root causes of the above problems are related to MRO, MRO (Method Resolution Order), also known as method parsing order, is mainly used in multiple inheritance to determine which class the attributes of the tone come from, which uses an algorithm called C3, whose basic idea is to use breadth-first and left-to-right principles to find needed attributes and methods on the premise of avoiding multiple calls to the same class.
So how to prevent a method in the top-level parent class from being called many times? at this time, super () is needed to play a role. Super is essentially a class, and the MRO information is recorded internally. Because the C3 algorithm ensures that the same class will only be searched once, this prevents the methods in the top-level parent class from being executed multiple times. The above code can be changed to:
Class A (): def _ _ init__ (self): print ('init A..') Print ('end A..') class B (A): def _ _ init__ (self): print ('init B..') Super (B, self). _ _ init__ () print ('end B..') class C (A): def _ _ init__ (self): print ('init C.') Super (C, self). _ init__ () print ('end C..') class D (B, C): def _ init__ (self): print ('init D.') Super (D, self). _ _ init__ () print ('end D..') if _ _ name__ ='_ _ main__': D ()
Output result:
Init D... Init B... Init C... Init A... End A... End C... End B... End D...
As you can see, the order of calls at this time is D-> B-> C-> A. That is, the traversal mode which is breadth first is adopted.
Supplementary content
There are two kinds of Python classes, one is called classical class, the other is called new class. All support multiple inheritance, but the inheritance order is different.
New class: a class inherited from object. (for example: class A (object)), inherit by breadth-first search (that is, horizontal search first, and then upward search).
Classic class: a class that does not inherit from object. (for example: class A ()), inherit by depth-first search (that is, first go deep into the left side of the inheritance tree, then return, and then find the right side).
There are two kinds of classes in Python2.x: classical class and new class. Python3.x is full of new classes.
This is the end of the case analysis of Python diamond inheritance. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.