"Knacktus" <knack...@googlemail.com> wrote
I've got a question about class design. I want to model classes like
these (examples):
Since these only really have data and inheritance should be based on
behaviour its almost impossible to make a meaningful decision on what
the inheritance tree should look like.
However...
#####################################################################
class BaseItem(object):
def __init__(self, ident, name, description):
self.ident = ident
self.name = name
self.description = description
class DataSourceItem(object):
def __init__(self, ident, name, description, data_source):
self.ident = ident
self.name = name
self.description = description
self.data_source = data_source
Surely the DataSourceItem could inherit BaseItem?
They are both types of Item...
class DataSourceItem(BaseItem):
def __init__(self,ident, name, description, data_source):
BaseItem.__init__(self,ident, name, description) # or use
super()...
self.data_source = data_source
class BaseItemCollection(list):
def __init__(self, ident, name, description):
self.ident = ident
self.name = name
self.description = description
def default_version(self):
return self[-1]
class BaseDataSourceItemCollection(list):
And the same here - Make the DataSourceItemCollection inherit
from ItemCollection since they are both ItemCollections...
Now, to remove all the duplicated code I could use inheritance. But
that would lead to multiple inheritance and the question how to
initialise both superclasses?
Why would it lead to multiple inheritance? There seem to be two
different types of things - Items and collections. Inheritance is not
a code saving tool but an Is-A relationship that enables polymorphism.
Do not use inherirtance just to save code - its nearly always a bad
idea.
Although lots of duplicated code - especially methods - often suggest
that an Is-A relationship exists...
I would appreciate some advice about how to model classes like this.
Personaly, I don't have to use inheritance, but I have no better
idea. Would you accept at least some duplication to avoid multiple
inheritance?
Duplication is irerelevant. But I would not do anything to "avoid
multiple inheritance" if MI was appropriate. MI is a very effective
and
powerful tool. It is not something to be avoided just in principle.
But you
very rarely need it in Python in practice. Few things are genuinely of
two such different types that dynamic duck-typing can't deal with it
without MI. But don't use inheritance just to avoid duplicating code,
use delegation for that.
FWIW I've worked on projects using MI with a single class
inheriting 7 parent classes, if done properly and deliberately MI
is nothing to worry about. The problems only occur when MI is
abused and not designed IMHO
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor