Calling functions by array index in Python

前端 未结 4 1895
庸人自扰
庸人自扰 2020-11-30 06:27

I have a bunch of functions in Python out1, out2, out3 etc. and would like to call them based on an integer I pass in.

def arryofPointersToFns (value):
             


        
4条回答
  •  佛祖请我去吃肉
    2020-11-30 07:09

    One can access methods through a dictionary:

    def one1():
        print("Method one1 called")
    def one2():
        print("Method one2 called")
    def one3():
        print("Method one3 called")
    methodDictionary = {1: one1, 2:one2, 3: one3}
    method1 = methodDictionary[1]
    method1()
    method2 = methodDictionary[2]
    method2()
    method3 = methodDictionary[3]
    method3()
    

提交回复
热议问题