獲得對象可被調用的方法及方法文檔

def info(object, collapse=1, spacing=15):
    """
    使用更加格式化的方式來將
    object中可以被調用的方法,被調用的方法詳細的文檔說明打印出來

    collapse默認爲1,用於選擇分割字符串的方式,使processFun得值爲" ".join(s.split()),
    如果設爲0,則爲s
    """
    methodList = [method for method in dir(object)
                  if callable(getattr(object, method))]

    processFun = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)

    print('\n'.join(["%s %s" % ((method.ljust(spacing)),
                                processFun(str(getattr(object, method).__doc__)))
                     for method in methodList]
                    )
          )


if __name__ == '__main__':
    s = "123"
    info(s)

部分輸出結果:

__add__         Return self+value.
__class__       str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to 'strict'.
__contains__    Return key in self.
__delattr__     Implement delattr(self, name).
__dir__         Default dir() implementation.
__eq__          Return self==value.
__format__      Return a formatted version of the string as described by format_spec.
__ge__          Return self>=value.

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章