How to Override the “not” Operator in a Python Magic Method?
Short Answer: To override the logical not operator for a custom Python class My_Class, redefine the dunder method My_Class.__bool__() to return your custom Boolean value. This ensures that bool(x) on a My_Class object x returns either True or False. The operation not x will then return the inverse Boolean value, i.e, not x evaluates to … Read more