Designing for Inheritance

nspies.geo nspies.geo at yahoo.com
Wed Dec 12 10:42:03 CET 2001


Hi all,
I'm in the process of writing a fairly complex data-storage class,
where one of the main functions needed is pulling parts of the data
out intact.  Most of the time, these parts, extracted from the class,
should also be returned as objects of the same class.

The problem comes when I wish to add some functionality to the class:
there are likely an infinite number of ways to refer to the same data,
so I, and others, will likely be creating numerous base classes that
take a different approach.

The problem is thus: how do I design the base class so that it will
create an extracted object of the correct type, even if it's been
inherited?  It may well be that the inherited class needs to wrap the
base class function, but I do not wish to re-write the function in
every base class.

Here it is in code.


--

class BaseAlignment:
   def extractx(self, x):
      extraction = BaseAlignment()
      extraction = self.data[0:x]

      return extraction

class InheritedAlignment(BaseAlignment):
   def extractx(self, x):
      BaseAlignment.extractx(self, x)

      #do something more here...

      return extraction

--

The problem is that the return value of extractx from
InheritedAlignment is a BaseAlignment....  When the inherited function
attempts to add the inherited data to the extraction, it'll fail quite
miserably.

The only solution that I can think of is to create a copy of self and
modify that, but that smacks a bit of inelegance -- and probably is
asking for too many problems.

Any hints?

Thanks,

Noah
nspies at geocities.com





More information about the Python-list mailing list