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

Brendan Barnwell brenbarn at brenbarn.net
Tue Nov 24 01:41:45 EST 2015


On 2015-11-23 22:34, Random832 wrote:
> On 2015-11-24, Chris Angelico <rosuav at gmail.com> wrote:
>> I think it's reasonable, except for the potential confusion of having
>> *three* "and" operators. The one with the word is never going to
>> change - its semantics demand that it not be overridable.
>
> How? Are you referring to the short-circuit? C# allows
> overloading the short-circuit operators by doing it in two
> parts - in pseudocode:
>
> a and b: a if a.__false__() else a & b
> a or b: a if a.__true__() else a | b
>
> There's no fundamental reason short-circuiting "demands" that it
> not be overridable, just because C++ can't do it.

	The problem is that this kind of overriding doesn't handle the main use 
case, which is elementwise and-ing/or-ing.  If a is some numpy-like 
array of [1, 1], then it may be boolean true, but you still want "a 
magic_or b" to do the elementwise operation, not just return "a" by 
itself.  (I say "numpy-like" because a real numpy array will fail here 
precisely because its boolean truth value is undefined, so it raises an 
error.)

	The problem is that, with the existing and/or, the issue of the boolean 
truth values of the operands is entangled with the actual and/or 
operation.  For an elementwise operation, you *always* want to do it 
elementwise, regardless of whether the operands "count" as boolean false 
in some other situation.

-- 
Brendan Barnwell
"Do not follow where the path may lead.  Go, instead, where there is no 
path, and leave a trail."
    --author unknown


More information about the Python-ideas mailing list