How to make an immutable instance

Aahz aahz at pythoncraft.com
Fri Jun 18 01:58:41 CEST 2004


In article <2jem4iF10udstU1 at uni-berlin.de>,
Leif K-Brooks  <eurleif at ecritters.biz> wrote:
>Batista, Facundo wrote:
>>
>> So, if you use this "immutable" class in a dict, and then you (on purpose)
>> modify it, you'll have different hashes.
>> 
>> Said that, how safer is this approach? Is there a better way?
>
>  [...]
>
>But note that you can still use object.__setattr__ directly to get 
>around it. I don't think there's a way to get true immutability in pure 
>Python.

Correct.  Remember that Python is a language for consenting adults; the
only way to guarantee object immutability is to create a type in C.
-- 
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Typing is cheap.  Thinking is expensive."  --Roy Smith, c.l.py



More information about the Python-list mailing list