提取python方法参数的数量和名称
如何返回不同模块中函数的参数提取python方法参数的数量和名称,python,function,arguments,Python,Function,Arguments,如何返回不同模块中函数的参数 #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)
#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]
用于“重载”(自省功能)
使用\uuuu import\uuuuu
导入模块(给定其模块名称-“functions.py”
是指定模块名称的糟糕方法;-)
使用
getattr(moduleobject,functionname)
获取给定模块对象和函数名称的函数对象。读取源代码有什么问题?这是Python——你有了源代码,只要读一下就可以了。是什么阻止了这一点?此外,help()
函数有什么问题?这通常会告诉你一切。这有什么不对?看到了吗