On Thu, May 14, 2009 at 9:31 PM, Curt Hagenlocher
I think this takes the discussion in a more practical direction. Imagine that there were a special method name __immutable__ to be implemented appropriately by all builtin types.
Python already has something *vaguely* like this; __hash__ is only supposed to be implemented on immutable objects. So if the object supports __hash__ one could declare that it is supposed to be immutable. However, it's occasionally useful to define __hash__ on mutable objects, and indeed it's defined by default on user-defined classes. So __hash__ isn't a viable substitute for __immutable__ (&c). Chris Rebert wrote:
(A) Python's new Abstract Base Classes would probably be a better way of doing such checking rather than introducing a new special method
FWIW, I'm +0.3 for either __immutable__ or an ABC to express the concept. I don't know which would be the "right" way to do it--ultimately I defer to my betters.
(B) What about having an __immutable__() that returned an immutable version of the object if possible?
The "freeze" protocol was proposed in PEP 351. http://www.python.org/dev/peps/pep-0351/ It was rejected in 2006. It's a "can of worms". Reading PEPs is fun, /larry/