How to use the Super keyword in python

Source: Internet
Author: User

Class A:
def __init__ (self):
Print "Enter A"
Print "Leave A"


Class B (A):
def __init__ (self):
Print "Enter B"
a.__init__ (self) # old method
Print "Leave B"


>>> B = B ()

Enter B
Enter A
Leave A
Leave B

The disadvantage of this is that when the parent class of a subclass changes (such as when the parent class of Class B changes from A to C), the entire class definition must be traversed, replacing all the class names of the unbound methods.

Since Python 2.2, Python has added a keyword super to solve this problem.

Super (Type [, Object-or-type])

Return the superclass of type. If The second argument is omitted the Super object
Returned is unbound. If The second argument is an object, isinstance (obj, type)
Must be true. If The second argument is a type, Issubclass (type2, type) must be
True. Super () only works for New-style classes.

A typical use for calling a cooperative superclass method is:

Class C (B):
def meth (self, arg):
Super (C, self). Meth (ARG)

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"

1. Super is not a function, it is a class name, like Super (B, self) actually called the Super class initialization function, produced a Super object;
2. The Super class initialization function does not do anything special, but simply records the class type and the concrete instance;
3. Super (B, self). The invocation of Func is not a Func function that invokes the parent class of the current class;
4. Python's multi-inheritance class ensures that functions of each parent class are invoked one at a time through MRO, and that each parent class function is called only once (if each class uses Super);
5. Mixing super and unbound functions is a risky behavior, which can cause the parent class function that should be called to not be called or a parent class function to be called multiple times.

------------------------------

We can see that from the Super keyword Help.

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 can be seen 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 called. Why do you have to use super?

This is because super is able to block multiple calls to the parent class method.

Super, which changed the order of the parent search, returned a special parent class objectSee example:class A:pass Class B (a): Pass Class C (a):p (b, C): PassThis is the basic relationship of 4 classes. If you do not use Super, let the object of D call a method that they have in common, and call the method in a 2 times.

How to use the Super keyword in python

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.