[Python-ideas] OrderedCounter and OrderedDefaultDict
Steven D'Aprano
steve at pearwood.info
Wed Oct 21 03:50:26 CEST 2015
On Wed, Oct 21, 2015 at 09:50:21AM +1100, Chris Angelico wrote:
> To me, this is the biggest benefit of inheritance: you do NOT have to
> predict what someone might want to do. I can subclass someone else's
> object and change how it works.
I think this is wrong. I think that in general you need to have a fairly
good idea of the internal workings of the class before you can inherit
from it. Otherwise, how do you know what methods need to be overwritten?
Take dict. One nuisance with inheriting from dict is that it isn't
sufficient to override __setitem__, you also have to override update and
clear as well. And possibly others -- the exact set of which methods
depend on which other methods are not documented in the dict API. Given
an arbitrary class, how can you possibly tell which methods you need to
override, or even which methods are *safe* to override?
[...]
> Composition has its place, don't get me wrong. But inheritance isn't
> the attractive nuisance she makes it out to be. It works just fine.
It doesn't work "just fine", it has risks and annoyances, and multiple
inheritance even more so. Classes have problems.
See Jack Diederich's talk "Stop Writing Classes", or at least stop
writing *stupid* classes:
http://eev.ee/blog/2013/03/03/the-controller-pattern-is-awful-and-other-oo-heresy/
As far as multiple inheritance, there are real issues with MI that
aren't solved by coming up with a nifty solution to the diamond problem.
See Michele Simionato's series of essays on super, multiple inheritance,
mixins and traits:
http://www.artima.com/weblogs/viewpost.jsp?thread=246488
Clearly classes are useful, but they aren't an unalloyed good thing, and
inheriting from them has its issues.
--
Steve
More information about the Python-ideas
mailing list