I've got a code:
def constant(f):
def fset(self, value):
raise SyntaxError( "You can't change a constant!" )
def fget(self):
return f(self)
return property(fget, fset)
class A( object ):
def __init__( self, a_list ):
super( A, self ).__init__()
self.__data = a_list[:]
@constant
def data( self ):
return self.__data
I would like to make self.__data accessible, but unchangeable and uneditable from outside of the class. I can't simply change it to a tuple, cause I would like it to be easy to change for methods inside the class. The solution that I presented above doesn't work, when someone modifies the list:
>>> a = A( range(5) )
>>> a.data = []
Traceback (most recent call last):
...
SyntaxError: You can't change a constant! # it works
>>> a.data[0]=6 # it doesn't work
>>> a.data
[6, 1, 2, 3, 4]
Do you know any nice solution of my problem?