Skip to content

Instantly share code, notes, and snippets.

@victory-sokolov
Created January 15, 2024 14:07
Show Gist options
  • Save victory-sokolov/5372bd0bd446fd86bff7952c1bf55322 to your computer and use it in GitHub Desktop.
Save victory-sokolov/5372bd0bd446fd86bff7952c1bf55322 to your computer and use it in GitHub Desktop.
Python Enum from Dict
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