Open In App

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. Let’s cover the different concepts of Python Enum in this article.

What are Enums and Why are they useful?

Enumerations or Enums is a set of symbolic names bound to unique values. It can be iterated over to return its canonical members in definition order. It provides a way to create more readable and self-documenting code by using meaningful names instead of arbitrary values.



Properties of Enum

What are the Advantages of Enum

Some of the advantages of using enums include:

Enum class in Python

Python code to demonstrate enumerations 



This code defines an enumeration class Season with four members: SPRING, SUMMER, AUTUMN, and WINTER. It showcases key properties of the enum, such as accessing an enum member, its name, and value. Additionally, it demonstrates how to obtain a list of all enum members. The output reflects the name, value, type, and a list of all Season enum members.




from enum import Enum
 
class Season(Enum):
    SPRING = 1
    SUMMER = 2
    AUTUMN = 3
    WINTER = 4
print(Season.SPRING)
print(Season.SPRING.name)
print(Season.SPRING.value)
print(type(Season.SPRING))
print(repr(Season.SPRING))
print(list(Season))

Output: 

Season.SPRING
SPRING
1
<enum 'Season'>
<Season.SPRING: 1>
[<Season.SPRING: 1>, <Season.SUMMER: 2>, <Season.AUTUMN: 3>, <Season.WINTER: 4>]


Accessing Modes 

Enum members can be accessed in two ways:

A separate value or name can also be accessed using the “name” or “value” keyword.

The code defines an enumeration class Season' with four members. It showcases how to access enum members by value and name. It demonstrates that you can obtain an enum member by specifying its value or name, and then access its name or value accordingly. This code provides examples for both value-based and name-based enum member access.




from enum import Enum
class Season(Enum):
    SPRING = 1
    SUMMER = 2
    AUTUMN = 3
    WINTER = 4
print("The enum member associated with value 2 is : ", Season(2).name)
print("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

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.

The code defines an enumeration class Season' with four members. It iterates through the enum members and prints their values and names. The output displays each enum member’s value and its fully-qualified name, providing a way to work with and display enum values.




from enum import Enum
 
class Season(Enum):
    SPRING = 1
    SUMMER = 2
    AUTUMN = 3
    WINTER = 4
 
for season in (Season):
    print(season.value,"-",season)

Output: 

1 - Season.SPRING
2 - Season.SUMMER
3 - Season.AUTUMN
4 - Season.WINTER

Enumerations Support Hashing

In this example, we will show how users can hash the Enum class that can be used in dictionaries or sets.

This code uses the enum' module to define an enumeration class Animal' with three members: dog, cat, and lion. It then creates a dictionary di and assigns values to it based on enum members. Finally, it checks if the dictionary matches a specific key-value pair.




import enum
class Animal(enum.Enum):
    dog = 1
    cat = 2
    lion = 3
di = {}
di[Animal.dog] = 'bark'
di[Animal.lion] = 'roar'
if di == {Animal.dog: 'bark', Animal.lion: 'roar'}:
    print("Enum is hashed")
else:
    print("Enum is not hashed")

Output:

Enum is hashed

Compare Enums in Python

Enumerations support two types of comparisons, that are:

This code defines an enumeration class Animal using the enum module with three members: dog, cat, and lion. It then performs comparisons between enum members to check for equality and inequality.




import enum
class Animal(enum.Enum):
    dog = 1
    cat = 2
    lion = 3
if Animal.dog is Animal.cat:
    print("Dog and cat are same animals")
else:
    print("Dog and cat are different animals")
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

Article Tags :