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

Nick Coghlan ncoghlan at gmail.com
Wed Nov 25 02:06:04 EST 2015


On 24 November 2015 at 16:05, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Nick Coghlan wrote:
>>
>> Something that *could* potentially be comprehensible is the idea of
>> allowing "elementwise" logical operators, with a suitable syntactic
>> spelling.
>
> -1 on tying any of this explicity to a notion of
> elementwise operations. The range of potential uses
> is wider than that.

While I'm still largely of the view that introducing additional
operators would make things more confusing rather than less, I'm also
convinced that if anything like this is going to be pursued without
being incredibly confusing for beginners there needs to be a fairly
concise answer to "What are these operators for?".

Take the "bitwise operators", for example. The notion of a "bitwise
operator" is conceptually dense for folks that have never worked with
binary numbers before. Despite that, if someone asks "What do the &
and | operators do in Python?" the semantics can still be conveyed
relatively quickly using some truth table examples like:

>>> bin(0b101 & 0b110)
'0b100'
>>> bin(0b101 & 0b101)
'0b101'
>>> bin(0b101 | 0b110)
'0b111'
>>> bin(0b101 | 0b101)
'0b101'

Explaining "~" fully is a bit trickier (since you would need to
explain why two's complement representations of binary numbers are
useful), but it's possible to avoid that explanation by using the
alternative arithmetic formulation for "~" given in
https://wiki.python.org/moin/BitwiseOperators : "~x == -x -1"

Matrix multiplication is another example of something that isn't
particularly easy to explain to folks that aren't already familiar
with the relevant domain, but also conveys clearly that you can ignore
it if you're not working with matrices.

It's then also useful to remember that the answers to "What is this
for?" and "How is this used?" for a language construct can diverge
over time. The original "What is this for?" use cases are the ones
that guide the design decisions towards concrete answers that define
how the construct works, and provide the underlying rationale for the
way the construct behaves. The "How is this used?" cases then arise
later when folks say "Yes, those existing semantics are suitable for
my current use case, so I can reuse the syntax".

Some specific examples:

"+" is used not only for addition, but also sequence concatenation.
"&" is not only "bitwise and", but also set intersection
"/" is not only division, but also pathlib path joining

NumPy repurposes most of the binary operators (including the bitwise
ones) as element-wise matrix operations. SQL Alchemy repurposes a
number of them for SQL query operations. SymPy changes them from
arithmetic operations to symbolic ones. Those use cases don't change
the answers to "What are these operators for?" from a language design
perspective, they only change the answers to "How are these operators
used?" from a practical perspective.

Getting back to the specific topic of this thread, this could actually
make an interesting usability study for a language design theorist, by
looking at the kinds of mistakes folks make trying to learn
elementwise logic operations in NumPy, and then seeing whether the
introduction of overridable elementwise logical operators reduces the
learning curve.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list