[Numpy-discussion] ufunc oddities

Keith Goodman kwgoodman at gmail.com
Sat May 24 22:46:04 EDT 2008


On Sat, May 24, 2008 at 7:36 PM, Robert Kern <robert.kern at gmail.com> wrote:
> On Sat, May 24, 2008 at 9:28 PM, Keith Goodman <kwgoodman at 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.

Sounds like there is no perfect solution. I like it the way it is but
these are differences I never noticed.

>> x = np.array([True, True], dtype=bool)
>> x.sum()
   2
>> x[0] + x[1]
   True



More information about the NumPy-Discussion mailing list