Issue 37110: Clarify hashability of custom class instances (original) (raw)
Issue37110
Created on 2019-05-31 09:07 by cito, last changed 2022-04-11 14:59 by admin. This issue is now closed.
Messages (3) | ||
---|---|---|
msg344042 - (view) | Author: Christoph Zwerschke (cito) * | Date: 2019-05-31 09:07 |
The Python documentation says about hashability in the glossary (https://docs.python.org/3/glossary.html#term-hashable): "Objects which are instances of user-defined classes are hashable by default." This is not quite true. Objects of a user-defined class with an __eq__ method are not hashable. Maybe it would be better to make this more explicit: "Objects which are instances of user_defined classes without custom __eq__ and __hash__ methods are hashable by default." | ||
msg344138 - (view) | Author: Raymond Hettinger (rhettinger) * ![]() |
Date: 2019-05-31 23:00 |
The docs look correct to me: >>> class A: pass >>> hash(A()) 274859987 User defined classes are in-fact hashable by default. Other methods can be defined to change hashability, but they are not the default. FWIW, it isn't the purpose of the glossary to be a language spec; rather, it is to provide a rough meaning of what the word "hashable" means. Already, the wording has exceeded its original intent. The correct place for a more detailed specification in the language reference for object.__hash__(): https://docs.python.org/3/reference/datamodel.html?highlight=__hash__#object.__hash__ Thank you for the suggestion, but we'll pass on this one. | ||
msg344139 - (view) | Author: Christoph Zwerschke (cito) * | Date: 2019-05-31 23:25 |
My point was that it's not immediately obvious what "by default" means and that hashability is not only affected by the __hash__ method but also by __eq__. But I agree, you can argue that "by default" already includes not adding any special methods like __eq__ and the glossary should not become too verbose. (I remember these words from Donald Knuth in one of his books: "In the interest of conciseness, you need to indulge in simplifications that are really little lies; these should be overlooked.") |
History | |||
---|---|---|---|
Date | User | Action | Args |
2022-04-11 14:59:16 | admin | set | github: 81291 |
2019-05-31 23:25:28 | cito | set | messages: + |
2019-05-31 23:00:39 | rhettinger | set | status: open -> closedassignee: docs@python -> rhettingernosy: + rhettingermessages: + resolution: rejectedstage: resolved |
2019-05-31 09:07:25 | cito | create |