multiple inheritance of a dynamic list of classes?

Neil Cerutti horpner at yahoo.com
Thu Feb 15 12:09:06 EST 2007


On 2007-02-15, Michele Simionato <michele.simionato at gmail.com> wrote:
> On Feb 13, 9:14 am, Peter Otten <__pete... at web.de> wrote:
>> "Avoid inheritance" would be almost as justified :-)
>
> In other words, if you are inheriting just two or three methods
> it may works, but when you start having dozens of methods
> inherited from different sources, your code will become to look
> as spaghetti code. This is why in general I (as many people
> here) suggest delegation over inheritance.

I consider inheritance in Python when I see that the class I'm
implementing contains some sort of status code that controls
behavior.

For example:

class Stream(object):
  def __init__(self, readable, writable):
    if readable and writable:
      self.io_type = 'inout'
    elif readable:
      self.io_type = 'out'
    else:
      self.io_type = 'in'

That code sets me to thinking I'll get good mileage from:

class Stream(object):
  ...

class InStream(object):
  ...

class OutStream(object):
  ...

class InOutStream(object):
  ...

I always get myself into trouble when I try to design a class
hierarchy *before* I see something like that.

-- 
Neil Cerutti



More information about the Python-list mailing list