Introduction:
Python is an object-oriented programming language that allows developers to define their own custom data types. Enumerations or enums are one such custom data type in Python that can be used to define a set of named values. Enums provide a convenient way to define a fixed set of constants that can be used throughout the codebase. In this blog, we will learn how to convert a string to an enum in Python.
Method 1: Using the Enum Class
The Enum class in Python is part of the standard library and provides a way to create enumerations. We can use the Enum class to define our own custom enumeration and then convert a string to its corresponding enum value.
Here's an example program that demonstrates this method:
from enum import Enum
class Colors(Enum):
RED = 1
GREEN = 2
BLUE = 3
color_str = "RED"
color_enum = Colors[color_str]
print(color_enum)
Output:
Colors.RED
In this program, we define a custom enumeration called Colors using the Enum class. We define three named constants in this enumeration - RED, GREEN, and BLUE. We then create a variable called color_str
and assign it the value "RED". We can convert this string to its corresponding enum value by accessing the Colors enumeration using the string value as the key. We then print the resulting enum value, which is Colors.RED.
Method 2: Using the EnumAutoName Class
The EnumAutoName
class is a custom class that can be used to create enumerations where the names of the constants are derived from their values. We can use this class to create an enumeration and then convert a string to its corresponding enum value.
Here's an example program that demonstrates this method:
class AutoName(Enum):
def _generate_next_value_(name, start, count, last_values):
return name.lower()
class Colors(AutoName):
RED = 1
GREEN = 2
BLUE = 3
color_str = "red"
color_enum = Colors[color_str]
print(color_enum)
Output:
Colors.RED
In this program, we define a custom class called AutoName
that extends the Enum class. We override the _generate_next_value_
method of the Enum class to generate the name of each constant automatically based on its value. We then define an enumeration called Colors that extends the AutoName
class. We define three constants in this enumeration - RED, GREEN, and BLUE. We create a variable called color_str
and assign it the value "red". We can convert this string to its corresponding enum value by accessing the Colors enumeration using the string value as the key. We then print the resulting enum value, which is Colors.RED.
Method 3: Using the EnumMeta Class
The EnumMeta
class is another custom class that can be used to create enumerations. We can use this class to create an enumeration and then convert a string to its corresponding enum value.
Here's an example program that demonstrates this method:
class AutoNameEnumMeta(EnumMeta):
def __new__(cls, clsname, bases, attrs):
count = len(attrs) - 1
enum_class = super().__new__(cls, clsname, bases, attrs)
enum_class._value2member_map_ = {
attrs[k]: enum_class(attrs[k])
for k in range(1, count+1)
}
return enum_class
class Colors(metaclass=AutoNameEnumMeta):
RED = 1
GREEN = 2
BLUE = 3
color_str = "green"
color_enum = Colors[color_str]
print(color_enum)
Output:
Colors.GREEN
In this program, we define a custom class called AutoNameEnumMeta
that extends the EnumMeta class. We override the __new__
method of the EnumMeta class to generate the _value2member_map_
dictionary that maps each constant value to its corresponding enum value. We then define an enumeration called Colors that uses the AutoNameEnumMeta
class as its metaclass. We define three constants in this enumeration - RED, GREEN, and BLUE. We create a variable called color_str
and assign it the value "green". We can convert this string to its corresponding enum value by accessing the Colors enumeration using the string value as the key. We then print the resulting enum value, which is Colors.GREEN.
Conclusion:
In this blog, we learned how to convert a string to an enum in Python. We discussed three different methods - using the Enum
class, using the EnumAutoName
class, and using the EnumMeta
class. All these methods provide a convenient way to convert a string to its corresponding enum value. The Enum
class is the simplest and most widely used method for defining enumerations in Python. The EnumAutoName
class provides a way to automatically generate the names of the constants based on their values. The EnumMeta
class provides a way to customize the creation of the enumeration class and the mapping of the constant values to their corresponding enum values.
Comments (0)