How to Override the “not” Operator in a Python Magic Method?

Rate this post

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 True if bool(x) evaluates to False, and
  • not x evaluates to False if bool(x) evaluates to True.

Here’s how this works in a Gif:

Problem Formulation

Say, you want to override the class of a custom object x so that you can define the return value of not x. How to override the logical not operator in Python?

Background Python “not” Operator

Python’s not operator returns True if the single operand evaluates to False, and returns False if it evaluates to True. Thus, it logically negates the implicit or explicit Boolean value of the operand.

Python Not Operator - Deep Dive

Overriding the Python “not” Operator

Interestingly, you can apply the logical NOT operator on arbitrary Python objects. The base idea is the “truthiness” of Python objects, i.e., every Python object has an associated Boolean value as determined by the __bool__() magic method.

💡 Note: We call this a “Dunder Method” for Double Underscore Method” (also called “magic method”). To get a list of all dunder methods with explanation, check out our dunder cheat sheet article on this blog.

In the following code, you create a custom class called My_Obj and use the “not” operator on instances of this class. You explicitly define the __bool__() method to customize the behavior of the not operator on your custom classes.

class My_Obj:
    def __init__(self, number):
        self.number = number

    def __bool__(self):
        return bool(self.number)

x = My_Obj(0)
y = My_Obj(1)
z = My_Obj(99)

print('not', x.number, '=', not x)
print('not', y.number, '=', not y)
print('not', z.number, '=', not z)


not 0 = True
not 1 = False
not 99 = False

If you hadn’t overridden the __bool__ magic method, Python would assume all objects of the custom objects to be True, so the results would be False for all three objects.


Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!