[Python] How to access Enum

How to access Enum

How did I encounter this?

I needed to implement an Enum to store frequently used value. 

Syntax

There are 2 way of accessing the values in Enum: by giving the left side value and by using '.' with the left side value

from enum import Enum
class Expectation(Enum):
    """Collection of the level of being excited about the game."""
    VERY_HIGH = 3
    HIGH = 2
    INTERESTED = 1
    NOTICING = 0

print(Expectation(3)) # returns Expectation.VERY_HIGH (by value)
print(Expectation.HIGH) # returns Expectation.VERY_HIGH (by name)

Comments