Newbie - Variable Scope

R

RN

I have a 2 Python scripts that contain the following lines:

test.py
-------
from testmod import *
a1 = 10
modfunc()

testmod.py
-----------
def modfunc():
print a1

When I run test.py, it returns the following error:

File "testmod.py", line 2, in modfunc
print a1
NameError: global name 'a1' is not defined

My intent is to make a1 a global variable - so that I can access its value
in all functions of imported modules. What should I do?

Thanks in advance,

Rohit
 
K

KefX

I have a 2 Python scripts that contain the following lines:
test.py
-------
from testmod import *
a1 = 10
modfunc()

testmod.py
-----------
def modfunc():
print a1

When I run test.py, it returns the following error:

File "testmod.py", line 2, in modfunc
print a1
NameError: global name 'a1' is not defined

My intent is to make a1 a global variable - so that I can access its value
in all functions of imported modules. What should I do?

Thanks in advance,

Rohit

I think this is technically one script (not "two scripts"), just in two
modules, but whatever.

First off, you're using the form "from x import y", (in this case, x is
'testmod' and y is '*') which shouldn't be used for your own modules unless you
know what you're doing. It is better to write this as "import x".

This has nothing to do with the problem, though. What you have to do for your
specific test case is have testmod.py import test.py, because it uses a
variable that's defined in test.py. Using "from x import *" doesn't make the x
act like a C-style include file, though there are many similarities.

Of course, it's best to avoid situations where two modules import each other
altogether. I would put the globals in a third module and have everything
import that module. Thus, one solution would be this:

test.py
-------
import testmod
import globals

globals.a1 = 10
testmod.modfunc()


testmod.py
----------
import globals

def modfunc():
print globals.a1


globals.py
----------
# Blank, but you can define 'a1' here instead of in test.py if it's more
appropriate


Of course, the uber-best solution is to avoid globals altogether, but that's
another thing...

- Kef
 
G

Gonçalo Rodrigues

I have a 2 Python scripts that contain the following lines:

test.py
-------
from testmod import *
a1 = 10
modfunc()

testmod.py
-----------
def modfunc():
print a1

When I run test.py, it returns the following error:

File "testmod.py", line 2, in modfunc
print a1
NameError: global name 'a1' is not defined

My intent is to make a1 a global variable - so that I can access its value
in all functions of imported modules. What should I do?

The only truly global names in Python are the builtin ones and you
shouldn't be mucking with those unless you have a very good reason to.
Plain global variables are just global at module-level not across
modules.

To solve your problem:

- import test.py everywhere you need a1.

- Even better, reorganize your code so that you do not need global
variables in the first place.

Any more questions just holler, with my best regards,
G. Rodrigues
 
D

Duncan Booth


Don't use 'from testmod import *'. It will just cause confusion. Use
'import testmod' instead.

Use: testmod.a1 = 10

You want to set a1 in the other module. If you had a global variable called
a1 in the other module, assigning to a1 in this module would have no
effect. They are two different variables. And that is why you don't want to
use the 'from' variant of import, it simply assigns the value of each
variable from testmod into an unrelated variable of the same name in the
local module when you want to maintain a relationship between them.
modfunc()
Use: testmod.modfunc()
testmod.py

If you are going to have a variable called 'a1' in this module, it would be
nicer to initialise it here so as to make it obvious. So add:

a1 = 0

outside the function.
When I run test.py, it returns the following error:

File "testmod.py", line 2, in modfunc
print a1
NameError: global name 'a1' is not defined

My intent is to make a1 a global variable - so that I can access its
value in all functions of imported modules. What should I do?

By the way, a1 isn't a terribly good name for a global variable; it doesn't
convey anything at all about the purpose of the variable. If you must use
global variables (and you should think twice or thrice before you do), you
should at least give them purposeful names.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top