Enum in Python

Enum is a class in python for creating enumerations, which are a set of symbolic names (members) bound to unique, constant values. The members of an enumeration can be compared by these symbolic anmes, and the enumeration itself can be iterated over. An enum has the following characteristics.
The enums are evaluatable string representation of an object also called repr().
The enums are evaluatable string representation of an object also called repr().
The name of the enum is displayed using ‘name’ keyword.
The name of the enum is displayed using ‘name’ keyword.
Using type() we can check the enum types.
Using type() we can check the enum types.
import enum
# Using enum class create enumerations
class Days(enum.Enum):
   Sun = 1
   Mon = 2
   Tue = 3
# print the enum member as a string
print ("The enum member as a string is : ",end="")
print (Days.Mon)

# print the enum member as a repr
print ("he enum member as a repr is : ",end="")
print (repr(Days.Sun))

# Check type of enum member
print ("The type of enum member is : ",end ="")
print (type(Days.Mon))

# print name of enum member
print ("The name of enum member is : ",end ="")
print (Days.Tue.name)
Running the above code gives us the following result −
The enum member as a string is : Days.Mon
he enum member as a repr is :
The type of enum member is :
The name of enum member is : Tue
We can print the enum as an iterable list. In the below code we use a for loop to print all enum members.
import enum
# Using enum class create enumerations
class Days(enum.Enum):
   Sun = 1
   Mon = 2
   Tue = 3
# printing all enum members using loop
print ("The enum members are : ")
for weekday in (Days):
   print(weekday)
Running the above code gives us the following result −
The enum members are :
Days.Sun
Days.Mon
Days.Tue
The members in an Enumeration are hashable, hence they can be used in dictionaries and sets. in the below example we see the hashing in action and check if the hashing is successful.
import enum
# Using enum class create enumerations
class Days(enum.Enum):
   Sun = 1
   Mon = 2
# Hashing to create a dictionary
Daytype = {}
Daytype[Days.Sun] = 'Sun God'
Daytype[Days.Mon] = 'Moon God'

# Checkign if the hashing is successful
print(Daytype =={Days.Sun:'Sun God',Days.Mon:'Moon God'})
Running the above code gives us the following result −
True
We can access the enum members by using the name or value of the member items. In the below example we first access the value by name where we use the name of the enu as an index.
import enum
# Using enum class create enumerations
class Days(enum.Enum):
   Sun = 1
   Mon = 2
print('enum member accessed by name: ')
print (Days['Mon'])
print('enum member accessed by Value: ')
print (Days(1))
Running the above code gives us the following result −
enum member accessed by name:
Days.Mon
enum member accessed by Value:
Days.Sun
Comparing the enums is a sraight forward process, we use the comparison operator.
import enum
# Using enum class create enumerations
class Days(enum.Enum):
   Sun = 1
   Mon = 2
   Tue = 1
if Days.Sun == Days.Tue:
   print('Match')
if Days.Mon != Days.Tue:
   print('No Match')
Running the above code gives us the following result −
Match
No Match