Enum is the Python class that is used to create enumerations. Enumeration is a set of symbolic members or names that are bound to constant and unique values. Using these symbolic names, the members of the enumeration can be equated. The enumeration can be repeated over them by itself.
Properties of Enum Class:
- The type() method allows users to check the types of Enum.
- Users can display the name of the Enum by using the ‘name’ keyword.
- Enums can be shown as a string or as a repr.
enum in Python
Method #1: Enum class
Approach:
- Import Enum class of the enum module using the import keyword.
- Create a class say Months.
- Print the enum member as a string.
- Print the name of the enum member by using “name” keyword.
- Print the value of enum member by using the “value” keyword.
- Print the type of enum member using type() function.
- Print the enum member as repr using the repr() function.
- Print the list of all the enum members using the list() function.
- The Exit of the Program.
Below is the implementation:
# Import Enum class of the enum module using the import keyword from enum import Enum # Create a class say Months class Months(Enum): January = 1 Febraury = 2 March = 3 April = 4 # print the enum member as string print(Months.January) # print the name of the enum member by using "name" keyword print(Months.January.name) # print the value of enum member by using the "value" keyword print(Months.January.value) # print the type of enum member using type() function print(type(Months.January)) # print the enum member as repr using the repr() function print(repr(Months.January)) # print the list of all the enum members using the list() function print(list(Months))
Output:
Months.January January 1 <enum 'Months'> <Months.January: 1> [<Months.January: 1>, <Months.Febraury: 2>, <Months.March: 3>, <Months.April: 4>]
Method #2: Printing Enum as an Iterable list
Approach:
- Import Enum class of the enum module using the import keyword.
- Create an Enum class say Months (enum class is used for creating enumerations ).
- Loop in the above Enum class Months using the for loop.
- Print each member(month) of the Enum class.
- The Exit of the Program.
Below is the implementation:
# Import Enum class of the enum module using the import keyword from enum import Enum # Create a Enum class say Months # (enum class is used for creating enumerations ) class Months(Enum): January = 1 Febraury = 2 March = 3 April = 4 May = 5 June = 6 print("Enum class(Months) members: ") # Loop in the above Enum class Months using the for loop for month in (Months): # Print each member(month) of the Enum class print(month)
Output:
Enum class(Months) members: Months.January Months.February Months.March Months.April Months.May Months.June
Method #3: Accessing Modes of Enum
There are two ways to access enum members:
- By value: The value of an enum member is passed in this method.
- By name: The name of the enum member is passed in this method.
The “name” or “value” keywords can also be used to access a separate value or name.
Approach:
- Import Enum class of the enum module using the import keyword.
- Create an Enum class say Months (enum class is used for creating enumerations ).
- Apply name keyword on the enum class by passing some random value to it to access the enum member by its value.
- Apply value keyword on the enum class by passing some random name to it to access the enum member by its name.
- The Exit of the Program.
Below is the implementation:
# Import Enum class of the enum module using the import keyword from enum import Enum # Create a Enum class say Months # (enum class is used for creating enumerations ) class Months(Enum): January = 1 Febrauary = 2 March = 3 April = 4 May = 5 June = 6 # Apply name keyword on the enum class by passing some random value to it # to access the enum member by its value print("The name of Months class with the value 3 = ", Months(3).name) # Apply value keyword on the enum class by passing some random name to it # to access the enum member by its name print("The value of Months class with the name April = ", Months['April'].value)
Output:
The name of Months class with the value 3 = March The value of Months class with the name April = 4
Method #4: Hashing Enum Class
Members of the Enum class are also known as Enumeration and are hashable. As a result, these members can be utilized for sets and dictionaries.
Approach:
- Create an Enum class say Vehicle.
- Create a new empty dictionary for hashing enum member as dictionary
- Check whether the enum class values are hashed successfully or NOT using the if conditional statement.
- The Exit of the Program.
Below is the implementation:
import enum # Create a Enum class say Vehicle # (enum class is used for creating enumerations ) class Vehicle(enum.Enum): bike = 1 car = 2 train = 3 # Create a new empty dictionary for hashing enum member as dictionary dictionary = {} dictionary[Vehicle.bike] = 'Two wheeler' dictionary[Vehicle.car] = 'Four Wheeler' # Check whether the enum calss values are hashed successfully or NOT using the # if conditional statement if dictionary == {Vehicle.bike: 'Two wheeler', Vehicle.car: 'Four Wheeler'}: print("The Enum class is hashable") else: print("The Enum class is NOT hashable")
Output:
The Enum class is hashable
Method #5: Comparing the Enums
We can easily compare Enums by using the comparison operator.
- Identity operators- We can compare using the keywords “is” and “is not“.
- Equality operators- Equality comparisons can be made using “==” and “!=”.
Approach:
- Import enum module using the import keyword
- Create an Enum class say Vehicle (enum class is used for creating enumerations ).
- Compare the enum class members using the identity operator “is” in an if conditional statement.
- Compare the enum class members using the “!=” in an if conditional statement.
- The Exit of the Program.
Below is the implementation:
# import enum module using the import keyword import enum # Create a Enum class say Vehicle # (enum class is used for creating enumerations ) class Vehicle(enum.Enum): bike = 1 car = 2 train = 3 # Compare the enum class members using the identity operator "is" # in an if conditional statement if Vehicle.bike is Vehicle.car: print("Both bike and car are same vehicles") else: print("Both bike and car are NOT same vehicles") # Compare the enum class members using the "!=" # in an if conditional statement if Vehicle.bike != Vehicle.car: print("Both bike and car are NOT same vehicles") else: print("Both bike and car are same vehicles")
Output:
Both bike and car are NOT same vehicles Both bike and car are NOT same vehicles