Unqualified global vars, how?
Josiah Carlson
jcarlson at uci.edu
Mon Oct 25 17:11:20 EDT 2004
"Christopher J. Bottaro" <cjbottaro at alumni.cs.utexas.edu> wrote:
>
> >From the python programming FAQ, I learned you can do this:
[snip example]
> 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.
Quick answer:
yes it is possible.
Long answer:
C:\Temp>cat > Globals.py
class GV:
val = 0
def __iadd__(self, arg):
self.val += arg
return self
def __repr__(self):
return repr(self.val)
gv = GV()
C:\Temp>cat > A.py
from Globals import gv
class A:
def __init__(self):
global gv
print gv
gv += 1
C:\Temp>cat > B.py
from Globals import gv
class B:
def __init__(self):
global gv
print gv
gv += 1
C:\Temp>cat > main.py
import A
import B
a = A.A()
b = B.B()
C:\Temp>main.py
1
2
C:\Temp>
More information about the Python-list
mailing list