[Python-Dev] Documentation Error for hash (original) (raw)

Michael Foord fuzzyman at voidspace.org.uk
Thu Aug 28 21:31:16 CEST 2008


Hello all,

The documentation for hash seems to be outdated. I'm happy to submit a patch, so long as I am not misunderstanding something.

http://docs.python.org/dev/reference/datamodel.html#object.hash

The documentation states:

If a class does not define a cmp() or eq() 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).

This may have been true for old style classes, but as new style classes inherit a default hash from object - mutable objects will be usable as dictionary keys (hashed on identity) unless they implement a hash method that raises a type error.

Shouldn't the advice be that classes that implement comparison methods should always implement hash (wasn't this nearly enforced?), and that mutable objects should raise a TypeError in hash.

Additionally the following documentation states that reversed is new in Python 2.6 and I think it was actually new in Python 2.4 (it certainly works for Python 2.5 and IronPython 1 which targets 2.4...).

http://docs.python.org/dev/reference/datamodel.html#object.reversed

Michael Foord

-- http://www.ironpythoninaction.com/ http://www.voidspace.org.uk/ http://www.trypython.org/ http://www.ironpython.info/ http://www.theotherdelia.co.uk/ http://www.resolverhacks.net/



More information about the Python-Dev mailing list