The future of Python immutability
nagle at animats.com
Mon Sep 7 20:26:02 CEST 2009
Graham Breed wrote:
> John Nagle wrote:
>> In the beginning, strings, tuples, and numbers were immutable, and
>> everything else was mutable. That was simple enough. But over time,
>> Python has acquired more immutable types - immutable sets and immutable
>> byte arrays. Each of these is a special case.
>> Immutability is interesting for threaded programs, because
>> immutable objects can be shared without risk. Consider a programming
>> model where objects shared between threads must be either immutable or
>> "synchronized" in the sense that Java uses the term. Such programs
>> are free of most race conditions, without much programmer effort to
>> make them so.
> Of course, tuples would still be a special case because they may contain
> mutable objects. You need to check they're immutable all the way down.
Right. Tracking mutablity and ownership all the way down without
making the language either restrictive or slow is tough.
In multi-thread programs, though, somebody has to be clear on who owns
what. I'm trying to figure out a way for the language, rather than the
programmer, to do that job. It's a major source of trouble in threaded
More information about the Python-list