suppose I have two files File1.py and then File2.py and the Multiple Functions Written in the Files, and then I have the main.py file where I am calling the File1.py functions using getattr builtin function. after reading one by one function of the File1.py now I want to call the File2.py Functions.
File1.py
def function1():
print('function 1')
def function2():
print('function 2')
def function3():
print('function 3')
File2.py
def function1():
print('function 1')
def function2():
print('function 2')
def function3():
print('function 3')
main.py
import File1
import File2
'''
here I want to call Both File Functions One By one simultaneously Using getattr function.
'''
CodePudding user response:
I'm not entirely sure if I got your question correct usually it helps if you show a bit of example code. But I believe the runpy function does what your asking for
import runpy runpy.run_path(path_name='Module_you_want_to_run.py')
And you will get a dict as output with all global variables of that module
CodePudding user response:
You can simply enumerate the module's attributes.
from inspect import isfunction
def call_all(module):
for name in dir(module):
if name[0]=="_": continue
fn = getattr(module,name)
if isfunction(fn):
fn()
call_all(File1)
call_all(File2)
