是否可以使用命令行获取给定函数的详细帮助?

时间:2012-01-04 19:01:57

标签: python

我是一个新的Python用户:) 是否可以使用命令行获取给定函数的详细帮助?

6 个答案:

答案 0 :(得分:4)

正如python告诉你何时启动CLI:

Type "help", "copyright", "credits" or "license" for more information.
>>>

只要求help

help(help)
help(list)
help(locals)
import math
help(math)
help(math.atan2)

答案 1 :(得分:3)

考虑安装IPython。它不仅可以让您快速轻松地访问help doc strings

In [3]: os.path.expanduser?
Type:       function
Base Class: <type 'function'>
String Form:    <function expanduser at 0xb77ffe64>
Namespace:  Interactive
File:       /usr/lib/python2.7/posixpath.py
Definition: os.path.expanduser(path)
Docstring:
    Expand ~ and ~user constructions.  If user or $HOME is unknown,
    do nothing.

它还可以帮助您通过tab completion找出对象具有的属性/方法:

os.path.__name__                    os.path.ismount
os.path.__new__                     os.path.join
os.path.__package__                 os.path.lexists
os.path.__reduce__                  os.path.normcase
os.path.__reduce_ex__               os.path.normpath
os.path.__repr__                    os.path.os
os.path.__setattr__                 os.path.pardir
os.path.__sizeof__                  os.path.pathsep
os.path.__str__                     os.path.realpath
os.path.__subclasshook__            os.path.relpath
os.path._resolve_link               os.path.samefile
os.path._varprog                    os.path.sameopenfile
os.path.abspath                     os.path.samestat
os.path.altsep                      os.path.sep
os.path.basename                    os.path.split
os.path.commonprefix                os.path.splitdrive
os.path.curdir                      os.path.splitext
os.path.defpath                     os.path.stat
os.path.devnull                     os.path.supports_unicode_filenames
os.path.dirname                     os.path.sys
os.path.exists                      os.path.walk
os.path.expanduser                  os.path.warnings

In [4]: os.path.[TAB]

在Debian / Ubuntu上,可以使用

安装ipython
sudo apt-get install ipython

答案 2 :(得分:2)

如果您的意思是使用shell命令行,这是一个可能的解决方案:

python -c "help(help)"

答案 3 :(得分:0)

对于大多数模块:

Python 2.6.7 (r267:88850, Sep 23 2011, 00:28:08) 
....
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> help(os)

>>> print os.__doc__
OS routines for Mac, NT, or Posix depending on what system we're on.

This exports:
  - all functions from posix, nt, os2, or ce, e.g. unlink, stat, etc.
  - os.path is one of the modules posixpath, or ntpath
  - os.name is 'posix', 'nt', 'os2', 'ce' or 'riscos'
  - os.curdir is a string representing the current directory ('.' or ':')
  - os.pardir is a string representing the parent directory ('..' or '::')
  - os.sep is the (or a most common) pathname separator ('/' or ':' or '\\')
  .....
>>> 

答案 4 :(得分:0)

如果您的意思是Python命令提示符,那么:

help(whatever)

whatever是保留字:

help("whatever")

如果您指的是shell或Windows命令提示符,请使用pydoc:

C:\Python32>lib\pydoc.py json.dumps
Help on function dumps in json:

json.dumps = dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, s
eparators=None, default=None, **kw)
    Serialize ``obj`` to a JSON formatted ``str``.

    If ``skipkeys`` is false then ``dict`` keys that are not basic types
    (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped
    instead of raising a ``TypeError``.

    If ``ensure_ascii`` is false, then the return value can contain non-ASCII
    characters if they appear in strings contained in ``obj``. Otherwise, all
    such characters are escaped in JSON strings.

    If ``check_circular`` is false, then the circular reference check
    for container types will be skipped and a circular reference will
    result in an ``OverflowError`` (or worse).

    If ``allow_nan`` is false, then it will be a ``ValueError`` to
    serialize out of range ``float`` values (``nan``, ``inf``, ``-inf``) in
    strict compliance of the JSON specification, instead of using the
    JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``).

    If ``indent`` is a non-negative integer, then JSON array elements and
    object members will be pretty-printed with that indent level. An indent
    level of 0 will only insert newlines. ``None`` is the most compact
    representation.

    If ``separators`` is an ``(item_separator, dict_separator)`` tuple
    then it will be used instead of the default ``(', ', ': ')`` separators.
    ``(',', ':')`` is the most compact JSON representation.

    ``default(obj)`` is a function that should return a serializable version
    of obj or raise TypeError. The default simply raises TypeError.

    To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
    ``.default()`` method to serialize additional types), specify it with
    the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.

答案 5 :(得分:0)

您可以使用pydoc