Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Newbie - Variable Scope (http://www.velocityreviews.com/forums/t324962-newbie-variable-scope.html)

RN 11-17-2003 10:32 AM

Newbie - Variable Scope
 
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





KefX 11-17-2003 12:50 PM

Re: Newbie - Variable Scope
 
>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


Gonšalo Rodrigues 11-17-2003 12:55 PM

Re: Newbie - Variable Scope
 
On Mon, 17 Nov 2003 16:02:14 +0530, "RN"
<rohit.nadhani@tallysolutions.com> wrote:

>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

Duncan Booth 11-17-2003 03:15 PM

Re: Newbie - Variable Scope
 
"RN" <rohit.nadhani@tallysolutions.com> wrote in
news:mailman.788.1069069240.702.python-list@python.org:

> test.py
> -------
> from testmod import *


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

> a1 = 10


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
> -----------
> def modfunc():
> print a1


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.

--
Duncan Booth duncan@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?


All times are GMT. The time now is 08:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.