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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -