[Tutor] Newbie: "Global" vars across modules
Alan Trautman
ATrautman at perryjudds.com
Wed Dec 3 16:41:47 EST 2003
My favorite method, from a discussion on this list is to create a module and
import it into all modules that need access. I name it myappname_globals.py
and it contains things like database name, debug level, and other constants
that might need changing. A config file is the other option I use if there
is a lot to configure. If debug level is the only reason a command line
argument is safest.
This is the one of the very few cases where I like FROM myappname_globals.py
IMPORT *. this means that the variable names are the same in every case.
Hope that helps,
Alan
-----Original Message-----
From: Jeremy Oddo [mailto:joddo at apixels.net]
Sent: Wednesday, December 03, 2003 3:43 PM
To: Python Tutor
Subject: [Tutor] Newbie: "Global" vars across modules
I'm guessing this has come up time and time again, but I can't seem to
find the information that I need.
Here's my problem: I want to create a global variable that EVERY module
can see. Why? Because I like to define a "debug_level" variable to print
out debugging information. I want to define "debug_level", then use it
throughout my main code and modules. I'm running into problems when I
call an external module that I've written. Here's the basic idea:
--begin gvars.py--
# Debug Level (0=NONE, 1=Normal, 2=Extended, 3=Verbose)
debug_level = 2
--myFunctions.py--
def ftest():
if (debug_level = 3):
print "VERBOSE DB: This is a verbose statement"
return 1
--testing.py--
from gvars import *
from myFunctions import *
print "Current debug level is:", debug_level
ftest()
So, testing.py is my main file. It imports gvars.py which contains my
global variable "debug_level". The testing.py file prints out
"debug_level" and all is good. However, when I make the call to "ftest()"
which is a function from A DIFFERENT FILE called "myFunctions.py", it
doesn't see "debug_level" (even if I add the line "global debug_level").
So how can I make a "truly global" global variable?
Thanks!
JO
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list