In this tutorial we will explore how to create and use an Enum Class in Python.
Enums are a fairly common programming concept, and included in many languages like C++. In Python, we need to first import the Enum class from the enum module before using it. But what are Enums?
An Enum, is a special type of Class or datatype consisting of a set of names, each of which corresponds to a certain value. You can also think of an Enum as a set of constants.
WIDTH = 1920
The above code shows that the name WIDTH
has a value of 100. An Enum Class is a collection of such names, each representing a value.
Using Enum Classes in Python
So let’s discuss some code now. A very popular example of where Enums are used is with the days of the Week. Let’s create an enum based off the seven days, where the name of each day corresponds to a numeric value.
from enum import Enum
class Days(Enum):
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
Here we have created a new Class called Days, which inherits from the Enum Class. Let’s try and access some of the values contained inside this Enum.
There are three ways in which we can access an Enum.
print(Days.Monday)
print(Days.Monday.name)
print(Days.Monday.value)
Days.Monday
Monday
1
We can either access the class + object name, or we can access object name, or we can access it’s value.
Enum Classes Example# 2
Let’s take a look at one more example, with an Enum containing information about a screen display.
class Screen(Enum):
WIDTH = 60
HEIGHT = 50
RES_WIDTH = 1920
RES_HEIGHT = 1080
Here we have an enum with 4 members. We have the WIDTH and HEIGHT for the screen size in centimeters. We also have RES_WIDTH and RES_HEIGHT for the length and height in terms of pixels (resolution).
So let’s say we wanted to calculate the Pixel Density of the screen. We would do the following:
pixels = Screen.RES_HEIGHT.value * Screen.RES_WIDTH.value
area = Screen.HEIGHT.value * Screen.WIDTH.value
density = pixels / area
print(density)
691.2
Benefits of Enum Classes in Python
So what’s the benefit of using Enums? Well, the main reason is that it makes it more readable. For example, in a large application with many “id’s” and “keys” it is easer to remember them by name instead of the numeric value.
This is especially useful when the numeric values are large, or very similar to each other as seen in the Screen Information example.
More about Python Enums
Another interesting thing about Enums, is that they will automatically return an error if you try declaring two Enum members with the same name. For e.g, you cannot have two Monday
‘s, as this would create ambiguity (same name pointing to two values).
It’s also good practice to add in doc-strings for your Enum Classes. This helps people understand what names are included in the Enum, and what values they have.
from enum import Enum
class Days(Enum):
"""
Enum for Days in a Week
Monday to Sunday
1 to 7
"""
Monday = 1
Tuesday = 2
Wednesday = 3
Thursday = 4
Friday = 5
Saturday = 6
Sunday = 7
Now if we print the doc string,
print(Days.__doc__)
We get the following output:
Enum for Days in a Week
Monday to Sunday
1 to 7
This quickly tells you what the Enum is about, and what names it contains.
This marks the end of the Python Enum class Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.