Enumerations in Python are implemented by using the module named “enum“.Enumerations are created using classes. Enums have names and values associated with them.
Properties of enum:
1. Enums can be displayed as string or repr.
2. Enums can be checked for their types using type().
3. “name” keyword is used to display the name of the enum member.
The string representation of enum member is : Animal.dog The repr representation of enum member is : <Animal.dog: 1> The type of enum member is : <enum 'Animal'> The name of enum member is : dog
4. Enumerations are iterable. They can be iterated using loops
All the enum values are : Animal.dog Animal.cat Animal.lion Enum is hashed
Accessing Modes : Enum members can be accessed by two ways
1. By value :- In this method, the value of enum member is passed.
2. By name :- In this method, the name of enum member is passed.
Seperate value or name can also be accessed using “name” or “value” keyword.
Comparison : Enumerations supports two types of comparisons
1. Identity :- These are checked using keywords “is” and “is not“.
2. Equality :- Equality comparisons of “==” and “!=” types are also supported.
The enum member associated with value 2 is : Animal.cat The enum member associated with name lion is : Animal.lion The value associated with dog is : 1 The name associated with dog is : dog Dog and cat are different animals Lions and cat are different
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Python | Index of Non-Zero elements in Python list
- Reading Python File-Like Objects from C | Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Convert list to Python array
- Python | Merge Python key values to list
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to a Python Script
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to Python Libraries
- Python | Visualizing O(n) using Python
- gcd() in Python
- max() and min() in Python
- try and except in Python
- try-except vs If in Python