class Foo:
def __init__(self, data: list):
self._data = data
@property
def data(self):
return self._data
Doing this prevents from assignments like obj.data = [1, 2, 3], but it is still possible to modify it with obj.data.append(10) because lists are mutable. There's no problem for being able to modify it from obj._data, but I don't want it to be mutable from obj.data.
One thing I tried is making a copy of _data and then returning it
class Foo:
def __init__(self, data: list):
self._data = data
@property
def data(self):
copy = self._data[:]
return copy
but I'm not sure if this is the best way to do it, are there any other alternative ways?
tupleinstead of alist.