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

Source: Internet
Author: User
Tags inheritance

Super () and __init__ () implementations are similar in the case of 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 results:

Base Create
creat A base Create
creat B Base Create

The difference is that the base class is not explicitly referenced when using super () inheritance.

Super () can only be used in new classes

Change the base class to a legacy class without inheriting any base classes

Class Base ():
def __init__ (self):
print ' base create '

When executed, the error occurs when B is initialized:

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

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

Inheritance order is involved in multiple inheritance, and super () is equivalent to returning the next class of inheritance order, not to the parent class, similar to the function:

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 classes.

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.__class__._ _mro__

The output results are as follows:

Enter a 
enter B 
Base create
leave b
Leave a
(<class ' __main__.childc ', <class ' __main__.) Childa ', <class ' __main__.childb ', <class ' __main__. Base ', <type ' object ' >)

Supder is not associated with the parent class, so the execution order is a-> b->->base

The execution process is equivalent to: when initializing CHILDC (), the super (Childa, self) in the Childa constructor is invoked first. __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 replaced by base.__init__ (self), when executed, the inheritance Childa jumps directly into the base class and skips over the CHILDB:

Enter a 
Base create
Leave a
(<class ' __main__.childc ', <class ' __main__.childa ', <class ') __main__.childb ', <class ' __main__. Base ', <type ' object ' >)

From the super () method, it can be seen that 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 turn;

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

If the class is later in the inheritance chain, the class between the inheritance chain rollup itself and the incoming class is ignored;

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

Such as:

File "c:/users/administrator/desktop/crawler/learn.py", line, 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, 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, the __init__ () is executed two times if childb need to invoke the base's __init__ () method:

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 ()

Base's __init__ () method was executed twice

Enter a 
base create
leave a
base 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 Create
leave A
[<class ' __main__.childb ', <class ' __main__.childa ', <class ' __main__ '. Base ', <type ' object ';

The above is a small set to introduce the Python class super () and __init__ () the difference between the hope for everyone to help, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.