Help understanding the decisions *behind* python? - immutable objects

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Jul 26 18:58:34 EDT 2009


On Sun, 26 Jul 2009 11:24:48 -0700, John Nagle wrote:

>      An interesting issue is Python objects, which are always mutable.
> A "dict" of Python objects is allowed, but doesn't consider the contents
> of the objects, just their identity (address). Only built-in types are
> immutable; one cannot create a class of immutable objects.

Yes you can, for some definition of "can":

http://northernplanets.blogspot.com/2007/01/immutable-instances-in-python.html


Admittedly pure Python objects are only "cooperatively immutable". The 
immutability relies on the caller not going out of its way to break the 
instance, so you can mutate it if you work at it. One could, I suppose, 
try putting in complicated tricks to prevent that, but anything written 
in Python can ultimately be modified in Python.



-- 
Steven



More information about the Python-list mailing list