[Python-ideas] Revisiting dedicated overloadable boolean operators

Chris Barker chris.barker at noaa.gov
Mon Aug 6 14:11:17 EDT 2018


On Fri, Aug 3, 2018 at 9:13 PM, Todd <toddrjen at gmail.com> wrote:

>
>> Also, in a common use-case, bitwise-and behaves the same as logical_and,
>> e.g.
>>
>> if (arr > x) & (arr2 == y)
>>
>> This "works" because both arrays being bitwise-anded are boolean arrays.
>>
>

> There are a few problems with using the bitwise operators.
>
> First, and most important in my opinion, is that the precedence is
> significantly off from that of the logical operators.
>

yes, that's true, and perhaps too bad, but as they are spelled differently,
not a killer.


 if you are switching back and forth between, say, array logical operations
> and "normal" logical operations it is easy to mess up.
>

well, as you generally are working with arrays or not, again, not too bad.


> Third is that it allows both boolean and bitwise operations to be carried
> out on the same data types.  Numpy is a special case where the two
> basically are equivalent if you are working with boolean arrays.  But that
> is a special case.
>

I kind of muddled my point -- the main trust was that overloading the
bitwise operators to do logical operations is a fine idea -- many objects
will have no or limited use for bitwise operations.

In fact, if I were to re-design the numpy API, I would overload the bitwise
operators to do logic, and use the special functions for bitwise operations:

np.bitwise_and

etc.

rather than having to use logical_and and friends the way we do now.

So any new class that doesn't already make use of the bitwise operators can
do that.

(yes, still the precedence issue, but what can you do?)

-CHB



-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180806/9c5b293e/attachment.html>


More information about the Python-ideas mailing list