我有两个文件

test_def.py
def hi_test(a):
    return a

测试运行时间
from test_def import hi_test
a = 'hi'
b = 'test'
c = 'lion'

run = "{0}_{1}".format(a, b)
run1 = run(c)
print run1

它正在打印hi_test(lion),而不是执行/调用def函数。
有人能帮忙执行def函数吗?

最佳答案

import test_def
a = 'hi'
b = 'test'
c = 'lion'

run = "{0}_{1}".format(a, b)
run1 = getattr(test_def, run)(c)
print run1

关于python - 调用在python中通过变量定义,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29763901/

10-08 23:43