global variable

T

Tom

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
 
H

Harry George

Tom said:
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


master.py:
my_global_var=1234

small.py:
import master
....
my_local_var=master.my_global_var * 3.14
 
J

Joe Francia

Tom said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top