Several concepts of object-oriented programming in Python

Source: Internet
Author: User

    • Python Super () function

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:

      1. Parent class name. __init__ (self, parameter) #注意名字是父类
      2. 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

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.