473,574 Members | 2,213 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 1300


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.comwr ote:
... 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
16735
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
1609
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 in a couple of low-level modules that are imported into the main module, and in the main module itself. They
0
11526
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 file. I'm not very familiar with this aspect of perl, and have been having some trouble in the POSTing of the form data, while using cookies to 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) I want to dynamically create a multidimensional array and write code to manipulate that. Essentially I have the following setup:
9
2372
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 - perhaps I'm just not very smart or perhaps MS is making this too hard for us sql bunnies to understand - I dunno, but I'd really appreciate someone...
1
1595
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 manipulation, etc). But I have variables in the main module that other modules need to access such as AppPath, etc. What is the "best practice"...
5
1430
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 variable between pages. any ideas?
21
34368
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 obvious of which is the sharing of files. For example, you upload images to a server to share them with other people over the Internet. Perl comes ready...
3
2391
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 modules, only 2 out of 9 have copied across. Do I need to do something special to recompile modules and take them across to the new version? I...
0
7803
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7720
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8044
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8230
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8096
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5618
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5299
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3749
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2240
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.