Design Question

Bernhard Herzog bh at intevation.de
Tue Jan 13 12:40:59 EST 2004


ferrell at diablotech.com (Robert Ferrell) writes:

> My question is about how to architect a class hierarchy where some
> methods are nearly identical between a superclass and a subclass, but
> differ slightly.  For example:
>
> class Sup(object):
>    def __init__(self):
>       self.specialFlag = False
>    def aMeth(self):
>        <do some stuff>
>        <if self.specialFlag, do a special thing>
>        <do more stuff>
>
> class Sub(Sup):
>   def __init__(self):
>     self.specialFlag = True

A commonly used alternative would be 

class Sup(object):
   def aMeth(self):
       <do some stuff>
       self.specialMethod()
       <do more stuff>
   def specialMethod(self):
       pass

class Sub(Sup):
   def specialMethod(self):
       <do a special thing>



   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
Thuban                                  http://thuban.intevation.org/



More information about the Python-list mailing list