In this article, we are going to python Enum and class with examples, how to create, how to access enum elements, how to iterate over an enum, python enumerate string, How to convert Python Enum to string?, Python enum class, and many more,
What is Python Enum
The definition of an Enum or Enumeration is “An enumeration or enum is a set or collection of symbolic names (members) bound to unique, constant values.”In Python enums, we can compare the members for equality, we can iterate over them, and more. In Python, we have an Enum class that is used for creating enumerations. Let us see with the help of an example how to create an enum in Python.
Note-Enums are used to represent constants, so using UPPER_CASE names for enum members is recommended.
How to define Python Enum class
Enumerations are defined using the class syntax, which makes them easy to read and write. An alternative creation method is described in Functional API. To define an enumeration, subclass Enum as follows:
Program Example
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
Note: Please note that we use the class syntax to create Enums, but Enums are not like normal Python classes.
How to access all values of Python Enum?
We are looping the overall values of an enum using list comprehension and printing all values as a list.
Program Example
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
e_valu_list = [myenum.value for myenum in Color]
print('enum values:',e_valu_list)
Output
enum values: [1, 2, 3]
How to convert Python Enum to string
To print an enum member as a string we use the class and member name.
Program Example
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
print ("The enum member as a string is :\n ",end="")
print (Color.RED)
print (Color.BLUE)
print (Color.BLUE)
Output
The enum member as a string is :
Color.RED
Color.BLUE
Color.BLUE
How to access enum members in Python?
As you can see enum members have two information, name and a value. We can access the enum members by using the dot operator(.) with the enum class name. Here are some of the examples to show how enum type, name, representation can be accessed.
Program Example
from enum import Enum
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
# Accessing the enum member
print(Color.RED)
# checking enum member's repr information
print(repr(Color.RED))
# checking the type of enum member
print(type(Color.RED))
# check the name of enum member
print(Color.RED.name)
Output
Color.RED
<Color.RED: 1>
<enum 'Color'>
RED
How to Iterate over enum in Python?
As we explained in the definition of enums, Enums support iteration over its members. So let us see how we can iterate over a Python enum. We are going to use a simple for loop to iterate over an enum ( name, value)
Program code
from enum import Enum
class Color(Enum):
RED= 7
BLUE= 4
GREEN= 9
WHITE= 3
BLACK = 2
print('enum name and value :\n ')
for color in Color:
print(color.name ,':',color.value)
Output
enum name and value :
RED : 7
BLUE : 4
GREEN : 9
WHITE : 3
BLACK : 2
How to check member exist in Enum
Also if we want to check any member is present in an enum or not, we can do that check by using the isinstance() method like:
isinstance() : method will return true if the member is present in enum and false if it is not.
Program Example
from enum import Enum
class Color(Enum):
RED= 7
BLUE= 4
GREEN= 9
WHITE= 3
BLACK = 2
print(isinstance(Color.GREEN, Color))
Output
True
Rules for Enum definations
While defining enums, we need to make sure we can not declare two members that have the same names. If we do so this will give us an error. But on the other hand, we are allowed to use the same values for two different enum members. This restriction is only on the names and not values.
Not allowed in enums
Here is an example:it will give you error like TypeError: Attempted to reuse key: ‘RED’
class Color(Enum):
RED= 2
RED= 3
Allowed in enums
class Color(Enum):
RED= 2
BLUE= 2
How to Restrict Duplicate Value of Enum :unique decorator.
But if want to restrict these duplicate values also then we can do so by using the unique decorator. This decorator will ensure that all members used a unique value and if someone tries to use a duplicate value it will flag an error.
Here is an example:
from enum import Enum
@unique
class Color(Enum):
RED= 2
BLUE= 2