On 07/26/2019 11:56 AM, Erik Aronesty wrote:

I just spend a while tracking down and killing all "if Enum" and "if not
Enum" bugs in my code.   I was frankly shocked that this didn't raise a
ValueError to begin with.

Very few items, if any, raise a ValueError when asked for its True/False 
equivalence.

Apparently all enums are true/false depending on whether the underlying
value is truthy or falsy.

That is the default.  You can, of course, change that:

  class AlwaysFalse(Enum):
      def __bool__(self):
          return False

  class AlwaysTrue(Enum):
      def __bool__(self):
          return True

Which breaks the abstraction Enum's are trying to achieve because now the
user of an Enum has to know "stuff" about the underlying value and how it
behaves.

You can make it always fail:

  class OnTheFence(Enum):
      def __bool__(self):
          raise ValueError('%s members are neither True nor False' % 
self.__class__.__name__)

The default Enum always returns True because the default Enum starts counting 
at 1.

Perhaps you could share your Enum definition and what you expected to happen 
when you used it in a boolean context?

--
~Ethan~
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to