分享

打印类方法的工具类 in Python

 java_laq小馆 2014-01-09

打印类方法的工具类 in Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# coding=utf-8
'''
Created on Mar 8, 2013
   
@author: goreycn
'''
   
def info(object, spacing=10,collapse=1):
    """打印方法和DOC字符串
       
    把 模块,类,列表,字典 或者 字符串."""
    methodList = [method for method in dir(objectif 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.__doc__
       
    li = []
    info(li,30)

运行结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
打印方法和DOC字符串
      
    把 模块,类,列表,字典 或者 字符串.
__add__                        x.__add__(y) <==> x+y
__class__                      list() -> new empty list list(iterable) -> new list initialized from iterable's items
__contains__                   x.__contains__(y) <==> y in x
__delattr__                    x.__delattr__('name') <==del x.name
__delitem__                    x.__delitem__(y) <==del x[y]
__delslice__                   x.__delslice__(i, j) <==del x[i:j] Use of negative indices is not supported.
__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]
__getslice__                   x.__getslice__(i, j) <==> x[i:j] Use of negative indices is not supported.
__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)
__reversed__                   L.__reversed__() -- return a reverse iterator over the list
__rmul__                       x.__rmul__(n) <==> n*x
__setattr__                    x.__setattr__('name', value) <==> x.name = value
__setitem__                    x.__setitem__(i, y) <==> x[i]=y
__setslice__                   x.__setslice__(i, j, y) <==> x[i:j]=y Use of negative indices is not supported.
__sizeof__                     L.__sizeof__() -- size of L in memory, in bytes
__str__                        x.__str__() <==str(x)
__subclasshook__               Abstract classes can override this to customize issubclass(). This is invoked early on by abc.ABCMeta.__subclasscheck__(). It should return TrueFalse or NotImplemented. If it returns NotImplemented, the normal algorithm is used. Otherwise, it overrides the normal algorithm (and the outcome is cached).
append                         L.append(object-- append object to end
count                          L.count(value) -> integer -- return number of occurrences of value
extend                         L.extend(iterable) -- extend list by appending elements from the iterable
index                          L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present.
insert                         L.insert(index, object-- insert object before index
pop                            L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range.
remove                         L.remove(value) -- remove first occurrence of value. Raises ValueError if the value is not present.
reverse                        L.reverse() -- reverse *IN PLACE*
sort                           L.sort(cmp=None, key=None, reverse=False-- stable sort *IN PLACE*cmp(x, y) --101

来自:http://hi.baidu.com/goreycn/item/41453b5134c48914db163503

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多