Created
January 15, 2024 14:07
-
-
Save victory-sokolov/5372bd0bd446fd86bff7952c1bf55322 to your computer and use it in GitHub Desktop.
Python Enum from Dict
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from enum import Enum | |
# Define a dictionary | |
my_dict = { | |
"foo": 1, | |
"bar": 2, | |
"baz": 3, | |
} | |
# Create an Enum class by passing the dictionary to the Enum constructor | |
MyEnum = Enum("MyEnum", my_dict) | |
# Access the enum members using the dot notation | |
print(MyEnum.foo) # Output: 1 | |
print(MyEnum.bar) # Output: 2 | |
print(MyEnum.baz) # Output: 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment