[Python-Dev] Python semantic: Is it ok to replace not x == y with x != y? (no)
Franklin? Lee
leewangzhong+python at gmail.com
Tue Dec 15 08:45:55 EST 2015
On Tue, Dec 15, 2015 at 8:04 AM, Victor Stinner
<victor.stinner at gmail.com> wrote:
> Is it expected that "not x.__eq__(y)" can be different than
> "x.__ne__(y)"? Is it part of the Python semantic?
In Numpy, `x != y` returns an array of bools, while `not x == y`
creates an array of bools and then tries to convert it to a bool,
which fails, because a non-singleton Numpy array is not allowed to be
converted to a bool. But in the context of `if`, both `not x == y` and
`x != y` will fail.
>From the docs, on implementing comparison:
https://docs.python.org/3/reference/datamodel.html#object.__ne__
"""
By default, __ne__() delegates to __eq__() and inverts the result
unless it is NotImplemented. There are no other implied relationships
among the comparison operators, for example, the truth of (x<y or
x==y) does not imply x<=y.
"""
More information about the Python-Dev
mailing list