Why does the super function exist in Python if the parent class method can be called directly from the parent class name?

Source: Internet
Author: User
Like what

class  Child (Parent):      def __init__ (self):           Parent . __init__ (self)

Reply content:

For your question, the answer is yes, and there is no difference. But I don't feel good enough to answer the question.

To talk about super, first we should ignore the interference of the name "Super".

don't talk about super when you think of the parent class! Super refers to the next class in MRO!
don't talk about super when you think of the parent class! Super refers to the next class in MRO!
don't talk about super when you think of the parent class! Super refers to the next class in MRO!

When it comes to super, the parent class is a mistake that beginners can easily make and a mistake I made in the same year.
After forgetting this, read this article: Python's Super () considered super!
This is an article written by Raymond Hettinger, and is the world's most well-known super-lecture article, which is sure to mention it (and, of course, a Python's super considered harmful).

Look at the answer if you don't want to see a lengthy speech. , super is actually doing this thing:
defsuper(cls,inst):    mro=inst.__class__.mro()    returnmro[mro.index(cls)+1]
Super is used to solve multiple inheritance problems, it is not a problem to call the parent class directly with the class name when using single inheritance, but if multiple inheritance is used, it involves various problems such as lookup order (MRO), repeated invocation (Diamond inheritance).

In short, the experience left behind is: to maintain consistency. Either call the parent class with the class name, or use Super, not half.

If there is no complex inheritance structure, super has little effect. And the complex inheritance structure itself is bad design. For the use of multiple inheritance, it is now more of a way of advocating Mixin, namely
    • Common class multiple inheritance can only have one common parent and several Mixin classes (keeping the backbone single)
    • Mixin class cannot inherit normal class (avoid diamond inheritance)
    • The Mixin class should have a single responsibility (refer to Java's interface design, Mixin is very similar to this, but with implementation only)
If according to the above criteria, only use Mixin form of multiple inheritance, then there will not be a diamond inheritance to bring a duplicate method call, there will be no complex search order-at this time, super can have no, with no need to look at a person's liking, just remember never and the class name call of the way to mix. Sometime you think that the parent name is not good, change to myparent, the result has to change each subclass to go over

In addition to a class to get the parent class can only use super, for example:
def get_super (CLS): Super (A,self). Func does not execute the func of the parent class of a, but instead executes the func of all types in the class type sequence of the parent class of a. / http Blog.csdn.net/johnsongu o/article/details/585193
This is very good, can refer to the explanation of the next @laike9m is in place, I would like to say: seemingly unable to solve the parameter transfer problem Ah, can only use Python so-called magic, the parameters are passed all over, but two classes, the author of different, what things can happen, the name of the parameter is repeated, Feeling python this mechanism is not very good just for the first time come into contact with this problem, Porter

Inheritance-understanding Python super () with __init__ () methods
  • 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.