a new design pattern for Python Library?

The Eternal Squire eternalsquire at comcast.net
Thu Nov 24 00:59:42 CET 2005


Let me try again:

Suppose I have a central class with complex behavior that I want to
simply write as a bare skeleton upon which to hang the auxillary
classes that help provide the complex behavior.

The naive approach would be to write:

class Skeleton:
  def __init__ (self):
    self.core_data = 1

class Auxillary:
  def __init__ (self):
    pass

  def  string   (self, skeleton):
    return "%s %d" % (self.__class__.__name__, skeleton.core_data)


skeleton = Skeleton ()
auxillary = Auxillary ()
auxillary.behavior ()


What I will typically do is create the auxillary class as a friend so
that I can have tighter integration between the Skeleton instance and
an Auxillary member instance, where the Auxillary instance isolates
behavior that I might want to modify later through inheritance.

class Auxillary (Friend):
  def  __str__  (self):
    return "%s %d" % (self.__class__.__name__, self.friend.core_data)

class Skeleton:
  def __init__ (self):
    self.auxillary = Auxillary (self)

skeleton = Skeleton ()

print skeleton.auxillary


See the difference?  I know this is a bit contrived, but I wanted to
keep the effect simple.  Effectively I now have a self-documenting
friend relationship between classes.  

The Eternal Squire




More information about the Python-list mailing list