Python enum get name by value

Python enum get name by value

Last updated: Feb 18, 2023
Reading time · 4 min

banner

# Table of Contents

# Get Enum name by value in Python

To get an enum name by value, pass the value to the enumeration class and access the name attribute, e.g. Sizes(1).name .

When the value is passed to the class, we get access to the corresponding enum member, on which we can access the name attribute.

Copied!
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 print(Sizes(1).name) # 👉️ SMALL print(Sizes(2).name) # 👉️ MEDIUM print(Sizes(3).name) # 👉️ LARGE

The Sizes(1) syntax allows us to pass an integer to the class and get the corresponding enum member.

Copied!
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 value = 1 print(Sizes(value)) # 👉️ Sizes.SMALL print(Sizes(value).name) # 👉️ SMALL

This is useful when you don’t know the name of the enum member ahead of time (because it’s read from a file or fetched from an API).

Читайте также:  Get span title javascript

# Get a List of all Enum Values or Names in Python

Use a list comprehension to get a list of all of an enum’s values or names.

On each iteration, access the value or name attributes on the enum member to get a list of all of the enum’s values or names.

Copied!
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 values = [member.value for member in Sizes] print(values) # 👉️ [1, 2, 3] names = [member.name for member in Sizes] print(names) # 👉️ ['SMALL', 'MEDIUM', 'LARGE']

On each iteration, we access the value attribute to get a list of all of the enum’s values.

Similarly, we can access the name attribute on each iteration to get a list of all of an enum’s names.

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

You can use the same approach if you need to get a list of tuples containing the name and value of each enum member.

Copied!
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 result = [(member.name, member.value) for member in Sizes] # 👇️ [('SMALL', 1), ('MEDIUM', 2), ('LARGE', 3)] print(result)

The first element in each tuple is the name, and the second is the value of the enum member.

Use the in operator if you need to check if a value is in an enum.

Copied!
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 values = [member.value for member in Sizes] print(values) # 👉️ [1, 2, 3] if 2 in values: # 👇️ this runs print('2 is in values')

The in operator tests for membership. For example, x in l evaluates to True if x is a member of l , otherwise it evaluates to False .

You can use a simple for loop if you need to iterate over an enum.

Copied!
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 for size in Sizes: print(size) print(size.name, size.value)

# Getting Enum Names and Values in Python

To access an enum’s names and values:

  1. Use dot notation to access a specific enum member, e.g. Sizes.SMALL .
  2. Use the name and value properties to access the enum’s names and values.
Copied!
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 # ✅ Get Enum name print(Sizes.SMALL.name) # 👉️ SMALL # 👇️ access enum name by value print(Sizes(1).name) # 👉️ SMALL print(Sizes['SMALL'].name) # 👉️ SMALL # --------------------------------- # ✅ Get Enum value print(Sizes.SMALL.value) # 👉️ 1 print(Sizes['SMALL'].value) # 👉️ 1

As shown in the code sample, you can use the name and value properties on an enum member to get the enum’s name and value.

Copied!
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 print(Sizes.MEDIUM.name) # 👉️ MEDIUM print(Sizes.MEDIUM.value) # 👉️ 2

You can also use square brackets to access enum members.

Copied!
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 name = 'LARGE' print(Sizes[name].name) # 👉️ MEDIUM print(Sizes[name].value) # 👉️ 2

This is useful when you don’t know the name of the enum member ahead of time (because it’s read from a file or fetched from an API).

If you only have the value that corresponds to the enum member, pass it to the enumeration class and access the name attribute.

Copied!
from enum import Enum class Sizes(Enum): SMALL = 1 MEDIUM = 2 LARGE = 3 print(Sizes(1).name) # 👉️ SMALL print(Sizes(2).name) # 👉️ MEDIUM print(Sizes(3).name) # 👉️ LARGE

This is only necessary when you need to get the name of an enum member by its value.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

How To Get Enum Name By Value In Python

Get Enum name by value in Python

In this article, I will suggest how to get Enum name by value in Python using the ‘name’ attribute, the list comprehension, and the for loop. Hope you will read this article in its entirety.

Get Enum name by value in Python

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

To get Enum name by value in Python, you can use the following ways:

Use the ‘name’ attribute

  • Import the module named “enum”.
  • The Enums class generates enumerations. In this example, I created an enumeration named ‘FootballClubs’.
  • To get the names of Enum members, I use the ‘name’ attribute.
from enum import Enum class FootballClubs(Enum): ManchesterUnited = 1 Liverpool = 2 ManchesterCity = 3 NottinghamForest = 4 def getNamebyValue(val): # Use the "name" keyword to get the name of the enum member return FootballClubs(val).name print('Enum name by 1: ', getNamebyValue(1))
Enum name by 1: ManchesterUnited

