Enum in Python

Enum is a collection of symbolic names and its values are unique and constant. In essence enums are collection of constants.

You can use enum when you have a specific string or integer appearing in several places in the code. By placing enum instead of a string, you would avoid possible errors that could be result of typos in your code.

For example:

if color == 'red':
    return "It's red"

if color != 'red':
    return "It's not red"


from enum import Enum

class Color(Enum):
    RED = 'red'

if color == Color.RED:
    return "It's red"

if color != Color.RED:
    return "It's not red"

When you have to type a string, such as this, across several files in your source code, typos often happen. Another benefit of using enum is that you’ll have only one place in your code to change the value of your constant. Moreover, most of the time, you won’t even care what’s the exact value of the enum - in the end it’s used as a collection of symbolic names.

There are two ways to define enum:

from enum import Enum

# The awkward one-liner - kinda resembles Java like enum
# in Java: public enum Color {RED, GREEN, BLUE}
Color = Enum('Color', 'RED GREEN BLUE')

# Or explicitly
class Color(Enum):

    RED = 'red'
    GREEN = 'green'
    BLUE = 'blue'

Because you often need just a symbolic representation, it’s common to use integers to represent values of enumerable:

from enum import Enum, auto

class Color(Enum):

    RED = 1
    GREEN = 2
    BLUE = 3

# And since typing numbers manually is too hard
# when you have a lot of member of enum, you can
# use auto() function to autoincrement values
class Color(Enum):

    BLACK = auto()
    WHITE = auto()
    RED = auto()
    GREEN = auto()
    BLUE = auto()
    CYAN = auto()
    MAGENTA = auto()

You can access the name and the value like so:

print(Color.RED.value)  # -> 1
print(Color.READ.name)  # -> 'RED'

By default it is possible to have aliases in an enum. If you don’t need such a behavior, then you can use the @unique decorator:

>>> from enum import Enum, unique
>>> @unique
... class WillNotWork(Enum):
...    ONE = 1
...    ANOTHER_ONE = 1
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib/python3.6/enum.py", line 836, in unique
    (enumeration, alias_details))
ValueError: duplicate values found in <enum 'WillNotWork'>: ANOTHER_ONE -> ONE

However, sometimes you do care about the enum values but you’re too lazy to type them. I often wind myself that I need a value which is the same as the name of the constant. You can override the behavior of auto() and achieve this:

class ColorEnum(Enum):

    def _generate_next_value_(name, start, count, last_values):
        return name

class Color(ColorEnum):

    RED = auto()
    GREEN = auto()
    BLUE = auto()

print(Color.RED.value)  # -> 'RED'
print(Color.RED.name)  # -> 'RED'

This is enough to get you started working with enums. If you need more advanced features of enum (such as iterating over members) check out the documentation. Don’t forget that your enum is a Python class, and that you can add any methods that you like, so go crazy.

python  enum 

We're not spammers, and you can opt-out at any moment. We hate spam as much as you do.

powered by TinyLetter

See also