【问题标题】:extract the number and name of python method arguments提取python方法参数的数量和名称
【发布时间】:2009-11-19 04:48:36
【问题描述】:

如何在不同的模块中返回函数的参数

#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]

【问题讨论】:

标签: python function arguments


【解决方案1】:

inspect.getargspec 用于“繁重的工作”(内省函数)。

使用__import__ 导入模块(鉴于其模块名称 -- "functions.py" 是指定模块名称 的糟糕方法;-)。

使用getattr(moduleobject, functionname)获取给定模块对象和函数名的函数对象。

【讨论】:

    猜你喜欢
    • 2015-10-01
    • 2011-10-09
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 2012-10-19
    • 2010-09-18
    • 2010-09-17
    • 1970-01-01
    相关资源
    最近更新 更多