Use list comprehension

  • Import the module named “enum “.
  • The Enums class generates enumerations. In this example, I created an enumeration named ‘FootballClubs’.
  • Use the list comprehension to get the name and value of the corresponding Enum member.
  • To get the names of Enum members, I use the ‘name’ attribute.
  • And to get the value, I use the ‘value’ attribute.
from enum import Enum class FootballClubs(Enum): ManchesterUnited = 1 Liverpool = 2 NottinghamForest = 4 # Value in the enum that you need to get the name val = 2 # Use the list comprehension to get the name of the corresponding Enum member nameOfEnum = [i.name for i in FootballClubs if i.value == val] print(nameOfEnum)

Use the for loop

  • Import the module named “enum “.
  • The Enums class generates enumerations. In this example, I created an enumeration named ‘FootballClubs’.
  • Use the for loop to get the name and value of the corresponding Enum member.
from enum import Enum class FootballClubs(Enum): ManchesterUnited = 1 Liverpool = 2 ManchesterCity = 3 NottinghamForest = 4 # Value in the enum that you need to get the name val = 3 # Use a for loop down to the members of the Enum for club in FootballClubs: if club.value == val: print(club.name)

Summary

So that’s all for how to get Enum name by value in Python. If you have any other way or questions about this, please leave a comment, and we will try to answer them. Thanks for reading!

Maybe you are interested:

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.

Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java

Источник

How To Get Enum Names In Python

How to get Enum names in Python

In this article, I will suggest how to get Enum names in Python using the ‘name’ attribute, the list comprehension, and the for loop. Hope you will read this article in its entirety.

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

How to get Enum names in Python

Use the ‘name’ attribute

  • Import the module named “enum”.
  • The Enums class generates enumerations. In this example, I created an enumeration named ‘footballClubs’.
  • To get the names of Enum members, I use the ‘name’ attribute.
from enum import Enum class footballClubs(Enum): ManchesterUnited = 1 Liverpool = 2 ManchesterCity = 3 NottinghamForest = 4 # Use the "name" attribute on a member to get the name of the enum print(footballClubs.ManchesterUnited.name) print(footballClubs.Liverpool.name) print(footballClubs.ManchesterCity.name) print(footballClubs.NottinghamForest.name)
ManchesterUnited Liverpool ManchesterCity NottinghamForest

You can access the name corresponding to the value by passing the value to the enumeration class and calling the name attribute.

from enum import Enum class footballClubs(Enum): ManchesterUnited = 1 Liverpool = 2 ManchesterCity = 3 NottinghamForest = 4 # Get the names of the enum by value. print('Corresponding enum member names:') print(footballClubs(1).name) print(footballClubs(2).name) print(footballClubs(3).name) print(footballClubs(4).name)
Corresponding enum member names: ManchesterUnited Liverpool ManchesterCity NottinghamForest

Use list comprehension

  • Import the module named “enum”.
  • The Enums class generates enumerations. In this example, I created an enumeration named ‘footballClubs’.
  • Use list comprehension to get Enum names.
  • To get the names of Enum members, I use the ‘name’ attribute.
from enum import Enum class footballClubs(Enum): ManchesterUnited = 1 Liverpool = 2 ManchesterCity = 3 NottinghamForest = 4 # Use the list comprehension to get Enum names nameOfEnum = [i.name for i in footballClubs] print(nameOfEnum)
['ManchesterUnited', 'Liverpool', 'ManchesterCity', 'NottinghamForest']

Use the for loop

  • Import the module named “enum”.
  • The Enums class generates enumerations. In this example, I created an enumeration named ‘footballClubs’.
  • Use the for loop to get Enum names.
from enum import Enum class footballClubs(Enum): ManchesterUnited = 1 Liverpool = 2 ManchesterCity = 3 NottinghamForest = 4 # Use a for loop to get Enum names for i in (footballClubs): print(i.name)
ManchesterUnited Liverpool ManchesterCity NottinghamForest

Summary

That’s what I would like to introduce to you how to get Enum names in Python. The critical point of all ways is that I always use the ‘name’ attribute to get the Enum name. Keep this attribute in mind. You will probably use it later. Hope the article is helpful to you. If you have any comments on the article, leave me a comment below.

Maybe you are interested:

My name is Jason Wilson, you can call me Jason. My major is information technology, and I am proficient in C++, Python, and Java. I hope my writings are useful to you while you study programming languages.

Name of the university: HHAU
Major: IT
Programming Languages: C++, Python, Java

Источник

Оцените статью