Python: Super

Source: Internet
Author: User

The definition of object methods in python is very strange. The first parameter is generally named self (equivalent to this in other languages), 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.

A common example is as follows:

>>> Class FOO:
Def bar (self, message ):
Print (Message)

>>> Foo (). Bar ("Hello, world .")
Hello, world.

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, world .")
Hello, world.

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, world .")
Hello, world.

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:
Def _ init _ (Self ):
Print ("enter ")
Print ("Leave ")

Class B ():
Def _ init _ (Self ):
Print ("Enter B ")
A. _ init _ (Self)
Print ("leave B ")

Class C ():
Def _ init _ (Self ):
Print ("Enter C ")
A. _ init _ (Self)
Print ("Leave C ")

Class D ():
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:
Def _ init _ (Self ):
Print ("enter ")
Print ("Leave ")

Class B ():
Def _ init _ (Self ):
Print ("Enter B ")
Super (B, self). _ init __()
Print ("leave B ")

Class C ():
Def _ init _ (Self ):
Print ("Enter C ")
Super (C, self). _ init __()
Print ("Leave C ")

Class D ():
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 sequence, the class is executed according to Mro (E. _ Mro __).

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.