Tutorial: Elaborate on Class Inheritance with super() example

It would be great if the Python Tutorial section on Classes and Inheritance (9. Classes — Python 3.13.1 documentation) could have an explanation of when and how to use super() for initialization in derived classes, with examples if possible.
The function is only briefly mentioned in the next section.
Its docs (Built-in Functions — Python 3.13.1 documentation) are not really helpful for beginners to get started.

2 Likes

+1. I’d also like the example to show the ordering it uses when calling parents. Maybe even mention what happens with “diamond inheritance”.

1 Like

If I may, I have another request :slight_smile:

The tutorial on classes does not seem to explain that/why every class method’s first argument is the instance (usually named self).
The code examples all use it, but the first mention of self in the text is in 9. Classes — Python 3.13.1 documentation and only regarding the naming convention.

2 Likes

At the end of the super() function docs, there is a link to Raymond Hettinger’s blogpost with examples: Python’s super() considered super! | Deep Thoughts by Raymond Hettinger.

1 Like

Actually I’ve just realized that the Inheritance section fragment dates to from before Python 3, when super() was supported only in new-style classes, which are the standard for long now.

An overriding method in a derived class may in fact want to extend rather than simply replace the base class method of the same name. There is a simple way to call the base class method directly: just call BaseClassName.methodname(self, arguments) . This is occasionally useful to clients as well. (Note that this only works if the base class is accessible as BaseClassName in the global scope.)

This should be rewritten to:

An overriding method in a derived class may in fact want to extend rather than simply replace the base class method of the same name. There is a simple way to call the base class method directly: just call super().methodname(arguments) . This is occasionally useful to clients as well.

Answering to another comment:

Actually, in Method Objects section there is a paragraph about it.

Actually, you may have guessed the answer: the special thing about methods is that the instance object is passed as the first argument of the function. In our example, the call x.f() is exactly equivalent to MyClass.f(x) . In general, calling a method with a list of n arguments is equivalent to calling the corresponding function with an argument list that is created by inserting the method’s instance object before the first argument.

This section refers to code used in Class Objects section. Should we duplicate the code snippets just for Method Objects section? :thinking:

@none Thank you for bringing this up! Would you be willing to open a pull request with improvements?

Just pointing out, there’s a difference between the two: one explicitly
calls the method in the class you specify, while super() follows the MRO
to decide what to call. The latter is often what you want, but not
necessarily always. Maybe there’s nothing to say about that: In the
tutorial there’s always a question of how much detail to go into to not
obscure the points in excess verbiage. Quite a few people in more recent
years have noted that Chapter 9 in particular isn’t that easy to follow,
for example when it contains references to a languages a lot of today’s
programmers don’t know much about (Smalltalk and Modula-3 are both
mentioned in the opening section).

2 Likes