[Tutor] When to use classes

boB Stepp robertvstepp at gmail.com
Sun Aug 20 22:06:51 EDT 2017


On Sat, Aug 19, 2017 at 8:08 PM, Steven D'Aprano <steve at pearwood.info> wrote:

> The Art of Subclassing
>
> http://www.youtube.com/watch?v=miGolgp9xq8
>
> If its the one which starts off with him talking about his newly born
> child, it is the one.

This is indeed the "one"!

> The traditional viewpoint of inheritance is that it is used for
> specialisation, where each subclass is a kind of the parent class:
>
> class Animal:
>     # all generic code for animals goes here
>
> class Mammal(Animal):
>     # mammals are a kind of animal
>     # specialise the animal code for mammals
>
> class Dog(Mammal):
>     # dogs are a kind of mammal
>     # specialise the mammal code for dogs
>
> class Collie(Dog):
>     # collies are a kind of dog
>     # specialise the dog code for the collie breed
>
> lassie = Collie()
>
> If you ever hear people talking about the Liskov Substitution Principle,
> that's the model they have in mind. But it's not the only one possible.
>
> Raymond talks about inheritance as expressing a parent-child
> relationship, where the child can delegate tasks to the parent, but the
> child doesn't necessarily need to be seen as "a kind of" whatever the
> parent is.
>
> That's the model used by mixin classes (or traits, a variation on
> mixins).

Well the video has put a new twist on things:  Choose the parent class
to be the one which maximizes code reuse instead of thinking in terms
of classic specialization.  And the children are in charge, not the
parents!  Interesting stuff!

-- 
boB


More information about the Tutor mailing list