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

相关推荐
浩瀚地学5 分钟前
【面试算法笔记】0105-数组-螺旋矩阵
java·开发语言·笔记·算法·面试
Geek-Chow19 分钟前
Mobile App Certificate Pinning: Underlying Principle and a Swift Example
开发语言·ios·swift·安全架构
码云骑士23 分钟前
61-LangChain-vs-LlamaIndex-选型对比-功能矩阵-混用实践
python·线性代数·矩阵·langchain
阿豪只会阿巴28 分钟前
两小时快速入门 FastAPI--第一回
开发语言·python·fastapi
Hesionberger38 分钟前
动态规划与二分法破解最长递增子序列
java·数据结构·python·算法·leetcode
JustNow_Man40 分钟前
【Claude Code】 中给 Python + XML 项目建立可靠验证体系
xml·linux·python
你怎么知道我是队长44 分钟前
JavaScript 事件介绍
开发语言·前端·javascript
薛定猫AI1 小时前
【技术干货】多模型AI编程代理实战:用Python统一接入Claude Opus 4.8
人工智能·python·ai编程
CodeStats1 小时前
【Linux IO】从文件描述符到硬件中断:Linux IO 系统底层完全拆解
java·linux·开发语言·io·socket
着迷不白1 小时前
Linux系统故障修复、日志管理与安全加固实战指南
开发语言·php