def info(object, spacing=10,collapse=1):
""" print methos and doc strings.
takes module,class,list,dictionary,or string. """
methodList = [method for method in dir(object) if callable(getattr(object,method))]
processFunc = collapse and (lambda s : "".join(s.split())) or (lambda s: s)
print "\n".join(["%s %s" %(method.ljust(spacing),
processFunc(str(getattr(object,method).__doc__)))
for method in methodList ])
if __name__ == "__main__" :
print info(dict)
""" print methos and doc strings.
takes module,class,list,dictionary,or string. """
methodList = [method for method in dir(object) if callable(getattr(object,method))]
processFunc = collapse and (lambda s : "".join(s.split())) or (lambda s: s)
print "\n".join(["%s %s" %(method.ljust(spacing),
processFunc(str(getattr(object,method).__doc__)))
for method in methodList ])
if __name__ == "__main__" :
print info(dict)
本文介绍了一个简单的Python函数definfo,该函数可以打印出传入对象(如模块、类、列表等)的所有可调用方法及其文档字符串。通过调整参数,可以控制输出格式。
276

被折叠的 条评论
为什么被折叠?



