Enumerations or Enums are a way to manage strictly defined states. They avoid ambiguities and accidental bugs when using standard data types.

Defining an Enum

An Enum is defined by calling the Enum as a function with the possible states as as a list:

TrafficLight = Enum('TrafficLight', ["RED", "AMBER", "GREEN"])

Alternatively you can define an Enum as a subclass, with the possible states as attributes:

from enum import Enum

class TrafficLight(Enum):
    RED = 1
    AMBER = 2
    GREEN = 3

Using an Enum

To use the states in an Enum, assign it to a state variable:

a = TrafficLight.RED

Then compare values to another state:

print(a is TrafficLight.GREEN)
print(a TrafficLight.RED)

Inspecting Enums

Also try:

print(TrafficLight.RED == 1)

Iterating over an Enums

iterating over an Enum gives you all states:

for x in TrafficLight:

Dr. Kristian Rother

I am a professional Python trainer, developer and author based in Berlin. I believe everybody can learn programming.

Contact me via:
+49 176 3052 4691


Thanks, everybody. Without your support... there would be no Spectacular Speaking...

Jerzy Zientkowski, Speakerslair

See also