I have a variable ab = 2
; Now I let's say we have this variable as a string 'a'+'b'
. Based on this and this I know how to print the value of ab
when I call it as a string. My question is how I can modify it when I have it as a string.
For example:
class HELP:
def __init__(self):
self.ab = 2
self.a2b2 = 2
self.a3b3 = 2
self.a4b4 = 2
self.a5b5 = 2
count = 1
for k in [('a','b'),('a2','b2'),('a3','b3'),('a4','b4'),('a5','b5')]:
print(eval('self.'+k[0]+k[1])) # this will return the value
NewVar = 'self.'+'{}{}'.format(k[0],k[1])+'='+str(count*3)
globals()[NewVar] = count*3
count += 1
for k in [('a','b'),('a2','b2'),('a3','b3'),('a4','b4'),('a5','b5')]:
print(eval('self.'+k[0]+k[1]))
HELP()
will print:
2
2
2
2
2
2
2
2
2
2
but I expect to see:
2
2
2
2
2
3
6
9
12
15