[Python-Dev] Making mutable objects readonly

Skip Montanaro skip@mojam.com (Skip Montanaro)
Wed, 31 Jan 2001 10:11:19 -0600 (CST)

What stimulated this thread about making mutable objects (temporarily)
immutable?  Can someone give me an example where this is actually useful and
can't be handled through some existing mechanism?  I'm definitely with
Fredrik on this one.  Sounds like madness to me.

I'm just guessing here, but since the most common need for immutable objects
is a dictionary keys, I can envision having to test the lock state of a list
or dict that someone wants to use as a key everywhere you would normally
call has_key:

    if l.islocked() and d.has_key(l):

If you want immutable dicts or lists in order to use them as dictionary
keys, just serialize them first:

    survey_says = {"spam": 14, "eggs": 42}
    sl = marshal.dumps(survey_says)
    dict[sl] = "spam"

Here's another pitfall I can envision.

    survey_says = {"spam": 14, "eggs": 42}
    dict[survey_says] = "Richard Dawson"

At this point can I safely iterate over the keys in the dictionary or not?