How To Convert An Integer To An Enum In Python

Convert an Integer to an Enum in Python

How to convert an integer to an Enum in Python? What ideas do you have for this topic? If not, I have some suggestions, let’s calling the enum class to pass integers in or creating a function to convert integers to enums with a try/except block. Read the article below for details.

Convert an Integer to an Enum in Python

Enum stands for enumeration, which is enumeration in Python. The point of Enum is that it is advantageous when representing data and a finite set. The Enums class generates the enumeration. An enumeration consists of names and data linked together.

Call the Enum class and pass an integer to the Enum class

Example:

  • Import the module named “enum”.
  • The Enums class generates enumerations. In this example, I created an enumeration named ‘Cars’.
  • To convert an integer to an enum, I call the class ‘Cars’ and pass the integer assigned to each ‘name’ of the ‘Cars’ object. Return values ​​are enum members.
from enum import Enum

class Cars(Enum):
    Audi = 1
    Toyota = 2
    Mercedes = 3
    Lexus = 4
    Hyundai = 5 
    
# Convert integer to enum. Return values are enum members
print(Cars(1))
print(Cars(2))
print(Cars(3))
print(Cars(4))
print(Cars(5))

Output:

Cars.Audi
Cars.Toyota
Cars.Mercedes
Cars.Lexus
Cars.Hyundai

Create a function that converts integers to enums

Example:

  • Import the module named “enum”.
  • The Enums class generates enumerations. In this example, I created an enumeration named ‘Cars’.
  • Initialize a function to convert integers to enums.
from enum import Enum

def convertIntToEnum(intValue):
    try:
        convertIntToEnum = Cars(intValue)
        print(convertIntToEnum.name)
    except:
        return None
      
class Cars(Enum):
    Audi = 1
    Toyota = 2
    Mercedes = 3
    Lexus = 4
    Hyundai = 5
    
# Call a function to convert integer values to enums
convertIntToEnum(1)
convertIntToEnum(2)
convertIntToEnum(3)
convertIntToEnum(4)
convertIntToEnum(5)

Output:

Audi
Toyota
Mercedes
Lexus
Hyundai

You can use the isinstance function to check whether members belong to Enum. 

Example:

  • Import the module named “enum”.
  • The Enums class generates enumerations. In this example, I created an enumeration named ‘Cars’.
  • Use the isinstance function to check if a member belongs to an Enum.
from enum import Enum

class Cars(Enum):
    Audi = 1
    Toyota = 2
    Mercedes = 3
    Lexus = 4
    Hyundai = 5
    
# Use the isinstance function to check whether members belong to Enum or not
print(isinstance(Cars.Audi, Cars))
print(isinstance(Cars.Toyota, Cars))

Output:

True
True

Summary

There are several ways to convert an integer to an Enum in Python. The first one is Call the Enum class and pass an integer to the Enum class, this is simple for you to do. If there is any problem with the article, please leave a comment to let us know. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *