[issue45368] ~(True) and ~(False) gives incorrect result

Xavier Morel report at bugs.python.org
Tue Oct 5 02:16:57 EDT 2021


Xavier Morel <xavier.morel at masklinn.net> added the comment:

> True is a boolean so ~True should return False according to me.

That's be a BC break for no reason: if you want to invert a boolean you can just `not` it.

> True is not the same as 1

For historical reasons, in Python it is:

>>> bool.mro()
[<class 'bool'>, <class 'int'>, <class 'object'>]
>>> True == 1
True
>>> False == 0
True

So when you call ~True, you're calling `int.__invert__(True)`, which behaves as what it is: the bitwise inverse of a two's-complement signed integer.

----------
nosy: +xmorel

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45368>
_______________________________________


More information about the Python-bugs-list mailing list