[Python-ideas] Keyword for direct pass through of kwargs to super

Chris Angelico rosuav at gmail.com
Sat May 26 07:09:35 EDT 2018

On Sat, May 26, 2018 at 7:22 PM, Michael Lohmann <mial.lohmann at gmail.com> wrote:
> [Chris Angelico]
>> Does that make sense?
> Well yes, of course it does. When instantiating a HawaiianPizza of course you want to set size and price. I don’t want to remove **kwargs and the current way of handeling this. But if you now add:
>     class Lasagna:
>         def __init__(self, *, number_of_layers=5):
>             print("This Lasagna has %s layers", number_of_layers)
>     class HaveYouEverTriedThis(Pizza, Lasagna):
>         """Well, this is just bizarre"""
> Now suddenly `Pizza` would have to accept kwargs. Why????????? It didn’t "learn anything new". It can’t do anything with them on it’s own. Shouldn’t you expect from a function that in the brackets you are shown what is possible to actually call this function on? You could never actually call Pizza with kwargs, so 1) Why should you be able to do so in the first place? and 2) Why show the programmer that you could?. Just to make the MRO work. And I would suggest that to handle this it would be a lot more elegant to say: "If you are in the middle of an MRO: just pass everything unexpected down“

Right, which means that Pizza and Lasagna are not compatible classes
in that way. If you were to try to concoct some sort of, I don't know,
layered conglomerate with meat, tomato, and pizza bases, you'd have to
write an init method that does that for you.

class Pizzagna(Pizza, Lasagna):
    def __init__(self, *, number_of_layers, **kw):
        Lasagna.__init__(self, number_of_layers=number_of_layers)
        Pizza.__init__(self, **kw)

The Pizza class was deliberately designed to be an apex class - one
that is the head of a hierarchy. The Lasagna class isn't part of that
hierarchy, so merging it in takes some extra work - a pizza can't
automatically subsume a lasagna.

We had the family over here for dinner tonight, and we ate two pizzas.
Now I'm wondering if a Pizzagna would have been a better choice...

Anyhow. Further discussion about this should probably go onto python-list.


More information about the Python-ideas mailing list