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

Andrew Barnert abarnert at yahoo.com
Thu Nov 26 01:27:19 EST 2015


On Nov 25, 2015, at 21:44, Nick Coghlan <ncoghlan at gmail.com> wrote:
> 
> On 26 November 2015 at 09:58, Chris Barker - NOAA Federal
> <chris.barker at noaa.gov> wrote:
>>> 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"
> 
> Right, that's why I think "elementwise logical operators (which may
> potentially be useful for other things)" is an idea that has some hope
> of avoiding creating new barriers to learning:
> 
> * if "elementwise" doesn't mean anything to you, and no library you're
> using mentions them in its documentation, you can ignore them

But "elementwise" isn't what people doing symbolic computation or most other uses of DSL/expression-tree libraries are doing.

Even for ORMs and other query-based libraries like AppScript, where arguably it is what they're doing, they probably aren't thinking of it that way, and wouldn't recognize that it should mean something to them, much less that it's what they're looking for.

So I think this is effectively less general/useless than a solution that just allows overloading boolean operators somehow, without adding a distinction between elementwise (and overloadable) and objectwise (and not).

> * for folks that do know what it means, "elementwise" is evocative of
> the relevant semantics for at least the data analysis use case
> 
>> But as for general element-wise operators:
> 
> I wasn't suggesting those - just element-wise logical operators. In
> terms of scoping the use case: bitwise and/or/not work fine for
> manipulating existing boolean masks, and converting a single matrix to
> a boolean mask involves apply an elementwise function, not elementwise
> logical operations.
> 
> So elementwise logical operators would presumably be aimed at *data
> merging* problems - creating a combined matrix where some values are
> taken from matrix A and others from matrix B, based on the truthiness
> of those values.
> I'm not enough of a data analyst to know how common
> that problem is, or whether it might be better served by a higher
> level "replace_elements" operation that accepts a base array, a
> replacement array, and a boolean mask saying which values to replace.

When I use NumPy, sometimes I'm doing GPU-ish stream operations, which need things like compacting select, which aren't obviously expressible in boolean terms, so I end up looking for methods for everything rather than operators even when they might make sense.

But otherwise, when I'm doing more typical NumPy stuff (or at least what I think is more typical, but I could easily be wrong), I look for elementwise operators all over the place, including abusing the bitwise operators when it makes sense, so I probably would use real boolean operators if it were more obvious/readable.


More information about the Python-ideas mailing list