Home » Python Tutorial » Python Enum with Syntax and Examples

Python Enum with Syntax and Examples

Enum stands for Enumeration. The python enum was introduced in python 3.4. To create an enumeration, we can import the enum class in python. Enum is a set of symbolic names assigned with unique or constant values. We can assign the same values for different members in the python enum. Enums are iterable, and we can compare them with themselves. The properties are of enum as follows.

  1. We can convert and display python enum to string format.
  2. Type() function is used to check the type of enum.
  3. We can iterate the enumeration using loops.
  4. To display the name of the enum members, the name keyword is used.
  5. We can use the python dictionary or sets to achieve hashing in enum.

How to create an enum in python?

To create the enum in python, import the enum class in the python file and create a class to store the symbolic names.

Example:

from enum import Enum

class car(Enum):
   Lamborghini = 1
   Ferrari = 2
   Porsche = 3

print("The representation of enum is ",car.Lamborghini) #printing the result as a string
print("The name of car is ", car.Lamborghini.name)
print("The type of enum is ",type(car.Lamborghini))
print("The representation of enum is ",repr(car.Lamborghini)) #using repr function

Output

The representation of enum is car.Lamborghini
The name of car is Lamborghini
The type of enum is <enum 'car'>
The representation of enum is <car.Lamborghini: 1>

Accessing the enum:

We can access the enum values in two ways:

  1. Accessing the enum member by value.
  2. Accessing the enum member by name.

Example:

from enum import Enum

class car(Enum):
   Lamborghini = 1
   Ferrari = 2
   Porsche = 3

print("My favorite car is",car['Lamborghini'].name) #accessing member by name

print("I have a", car(3).name) #accessing member by value

Output

My favorite car is Lamborghini
I have a  Porsche

Iterable python enumerations:

We can iterate the python enum using loops. Consider the above example, using a for loop. We are going to iterate the enum.

Example:

from enum import Enum

class car(Enum):
   Lamborghini = 1
   Ferrari = 2
   Porsche = 3

print("The names of cars stored in an enum are :")
for i in (car):
   print(car(i).name)

Output

The names of cars stored in an enum are :
Lamborghini
Ferrari
Porsche

Comparing the members of the enum.

Comparing the members is very easy. Using the comparison operators, we can compare it. Also, the is and is not a keyword is an alternative to compare it. Let’s understand it with an example.

Example:

from enum import Enum

class car(Enum):
   Lamborghini = 1
   Ferrari = 2
   Porsche = 1
   Rollsroyce = 4

if(car.Lamborghini == car.Porsche):
   print("Both cars are of the same type")
if(car.Lamborghini != car.Rollsroyce):
   print("Both cars are of different types ")

Output

Both cars are of the same type
Both cars are of different types 

Using dictionaries to store the python enum members:

Using the dictionary to store the enum member is beneficial for comparison. By Using key-value pairs is quite simple to make operations on the enum data set.

Example:

from enum import Enum

class car(Enum):
   Lamborghini = 1
   Ferrari = 2
   Bentley = 1
   Rollsroyce = 4

mydict = {}

mydict[car.Lamborghini] = 'sports car'
mydict[car.Rollsroyce] = 'luxury car'
mydict[car.Bentley] = 'luxury car'

if(mydict == {car.Lamborghini, car.Rollsroyce}):
   print("Both cars are of the same type")
else:
   print("Both cars are of different types")

if(mydict == {car.Bentley, car.Rollsroyce}):
   print("Both cars are of different types ")
else:
   print("Both cars are of the same type")

Output

Both cars are of different types
Both cars are of the same type

FAQ’s

How to define enum in python?

To define an enum in python, import the enum package in the source file. Create a class and pass the object to it.

How to create enum in python?

Creating an enum is quite simple in python. From package enum import Enum class in the code. Make a class to store the enum members and pass the Enum class reference to it.

When to use enum in python?

When you have to create simple custom data like weekdays, months, school standards, etc., you can use enum in python.

Did you know?
1. Matrix In Python
2. Binary Search in Python
3. Polymorphism in Python
4. Python return
5. Python Strftime
6. Encapsulation in Python
7. Operator Overloading in Python
8. iloc in Python
9. Python Pop
10. Python Enum

Pin It on Pinterest