As of Python 3.4, there is an Enum class.
I am writing a program where some constants have a certain order, and I wonder what path the most pythons are for comparing them:
class Information(Enum): ValueOnly = 0 FirstDerivative = 1 SecondDerivative = 2
Now there is a method that should compare the given information from information with various enumerations:
information = Information.FirstDerivative print(value) if information >= Information.FirstDerivative: print(jacobian) if information >= Information.SecondDerivative: print(hessian)
Direct comparison does not work with Enums, so there are three approaches, and I wonder which one is preferable:
Approach 1: Use Values:
if information.value >= Information.FirstDerivative.value: ...
Approach 2: Using IntEnum:
class Information(IntEnum): ...
Approach 3: Do not use enumerations at all:
class Information: ValueOnly = 0 FirstDerivative = 1 SecondDerivative = 2
Every approach works. Approach 1 is a bit more verbose, while approach 2 uses the not recommended IntEnum class, while approach 3 seems to be the same as it was before the addition of Enum.
I tend to use approach 1, but I'm not sure.
Thanks for the tips!
python enums compare
Sebastian werk
source share