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
what can be done to achieve this functionality.
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
what can be done to achieve this functionality.
You are printing the enum object. Use the
.value
attribute if you wanted just to print that:See the Programmatic access to enumeration members and their attributes section:
You could add a
__str__
method to your enum, if all you wanted was to provide a custom string representation:Demo:
I implemented access using the following
now I can just do
print D.x to get the print.