Data Structures In Python 4

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
    Wed = 4
    Thru = 5
    Fri = 6
    Sat = 7
    
# 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 ("enum member as a repr is : ")
print (repr(Days.Sun))
print(repr(Days.Mon))

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


# print name of enum member
print ("The name of enum member is : ")
print (Days.Mon.name)
print (Days.Tue.name)

Output:

The enum member as a string is : Days.Mon
enum member as a repr is : 
<Days.Sun: 1>
<Days.Mon: 2>
The type of enum member is : 
<enum 'Days'>
<enum 'Days'>
The name of enum member is : 
Mon
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
    Wed = 4
    Thru = 5
    Fri = 6
    Sat = 7
    
# printing all enum members using loop
print ("The enum members are : ")
for weekday in (Days):
   print(weekday)

Output:

The enum members are : 
Days.Sun
Days.Mon
Days.Tue
Days.Wed
Days.Thru
Days.Fri
Days.Sat

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.

import enum
# Using enum class create enumerations
class Days(enum.Enum):
    Sun = 1
    Mon = 2
    Tue = 3
    Wed = 4
    Thru = 5
    Fri = 6
    Sat = 7
    
print('enum member accessed by name: ')
print (Days['Mon'])
print (Days['Wed'])
print (Days['Fri'])
print (Days['Sun'])
print (Days['Sat'])

print('enum member accessed by Value: ')
print (Days(1))
print (Days(2))
print (Days(3))
print (Days(4))
print (Days(5))
print (Days(6))
print (Days(7))

Output:

enum member accessed by name: 
Days.Mon
Days.Wed
Days.Fri
Days.Sun
Days.Sat
enum member accessed by Value: 
Days.Sun
Days.Mon
Days.Tue
Days.Wed
Days.Thru
Days.Fri
Days.Sat

Comparing the enums

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

Output:

Match
No Match