Python Enum Class - Explanation and Examples
Enum Class
Python Enum class is a set of symbolic names. The number of members of an Enum class are bound to be unique and constant values.
In this tutorial, we shall learn how to create an enum in your Python program, how to define the constants in it, access these constants and the datatypes we can assign.
Create an Enum class
To create an Enum in Python programming language, use syntax of Python Class and Enum class from enum module.
In the following example, we create an Enum class with three named integer constants.
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3Access values of Enum class
To access the values of an Enum Class, you cas use the Enum Class name with dot operator followed by the member.
In the following example, we shall access an Enum constant and print its value.
Python Program
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(Color.GREEN)Explanation
- The program imports the
Enumclass from theenummodule to create an enumeration calledColor. - The
Colorenumeration defines three colors:RED,GREEN, andBLUE, with corresponding values1,2, and3. - The
print(Color.GREEN)statement prints theColor.GREENmember of the enumeration. - The program outputs:
Color.GREEN, which is the name of the member in the enumeration.
Output
Color.GREENInformation about Enum member
To get more information about the Enum member, use repr() function.
Python Program
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(repr(Color.GREEN))Explanation
- The program imports the
Enumclass from theenummodule to create an enumeration calledColor. - The
Colorenumeration defines three colors:RED,GREEN, andBLUE, with corresponding values1,2, and3. - The
repr()function is used to print the string representation of theColor.GREENmember, which shows the name and value of the color. - The program prints the following:
Color.GREENwith value2.
Output
<Color.GREEN: 2>Enum with constants of different datatypes
You can define constants of any datatype in Enum. In the following example, we will define an Enum class with constants belonging to datatypes of Integer, String and Float.
Python Program
from enum import Enum
class Color(Enum):
RED = 1
GREEN = '#00FF00'
BLUE = 14.0
print(repr(Color.RED))
print(repr(Color.GREEN))
print(repr(Color.BLUE))Explanation
- The program imports the
Enumclass from theenummodule to create an enumeration calledColor. - The
Colorenumeration defines three colors:RED,GREEN, andBLUE, with values1,#00FF00, and14.0, respectively. - The
repr()function is used to print the string representation of each enumeration member, showing the name and value of the color. - The program prints the following:
Color.REDwith value1,Color.GREENwith value#00FF00, andColor.BLUEwith value14.0.
Output
<Color.RED: 1>
<Color.GREEN: '#00FF00'>
<Color.BLUE: 14.0>Summary
In this tutorial of Python Examples, we have learned how to create an Enum class, access members of Enum class and get some extra information about Enum class members.