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 aenum
1. The enum
2 package is an old-style library.
The stdlib versions contain:
3.4
3.6
*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.