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

Random832 random832 at fastmail.com
Tue Nov 24 10:11:45 EST 2015


On 2015-11-24, Chris Angelico wrote:
> 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.

I don't see that as "fundamental". Certainly it can't _actually_
be a & b, but it could certainly be "a __foo__ b" where the
default implementation of the __foo__ method (on object) simply
returns b.

If that's "fundamental" then adding overloading to _any_ operator
that didn't support it in Python 1.0 violates those operators'
"fundamental" behavior of raising a TypeError when applied to
types they did not support in Python 1.0.

This would also allow b.__rfoo__, to address the objection
Nathaniel Smith raised, of the right-hand-side not being able to
override. It can't override "does it short-circuit", since
that'd defeat the point of short-circuiting, but it can override
the value in the case where it doesn't, and a well-behaved
'elementwise-"and"able' type would work either way, only
short-circuiting if every element of the left side is false (or
true for "or").



More information about the Python-ideas mailing list