Inheritance Question

Frank Millman frank at chagford.com
Sat Nov 11 09:25:46 CET 2006


Gabriel Genellina wrote:
> At Saturday 11/11/2006 03:31, Frank Millman wrote:
>
> >Continuing your analogy of animals, assume a class A with a 'walk'
> >method and an 'eat' method.
> >
> >Most animals walk the same way, but a few don't, so I create a subclass
> >AW and override the walk method.
> >
> >Most animals eat the same way, but a few don't, so I create a subclass
> >AE and override the eat method.
> >
> >How do I create an instance of an animal that both walks and eats
> >differently?
> >

[snip]

> Answer 1) Move *both* ways of walk, and *both* ways of eating, to
> another base class. Best when this is pure behavior - no new
> attributes are involved.
>

[snip details]

>
> Answer 2) Use an instance of another class to define how to walk and
> how to eat. Advantage: it can later be modified at runtime (strategy pattern).
>

[snip details]

Many thanks for this, Gabriel.

I have seen explanations like this before, but my eyes usually glaze
over before I have finished, and I end up more confused than when I
started.

With a combination of my subconscious slowly getting an understanding
of this, and your clear explanation, I think I have finally got it.

Obviously my real world situation is quite a bit more complex than this
simple example, but with the help you have given me I can now
experiment with different ideas and decide on the best strategy.

Thanks again

Frank




More information about the Python-list mailing list