本代码来源于深入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)