在他们的文档中有一个doctstring example 代表 Sphinx。具体来说,它们显示以下内容:
def public_fn_with_googley_docstring(name, state=None):
"""This function does something.
Args:
name (str): The name to use.
Kwargs:
state (bool): Current state to be in.
Returns:
int. The return code::
0 -- Success!
1 -- No good.
2 -- Try again.
Raises:
AttributeError, KeyError
A really great idea. A way you might use me is
>>> print public_fn_with_googley_docstring(name='foo', state=None)
0
BTW, this always returns 0. **NEVER** use with :class:`MyPublicClass`.
"""
return 0
虽然您明确询问了sphinx,但我也会指出Google Python Style Guide。他们的文档字符串示例似乎暗示他们没有专门调用 kwargs。 (other_silly_variable=None)
def fetch_bigtable_rows(big_table, keys, other_silly_variable=None):
"""Fetches rows from a Bigtable.
Retrieves rows pertaining to the given keys from the Table instance
represented by big_table. Silly things may happen if
other_silly_variable is not None.
Args:
big_table: An open Bigtable Table instance.
keys: A sequence of strings representing the key of each table row
to fetch.
other_silly_variable: Another optional variable, that has a much
longer name than the other args, and which does nothing.
Returns:
A dict mapping keys to the corresponding table row data
fetched. Each row is represented as a tuple of strings. For
example:
{'Serak': ('Rigel VII', 'Preparer'),
'Zim': ('Irk', 'Invader'),
'Lrrr': ('Omicron Persei 8', 'Emperor')}
If a key from the keys argument is missing from the dictionary,
then that row was not found in the table.
Raises:
IOError: An error occurred accessing the bigtable.Table object.
"""
pass
A-B-B 对引用子流程管理文档的公认答案有疑问。如果您导入一个模块,您可以通过 inspect.getsource 快速查看模块文档字符串。
使用 Silent Ghost 推荐的 Python 解释器示例:
>>> import subprocess
>>> import inspect
>>> import print inspect.getsource(subprocess)
当然您也可以通过帮助功能查看模块文档。例如帮助(子进程)
我个人并不喜欢以 kwargs 为例的子流程文档字符串,但与 Google 示例一样,它没有单独列出 kwargs,如 Sphinx 文档示例中所示。
def call(*popenargs, **kwargs):
"""Run command with arguments. Wait for command to complete, then
return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
"""
return Popen(*popenargs, **kwargs).wait()
我将这个答案包含在 A-B-B 的问题中,因为值得注意的是,您可以通过这种方式查看任何模块的源代码或文档,以获得评论代码的见解和灵感。