~bool deprecation

I’ve found using ~ to be very useful in many different situations, and I’ve using it a lot, both in Python and C++. I’ve identified 3 cases where my Python code breaks because of this change. Codes involving bitmasks, codes making use of ~ for “reverse indexing” of a list, and the codegolf trick of using -~x to increment x by 1.

There is a relatively easy workaround, simply search replace ~ with ~+. The + will convert any Boolean to an int.

I’m against deprecating ~ on Booleans in general, but I am strongly against changing ~True to False. A change like that is really dangerous since it could (and would) create silent bugs. It also would make new code incompatible with older versions of Python.

Something I also want to note is that ~ in numpy is always bit-inverse. ~0 is always -1, even in numpy. However, depending on the data type, -1 can have different representations. Modulo two, -1 is equal to 1.

6 Likes