Python string to enumeration
Converting a String to an Enumeration in Python
In Python, an enumeration is a data type used to define a collection of named constants. Enumeration constants remain unique throughout a program and offer improved readability and ease of use. Sometimes you may need to convert a string to an enumeration type. This article will detail how to do this in Python.
1. Defining an Enumeration
In Python, we can use the Enum
module to define enumeration types. Here’s a simple example:
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
The above code defines an enumeration type named Color
, which contains three enumeration constants: RED
, GREEN
, and BLUE
.
2. Converting Strings to Enumerations
In real-world development, we sometimes need to convert a string received from external input to the corresponding enumeration type. Here, we introduce two methods to achieve this.
Method 1: Using the __members__ Attribute of Enum
The first method uses the __members__ Attribute of Enum
. The specific steps are as follows:
# Define the enumeration type
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
# Convert the input string to the corresponding enumeration value
input_color = "GREEN"
enum_color = Color.__members__[input_color]
print(enum_color)
In the above code, we first define an enumeration type named Color
, then retrieve the corresponding enumeration value based on the input string "GREEN"
and print it out. The running result is as follows:
Color.GREEN
Method 2: Using the Enum
function of the enum
module
Another method is to use the Enum
function of the enum
module. The specific steps are as follows:
# Import the enum module
from enum import Enum
# Define the enumeration type
Color = Enum("Color", ["RED", "GREEN", "BLUE"])
# Convert the input string to the corresponding enumeration value
input_color = "BLUE"
enum_color = Color[input_color]
print(enum_color)
In the above code, we use the Enum
function to define an enumeration type named Color
. Then, based on the input string "BLUE"
, we retrieve the corresponding enumeration value and print it out. The running result is as follows:
Color.BLUE
3. Error Handling
When converting a string to an enumeration, if the input string is not one of the enumeration constants, a KeyError
exception will be thrown. To prevent the program from crashing, we can implement some error handling measures.
Using try
and except
# Define an enumeration type
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
# Convert the input string to the corresponding enumeration value
input_color = "YELLOW"
try:
enum_color = Color.__members__[input_color]
print(enum_color)
except KeyError:
print("The input color does not exist")
In the above code, we use the try
and except
statements to catch the KeyError
exception. If the input string is not in the enumeration constants, the program will output “The input color does not exist.”
4. Summary
This article introduced how to convert strings to enumeration types in Python. We can achieve this by using the __members__ property of Enum or the Enum function of the enum module. We also explain how to handle situations where the input string is not among the enum constants to avoid program crashes.