Summary: python implements the difference between the two methods called by the parent class, and python implements the two methods.

Source: Internet
Author: User

Summary: python implements the difference between the two methods called by the parent class, and python implements the two methods.

There are two methods in python that can call the method of the parent class:

super(Child, self).method(args)

 Parent.method(self, args)

I reported the following error using one of the methods:

Classobj cannot be found. When I change the callsuper(B, self).f(name)And the results are correct.

Analysis Error

Because the base class does not inherit objects, in python, one can be created as follows:

class A: pass

You can also create it as follows:

class A(object): pass

The difference between the two is:

This is the difference between the old class (the former) and the new class (the latter. Differences can be referred to: https://docs.python.org/release/2.5.2/ref/node33.html

Python3 has removed the old type, that is, it has implicitly inherited the object. Therefore, there is no difference between writing and inheriting objects in python3.

Differences between the two calls

Parent.__init__(self) Andsuper(Child, self).__init__() What is the difference?

Super is taken for granted as a parent class. In python, it actually refers to the next class in MRO!

Super actually did this. Let's look at the answer:

def super(cls, inst): mro = inst.__class__.mro() # Always the most derived class return mro[mro.index(cls) + 1]

MRO stands for Method Resolution Order, which represents the sequence of class inheritance.

Super is used to solve the problem of multiple inheritance. Assume that B C D directly inherits class

class E(B, C, D): def __init__(self):  # code...

If the class E constructor usessuper(E, self).__init__() The constructor of Class A is executed once, And the constructor of Class A is executed multiple times in another method.

In MRO, the base class always appears after the derived class. If there are multiple base classes, the relative sequence of the base classes remains unchanged.

Summary

I personally think that a complex inheritance structure is a poor design in design. When the inheritance structure is clear, there is no difference between the two methods. The above is all the content of this article. I hope this article will help you in your study or work.

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.