[Numpy-discussion] ufunc oddities

Robert Kern robert.kern@gmail....
Sat May 24 21:36:49 CDT 2008


On Sat, May 24, 2008 at 9:28 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
> I think it's interesting how python and numpy bools behave differently.
>
>>> x = np.array([True, True], dtype=bool)
>>> x[0] + x[1]
>   True
>>> x[0] & x[1]
>   True
>>>
>>> x = [True, True]
>>> x[0] + x[1]
>   2
>>> x[0] & x[1]
>   True

The difference arises straightforwardly from the principle that numpy
tries not to upcast when you do an operation on two arrays of the same
dtype; True+True==True is of somewhat more use than True+True==False.
Python bools are just ints subclasses to give a nice string
representation.

-- 
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