global variable

Joe Francia usenet at soraia.com
Mon Jul 21 19:13:00 CEST 2003


Tom wrote:
> Hi,
> 
> I have one "master" program which calls a small program. In this small 
> program I want to work with a value from the "master" program. But I 
> always get this error: NameError: global name 'T' is not defined
> 
> How can I define a global variable?
> 
> Thanks for your help.
> Regards, Tom

You can't (well, there is a way, but you probably don't want to be 
messing with that).

By "program" I assume you mean "module".  You can do something like:

#master.py
T = 'Some value'

#small.py
import master
doSomething(master.T)

However, if master.py is also importing small.py, you'll get a circular 
reference, which may cause hard to track bugs, in which case it's best 
to have a third module for common (global) variables (and functions, if 
you'd like), and have both master.py and small.py import that.

jf





More information about the Python-list mailing list