Friday, December 27, 2024
Google search engine
HomeLanguagesenum in Python

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

  • 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.

What are the Advantages of Enum

Some of the advantages of using enums include:

  • Ease of maintenance: Enums centralize the definition of name values which makes it easier to upgrade or extend the set of values as per our requirements.
  • Readability and Self-Documentation: Enums provide meaningful names to values, making the code more human-readable and self-explanatory.
  • Type safety: Enums provide some level of type safety, ensuring that only valid values can be used.
  • Reduced risk of errors: Enums help prevent the use of incorrect or inconsistent values in your code, reducing the risk of bugs and errors.

Enum class in Python

Python code to demonstrate enumerations 

Python3




from enum import Enum
 
class Season(Enum):
    SPRING = 1
    SUMMER = 2
    AUTUMN = 3
    WINTER = 4
 
# printing enum member as string
print(Season.SPRING)
 
# printing name of enum member using "name" keyword
print(Season.SPRING.name)
 
# printing value of enum member using "value" keyword
print(Season.SPRING.value)
 
# printing the type of enum member using type()
print(type(Season.SPRING))
 
# printing enum member as repr
print(repr(Season.SPRING))
 
# printing all enum member using "list" keyword
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:

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

Python3




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.

Python3




import enum
 
# creating enumerations using class
class Animal(enum.Enum):
    dog = 1
    cat = 2
    lion = 3
 
# Hashing enum member as dictionary
di = {}
di[Animal.dog] = 'bark'
di[Animal.lion] = 'roar'
 
# checking if enum values are hashed successfully
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:

  • Identity:- These are checked using keywords “is” and “is not“.
  • Equality :- Equality comparisons of “==” and “!=” types are also supported.

Python3




# importing enum for enumerations
import enum
 
# creating enumerations using class
class 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

RELATED ARTICLES

Most Popular

Recent Comments