Detailed explanation of the principle of Python super() method

When learning Python classes, you will encounter a function like init() in the class, which is actually the construction method of Python.

The construction method is similar to the initialization method like init() to initialize the state of the newly created object. It will be called immediately after an object is created, such as instantiating a class:

f = FooBar()
f.init()#Manual initialization

It can be simplified into the following form by using the construction method: the magic method init() is automatically called after the object is created to initialize the object

f = FooBar()

After understanding the construction method, come to an advanced question, that is, the problem that the initial value in the construction method of the parent class cannot be inherited.

classBird:
 def __init__(self):
  self.hungry = True
 def eat(self):if self.hungry:
  print 'Ahahahah'else:
  print 'No thanks!'classSongBird(Bird):
 def __init__(self):
  self.sound ='Squawk'
 def sing(self):
  print self.song()

sb =SongBird()
sb.sing()  #Can output normally
sb.eat()   #Report an error because there is no hungry feature in songgird

There are two ways to solve this problem:

  1. Call the unbound super class constructor (not recommended)
classSongBird(Bird):
 def __init__(self):
  Bird.__init__(self) #
  self.sound ='Squawk'
 def sing(self):
  print self.song()

Principle: When the method of an instance is called, the self parameter of the method will be automatically bound to the instance (called the binding method); if the method of the class (such as Bird.init) is called directly, then no instance will be Binding, you can freely provide the required self parameter (unbound method).

  1. Use super function (recommended)
classSongBird(Bird):
 def __init__(self):super(SongBird,self).__init__()
  self.sound ='Squawk'
 def sing(self):
  print self.song()

Principle: It will find all superclasses and superclasses of superclasses until it finds the required characteristics.

The super() function is a method used to call the parent class (super class).

super is used to solve the problem of multiple inheritance. It is okay to directly call the parent class method with the class name when using single inheritance, but if you use multiple inheritance, it will involve the search order (MRO) and re

Recall (diamond inheritance) and other issues.

MRO is the method resolution sequence table of the class, in fact, it is the sequence table when inheriting the methods of the parent class. (Novice Document)

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Detailed explanation of the principle of Python super() method
Detailed explanation of the principle of Python function parameter classification
Detailed explanation of the principle of Python timer thread pool
Detailed explanation of the usage of Python decimal module
Detailed explanation of the implementation steps of Python interface development
Detailed explanation of the attribute access process of Python objects
Detailed explanation of the remaining problem based on python (%)
Detailed explanation of python backtracking template
Detailed explanation of python sequence types
End the method of running python
Detailed explanation of Python IO port multiplexing
The specific method of python instantiation object
Detailed explanation of -u parameter of python command
Detailed explanation of Python guessing algorithm problems
The specific method of python import library
Detailed explanation of the use of pip in Python | summary of third-party library installation
Detailed explanation of python standard library OS module
Detailed explanation of how python supports concurrent methods
Detailed explanation of data types based on Python
Detailed explanation of static DNS configuration method in Ubuntu
Detailed explanation of common tools for Python process control
Detailed explanation of Python web page parser usage examples
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
Python method of parameter passing
Consolidate the foundation of Python (3)
The usage of wheel in python
Summary of logarithm method in Python
Detailed implementation of Python plug-in mechanism
Detailed explanation of ubuntu using gpg2
Python handles the 4 wheels of Chinese
Python error handling assert detailed explanation
Python simulation of the landlord deal
What is the use of Python
Detailed usage of dictionary in Python
Python implements the steepest descent method
The premise of Python string pooling
Secrets of the new features of Python 3.8
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
Detailed explanation of the installation and use of SSH in the Ubuntu environment
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
What are the required parameters of python
Logistic regression at the bottom of python
The usage of Ajax in Python3 crawler
Python solves the Tower of Hanoi game
Solve the conflict of multiple versions of python
What is the scope of python variables
Python implements the sum of fractional sequences
Detailed analysis of Python garbage collection mechanism
Two days of learning the basics of Python
What is the id function of python
Implementation principle of dynamic binding of Python classes
Python from attribute to property detailed explanation
Where is the pip path of python3
Method of installing django module in python