Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Python Enum Comparison: Choosing the Right Enum Type

Learn how Python enum members compare for equality, ordering, and value behavior, and when to use Enum, IntEnum, StrEnum, or Flag.

~5 min readPython
#Enum#IntEnum#StrEnum#Flag
A visual comparison of Python enum types showing Enum, IntEnum, StrEnum, and Flag with their comparison behaviors.

When you need to represent a fixed set of named values in Python, the enum module gives you several options. The choice affects how members compare to each other and to plain values. Comparing Python enums is not just a syntax detail; it determines whether your code behaves correctly when you check equality, sort members, or combine flags.

Why Enum Comparison Matters

Enums exist to give names to constant values, but the way those names participate in comparisons is not uniform across all enum types. The base Enum class treats members as unique objects, so equality is identity-based. IntEnum members are also int instances, which means they compare directly to integers. StrEnum members are strings. Flag members support bitwise operations, which changes how you combine and compare them.

If you choose the wrong enum type, you might write code that works in one context but fails in another. For example, using Enum when you need to pass values to a function that expects an integer will raise a TypeError. Understanding the comparison behavior of each type helps you avoid such mismatches.

Comparing Enum Members: Equality and Identity

By default, two enum members are equal only if they are the same object. This is because Enum creates singletons for each value. Consider this example:

python
from enum import Enum class Color(Enum): RED = 1 GREEN = 2 c1 = Color.RED c2 = Color.RED print(c1 == c2) # True print(c1 is c2) # True

The default == behavior for Enum is identity-based. This is safe because the members are singletons—you cannot create two distinct Color.RED objects. For plain Enum, comparing members from different enum classes returns False, even if their values match:

python
class Shape(Enum): RED = 1 BLUE = 2 print(Color.RED == Shape.RED) # False

This identity rule applies to plain Enum. IntEnum, StrEnum, and IntFlag compare by their underlying value, so members from different classes can be equal when their values match; those cases are covered below.

If you need equality based on value rather than identity in a plain Enum, you can override __eq__ in your enum class. To keep members usable as dictionary keys, you should also provide a matching __hash__. In most cases this is unnecessary, because the singleton behavior already gives you consistent equality within a single enum.

Ordering Enums: When IntEnum Helps

The base Enum does not support ordering operators like < and >, so it cannot be used with sorted() directly. If you try to sort a list of Enum members, Python raises a TypeError. This is intentional; Enum values are not designed to be ordering keys, even when they are numbers.

IntEnum changes this by making each member an int subclass. Because integers have a natural ordering, IntEnum members can be compared with < and used in sorted():

python
from enum import IntEnum class Priority(IntEnum): LOW = 1 MEDIUM = 2 HIGH = 3 priorities = [Priority.MEDIUM, Priority.LOW, Priority.HIGH] print(sorted(priorities)) # [<Priority.LOW: 1>, <Priority.MEDIUM: 2>, <Priority.HIGH: 3>]

IntEnum also allows direct comparison to integers:

python
print(Priority.HIGH == 3) # True

This is convenient when you receive integer values from external systems. However, it also means your enum members can be used anywhere an int is expected, which can hide bugs if you accidentally mix types. Use IntEnum when you need numeric comparison or when your enum values must interoperate with integer APIs.

String Enums: StrEnum and String-Based Comparison

Python 3.11 introduced StrEnum, which is to strings what IntEnum is to integers. StrEnum members are str subclasses, so they compare directly to strings and support string methods:

python
from enum import StrEnum class Status(StrEnum): ACTIVE = "active" INACTIVE = "inactive" print(Status.ACTIVE == "active") # True print(Status.ACTIVE.upper()) # ACTIVE

This is useful when your enum values come from JSON payloads or configuration files that use string representations. Without StrEnum, you would have to call .value to get the underlying string, which adds noise and risks missing a conversion. StrEnum also supports ordering because strings are orderable, though alphabetical order may not always be semantically meaningful.

If you are on Python 3.10 or earlier, you can get the same equality and string-method behavior by combining str and Enum:

python
class Status(str, Enum): ACTIVE = "active" INACTIVE = "inactive"

This works but is less explicit than StrEnum. The StrEnum class also provides a cleaner __str__ implementation that returns the value rather than the member representation.

Flag Enums for Bitwise Combinations

Flag and IntFlag are designed for enums that represent bit flags. Members are combined using the | operator, and you can test whether a combination contains a specific flag using & or the in operator:

python
from enum import Flag class Permission(Flag): READ = 1 WRITE = 2 EXECUTE = 4 combined = Permission.READ | Permission.WRITE print(combined) # <Permission.READ|WRITE: 3> print(Permission.READ in combined) # True print(Permission.EXECUTE in combined) # False

Equality for Flag members is based on the integer value. Two different combinations are equal only if they represent the same set of bits. This makes Flag ideal for permission systems, protocol options, or any scenario where you need to combine independent boolean options.

IntFlag adds integer comparison on top of Flag, so you can compare a flag combination to an integer directly. This can be useful when passing flags to C libraries or network APIs that expect an integer bitmask.

Choosing the Right Enum Type

The decision depends on how you will use the enum and what external constraints exist. The table below summarizes the key differences:

Enum TypeInherits FromEqualityOrderingUse Case
EnumobjectIdentityNot supportedSimple named constants with no external value comparison
IntEnumint, EnumValue (int)SupportedNumeric values that must interoperate with integers
StrEnumstr, EnumValue (str)SupportedString values from external sources
FlagEnumValue (bitmask)Not directlyCombining independent boolean flags
IntFlagint, EnumValue (int/bitmask)SupportedBit flags that also need integer comparison

Use Enum when you only need named constants and never compare them to raw values. Use IntEnum when your enum values are inherently numeric and you need to sort or compare them to integers. Use StrEnum when your values are strings and you want to avoid calling .value repeatedly. Use Flag or IntFlag when you need bitwise combinations.

Common Pitfalls in Enum Comparison

One subtle issue is that IntEnum and StrEnum members compare equal to their underlying values, which can cause unexpected behavior in dictionaries and sets. For example, Priority.HIGH and 3 are considered equal, so they hash the same. This means you cannot use both as distinct keys in a dictionary:

python
d = {Priority.HIGH: "high", 3: "three"} print(d) # {<Priority.HIGH: 3>: 'three'} # The second entry overwrites the first

This happens because Priority.HIGH == 3 is True and they have the same hash. If you need to keep them separate, use the base Enum instead.

Another pitfall is relying on is for comparison when you have aliases. If you define two names with the same value, they become aliases of the same member:

python
class Color(Enum): RED = 1 CRIMSON = 1 print(Color.RED is Color.CRIMSON) # True

This is usually fine. The members are still singletons, so identity-based equality behaves consistently. If you override __eq__, be aware that you are changing the comparison semantics, not the singleton behavior.

Finally, be careful when comparing enum members from different enum classes. For plain Enum, members are distinct objects, so cross-class equality is False even when names and values match. With IntEnum, StrEnum, and IntFlag, members from different classes can compare equal based on their underlying integer or string values. If you need cross-enum equality for plain Enum, compare .value explicitly or convert both sides to a common representation.

Understanding these comparison semantics helps you select the right enum type and avoid subtle bugs that appear only when your code interacts with external data or performs sorting and hashing.

Try matching browser tools

All tools

Small utilities that fit Python work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Python guides

More in Python

All Python guides

The guides filed next to this one in Python, in library order.