Roy Smith wrote:
Jeff Shannon <[EMAIL PROTECTED]> wrote:
The aesthetic purity I'm referring to is that Python respects the proper meaning of hashing, even if it doesn't force the programmer to. The builtin objects that Python provides don't offer a __hash__() method that fails to meet the mathematical prerequisites of a proper hash function -- that is, objects that are equal will hash identically. In addition, dictionaries expect keys to have proper hashing semantics, such that a given object's hash value will not change over its lifetime. It is not possible for a mutable object to satisfy both aspects of proper hashing behavior, therefore Python does not pretend that mutable objects are hashable.
If you look back over this thread, I've given several examples of mutable objects which meet your requirements:
* Objects that are equal hash the same. * Hash value does not change over the lifetime of the object.
All that's needed is to define __hash__() and __cmp__() methods which
only look at some subset of the object's data atrributes. You can
keep those attributes constant (perhaps enforced with __setattr__()
hooks) and let others mutate.
In which case, one could argue that your __cmp__() is not fulfilling the semantics of mathematical equality, because even though the objects compare the same, they are not interchangeable. (They may be "close enough to equal for this particular purpose", but that's a different thing regardless of whether or not it's useful in practice.)
Once again, you can fudge the definitions (possibly even to beneficial effect), but you can't fulfill all of them. :)
I agree that saying "dictionary keys must be immutable" is a
reasonable simplification which works fine the vast majority of the
time. I would not, however, go as far as saying, "It is not possible
for a mutable object to satisfy both aspects of proper hashing
behavior".
It is possible to create mutable objects for which one can arrange things so that they can act like they're hashable. Unless I'm completely mistaken about the mathematical principles involved, however, then the mathematics implied by the terms "hash function" and "equality" cannot be universally fulfilled by mutable objects.
(Of course, this *is* mathematics, and not engineering. It's also mathematically impossible to prove that any generic computer program will halt, and yet every day we demonstrate programs that halt reliably.)
I still say that Python does the right thing by sticking as much as possible to the mathematical meanings, while leaving users the capability to bend those rules if they're determined. Bending rules in the right cases is essential; decrying (or denying) the very existence of those rules is pointless.
Jeff Shannon Technician/Programmer Credit International
-- http://mail.python.org/mailman/listinfo/python-list