[Edu-sig] Design Pattern question

Kirby Urner urnerk at qwest.net
Sun Oct 19 20:02:51 EDT 2003


I wondered if anyone had ideas about a design pattern I've been playing
with.  Is it perverse?

I don't want to create global variables at the module level, but have a lot
of information that many objects need to share and keep current about,
possibly update.  I don't always know these variables at the time my objects
get initialized.  Some become available later.

So the idea is to have my classes inherit from a base class the main purpose
of which is to get runtime "globals".  These may be defined well after the
subclass objects have been initialized.

Also, I don't explicitly initialize this shared parent class either (it
doesn't have an __init__ method) -- I just stuff it with variables at the
class level when I want to communicate them to the children.

Example:

 >>> class Holder:  #  a holding tank for shared values/references
	 pass

 >>> class Foo(Holder):     #  use the holding tank as a base class
	 def __init__(self):
		self.a = 1

		
 >>> o = Foo()
 >>> o.a
 1
 >>> Holder.b = 3           # create a value inside the base
 >>> o.b                    # now the subclass has access to it
 3
 
 >>> class Goo(Holder):
	def __init__(self):
		self.q = 2

		
 >>> k = Goo()
 >>> k.q
 2
 >>> k.b                     # a new subclass is automatically up to date
 3

 >>> class Super:
  	  def __init__(self):
		 self.m = 5
		 Holder.topclass = self  # note the instance stuffs in
Holder

		
 >>> t = Super()         # here we create a new object
 >>> k.topclass          # and find it automatically accessible
 <__main__.Super instance at 0x00ADD0F8>

 >>> k.topclass.m        # k and o are different user types
 5
 >>> o.topclass.m
 5
 >>> o.topclass.m = 9    # but are both free manipulate topclass
 >>> k.topclass.m
 9

Kirby





More information about the Edu-sig mailing list