查看Python帮助文档,可以用官方自带的:dir、help、__doc__。 查看对象属性,最佳方案不是dir,是一个开源替代工具pdir。

查看Python帮助文档,可以用官方自带的dir、help、doc等方式。

dir:

Cpython

import array
>>> dir(array.array)
['__add__', '__class__', '__contains__', '__copy__', '__deepcopy__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'buffer_info', 'byteswap', 'count', 'extend', 'frombytes', 'fromfile', 'fromlist', 'fromstring', 'fromunicode', 'index', 'insert', 'itemsize', 'pop', 'remove', 'reverse', 'tobytes', 'tofile', 'tolist', 'tostring', 'tounicode', 'typecode']

iPython

import array
In [22]: dir(array.array)
Out[22]:
['__add__',
 '__class__',
 '__contains__',
 '__copy__',
 '__deepcopy__',
 '__delattr__',
 '__delitem__',
 '__dir__',
 '__doc__',
 ...
 'itemsize',
 'pop',
 'remove',
 'reverse',
 'tobytes',
 'tofile',
 'tolist',
 'tostring',
 'tounicode',
 'typecode']

__doc__:

Cpython & iPython

import array
>>> array.array.__doc__
"array(typecode [, initializer]) -> array\n\nReturn a new array whose items are restricted by typecode, and\ninitialized from the optional initializer value, which must be a list,\nstring or iterable over elements of the appropriate type.\n\nArrays represent basic values and behave very much like lists, except\nthe type of objects stored in them is constrained. The type is specified\nat object creation time by using a type code, which is a sing...

help:

Cpython & iPython

import array
help(array.array)

Help on class array in module array:

class array(builtins.object)
 |  array(typecode [, initializer]) -> array
 |
 |  Return a new array whose items are restricted by typecode, and
 |  initialized from the optional initializer value, which must be a list,
 |  string or iterable over elements of the appropriate type.
 |
 |  Arrays represent basic values and behave very much like lists, except
 |  the type of objects stored in them is constrained. The type is specified
 |  at object creation time by using a type code, which is a single character.
 |  The following type codes are defined:
 |
 |      Type code   C Type             Minimum size in bytes
 |      'b'         signed integer     1
 |      'B'         unsigned integer   1
 |      'u'         Unicode character  2 (see note)
 |      'h'         signed integer     2
 |      'H'         unsigned integer   2
 |      'i'         signed integer     2
 ...

综合以上输出,在命令行下: iPython下的dir稍微还能看,但信息量不够;__doc__可以查看自己写的说明文档,文档不能太长否则没有可读性;help是以文件形式打开的帮助文档,最全的文档。

日常使用可以用dir的加强美化版pdir,简洁实用美观:

项目github是这里

作者自己的博客关于pdir的介绍: Python程序员需要一个更好的dir()!

安装:

pip install pdir2

Cpython & iPython输出:

import pdir
>>> pdir(array.array)

abstract class:
    __subclasshook__
arithmetic:
    __add__, __iadd__, __imul__, __mul__, __rmul__
attribute access:
    __delattr__, __dir__, __getattribute__, __setattr__
copy:
    __copy__, __deepcopy__
emulating container:
    __contains__, __delitem__, __getitem__, __iter__, __len__, __setitem__
object customization:
    __format__, __hash__, __init__, __new__, __repr__, __sizeof__, __str__
pickle:
    __reduce__, __reduce_ex__
rich comparison:
    __eq__, __ge__, __gt__, __le__, __lt__, __ne__
special attribute:
    __class__, __doc__
descriptor:
    itemsize: class getset_descriptor with getter, setter, deleter, the size, in bytes, of one array item
    typecode: class getset_descriptor with getter, setter, deleter, the typecode character used to create the array
function:
    append: Append new value v to the end of the array.
...

pdir不仅支持Cpython、iPython,还能在Jupyter Notebook下使用,此外还有搜索等功能,详细参见github文档。

Jupyter Notebook使用pdir截图:

如果不想每次打开命令行都import pdir,可以写到用户主目录下的.pythonstartup文件中,就可以自动加载了。

vim ~/.pythonstartup
添加: import pdir 保存即可