New docs for set elements/dictionary keys

Mike Meyer mwm at mired.org
Mon Nov 28 00:43:19 EST 2005


Christoph Zwerschke <cito at online.de> writes:
> 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.

I think that will do quite nicely, as the information about __hash__,
__cmp__ and __eq__ are all in the __hash__ documentation. You wrote it
- why don't you submit a PR with it?

   <nmike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list