I have hundreds of variables (here is only few of them so you'll get idea how this code works) and I have no idea how to solve this problem. What I want to do is in second if statement to somehow modify variable before declaring it so I could copy class to it so I could bypass many needless if satements. Is it possible?
This code in second if statement of course is incorrect
globals()["p%sg%sr01_id" % (cp, awsg)]) = copy.copy(temp1)
but this is what I'd like to achieve before declaring variable to which I'll copy class
import copy
cp = "01"
awsg = "01"
temp1 = 0
temp2 = 0
first_hovered_variable = "01"
second_hovered_variable = "02"
class Char():
def __init__(self, name):
self.n = name
p01g01r01_id = Char("A1")
p01g01r02_id = Char("B2")
p01g01r03_id = Char("C3")
reg01_id = Char("001")
reg02_id = Char("002")
reg03_id = Char("003")
def swap_variables():
global cp
global awsg
global temp1
global temp2
global first_hovered_variable
global second_hovered_variable
global reg01_id
global reg02_id
global reg03_id
global p01g01r01_id
global p01g01r02_id
global p01g01r03_id
if first_hovered_variable == second_hovered_variable:
first_hovered_variable = 0
second_hovered_variable = 0
elif first_hovered_variable != 0 and second_hovered_variable != 0:
temp1 = copy.copy(globals()["p%sg%sr%s_id" % (cp, awsg, second_hovered_variable)])
temp2 = copy.copy(globals()["p%sg%sr%s_id" % (cp, awsg, first_hovered_variable)])
if first_hovered_variable == "01":
reg01_id = copy.copy(temp1)
globals()["p%sg%sr01_id" % (cp, awsg)]) = copy.copy(temp1)
elif first_hovered_variable == "02":
reg02_id = copy.copy(temp1)
globals()["p%sg%sr02_id" % (cp, awsg)]) = copy.copy(temp1)
elif first_hovered_variable == "03":
reg03_id = copy.copy(temp1)
globals()["p%sg%sr03_id" % (cp, awsg)]) = copy.copy(temp1)
if second_hovered_variable == "01":
reg01_id = copy.copy(temp2)
globals()["p%sg%sr01_id" % (cp, awsg)]) = copy.copy(temp1)
elif second_hovered_variable == "02":
reg02_id = copy.copy(temp2)
globals()["p%sg%sr02_id" % (cp, awsg)]) = copy.copy(temp1)
elif second_hovered_variable == "03":
reg03_id = copy.copy(temp2)
globals()["p%sg%sr03_id" % (cp, awsg)]) = copy.copy(temp1)
temp1 = 0
temp2 = 0
swap_variables()
print p01g01r01_id.n
print p01g01r02_id.n
print p01g01r03_id.n
print reg01_id.n
print reg02_id.n
print reg03_id.n