Examples of usage of super in Python

Source: Internet
Author: User
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). In short, the experience left behind is: to maintain consistency. Either call the parent class with the class name, or use Super, not half.

Common inheritance

The code is as follows:


Class Fooparent (object):
def __init__ (self):
self.parent = ' I\ ' m the parent. '
print ' Parent '

def bar (Self,message):
Print message, ' from Parent '

Class Foochild (fooparent):
def __init__ (self):
Fooparent.__init__ (self)
print ' child '

def bar (Self,message):
Fooparent.bar (Self,message)
print ' Child bar function. '
Print Self.parent

If __name__== ' __main__ ':
Foochild = Foochild ()
Foochild.bar (' HelloWorld ')

Super inheritance

The code is as follows:


Class Fooparent (object):
def __init__ (self):
self.parent = ' I\ ' m the parent. '
print ' Parent '

def bar (Self,message):
Print message, ' from Parent '

Class Foochild (fooparent):
def __init__ (self):
Super (Foochild,self). __init__ ()
print ' child '

def bar (Self,message):
Super (Foochild, self). Bar (Message)
print ' Child Bar fuction '
Print Self.parent

if __name__ = = ' __main__ ':
Foochild = Foochild ()
Foochild.bar (' HelloWorld ')

The program runs the same result as:

The code is as follows:


Parent
Child
HelloWorld from Parent
Child Bar Fuction
I ' m the parent.


From the running results, normal inheritance and super inheritance are the same. But in fact, their internal operating mechanism is different, this is reflected in the multiple inheritance is obvious. In the super mechanism, the public parent class is guaranteed to be executed only once, and the order of execution is in accordance with MRO (E.__MRO__).
Note that super inheritance can only be used in modern classes and will be used for the classic class.
New class: Must have an inherited class, if there is nothing to inherit, then inherit object
Classic class: There is no parent class, and if you call super at this point there will be an error: "Super () Argument 1 must is type, not Classobj"

For a detailed study of super usage, refer to "http://www.bitscn.com/article/66912.htm"

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