How to Create an Enum in Python?


An enum, short for “enumeration,” is a data type that consists of a set of named values. In Python, enums are created using the class keyword.

An enum is a set of symbolic names (members) bound to unique, constant values. Each member of an enum is represented by a value.

Enumeration is a class type that defines all possible values for another variable. In other words, an enumeration is a user-defined data type consisting of a set of named values (members).

By definition, an enum is immutable. This means you can’t add or remove members from an existing enum. However, you can subclass an existing enumeration to create a new one.

Creating an Enum

To create an enum, we use the class keyword followed by the name of the enum. Inside the enum, we define a list of named values using the def keyword. For example, let’s say we want to create an enum of animals. We would do this as follows:

import enum class Animals(Enum): DOG = 1 CAT = 2 BIRD = 3
Code language: Python (python)

We can now use this enum anywhere we need it. When used in conjunction with other data types, enums can be quite powerful.

Accessing an Enum Value

Enums are often used in conjunction with other data types, such as dictionaries and lists. To access an enum value, we use the name of the enum followed by the dot (.) operator and the name of the value we want to access. For example, if we want to access the BIRD value from our Animals enum, we would do so as follows:

Animal = Animals.BIRD print(animal) #Prints 3
Code language: Python (python)

Another Example: Represent the four seasons

Enums are especially useful when you need to define a set of predefined constants. For example, let’s say you want to represent the four seasons in your Python code.

You could do this by creating a list or tuple containing the four season names. However, this approach can be error-prone because you might accidentally add or remove a season name from the list/tuple.

Also, Season(“Summer”) and Season(“winter”) would be considered two different seasons. A more elegant way to solve this problem is to use an enum as follows:

The following example shows how to create and use an enum in Python:

import enum class Season(enum.Enum): winter = 1 spring = 2 summer = 3 autumn = 4 def main():   # get all members of Season enum   for season in Season:     print(season)   # get Winter member using its value (1)   print(Season(1)) # output: Season.winter   # get all members with their values   for season in Season:     print('{}:{}'.format(season.name, season.value))   # iterate over members   for season in Season:     print('{}:{}'.format(season, season.value)) # output: winter:1 ...     # check if member exists in Season enum   if Season.spring in Season: # output: True        print('Spring exists inSeason enumeration')            else:           print('Spring does not exist')   ## main() ## if __name__ == '__main__': main()
Code language: Python (python)

The example shows how easy it is to work with enums in Python. Enums have many benefits over other data types such as lists/tuples: enums are much safer (you can’t accidentally add or remove members), more readable, and easier to use (you can iterate over them just like any other data type). If you need to define a set of predefined constants in your code, then enums are the way to go!

Andy Avery

I really enjoy helping people with their tech problems to make life easier, ​and that’s what I’ve been doing professionally for the past decade.

Recent Posts