8.python Face Object Part.5 (the subclass calls the method of the parent class, and the Super keyword)

Source: Internet
Author: User
Tags class definition

Python neutron class to invoke a method of the parent class, which was implemented in earlier versions of Python:

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"

b = B ()

>>>enter B

Enter A

Leave A

Leave B


is to use a non-binding class method (a method referenced with the class name), and in the argument list, introduce the object to be bound (self) to achieve the purpose of calling the parent class.

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

Code Snippet 2:

Class B (c): # A--C

def __init__ (self):

Print "Enter B"

C.__init__ (self) # A--C

Print "Leave B"

If the code is simple, this change might be acceptable. But if the amount of code is large, such a modification could be catastrophic. It is easy to cause modification errors to occur.

In the post-python2.3 version, a keyword called super was added to solve this problem, but if you want to use the Super function to invoke the parent class method must have a premise, the function of the parent class must be a new class (that is, the class inherits the object Class)!! Otherwise, the Super keyword cannot be used!!!



Class A (object): # A is a new 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 only one place to modify the code, to minimize the maintenance of the code, is a good use. So in our development process, the Super keyword is heavily used and has been performing well.


How to use the Super keyword:

Super (' Parent ' of the class to invoke, self). The method of this class parent class.



Here's an anatomy of the Super Keyword principle:

1.super is not a function, it is a class name, like Super (B, self) actually called the Super class initialization function, resulting in a Super object.

The Super object, instantiated by the 2.super class, does nothing else but records the type of the class and the specific object.

The multi-inheritance class of 3.Python ensures that functions of each parent class are called one at a time by means of MRO, and that each parent class function is called only once (if each class uses Super).



An example of a use of the Super function:

Inherit the Dict class and make an ordered dictionary type based on the Dict class.

Class Mydict (dict): #有序字典实现

def __init__ (self):

Self.li = []

Super (Mydict,self). __init__ ()

def __setitem__ (self, Key,value): #__setitem__ (self,key,value): Sets the value of the given key

Self.li.append (Key)

Super (Mydict,self) __setitem__ (Key,value)

def __str__ (self): How to implement #__str__ direct print objects

Temp_list = []

For key in Self.li:

Value = Self.get (key)

Temp_list.append ("'%s ':%s"% (Key,value,))

Temp_str = ' {' + ', '. Join (Temp_list) + '} '

Return TEMP_STR

obj = Mydict ()

obj[' k1 '] = 123

obj[' K2 '] = 456

Print (obj)



This article is from the "Rebirth" blog, make sure to keep this source http://suhaozhi.blog.51cto.com/7272298/1913200

8.python Face Object Part.5 (the subclass calls the method of the parent class, and the Super keyword)

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.