我应该记录类似函数签名的参数吗? [英] Should I document parameters for similar function signatures?
问题描述
我有一些辅助函数,除了第一个参数外,它们采用与核心函数相同的参数.参数在核心函数中进行了详细记录.我是否也应该将此文档复制粘贴到辅助函数中,或者只是指向核心文档?
这很重要,我主要希望我的 API 参考被读取为 Sphinx 生成的 HTML,并且我使用 numpydoc 样式.我在
I have some helper functions that, except for the first argument, take the same arguments as the core function. The parameters are thoroughly documented in the core function. Should I copy-paste this documentation to the helper function too, or rather just point to the core documentation?
It that matters, I primarily intend my API reference to be read as HTML generated by Sphinx, and I use the numpydoc style. I didn't find an answer in the numpydoc manual.
EDIT
Here is an MWE:
def core(param0, param1=3, param2=8):
"""Core function with thorough documentation.
Parameters
----------
param0 : ndarray
Description.
param1 : int
Long description.
param2 : int
Long description.
Returns
-------
param_out : ndarray
Long description
"""
pass
def helper(param3, param1=3, param2=8):
"""Helper function.
"""
pass
As you can see, only the first parameter differs in the two functions.
The best, and easiest, way to do this is using Python Sphinx docstring sections from the sphinx.ext.napoleon
extension.
Only arguments unique to the helper function need to be explicitly documented, you can remit with a cross-reference to the function/method defining the shared parameters. The Google style guide for Python advises the same reasoning for overloading inherited methods from a base class :
A method that overrides a method from a base class may have a simple docstring sending the reader to its overridden method’s docstring, such as """See base class.""". The rationale is that there is no need to repeat in many places documentation that is already present in the base method’s docstring. However, if the overriding method’s behavior is substantially different from the overridden method, or details need to be provided (e.g., documenting additional side effects), a docstring with at least those differences is required on the overriding method.
Args:
List each parameter by name. A description should follow the name, and be separated by a colon and a space.
The following example:
def core(param0, param1=3, param2=8):
"""Core function with thorough documentation.
Parameters
----------
param0 : ndarray
Description.
param1 : int
Long description.
param2 : int
Long description.
Returns
-------
param_out : ndarray
Long description
"""
pass
def helper(param3, param1=3, param2=8):
"""Remaining
Parameters
----------
param3 : int
Description.
Other Parameters
A short string remitting reader to :py:func:`core` function.
See Also
A short string remitting reader to :py:func:`core` function.
Note
A short string remitting reader to :py:func:`core` function.
"""
pass
Would give this result:
这篇关于我应该记录类似函数签名的参数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!