Making immutable instances

Mike Meyer mwm at
Fri Dec 2 05:38:47 CET 2005

bonono at writes:
> Mike Meyer wrote:
>> > By design, this is a "don't use" feature so it would be very hard to
>> > find a "use case" ;-)
>> But I can think of use cases for instances with no mutable attributes,
>> which is another "don't use" case. If I can do that, those proposing
>> that instances ought to be immutable should be able to come up with a
>> use case.
> Lose you, can you clarify ?

There are lots of use cases where you want your attributes to be
immutable. Rationals, enums, anything where you want your instance to
contain some used as a "value", or usable as a dictionary key that
might equal other instances, or - well, you should get the idea.

>> This is a problem with OO in general, not with not having immutable
>> instances. You get the same problem if, instead of attaching
>> attributes to your instances, I subclass your class and add the
>> attribute in the subclass (which I can do even if both my instances
>> and yours are immutable). In short, immutable instances don't solve
>> your problem, so this doesn't work as a use case.
> um, that could be true(already forgot if I can do this in say C++). How
> would I solve this kind of issue then, just curious.

B&D languages have facilities for doing this kind of thing. Alex
Martelli has mentioned "final" classes in Java. C++ and Eiffel let you
declare an attribute as not modifiable by subclasses. Eiffel requires
that subclasses that override attributes (or methods, for that matter)
do so in a type-compatable manner, so you'd probably get a compile
error in your subclass in this case. There are certainly methods I've
never heard of as well. In Python, you can do things that try and
enforce this (I don't know how, because it's not something I think is
reasonable to want to do), but the language is also powerful enough
that I'd be surprised if they couldn't be defeated in some manner.

Mike Meyer <mwm at>
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

More information about the Python-list mailing list