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

Chris Angelico rosuav at gmail.com
Tue Nov 24 01:52:13 EST 2015


On Tue, Nov 24, 2015 at 5:34 PM, Random832 <random832 at fastmail.com> 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 Python semantics are defined more tightly than that, though - the
"else" clause in each case would simply be "b". Changing that is not
something you can do with operator overloading, so it would mean a
fundamental change in the operator's semantics. And once you do that,
you end up with a completely different operator.

So, yes, the semantics of Python's short-circuiting 'and' and 'or'
operators precludes any form of overriding. Yes, it's possible to have
overridable short-circuiting operators, but Python's ones are not
those. (Also, I think I prefer the simpler semantics. But that's a
matter of personal choice.)

ChrisA


More information about the Python-ideas mailing list