Since Python 3.4, the Enum
class exists.
I am writing a program, where some constants have a specific order and I wonder which way is the most pythonic to compare them:
class Information(Enum):
ValueOnly = 0
FirstDerivative = 1
SecondDerivative = 2
Now there is a method, which needs to compare a given information
of Information
with the different enums:
information = Information.FirstDerivative
print(value)
if information >= Information.FirstDerivative:
print(jacobian)
if information >= Information.SecondDerivative:
print(hessian)
The direct comparison does not work with Enums, so there are three approaches and I wonder which one is preferred:
Approach 1: Use values:
if information.value >= Information.FirstDerivative.value:
...
Approach 2: Use IntEnum:
class Information(IntEnum):
...
Approach 3: Not using Enums at all:
class Information:
ValueOnly = 0
FirstDerivative = 1
SecondDerivative = 2
Each approach works, Approach 1 is a bit more verbose, while Approach 2 uses the not recommended IntEnum-class, while and Approach 3 seems to be the way one did this before Enum was added.
I tend to use Approach 1, but I am not sure.
Thanks for any advise!