Python enum.IntEnum Class

The enum.IntEnum class in Python’s enum module allows you to create enumerations where the members are also subclasses of int. This means that the enumeration members can be compared to integers and used in places where integers are expected. This is particularly useful for scenarios where you need both the readability of enumeration members and the functionality of integers.

Table of Contents

  1. Introduction
  2. enum.IntEnum Class Syntax
  3. Examples
    • Basic Usage
    • Comparison with Integers
    • Using in Arithmetic Operations
    • Iterating Over IntEnum Members
    • Using IntEnum in Functions
  4. Real-World Use Case
  5. Conclusion

Introduction

The enum.IntEnum class allows you to define enumerations where members are also subclasses of int. This makes the enumeration members compatible with integer operations and comparisons, providing the benefits of both enums and integers.

enum.IntEnum Class Syntax

Here is how you define an enumeration with the enum.IntEnum class:

from enum import IntEnum

class EnumName(IntEnum):
    MEMBER1 = value1
    MEMBER2 = value2
    MEMBER3 = value3

Parameters:

  • EnumName: The name of the enumeration.
  • MEMBER: The symbolic name of the enumeration member.
  • value: The integer value associated with the enumeration member.

Examples

Basic Usage

Define a simple enumeration using IntEnum.

Example

from enum import IntEnum

class Day(IntEnum):
    SUNDAY = 1
    MONDAY = 2
    TUESDAY = 3
    WEDNESDAY = 4
    THURSDAY = 5
    FRIDAY = 6
    SATURDAY = 7

print(Day.MONDAY)
print(Day.MONDAY.name)
print(Day.MONDAY.value)

Output:

2
MONDAY
2

Comparison with Integers

Compare IntEnum members with integers.

Example

from enum import IntEnum

class Status(IntEnum):
    ACTIVE = 1
    INACTIVE = 0

status = Status.ACTIVE

if status == 1:
    print("Status is active")
else:
    print("Status is not active")

Output:

Status is active

Using in Arithmetic Operations

Use IntEnum members in arithmetic operations.

Example

from enum import IntEnum

class Level(IntEnum):
    LOW = 1
    MEDIUM = 2
    HIGH = 3

current_level = Level.MEDIUM
next_level = current_level + 1

print(f"Current level: {current_level}")
print(f"Next level: {next_level}")

Output:

Current level: 2
Next level: 3

Iterating Over IntEnum Members

Iterate over the members of an IntEnum enumeration.

Example

from enum import IntEnum

class Color(IntEnum):
    RED = 1
    GREEN = 2
    BLUE = 3

for color in Color:
    print(color)

Output:

1
2
3

Using IntEnum in Functions

Use IntEnum members as function arguments and return values.

Example

from enum import IntEnum

class Direction(IntEnum):
    NORTH = 1
    SOUTH = 2
    EAST = 3
    WEST = 4

def move(direction):
    if direction == Direction.NORTH:
        return "Moving north"
    elif direction == Direction.SOUTH:
        return "Moving south"
    elif direction == Direction.EAST:
        return "Moving east"
    elif direction == Direction.WEST:
        return "Moving west"

print(move(Direction.NORTH))
print(move(Direction.WEST))

Output:

Moving north
Moving west

Real-World Use Case

Representing Error Codes

Use an IntEnum to represent error codes in an application, allowing for easy comparison with integer values.

Example

from enum import IntEnum

class ErrorCode(IntEnum):
    SUCCESS = 0
    NOT_FOUND = 1
    PERMISSION_DENIED = 2
    UNKNOWN_ERROR = 99

def handle_error(code):
    if code == ErrorCode.SUCCESS:
        return "Operation successful"
    elif code == ErrorCode.NOT_FOUND:
        return "Resource not found"
    elif code == ErrorCode.PERMISSION_DENIED:
        return "Permission denied"
    elif code == ErrorCode.UNKNOWN_ERROR:
        return "An unknown error occurred"

print(handle_error(ErrorCode.SUCCESS))
print(handle_error(1))
print(handle_error(99))

Output:

Operation successful
Resource not found
An unknown error occurred

Conclusion

The enum.IntEnum class is used for creating enumerations that are also compatible with integers in Python. This allows you to leverage the readability and maintainability of enumerations while still being able to use them in integer operations and comparisons. This can be especially useful in scenarios like error handling, where you need to work with predefined sets of integer values.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top