[Python-Dev] Re: Making mutable objects readonly
Eric S. Raymond
Mon, 29 Jan 2001 21:49:59 -0500
Guido van Rossum <email@example.com>:
> Yes, this is a good thing. Easy to do on lists and dicts. Questions:
> - How to spell it? x.freeze()? x.readonly()?
I like "freeze", it'a a clear imperative where "readonly()" sounds
like a test (e.g. "is this readonly()?")
> - Should we support something like this for instances too? Sometimes
> it might be cool to be able to freeze changing attribute values...
Moshe Zadka sent me a hack that handles instances:
> class MarkableAsConstant:
> def __init__(self):
> def __setattr__(self, name, value):
> if self._writable:
> self.__dict__[name] = value
> raise ValueError, "object is read only"
> def mark_writable(self):
> self.__dict__['_writable'] = 1
> def mark_readonly(self):
> self.__dict__['_writable'] = 0
> - Should this reversible? I.e. should there be an x.unfreeze()?
I gave this some thought earlier today. There are advantages to either
way. Making freeze a one-way operation would make it possible to use
freezing to get certain kinds of security and integrity guarantees that
you can't have if freezing is reversible.
Fortunately, there's a semantics that captures both. If we allow
freeze to take an optional key argument, and require that an unfreeze
call must supply the same key or fail, we get both worlds. We can
even one-way-hash the keys so they don't have to be stored in the
Want to lock a structure permanently? Pick a random long key. Freeze
with it. Then throw that key away...
<a href="http://www.tuxedo.org/~esr/">Eric S. Raymond</a>
Strict gun laws are about as effective as strict drug laws...It pains
me to say this, but the NRA seems to be right: The cities and states
that have the toughest gun laws have the most murder and mayhem.
-- Mike Royko, Chicago Tribune