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

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Nov 24 16:24:07 EST 2015


Brendan Barnwell wrote:
> On 2015-11-23 22:34, Random832 wrote:
> 
 >> C# allows overloading the short-circuit operators
>>
>> a and b: a if a.__false__() else a & b
>> a or b: a if a.__true__() else a | b
>>
>     The problem is that this kind of overriding doesn't handle the main 
> use case, which is elementwise and-ing/or-ing.

Maybe we can hook into __bool__ somehow, though?

Suppose 'a and b' were treated as:

    try:
       result = bool(a)
    except IDoNotShortCircuit:
       result = a.__logical_and___(b)
    else:
       if not result:
          result = b

Since a __bool__ call is required anyway, this
shouldn't slow down the case where there is no
overriding.

-- 
Greg


More information about the Python-ideas mailing list