Comparing Enums or a String with an Enum in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Table of Contents

  1. Compare a string with an Enum in Python
  2. Comparing Enums in Python

# Compare a string with an Enum in Python

To compare a string with an enum, extend from the str class when declaring your enumeration class, e.g. class Color(str, Enum):.

You will then be able to compare a string to an enum member using the equality operator ==.

main.py
from enum import Enum class Color(str, Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' my_str = 'go' if my_str == Color.GREEN: # ๐Ÿ‘‡๏ธ This runs print('success') print(my_str == Color.YELLOW) # ๐Ÿ‘‰๏ธ False

compare string with an enum

The code for this article is available on GitHub

The Color class is a subclass of both str and Enum which makes comparing strings to enum members possible.

Note that there are some rules when working with derived enumerations:

  1. When subclassing Enum, specify mix-in types before the Enum class.
  2. While Enum can have members of any type, once additional types are mixed, all the members in the enum must have values of the specified type (e.g. str).

# Using the value attribute on the enum member

Alternatively, you can use the value attribute on the enum member and compare the value to a string.

main.py
from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' my_str = 'go' if my_str == Color.GREEN.value: # ๐Ÿ‘‡๏ธ This runs print('success') print(my_str == Color.YELLOW.value) # ๐Ÿ‘‰๏ธ False

using value attribute on the enum member

The code for this article is available on GitHub

We used the value attribute on members of the enum to get string values that we can safely compare.

main.py
from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' print(Color.RED.value) # ๐Ÿ‘‰๏ธ 'stop' print(Color.GREEN.value) # ๐Ÿ‘‰๏ธ 'go' print(Color.YELLOW.value) # ๐Ÿ‘‰๏ธ 'get ready'

You can also use square brackets to access the enum member if you don't know its name ahead of time.

main.py
from enum import Enum class Color(Enum): RED = 'stop' GREEN = 'go' YELLOW = 'get ready' name_1 = 'RED' print(Color[name_1].value == 'stop') # ๐Ÿ‘‰๏ธ True

This is useful when you don't know the name of the enum member ahead of time (because it's read from a file or fetched from an API).

# Comparing Enums in Python

Use the value attribute on each enum member to compare enums in Python.

The value attribute on the enum member returns the literal that can be used to safely compare enum values.

main.py
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 # ๐Ÿ‘‡๏ธ True print(Sizes.LARGE.value > Sizes.MEDIUM.value) # ๐Ÿ‘‡๏ธ True print(Sizes.MEDIUM.value < Sizes.LARGE.value) # ๐Ÿ‘‡๏ธ False print(Sizes.SMALL.value == Sizes.LARGE.value)

comparing enums in python

The code for this article is available on GitHub

We used the value attribute on members of the enum to get integer values that we can safely compare.

main.py
class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 print(Sizes.SMALL.value) # ๐Ÿ‘‰๏ธ 1 print(Sizes.MEDIUM.value) # ๐Ÿ‘‰๏ธ 2 print(Sizes.LARGE.value) # ๐Ÿ‘‰๏ธ 3

You can also use square brackets to access the enum member if you don't know its name ahead of time.

main.py
class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 name_1 = 'LARGE' name_2 = 'SMALL' # ๐Ÿ‘‡๏ธ True print(Sizes[name_1].value > Sizes[name_2].value)
This is useful when you don't know the name of the enum member ahead of time (because it's read from a file or fetched from an API).

# Comparing Enums by extending from OrderedEnum

An alternative approach is to extend from the OrderedEnum class from the official docs.

main.py
from enum import Enum class OrderedEnum(Enum): def __ge__(self, other): if self.__class__ is other.__class__: return self.value >= other.value return NotImplemented def __gt__(self, other): if self.__class__ is other.__class__: return self.value > other.value return NotImplemented def __le__(self, other): if self.__class__ is other.__class__: return self.value <= other.value return NotImplemented def __lt__(self, other): if self.__class__ is other.__class__: return self.value < other.value return NotImplemented # class Sizes(OrderedEnum): class Sizes(OrderedEnum): SMALL = 1 MEDIUM = 2 LARGE = 3 # ๐Ÿ‘‡๏ธ True print(Sizes.LARGE.value > Sizes.MEDIUM.value) # ๐Ÿ‘‡๏ธ True print(Sizes.MEDIUM.value < Sizes.LARGE.value) # ๐Ÿ‘‡๏ธ False print(Sizes.SMALL.value == Sizes.LARGE.value)
The code for this article is available on GitHub

The OrderedEnum class example from the official docs is the recommended way to compare enums (if you don't consider accessing the value attribute enum comparison).

Note that the class is not exported from the enum module, so we have to copy and paste it in order to use it.

The class implements the comparison methods, so we don't have to.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev