@@ -290,10 +290,17 @@ \section{The standard type hierarchy} \label{types}
290290\begin {description }
291291
292292\item [Dictionaries]
293- These represent finite sets of objects indexed by strings.
293+ These represent finite sets of objects indexed by almost arbitrary
294+ values. The only types of values not acceptable as keys are values
295+ containing lists or dictionaries or other mutable types that are
296+ compared by value rather than by object identity --- the reason being
297+ that the implementation requires that a key's hash value be constant.
298+ Numeric types used for keys obey the normal rules for numeric
299+ comparison: if two numbers compare equal (e.g. 1 and 1.0) then they
300+ can be used interchangeably to index the same dictionary entry.
301+
294302Dictionaries are mutable; they are created by the \verb \{...} \
295- notation (see section \ref {dict }). (Implementation note: the strings
296- used for indexing must not contain null bytes.)
303+ notation (see section \ref {dict }).
297304\obindex {dictionary}
298305\obindex {mutable}
299306
@@ -409,7 +416,7 @@ \section{The standard type hierarchy} \label{types}
409416\obindex {instance}
410417\indexii {class object}{call}
411418\index {container}
412- \index {dictionary}
419+ \obindex {dictionary}
413420\indexii {class}{attribute}
414421
415422Class attribute assignments update the class's dictionary, never the
@@ -589,12 +596,30 @@ \subsection{Special methods for any type}
589596Called by the \verb \print \ statement and conversions (reverse quotes) to
590597compute the string representation of an object.
591598
592- \item [\tt _cmp__ (self, other)]
599+ \item [\tt __cmp__ (self, other)]
593600Called by all comparison operations. Should return -1 if
594601\verb \self < other \, 0 if \verb \self == other \, +1 if
595- \verb \self > other \. (Implementation note: due to limitations in the
596- interpreter, exceptions raised by comparisons are ignored, and the
597- objects will be considered equal in this case.)
602+ \verb \self > other \. If no \code {__cmp__} operation is defined, class
603+ instances are compared by object identity (`` address'' ).
604+ (Implementation note: due to limitations in the interpreter,
605+ exceptions raised by comparisons are ignored, and the objects will be
606+ considered equal in this case.)
607+
608+ \item [\tt __hash__(self)]
609+ Called by dictionary operations and by the built-in function
610+ \code {hash()}. Should return a 32-bit integer usable as a hash value
611+ for dictionary operations. The only required property is that objects
612+ which compare equal have the same hash value; it is advised to somehow
613+ mix together (e.g. using exclusing or) the hash values for the
614+ components of the object that also play a part in comparison of
615+ objects. If a class does not define a \code {__cmp__} method it should
616+ not define a \code {__hash__} operation either; if it defines
617+ \code {__cmp__} but not \code {__hash__} its instances will not be
618+ usable as dictionary keys. If a class defines mutable objects and
619+ implements a \code {__cmp__} method it should not implement
620+ \code {__hash__}, since the dictionary implementation assumes that a
621+ key's hash value is a constant.
622+ \obindex {dictionary}
598623
599624\end {description }
600625
0 commit comments