Python 从 Enum 类返回 int

Joh*_*uls 5 python enums

目的是使用一个Enum类来允许对一定数量的选项进行可预测的输入。

我希望得到整数作为输出,但是class.attrib如果我打印它,我就会得到。

我怎样才能得到整数呢?

from enum import Enum

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

print(Stiffness.FLEXIBLE)
Run Code Online (Sandbox Code Playgroud)

输出:Stiffness.FLEXIBLE

预期输出:2

Thi*_*lle 7

只需打印value

print(Stiffness.FLEXIBLE.value)
# 2
Run Code Online (Sandbox Code Playgroud)

您可以查看枚举文档中有关枚举成员属性的部分