[Python-Dev] Making mutable objects readonly

Greg Ewing greg@cosc.canterbury.ac.nz
Thu, 01 Feb 2001 12:34:50 +1300 (NZDT)


Skip Montanaro <skip@mojam.com>:

> Can someone give me an example where this is actually useful and
> can't be handled through some existing mechanism?

I can envisage cases where you want to build a data structure
incrementally, and then treat it as immutable so you can use it as a
dict key, etc. There's currently no way to do that to a list
without copying it.

So, it could be handy to have a way of turning a list into a tuple
in-place. It would have to be a one-way transformation, otherwise
you could start using it as a dict key, make it mutable again, and
cause havoc.

Suggested implementation: When you allocate the space for the values
of a list, leave enough room for the PyObject_HEAD of a tuple at the
beginning. Then you can turn that memory block into a real tuple
later, and flag the original list object as immutable so you can't
change it later via that route.

Hmmm, would waste a bit of space for each list object. Maybe this
should be a special list-about-to-become-tuple type. (Tist?
Luple?)

Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg@cosc.canterbury.ac.nz	   +--------------------------------------+