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

Antoine Pitrou solipsis at pitrou.net
Thu Apr 7 03:46:18 EDT 2016


Hello,

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_)

Regards

Antoine.




More information about the Python-ideas mailing list