PYTHON ENUM

Python Enum

Developers have created a module in python to create enumerations. Python enum is a kind of enumeration for the set of symbolic names that binds a constant value. We can import it in python by importing โ€œenumโ€ directly.

There are four classes in this module that has their different pair of names and value. These are enum, IntEnum, Flag, IntFlag. It also defines the โ€œunique()โ€ function which is a decorator and โ€œautoโ€ which is a helper.

How are the classes defined?

class enum.Enum – It is the base class for creating enumerated constants.

enum.IntEnum – Enumerated constants can use that are already a subclass of int.

enum.IntFlag – Base for creating enumerated constants that can be used with the bitwise operator. These are also a subclass of Int.

class enum.Flag – Base for creating enumerated constants combinable with the bitwise operator.

enum.unique() – enum decorator that tells us that only one name is bound with the value

enum.auto – An appropriate value of Enum replaces the instances using this function.

Properties of Python Enum

  1. The name of the enum can print using the keyword โ€œnameโ€.
  2. To check the type of enum, we run the type() function passing the required arguments.

Creating an Enum

Download enum by running the command pip install enum directly in the terminal. Import it and use the class syntax to create it. 

Note: Enum is not a stock python class even though we create them using the class syntax.

Python Enum vs Dictionary

Enum Dictionary
Enum is used to create a set of related constants.Dictionary is used to store data values in key:value pairs.
Enum is more similar to an array.The dictionary doesnโ€™t allow duplicates.

Python enum Comparison

The direct comparison never works with Enums, so there are three approaches:

  • Using Values
  • By using IntEnum
  • Not using Enums at all

Enum properties and implementation

1. We can print enum through an iterable list using for loop or while loop. The code provides below is an implementation.

import enum
 
class Days(enum.Enum) :
โ€‹Sun = 1
โ€‹Mon = 2
โ€‹Tue = 3
 
print("The enum are: ")
 
for i in(Days) :
โ€‹print(i);

Output :

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

2. The user can also use enums in dictionaries and sets since their members are hashable.

Below is an implementation:

Import enum
Class Days(enum.Enum):
Sun = 1
Mon = 2
Daytype = {}
Daytype[Days.Sun] = โ€˜Sunโ€™
Daytype[Days.Mon] = Moonโ€™
print(Daytype == {Days.Sun: โ€˜Sunโ€, Days.Mon:โ€™Moonโ€™})

Output :

True

3. Enum is accessible using the name or value in the member items.

import enum
 
class Days(enum.Enum) :
โ€‹Sun = 1
โ€‹Mon = 2
 
 
print ('accesing member by name:')
print(Days['Mon'])
 
 
print('accesing member by Value:')
print(Day(1))

Output :

accessing member by name:
Days.Mon
accessing member by Value:
Days.Sun

4. If you want to compare the enum process it is very quick using the comparison operator.

import enum
 
class Days(enum.Enum) :
โ€‹Sun = 1
โ€‹Mon = 2
โ€‹Tue = 1
 
if Days.Sun ==Days.Tue:
โ€‹print(โ€œEqualโ€)
 
if Days.Mon != Days.Tue:
โ€‹print(โ€œNot Equalโ€)
 

Output :

Equal
Not Equal