Enumerations in Python are implemented by using the module named “enum“.Enumerations are created using classes. Enums have names and values associated with them. Show Properties of enum:
Example 1: Enum class in PythonPython code to demonstrate enumerations Python3
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 30 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 32 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 36 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 38 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER0 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER2 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER4 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER6
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER7 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER9
Enum is hashed0 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8 Enum is hashed2
Enum is hashed3 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8 Enum is hashed5
Enum is hashed6 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8 Enum is hashed8 Enum is hashed9 Dog and cat are different animals Lions and cat are different0
Dog and cat are different animals Lions and cat are different1 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8 Enum is hashed8 Dog and cat are different animals Lions and cat are different4 Dog and cat are different animals Lions and cat are different0
Dog and cat are different animals Lions and cat are different6 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8 Enum is hashed8 Dog and cat are different animals Lions and cat are different9 from 0Output: Season.SPRING SPRING 1 <enum 'Season'> <Season.SPRING: 1> [<Season.SPRING: 1>, <Season.SUMMER: 2>, <Season.AUTUMN: 3>, <Season.WINTER: 4>] Example 2: Accessing ModesEnum members can be accessed in two ways:
A separate value or name can also be accessed using the “name” or “value” keyword. Python3
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 30 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 32 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 36 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 38 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER0 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER2 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER4 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER6
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8 Enum is hashed8 import 6import 7The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 38 import 9
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8 Enum is hashed8 Enum 3Enum 4Enum 5Enum 6Output: The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 3 Example 3: Enumerations are iterable. They can be iterated using loopsIn this example, we will use for loop to print all the members of the Enum class. Python3
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 30 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 32 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 34 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 36 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 38 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER0 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER2 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER4 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 33 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER6
The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 309 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 310 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 311 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 312 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 31 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER8 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 315 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 316 The enum member associated with value 2 is : SUMMER The enum member associated with name AUTUMN is : 317 Output: 1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER Example 4: Enumerations support hashingIn this example, we will show how users can hash the Enum class that can be used in dictionaries or sets. |