The unPEP: List-Tuple Unification

Stephen Horne steve at lurking.demon.co.uk
Fri Jul 20 17:11:10 EDT 2001


On Fri, 20 Jul 2001 15:31:44 -0400, Raymond Hettinger
<othello at javanet.com> wrote:

>Steve Holden wrote:
>
>> "I don't
>> see why immutability has to be a one-way attribute when converiosn between
>> list and tuple is so easy.

>Dictionary's rely on immutability of keys.  If the list is used as a key, then
>it cannot be subsequently assigned to without breaking the key reference.
>
>Also, immutability can be an important internal optimization. Once made
>immutable, the list can be fixed length, stored as an array, and hashed
>once and for all.
>
>tuples-who-needs-them-ly, yours Raymond

I'd have...

  <object>.lock ()           Lock any object in place
  locked (<object>)        Return immutable copy of object
  unlocked (<object>)    Return mutable copy of object
  <object>.is_locked      Return bool - is object immutable

locked and unlocked would only change the mutability - all other
properties would remain the same.

unlocked (locked (x)) == x - 'locked' mustl necessarily lock all
nested objects, but they should be marked differently so that
'unlocked' can unlock only the nested objects that were previously
unlocked.

I don't like the idea of having an immutable attribute as such - I'd
rather the actual implementation was separate. But I don't have a good
reason.

Just a thought ;-)




More information about the Python-list mailing list