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 +?
**`__invert__`** In Python, the `__invert__` method is used to overload the bitwise NOT operator (`~`). This method should return the inverted value of the object. Here's an example:
1 2 3 4 5 6 7 8 9 10 | class MyClass: def __init__(self, value): self.value = value def __invert__(self): return MyClass(~self.value) obj = MyClass(5) inverted_obj = ~obj print(inverted_obj.value) # Output: -6 |
Testing if the previous output contains the string __invert__
: True
Testing if the previous output contains the string __inv__
: False