Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the difference between super () and _ _ init__ () in the python class

Shulou Source: shulou.com Published: 2022-06-03 00:17:08 09月15日 Update

In this issue, the editor will bring you about the difference between super () and _ _ init__ () in the python class. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Preface

Today we're going to talk about the difference between super () and-- init_ (), the idea of unitary inheritance.

1. The functions of super () and init () are similar in single inheritance.

Class Base (object): def _ init__ (self): print 'Base create' class childA (Base): def _ init__ (self): print' creat A', Base.__init__ (self) class childB (Base): def _ init__ (self): print 'creat B', super (childB, self). _ _ init__ () base = Base () a = childA () b = childB ()

Output result:

Base createcreat A Base createcreat B Base create

You do not have to explicitly reference the base class when using super () inheritance.

2. Super () can only be used in new classes

Change the base class to the old class, that is, do not inherit any base class

Class Base (): def _ _ init__ (self): print 'Base create'

When executed, an error is reported when b is initialized:

Super (childB, self). _ _ init__ () TypeError: must be type, not classobj3, super are not the parent class, but the next class in the inheritance order

The inheritance order is involved in multiple inheritance, and super () is equivalent to returning the next class in the inheritance order, rather than the parent class, similar to this:

Def super (class_name, self): mro = self.__class__.mro () return mro [mro.index (class_name) + 1]

Mro () is used to get the inheritance order of the class.

For example:

Class Base (object): def _ init__ (self): print 'Base create' class childA (Base): def _ init__ (self): print' enter A'# Base.__init__ (self) super (childA Self). _ init__ () print 'leave A'class childB (Base): def _ _ init__ (self): print' enter B' # Base.__init__ (self) super (childB, self). _ _ init__ () print 'leave B'class childC (childA, childB): pass c = childC () print c.classrooms classrooms.

The input results are as follows:

Enter An enter B Base createleave Bleave A (,)

Supder is not associated with the parent class, so the order of execution is A-> B->-> Base.

The execution process is equivalent to: when initializing childC (), it first calls super (childA, self). Init () in the constructor of childA, and super (childA, self) returns a class childB; after childA in the inheritance order of the current class, and then executes childB (). Init (), which goes on in this order.

In multiple inheritance, if you replace super (childA, self). Init () in childA () with Base.init (self), after inheriting childA, you will directly skip to the Base class and skip childB:

Enter A Base createleave A (,)

As you can see from the super () method, the first parameter of super () can be the name of any class in the inheritance chain

If it is, it will inherit the next class in turn.

If it is the previous class in the inheritance chain, it will be recursive infinitely.

If it is the later class in the inheritance chain, the class between the inheritance chain summary itself and the incoming class will be ignored.

For example, change the super in childA () to: super (childC, self). Init (), the program will be infinitely recursive.

Such as:

File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in _ _ init__ super (childC, self). _ _ init__ () File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in _ init__ super (childC, self). _ _ init__ () File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in _ init__ super (childC Self). _ init__ () File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in _ init__ super (childC, self). _ _ init__ () File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in _ init__ super (childC, self). _ init__ () File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in _ init__ super (childC) Self). _ init__ () File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in _ init__ super (childC, self). _ _ init__ () File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in _ init__ super (childC, self). _ init__ () File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in _ init__ super (childC) Self). _ init__ () File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in _ init__ super (childC, self). _ _ init__ () File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in _ init__ super (childC, self). _ init__ () File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in _ init__ super (childC) Self). _ init__ () File "C:/Users/Administrator/Desktop/crawler/learn.py", line 10, in _ _ init__ super (childC, self). _ _ init__ () RuntimeError: maximum recursion depth exceeded while calling a Python object4, super () can avoid repeated calls

If the childA base Base, childB inherits childA and Base, and if childB needs to call the init () method of Base, it will cause init () to be executed twice:

Class Base (object): def _ init__ (self): print 'Base create' class childA (Base): def _ init__ (self): print' enter A' Base.__init__ (self) print 'leave A'class childB (childA, Base): def _ init__ (self): childA.__init__ (self) Base.__init__ (self) b = childB ()

The init () method of Base is executed twice

Enter ABase createleave ABase create

Use super () to avoid repeated calls

Class Base (object): def _ init__ (self): print 'Base create' class childA (Base): def _ init__ (self): print' enter A' super (childA, self). _ init__ () print 'leave A' class childB (childA, Base): def _ init__ (self): super (childB) Self). _ init__ () b = childB () print b.__class__.mro () enter A Base createleave A [,] this is the difference between super () and _ _ init__ () in the python class shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Order method between content function result recursion analysis new style no use professional small and medium rich in content preface parameters name foundation that is articles more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker vpn Shulou Tech Info macOS Linux