简体   繁体   中英

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:

def a():
       print("\nphrase1")
def b():
       print("\nphrase2")
def c():
       print("\nphrase3")

I would like to run those 3 functions from a different file. 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 -

import fun
for i in dir(fun):
    item = getattr(fun,i) 
    if callable(item):
        item()

dir(fun) retrieves all the attributes of module fun. If the attribute is a callable object, call it. Just a note, it will call everything which is callable in the fun module .

Hope this answers your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM