Skip to content

Python Enum Member Access Error Fix

DodaTech Updated 2026-06-24 3 min read

In this tutorial, you'll learn about Python Enum Member Access Error Fix. We cover key concepts, practical examples, and best practices.

Python enums raise errors like "AttributeError: 'Color' has no attribute 'red'", "ValueError: 'green' is not a valid Color", or "TypeError: '>' not supported between instances of 'Color'" when member names are wrong, values are duplicated, or comparisons use unsupported operators.

The Problem

from enum import Enum

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

print(Color.red)  # lowercase

Error:

AttributeError: 'Color' has no attribute 'red'

Wrong Code

from enum import Enum

class Status(Enum):
    ACTIVE = 1
    INACTIVE = 2
    ACTIVE = 3  # Duplicate — overrides the first

print(Status.ACTIVE)  # Status.ACTIVE — but value is 3, not 1

Right Code

from enum import Enum

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

# Access by name (case-sensitive)
print(Color.RED)
print(Color.RED.name)
print(Color.RED.value)

Expected output:

Color.RED
RED
1

Step-by-Step Fix

Step 1: Use uppercase names for enum members

from enum import Enum

# Convention: uppercase for enum values
class Direction(Enum):
    NORTH = 1
    SOUTH = 2
    EAST = 3
    WEST = 4

# Access with correct casing
print(Direction.NORTH)

Step 2: Use @unique to prevent duplicates

from enum import Enum, unique

@unique
class Status(Enum):
    ACTIVE = 1
    INACTIVE = 2
    # ACTIVE = 3  ← ValueError if uncommented

try:
    class Bad(Enum):
        A = 1
        B = 1  # No error without @unique
except ValueError:
    pass

Step 3: Access by value

print(Color(1))      # Color.RED
print(Color(2))      # Color.GREEN
print(Color(3))      # Color.BLUE

# ValueError if value does not exist
try:
    print(Color(99))
except ValueError:
    print("99 is not a valid Color")

Step 4: Access by name

print(Color['RED'])    # Color.RED
print(Color['BLUE'])   # Color.BLUE

# KeyError if name does not exist
try:
    print(Color['YELLOW'])
except KeyError:
    print("YELLOW is not a Color member")

Step 5: Iterate over enum members

for member in Color:
    print(f"{member.name} = {member.value}")

Step 6: Use IntEnum for integer comparison

from enum import IntEnum

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

# IntEnum supports comparison
print(Priority.HIGH > Priority.LOW)  # True
print(Priority.MEDIUM == 2)          # True

Step 7: Use StrEnum for string values (Python 3.11+)

from enum import StrEnum

class Language(StrEnum):
    PYTHON = "python"
    JAVASCRIPT = "javascript"
    RUST = "rust"

print(Language.PYTHON.upper())  # PYTHON

Prevention Tips

  • Use uppercase names for enum members (convention)
  • Decorate with @unique to prevent accidental duplicate values
  • Use IntEnum when enum values need to be compared as integers
  • Access members with Class.MEMBER or Class['MEMBER'] syntax
  • Add a @staticmethod or @classmethod for lookup utilities

Common Mistakes with enum members

  1. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  2. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  3. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks

These mistakes appear frequently in real-world PYTHON code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What is the difference between Enum and IntEnum?

Enum members are distinct types — Color.RED == 1 is False. IntEnum members inherit from int, so Priority.HIGH > Priority.LOW and Priority.MEDIUM == 2 work. Use Enum for type safety and IntEnum when integer compatibility is needed.

How do I add methods to an Enum?

Enums can have methods like regular classes. Add instance methods for member-specific behavior and classmethods/staticmethods for lookup utilities. The __init__ method can store additional attributes per member.

How do I create an Enum from a string?

Use Color['RED'] to get an enum member from its name. Use a classmethod for custom lookup: @classmethod def from_str(cls, s): return cls[s.upper()]. For value lookup, use Color(value) directly.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro