[wxPython-users] Accessing Variable Across Modules
Christopher Barker
Chris.Barker at noaa.gov
Fri Nov 3 13:06:02 PST 2006
Rich Shepard wrote:
> Interestingly enough, I stumbled into the middle of a thread on the
> python
> mail list that addressed this exact issue. I created a new module,
> config.py, that contains only "projname = ' '." By importing config in all
> files needing that variable, it's made available to them.
>
> This is, apparently, the same approach used by the 'singleton pattern'
yup. You could google it, but essentially, the 'singleton pattern' is
simply when you have a class that there is designed to be only a single
instance of.
In Python, everything is about namespaces. A class, a class instance, a
module -- they are all simply different namespaces, with slightly
different rules about how names are resolved.
I use a Config.py module quite frequently in my code. However, I usually
put a few classes in it to hold different types of information, rather
than putting it all in the global namespace. That also makes it easier
to have methods (or properties) to set values. If you put them all in
the module global namespace , you have to make the attributes global to
set them.
-Chris
--
Christopher Barker, Ph.D.
Oceanographer
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception
Chris.Barker at noaa.gov
More information about the wxpython-users
mailing list