Unqualified global vars, how?

  • Thread starter Christopher J. Bottaro
  • Start date
C

Christopher J. Bottaro

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.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top