enum in Python
Enumerations in Python are implemented by using the module named “enum“.Enumerations are created using classes. Enums have names and values associated with them.
Properties of enum:
- Enums can be displayed as string or repr.
- Enums can be checked for their types using type().
- The “name” keyword is used to display the name of the enum member.
Example 1: Enum class in Python
Python code to demonstrate enumerations
Python3
from enum import Enumclass Season(Enum): SPRING = 1 SUMMER = 2 AUTUMN = 3 WINTER = 4# printing enum member as stringprint(Season.SPRING)# printing name of enum member using "name" keywordprint(Season.SPRING.name)# printing value of enum member using "value" keywordprint(Season.SPRING.value)# printing the type of enum member using type()print(type(Season.SPRING))# printing enum member as reprprint(repr(Season.SPRING))# printing all enum member using "list" keywordprint(list(Season)) |
Output:
Season.SPRING SPRING 1 <enum 'Season'> <Season.SPRING: 1> [<Season.SPRING: 1>, <Season.SUMMER: 2>, <Season.AUTUMN: 3>, <Season.WINTER: 4>]
Example 2: Accessing Modes
Enum members can be accessed in two ways:
- By value:- In this method, the value of enum member is passed.
- By name:- In this method, the name of the enum member is passed.
A separate value or name can also be accessed using the “name” or “value” keyword.
Python3
from enum import Enumclass Season(Enum): SPRING = 1 SUMMER = 2 AUTUMN = 3 WINTER = 4# Accessing enum member using valueprint("The enum member associated with value 2 is : ", Season(2).name)# Accessing enum member using nameprint("The enum member associated with name AUTUMN is : ", Season['AUTUMN'].value) |
Output:
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 loops
In this example, we will use for loop to print all the members of the Enum class.
Python3
from enum import Enumclass Season(Enum): SPRING = 1 SUMMER = 2 AUTUMN = 3 WINTER = 4for animals in (Season): print(animals.value,"-",animals) |
Output:
1 - Season.SPRING 2 - Season.SUMMER 3 - Season.AUTUMN 4 - Season.WINTER
Example 4: Enumerations support hashing
In this example, we will show how users can hash the Enum class that can be used in dictionaries or sets.
Python3
import enum# creating enumerations using classclass Animal(enum.Enum): dog = 1 cat = 2 lion = 3# Hashing enum member as dictionarydi = {}di[Animal.dog] = 'bark'di[Animal.lion] = 'roar'# checking if enum values are hashed successfullyif di == {Animal.dog: 'bark', Animal.lion: 'roar'}: print("Enum is hashed")else: print("Enum is not hashed") |
Output:
Enum is hashed
Example 5: Enumerations support two types of comparisons
- Identity:- These are checked using keywords “is” and “is not“.
- Equality :- Equality comparisons of “==” and “!=” types are also supported.
Python3
# importing enum for enumerationsimport enum# creating enumerations using classclass Animal(enum.Enum): dog = 1 cat = 2 lion = 3# Comparison using "is"if Animal.dog is Animal.cat: print("Dog and cat are same animals")else: print("Dog and cat are different animals")# Comparison using "!="if Animal.lion != Animal.cat: print("Lions and cat are different")else: print("Lions and cat are same") |
Output:
Dog and cat are different animals Lions and cat are different





.gif)

Please Login to comment...