Python Return int from Enum class

Viewed 944

The aim is to use an Enum class to allow for predictable input of a definite amount of options.

I expected to get the integers as output, however I get class.attrib if I print it.

How can I get the integer instead?

from enum import Enum

class Stiffness(Enum):
    FREE = 0
    RIGID = 1
    FLEXIBLE = 2

print(Stiffness.FLEXIBLE)

Output: Stiffness.FLEXIBLE

Expected output: 2

2 Answers

You can use IntEnum instead of Enum.

This allows to use Stiffness.FLEXIBLE interchangeably with the integer 2, for example you can pass it to a function that expects an integer.

Related