Content should be:
- What is MRO?
- What problems does Super mainly solve?
- The code examples
MRO:
Method Resolution Order is mainly used to determine the invocation path of methods and attributes in multiple inheritance. It uses a C3 algorithm, which I won’t describe in detail here, but will summarize later.
super
- Calling the parent class, that’s easy to understand;
- To solve the problem of multiple inheritance, directly call the parent class method with the class name in the use of single inheritance is no problem, but if the use of multiple inheritance, will involve the search order (MRO), repeated calls (diamond inheritance) and other problems. Super is called one by one in MRO order in multiple inheritance, avoiding the problem of repeated execution of superclass methods caused by ‘class name calls’ in multiple inheritance.
The code examples
1, class name
class Parent(object) :
def __init__(self, name) :
print('Parent's init starts being called')
self.name = name
print('Parent's init ends called')
class Son1(Parent) :
def __init__(self, name, age) :
print(Son1 init is getting called)
self.age = age
Parent.__init__(self, name)
print('Son1 init ends called ')
class Son2(Parent) :
def __init__(self, name, gender) :
print(Son2 init is getting called)
self.gender = gender
Parent.__init__(self, name) #
print('Son2 init ends called ')
class Grandson(Son1, Son2) :
def __init__(self, name, age, gender) :
print('Grandson's init is getting called. ')
Son1.__init__(self, name, age)
Son2.__init__(self, name, gender)
print('Grandson's init ends being called. ')
gs = Grandson('grandson'.12.'male')
print('Name:', gs.name)
print('Age:', gs.age)
print('Gender:', gs.gender)
# Execution result
# Grandson's init starts being called
# Son1 init starts being called
# parent init starts being called
The # parent init end is called
The init end of # Son1 is called
# Son2 init starts getting called
# parent init starts being called
The # parent init end is called
The init end of # Son2 is called
# Grandson's init end is called
# Name: Grandson
# Age: 12
# Gender: Male
Copy the code
2. Super call
class Parent(object) :
def __init__(self, name, *args, **kwargs) :
print('Parent's init starts being called')
self.name = name
print('Parent's init ends called')
class Son1(Parent) :
def __init__(self, name, age, *args, **kwargs) :
print(Son1 init is getting called)
self.age = age
super().__init__(name, *args, **kwargs)
print('Son1 init ends called ')
class Son2(Parent) :
def __init__(self, name, gender, *args, **kwargs) :
print(Son2 init is getting called)
self.gender = gender
super().__init__(name, *args, **kwargs)
print('Son2 init ends called ')
class Grandson(Son1, Son2) :
def __init__(self, name, age, gender) :
print('Grandson's init is getting called. ')
super().__init__(name, age, gender)
print('Grandson's init ends being called. ')
print(Grandson.__mro__)
gs = Grandson('grandson'.12.'male')
print('Name:', gs.name)
print('Age:', gs.age)
print('Gender:', gs.gender)
# Execution result
# (<class '__main__.Grandson'>, <class '__main__.Son1'>, <class '__main__.Son2'>, <class '__main__.Parent'>, <class 'object'>)
# Grandson's init starts being called
# Son1 init starts being called
# Son2 init starts getting called
# parent init starts being called
The # parent init end is called
The init end of # Son2 is called
The init end of # Son1 is called
# Grandson's init end is called
# Name: Grandson
# Age: 12
# Gender: Male
Copy the code
You can see the ‘class name’ by comparing the results. The method name ‘method causes repeated execution of the parent method during execution.
The content of this article refers to the content of the Internet