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: function comparing lacks NotImplemented error
Type: Stage:
Components: Interpreter Core Versions: Python 3.0
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: gvanrossum Nosy List: _doublep, christian.heimes, ggenellina, gvanrossum
Priority: normal Keywords:

Created on 2007-11-05 20:40 by _doublep, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
test.py _doublep, 2007-11-05 20:40
Messages (6)
msg57135 - (view) Author: Paul Pogonyshev (_doublep) Date: 2007-11-05 20:40
I believe attached script demonstrates a bug in Python 3000.  As far as
I can tell, it should print four times 'True'.
msg57165 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-11-06 17:37
Odd. I'll look into it.
msg57237 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-11-08 13:28
Additional prints make it easy to understand what happens here:

>>> class Anything:
...     def __eq__(self, other):
...         print("eq")
...         return True
...     def __ne__(self, other):
...         print("ne")
...         return False
...
>>> x = lambda: None
>>> print(x == Anything())
False
>>> print(Anything() == x)
eq
True
>>> y = object()
>>> print(y == Anything())
eq
True
>>> print(Anything() == y)
eq
True

x == Anything() doesn't invoke Anything.__eq__(). It's using
function.__eq__().
msg57286 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2007-11-09 00:23
It's still odd though.  Why does object() == Anything() pass control to
the right hand side, while (lambda: None) == Anything() doesn't? 
There's no definition of equality in PyFunction_Type, so it would seem
to fall back on the definition in PyBaseObject_Type, which I would
expect to return False from the code in object_richcompare()...

[...gdb...]

OK, here's the solution of the mystery.  do_richcompare() ends up taking
the swapped code path for object() == Anything(), but not for function
objects.  This is because Anything() is a subclass of object, but not of
<function>.

Perhaps the solution is to change object_richcompare() to return
NotImplemented instead of returning False?  Or perhaps even remove
object_richcompare() altogether, since it doesn't do anything that
do_richcompare() doesn't know how to do...
msg59288 - (view) Author: Paul Pogonyshev (_doublep) Date: 2008-01-05 16:30
I'd like to ping this issue as I think it is important enough (core is
affected).
msg59346 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-01-06 00:10
That solution (return NotImplemented instead of False) appears to work.
Committed revision 59761.

Thanks for bringing up this regression w.r.t. 2.5!
History
Date User Action Args
2022-04-11 14:56:27adminsetgithub: 45734
2008-01-06 00:10:12gvanrossumsetstatus: open -> closed
resolution: fixed
messages: + msg59346
2008-01-05 16:30:44_doublepsetmessages: + msg59288
2007-11-20 00:33:56ggenellinasetnosy: + ggenellina
2007-11-09 00:23:01gvanrossumsetmessages: + msg57286
2007-11-08 13:28:13christian.heimessetnosy: + christian.heimes
messages: + msg57237
2007-11-06 17:37:59gvanrossumsetpriority: normal
assignee: gvanrossum
messages: + msg57165
nosy: + gvanrossum
2007-11-05 20:40:45_doublepcreate