Using variables across modules

A

Aaron Scott

I'm having some trouble understanding how Python handles variables
across multiple modules. I've dug through the documentation, but I
still find myself at a loss.

When you import a module, are you creating an instance of the
variables within? For instance, if I have one file, "variables.py",
which contains "myvar = 0", and I import it into both "foo.py" and
"bar.py" with the line "from variables import *", and then set myvar
in "foo.py" and "bar.py" to different values, will each file have a
different value for myvar? If so, how can I ensure that a change to
myvar in "bar.py" is reflected by "foo.py"? Or am I completely off
base?
 
U

Uwe Schmitt

Aaron said:
I'm having some trouble understanding how Python handles variables
across multiple modules. I've dug through the documentation, but I
still find myself at a loss.

When you import a module, are you creating an instance of the
variables within? For instance, if I have one file, "variables.py",
which contains "myvar = 0", and I import it into both "foo.py" and
"bar.py" with the line "from variables import *", and then set myvar
in "foo.py" and "bar.py" to different values, will each file have a
different value for myvar? If so, how can I ensure that a change to
myvar in "bar.py" is reflected by "foo.py"? Or am I completely off
base?

Just wirte test code ! Python is cool for checking such things, you do
do
not have to compile, you need not write boilerplate main() stuff.

Greetings, Uwe
 
A

Aaron Scott

Just wirte test code !

variables.py:

myvar = 5
print myvar

foo.py:

from variables import *
def PrintVar():
print myvar

bar.py:

from variables import *
from foo import *
print myvar
myvar = 2
print myvar
PrintVar()

"python bar.py" output:

5
5
2
5

.... which is what I was expecting, but not what I want. Obviously,
each import is creating its own instance of the variable. What I need
is a way to change myvar from within "bar.py" so that PrintVar()
returns the new value, even though it's in a different module.
 
J

Jerry Hill

... which is what I was expecting, but not what I want. Obviously,
each import is creating its own instance of the variable. What I need
is a way to change myvar from within "bar.py" so that PrintVar()
returns the new value, even though it's in a different module.

That's what happens when you do "from variables import *", it creates
those names in the local namespace. If you just import the module,
then you can do what you want. For example:

---variables.py---
myvar = 5
print myvar

---foo.py---
import variables
def PrintVar():
print variables.myvar

---bar.py---
import variables, foo
print variables.myvar
variables.myvar = 2
print variables.myvar
foo.PrintVar()

---output from running bar.py---
5
5
2
2

For more on how the import statement works, see the library reference
(http://docs.python.org/ref/import.html), or maybe this article on the
various ways you can import things and the differences between them
(http://effbot.org/zone/import-confusion.htm)
 
F

Fredrik Lundh

Aaron said:
I'm having some trouble understanding how Python handles variables
across multiple modules. I've dug through the documentation, but I
still find myself at a loss.

When you import a module, are you creating an instance of the
variables within? For instance, if I have one file, "variables.py",
which contains "myvar = 0", and I import it into both "foo.py" and
"bar.py" with the line "from variables import *", and then set myvar
in "foo.py" and "bar.py" to different values, will each file have a
different value for myvar? If so, how can I ensure that a change to
myvar in "bar.py" is reflected by "foo.py"? Or am I completely off
base?

first read this to learn how objects and variables work in Python:

http://effbot.org/zone/python-objects.htm

and then read this to learn how from-import works, and when you're
supposed to use it:

http://effbot.org/zone/import-confusion.htm

hope this helps!

</F>
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top