[Python-ideas] Composition over Inheritance
Koos Zevenhoven
k7hoven at gmail.com
Sun Oct 29 08:57:59 EDT 2017
(looks like you forgot to reply to the list)
On Sun, Oct 29, 2017 at 7:47 AM, Greg Ewing <greg.ewing at canterbury.ac.nz>
wrote:
> Koos Zevenhoven wrote:
>
>> It looks like you can just as easily drive the car without having the key
>>
>
> That's been found to be a problem in real life, too.
> More than one Python-programming car thief has been
> caught with this function in their personal library:
>
> def hotwire(car):
> car.engine.start()
Yes, but my point was still about what the public API is. Starting the car
with the key should be easier than starting it without the key. Surely you
should be able to start the engine without the key if you look under the
hood and figure it out. And maybe you'd need a tool for that to be kept in
your garage for debugging, but not as a button attached to the outside of
the car or on the dashboard :-).
The problem becomes even worse if you instead make Car inherit from its
parts (including Engine), because the relationship of a car and and engine
is not well described by inheritance. A car is not a kind of engine, nor
should it pretend to be an implementation of an engine. A car *comprises*
(or contains) an engine. And who knows, maybe it has two engines!
-- Koos
--
+ Koos Zevenhoven + http://twitter.com/k7hoven +
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20171029/34430a4b/attachment.html>
More information about the Python-ideas
mailing list