[英]How can I run 3 functions in python from a different script?
I have 3 functions in a python script that I would like to run at the same time but from another python script, for example:我在 python 脚本中有 3 个函数,我想同时运行它们,但来自另一个 python 脚本,例如:
def a():
print("\nphrase1")
def b():
print("\nphrase2")
def c():
print("\nphrase3")
I would like to run those 3 functions from a different file.我想从不同的文件中运行这 3 个函数。 Could anyone support me on that?
有人可以支持我吗?
I suggest you copy the program with the functions to the same folder as the program you want to run them我建议您将具有这些功能的程序复制到与您要运行它们的程序相同的文件夹中
from yourprogram import a, b, c
#code
a()
b()
c()
Suppose if all above function is inside module fun.py
then use below code snippet to run all of it -假设如果 function 以上的所有内容都在模块
fun.py
,那么使用下面的代码片段来运行所有这些 -
import fun
for i in dir(fun):
item = getattr(fun,i)
if callable(item):
item()
dir(fun) retrieves all the attributes of module fun. dir(fun) 检索模块 fun 的所有属性。 If the attribute is a callable object, call it.
如果属性是可调用的 object,则调用它。 Just a note, it will call everything which is callable in the fun module .
请注意,它将调用 fun 模块中可调用的所有内容。
Hope this answers your question.希望这能回答你的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.