Creating variables on the fly...
Warren Postma
embed at geocities.com
Mon Apr 3 16:29:34 EDT 2000
You're using the Wrong Tool for the Job.
You want a Dictionary, or an Array. If you really like naming these objects
a,b, and c, use a dictionary:
V = {} # create a dictionary just for this bunch of variables
def AddVars( a, count, initial_value):
global V
c=ord(a[0])
for n in range(c,c+count):
V[chr(n)] = initial_value
def Sum(dict):
n = 0
for i in dict.keys():
n = n + dict[i]
return n
def Dump(dict):
k = dict.keys()
k.sort()
for i in k:
print i, "= ",dict[i]
AddVars("a",25,0)
V["a"] = 5
V["b"] = 10
print Sum(V)
Dump(V)
More information about the Python-list
mailing list