New docs for set elements/dictionary keys

Christoph Zwerschke cito at online.de
Sun Nov 27 22:22:15 EST 2005


Mike Meyer wrote:

> Any object for which hash() returns an appropriate value(1) can be
> used as a dictionary key/set element. Lists, sets and dicts are not
> hashable, and can not be used. Tuples can be used if all the things
> they contain are hashable. instances of all other builin types can be
> used. Instances of most classes written in Python can be used(2).
> 
> 1) See the __hash__ documentation for details on what an approriate
> value is.
> 
> 2) Instances that have a __hash__ method that returns an appropriate
> value can be used. Instances that don't have a __cmp__ or an __eq__
> method can be used even if they don't have a __hash__ method.

I think that is not so bad. How about this simplification:

Any hashable object(1) can be used as a dictionary key/set element. 
Lists, sets and dicts are not hashable, and can not be used. Tuples can 
be used if all the things they contain are hashable. Instances of all 
other built-in types and most user-defined classes are hashable.

(1) Objects for which the hash() function returns an appropriate 
(proper?) value. See the __hash__ documentation for details.

-- Christoph





More information about the Python-list mailing list