I want to create, in Python 3, a class MyClass where
bool(MyClass) returns False. Is it possible?
I want this behavior with the class itself, not objects of that class. For objects of that class I know that I can just
return False in
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.
class FalseMeta(type): def __bool__(self): return False class MyClass(metaclass=FalseMeta): pass print(bool(MyClass)) # False