Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中按变量定义的调用_Python_Function - Fatal编程技术网

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
我想这是复制品