Proposal: === and !=== operators
Robert Kern
robert.kern at gmail.com
Wed Jul 9 11:27:42 EDT 2014
On 2014-07-09 08:00, Steven D'Aprano wrote:
> At the moment, Python has two (in)equality operators, == and != which
> call __eq__ and __ne__ methods. Some problems with those:
>
>
> * Many people expect == to always be reflexive (that is, x == x for
> every x) but classes which customise __eq__ may not be.
>
> * The == operator requires __eq__ to return True or False
> (or NotImplemented) and raises TypeError if it doesn't, which
> makes it impossible to use == with (say) three-valued or fuzzy
> logic.
No, it doesn't. It can return anything.
[~]
|1> x = np.arange(5)
[~]
|2> x == 3
array([False, False, False, True, False], dtype=bool)
You can blame Numeric/numpy for that feature getting in. :-)
Now certainly, many uses of __eq__, like containment comparisons, do assume that
the result is a bool(able).
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
More information about the Python-list
mailing list