473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Global variables in modules/functions

I'm trying to write program with a main.py and several functions
distributed in auxiliary *.py files. I've looked through the archives on
the 'global' keyword, but I'm still not sure how to get this right. I
want to be able to have some variables that can be accessed/changed by
many modules/functions.

Here's an example.

____________
main.py
____________
import change
n = 1
change.change_n()
print n

____________
change.py
____________
def change_n():
global n
n = 2
Running main.py gives this: 1. So n is not being changed when the
function is called. I've tried adding a 'import from main *' line to
change.py, but this only gives an error message.

Any ideas?

Thanks
--
Aaron Deskins
Graduate Student
Chemical Engineering
Purdue University
Jul 18 '05 #1
7 4297
Hi !

It is extraordinary: I have just had the same problem, there is seulemet
one hour!
For the moment, I prefix the variable; but I will read with interest the
reponses with your message.

@-salutations
--
Michel Claveau

Jul 18 '05 #2
Aaron Deskins wrote:
I'm trying to write program with a main.py and several functions
distributed in auxiliary *.py files. I've looked through the archives on
the 'global' keyword, but I'm still not sure how to get this right. I
want to be able to have some variables that can be accessed/changed by
many modules/functions.

Here's an example.

____________
main.py
____________
import change
n = 1
change.change_n()
print n

____________
change.py
____________
def change_n():
global n
n = 2
Running main.py gives this: 1. So n is not being changed when the
function is called. I've tried adding a 'import from main *' line to
The correct syntax is

from main import *

This will only copy the binding
n = 1
and a following
n = 2
assignment will not affect n in the main module.
change.py, but this only gives an error message.

Any ideas?


A clean way to share data between different modules is to introduce another
module and use a qualified name to access data in that module:

shared.py
n = 1

main.py
import shared
import change

change.change_n()
print n

change.py
import shared
def change_n():
shared.n = 2
However, I'm not convinced that you really need that kind of global data at
all:

main.py
import change
n = 1
n = change.change_n()
print n

change.py
def change_n():
return 2

is clearly a superior design.

Finally, what you originally requested is not impossible, just bad:

change.py
import sys

def change_n():
sys._getframe(1).f_globals["n"] = 2

Peter


Jul 18 '05 #3
Peter Otten wrote:
A clean way to share data between different modules is to introduce
another module and use a qualified name to access data in that module:

shared.py
n = 1

main.py
import shared
import change

change.change_n()
print n
Oops, that should be

print shared.n
change.py
import shared
def change_n():
shared.n*=*2


Jul 18 '05 #4
Aaron Deskins wrote:
I'm trying to write program with a main.py and several functions
distributed in auxiliary *.py files. I've looked through the archives on
the 'global' keyword, but I'm still not sure how to get this right. I
want to be able to have some variables that can be accessed/changed by
many modules/functions.


Globals aren't common to all modules. You can think of globals as
module-specific. That is, the global n is not shared between your two
modules. It will be in one of the modules, and the other module can
then reference it explicitly. Something like (untested):

main.py
-------
import change
change.n = 1
change.change_n()
print change.n

change.py
---------
n = 0 # here's the actual global
def change_n():
global n
n = 2

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
They have rights who dare defend them.
-- Roger Baldwin
Jul 18 '05 #5
Aaron Deskins wrote:
I'm trying to write program with a main.py and several functions
distributed in auxiliary *.py files. I've looked through the archives on
the 'global' keyword, but I'm still not sure how to get this right. I
want to be able to have some variables that can be accessed/changed by
many modules/functions.

The easiest way to do this is to create a module named "globals", and
have any modules wanting to work like:

import globals
....
def change_n():
globals.n = 2

Here's an example.
____________
main.py
____________
import globals, change
globals.n = 1
change.change_n()
print globals.n
____________
change.py
____________
import globals
def change_n():
globals.n = 2

There are ways to force python into doing what you want, but they are
fraught with peril.

--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #6
Scott David Daniels wrote:
The easiest way to do this is to create a module named "globals", and
have any modules wanting to work like:

.... except that you might want to use a different name, to avoid
shadowing the globals() builtin function. :)

Jeff Shannon
Technician/Programmer
Credit International

Jul 18 '05 #7
Thanks! That works great.

Scott David Daniels wrote:

The easiest way to do this is to create a module named "globals", and
have any modules wanting to work like:

import globals
...
def change_n():
globals.n = 2

Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

88
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
6
by: dkeeney | last post by:
I have a newby type question on how global variables work between modules. I have a module test2.py that defines a global variable as well as two functions to operate on that variable. A script...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
11
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use...
7
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a...
25
by: Daniel Bass | last post by:
how do i declare a global variable in c#.net? it's like it want's everything in classes... there are times when globals are good, like having constants in a program which apply to several...
8
by: newbie | last post by:
Hello, I have questions about global variables in OOP (in general) and Python (in specific). I understand (I think) that global variables are generally not a good idea. However, if there are...
7
by: zeecanvas | last post by:
Hi, First of all: Yes, I know global variables are bad, but I've a huge amount of legacy code, and I've to maintain it _as_is_. I'm maintaining a big program. I moved all (program-wide scope)...
3
by: barronmo | last post by:
I'm getting an error msg I don't understand, "global name EMR_globals is not defined", and could use some help. I've separated the application I'm building into several modules. One of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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,...

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.