Last updated: Apr 8, 2024
Reading timeยท3 min
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 ==
.
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
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:
Enum
, specify mix-in types before the Enum
class.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
).value
attribute on the enum memberAlternatively, you can use the value
attribute on the enum member and compare
the value to a string.
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
We used the value
attribute on members of the enum to get string values that
we can safely compare.
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.
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).
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.
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)
We used the value
attribute on members of the enum to get integer values that
we can safely compare.
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.
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)
An alternative approach is to extend from the OrderedEnum class from the official docs.
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 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).
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.
You can learn more about the related topics by checking out the following tutorials: