[Python-ideas] Changing the meaning of bool.__invert__

Ethan Furman ethan at stoneleaf.us
Thu Apr 7 11:15:59 EDT 2016


On 04/07/2016 12:46 AM, Antoine Pitrou wrote:

> Booleans currently have reasonable overrides for the bitwise binary
> operators:
>
> --> True | False
> True
> --> True & False
> False
> --> True ^ False
> True
>
> However, the same cannot be said of bitwise unary complement, which
> returns rather useless integer values:
>
> --> ~False
> -1
>  --> ~True
> -2
>
> Numpy's boolean type does the more useful (and more expected) thing:
>
>>>> ~np.bool_(True)
> False
>
> How about changing the behaviour of bool.__invert__ to make it in line
> with the Numpy boolean?
> (i.e. bool.__invert__ == operator.not_)

No.  bool is a subclass of int, and changing that now would be a serious 
breach of backward-compatibility, not to mention breaking existing code 
for no good reason.

Anyone who wants to can create their own Boolean class that doesn't 
subclass int and then declare the behaviour they want.

If bool had been it's own thing from the start this wouldn't have been a 
problem, but it is far too late to change that now.  You would be better 
off suggesting a new Logical type instead (it could even support unknown 
values).

> Apparently someone went to the trouble of overriding __and__, __or__
> and __xor__ for booleans, which is why it looks unexpected to leave
> __invert__ alone.

__and__, __or__, and __xor__'s results in within bool's domain (right 
word?) so keeping them in the bool subtype makes sense;  the result of 
__invert__ is not.

> But I'm not surprised by such armchair commenting and pointless controversy
> on python-ideas, since that's what the list is for....

If you aren't going to be civil, don't bother coming back.

--
~Ethan~



More information about the Python-ideas mailing list