Inheritance Question

George Sakkis george.sakkis at
Sat Nov 11 03:53:50 CET 2006

Gabriel Genellina wrote:

> If walking in general, have some common structure, you can put the
> "sketch" on Legs and let the derived classes "fill the gaps". This is
> known as "Template Method Pattern" - look for it.

Or if you'd rather see a concrete example, here's how your toy example
would look refactored using the template pattern:

class Creature(object)
  def run(): pass
  def walk():
    # lots of commands
    # that do not depend on the
    # number of legs but definitely
    # have to do with walking

    self._specialized_walk(*args, **kwds)

    # more commands

  def _specialized_walk(self, *args):
    raise NotImplementedError

class UnipedalCreature(Creature):
  def _specialized_walk(self, *args, **kwds):
    # blahblah

class BipedalCreature(Creature):
  def _specialized_walk(self, *args, **kwds):
    # blahblah


More information about the Python-list mailing list