自 Python 3.4 起,Enum
类已经存在。
我正在编写一个程序,在其中一些常量具有特定的顺序,我想知道比较它们的最佳 Pythonic 方法是什么:
class Information(Enum):
ValueOnly = 0
FirstDerivative = 1
SecondDerivative = 2
现在有一种方法,需要将给定的Information
的information
与不同的枚举值进行比较:
information = Information.FirstDerivative
print(value)
if information >= Information.FirstDerivative:
print(jacobian)
if information >= Information.SecondDerivative:
print(hessian)
直接比较 Enums 是行不通的,因此有三种方法可供选择,我想知道哪一种更好:
方法 1:使用 values:
if information.value >= Information.FirstDerivative.value:
...
方法2:使用IntEnum:
class Information(IntEnum):
...
方法三:完全不使用枚举:
class Information:
ValueOnly = 0
FirstDerivative = 1
SecondDerivative = 2
每种方法都有效,方法1稍微冗长一些,而方法2使用了不推荐的IntEnum类,而方法3似乎是在枚举添加之前完成此操作的方式。
我倾向于使用方法1,但我不确定。
谢谢任何建议!