The __invert__ Method
What is the purpose of the __invert__ method? I was exploring Python internals and came across: >>> dir(__builtins__.int) ['__abs__', '__add__', '__and__', '__bool__', '__
Solution 1:
__invert__
is the implementation of the "binary not" operator ~
-- it has absolutely nothing to do with any formal definition of the word "invert" in mathematics
for the integer 0, using 2s complement, the binary negation of 0b000...000
is 0b111...111
-- or -1
in decimal
>>> ~int()
-1
>>> int().__invert__()
-1
>>> ~0
-1
Post a Comment for "The __invert__ Method"