Super usage in Python

Source: Internet
Author: User

Ext: 52205961

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

Examples of simple Python classes:

[Python]View PlainCopy
    1. >>> class Hello (object):
    2. ... def print_c ():
    3. ...  print"Hello world!"
    4. >>> hello (). Print_c ()
    5. Hello world!

Of course, in practice, the inevitable need for class inheritance, subclasses inherit the parent class, normal as follows:

[Python]View PlainCopy
    1. >>> class Child (Hello):
    2. ... def print_c (self):
    3. ... hello (). Print_c ()
    4. ...
    5. >>> Child (). Print_c ()
    6. Hello world!

The super () mechanism is also available in Python, as in the following example:

[Python]View PlainCopy
  1. >>> class Hello (object):
  2. ... def print_c (self):
  3. ...  print"Hello world!"
  4. ...
  5. >>> class Child (Hello):
  6. ... def print_c (self):
  7. ... super (Child, Self). Print_c ()
  8. ...
  9. >>> Child (). Print_c ()
  10. Hello world!

Does it feel the same at first glance?

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

1super () mechanism is used to solve multiple inheritance, for the direct invocation of the parent class name is not a problem, but in accordance with previous experience is: either use the class name is called, or all with super (), do not mix with, so for people to do things or to be exclusive O (∩_∩) o~

2 super () inheritance can only be used in modern classes and will be used for the classic class.
New class: Must have an inherited class, and if there is no inheritance, inherit the object
Classic class: There is no parent class, and if you call super at this point there will be an error: "Super () Argument 1 must is type, not classobj)

Okay, one more example.

[Python]View PlainCopy
  1. Class Parent1 (object):
  2. def __init__ (self):
  3. print ' is Parent1 '
  4. print ' goes parent1 '
  5. Class Parent2 (object):
  6. def __init__ (self):
  7. print ' is Parent2 '
  8. print ' goes Parent2 '
  9. Class Child1 (PARENT1):
  10. def __init__ (self):
  11. print' is Child1 '
  12. Parent.__init__ (self)
  13. print ' goes child1 '
  14. Class Child2 (PARENT1):
  15. def __init__ (self):
  16. print ' is child2 '
  17. Parent.__init__ (self)
  18. print ' goes child2 '
  19. Class Child3 (Parent2):
  20. def __init__ (self):
  21. print ' is child3 '
  22. Parent2.__init__ (self)
  23. print ' goes child3 '
  24. Class grandson (Child3,child2,child1):
  25. def __init__ (self):
  26. Print ' is grandson '
  27. Child1.__init__ (self)
  28. Child2.__init__ (self)
  29. Child3.__init__ (self)
  30. print' goes grandson '
  31. If __name__==' __main__ ':
  32. Grandson ()
[Python]View PlainCopy
    1. is grandson
    2. Is Child1
    3. Is Parent1
    4. Goes Parent1
    5. Goes Child1
    6. Is Child2
    7. Is Parent1
    8. Goes Parent1
    9. Goes child2
    10. Is child3
    11. Is Parent2
    12. Goes Parent2
    13. Goes child3
    14. Goes grandson
[Python]View PlainCopy
[Python]View PlainCopy
    1. Okay, did you find anything wrong here? Yes, the base class Parent1 is executed several times, and sometimes we just want the public class to be executed only once, then we introduce the super () mechanism to see the effect:
[Python]View PlainCopy
    1. <span style="font-family:arial, Helvetica, Sans-serif;" >class Parent1 (object):</span>
[Python]View PlainCopy
  1. def __init__ (self):
  2. Super (parent1, Self ). __init__ ()
  3. print ' is Parent1 '
  4. print ' goes parent1 '
  5. Class Parent2 (object):
  6. def __init__ (self):
  7. Super (Parent2, Self ). __init__ ()
  8. print ' is Parent2 '
  9. print ' goes Parent2 '
  10. Class Child1 (PARENT1):
  11. def __init__ (self):
  12. print' is Child1 '
  13. #parent1. __init__ (self)
  14. Super (child1, Self ). __init__ ()
  15. print ' goes child1 '
  16. Class Child2 (PARENT1):
  17. def __init__ (self):
  18. print ' is child2 '
  19. #parent1. __init__ (self)
  20. Super (child2, Self ). __init__ ()
  21. print ' goes child2 '
  22. Class Child3 (Parent2):
  23. def __init__ (self):
  24. print ' is child3 '
  25. #parent2. __init__ (self)
  26. Super (child3, Self ). __init__ ()
  27. print ' goes child3 '
  28. Class grandson (Child3,child2,child1):
  29. def __init__ (self):
  30. Print ' is grandson '
  31. #child1. __init__ (self)
  32. #child2. __init__ (self)
  33. #child3. __init__ (self)
  34. Super (grandson, self ). __init__ ()
  35. print' goes grandson '
  36. If __name__==' __main__ ':
  37. Grandson ()

[Python]View PlainCopy
    1. At this point we look at the results:
[Python]View PlainCopy
    1. is grandson
    2. Is child3
    3. Is Child2
    4. Is Child1
    5. Is Parent1
    6. Goes Parent1
    7. Goes Child1
    8. Goes child2
    9. Is Parent2
    10. Goes Parent2
    11. Goes child3
    12. Goes grandson

As a result, it is clear that the public base class Parent1 is only executed once.

The inheritance system for the grandson class is as follows:

[Python]View PlainCopy
    1. Object
    2. |
    3. /   \
    4. P1 P2
    5. /     \      |
    6. C2 C3
[Python]View PlainCopy

So for the class inheritance system, we can see as a graph, and each class as a node, then the super () mechanism is executed in accordance with the graph's breadth-first search algorithm to find super () of the parent class.

Follow-up Summary:

1. Super () is a class name instead of a function, super (class, self) actually calls the Super class initialization function, resulting in a Super object;

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

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

Super usage 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.