In Python, Enumerations are implemented by using the enum module. The Enums has names and values. The Enums can be accessed by using names or values.
To use this module, we should import it using.
import enum
The Enum has some properties. These are −
- Enums can be displayed as string or repr format.
- The type() method can display the enum types
- There is name keyword, to show names of the enum members.
- Enums are iterable
Example Code
import enum class Rainbow(enum.Enum): VIOLET = 1 INDIGO = 2 BLUE = 3 GREEN = 4 YELLOW = 5 ORANGE = 6 RED = 7 print('The 3rd Color of Rainbow is: ' + str(Rainbow(3))) print('The number of orange color in rainbow is: ' + str(Rainbow['ORANGE'].value)) my_rainbow_green = Rainbow.GREEN print('The selected color {} and Value {}'.format(my_rainbow_green.name, my_rainbow_green.value))
Output
The 3rd Color of Rainbow is: Rainbow.BLUE The number of orange color in rainbow is: 6 The selected color GREEN and Value 4