[译]三个有用的python方法:dir, help 和 pprint

九月 29th, 2008 by Soloman | Print [译]三个有用的python方法:dir, help 和 pprint | 796 views

Python提供了三个方便的函数,可以使我们直接从解释器中就获得对象的文档。

  • dir - 列出一个对象的所有方法和属性
  • help - 用来显示文档
  • pprint - 非常棒的一个打印库,可以将对象的内容用易阅读的格式打印出来

下面的例子能让你更清楚它们的使用:

PythonWin 2.2.1 (#34, Apr 15 2002, 09:51:39)...
>>> import string
>>> dir(string)
['_StringType', '__builtins__', '__doc__',...]
>>> help(string)
Help on module string:

NAME
    string - A collection of string operations (most are no longer used in Python 1.6).

FILE
    d:\python22\lib\string.py

DESCRIPTION
    Warning: most of the code you see here isn’t normally used nowadays.  With
    Python 1.6, many of these functions are implemented as methods on the
    standard string object. 

...

>>> from pprint import pprint
>>> pprint(dir(string))
['_StringType',
 '__builtins__',
 '__doc__',
 '__file__',
...

简单且方便,不是吗?

本文翻译并转载自这里

, , ,

 

本文有2条评论

  1. slysm j0lv3 说: [回复]

    bpKFg, utnff , [url=http://www.autoe4hml5a.com]5p3ld[/url], http://www.ihuxsbtjuvg.com edyhat

添加评论 (支持Gravatar头像)

注意: 评论者允许使用'@user空格'的方式将自己的评论通知另外评论者。例如, ABC是本文的评论者之一,则使用'@ABC '(不包括单引号)将会自动将您的评论发送给ABC。使用'@all ',将会将评论发送给之前所有其它评论者。请务必注意user必须和评论者名相匹配(大小写一致)。

实时评论预览