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 with different pairs of names and values. These are enum, IntEnum, Flag, IntFlag. It also defines the β€œunique()” function which is a decorator and β€œauto” which is a helper.

Python Enum features

  • The user can display the Enums “string”Β or repr.
  • Β Enums can be checked for their types usingΒ type()
  • The user can use the β€œname” keyword for displaying the name of the enum member.

How are the classes defined?

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

enum.IntEnum – Enumerated constants can use that is 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 tells us that only one name is present 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 Animal(enum.Enum):
    Tiger = 1
    Lion = 2
    Leopard = 3
print (Animal.Tiger)
print ("The representation of enum member will be : ",end="")
print (repr(Animal.Tiger))
print ("The type of enum member is : ",end ="")
print (type(Animal.Tiger))
print ("The name of enum member is : ",end ="")
print (Animal.Tiger.name)

Output :

Animal.Tiger
The representation of enum member will be : <Animal.Tiger: 1>
The type of enum member is : <enum 'Animal'>
The name of enum member is : Tiger
> 

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(Days(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

Accessing modes of Python Enum

  • By Value: The value of enum member will be pass in this accessing mode
  • By Name: The name of enum member will be pass in this accessing mode
  • Identity: It is used as ‘is’ and ‘is not’ checks the identity of the keywords.
  • Equality: Equality will consist of β€œ==” and β€œ!=” types to check the equality of the keywords.