[Tutor] Sharing data between modules

Python python at venix.com
Thu Oct 19 17:48:13 CEST 2006

On Thu, 2006-10-19 at 10:41 +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).
> (1) via temp files: modules read to and write from temporary files
> (2) via function arguments: data passed from one module to another via 
> argument lists.  This seems the most logical but I have an irrational dislike 
> of long argument lists.  Could pass data objects (eg. C type structs) or 
> dictionaries via the argument list but I don't think this gets around 
> the "problem" of long argument lists and we now need pack/unpack type 
> functions.  Maybe this isn't a problem anyway?

dictionaries can work pretty well if you leverage the ** syntax in
function arguments.  If we imagine a dictionary named passdata where the
keys are named k1 to k99:

def func1(k1, k30, k47, **kwds):
	# the unused parameters are collected in the dictionary kwds
	# do stuff with the parameters
	return result

result = func1( **passdata)

The functions that depend on specific variables can identify the
variables in the parameter definition.  I've generally been happy with
this kind of approach.

> (3) via globals: AFAIK this is not recommended so we will move swiftly on
> (4) indirectly: thinking here of something along the FORTRAN COMMON blocks 
> which (I think) Python doesn't have
> So, looks like it's a choice between (1) and (2) - or have I missed something 
> obvious?  What is the most "pythonic" way of doing this?  Thanks in advance 
> for any suggestions
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
Lloyd Kvam
Venix Corp

More information about the Tutor mailing list