473,508 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using variables across modules

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
5 1287


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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
16725
by: Fuming Wang | last post by:
Hi, I have several modules that need to access global variables among them. I can do that by import modules: module A: gA = 'old' module B: import A
4
1604
by: Torsten Bronger | last post by:
Hallöchen! I have a file that looks a little bit like a C header file with a long list of variables (actually constants) definitions, e.g. VI_ATTR_TIMO = 0x54378 .... Actually I need this...
0
11519
by: Antwerp | last post by:
Hi, I'm trying to create a perl script that will log into a website (the login form uses POST), navigate to several pages, and append the (html) content parsed from those pages to a seperate log...
35
721
by: whisper | last post by:
My question is whether it is better to use a global variable to hold a dynamically malloced multidim array or pass around pointers to it. The details are below (forgive the long winded explanation)...
9
2364
by: Greg Linwood | last post by:
I'm having difficulty understanding Session state in ASP.Net. It's almost embarrassing asking this as I've been using ASP since it was first released & it really shouldn't be this hard to use -...
1
1583
by: Terry Olsen | last post by:
What is the best practice for sharing variables across modules in the same project? I'm doing a console app with different modules for different functions (file i/o, sql commands, string...
5
1425
by: Steve Mauldin | last post by:
Having weird things happening with my code. Two users on at the same time and data entered by one user is added into another users global variable. global variable data being stored in session...
21
34326
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
3
2381
by: Roy Tong | last post by:
I maintain a shared database on Access 97. I've just tried converting a test copy of the database to 2003 and it appeared to work OK. That is I got no error messages. However then I look at my...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.