Making immutable instances

Alex Martelli aleax at mail.comcast.net
Wed Nov 23 23:49:52 EST 2005


Ben Finney <bignose+hates-spam at benfinney.id.au> wrote:
   ...
> > 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?

A type implemented in C offers different possibilities than one
implemented in Python -- no deep conceptual reason, just practical ones.

A substantial fraction of the time, people asking "how do I stop the
users of my code from doing X" proceed by screaming "but they could
still do X if they hopped on their left foot in a moonless midnight
while sprinkling bat's blood!" as a response to any suggestion.  So,
when you ask "how do I stop the users of my code from doing X" without
qualification, you're quite likely to get such disclaimers.  If you
don't want them, learn to ask about stopping your users from
ACCIDENTALLY doing X, and no reasonable respondant will fail to notice
the qualification.

 
> 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?

There was nothing unreasonable in your original request, either; and I
don't think that making it explicit that you intended to document the
restriction would have changed my answer (although an explicit
acknowlegment that you're looking for restrictions against accidental
misuse rather than against determined attackers surely would).


Alex



More information about the Python-list mailing list