The difference between super () and __init__ () in the Python class

Source: Internet
Author: User
The ability to implement super () and __init__ () is similar for single-inheritance

Class Base (object):d EF __init__ (self):p rint ' base create ' class Childa (base):d EF __init__ (self):p rint ' creat A ', base.__ Init__ (Self) class childb (base):d the EF __init__ (self):p rint ' creat B ', super (childb, self). __init__ () base = Base () A = Childa () b = childb ()

Output Result:

Base Createcreat A Base createcreat B Base Create

The difference is that you use super () inheritance without explicitly referencing the base class.

Super () can only be used in modern classes

Change base class to Legacy class, that is, do not inherit any base class

Class Base ():d ef __init__ (self):p rint ' base create '

When executing, an error occurs when initializing B:

Super (CHILDB, self). __init__ () Typeerror:must is type, not classobj

Super is not the parent class, but the next class of inheritance order

In multiple inheritance, the inheritance order is involved, and super () is equivalent to the next class that returns the inheritance order, not the parent class, similar to the functionality:

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

MRO () is used to obtain the class inheritance order.

For example:

Class Base (object):d EF __init__ (self):p rint ' base create ' class Childa (base):d EF __init__ (self):p rint ' Enter A ' # base.__ Init__ (self) Super ("Childa, Self"). __init__ () print ' Leave A ' class Childb (Base):d ef __init__ (self):p rint ' Enter B ' # base._ _init__ (self) Super (childb, self). __init__ () print ' Leave B ' class CHILDC (Childa, childb):p ASSC = CHILDC () print C.__class __.__mro__

The output results are as follows:

Enter a enter B Base createleave bleave A (
 
   
  
    
   
     
    
     
      
     , 
     , 
      
       ,,
      )
     
      
    
     
   
    
  
   
 
  

The 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 (), the first call to the super (Childa, self) in Childa's construction method. __init__ (), super (Childa, self) Returns a class childb after Childa in the inheritance order of the current class, and then executes Childb (). __init () __, in this order.

In multiple inheritance, if the super (Childa, self) in Childa () is changed to base.__init__ (self), when executed, the inheritance Childa will jump directly into the base class and skip the CHILDB:

Enter a Base createleave a (,,,
 
   
  
    
   
     
    
     
      
     , 
     
      
       
      )
     
      
    
     
   
    
  
   
 
  

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

If it is itself, it will inherit the next class in succession;

If it is in the inheritance chain before the class will be infinite recursion down;

If the following class is in the inheritance chain, the class between the inheritance chain summary itself and the passed-in class is ignored;

For example, change the super in Childa () to: Super (CHILDC, self). Init (), the program will recursively go down indefinitely.

Such as:

File "c:/users/administrator/desktop/crawler/learn.py", line ten, in __init__super (CHILDC, self). __init__ () file "c:/ users/administrator/desktop/crawler/learn.py ", line ten, in __init__super (CHILDC, self). __init__ () File" c:/users/ administrator/desktop/crawler/learn.py ", line ten, in __init__super (CHILDC, self). __init__ () File" c:/users/ administrator/desktop/crawler/learn.py ", line ten, in __init__super (CHILDC, self). __init__ () File" c:/users/ administrator/desktop/crawler/learn.py ", line ten, in __init__super (CHILDC, self). __init__ () File" c:/users/ administrator/desktop/crawler/learn.py ", line ten, in __init__super (CHILDC, self). __init__ () File" c:/users/ administrator/desktop/crawler/learn.py ", line ten, in __init__super (CHILDC, self). __init__ () File" c:/users/ administrator/desktop/crawler/learn.py ", line ten, in __init__super (CHILDC, self). __init__ () File" c:/users/ administrator/desktop/crawler/learn.py ", line ten, in __init__super (CHILDC, self). __init__ () File" c:/users/ administrator/desktop/crawler/learn.py ", line ten, in __init__super (CHILDC, self). __init__ () File" c:/users/administrator/desktop/crawler/learn.py ", Line ten, in __init__super (CHILDC, self). __init__ () File "c:/users/administrator/desktop/crawler/learn.py", line ten, in _ _init__super (CHILDC, self). __init__ () File "c:/users/administrator/desktop/crawler/learn.py", line ten, in __init__ Super (CHILDC, self). __init__ () runtimeerror:maximum recursion depth exceeded while calling a Python object

Super () can avoid repeated calls

If Childa base base, Childb inherits Childa and base, if CHILDB needs to call Base's __init__ () method, it will cause __init__ () to be executed two times:

Class Base (object):d EF __init__ (self):p rint ' base create ' class Childa (base):d EF __init__ (self):p rint ' Enter A ' base.__ Init__ (self) print ' leave A ' class Childb (Childa, Base):d EF-__init__ (self): childa.__init__ (self)-base.__init__ (self) b = Childb ()

The base's __init__ () method was executed twice

Enter A Base Createleave Abase Create

Using super () is to avoid repeated calls

Class Base (object):d EF __init__ (self):p rint ' base create ' class Childa (base):d EF __init__ (self):p rint ' Enter A ' super ( Childa, self). __init__ () print ' Leave A ' class Childb (Childa, Base):d EF-__init__ (self): Super (Childb, self). __init__ () b = Childb () Print B.__class__.mro () Enter A Base createleave a[
 
   
  
    
   
     
    
     
      ,,,
     ]
    
     
   
    
  
   
 
  

The above is a small series to introduce you to the Python class super () and __init__ () of the difference, I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of the Scripting House website!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.