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....@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
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to