I have a class which contains data as attributes and which has a method to return a tuple containing these attributes:
class myclass(object):
def __init__(self,a,b,c):
self.a = a
self.b = b
self.c = c
def tuple(self):
return (self.a, self.b, self.c)
I use this class essentially as a tuple where the items (attributes) can be modified/read through their attribute name. Now I would like to create objects of this class, which would be constants and have pre-defined attribute values, which I could then assign to a variable/mutable object, thereby initializing this variable object's attributes to match the constant object, while at the same time retaining the ability to modify the attributes' values. For example I would like to do this:
constant_object = myclass(1,2,3)
variable_object = constant_object
variable_object.a = 999
Now of course this doesn't work in python, so I am wondering what is the best way to get this kind of functionality?
copyyourconstant_objectintovariable_object?constant_objectisn't actually constant, but… how could you expect Python to magically figure that out from the name of the variable?=assignment in Python doesn't "copy" anything, it just assigns the same value to another name. And it's not an operator that you can overload. There is no way thatvandccould possibly refer to different objects after you writev = c, becausev = cmeans that they refer to the same object.constant_object. So when you put the same value intovariable_object, it's still not constant. So, despite what the question says,variable_object.a = 999actually works just fine.