用Python打印出对象的函数和对应的帮助文档

本代码来源于深入Python

def info(object,spacing=10,collapse=1):
    """Print methods and doc strings.

     Takes module,clas,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)#被用于控制__doc__是否显示出来,使用到的是and or,相当于c语言中的条件三元运算符
    print("\n".join(["%s %s" %
                     (method.ljust(spacing),processFunc(str(getattr(object,method).__doc__)))
                     for method in methodList]))
  

if __name__=='__main__':
    print(info.__doc__)
一个很有用的方法,使用方法:


import apihelper
info(list)

打印结果如下:

__add__    x.__add__(y) <==> x+y
__class__  type(object) -> the object's type type(name, bases, dict) -> a new type
__contains__ x.__contains__(y) <==> y in x
__delattr__ x.__delattr__('name') <==> del x.name
__delitem__ x.__delitem__(y) <==> del x[y]
__dir__    __dir__() -> list default dir() implementation
__eq__     x.__eq__(y) <==> x==y
__format__ default object formatter
__ge__     x.__ge__(y) <==> x>=y
__getattribute__ x.__getattribute__('name') <==> x.name
__getitem__ x.__getitem__(y) <==> x[y]
__gt__     x.__gt__(y) <==> x>y
__iadd__   x.__iadd__(y) <==> x+=y
__imul__   x.__imul__(y) <==> x*=y
__init__   x.__init__(...) initializes x; see help(type(x)) for signature
__iter__   x.__iter__() <==> iter(x)
__le__     x.__le__(y) <==> x<=y
__len__    x.__len__() <==> len(x)
__lt__     x.__lt__(y) <==> x<y
__mul__    x.__mul__(n) <==> x*n
__ne__     x.__ne__(y) <==> x!=y
__new__    T.__new__(S, ...) -> a new object with type S, a subtype of T
__reduce__ helper for pickle
__reduce_ex__ helper for pickle
__repr__   x.__repr__() <==> repr(x)


转载于:https://my.oschina.net/u/170911/blog/125512

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值