Enum Class in Python

Level 4152 keystrokes

This creates an enumeration using the Enum class. auto() automatically assigns sequential values to members.

Python code

# Import Enum base class and auto function
1from enum import Enum, auto
2
# Define enumeration class inheriting from Enum
3class Color(Enum):
# auto() assigns value 1
4 RED = auto()
# auto() assigns value 2
5 GREEN = auto()
# auto() assigns value 3
6 BLUE = auto()
7
# Print the enum member: Color.RED
8print(Color.RED)
# Print the name attribute: RED
9print(Color.RED.name)
# Print the value attribute: 1
10print(Color.RED.value)

Output

Color.RED
RED
1
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Python code and how often they appear.

  • (× 7
  • )× 7
  • .× 5
  • =× 3
  • ,× 1
  • :× 1

More Python examples at level 4

← Python typing practice