A function that calls X function

后端 未结 4 398
借酒劲吻你
借酒劲吻你 2021-01-17 05:41

I want to basically turn a list element into a function with the do function. This way any pre-written funcction i can call by just use a do(list[x]).

What im tryin

4条回答
  •  别那么骄傲
    2021-01-17 06:40

    As the people above have said the best way is to define a dictionary of functions, as functions are objects in python this is possible

    def One():
       pass
    def Two():
       pass
    functions = {"ONE":One, "TWO":Two}
    

    You can then call it like this:

    functions[input]()
    

    If you want to give true control to the user (And I DO NOT recommend doing this) you could use the eval function.

    eval(input+"()")
    

提交回复
热议问题