By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,835 Members | 984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,835 IT Pros & Developers. It's quick & easy.

Using variables across modules

P: n/a
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?
Jul 23 '08 #1
Share this Question
Share on Google+
5 Replies

P: n/a


Aaron Scott schrieb:
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
Jul 23 '08 #2

P: n/a
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.
Jul 23 '08 #3

P: n/a
On Wed, Jul 23, 2008 at 3:06 PM, Aaron Scott
<aa***************@gmail.comwrote:
... 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)

--
Jerry
Jul 23 '08 #4

P: n/a
Aaron Scott wrote:
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>

Jul 23 '08 #5

P: n/a
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!
Awesome. After reading those two pages, I was able to correct the code
and get things up and running. Thanks!

Aaron
Jul 23 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.