[Python-ideas] Revisiting dedicated overloadable boolean operators

Benedikt Werner 1benediktwerner at gmail.com
Sat Aug 4 10:56:56 EDT 2018


> I think that before adding more ad hoc binary operators, we ought to
> consider the possibility of custom operators.
That actually sounds like the most sensible solution so far, altough the 
scope of such a change is also much larger. But custom operators really 
would be a cool addition to Python.

> 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.
As Dan already pointed out it's very hard to see the difference between 
two and three underscores so I don't think "trunders" would be a good idea.

I think it would make sense to instead use a new keyword to define 
operators. Maybe something like "defop". I don't think that's a very 
common variable or function name.
Example syntax could be:

class MyInt(int):
     defop combine(self, other):
         return self, other

# and now we can use it
x combine y

# which is equivalent to
x.combine(y)

Of course it would actually have to check if and where the operator is 
defined like currently done for overloading.

Also it might be worth considering support for pre/postfix operators, 
maybe either with a differnt keyword (something like defpre/defprefix, 
don't really like those but something similar maybe) or a symbol as 
indicator (e.g. "defop <combine(..)" for prefix and "defop >combine(..)" 
for postifx).



More information about the Python-ideas mailing list