Making immutable instances

Mike Meyer mwm at mired.org
Thu Nov 24 03:47:47 EST 2005


"Giovanni Bajo" <noway at sorry.com> writes:
> Mike Meyer wrote:
>> Note that this property of __slots__ is an implementation detail. You
>> can't rely on it working in the future.
> I don't "rely" on it. I just want to catch bugs in my code.

I certainly hope you're not relying on it to catch bugs. You should do
proper testing instead. Not only will that catch pretty much all the
bugs you mention later - thus resolving you of the need to handcuff
clients of your class - it will catch lots of other bugs as well.

>> I'm curious as to why you care if people add attributes to your
>> "immutable" class. Personally, I consider that instances of types
>> don't let me add attributes to be a wart.
> To catch stupid bugs, typos and whatnot. If an instance is immutable, you can't
> modify it, period. If you do it, it's a bug. So why not have the bug raises an
> exception, rather than go unnoticed?

It's only a bug if you didn't mean to do it. If you meant to do it,
you're taking advantage of a powerful feature of Python. If you feel
the need to restrict the use of such features, maybe you should
consider a less powerful language? There are lots of languages around
that prevent accidental creation of attributes - and lots of other
similar usages that are bugs if you don't mean to do them.

> I don't see your point, either. Why would you want to add attributes to an
> object documented to be immutable?

The documentation is immaterial. The *need* is what's important. I've
had use cases were some object was almost exactly what I wanted,
except I needed another bit of data dragged along. Python lets me do
that for most classes, which is one of the things that I like about it
- it doesn't handcuff me.

> >> If it's not a wart, why would it be a wart for user-defined types to
> >> have the same behaviour?
> >
> > It's a wart because user-defined classes *don't* have the same
> > behavior.
> Then *my* solution for this would be to give user-defined classes a way to
> behave like builtins, eg. explicitally and fully implement immutability.

Well, if you want to propose a change to the language, you need a good
use case to demonstrate the benefits of such a change. Do you have
such a use case? Catching bugs doesn't qualify, otherwise Python would
be radically different from what it is.

> Immutability is an important concept in Python programs, and I'm impressed it
> does not have explicit support.

I'm not convinced that immutability is that important a concept. Yeah,
you have to know about it, but it seems more like an implementation
detail than a crucial concept. I'm not sure it's more important than
things like interned strings and the sharing of small integers.  Most
of the discussion of immutables here seems to be caused by newcomers
wanting to copy an idiom from another language which doesn't have
immutable variables. Their real problem is usually with binding, not
immutability.

        <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list