"intermodule-global" variables

Hung Jung Lu hungjunglu at yahoo.com
Sun Jun 6 00:45:39 EDT 2004


eddy at netido.de (Eddy Ilg) wrote:
> ---- helper.py 
> a=5
> 
> def printa():
> 	global a
> 	print a
> ----
> 
> >> from helper import * 
> >> a
>  5
> >> a=6
> >> a
>  6
> >> printa()
> 5

Change that to

---- helper.py 
import sys
sys.a = 5
def printa():
    print sys.a
----
from helper import *
sys.a = 6
printa()

----------------------------
There are three namespaces in Python at any given moment: local,
global, and built-in. You need to go through the built-in namespace,
for inter-module purposes. You could choose any other module instead
of sys.

A more drastic approach is to directly tweak the __builtin__ module.
That simplifies the reading (retrieval), but not the writing
(assignment).

import __builtin__
__builtin__.a = 4
print a

regards,

Hung Jung



More information about the Python-list mailing list