1

Today I tried an enumeration with aenum (like in this answer: https://stackoverflow.com/a/1695250/4720441):

orientation = Enum('red', 'yellow', 'green', 'orange')

but this generated an error in the aenum library:

File "C:\Python\27\lib\site-packages\aenum__init__.py", line 1417, in _get_mixins_ issubclass(base, Enum) and TypeError: issubclass() arg 1 must be a class

How can I fix this?

Community
  • 1
  • 1
Creatronik
  • 189
  • 3
  • 18
  • The answer you linked to details a couple different ways -- you are trying to use an old-style way with the new `Enum`s. – Ethan Furman Mar 24 '17 at 15:30
  • I could only find the detailed documentation on `https://bitbucket.org/stoneleaf/aenum/src/default/aenum/doc/aenum.rst`. – John Strood Jul 24 '18 at 06:47

2 Answers2

5

The various ways to create an Enum using call syntax are:

# starts at 1
SomeEnum = Enum('SomeEnum', 'member1 member2 member3')

# starts at 1
SomeEnum = Enum('SomeEnum', ['member1', 'member2', 'member3'])

# values manually assigned
SomeEnum = Enum('SomeEnum', [('member1', 3), ('member2', 4), ('member3', 5)])

# values manually assigned
SomeEnum = Enum('SomeEnum', {'member1': 6, 'member2': 7, 'member3': 8})

# starts at 9
SomeEnum = Enum('SomeEnum', 'member1 member2 member3', start=9)

This is the same across the stdlib version, enum34, and aenum1. The enum2 package is an old-style library.


The stdlib versions contain:

3.4

  • Enum
  • IntEnum
  • unique

3.6

  • Flag
  • IntFlag

*enum34 is the same as the 3.4 stdlib.

aenum

contains the above, plus:

  • OrderedEnum - members can be compared against each other
  • AutoNumberEnum - value is automatically generated
  • MultiValueEnum - members can be looked up with have multiple values
  • NoAliasEnum - duplicates are unique, not aliases

and various helpers to manipulate the construction of enumerations:

  • skip - prevents attributes from becoming enum members (attribute stays the same)
  • constant -- to prevent attributes from becoming enum members (attribute becomes a type of read-only property)
  • extend_enum - add new members to an existing enumeration (useful when the Enum is built in stages)

And extra related types:

  • NamedConstant - No enumeration fanciness, just a value with a name
  • NamedTuple - similar to the stdlib namedtuple, but built using metaclasses instead of eval

1 Disclosure: I am the primary author of the Python stdlib Enum, the enum34 backport, and the Advanced Enumeration (aenum) library.

2 The author of the older enum library also helped with the stdlib version.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
1

aenum.Enum is not constructed like this (see https://bitbucket.org/stoneleaf/aenum).

Instead you should be passing in a first argument containing the Enum name, and a second argument corresponding to a space separated list of enum keys you want enumerated.

E.g.

orientation = Enum('red', 'yellow green orange')

Note on the answer at https://stackoverflow.com/a/1695250/4720441

In this answer, the poster creates an enum function, which combines it's arguments into a dict, and then passes that dict to the standard Enum constructor (as the third argument).

Community
  • 1
  • 1
jmetz
  • 12,144
  • 3
  • 30
  • 41
  • So the example in the linked answer is wrong then? Sorry, but I'm not understanding your explanation. Like this: Enum(orientation, 'red' 'yellow' 'green' 'orange') – Creatronik Mar 24 '17 at 12:45
  • 1
    No, the answer there also uses a single space-delimited string for the enum values. – jmetz Mar 24 '17 at 12:48
  • They add an `enum` method that takes multiple **args** and combines them into a dict, then calling the other form of the standard constructor which takes a dict as a third argument. – jmetz Mar 24 '17 at 12:49
  • Alright now I kind of understand: orientation = Enum('orientation', 'red yellow green orange') Because strangely, after installing also the "enum" package, my initial Enum works. That confuses me. – Creatronik Mar 24 '17 at 13:12
  • @Creatronik: The original `enum` package is not the `Enum` that went into the stdlib, and it made different choices about construction. – Ethan Furman Sep 24 '21 at 02:46