开发者

extract the number and name of python method arguments

开发者 https://www.devze.com 2022-12-12 02:00 出处:网络
How can I return the arguments of a function in a different module #Module: functionss.py def simple(a, b, c):

How can I return the arguments of a function in a different module

#Module: functionss.py
def simple(a, b, c):
    print "does something"


#Module: extract.py
#load the called开发者_开发问答 module and function
def get_args(module_name, function_name):
    modFile, modPath, modDesc = imp.find_module(module_name)
    mod = imp.load_module(module_name,modFile,modPath,modDesc)
    attr = getattr(mod, function_name)

#this is the part I don't get - how do I read the arguments
    return = attr.get_the_args()

 if __name__ == "__main__":
     print get_args("functions.py", "simple")

 #this would ideally print [a, b, c]


Use inspect.getargspec for the "heavy lifting" (introspecting a function).

Use __import__ to import a module (given its module name -- "functions.py" is a terrible way to specify a module name;-).

Use getattr(moduleobject, functionname) to get the function object given module object and function name.

0

精彩评论

暂无评论...
验证码 换一张
取 消