- [PYTHON] How to check if a value exists in an enum
- Isn’t Python constant in the first place?
- Enumeration notation
- Enums and loops
- List type
- Enum
- I think I can write if I come here
- bonus
- Postscript
- How To Check If A Value Exists In Python Enum
- Check if a value exists in an Enum in Python
- Use ‘value’ attribute
- Use the __member__ attribute
- Summary
[PYTHON] How to check if a value exists in an enum
You want to use a constant when you want to have a value for a specific thing. When dealing with constants in Python, I personally think it’s better to use ʻEnum` (enumeration type).
Isn’t Python constant in the first place?
That’s right. There is no strict constant, as any value can be overwritten depending on how you do it. But the official document says so.
Enumeration notation
from enum import Enum class Hoge(Enum): ONE = 1 TWO = 2 THREE = 3
Enums and loops
Since the enumeration type is a set of identifiers, I think there are few cases where the value itself given to the identifier is meaningful. If it is a list type, it can be easily output with a for statement, but if it is an enumeration type, it requires some ingenuity in writing.
List type
>>> lst = [1, 2, 3] >>> [l for l in lst] [1, 2, 3]
Enum
>>> from enum import Enum >>> class Hoge(Enum): . ONE = 1 . TWO = 2 . THREE = 3 . >>> [v.value for n, v in Hoge.__members__.items()] [1, 2, 3]
I think I can write if I come here
>>> from enum import Enum >>> class Hoge(Enum): . ONE = 1 . TWO = 2 . THREE = 3 . >>> [v.value for n, v in Hoge.__members__.items()] [1, 2, 3] >>> 1 in [v.value for n, v in Hoge.__members__.items()] True
It’s done. I don’t know where to use it.
bonus
What’s inside the enumerated __members__ ?
>>> Hoge.__members__ mappingproxy(< 'ONE': , 'TWO': , 'THREE': >)
What is MappingProxyType ? According to the official documentation
Read-only mapping proxy. Provides a dynamic view of mapping entries. This means that if the mapping changes, the view will reflect these changes.
I see(?) There is ʻitems ()` in this proxy. It looks like a dictionary when you take a quick look.
Postscript
I was told that there is a simpler way of writing than @shiracamus.
>>> 1 in [e.value for e in Hoge] True
>>> any(e.value == 1 for e in Hoge) True
It seems that you can write. Thank you! !! The first one is the level of why I didn’t notice .
I can get advice by writing an article, so I thought that output is important.
How To Check If A Value Exists In Python Enum
To check if a value exists in an Enum in Python, you can use the ‘value’ and the __ member __ attribute. Follow the article to better understand.
Check if a value exists in 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 representing a finite set. The Enums class generates the enumeration. An enumeration consists of names and data linked together.
from enum import Enum class FootballClubs(Enum): ManchesterUnited = 1 Liverpool = 2 ManchesterCity = 3 NottinghamForest = 4 # Printing enum member as a string print(FootballClubs.ManchesterUnited) # Use the "name" keyword to print out the name of the enum member print(FootballClubs.ManchesterUnited.name) # Use the "name" keyword to print out the value of the enum member print(FootballClubs.ManchesterUnited.value)
To check if a value exists in an Enum in Python. You can use the following ways:
Use ‘value’ attribute
- Import the module named “enum “.
- The Enums class generates enumerations. In this example, I created an enumeration named ‘FootballClubs’.
- Using the ‘value’ attribute to get the values in the ‘FootballClubs’ enum
- Use the in operator to check if a value exists in an Enum.
from enum import Enum class FootballClubs(Enum): ManchesterUnited = 1 Liverpool = 2 ManchesterCity = 3 NottinghamForest = 4 # Using the 'value' attribute to get the values in the 'FootballClubs' enum valuesClub = [club.value for club in FootballClubs] # The in operator checks if a value exists in an Enum print(1 in valuesClub) print(7 in valuesClub)
The value 1 is in the Enum, so it returns True. The value 7 is not in the Enum, so it returns False.
Use the __member__ attribute
- Import the module named “enum “.
- The Enums class generates enumerations. In this example, I created an enumeration named ‘FootballClubs’.
- Use the in operator with the _member_ attribute to check if a name exists in an Enum.
from enum import Enum class FootballClubs(Enum): ManchesterUnited = 1 Liverpool = 2 ManchesterCity = 3 NottinghamForest = 4 # Using the in operator with the _member_ attribute to check if a value exists in an Enum print('ManchesterUnited' in FootballClubs.__members__) print('Liverpool' in FootballClubs.__members__) print('RealMadrid' in FootballClubs.__members__)
Summary
Here are the methods that can help you check if a value exists in an Enum in Python. If you have any questions about this article, leave a comment below. I will answer your questions. Thank you 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