The future of Python immutability

Graham Breed x31eq at cnntp.org
Mon Sep 7 11:58:06 CEST 2009


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.

<snip>

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

Nothing to do with threading, but it's also the cause of 
this weirdness:

http://bytes.com/topic/python/answers/752154-list-tuple

 >>a = ([1], 2)
 >>a[0] += [3]

succeeds, but raises an error.


                     Graham



More information about the Python-list mailing list