Unqualified global vars, how?

Christopher J. Bottaro cjbottaro at alumni.cs.utexas.edu
Mon Oct 25 10:08:39 CEST 2004


>From the python programming FAQ, I learned you can do this:

Globals.py:
gv = 1

A.py:
import Globals
class A:
        def __init__(self):
                print Globals.gv
                Globals.gv += 1

B.py:
import Globals
class B:
        def __init__(self):
                print Globals.gv
                Globals.gv += 1

main.py:
import A
import B
a = A.A()
b = B.B()

That will print:
1
2
and Globals.gv will have a final value of 3.

I want access to gv without having to qualify it with Globals.  I tried
saying:

A.py:
from Globals import gv
class A:
        def __init__(self):
                print gv
                gv += 1

But python complains that gv isn't defined yet.  Is what I want to do
possible?  I just want one module (file) with a bunch of globals vars and
to able to access those global vars without qualify them the module name.

Thanks for the help.




More information about the Python-list mailing list