Making immutable instances

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Nov 23 23:39:02 EST 2005


Alex Martelli <aleax at mail.comcast.net> wrote:
> Ben Finney <bignose+hates-spam at benfinney.id.au> wrote:
> > How can a (user-defined) class ensure that its instances are
> > immutable, like an int or a tuple, without inheriting from those
> > types?
> 
> [...]
> Of course, these restrictions can be easily worked around by a
> sufficiently determined attacker... but if you have to think of the
> user of your code as an attacker, you've got worse problems than
> this trifling one.

I've probably stumbled across something that people often want to do
for ill-considered reasons; I don't really understand the reaction I'm
getting.

Why is "I want to make objects immutable" seen as "I don't trust my
users"? Are Python's existing immutable types also seen the same way?
If not, why the distinction?

For context, I'm intending for the immutability of class instances to
be a properly documented part of the class protocol. Does that sound
more reasonable?

-- 
 \          "In general my children refuse to eat anything that hasn't |
  `\                           danced on television."  -- Erma Bombeck |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list