Python从枚举类返回int

2024-09-22 18:32:55 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我希望得到整数作为输出,但是如果我打印它,我会得到class.attrib。 我怎样才能得到整数呢?提前谢谢


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

print(Stiffness.FLEXIBLE)

#Output: Stiffness.FLEXIBLE

#Expected output: 2

Tags: 目的freeoutput数量选项整数enumclass
2条回答

您可以使用^{}代替Enum

这允许Stiffness.FLEXIBLE与整数2互换使用,例如,您可以将其传递给需要整数的函数

只需打印value

print(Stiffness.FLEXIBLE.value)
# 2

你可以看看the part of the enum documentation about attributes of enum members

相关问题 更多 >