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: Object defines '__ne__' as 'not __eq__' if '__ne__' is not implemented
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: works for me
Dependencies: Superseder:
Assigned To: Nosy List: ethan.furman, josh.r, magu
Priority: normal Keywords:

Created on 2016-03-08 21:55 by magu, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg261385 - (view) Author: Marc Guetg (magu) Date: 2016-03-08 21:55
I propose to change __ne__ of `object` in the following form:

	class NewObject(object):
		def __ne__(self, other):
			return not self.__eq__(other)

Currently overwriting the `__eq__` method requires also overwriting `__ne__`. In a vast majority of cases this results in some boilerplate code as:

    (a == b) ^ (a != b) == True

to reduce surprises. Changing the default behavior still allows for the limited number of use cases where we want to implement __ne__ differently.


In short I propose the same behavior than __str__ and __repr__ have for __eq__ and __ne__.
(https://docs.python.org/3/reference/datamodel.html#object.__str__)
msg261386 - (view) Author: Ethan Furman (ethan.furman) * (Python committer) Date: 2016-03-08 22:10
The following behaviour is from 3.5:

--> class Huh:
...   def __eq__(self, other):
...     return other == 'blah'
... 
--> h = Huh()
--> h == 'ew'
False
--> h != 'ew'
True
--> h == 'blah'
True
--> h != 'blah'
False

Which seems to be exactly what you want.  Do you have a counter-example?
msg261387 - (view) Author: Josh Rosenberg (josh.r) * (Python triager) Date: 2016-03-08 22:35
It's not exactly what was asked for, but it's actually better (in that the __ne__ default implementation handles NotImplemented correctly). Per the docs at https://docs.python.org/3/reference/datamodel.html#object.__eq__ :

"By default, __ne__() delegates to __eq__() and inverts the result unless it is NotImplemented."

This feature was never backported to 2.7, but it's existed in one form or another for the entire 3.x line.
History
Date User Action Args
2022-04-11 14:58:28adminsetgithub: 70701
2016-03-08 23:54:15eryksunsetstatus: open -> closed
resolution: works for me
stage: resolved
2016-03-08 22:35:44josh.rsetnosy: + josh.r
messages: + msg261387
2016-03-08 22:10:09ethan.furmansetnosy: + ethan.furman
messages: + msg261386
2016-03-08 21:55:55magucreate