AdrMXR
AdrMXR

Reputation: 25

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?

Upvotes: 0

Views: 101

Answers (2)

Tabaene Haque
Tabaene Haque

Reputation: 574

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.

Upvotes: 1

NIKOLAOS ILIOPOULOS
NIKOLAOS ILIOPOULOS

Reputation: 28

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()

Upvotes: 1

Related Questions