Use the value attribute in the enum if you want to get value in Python. This allows you to work with the underlying values of the enumerated constants defined in the Enum class.
Python enum get value example
A simple example code has defined the enum and prints the value.
from enum import Enum
class D(Enum):
x = 100
y = 200
print(D.x.value)
Output:
You could add a __str__
method to your enum, if all you wanted was to provide a custom string representation. It will also allow you to print it in that way also in lists.
from enum import Enum
class D(Enum):
def __str__(self):
return str(self.value)
x = 1
y = 2
print(D.x)
Output: 1
More examples
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print(Color.RED.value) # Output: 1
print(Color.GREEN.value) # Output: 2
print(Color.BLUE.value) # Output: 3
Do comment if you have any doubts or suggestions on this Python enum topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.