Why does the super function still exist in Python since the parent class method can be called directly through the parent class name?

Source: Internet
Author: User
For example, classChild (Parent): def _ init _ (self): Parent. _ init _ (self), for example

class Child(Parent):      def __init__(self):           Parent.__init__(self) 
Reply content: the answer to your question is yes, and there is no difference. However, I don't feel good enough to answer this question.

To talk about super, we should first ignore the interference brought by the name "super.

Do not think of the parent class when talking about super! Super refers to the next class in MRO!
Do not think of the parent class when talking about super! Super refers to the next class in MRO!
Do not think of the parent class when talking about super! Super refers to the next class in MRO!

When talking about super, I thought of the parent class. This is a very easy mistake for beginners and also a mistake I made in the past.
After forgetting this, I will go back to this article: Python's super () considered super!
This is an article written by Raymond Hettinger. It is also the world's most widely recognized article about super, anyone who talks about super will mention it (and of course there is also a Python's Super Considered Harmful ).

If you don't want to read a long article, let's look at this answer. Super is actually doing this:

def super(cls, inst):    mro = inst.__class__.mro()    return mro[mro.index(cls) + 1]
Super is used to solve the problem of multi-inheritance. It is okay to directly call the parent class method using the class name when using single inheritance. However, if multiple inheritance is used, the search order (MRO) is involved) and repeated calls (diamond inheritance.

In short, the experience left by our predecessors is to maintain consistency. Do not call the parent class with the class name, or use super for all, not half.

If there is no complex inheritance structure, super has little effect. The complex inheritance structure itself is a poor design. For the usage of Multi-inheritance, the Mixin method is more favored now, that is
  • Multi-inheritance of common classes can only have one common parent class and several Mixin classes (keep the trunk Single)
  • Mixin classes cannot inherit common classes (avoid diamond inheritance)
  • The Mixin class should have a single responsibility (refer to the interface design of Java, Mixin is very similar to this, but only with implementation)
According to the above standard, only multi-inheritance in the Mixin form is used, so there will be no repeated method calls resulting from diamond inheritance, and there will be no complicated search order-at this time, super is available, you don't need to look at your personal preferences. You just need to remember not to mix them with the method called by class name. Sometimes you think the Parent name is too bad and you have to change it to MyParent.

In addition, only super can be used to obtain the parent class for a class, for example:
Def get_super (cls): super (A, self ). func does not execute the func of the parent class of A, but executes the func of all types in the class type sequence of the parent class of. Http://blog.csdn.net/johnsonguo/article/details/585193
The description here is quite good. You can refer to the @ laike9m explanation below. I want to say: It seems that the parameter passing problem cannot be solved. You can only use the so-called magic of python, pass all the parameters in the past, but the two classes have different authors. Everything can happen. The parameter names are repeated. I feel that the mechanism of python is just the first time I came into contact with this problem.

Inheritance-Understanding Python super () with _ init _ () methods

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.