The super () function is a method for calling the parent class (the superclass).
Super is used to solve multiple inheritance problems, it is not a problem to call the parent class directly with the class name when using single inheritance, but if multiple inheritance is used, it involves various problems such as lookup order (MRO), repeated invocation (Diamond inheritance).
MRO is the method parsing order table of classes, in fact, the sequential table that inherits the parent class method.
One difference between python3.x and python2.x is that Python 3 can be used to directly use super (). XXX instead of super (Class, self). XXX:
python3.x instances:
Class A: Passclass B(a):def add(self, x ):Super(). Add(x)
python2.x instances:
class A (object # python2.x remember to inherit Object pass< Span class= "KWD" >class B (a def Add (self , X): super (b, self add (x)
#!/usr/bin/python#-*-coding:utf-8-*- classfooparent (object):def __init__(self): self.parent='I\ ' m the parent.' Print('Parent') defBar (self,message):Print("%s from Parent"%message)classFoochild (fooparent):def __init__(self):#Super (Foochild,self) first finds Foochild's parent class (that is, class fooparent) and then converts the object of Class B Foochild to the object of class FooparentSuper (Foochild,self).__init__() Print(' Child') defBar (Self,message): Super (Foochild, self). Bar (message)Print('Child Bar Fuction') Print(self.parent)if __name__=='__main__': Foochild=Foochild () Foochild.bar ('HelloWorld')
- How to specify the parent class when multiple inheritance???
super()The advantage is that you can avoid directly using the name of the parent class. But it is mainly used for multiple inheritance, there are a lot of fun things. If you don't know, you can look at the official documents.
- How the Python subclass calls the __init__ method of the parent class
Python does not automatically invoke the parent class's constructor, you have to call it yourself.
Two methods:
- Parent class name. __init__ (self, parameter) #注意名字是父类
- Super (subclass name, self). __init__ (parameter) #注意名字是子类, and after Init is a parameter other than self
#Coding=utf-8classPerson (object):def __init__(Self, name="Jim"): Self.name=name Self.flag=FalsePrint " Person", Self.namedefCall (self):PrintSelf.flag,"Name:", Self.name Self.flag= notSelf.flagclassProgrammer (person):def __init__(self): person.__init__(Self,"Dotjar") #第一种方法Print "Programmer" defSet_name (self,name): Self.name=name Coder=Programmer () Coder.call () Coder.set_name ("Dotjar") Coder.call ()
#Coding=utf-8classPerson (object):def __init__(Self, flag=false, name="Jim"): Self.name=name Self.flag=FlagPrint " Person", Self.namedefCall (self):PrintSelf.flag,"Name:", Self.name Self.flag= notSelf.flagclassProgrammer (person):def __init__(self, flag = True, name="Dotjar", age = 19): Self.age= 19Super (programmer,self).__init__(Flag,name) # The second methodPrint "Programmer ' s Age:", Self.agedefSet_name (self,name): Self.name=name
- The difference between abstract classes and interfaces in Python
https://code.i-harness.com/zh-CN/q/5ad4a
Several concepts of object-oriented programming in Python