[Python-Dev] PyObject_RichCompareBool identity shortcut
Robert Kern
robert.kern at gmail.com
Thu Apr 28 06:25:03 CEST 2011
On 2011-04-27 23:01 , Guido van Rossum wrote:
> On Wed, Apr 27, 2011 at 8:42 PM, Robert Kern<robert.kern at gmail.com> wrote:
>> But for dtype=float arrays (which contain C doubles, not Python objects) we
>> use C semantics. Literally, we use whatever C's == operator gives us for the
>> two double values. Since there is no concept of identity for this case,
>> there is no cognate behavior of Python to match.
>>
>> [~]
>> |10> b = np.array([1.0, 2.0, nan], dtype=float)
>>
>> [~]
>> |11> b == nan
>> array([False, False, False], dtype=bool)
>>
>> [~]
>> |12> nan in b
>> False
>
> And I wouldn't want to change that. It sounds like NumPy wouldn't be
> much affected if we were to change this (which I'm not saying we
> would).
Well, I didn't say that. If Python changed its behavior for (float('nan') ==
float('nan')), we'd have to seriously consider some changes. We do like to keep
*some* amount of correspondence with Python semantics. In particular, we like
our scalar types that match Python types to work as close to the Python type as
possible. We have the np.float64 type, which represents a C double scalar and
corresponds to a Python float. It is used when a single item is indexed out of a
float64 array. We even subclass from the Python float type to help working with
libraries that may not know about numpy:
[~]
|5> import numpy as np
[~]
|6> nan = np.array([1.0, 2.0, float('nan')])[2]
[~]
|7> nan == nan
False
[~]
|8> type(nan)
numpy.float64
[~]
|9> type(nan).mro()
[numpy.float64,
numpy.floating,
numpy.inexact,
numpy.number,
numpy.generic,
float,
object]
If the Python float type changes behavior, we'd have to consider whether to keep
that for np.float64 or change it to match the usual C semantics used elsewhere.
So there *would* be a dilemma. Not necessarily the most nerve-wracking one, but
a dilemma nonetheless.
--
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-Dev
mailing list