Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue14617] confusing docs with regard to __hash__

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Apr 18, 2012, 10:55 AM

Post #1 of 7 (44 views)
Permalink
[issue14617] confusing docs with regard to __hash__

New submission from Ethan Furman <ethan [at] stoneleaf>:

>From http://docs.python.org/py3k/reference/datamodel.html#object.__hash__
-----------------------------------------------------------------------
Classes which inherit a __hash__() method from a parent class but change the meaning of __eq__() such that the hash value returned is no longer appropriate (e.g. by switching to a value-based concept of equality instead of the default identity based equality) can explicitly flag themselves as being unhashable by setting __hash__ = None in the class definition. Doing so means that not only will instances of the class raise an appropriate TypeError when a program attempts to retrieve their hash value, but they will also be correctly identified as unhashable when checking isinstance(obj, collections.Hashable) (unlike classes which define their own __hash__() to explicitly raise TypeError).

If a class that overrides __eq__() needs to retain the implementation of __hash__() from a parent class, the interpreter must be told this explicitly by setting __hash__ = <ParentClass>.__hash__. Otherwise the inheritance of __hash__() will be blocked, just as if __hash__ had been explicitly set to None.
-----------------------------------------------------------------------

The first paragraph says the user has to change __hash__ if it's different because of changes to __eq__, the second paragraph says __hash__ is automatically removed if __eq__ is changed; the second paragraph reflects reality.

Proposed change:
-----------------------------------------------------------------------
Classes which change the meaning of __eq__() (thus losing automatic delegation to the parent class' __hash__) can explicitly flag themselves as being unhashable by setting __hash__ = None in the class definition (which is otherwise done implicity). Having __hash__ set to None, either explicitly or implicitly, means that not only will instances of the class raise an appropriate TypeError when a program attempts to retrieve their hash value, but they will also be correctly identified as unhashable when checking isinstance(obj, collections.Hashable) (unlike classes which define their own __hash__() to explicitly raise TypeError).

If a class that overrides __eq__() needs to retain the implementation of __hash__() from a parent class, the interpreter must be told this explicitly by setting __hash__ = <ParentClass>.__hash__.
-----------------------------------------------------------------------

Patch attached.

----------
assignee: docs [at] pytho
components: Documentation, Interpreter Core
files: __hash__.diff
keywords: patch
messages: 158644
nosy: docs [at] pytho, stoneleaf
priority: normal
severity: normal
status: open
title: confusing docs with regard to __hash__
type: behavior
versions: Python 3.1, Python 3.2, Python 3.3
Added file: http://bugs.python.org/file25261/__hash__.diff

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14617>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 18, 2012, 12:13 PM

Post #2 of 7 (45 views)
Permalink
[issue14617] confusing docs with regard to __hash__ [In reply to]

Changes by Éric Araujo <merwok [at] netwok>:


----------
versions: +Python 2.7 -Python 3.1

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14617>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 18, 2012, 12:30 PM

Post #3 of 7 (40 views)
Permalink
[issue14617] confusing docs with regard to __hash__ [In reply to]

Éric Araujo <merwok [at] netwok> added the comment:

Some of the sentence phrasing still sounds a bit awkward to me (“[...], means that not only will instances” for example, and I would also remove the parens at the end), but globally I think this is an improvement.

----------
nosy: +eric.araujo

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14617>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 18, 2012, 1:34 PM

Post #4 of 7 (40 views)
Permalink
[issue14617] confusing docs with regard to __hash__ [In reply to]

Ethan Furman <ethan [at] stoneleaf> added the comment:

Éric Araujo wrote:
> Changes by Éric Araujo <merwok [at] netwok>:
>
>
> ----------
> versions: +Python 2.7 -Python 3.1

The docs for 2.7 are correct, as __hash__ is not automatically
suppressed in that version.
--
~Ethan~

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14617>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 18, 2012, 2:01 PM

Post #5 of 7 (41 views)
Permalink
[issue14617] confusing docs with regard to __hash__ [In reply to]

Changes by Éric Araujo <merwok [at] netwok>:


----------
versions: -Python 2.7

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14617>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 18, 2012, 2:38 PM

Post #6 of 7 (36 views)
Permalink
[issue14617] confusing docs with regard to __hash__ [In reply to]

Ethan Furman <ethan [at] stoneleaf> added the comment:

More re-writing...

----------
Added file: http://bugs.python.org/file25264/__hash__2.diff

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14617>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Apr 19, 2012, 11:45 PM

Post #7 of 7 (37 views)
Permalink
[issue14617] confusing docs with regard to __hash__ [In reply to]

Changes by Arfrever Frehtes Taifersar Arahesis <Arfrever.FTA [at] GMail>:


----------
nosy: +Arfrever

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue14617>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.