Op 2004-12-18, Nick Coghlan schreef <[EMAIL PROTECTED]>: > Jp Calderone wrote: >> On Fri, 17 Dec 2004 11:21:25 -0800, Jeff Shannon <[EMAIL PROTECTED]> wrote: >> >> The correct characterization is that Python makes user-defined >> mutable classes hashable (arguably correctly so) as the default >> behavior. > > However, that is only achieved by using identity based equality by default. > If > you inherit from something which *doesn't* use identity based equality (say, > list or dict), then the hashability isn't there. So while you're quite > correct, > it doesn't affect the OP's original complaint (not being allowed to use a > list > as a dictionary key). > > The Python docs are pretty precise about what the rules are: > > "__hash__(self) > Called for the key object for dictionary operations, and by the built-in > function hash(). Should return a 32-bit integer usable as a hash value for > dictionary operations. The only required property is that objects which > compare > equal have the same hash value; it is advised to somehow mix together (e.g., > using exclusive or) the hash values for the components of the object that > also > play a part in comparison of objects. If a class does not define a __cmp__() > method it should not define a __hash__() operation either; if it defines > __cmp__() or __eq__() but not __hash__(), its instances will not be usable as > dictionary keys. If a class defines mutable objects and implements a > __cmp__() > or __eq__() method, it should not implement __hash__(), since the dictionary > implementation requires that a key's hash value is immutable (if the object's > hash value changes, it will be in the wrong hash bucket)." > > (from: http://www.python.org/dev/doc/devel/ref/customization.html) > > > The key points are: > 1. Objects which compare equal must have the same hash value > 2. An object's hash value must never change during it's lifetime > > Lists could achieve the former, but not the latter. Tuples can achieve both, > by > virtue of being immutable, with immutable contents. Ditto for sets & > frozensets.
Yes they can. If you don't mutate a list it will never change during it's lifetime. -- Antoon Pardon -- http://mail.python.org/mailman/listinfo/python-list