python - Executing a list of imported functions -
i have file functions:
modules.py:
def a(str): return str + ' a' def b(str): return str + ' b'
i want perform these functions in cycle. like:
main.py:
import modules modules_list = [modules.a, modules.b] hello = 'hello' m in modules_list: print m(hello)
the result should be:
>>> hello >>> hello b
this code work. not want use decorators, because functions in modules.py
. best way? thanks.
something this:
import modules hello = 'hello' m in dir(modules): obj = getattr(modules,m) if hasattr( obj, "__call__" ): #or use `if callable(obj):` print obj(hello)
output:
hello hello b
by way, don't use str
variable name, str
used name of built-in function in python.
Comments
Post a Comment