【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.

相关推荐
Yang-Never几秒前
Open GL ES->EGL渲染环境、数据、引擎、线程的创建
android·java·开发语言·kotlin·android studio
Justinyh1 分钟前
Notion同步到CSDN + 构建Obsidian本地博客系统指南
python·csdn·图床·notion·obsidian·文档同步·piclist
unicrom_深圳市由你创科技1 分钟前
使用 Vue3 + Nest.js 构建前后端分离项目的完整指南
开发语言·javascript·状态模式
我叫张小白。3 分钟前
Vue3 v-model:组件通信的语法糖
开发语言·前端·javascript·vue.js·elementui·前端框架·vue
翻斗花园牛图图-9 分钟前
Qt开发——系统相关3(Qt网络编程)
开发语言·qt
kk”13 分钟前
C++ AVL树
开发语言·数据结构·c++
D***y20117 分钟前
【Python】网络爬虫——词云wordcloud详细教程,爬取豆瓣最新评论并生成各式词云
爬虫·python·信息可视化
繁华似锦respect22 分钟前
C++ 设计模式之观察者模式详细介绍
linux·开发语言·c++·windows·观察者模式·设计模式·visual studio
后台开发者Ethan27 分钟前
py文件被初始化执行了2次
python
a31582380628 分钟前
Linux部署Python Django工程和Node工程,使用宝塔面板
linux·服务器·python·django·node·strapi·宝塔面板