Global variables in modules/functions

A

Aaron Deskins

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
 
M

Michel Claveau - abstraction méta-galactique non t

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
 
P

Peter Otten

Aaron said:
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
 
P

Peter Otten

Peter said:
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
 
E

Erik Max Francis

Aaron said:
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
 
S

Scott David Daniels

Aaron said:
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
(e-mail address removed)
 
J

Jeff Shannon

Scott said:
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
 

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,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top