I have a class, and I created two instances of it ,object1 and object2, I addend an attribute to each called "number", I want to be able to choose to which class I apply a function that I have created to change the "number"attribute, I managed to create the function but when i execute it only does it at a local level in the function, it does not return the result to the original object1.number instance, I assumne the problem is in how I positioned my variables.
So i want my global object1.number attribute to be changed inside the number_changer function and then be able to see that change outside the function.
I have used the print function along the code to follow the process, and it seems to do well, just its unable to change the global attribute
class ClassA():
pass
object1 = ClassA()
object1.number = "0"
object2 = ClassA()
object2.number = "1"
current_object = "object1"
x = input("new number ")
print(object1.number)
def number_changer(object_name,new_number):
variable = object_name + ".number"
global object1
ref_obj_list = ["object1.number", "object2.number"]
real_obj_list = [object1.number, object2.number]
count = -1
for item in ref_obj_list:
count += 1
if variable == item:
real_obj_list[count] = new_number
d = real_obj_list[count]
return d
print(number_changer(current_object,x))
print(object1.number)
when I put the number "5" in the input the result is this
0
5
0
the object1.number attribute does not change or at least not in a global level