Describes the functions and principles of python's super ().

Source: Internet
Author: User
The role of the super () in python and the definition of object methods in Python are weird. The first parameter is generally named self (equivalent to this in other languages), which is used to pass the object itself, when a call is made, it does not need to be explicitly transmitted, and the system will automatically pass it.

The main character we introduced today is super (). in the inheritance of classes, super () is very commonly used. it solves some problems of subclass calling the parent class method, when the parent class is called multiple times, the execution logic is optimized. let's take a look at it in detail.


For example:

class Foo:  def bar(self, message):    print(message)

>>> Foo().bar("Hello, Python.")Hello, Python.

When there is an inheritance relationship, you sometimes need to call the method of the parent class in the subclass. The simplest way is to convert the object call to a class call, note that the self parameter needs to be passed explicitly, for example:

class FooParent:  def bar(self, message):    print(message)class FooChild(FooParent):  def bar(self, message):    FooParent.bar(self, message)

>>> FooChild().bar("Hello, Python.")Hello, Python.


This method has some disadvantages. for example, if the parent class name is modified, multiple modifications are involved in the subclass. In addition, Python allows multiple inheritance languages, the method shown above needs to be repeatedly written multiple times for multi-inheritance, which is cumbersome. To solve these problems, Python introduces the super () mechanism. The example code is as follows:

class FooParent:  def bar(self, message):    print(message)class FooChild(FooParent):  def bar(self, message):    super(FooChild, self).bar(message)


>>> FooChild().bar("Hello, Python.")Hello, Python.

On the surface, super (FooChild, self ). bar (message) method and FooParent. the results of bar (self, message) methods are consistent. In fact, the internal processing mechanisms of these two methods are significantly different. when multiple inheritance conditions are involved, there will be significant differences, for example:


Code 1:

class A:  def __init__(self):    print("Enter A")    print("Leave A")class B(A):  def __init__(self):    print("Enter B")    A.__init__(self)    print("Leave B")class C(A):  def __init__(self):    print("Enter C")    A.__init__(self)    print("Leave C")class D(A):  def __init__(self):    print("Enter D")    A.__init__(self)    print("Leave D")class E(B, C, D):  def __init__(self):    print("Enter E")    B.__init__(self)    C.__init__(self)    D.__init__(self)    print("Leave E")E()


Result:


Enter E

Enter B

Enter

Leave

Leave B

Enter C

Enter

Leave

Leave C

Enter D

Enter

Leave

Leave D

Leave E


The execution sequence is well understood. The only thing you need to note is that the common parent class A is executed multiple times.


Code 2:

class A:  def __init__(self):    print("Enter A")    print("Leave A")class B(A):  def __init__(self):    print("Enter B")    super(B, self).__init__()    print("Leave B")class C(A):  def __init__(self):    print("Enter C")    super(C, self).__init__()    print("Leave C")class D(A):  def __init__(self):    print("Enter D")    super(D, self).__init__()    print("Leave D")class E(B, C, D):  def __init__(self):    print("Enter E")    super(E, self).__init__()    print("Leave E")E()


Result:


Enter E

Enter B

Enter C

Enter D

Enter

Leave

Leave D

Leave C

Leave B

Leave E


In the super mechanism, the public parent class can be executed only once. As for the execution Order, it is in the MRO (Method Resolution Order): Method Resolution Order. This MRO mechanism will be described in detail later.

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.