enum in Python



Enumeration properties:

1. Enumerations can be displayed as string or repr .

2. Enumerations can be type checked using type () .

3. The keyword “ name ” is used to display the name of an enumeration member.

# Python code to demonstrate enumerations

  
# enum import for enums

import enum

 
# creating enums using the class

class Animal (enum.Enum):

dog = 1

cat = 2

  lion = 3

 
# print an enum member as a string

print ( " The string representation of enum member is: " , end = "")

print (Animal.dog)

 
# print enum element as re pr

print ( "The repr representation of enum member is: " , end = " ")

print ( repr (Animal.dog))

 
# print enumeration member type using type ()

print ( " The type of enum member is: " , end = "")

print ( type (Animal.dog))

 
# print member name and enum using the name keyword

print ( " The name of enum member is: " , end = "")

print (Animal.dog.name)

Output:

 The string representation of enum member is: Animal.dog The repr representation of enum member is: & lt; Animal.dog: 1 & gt; The type of enum member is: & lt; enum `Animal` & gt; The name of enum member is: dog 

4. Enumerations are repeatable . They can be repeated using loops

5. Enumerations support hashing . Enumerations can be used in dictionaries or sets .

# Python code to demonstrate enums
# iteration and hashing
# enum import for enums

import enum

  
# create enums using class

class Animal (enum.Enum):

dog = 1

 < / code> cat = 2

lion = 3

 
# print all enum members using a loop

print ( "All the enum values ​​are:" )

for Anim in (Animal):

  print (Anim)

 
# Hashing enum member as a dictionary

di = {}

di [Animal.dog] = `bark `

di [Animal.lion] = `roar`

  
# check if enumeration values ​​have been successfully hashed

if di = = {Animal.dog: `bark` , Animal.lion: `roar` }:

  print ( " Enum is hashed " )

else : print ( "Enum is not hashed" )

Output:

 All the enum values ​​are: Animal.dog Animal.cat Animal.lion Enum is hashed 

Access to modes: There are two ways to access Enum elements

1. By value : — This method is passed the value of an enumeration member.

2. By name : — This method passes the name of an enumeration member.

A single value or name can also be retrieved using the keyword " name " or " value ".

Comparison: Enumerations support two types of comparisons

1. Identity : — They are checked using the keywords " is " and " not ".

2. Equality : — Comparisons for equality of types " == " and "! = ".

# Python code to demonstrate enumerations
# Access and comparison

 
# enum import for enums

import enum

 
# create enums using the class

class Animal (enum.Enum):

dog = 1

  cat = 2

lio n = 3

  
# Access enum element using value

print ( " The enum member associated with value 2 is: " , end = " ")

print (Animal ( 2 ))

  
# Access enum element by name

print ( "The enum member associated with name lion is:" , end = "")

print (Anima l [ `lion` ])

  
# Enum member assignment

mem = Animal.dog

  
# Display value

print ( " The value associated with dog is: " , end = "")

print (mem.value)

 
# Display name

print ( "The name associated with dog is:" , end = "")

print (mem.name)

 
# Comparison using & quot; is & quot;

if Animal.dog is Animal.cat:

  print ( " Dog and cat are same animals " )

else : print ( "Dog and cat are different animals"

 
# Comparison using & quot ;! = & quot;

if Animal.lion! = Animal.cat:

print ( "Lions and cat are different" )

else : print ( "Lions and cat are same" )

Output:

 The enum member associated with value 2 is: Animal.cat The enum member associated with name lion is: Animal.lion The value associated with dog is: 1 The name associated with dog is: dog Dog and cat are different animals Lions and cat are different 

This article is courtesy of Manjit Singh . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.