Title: Odd code fragment in ABC definitions
Type: Stage:
Components: Versions: Python 3.0, Python 2.6
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: amaury.forgeotdarc, gvanrossum, rhettinger
Priority: normal Keywords:

Created on 2008-07-02 23:06 by rhettinger, last changed 2008-07-02 23:32 by gvanrossum. This issue is now closed.

Messages (4)
msg69140 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-07-02 23:06
In the Hashable ABC, there is a peculiar code fragment:

                if "__hash__" in B.__dict__:
                    if B.__dict__["__hash__"]:
                        return True

When would the innermost if-statement ever be False?  Is there a reason 
to define __hash__ to be something that evaluates to False?
msg69143 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2008-07-02 23:10
This is precisely under discussion in issue2235:
if a base class is hashable, a derived class may set __hash__ to None,
and disallow hashing.
msg69145 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2008-07-02 23:24
I don't think we should build in explicit support for bad designs that 
violate the Liskov substitution principle. Are there any valid use 
cases for wanting non-hashable subclasses of hashable classes?

If for some reason, this feature survives, it would be better to use 
NotImplemented instead of None.
msg69147 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-07-02 23:32
There are tons of ways to violate Liskov in Python. Liskov is not always
the right rule.

NotImplemented is not appropriate -- it is only used as a magic return
value from binary functions.
Date User Action Args
2008-07-02 23:32:18gvanrossumsetstatus: open -> closed
resolution: rejected
messages: + msg69147
2008-07-02 23:24:04rhettingersetmessages: + msg69145
versions: + Python 2.6, - Python 2.5
2008-07-02 23:10:36amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg69143
2008-07-02 23:06:53rhettingercreate