Adding non-integer member values to IntEnum in Python3

Viewed 30

I have roughly the following:

class Foo(IntEnum):
  a = 0
  b = auto()
  c = auto()
  strings = ["Alpha", "Beta", "Charlie"]
  def __str__(self):
    return Foo.strings[self]

However, this raises:

TypeError: int() argument must be a string, a bytes-like object or a number, not 'EnumMeta'

I need to have additional data inside my Foo class, but it appears that Python doesn't like that.

Is there something I'm doing incorrectly, or is there a better way to do this? I'm used to Enum Classes in C++.

1 Answers

After a fair bit of digging, I found a way to get the proper behavior that I was looking for. Firstly, I had to use Enum instead of IntEnum. Secondly, I had to upgrade to Python3.11 and use the nonmember function to mark things that are not enumerable.

Related