Immutability and Python

Terry Reedy tjreedy at udel.edu
Mon Oct 29 14:03:57 EDT 2012


On 10/29/2012 1:05 PM, andrea crotti wrote:

> I meant how do I create new immutables classes myself, I guess that's
> possible writing C extensions but I don't see in pure Python..

If you mean class with immutable instances, mutate new instances in 
__new__ instead of __init__ and write a custom .__setattr__ that 
prevents changes thereafter.

If you want the class itself to be immutable (after creation), write a 
custom metaclass.

You may also need to think about .__getattribute__, but I never studied 
the detail completely and have forgotten what I learned.

-- 
Terry Jan Reedy




More information about the Python-list mailing list