Enums in Python
Enums in Python : Enumerate Your Values
Enums are a way to create a set of symbolic names (members) bound to unique, constant values. They are a way to define a set of named constants. Enums are defined using the enumenum module. Enumerations are created using classes. Enums have names and values associated with them. Enums are defined by creating a new class that inherits from EnumEnum. Enums can be iterated over. Enums can be compared using ==== and isis.
Syntax
The syntax for creating an Enum in Python is as follows:
from enum import Enum
class EnumName(Enum):
MEMBER1 = value1
MEMBER2 = value2
MEMBER3 = value3
...from enum import Enum
class EnumName(Enum):
MEMBER1 = value1
MEMBER2 = value2
MEMBER3 = value3
...In the above syntax:
EnumNameEnumNameis the name of the Enum.MEMBER1MEMBER1,MEMBER2MEMBER2,MEMBER3MEMBER3, etc. are the members of the Enum.value1value1,value2value2,value3value3, etc. are the values associated with the members.
Creating Enums
To create an Enum, you need to import the EnumEnum class from the enumenum module. Then, you can create a new class that inherits from EnumEnum. Each member of the Enum is defined as a class attribute. The value of the member is passed as an argument to the EnumEnum constructor. The value of the member can be any type, such as an integer, string, or float.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3In the above code:
ColorColoris the name of the Enum.REDRED,GREENGREEN, andBLUEBLUEare the members of the Enum.11,22, and33are the values associated with the members.- The members of the Enum are accessed using the dot notation, e.g.,
Color.REDColor.RED,Color.GREENColor.GREEN,Color.BLUEColor.BLUE. - The members of the Enum can be compared using
====andisis.
Iterating Over Enums
Enums can be iterated over using a forfor loop. When you iterate over an Enum, you get the members of the Enum.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
for color in Color:
print(color)from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
for color in Color:
print(color)In the above code, the output will be:
C:/Users/username/desktop>python enum.py
Color.RED
Color.GREEN
Color.BLUEC:/Users/username/desktop>python enum.py
Color.RED
Color.GREEN
Color.BLUEComparing Enums
Enums can be compared using the ==== and isis operators. When you compare two Enums using the ==== operator, it compares the values of the Enums. When you compare two Enums using the isis operator, it compares the memory addresses of the Enums.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
color1 = Color.RED
color2 = Color.RED
print(color1 == color2) # Output: True
print(color1 is color2) # Output: Truefrom enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
color1 = Color.RED
color2 = Color.RED
print(color1 == color2) # Output: True
print(color1 is color2) # Output: TrueIn the above code, the output will be:
C:/Users/username/desktop>python enum.py
True
TrueC:/Users/username/desktop>python enum.py
True
TrueEnum Members
You can access the members of an Enum using the dot notation. The members of an Enum are instances of the Enum class.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(Color.RED)
print(Color.GREEN)
print(Color.BLUE)from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(Color.RED)
print(Color.GREEN)
print(Color.BLUE)In the above code, the output will be:
C:/Users/username/desktop>python enum.py
Color.RED
Color.GREEN
Color.BLUEC:/Users/username/desktop>python enum.py
Color.RED
Color.GREEN
Color.BLUEAccessing Enum Members
You can access the members of an Enum using the dot notation. The members of an Enum are instances of the Enum class.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
currentColor = Color.RED
print(type(currentColor))
print(currentColor.name)
print(currentColor.value)from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
currentColor = Color.RED
print(type(currentColor))
print(currentColor.name)
print(currentColor.value)In the above code, the output will be:
C:/Users/username/desktop>python enum.py
<enum 'Color'>
RED
1C:/Users/username/desktop>python enum.py
<enum 'Color'>
RED
1In the above code:
currentColorcurrentColoris an instance of theColorColorEnum.currentColor.namecurrentColor.namereturns the name of the Enum member.currentColor.valuecurrentColor.valuereturns the value of the Enum member.
Accessing Enum Members using Iteration
You can access the members of an Enum using iteration. When you iterate over an Enum, you get the members of the Enum.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
for color in Color:
print(color.name, color.value)from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
for color in Color:
print(color.name, color.value)In the above code, the output will be:
C:/Users/username/desktop>python enum.py
RED 1
GREEN 2
BLUE 3C:/Users/username/desktop>python enum.py
RED 1
GREEN 2
BLUE 3A Real-World Example
Here is a real-world example of using Enums in Python. In this example, we define an Enum called WeekdayWeekday that represents the days of the week. We then create a function that takes a WeekdayWeekday Enum as an argument and prints the name of the day.
from enum import Enum
class Weekday(Enum):
MONDAY = 1
TUESDAY = 2
WEDNESDAY = 3
THURSDAY = 4
FRIDAY = 5
SATURDAY = 6
SUNDAY = 7
def print_day(day):
print(day.name)
print_day(Weekday.MONDAY)
print_day(Weekday.FRIDAY)from enum import Enum
class Weekday(Enum):
MONDAY = 1
TUESDAY = 2
WEDNESDAY = 3
THURSDAY = 4
FRIDAY = 5
SATURDAY = 6
SUNDAY = 7
def print_day(day):
print(day.name)
print_day(Weekday.MONDAY)
print_day(Weekday.FRIDAY)In the above code, the output will be:
C:/Users/username/desktop>python enum.py
MONDAY
FRIDAYC:/Users/username/desktop>python enum.py
MONDAY
FRIDAYConclusion
In this tutorial, you learned about Enums in Python. Enums are a way to create a set of symbolic names (members) bound to unique, constant values. Enums are defined using the enumenum module. Enums are created using classes. Enums have names and values associated with them. Enums can be iterated over. Enums can be compared using ==== and isis. For more information on Enums, you can refer to the official documentation. For more tutorials on Python Central Hub.
Was this page helpful?
Let us know how we did
