-1

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.
'''

  • 1
    Less prosa, more code. [ask] & [mre]. [edit] your question and add context instead "suppose"s. – Patrick Artner Jan 11 '22 at 08:06
  • "here I want to..." I don't understand the description. What should *happen* as a result of doing this? "Using getattr function" Okay, so you have some idea of the tools that you expect to be useful here. Did you read the documentation? Did you try to write the code? What went wrong? – Karl Knechtel Jan 11 '22 at 08:25
  • @KarlKnechtel, actually I implemented the Code using only 1 File. I am working on A application where previously more than 1K functions were written in a single file and call by the getattr function. now, actually, I want to break down this too large file into multiple small files where I can debug them easily and improve the Application code complexity. – Qasimwarraich07 Jan 11 '22 at 08:42
  • Please edit the question. Better grammar helps tremendously. – Matthias Urlichs Jan 11 '22 at 11:45

2 Answers2

0

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

0

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)
Matthias Urlichs
  • 2,301
  • 19
  • 29