[Python-ideas] documentation for python3 super()

Guido van Rossum gvanrossum at gmail.com
Sat Jul 2 15:28:34 EDT 2016


The trick is in the mro. If you print Person.__mro__ you'll get a list
showing that its MRO (method resolution order) is Person, QObject,
Age, object. In Person, super() refers to QObject (the next class in
the MRO). In QObject, *for a Person instance*, super() refers to Age.
In Age, in this case, super() refers to object. The methods on object
don't call super.

Hope this helps -- you should be able to get more help by Googling for
something like MRO.

On Sat, Jul 2, 2016 at 11:45 AM, Barry Scott <barry at barrys-emacs.org> wrote:
>
> On 2 Jul 2016, at 18:58, Guido van Rossum <gvanrossum at gmail.com> wrote:
>
> No, super() does not (directly) call multiple functions. The function it
> calls has to call the next with another super() call. Also, __init__() is
> not special, nor is **kwds
>
>
> The thing I do not understand is why did super() call 2 __init__ functions
> given the code I attached?
> The one in QObject and the one in Age.
>
> This is the output I get with python 3.5:
>
> $ python3.5 super_example.py
> instance of Person.__init__
> instance of QObject.__init__
> instance of Age.__init__
> Person.describe()
> name: Barry
> QObject.describe()
>
> I see no obvious code that should call Age.__init__, which is why I
> concluded that there is something about super() that is not documented.
>
> super() itself is special, it knows the class and instance.
>
> There are conventions around all of this though. It may be worth documenting
> those, as long as it is made clear which part of the docs is about
> conventions (as opposed to how things work).
>
> There are also opinions about those conventions. Here I am not so sure that
> they belong in the docs.
>
> Agreed.
>
> Barry
>
> --Guido (mobile)
>
> On Jul 2, 2016 10:32 AM, "Barry Scott" <barry at barrys-emacs.org> wrote:
>>
>> I have read the python3.5 docs for super() and
>> https://rhettinger.wordpress.com/2011/05/26/super-considered-super/.
>>
>> Between the two sources I still have no clear idea of what super() will
>> do and why it will doe what it does.
>>
>> I hope to get feedback that can be used as the basis to update the python
>> docs.
>>
>> For single inheritance the use of super() does not seem to have any
>> surprises.
>>
>> But in the case of a class with multiple-inheritance more then 1 function
>> may be called by 1 call to super().
>>
>> What are the rules for which functions are called by super() in the
>> multiple
>> inheritance case?
>>
>> Is __init__ special or can other super() calls end up
>> calling more then 1 function?
>>
>> What is the role of **kwds with super()?
>>
>> Here is the code I used to show the __init__ multiple calls.
>>
>> The attached example shows that one call to super() causes 2 to
>> __init__ of bases of Person. But describe does not follow the pattern
>> Age.describe is not called.
>>
>> Barry
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>
>



-- 
--Guido van Rossum (python.org/~guido)


More information about the Python-ideas mailing list