In C you might do this to create an automatically numbered from 0 list of values for example:

typedef enum _SM_USER_MODE
{
    UM_POWERUP,
    UM_IDLE,
    UM_DO_SOMETHING
} SM_USER_MODE;

In python you can do the same it with the Enum class

from enum import Enum, auto

#Method 1
class UM(Enum):
    UM_POWERUP = 0
    UM_IDLE = auto()
    UM_DO_SOMETHING = auto()

#Method 2
UM = Enum('UM', ['UM_POWERUP', 'UM_IDLE', 'UM_DO_SOMETHING'], start=0)      #Use start=0 to stop Enum starting from 1

Using Enum values

    print(list(UM))

    print(UM.UM_POWERUP.value)
    print(UM.UM_DO_SOMETHING.value)

    #The value property can be omitted if the context doesn't require it, e.g.:
    current_mode = UM.UM_IDLE
    match (current_mode):
        case UM.UM_POWERUP:
            print("Do something A")
        case UM.UM_IDLE:
            print("Do something B")

        case UM.UM_DO_SOMETHING:
            print("Do something C")
        case _:
            print("Unknown")

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published. Required fields are marked *