Python Multiple Inheritance
In this article, you will learn the rhythm of Python and how to use your program. You will also learn the inheritance and the solution often.
Table of Contents:
Ø Multiple Inheritance in python
Ø Multilevel Inheritance in Python
Ø Method Resolution Order in Python
Multiple Inheritance in python:
Like C ++, the class may pick up more than one grade in the Python language. This is called a number of heritage.
As part of the heritage, she has all the basic classes inherited in the mixed class. The inheritance of the inheritance is as simplified as an inheritance.
Example:
class Base1:
pass
class Base2:
pass
class MultiDerived(Base1, Base2):
pass
Here, MultiDerived is taken from the Base1 BASE2 class.
Multilevel Inheritance in Python:
On the other hand, we can also integrate into a mixed class. This is called a lot of inheritance. They can be deep in Python.
At the level of the inheritance, language classes and classes are inherited in a new class of grades.
For example an indication of the following.
class Base:
pass
class Derived1(Base):
pass
class Derived2(Derived1):
pass
Here, Earthquakes 1 separates Base and Derived2 from Derived1.
Method Resolution Order in Python:
Each classroom in Python produces class items. This type is the most typical Python.
So, technically, in all other classes, or using or defining a combination of classroom items from all classroom organizations.
# Output: True
print(issubclass(list,object))
# Output: True
print(isinstance(5.5,object))
# Output: True
print(instance("Hello",object))
In inheritance cases, all features are included in the search for the current class. If you can not find it, the search continues with the first parent, the left-hand right corner without looking for the same grade twice.
Therefore, the highest example of the MultiDerived class, the search order is [MultiDerived, Base1, BASE2, Item]. This combination is also called MultiDerived class linearization and many rules used to find the Order Order Order (MRO).
>>> MultiDerived.__mro__
(<class '__main__.MultiDerived'>,
<class '__main__.Base1'>,
<class '__main__.Base2'>,
<class 'object'>)
>>> MultiDerived.mro()
[<class '__main__.MultiDerived'>,
<class '__main__.Base1'>,
<class '__main__.Base2'>,
<class 'object'>]
MRO is to avoid asking for local priority as well as monotony. This ensures that each classroom is reflected in their parents and in some parents cases, similar to elementary.
MRO is a classroom that can be used to describe or to-date (MRO). The order restores the return address on the last list.
Here are a few examples of the inheritance and some of the photos along with the MRO.
Class X: pass
Class Y: pass
Class Y: pass
Class Z: pass
Class A(X, Y): pass
Class B(Y, Z): pass
Class M(B,A,Z): pass
# Output:
# [<class
'__main__.M'>, <class '__main__.B'>,
# <class
'__main__.A'>, <class '__main__.X'>,
# <class
'__main__.Y'>, <class '__main__.Z'>,
# <class
'object'>]
print(M.mro())
Comments
Post a Comment