MasterHolbytla MasterHolbytla - 1 year ago 66
Python Question

Call a Python function with arguments based on user input

I would like to call a function from a user input, but include arguments in the parenthesis. For example, if I have a function that takes one argument:

def var(value):

I would like to ask the user for a command and arguments, then call the function with the arguments:

Input Command: var("Test")

Answer Source

Assuming you absolutely trust the input, just use eval

eval(str(input("Command: ")))

"str" is important for some variable types.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download