In this tutorial, we will learn about Python Enum.
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
Python provides you with the enum module that contains the Enum type for defining new enumerations. And you define a new enumeration type by subclassing the Enum class.
The following example shows how to create an enumeration called Color :
from enum import Enum class Color(Enum): RED = 1 GREEN = 2 BLUE = 3
How it works.
First, import the Enum type from the enum module:
from enum import Enum
Second, define the Color class that inherits from the Enum type:
Third, define the members of the Color enumeration:
RED = 1
GREEN = 2
BLUE = 3
Note that the enumeration’s members are constants. Therefore, their names are in uppercase letters by convention.
In this example, the Color is an enumeration. The RED, GREEN, and BLUE are members of the Color enumeration. They have associated values 1, 2, and 3. The type of a member is the enumeration to which it belongs.
The following illustrates the type of Color.RED is the Color enumeration :
An enumeration is a set of symbolic names bound to unique, constant values. Enumerations can be used to create simple custom data types which include things such as seasons, weeks, types of weapons in a game, planets, grades, or days. By convention, enumeration names begin with an uppercase letter and are singular.
The enum module is used for creating enumerations in Python. Enumerations are created with the class keyword or with the functional API.
There is specific derived enumerations enum.IntEnum, enum.IntFlag, and enum.Flag.
Python enum access from another file :
The users can access the members of the Enum class by using the value or the name of the member items.
In the following example, we will show how the users can access the value by name where we have used the name of the Enum as an index.
import enum # we will use enum class for creating enumerations class Days(enum.Enum): Sunday = 1 Monday = 2 Tuesday = 3 Wednesday = 4 Thursday = 5 Friday = 6 Saturday = 7 print('The member of Enum class accessed by name: ') print (Days['Monday']) print('The member of Enum class accessed by name: ') print (Days['Friday']) print('The member of Enum class accessed by Value: ') print (Days(1)) print('The member of Enum class accessed by Value: ') print (Days(5))
The member of Enum class accessed by name: Days.Monday The member of Enum class accessed by name: Days.Friday The member of Enum class accessed by Value: Days.Sunday The member of Enum class accessed by Value: Days.Thursday
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 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 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 to use 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.
Thank you for reading our content on “Python Enum”. If you have any queries feel free to contact us here