A study of super usage of Python

Source: Internet
Author: User

Source: http://www.cnblogs.com/dkblog/archive/2011/02/24/1980654.html

The definition of an object method in Python is bizarre, and the first parameter is generally named self (equivalent to this in other languages), which is used to pass the object itself, and it is not explicitly passed when called, and the system is automatically passed.

To cite a very common example:

>>> class Foo:
def bar (self, message):
Print (message)

>>> Foo () bar ("Hello, World.")
Hello, world.

When there is an inheritance relationship, sometimes it is necessary to call a method of the parent class in the subclass, and the simplest method is to convert the object invocation into a class call, and it is important to note that the self parameter needs to be passed explicitly at this point, for example:

>>> class Fooparent:
def bar (self, message):
Print (message)

>>> class Foochild (fooparent):
def bar (self, message):
Fooparent.bar (self, message)

>>> foochild (). Bar ("Hello, World.")
Hello, world.

There are some drawbacks to this, for example, if you modify the parent class name, there will be multiple modifications in the subclass, and Python is a language that allows multiple inheritance, and the method shown above is redundant when multiple inheritance is required. To solve these problems, Python introduced 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, World.")
Hello, world.

On the surface, super (foochild, self). The result of the bar (message) method and the Fooparent.bar (self, message) method is consistent, in fact, the internal processing mechanism of the two methods is significantly different, when multiple inheritance cases are involved, Will show the obvious difference, directly to the example:

Code One:

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 ()

Results:

Enter E
Enter B
Enter A
Leave A
Leave B
Enter C
Enter A
Leave A
Leave C
Enter D
Enter A
Leave A
Leave D
Leave E

The order of execution is well understood, and the only thing to note is that the public parent class A has been executed multiple times.

Code two:

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 ()

Results:

Enter E
Enter B
Enter C
Enter D
Enter A
Leave A
Leave D
Leave C
Leave B
Leave E

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__).

E.__MRO__[1] () equivalent to B ()

A study of super usage of 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.