a new design pattern for Python Library?

The Eternal Squire eternalsquire at comcast.net
Tue Nov 22 20:38:03 EST 2005


Hi,

I tend to use this design pattern a lot in order to aid in
compartmentalizing interchangeable features in a central class that
depend on the central class's data.  I know that explicit class
friendship designs syntactic sugar, but I am thinking this should be a
standard design pattern to make friendship dependencies a little more
self documenting.

Opinions?

The Eternal Squire


class Friend (object):
  def __init__ (self, friend):
    object.__init__ (self)
    self.friend = friend


Example use:

class Bar (Friend):
  TITLE       = None
  DEFAULT = 1
  def __init__ (self, top):
    Friend.__init__ (self, top)
    self.data = self.default * self.default

  def __str__ (self):
    print "%s mode %d default = %d" % (self.__class__.__name__,

self.friend.mode,
                                                          self.data)


class ABar (Bar):
  DEFAULT = 2


class Foo:
  BAR = ABar

  def __init__  (self):
    self.bar = self.BAR (self)
    self.mode = 1

  def __str__  (self):
    return str   (self.bar)




More information about the Python-list mailing list