3 ms·
You should get a type error if you try to use an int in boolean logic. So define: def __bool__(self): raise TypeError("I don't like non-bool boole
by ubernostrum 9y ago
You should get a type error if you try to use an int in boolean logic.
So define:
def __bool__(self):
raise TypeError("I don't like non-bool booleans")
on your classes, and you're good to go. Python isn't doing type conversion, it's doing operator overloading and letting you define how your class should work with the 'if' statement. It won't help you with the already-defined behavior of built-in and third-party classes, but you can have all your own code be unusable in boolean operations if you like.