In python what __thing__ do I use for ~, kind of like how __add__ is for +?
In python what __thing__ do I use for ~, kind of like how __add__ is for +?
The special method you're looking for is `__invert__`. It corresponds to the bitwise NOT operator `~` in Python. ### Example:
1 2 3 4 5 6 7 8 9 | class Example: def __init__(self, value): self.value = value def __invert__(self): return Example(~self.value) # Define behavior for ~ obj = Example(5) result = ~obj # Calls obj.__invert__() |
Testing if the previous output contains the string __invert__
: True
Testing if the previous output contains the string __inv__
: False