从Python 3.4开始,Enum该类存在.
我正在编写一个程序,其中一些常量有一个特定的顺序,我想知道哪种方式是比较它们最pythonic:
class Information(Enum):
ValueOnly = 0
FirstDerivative = 1
SecondDerivative = 2
Run Code Online (Sandbox Code Playgroud)
现在有一种方法,需要将给定information的Information与不同的枚举进行比较:
information = Information.FirstDerivative
print(value)
if information >= Information.FirstDerivative:
print(jacobian)
if information >= Information.SecondDerivative:
print(hessian)
Run Code Online (Sandbox Code Playgroud)
直接比较不适用于Enums,所以有三种方法,我想知道哪一种是首选的:
方法1:使用值:
if information.value >= Information.FirstDerivative.value:
...
Run Code Online (Sandbox Code Playgroud)
方法2:使用IntEnum:
class Information(IntEnum):
...
Run Code Online (Sandbox Code Playgroud)
方法3:根本不使用枚举:
class Information:
ValueOnly = 0
FirstDerivative = 1
SecondDerivative = 2
Run Code Online (Sandbox Code Playgroud)
每种方法都有效,方法1有点冗长,而方法2使用不推荐的IntEnum类,而方法3似乎就是在添加Enum之前这样做的方式.
我倾向于使用方法1,但我不确定.
谢谢你的任何建议!