Special Method Protocols

__invert__() — numeric protocol

Bitwise inversion dispatches to this special method. Unary operations have no reflected fallback. The result is determined by the class; this example delegates to an integer.

Formal Syntax & Structure

python syntaxpython
def __invert__(self):

Return Value

object

Value produced by the hook.

Working Example

Example Snippetpython
class Number:    def __init__(self, value):        self.value = value    def __invert__(self):        return ~self.valueprint(~Number(7))
Python Reference