【python】Google 风格和 Numpy 风格 docstring

Google style and NumPy style are two conventions for writing docstrings in Python. They are designed to be readable and to work well with documentation generation tools like Sphinx. These styles provide guidelines on how to format documentation so that it can be parsed by tools like Sphinx and presented in a structured and consistent manner.

Google Style Docstrings

Google style docstrings are more verbose than some other styles and are often preferred for their readability. Here's an example of a Google style docstring:

python 复制代码
def function(arg1, arg2):
"""Summary of the function.

Extended description can be in multiple paragraphs if necessary.

Args:
arg1 (int): Description of arg1.
arg2 (str): Description of arg2.

Returns:
bool: Description of return value.

Raises:
ValueError: Explanation of when a ValueError is raised.
"""
pass

Key points:

  • A one-line summary that does not start with a variable name or an imperative verb.
  • A blank line follows the summary.
  • The "Args" section describes each parameter with its name, type, and description.
  • The "Returns" section describes the return type and purpose.
  • The "Raises" section lists any exceptions that the function might raise.

NumPy Style Docstrings

NumPy style docstrings are similar to Google style but have some differences in formatting, particularly in how they handle parameter and return value documentation. They are widely used in scientific and mathematical Python communities. Here's an example of a NumPy style docstring:

python 复制代码
def function(arg1, arg2):
"""
Summary of the function.

Extended description can be in multiple paragraphs if necessary.

Parameters
----------
arg1 : int
Description of arg1.
arg2 : str
Description of arg2.

Returns
-------
bool
Description of return value.

Raises
------
ValueError
Explanation of when a ValueError is raised.
"""
pass

Key points:

  • A one-line summary followed by an optional extended description.
  • The "Parameters" section is marked with a header and a dashed line.
  • Each parameter is listed with its name, type, and description.
  • The "Returns" section is similar, with a header, dashed line, and a description.
  • The "Raises" section lists exceptions in the same way.

Both styles are well supported by Sphinx, especially when used in combination with the Sphinx extension napoleon. The napoleon extension allows Sphinx to parse both Google style and NumPy style docstrings into the reStructuredText format that Sphinx uses to generate documentation.

When choosing between these styles, consider the conventions used in your project or community and your personal preference for readability and clarity.

相关推荐
Promising_GEO几秒前
使用R语言绘制简单地图的教程
开发语言·python·r语言
微尘82 分钟前
C++条件编译指令:#if、#elif、#ifdef、#ifndef、#elifdef、#elifndef、#else、#endif
开发语言·c++
许乌有3 分钟前
matlab基础使用
开发语言·matlab
湫ccc41 分钟前
《Python基础》之函数的用法
开发语言·python
测试老哥44 分钟前
pytest之收集用例规则与运行指定用例
自动化测试·软件测试·python·测试工具·职场和发展·测试用例·pytest
lly2024061 小时前
C# 类(Class)
开发语言
梦深时有鹿1 小时前
C#基础练习61-65
开发语言·c#
张声录11 小时前
使用client-go在命令空间test里面对pod进行操作
开发语言·后端·golang
庄毕楠2 小时前
金铲铲S13双城之战自动拿牌助手
python·pyautogui·金铲铲·金铲铲游戏助手·金铲铲助手
菜鸟小贤贤2 小时前
pyhton+yaml+pytest+allure框架封装-全局变量接口关联
开发语言·python·macos·自动化·pytest