Python Central HubSign in

Python Programming / Python OOPS

Enums in Python

Learn about Enums in Python. Enum is a class in Python for creating enumerations, which are a set of symbolic names (members) bound to unique, constant values.

Enums in Python : Enumerate Your Values

Enums are a way to create a set of symbolic names (members) bound to unique, constant values. They are a way to define a set of named constants. Enums are defined using the enum module. Enumerations are created using classes. Enums have names and values associated with them. Enums are defined by creating a new class that inherits from Enum. Enums can be iterated over. Enums can be compared using == and is.

Syntax

The syntax for creating an Enum in Python is as follows:

Python
from enum import Enum
 
class EnumName(Enum):
    MEMBER1 = value1
    MEMBER2 = value2
    MEMBER3 = value3
    ...

In the above syntax:

  • EnumName is the name of the Enum.
  • MEMBER1, MEMBER2, MEMBER3, etc. are the members of the Enum.
  • value1, value2, value3, etc. are the values associated with the members.

Creating Enums

To create an Enum, you need to import the Enum class from the enum module. Then, you can create a new class that inherits from Enum. Each member of the Enum is defined as a class attribute. The value of the member is passed as an argument to the Enum constructor. The value of the member can be any type, such as an integer, string, or float.

Python
from enum import Enum
 
class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3

In the above code:

  • Color is the name of the Enum.
  • RED, GREEN, and BLUE are the members of the Enum.
  • 1, 2, and 3 are the values associated with the members.
  • The members of the Enum are accessed using the dot notation, e.g., Color.RED, Color.GREEN, Color.BLUE.
  • The members of the Enum can be compared using == and is.

Iterating Over Enums

Enums can be iterated over using a for loop. When you iterate over an Enum, you get the members of the Enum.

Python
from enum import Enum
 
class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3
 
for color in Color:
    print(color)

In the above code, the output will be:

cmd
C:/Users/username/desktop>python enum.py
Color.RED
Color.GREEN
Color.BLUE

Comparing Enums

Enums can be compared using the == and is operators. When you compare two Enums using the == operator, it compares the values of the Enums. When you compare two Enums using the is operator, it compares the memory addresses of the Enums.

Python
from enum import Enum
 
class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3
 
color1 = Color.RED
color2 = Color.RED
 
print(color1 == color2) # Output: True
print(color1 is color2) # Output: True

In the above code, the output will be:

cmd
C:/Users/username/desktop>python enum.py
True
True

Enum Members

You can access the members of an Enum using the dot notation. The members of an Enum are instances of the Enum class.

Python
from enum import Enum
 
class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3
 
print(Color.RED) 
print(Color.GREEN)
print(Color.BLUE)

In the above code, the output will be:

cmd
C:/Users/username/desktop>python enum.py
Color.RED
Color.GREEN
Color.BLUE

Accessing Enum Members

You can access the members of an Enum using the dot notation. The members of an Enum are instances of the Enum class.

Python
from enum import Enum
 
class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3
 
currentColor = Color.RED
print(type(currentColor))
print(currentColor.name)
print(currentColor.value)

In the above code, the output will be:

cmd
C:/Users/username/desktop>python enum.py
<enum 'Color'>
RED
1

In the above code:

  • currentColor is an instance of the Color Enum.
  • currentColor.name returns the name of the Enum member.
  • currentColor.value returns the value of the Enum member.

Accessing Enum Members using Iteration

You can access the members of an Enum using iteration. When you iterate over an Enum, you get the members of the Enum.

Python
from enum import Enum
 
class Color(Enum):
    RED = 1
    GREEN = 2
    BLUE = 3
 
for color in Color:
    print(color.name, color.value)

In the above code, the output will be:

cmd
C:/Users/username/desktop>python enum.py
RED 1
GREEN 2
BLUE 3

A Real-World Example

Here is a real-world example of using Enums in Python. In this example, we define an Enum called Weekday that represents the days of the week. We then create a function that takes a Weekday Enum as an argument and prints the name of the day.

Python
from enum import Enum
 
class Weekday(Enum):
    MONDAY = 1
    TUESDAY = 2
    WEDNESDAY = 3
    THURSDAY = 4
    FRIDAY = 5
    SATURDAY = 6
    SUNDAY = 7
 
def print_day(day):
    print(day.name)
 
print_day(Weekday.MONDAY)
print_day(Weekday.FRIDAY)

In the above code, the output will be:

cmd
C:/Users/username/desktop>python enum.py
MONDAY
FRIDAY
sketch Enum members, aliases, and what equals what p5.js
Two things surprise people. A second name with the same value is not a new member -- it is an ALIAS, so it is missing from iteration and is the identical object as the original. And a plain Enum member does not compare equal to its own value: Color.RED == 1 is False, because the whole point is a distinct type. If you want it to equal the integer, that is what IntEnum is for. Every line here was run.

Conclusion

In this tutorial, you learned about Enums in Python. Enums are a way to create a set of symbolic names (members) bound to unique, constant values. Enums are defined using the enum module. Enums are created using classes. Enums have names and values associated with them. Enums can be iterated over. Enums can be compared using == and is. For more information on Enums, you can refer to the official documentation. For more tutorials on Python Central Hub.


Try it: Enum Exercises

Exercise 1 – Basic Enum

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 2 – Iterating Enum

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Exercise 3 – Enum Comparison

exerciseYour turn

Tab indents. Press Escape, then Tab, to leave the editor. Control plus Enter runs the code; Control plus Shift plus Enter submits it.

Output
Press Run to see what your code prints.

Was this page helpful?

Let us know how we did