Computer >> Computer tutorials >  >> Programming >> Python

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 name of the enum is displayed using ‘name’ keyword.

  • Using type() we can check the enum types.

Example

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)

Output

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

Printing enum as an iterable

We can print the enum as an iterable list. In the below code we use a for loop to print all enum members.

Example

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)

Output

Running the above code gives us the following result −

The enum members are :
Days.Sun
Days.Mon
Days.Tue

Hashing enums

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.

Example

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'})

Output

Running the above code gives us the following result −

True

Accessing enums

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.

Example

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

Output

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

Comparing the enums is a sraight forward process, we use the comparison operator.

Example

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

Output

Running the above code gives us the following result −

Match
No Match