在python中,如何通过从用户处获取函数名从另一个文件调用函数? 让我考虑一下,我有2个文件A.Py和B.Py

在python中,如何通过从用户处获取函数名从另一个文件调用函数? 让我考虑一下,我有2个文件A.Py和B.Py ,python,python-3.x,module,frontend,backend,Python,Python 3.x,Module,Frontend,Backend,a、 py包含函数 并且b.py包含代码 我想要的是,当用户输入addasfuncname和数据中的某个值时,就会调用adddata。您可以使用getattr 我会: from subprocess import call #Add your code here call(("python[replace this with 3 if it needs to run on Python3]"+data),shell=True) call函数基本上只运行用户在终端中输入的代码,就像您输入pyth

a、 py包含函数

并且b.py包含代码

我想要的是,当用户输入addasfuncname和数据中的某个值时,就会调用adddata。

您可以使用getattr

我会:

from subprocess import call
#Add your code here
call(("python[replace this with 3 if it needs to run on Python3]"+data),shell=True)
call函数基本上只运行用户在终端中输入的代码,就像您输入python然后输入函数一样


希望这有帮助

谢谢托尼,我只需要处理文件,虽然我得到了答案,但你的解决方案给了我新的见解。谢谢
import a
funcname=input("Enter function name")
data=int(input("Enter value to be sent")
import a
funcname=input("Enter function name")
data=int(input("Enter value to be sent"))
result = getattr(a, funcname)(data)
print(result)
from subprocess import call
#Add your code here
call(("python[replace this with 3 if it needs to run on Python3]"+data),shell=True)