Raymond Hettinger - Super considered super! - PyCon 2015

By: PyCon 2015

624   5   55840

Uploaded on 04/11/2015

"Speaker: Raymond Hettinger

Python's super() is well-designed and powerful, but it can be tricky to use if you don't know all the moves.

This talk offers clear, practical advice with real-world use cases on how to use super() effectively and not get tripped-up by common mistakes.

Slides can be found at: https://speakerdeck.com/pycon2015 and https://github.com/PyCon/2015-slides"

Comments (4):

By anonymous    2017-09-20

You'll need to call super(FirstClass, self).__init__() in the FirstClass.__init__() initializer too.

The whole point of using super() is to make passing on the call to a parent cooperative. In your specific MRO FirstClass is listed before threading.Thread, so without explicitly calling the next __init__ in MRO order threading.Thread.__init__() never gets invoked.

You may want to look at the excellent PyCon 2015 presentation by Raymond Hettinger on how super() works in this context.

Original Thread

By anonymous    2017-09-20

Because this is exactly what you have asked) Reed more info on c3 linearization. Short hint - super does not call parents method, instead it calls method, that corresponds to next class in linearized inheritance graph.

More specifically:

>>> D.mro()
0: [<class '__main__.D'>,
    <class '__main__.B'>,
    <class '__main__.C'>,
    <class '__main__.A'>,
    <class 'object'>

This mro thing is a list, through witch any method of class D that delegates its behavior to parents (sort of speak) would be pushed any time you call it. So you call D().show() - first it calls implementation of show in D, it does nothing but delegating this call further - to class B. Class B fist delegates this call to C (see the mro list) which delegates it further to A, which prints "A", then C prints "C" and then B prints "B".

You might ask - why B delegates to C instead of A (since B extends A, not C). This actually done on purpose, for more info have a look on this great talk

Original Thread

Popular Videos 39499

Submit Your Video

If you have some great dev videos to share, please fill out this form.