Home » Programming » Python » Python Enum

Enums in Python – What They Are, How to Use Them

This article will teach you what an Enum is, and how they are used in the Python programming language.

What is an Enum?

An Enum (Enumerated Type) is a data structure containing multiple values. Each value is assigned to an identifier – and can be accessed by that identifier.

Enums contain pre-defined constants that aren’t expected to change (they’ll be the same when the application is written and when it is run so that you can always refer to the data in the enum by the same identifier and get the same value).

In Python, Enums can be iterated over if needed.

Why use an Enum?

There are a few reasons why you might want to do this:

  • As the values of an Enum are predefined, you can enforce the values of certain values. If the value is assigned from an Enum, it can only be one of the pre-approved values.
    • Especially useful when you’re working with user input and want to make sure that the user has selected a valid option
  • Reduce the size of data stored in a file or database. For example, your Enum could contain complex or lengthy data – but because it’s not going to change, you can just store the name of the enum member in the database rather than storing the whole value.
  • You might come up with your own reasons for using it once you start building your own apps – I have no idea what you’re going to get up to!

Declaring an Enum in Python

Enums are declared by extending the Enum class – a built-in Python class (a class is a template for creating objects/variables) which has the purpose of being extended to create Enums.

Below, an enum is created containing months of the year, which are given integer values:

# Import the Enum class from the enum library
from enum import Enum

# Create the Month class by extending the Enum Class
class Month(Enum):
    # Define the members of the Enum - the name of the member followed by a unique value for the member
    JAN = 1
    FEB = 2
    MAR = 3

Above, the name of each member is an abbreviated month, and the value is the numeric representation of that moth.

Any type of value can be stored in the enum! Strings, numbers, objects – anything you wish to store – so long as each value is unique. You will then be able to access that value by the name of the member elsewhere in the application via the Enum.

Enum Member Types

Once added to the Enum, the type of the member is the Enum it belongs to. This can be shown using the type() function to check the type of an Enum member:

type(Month.JAN) # Returns <enum 'Month'>

The enum library is included with Python 3.

Using an Enum in Python

Now that the Enum is defined, here’s how it can be used.

Access & Print Member

When you print a member of an Enum, the Class name and member name are returned:

print(Month.JAN) # Prints Month.JAN

Print Member (With Detail)

If you want to print the name of the enum, the name of the member, and the value of the member, use the repr function:

print(repr(Month.JAN)) # Prints <Month.JAN: 1>

The repr function prints a human-readable version of a variable.

Print the Name of An Enum Member

You can print the name of the member-only:

print(Month.JAN.name) # Prints JAN

Access Enum Members by Value

As all values in an Enum are unique, you can access the member by value using standard brackets:

Month(1) # <Month.JAN: 1>

Access Enum Members by Name

You can access an enum member and its value by referring to it by name using square brackets:

Month['JAN'] # <Month.JAN: 1>

Accessing Enum Attributes from a Variable with an Enum Value (The Useful Bit)

Here’s where things get useful. You can assign a variable a value pulled from an Enum.

myVariable = Month.JAN # Assign myVariable a value pulled from the Month enum
print(myVariable.name) # Prints 'JAN' -  The variable will have the name and value from the enum
print(myVariable.value) # Prints 1

Now, this seems less than useful right now – but imagine if the value assigned to JAN in the enum was a list of every date in January. It’s a long list of dates.

Now you’re saving your application state and want to store the value of myVariable so that it’s the same when the application loads next time – you don’t really want to store that whole list of dates in a database column – it’ll take up lots of space! You just need to know that the days of January were loaded.

Well, seeing as you’ve got your Enum set up, you can just save the string “JAN” in your database – and then when your application loads next, you can reload all of those dates by simply looking in your Enum for the value assigned to the name “JAN.”

Handy!

For more information and examples, check out the official Python documentation.

SHARE:
Photo of author
Author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment