
[Ping]
Is a frozen list hashable?
[Guido]
Yes -- that's what started this thread (using dicts as dict keys, actually).
Except this doesn't actually work unless list.freeze() recursively ensures that all elements in the list are frozen too:
hash((1, 2)) 219750523 hash((1, [2])) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unhashable type
That bothered me in Eric's original suggestion: unless x.freeze() does a traversal of all objects reachable from x, it doesn't actually make x safe against modification (except at the very topmost level). But doing such a traversal isn't what *everyone* would want either (as with "const" in C, I expect the primary benefit would be the chance to spend countless hours worming around it in both directions <wink>). [Skip]
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"
marshal.dumps(dict) isn't canonical, though. That is, it may well be that d1 == d2 but dumps(d1) != dumps(d2). Even materializing dict.values(), then sorting it, then marshaling *that* isn't enough; e.g., consider {1: 1} and {1: 1L}. The latter example applies to marshaling lists too.