Istnieje doctstring example dla Sphinx w ich dokumentacji. W szczególności wykazują one następujące:
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
Choć poprosił o sphinx wyraźnie, chciałbym również wskazać na Google Python Style Guide. Ich przykład docstrujący wydaje się sugerować, że nie wywołują one specyficznie kwargów. (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 ma pytanie dotyczące przyjętej odpowiedzi na przedstawieniu dokumentacji zarządzania podproces. Jeśli importujesz moduł, możesz szybko zobaczyć dokumentację modułu przez inspect.getsource.
Przykład z interpretera Pythona z wykorzystaniem zalecenia SilentblokiSpółka Ghost:
>>> import subprocess
>>> import inspect
>>> import print inspect.getsource(subprocess)
Oczywiście można również przeglądać dokumentację modułu za pomocą funkcji pomocy. Na przykład help (podproces):
Nie jestem osobiście fanem podprocesowego docstringu dla kwargs jako przykładu, ale tak jak w przypadku przykładu Google, nie wyświetla ona kwargs oddzielnie, jak pokazano w przykładzie dokumentacji 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()
jestem tym to odpowiedź na A-B-B pytanie, bo warto zauważyć, że można przejrzeć źródła lub Documentation dowolny moduł w ten sposób do spostrzeżeń i inspiracji do komentowania kodu.
To zależy całkowicie od tego, z jakiej metody docletu korzystasz. (reStructuredText, Sphinx, Google) –