The Wayback Machine - https://web.archive.org/web/20230308005238/https://www.geeksforgeeks.org/enum-in-python/
Skip to content
Related Articles
Open in App
Not now

Related Articles

enum in Python

Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 16 Aug, 2022
Improve Article
Save Article

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 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>]

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

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 Enum
 
class Season(Enum):
    SPRING = 1
    SUMMER = 2
    AUTUMN = 3
    WINTER = 4
 
for 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 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

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

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!