Python Enum Class

I recently needed to use enumeration in my Python code, and found no suitable solution available. The reason was I needed the enumeration to implement some special features which are supported by enumerations in C/C++ and/or C#/.NET.  

These included the following:
1) Option to assign unrelated arbitrary values to different enumeration members.
2) Being able to use the values of enumeration members in bitwise operations.
3) Option to add a description that includes whitespaces to enumeration members.
Here are some description notes about the supported enumerations:
  1. The arguments of an enumeration are its members: member-1, member-2, member-3, ...
  2. A member must be either a member name string that contains no whitespace or a tuple.
  3. In case of a tuple:
    • Its first element must be a member name string that contains no whitespace.
    • If it has a second element, that element may be either a member description string or a member integer value.
    • If it has a third element, that element must be either a member description string (in case the second element was a member integer value) or a member integer value (in case the second element was a member description string).
  4. An enumeration may not include two members with same name or description.
  5. An enumeration may (may not) include two members with same value if below defined flag duplicate_values_allowed is set True (False), respectively.
  6. As long as no explicit values are specified, the values of the enumeration members are their indices their (0, 1, 2, ...).
  7. Whenever an explicit value is given, it is used instead (from the moment where the first explicit value is encountered, the indices aren't used anymore).
  8. When an explicit value (n) is encountered, and the following members (those that don't have an explicit value) get the consequtive values (n+1, n+2, n+3, ...).
  9. Member descriptions are useful for printings and for combo/list-boxes (recall that member names cannot contains whitespaces!).
  10. Where no description is explicitly specified for an enumeration element, its description is same as its name.
And here're some example enumerations:
  1. cards = enum.Enumeration(('king', -111, 'the sovereign'), 'queen', ('prince', 'the son of the sovereign', 333))
    Here, since 'king' starts with -111, 'queen' will get -110. But 'prince' breaks the order with 333. If 'king' wasn't explicitly set to -111, it was getting a value of zero. As you can see, the descriptions, which like the values are also optional,  may contain blank spaces.
  2. cards = enum.Enumeration(('king', -111), ('queen', -111))
    That's also a legal enumerationeration, as duplicate values allowed. Since values aren't converted back to names, duplicate value make no harm.
  3. flags = enum.Enumeration(("hex_0000", 0x0000), ("hex_1000", 0x1000), ("hex_2000", 0x2000), ("hex_8000", 0x8000), ("hex_ffff", 0xFFFF))
    That's an example of an enumeration that contains hexadecimal values, which may be used in bitwise operations. See below linked Test Class for sample usage. With this enumeration, the following Python expressions return TRUE:
    • (flags.hex_ffff.__rshift__(14) == 0x0003), which demonstrates a bitwise shift.
    • ((flags.hex_1000 & flags.hex_2000) == 0x0000), which demonstrates a bitwise and operation.
I therefore had to develop a new class of enumeration by myself, and here are links to the results (click to download):
  1. Python Enum module
  2. Python Enum module tests
Read the instructions, documentation, and code samples at the top of these modules. Note that the enumtests module contains further code samples as part of its tests. These tests are performed using Python's unittest module, so they may serve as a basic tutorial for working with this module.
Hope you'll find them useful!

You may email me, if you wish, to:


Leave comments

  • This is a very useful enum module, which we could benefit from. Please consider using LGPG license to allow inclusion in commercial products.

    • Thomas K.
  • Glad to hear that! Here's my permission granted for everyone to use it as he/she may find appropriate, including in commercial products. Thanks!

    • Ofer
  • Hi Ofer,

    Previously I also encountered the same problem of defining enum. And my solution is simply this:



    It use ctypes package to define an int argument

    • eko

Copyright(c) 2017 -
By using this website, you signify your acceptance of Terms and Conditions and Privacy Policy
All rights reserved