notAdvanced Examples

Logical NOT operator; inverts a boolean value

not in operator overloading

How not interacts with comparison operators.

python
# 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)
Expected Output
not FuzzyBool(0.7): False
not FuzzyBool(0.3): True
x > 5
NOT x > 5

'not' always returns a plain bool. For custom negation logic, use __invert__ (the ~ operator) instead, which can return any type.

Want to try these examples interactively?

Open Advanced Playground