[Python-ideas] Changing the meaning of bool.__invert__
Robert Kern
robert.kern at gmail.com
Thu Apr 7 09:08:44 EDT 2016
On 2016-04-07 13:19, Oscar Benjamin wrote:
> On 7 April 2016 at 08:46, Antoine Pitrou <solipsis at pitrou.net> wrote:
>> Numpy's boolean type does the more useful (and more expected) thing:
>>
>>>>> ~np.bool_(True)
>> False
>
> This is a consequence of another unfortunate design by numpy. The
> reason for this is that numpy uses Python's bitwise operators to do
> element-wise logical operations.
This is not correct. & | ^ ~ are all genuine bitwise operations on numpy arrays.
>>> i = np.arange(5)
>>> ~i
array([-1, -2, -3, -4, -5])
What you are seeing with bool arrays is that bool arrays are *not* just uint8
arrays. Each element happens to take up a single 8-bit byte, but only one of
those bits contributes to its value; the other 7 bits are mere padding. The
bitwise & | ^ ~ operators all work on that single bit correctly. They do not
operate on the padding bits as they are not part of the bool's value.
--
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-ideas
mailing list