I have following enum defined
from enum import Enum
class D(Enum):
x = 1
y = 2
print(D.x)
now the printed value is
D.x
instead I wanted the enum's value to be print
1
Hhat can be done to achieve this functionality?
This question is related to
python
python-3.x
enums
python-3.4
I implemented access using the following
class D(Enum):
x = 1
y = 2
def __str__(self):
return '%s' % self.value
now I can just do
print(D.x)
to get 1
as result.
You can also use self.name
in case you wanted to print x
instead of 1
.
Source: Stackoverflow.com