[Python-Dev] [Python-ideas] Boolean value of an Enum member

Ethan Furman ethan at stoneleaf.us
Sat Jan 16 13:18:15 EST 2016


[resending to lists -- sorry, Greg]

On 01/15/2016 12:36 PM, Greg Ewing wrote:
> Ethan Furman wrote:

>> So the question now is:  for a standard Enum (meaning no other type
>> besides Enum is involved) should __bool__ look to the value of the
>> Enum member to determine True/False, or should we always be True by
>> default and make the Enum creator add their own __bool__ if they want
>> something different?
>
> Can't you just specify a starting value of 0 if you
> want the enum to have a false value? That doesn't
> seem too onerous to me.

You can start with zero, but unless the Enum is mixed with a numeric 
type it will evaluate to True.

Also, but there are other falsey values that a pure Enum member could 
have:  False, None, '', etc., to name a few.

However, as Barry said, writing your own is a whopping two lines of code:

   def __bool__(self):
     return bool(self._value_)

With Barry and Guido's feedback this issue is closed.

Thanks everyone!

--
~Ethan~


More information about the Python-Dev mailing list