[Tutor] Immutable objects
Nitin Das
nitin.162 at gmail.com
Thu Aug 19 07:30:43 CEST 2010
class mymut(object):
def __setattr__(self,k,v):
if hasattr(self,k):
if self.__dict__.get(k) == None:
self.__dict__[k] = v
else:
raise TypeError("Cant Modify Attribute Value")
else:
raise TypeError("Immutable Object")
class mm(mymut):
x = ''
y = ''
def __init__(self,x,y):
self.x = x
self.y = y
p = mm(10,11)
print p.x
print p.y
I have created this immutable object.Is there any other better
implementation?
thanks
-- nitin
On Thu, Aug 19, 2010 at 8:54 AM, Nitin Das <nitin.162 at gmail.com> wrote:
> Hello,
>
> Can somebody help me how to make immutable objects in python. I have
> seen overriding the __new__, __setattr__ methods.. but not comfortable with
> how to use them to make the object immutable.
>
> thanks in advance
> --nitin
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100819/c248f77d/attachment.html>
More information about the Tutor
mailing list