Use the Functional API if you want to dynamically define an Enum
:
import enum
Listoffruits = [('Fruit1', 'apple'), ('Fruit2', 'orange'), ('Fruit3', 'banana')]
Indication = enum.Enum('Indication', dict(Listoffruits))
You can use string formatting to "generate" the Python code:
def generate_enum(enumClass, enumDict):
"""
Generates python code for an Enum
"""
enum_template = """
@unique
class {enumClass}(Enum)
{enumBody}
"""
enumBody = '\n'.join([f" {name} = '{value}'" for (name,value) in enumDict.items()])
return enum_template.format(enumClass=enumClass,enumBody=enumBody)
print(generate_enum('Indication',dict(Listoffruits)))
The generated code will be:
@unique
class Indication(Enum)
Fruit1 = 'apple'
Fruit2 = 'orange'
Fruit3 = 'banana'