Research on super usage in Python __python

Source: Internet
Author: User
Tags class definition

The Python language has similar class inheritance to C + +, and in the class definition, Python customizes the first self, similar to the this pointer in C + +, pointing to the object itself.

Examples of Python simple classes:

>>> class Hello (object):
...         Def print_c ():
.             print "Hello world!"
>>> hello (). Print_c ()
Hello world!
Of course, in practice, the inevitable need for class inheritance, subclass inherits the parent class, normal as follows:

>>> class Child (Hello):
...         def print_c (self):
...                 Hello (). Print_c () ...                 
>>> Child () print_c ()
Hello world!
The super () mechanism is also available in Python, as shown in the following example:

>>> class Hello (object):
...         def print_c (self):
...             print "Hello world!"
...             
>>> class Child (Hello):
...         def print_c (self):
...                 Super (Child,self). Print_c () ...                 
>>> Child () print_c ()
Hello world!
First glance is not the same feeling.

The purpose of introducing super () in Python is to ensure that the same base class is initialized only once (note:

The 1super () mechanism is used to resolve multiple inheritance, it is no problem to invoke the parent class name directly, but after the experience of the predecessors is: either use the class name call, or all with super (), do not mix with, so that people do things or single-minded O (∩_∩) o~

2 super () inheritance can only be used in the new class, when used in the classic class will be an error.
New class: Must have an inherited class, if no inheritance, then inherit object
Classic class: There is no parent class, and if you call super at this point, an error occurs: "super () argument 1 must be type, not classobj)

Okay, one more example.

class parent1 (object): Def __init__ (self): print ' is parent1 ' print ' goes Parent1 ' Class Parent2 (object): Def __init__ (self): print ' is Parent2 ' print ' goes Parent2 ' class child1 (par ENT1): def __init__ (self): print ' is Child1 ' parent.__init__ (self) print ' goes Child1 ' class C Hild2 (parent1): def __init__ (self): print ' is Child2 ' parent.__init__ (self) print ' goes chil  D2 ' class child3 (Parent2): def __init__ (self): print ' are child3 ' parent2.__init__ (self) print ' Goes Child3 ' class grandson (Child3,child2,child1): def __init__ (self): print ' is grandson ' Child1._ _init__ (self) child2.__init__ (self) child3.__init__ (self) print ' goes grandson ' if __name__== ' __m Ain__ ': Grandson () 
Is grandson are
child1 is
parent1
goes parent1
goes child1 are Child2 is parent1 goes Parent1
goes child2 is
child3 is
parent2
goes Parent2 goes child3 goes grandson
Okay, what's the problem here? Yes, the base class parent1 is executed multiple times, and sometimes we just want the public class to be executed only once, so at this point we introduce the super () mechanism to see the effect:
<span style= "font-family:arial, Helvetica, Sans-serif;" >class Parent1 (object):</span>
    def __init__ (self): Super (Parent1, self). __init__ () print ' is parent1 ' print ' goes Parent1 ' C Lass Parent2 (object): Def __init__ (self): Super (Parent2, self). __init__ () print ' is Parent2 ' p Rint ' goes Parent2 ' class Child1 (parent1): def __init__ (self): print ' is Child1 ' #parent1. __init__ (sel
        f) Super (child1, self). __init__ () print ' goes Child1 ' class Child2 (parent1): def __init__ (self): 

print ' is Child2 ' #parent1. __init__ (self) Super (child2, self). __init__ () print ' goes Child2 ' Class Child3 (Parent2): def __init__ (self): print ' is Child3 ' #parent2. __init__ (self) Super (c
        Hild3, self). __init__ () print ' goes Child3 ' class grandson (Child3,child2,child1): def __init__ (self):
        print ' is grandson ' #child1. __init__ (self) #child2. __init__ (self) #child3. __init__ (self) Super (grandson, self). __init__ () print ' goes grandson ' if __name__== ' __main__ ': grandson ()


 

At this point we look at the results:
Is grandson is
child3 are
child2
child1 is
parent1
goes
parent1 goes child1 goes child2< C7/>is parent2
goes parent2
goes child3 goes grandson

The result is obvious that the common base class Parent1 is only executed once.

The inheritance system of the grandson class is shown in the following illustration:

       Object
           |
         /   \
      P1    P2
    /     \      |
  C1   C2  C3
So for the inheritance system of the class, we can be seen as a graph, and each class as a node, then the super () mechanism is executed in order to find the parent class of super () according to the breadth-first search algorithm of the graph.

Follow-up summary:

1. Super () is a class name rather than a function, and super (class, self) actually invokes the initialization function of the super class, resulting in a Super object;

2 super () mechanism must use the new class, otherwise it will be an error;

3 super () or direct parent class invocation It is best to select only one form.







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.