What are init__ functions in Python

Understand Python super () with __init __ () methods

I try to understand

The reason we're using is that child classes that may be using cooperative multiple inheritance call the correct function of the next parent class in method resolution order (MRO) order.

In Python 3 we can call it this:

In Python 2 we have to use it like this:

Without Super, you are limited in your ability to use multiple inheritance:

I explain below.

"What's the difference in this code?:"

The main difference in this code is that it gives you a level of indirection that uses the current class to determine that of the next class to look up in the MRO.

I illustrate this difference in an answer to the Canonical Question, how to use 'super' in Python? who have dependency injection demonstrated and cooperative multiple inheritance .

If Python didn't have

Here is the code that actually corresponds (as implemented in C, minus some checking and fallback behaviors, and translated into Python):

Written a bit more like Python:

If we didn't have the object, we'd have to write (or re-create!) This manual code everywhere to make sure we're calling the correct next method in the order of method resolution!

How does super do this in Python 3 without explicitly knowing from which class and instance the method was called?

It gets the calling stack frame and finds the class (stored implicitly as a local free variable, which means that the calling function is a closure on the class) and the first argument for that function, which should be the instance or class that informs about which Method Resolution Order (MRO) should be used.

Since this first argument is required for the MRO, it cannot be used with static methods.

Criticism of other answers:

with super () you can avoid explicitly referring to the base class, which can be nice. . The main benefit, however, is multiple inheritance, where all kinds of fun things can happen. See the standard Super documentation if you haven't already.

It's pretty undulating and doesn't tell us much, but the point of is inevitable to write the parent class. It must be ensured that the next method is called in the order of method resolution (MRO). This becomes important with multiple inheritance.

I explain it here.

And let's create a dependency that we want to name after the child:

Now remember, used super, don't:

And doesn't call the UserDependency method:

But, because used, does!:

Criticism for another answer

Under no circumstances should you do any of the following, as another answer suggests, as you will definitely get errors subordinating ChildB:

(This answer is neither smart nor particularly interesting, but despite direct criticism in the comments and over 17 devaluations, the respondent insisted on suggesting it until a friendly editor fixed his problem.)

Explanation: This response suggested calling super as follows:

This is completely not correct. lets us look up the next parent in the MRO (see the first section of this answer) for child classes. If you tell us that we are in the child's method, it will look for the next method on the line (probably this one) which will result in a recursion and likely cause a logic error (in the respondent's example) or if the recursion depth is exceeded.