subclassing versus object redefinition

Roald de Vries downaold at gmail.com
Tue Aug 3 16:06:20 CEST 2010


On Aug 3, 2010, at 2:46 PM, wheres pythonmonks wrote:
> Hi!
>
> I have a class (supposed to be an abstract base class):
> In python (as opposed to static languages like C++) I don't seed to
> subclass the base class, but instead I can simply override the
> behavior of stub methods and values.
> Is there a preference between between subclassing (C++ approach) and
> overriding methods/data in an instance?  From a design perspective?
> I think I prefer the override/redefine approach because it result in a
> thinner class hierarchy.
>
> It seems like inheriting an ABC is needed only when I must share
> instances (between multiple parts of the code, or if the subclass is
> instantiated in different places...)
>
> Thoughts?

1) some things are just not possible in instances, like overriding  
operators
2) abstract base classes are not supposed to be instantiable, so if  
you are able to do it anyway, that is a hack
3) adding stuff to instances is less reusable that adding stuff to  
(sub)classes
4) if I'm reading your code and want to know what an object is like, I  
look at the class, not through your whole program to collect all bits  
and pieces of information spread out over it
5) why would you want a thinner class hierarchy?

So I would go for inheritance.

Cheers, Roald




More information about the Python-list mailing list