I wrote a server that accepts requests and performs tasks.
- Each task is represented by a function.
- Each request has the name of the task.
I want to make a reference to the relevant functions by the name of the task that found in the request.
I saw that it works fine using eval()
and even if I implement it through a dictionary but is there another generic way to control the flow of the code?
def din():
print("din")
def david():
print("david")
request = {"TestName": "din"}
# option 1: eval
test_to_run = request['TestName']
eval(f"{test_to_run}()")
# option 2: dict
my_dict = {"din": din, "david": david}
my_dict[test_to_run]()