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

Mark Dickinson dickinsm at gmail.com
Thu Apr 7 04:00:36 EDT 2016

On Thu, Apr 7, 2016 at 8:46 AM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> Booleans currently have reasonable overrides for the bitwise binary
> operators:
>>>> True | False
> True
>>>> True & False
> False
>>>> True ^ False
> True

All those are consistent with bool being a subclass of int, in the
sense that (for example) `int(True | False)` is identical to
`int(True) | int(False)`.

Redefining ~True to be False wouldn't preserve that: int(~True) ==
~int(True) would become invalid. In short, the proposal would break
the Liskov Substitution Principle. (The obvious fix is of course to
make True have value -1 rather than 1. Then everything's consistent.
No, I'm not seriously suggesting this - the amount of breakage would
be insane.)

NumPy has the luxury that numpy.bool_ is *not* a subclass of any integer type.


More information about the Python-ideas mailing list