[Python-ideas] Revisiting dedicated overloadable boolean operators

Steven D'Aprano steve at pearwood.info
Sat Aug 4 09:13:34 EDT 2018


On Fri, Aug 03, 2018 at 03:17:42PM -0400, Todd wrote:

> Boolean operators like the sort I am discussing have been a standard part
> of programming languages since forever.  In fact, they are the basic
> operations on which modern microprocessors are built.
> 
> The fact that Python, strictly speaking, doesn't have them is extremely
> unusual for a programming language.

I'm rather surprised at this claim.

Can you give a survey of such overridable boolean operators which are 
available on modern microprocessors?

What programming languages already have them? When you say "forever", 
are you going back to Fortran in the 1950s?


> In many cases they aren't necessary in
> Python since Python's logical operators do the job well enough, but there
> are a set of highly diverse and highly prominent cases where those logical
> operators won't work.

Can you list some of these diverse and highly prominent use-cases?

I can think of two:

- elementwise boolean operators, such as in numpy;

- SQL-like DSL languages;

plus a third rather specialised and obscure use-case:

- implementing non-binary logical operators, for (e.g. ternary
  or fuzzy logic).


> There are workarounds, but they are less than
> optimal for the reasons I describe, and the previous discussion I linked to
> goes into much more detail why these new operators are important.

There are certainly advantages to using binary operators over named 
functions, and a shortage of good, ASCII punctuation suitable for new 
operators.

I don't think much of your names bOR etc.

I think that before adding more ad hoc binary operators, we ought to 
consider the possibility of custom operators. For example, Julia uses 

    |> op <|

https://github.com/JuliaLang/julia/issues/16985

(which I think is ugly and excessively verbose); Swift allows code to 
define custom prefix, infix or postfix operators:

https://docs.swift.org/swift-book/LanguageGuide/AdvancedOperators.html

https://docs.swift.org/swift-book/ReferenceManual/Declarations.html#//apple_ref/doc/uid/TP40014097-CH34-ID380

https://docs.swift.org/swift-book/ReferenceManual/LexicalStructure.html#ID418


Haskell is another language which supports custom infix operators:

https://csinaction.com/2015/03/31/custom-infix-operators-in-haskell/


Here's a spur-of-the-moment suggestion: allow ~op for named infix 
operators. So:

    a ~foo b

is *roughly* equivalent to:

    if hasattr(a, '__foo__'):
        return a.__foo__(b)
    elif hasattr(b, '__foo__'):
        return b.__rfoo__(a)
    else:
        raise TypeError

Although possibly we might choose another pseudo-namespace, to avoid 
custom operators clashing with dunders. Trunders perhaps? (Triple 
underscores?)

Under this scheme, your operators would become:

    ~or
    ~and
    ~xor

and call trunders ___or___ etc.



-- 
Steve


More information about the Python-ideas mailing list