[Tutor] Sharing data between modules

Dave Kuhlman dkuhlman at rexx.com
Thu Oct 19 18:07:21 CEST 2006


On Thu, Oct 19, 2006 at 10:41:27AM +0100, etrade.griffiths at dsl.pipex.com wrote:
> Hi
> 
> just starting to get to grips with writing GUIs in Python using wxPython and 
> not being a computer scientist, have a philosophical question about the "best" 
> way to pass data between various modules.  For example, I anticipate having 
> one module to look after the GUI stuff where users can input data (either from 
> the keyboard or file), another to process the data and (probably) a third to 
> display the calculation results.  Just not sure what the most efficient way of 
> passing data between the various modules.  I can think of 4 ways to do this, 
> of which 1 is not possible in Python (I think).
> 

Ask yourself the following question: Does the data cohere or stick
together in some way?  For example, is a user entering information
for a new member or a new account?  If so, define a class that will
serve as a container for this collection of related information,
perhaps a class called AccountInfo.  Now somewhere (in the GUI
stuff, maybe), you will create an instance of the AccountInfo class
and pass that instance between your modules.

A slightly different need is where you want to share a *single* set
of common values among several modules.  An example might be a set
of configuration values that are read from a config file at the
application start-up.  For this you could use a "config" module
(config.py, say) containing variables that hold these values, and
each module that needed access to these values could do "import
config".  Although, I suppose that a singleton class (a class
intended to have only one instance) might be just as good, and
would be more pleasing to the "object-orientated" among us.

Dave

[snip]


-- 
Dave Kuhlman
http://www.rexx.com/~dkuhlman


More information about the Tutor mailing list