[Tutor] Confused about globals
Etrade Griffiths
etrade.griffiths at dsl.pipex.com
Fri Jun 9 11:42:03 CEST 2006
Hi
I have a series of python programs that plot stuff using PYX. Common to
these is the need to read in a list of (well) locations in (X,Y) coords so
I put that code in a separate module called shared_funcs.py. The coords
are stored in dictionaries which I want to use later in the "main" program
- every time I find a well with data, I can get the well's (x,y) coords by
looking up the well name in the dictionary. The code snippet looks like this:
=== main.py ====
# define coord dictionaries for global use
DE={}
DN={}
import shared_funcs()
shared_funcs.get_xy_data() # Get coords from file
... do plotting stuff
=== shared_funcs.py ===
def get_xy_data():
in_file=open("well_xy.txt","r")
for line in in_file
L=line.split()
well=L[0]
x=L[1]
y=L[2]
DE[well]=x
DN[well]=y
in_file.close()
The problem is that DE and DN appear not to be accessible to get_xy_data -
presumably this is because shared_funcs.py and main.py don't share the same
scope. So, is there any way to let get_xy_data change DE and DN? I guess
the obvious way is to pass them to get_xy_data as arguments - but is there
a more "pythonic" method? Thanks in advance!
More information about the Tutor
mailing list