Don't try to stick this into a global variable. It's horrid. Do this instead.
import string
variables = {}
for x in string.ascii_uppercase:
variables[x] = Variable(x)
The you access it from variables['Q']
, for example.
If you want attribute access you can do:
class Variables(object): pass
and
variables = Variables()
for x in string.ascii_uppercase:
setattr(variables, x) = Variable(x)
But in that case I'd rather create them dynamically in a Variables container class.
import string
class Variable(object):
def __init__(self, value):
self.value = value
def __repr__(self):
return "<Variable %s>" % self.value
class Variables(object):
def __getattr__(self, n):
if n in string.ascii_uppercase:
v = Variable(n)
setattr(self, n, v)
return v
raise AttributeError("Variables instance has no attribute %s" % n)
Usage:
>>> v = Variables()
>>> v.G
<Variable G>
>>> v.Y
<Variable Y>
>>> v.G is v.G
True
Simple and clean, no ugly hacks (well, except a getattr, and it's not that ugly), and you don't even have to make the Variables you don't need.