Example analysis of super keyword usage in python

Source: Internet
Author: User
Tags class definition in python

This example describes the Super keyword usage in python. Share to everyone for your reference. The specific analysis is as follows:

In the Python class method, you call a method of the parent class, which, before Python 2.2, is typically written like code Snippet 1:

Code Snippet 1:

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 Class A:def __init__ (self): print "Enter a" print "Leave A" class B (a): Def __init__ (self): print "Enter B" a.__init__ (SE LF) print "Leave B" >>> B = B () Enter B Enter a leave a leave B

That is, use the unbound class method (the method referenced by the class name) and, in the argument list, introduce the object to be bound (self) to achieve the purpose of invoking the parent class.

The disadvantage of this is that when a subclass's parent class changes (such as Class B's parent class from A to C), you must traverse the entire class definition, replacing all the class names of the unbound methods, such as code Snippet 2,

Code Snippet 2:

?

1 2 3 4 5 Class B (c): # a--> C def __init__ (self): print "Enter B" c.__init__ (self) # a--> C print "Leave B"

If the code is simple, such changes may be acceptable. But if the amount of code is large, such modifications can be catastrophic. It is easy to cause a modification error to occur.

So, since Python 2.2, Python has added a keyword super to solve this problem. Here is the official documentation note for Python 2.3:

?

1 2 3 4 5 6 7 8 9 10 Super (type[, Object-or-type]) return the superclass of type. If The second argument is omitted to the Super object returned is unbound. If The second argument is a object, isinstance (obj, type) must be true. If The second argument is a type, Issubclass (type2, type) must to be true. Super () only works for New-style classes. A typical use to calling a cooperative superclass method Is:class C (B): def meth (self, arg): Super (C, self). Meth (ARG) Ne W in version 2.2.

From the instructions, you can rewrite class B as code Snippet 3:

Code Snippet 3:

?

1 2 3 4 5 6 7 8 9 Class A (object): # A must be New-style class Def __init__ (self): print "Enter a" print "Leave A" class B (c): # a--> C def __init__ (self): print "Enter B" Super (b, self). __init__ () print "Leave B"

Try to execute the same code above, the result is consistent, but the modified code is only one place, the maintenance of the code to the minimum, is a good use. So in our development process, the Super keyword is used heavily and has been performing well.

1. Super is not a function, is a class name, like Super (B, self) in fact, called the Super class initialization function, produced a Super object;

2. The initialization function of super class does not do any special operation, but simply records the class type and concrete instance;

3. Super (B, self). Func calls are not Func functions used to invoke the parent class of the current class;

4. Python's multiple-inheritance class is the MRO approach to ensure that functions of each parent class are invoked one at a time, and that every parent class function is called only once (if each class uses Super);

5. Mixing super and unbound functions is a risky behavior that may cause the parent class function that should be invoked to not call or a parent class function to be called multiple times.

From the Super keyword help we can also see.

Help on Class super in module __builtin__:

Class Super (object)

| Super (type)-> unbound Super Object

| Super (type, obj)-> bound Super object; Requires Isinstance (obj, type)

| Super (Type, type2)-> bound Super object; Requires Issubclass (type2, type)

| Typical use to call a cooperative superclass method:

| Class C (B):

| def meth (self, arg):

| Super (C, self). Meth (ARG)

|

| Methods defined here:

.......

As you can see from the above, super is a class and is in the __builtin__ module.

From the above description, super is mainly used to invoke the parent class method.

Then, the method before 2.2 can also be invoked. Why do you have to use super?

This is because super can block multiple calls to a parent class method.

Super, changing the search order of the parent class, returns a special parent class object

See Example:

Class A:pass Class B (a): Pass Class C (a):p class D (b, C): Pass

This is the basic relationship of 4 classes.

If you do not use Super, let D objects invoke a method that they have in common, and call this method in a 2 times.

I hope this article will help you with your Python programming.

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.