[Python-ideas] Is multiple inheritance Pythonic?
Martin Teichmann
lkb.teichmann at gmail.com
Fri Mar 18 18:34:43 EDT 2016
Hi,
>> Is multiple inheritance bad, or not Pythonic?
>
> Definitely not.
>
> But at the same time, it's not always the answer to everything
> either.
Actually, I agree with you. But then I found the discussion about
asyncio.BaseEventLoop.create_task (especially:
https://github.com/python/asyncio/issues/208 and
https://codereview.appspot.com/110820049/ ),
which is now in the standard library, probably the most Pythonic
code out there.
During this discussion, all participants argue as if class inheritance
was something EVIL. And they were not just random people, but
in my opinion some of the finest Python programmers out there.
They desperately try to avoid letting users inherit classes, even
in cases where it seemed obvious to me that inheritance is the
right choice - but well, who am I to decide that. So I asked myself,
and hereby the Python community, is there something so bad
about inheritance that I missed?
The problem they wanted to solve is that the behavior of
BaseEventLoop.create_task should be changeable. Obvious,
I thought (and did in my code) inherit from the class, overwrite
the method! But what if I want to overwrite that method in
something that inherits from BaseEventLoop, say, some
QtEventLoop? No problem, I thought, just supply a
Mixin, and a simple class EventLoop(AMixin, QtEventLoop)
does the job. This is even chainable, multiple mixins can
be used as long as they properly call super().
Instead, the developers decided to write a set_task_factory
method, which overwrites create_task. It has none of the
above mentioned advantages. That's where I started
thinking, and I am still puzzled what obstacles might be
out there, and decided that inheritance must be something
magically non Pythonic, and I am still wondering why that
might be.
Greetings
Martin
More information about the Python-ideas
mailing list