not — Advanced Playground
Logical NOT operator; inverts a boolean value
Python Playground
# not and operator overloading
class FuzzyBool:
def __init__(self, value):
self.value = max(0.0, min(1.0, value))
def __bool__(self):
return self.value >= 0.5
def __repr__(self):
return f"FuzzyBool({self.value})"
maybe = FuzzyBool(0.7)
unlikely = FuzzyBool(0.3)
print(f"not {maybe}: {not maybe}")
print(f"not {unlikely}: {not unlikely}")
# Note: not always returns True/False, never a custom type
# For custom inversion, implement __invert__ (used with ~)
class Condition:
def __init__(self, expr, negated=False):
self.expr = expr
self.negated = negated
def __invert__(self):
return Condition(self.expr, not self.negated)
def __repr__(self):
prefix = "NOT " if self.negated else ""
return f"{prefix}{self.expr}"
cond = Condition("x > 5")
print(cond)
print(~cond)
Output
Click "Run" to execute your code
'not' always returns a plain bool. For custom negation logic, use __invert__ (the ~ operator) instead, which can return any type.
Challenge
Try modifying the code above to explore different behaviors. Can you extend the example to handle a new use case?