Thanks guys,<div> NamedTuple implementation is preety nice solution.</div><div><br></div><div>--nitin<br><br><div class="gmail_quote">On Thu, Aug 19, 2010 at 6:28 PM, Peter Otten <span dir="ltr"><__<a href="mailto:peter__@web.de">peter__@web.de</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div><div></div><div class="h5">Peter Otten wrote:<br>
<br>
> Nitin Das wrote:<br>
><br>
>> class mymut(object):<br>
>><br>
>> def __setattr__(self,k,v):<br>
>> if hasattr(self,k):<br>
>> if self.__dict__.get(k) == None:<br>
>> self.__dict__[k] = v<br>
>> else:<br>
>> raise TypeError("Cant Modify Attribute Value")<br>
>> else:<br>
>> raise TypeError("Immutable Object")<br>
>><br>
>><br>
>> class mm(mymut):<br>
>> x = ''<br>
>> y = ''<br>
>> def __init__(self,x,y):<br>
>> self.x = x<br>
>> self.y = y<br>
>><br>
>><br>
>><br>
>> p = mm(10,11)<br>
>> print p.x<br>
>> print p.y<br>
>><br>
>><br>
>> I have created this immutable object.Is there any other better<br>
>> implementation?<br>
><br>
> How about<br>
><br>
>>>> from collections import namedtuple<br>
>>>> Mm = namedtuple("Mm", "x y")<br>
>>>> p = Mm(10, 11)<br>
>>>> p.x<br>
> 10<br>
>>>> p.y<br>
> 11<br>
>>>> p.x = 42<br>
> Traceback (most recent call last):<br>
> File "<stdin>", line 1, in <module><br>
> AttributeError: can't set attribute<br>
>>>> p.z = 42<br>
> Traceback (most recent call last):<br>
> File "<stdin>", line 1, in <module><br>
> AttributeError: 'Mm' object has no attribute 'z'<br>
>>>><br>
<br>
</div></div>By the way, you can see the class definition with<br>
<br>
namedtuple("Mm", "x y", verbose=True)<br>
<div><div></div><div class="h5"><br>
Peter<br>
<br>
_______________________________________________<br>
Tutor maillist - <a href="mailto:Tutor@python.org">Tutor@python.org</a><br>
To unsubscribe or change subscription options:<br>
<a href="http://mail.python.org/mailman/listinfo/tutor" target="_blank">http://mail.python.org/mailman/listinfo/tutor</a><br>
</div></div></blockquote></div><br></div>