`
shrek
  • 浏览: 72330 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
qja
社区版块
存档分类
最新评论

python 自省(摘自 dive into python)

阅读更多
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)


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics