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
- The name of the enum can print using the keyword “name”.
- 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 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);
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)
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))
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”)
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.