I want to create a class MyClass where bool(MyClass) returns False. Is it possible?
MyClass
bool(MyClass)
False
I want this behavior with the class itself, no
To define the __bool__ method used by a class, not its instances, you need to modify its class. You do that by writing a metaclass.
__bool__
class FalseMeta(type): def __bool__(self): return False class MyClass(metaclass=FalseMeta): pass print(bool(MyClass)) # False