[Numpy-discussion] TypeError in dtype.__eq__()

Robert Kern robert.kern at gmail.com
Sun Jan 11 04:49:11 EST 2009


On Sun, Jan 11, 2009 at 03:41, Bryan Cole <bryan at cole.uklinux.net> wrote:
> Dtype objects throw an exception if compared for equality against other
> objects. e.g.
>
>>>> import numpy
>>>> numpy.dtype('uint32')==1
> Traceback (most recent call last):
>  File "<stdin>", line 1, in <module>
> TypeError: data type not understood
>>>>
>
> After some googling, I think python wisdom (given in the Python docs)
> says that equality testing should never throw an exception, but return
> the NotImplemented singleton where the comparison does not make "sense".
> Throwing an exception breaks membership testing for lists/tuples (using
> the 'in' operator calls implicitly calls __eq__()).
>
> My real-world example is serialisation of numpy arrays using PyYAML.
> This module should serialise any picklable object including ndarrays.
> However, it performs a test on each object:
>
>    if data in [None, ()]: blah...
>
> This fails on the dtype object of the array.
>
> What's the consensus on this? Is the current dtype behaviour broken?

It's suboptimal, certainly. Feel free to fix it. However, also note
that with ndarray's rich comparisons, such membership testing will
fail with ndarrays, too.

-- 
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 NumPy-Discussion mailing list