python中按变量定义的调用
我有两个文件python中按变量定义的调用,python,function,Python,Function,我有两个文件 test_def.py def hi_test(a): return a test_run.py from test_def import hi_test a = 'hi' b = 'test' c = 'lion' run = "{0}_{1}".format(a, b) run1 = run(c) print run1 from test_def import hi_test a = 'hi' b = 'test' c = 'lion' run = "{0}_
test_def.py
def hi_test(a):
return a
test_run.py
from test_def import hi_test
a = 'hi'
b = 'test'
c = 'lion'
run = "{0}_{1}".format(a, b)
run1 = run(c)
print run1
from test_def import hi_test
a = 'hi'
b = 'test'
c = 'lion'
run = "{0}_{1}".format(a, b)
exec("%s('%s')"%(run, c))
它正在打印hi_testlion,而不是执行/调用def函数。
有人可以帮助执行def功能吗 test_def.py
import test_def
a = 'hi'
b = 'test'
c = 'lion'
run = "{0}_{1}".format(a, b)
run1 = getattr(test_def, run)(c)
print run1
def hi_test(a):
print a
test_run.py
from test_def import hi_test
a = 'hi'
b = 'test'
c = 'lion'
run = "{0}_{1}".format(a, b)
run1 = run(c)
print run1
from test_def import hi_test
a = 'hi'
b = 'test'
c = 'lion'
run = "{0}_{1}".format(a, b)
exec("%s('%s')"%(run, c))
虽然第一个答案更好但可以通过以下方法存档
import test_def
a = 'hi'
b = 'test'
c = 'lion'
run = "{0}_{1}".format(a, b)
run1 = getattr(test_def,run)
run2 = run1(c)
print run2
我想这是复制品