pythonenums

How to get all values from python enum class?


I'm using Enum4 library to create an enum class as follows:

class Color(Enum):
    RED = 1
    BLUE = 2

I want to print [1, 2] as a list somewhere. How can I achieve this?


Solution

  • You can use IntEnum:

    from enum import IntEnum
    
    class Color(IntEnum):
       RED = 1
       BLUE = 2
    
    
    print(int(Color.RED))   # prints 1
    

    To get list of the ints:

    enum_list = list(map(int, Color))
    print(enum_list) # prints [1, 2]