This article mainly introduces the differences between super () and _ init _ () in the python class. This article is very detailed and has some reference value, you can refer to the functions implemented by super () and _ init _ () when inheriting a ticket.
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
The difference is that the base class is not explicitly referenced when super () is used for inheritance.
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'
During execution, when initializing B, an error is reported:
super(childB, self).__init__()TypeError: must be type, not classobj
Super is not the parent class, but the next class that inherits the sequence.
In multi-inheritance, the inheritance sequence is involved. super () is equivalent to returning the next class in the inheritance sequence, rather than the parent class. This function is similar to the following:
def super(class_name, self):mro = self.__class__.mro()return mro[mro.index(class_name) + 1]
Mro () is used to obtain the inheritance sequence of 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):passc = childC()print c.__class__.__mro__
The output result is as follows:
enter A enter B Base createleave Bleave A(
,
,
,
,
)
Supder is not associated with the parent class, so the execution order is A-> B-> Base
The execution process is equivalent to: when childC () is initialized, super (childA, self) in the childA constructor is called first ). _ init _ (), super (childA, self) returns a childB class after childA in the inheritance sequence of the current class, and then executes childB (). _ init () __, which is executed in this order.
In multi-inheritance, if super (childA, self) in childA () is set ). _ init _ () is replaced with Base. _ init _ (self): During execution, after inheriting childA, it will directly jump to the Base class, skipping childB:
enter A Base createleave A(
,
,
,
,
)
The super () method shows 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 an inheritance chain, the previous classes will be infinitely recursive;
If it is the class after the inheritance chain, the class between the inheritance chain summary itself and the incoming class will be ignored;
For example, if the super in childA () is changed to super (childC, self). init (), the program will recursion infinitely.
For example:
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 object
Super () can avoid repeated calls
If childA Base, childB inherits childA and Base, if childB needs to call the Base's _ init _ () method, it will lead to _ init __() 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 Base _ init _ () method is executed twice.
enter A Base createleave ABase create
Super () is used 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[
,
,
,
]
The above is the difference between super () and _ init _ () in the python class introduced by the small Editor. I hope it will help you, if you have any questions, please leave a message and the editor will reply to you in time. I would like to thank you for your support for the script home website!