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
x returns either
not x will then return the inverse Boolean value, i.e,
not xevaluates to
not xevaluates to
Here’s how this works in a Gif:
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
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.
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.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.