Falsey Enums

Dan Sommers dan at tombstonezero.net
Thu Jul 27 22:42:26 EDT 2017


On Fri, 28 Jul 2017 12:15:20 +1000, Steve D'Aprano wrote:

> I has some Enums:
> 
> from enum import Enum
> class X(Enum):
>     Falsey = 0
>     Truthy = 1
>     Fakey = 2
> 
> 
> and I want bool(X.Falsey) to be False, and the others to be True. What should I
> do?

Add the following to your enum:

    def __bool__(self):
        return False if self == X.Falsey else True

But something tells me that you would know that there exists such a
simple solution.  What am I missing?




More information about the Python-list mailing list