0
def function1(arguments):
    print("Function 1",arguments)

def function2(arguments):
    print("Function 2",arguments)

userInput = input() 

Is it possible for the user to enter a function and arguments and for said function to run. eg the user enters function2("Hello World")

1 Answer 1

1

Though you can always use eval to make this work but for reasons eval is evil, it is better to use a dictionary call back mechanism, notably

You can create a dictionary to bind the function with the names and call them with appropriate parameters

call_backs = {'function1': function1, 'function2': function2}

assuming you provide an input as follows function2, "Hello World",

You first need to split the data userInput = userInput .split(',') and pass it onto the callback function via the dictionary

call_backs[userInput[0]](userInput[1])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.