It's an idea, but it would be disappointing to see that
(1, {}) could be used as a dictionary key (and actually work
correctly, though prehaps slowly) but {} by itself could not.
> [You could also add a "mutable" flag to dictionaries, allowing
> them to be safely used as keys -- but I don't think this would
> be worth the extra overhead (use my slower kjDicts if you want
> to hash using dictionaries).]
Now we're talking. I'm not sure that there is much overhead -- just
one flag to be tested by every mutating operation. The problem I see
is that after you've asked for the hash() of a dictionary you can
never change it again, which would surely be surprising to some
fraction of Python users. How about the following: add a
"frozen" flag to lists and dictionaries, and add an explicit "freeze"
method to them which sets the "frozen" flag. There is no way to
unfreeze an object. Only frozen lists and dictionaries can have their
hash value computed. I'm sure there are twenty volunteers who could
submit a patch.
BTW for class instances this is not needed -- classes can define
their own __hash__ as they bloody well please. Are there any other
important mutable data types that could benefit from this?
> -- Quick, Robin, catch it in your teeth! [Throws batarang.]
> -- [Later, massaging jaw] Holy molars, Batman! That was close!
> -- Yes, Robin, I geuss all those years of good dental hygiene
> really paid off. --from _Batman_ the TV show.
(-: ROTFL :-)
--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>