[Edu-sig] Re: Design Pattern question

Lee Harr missive at hotmail.com
Mon Oct 20 19:38:48 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 
>get initialized.  Some become available later.
>So the idea is to have my classes inherit from a base class the main 
>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.

I think I might find it confusing because of the way names get bound
to certain things at class level, but overridden at the instance level.
Like this:

>>>class Holder:
...   pass
>>>class Foo(Holder):
...   def __init__(self):
...     self.a = 1
>>>Holder.b = 3
>>>class Goo(Holder):
...   def __init__(self):
...     self.q = 2

So, one instance changing the value does not change it in other
instances, unless they all always explicitly access it as Holder.x

What I have done (and I am certainly not saying it is the best way,
or even any better than what you have) is to create a separate
module that just holds a bunch of names that many other modules
need to access. Everyone always refers to it by it's full name and
so everyone always has the same information.

It's kind of a global namespace that is not global, if that makes
any sense.

In pygsear, it is the conf module. Some things that I keep in there
are the size of the window, sound system status, time count (ticks),
and I am probably going to put a handle for the currently running
Game instance there too.

Add photos to your messages with MSN 8. Get 2 months FREE*. 

More information about the Edu-sig mailing list