I have an object with an attribute that is a list. For example:
obj.a = [3, 4, 5]
I would like to get the following behavior (but I can't manage to find a solution using magics/etc.) :
l = obj.a
obj.a[0] = 2
print(l) --> [3, 4, 5]
print(obj.a) ---> [2, 4, 5]
Of course I could simply use copy.deepcopy :
l = copy.deepcopy(obj.a)
but for several reasons I would like, somehow, to make this step automatic/hide it for my users.
[EDIT] Using getattribute and returning a copy won't work of course:
import copy
class Test:
def __init__(self):
self.a = []
def __getattribute__(self, attr):
if attr == 'a':
return copy.deepcopy(super(Test, self).__getattribute__(attr))
Any help appreciated !
Thnak you, Thomas