-
-
Notifications
You must be signed in to change notification settings - Fork 31.6k
Document auto __ne__ generation; provide a use case for non-trivial __ne__ #48645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
3.0c3 doc (Basic customization) says In http://mail.python.org/pipermail/python-ideas/2008-October/002235.html And indeed, as Arnaud Delobelle posted on python-list a, b, c = A(1), A(1), A(2) Suggested revision: |
It would be really useful to explain, right in this section, why __ne__ <doctext> The values most commonly returned by the rich comparison methods are For instance, if equality testing of instances of some class is Python allows the programmer to define __ne__ separately from __eq__ for </doctext> Now that I write this, I see a use for magic __logical_or__, Is it too late to request these for Python 3.0? |
The current paragraph Please, let us replace it now, for 3.1 release, with the correct |
One other thought: The __ne__ method follows automatically from __eq__ |
The situation appears to be at least slightly different from what Guido >>> class A:
def __eq__(s,p): return 1
>>> id(object.__ne__)
10703216
>>> id(A.__ne__)
10703216 No new A.__ne__ added. But >>> c,d=object(),object()
>>> c==d
False
>>> c!=d
True
>>> a,b = A(),A()
>>> a==b
1
>>> a!=b
False So it seems that a!=b *is* evaluated as not a==b rather than as "There is one implied relationship among comparison operators: defining I am a bit puzzled though. In |
The implementation you are looking for is in object_richcompare, in
inherit, provides a single "rich comparison" method to which all of the
|
It would also be useful to point out that there is a shortcut in the |
Issue bpo-17151 closed as a duplicate of this one. |
Here is a patch that documents the default object.__ne__() implementation. It also documents the subclass priority rules for the reflected comparison methods, which is raised in bpo-22052. I have made some more tests to verify the relationships exists from __ne__ to __eq__, but no other relationships exist for the other methods. I will include it in my patch in bpo-21408 to avoid the patches conflicting with each other. |
While Martin's patch doesn't cover all the vagaries of comparison operations discussed above, it fixes the outright error, and provides an appropriate cross-reference to functools.total_ordering. |
The reference to @functools.total_ordering was actually already there; I just moved it into the paragraph about relationships between the operators. I should also point out that my description of the default __ne__() assumes that bpo-21408 is resolved; the current behaviour is slightly different. If you think something else could be added to the patch, I’m happy to try and add it. Perhaps the default object.__eq__() behaviour? |
Adding a new patch that just fixes the typo error in the first patch |
See also bpo-23326. |
bpo-21408 has been committed to 3.4 and 3.5 branches, so my patch can now be considered to document the newly fixed behaviour. |
Nick seemed to approve of this, so perhaps it is ready to commit? The new patch just resolves a minor conflict with the current code. |
Added comments on Rietveld. |
This updated patch adds the clarification about NotImplemented. |
LGTM. |
New changeset f5069e6e4229 by Robert Collins in branch '3.4': New changeset b9a0165a3de8 by Robert Collins in branch '3.5': New changeset e56893df8e76 by Robert Collins in branch 'default': |
Thanks for the patch; applied to 3.4 and up. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: