[Python-ideas] [Python-ideos] Dedicated overloadable boolean operators

Chris Barker - NOAA Federal chris.barker at noaa.gov
Wed Nov 25 18:58:46 EST 2015


> While I'm still largely of the view that introducing additional
> operators would make things more confusing rather than less,

Well, almost by definition, more stuff to understand is more confusing
for beginners.

> there needs to be a fairly
> concise answer to "What are these operators for?".

I don't think "they are for doing logical operations on each of the
elements in a sequence, rather than the sequence as a whole", along
with an example or two is particularly challenging.

In fact, much less so than the Bitwise operators, or matrix
multiplication, which require a bit of domain knowledge, as you say.
But those aren't a big problem either: "if you don't know what it
means, you probably don't need it"

But as for general element-wise operators:

IIRC, this was discussed a lot back in the day -- and was driven by
experience with e.g. Matlab, where the regular math operators do
linear algebra by default, and there are alternative "element wise"
operators. Numpy, on the other hand, does element-wise by default, so
we wanted another set for linear algebra. However, we came to realize
that the only one really needed was matrix multiply -- and thus the
new @ operator.

This all worked because Numpy could overload the math operators to be
element wise, and once rich comparisons were implemented, that covered
almost everything. So all that's left is and-or.

Add the fact that use the Bitwise & and | in their place in most
cases, and we've done fine so far.

All that being said -- two more operators for "rich and" and "rich
or". Would nicely complete the picture.

I was just introducing my intro Python class to the magic methods last
night -- there are a LOT of them! Two more is pretty trivial Addison
of complexity.

As long as we can find a way to spell them that is not too confusing
or ugly -- I think it's a win-win.

Note: having worked with array-oriented languages/libraries for a long
time, I'd like element-wise operators that worked with all the
built-in types. But I suspect that Python is never going to go there.
So we only need these two.

-CHB


More information about the Python-ideas mailing list