2

I have a program that I wrote that is creating and maintaining an array and I have another module that I wrote that has functions to manipulate the array. Is it possible to call every function in the imported module without having to hard code every function call? Meaning something like this:

#Some way I don't know of to get a list of function objects
    listOfFunctions = module.getAllFunctions()

    for function in listOfFunctions:
        array.function()

I want to do this so I don't have to update my main file every time I add a function to my manipulation module.

I found these:

How to call a function from every module in a directory in Python?

Is it possible to list all functions in a module?

listing all functions in a python module

and also only found listing the functions in a module on the python docs. I can think of a way to do this using some string manipulation and the eval() function but I feel like there has to be a better, more pythonic way

Community
  • 1
  • 1
user3282276
  • 3,674
  • 8
  • 32
  • 48
  • 2
    This smells like a really brittle way to hack in some unit testing. Why not hook up a legitimate unit test framework if that's what you're going after? Otherwise I don't quite understand what your intention is. – Cory Kramer Feb 17 '15 at 20:41
  • @Cyber I'm not using this for testing purposes so I'm not after a unit testing framework. If anything wouldn't I be running unit tests on the actual functions in the module I am importing to make sure those are functioning properly? – user3282276 Feb 17 '15 at 20:44
  • I would create a list in my module and then when I create a function, inside it, within the scope of the function I would add it to the list. This way the helper functions need not be in the list. getAllFunctions will then return the list. – user3885927 Feb 17 '15 at 20:55

2 Answers2

2

I think you want to do something like this:

import inspect

listOfFunctions = [func_name for func_name, func in module.__dict__.iteritems()\
                  if inspect.isfunction(func)]

for func_name in listOfFunctions:
    array_func = getattr(array, func_name)
    array_func()
mguijarr
  • 7,641
  • 6
  • 45
  • 72
2

When you import a module, the __dict__ attribute contains all the things defined in the module (variables, classes, functions, etc.). You can iterate over it and test if the item is a function. This can for example be done by checking for a __call__ attribute:

listOfFunctions = [f for f in my_module.__dict__.values()
                   if hasattr(f,'__call__')]

Then, we can call each function in the list by invoking the __call__ attribute:

for f in listOfFunctions:
    f.__call__()

But be careful! There is no guaranteed order to the dictionary. The functions will be called in a somewhat random order. If order is important, you might want to use a naming scheme that enforces this order (fun01_do_something, fun02_do_something, etc.) and sort the keys of the dictionary first.

Marijn van Vliet
  • 5,239
  • 2
  • 33
  • 45