Computing a function name from another function name

前端 未结 2 1335
名媛妹妹
名媛妹妹 2021-01-29 05:16

In python 3.4, I want to be able to do a very simple dispatch table for testing purposes. The idea is to have a dictionary with the key being a string of the name of the functi

2条回答
  •  情深已故
    2021-01-29 05:57

    Abarnert's answer seems to be useful but to answer your original question of how to call all test functions for a list of function names:

    def test_f():
        print("testing f...")
    
    def test_g():
        print("testing g...")
    
    myTestList = ['f', 'g']
    
    for funcname in myTestList:
        eval('test_' + funcname + '()')
    

提交回复
热议问题