This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Tutorial: mutable objects may be keys
Type: Stage:
Components: Documentation Versions:
process
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: brianhv, mcherm, rhettinger
Priority: normal Keywords:

Created on 2003-08-19 18:04 by brianhv, last changed 2022-04-10 16:10 by admin. This issue is now closed.

Messages (3)
msg17838 - (view) Author: Brian Victor (brianhv) Date: 2003-08-19 18:04
http://python.org/doc/tut/node7.html#SECTION007400000000000000000

The tutorial section 5.4 ("Dictionaries") says, "Unlike
sequences, which are indexed by a range of numbers,
dictionaries are indexed by keys, which can be any
immutable type; strings and numbers can always be
keys."  This strongly implies that only immutable
objects may be keys, and I always assumed that to be
the case.  I have recently learned that is not true.

I would suggest a change along the lines of "which can
be any immutable type or mutuable types under some
circumstances," with a link to further elaboration on
what is required for a type to be hashable.
msg17839 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2003-08-23 03:35
Logged In: YES 
user_id=80475

I prefer the current wording which shifts you away perilous 
paths.  It is possible to create a hashable, mutable class, but 
you had better know what you're doing if you want to use the 
objects as dictionary keys and expect to be able to retrieve 
them later
msg17840 - (view) Author: Michael Chermside (mcherm) (Python triager) Date: 2003-08-25 12:09
Logged In: YES 
user_id=99874

I'm with Raymond on this: the tutorial is intended for 
beginners, and I'm very happy to have beginners believe that 
only immutable objects can be keys. IMHO (which could easily 
be wrong in this case), more advanced users will realize that 
the existance of __hash__ makes it POSSIBLE to make 
mutables hashable, although they may well need additional 
documentation to figure out when doing so will work and 
when it will break things. But this level of detail doesn't 
belong in the tutorial (perhaps in a FAQ someplace?).
History
Date User Action Args
2022-04-10 16:10:43adminsetgithub: 39089
2003-08-19 18:04:04brianhvcreate