简体   繁体   中英

Adding non-integer member values to IntEnum in Python3

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++.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM