You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google风格文档字符串中格式化跨行参数描述

处理Google风格文档字符串中参数描述跨行的正确姿势

好问题!在Google风格的文档字符串里,处理参数描述跨行的情况其实很简单,核心就是保持缩进对齐,让整个文档结构清晰易读,同时和项目里的格式规范保持一致。

最常用的有两种格式化方式,你可以根据项目的统一约定来选:

1. 换行后对齐到参数名的起始位置

把换行后的描述内容缩进,对齐到参数名的第一个字符位置,这样视觉上的层级感非常明确:

def process_data(input_list, output_path):
    """Processes a list of data and writes results to a file.

    Args:
        input_list (list): A collection of raw data points that need to be cleaned,
            transformed, and analyzed. Each element should be a dictionary containing
            at least 'id' and 'value' keys.
        output_path (str): The full path to the file where processed results will be saved.
            Supports both relative and absolute paths, but ensure the parent directory
            exists before calling this function.

    Returns:
        bool: True if processing and writing completed successfully, False otherwise.
    """
    # 函数逻辑实现

2. 换行后缩进固定空格(推荐4个)

如果觉得对齐参数名太麻烦,也可以选择在换行后缩进4个空格(和Python代码的缩进保持一致),这样的格式同样符合Google风格的规范,可读性也不差:

def process_data(input_list, output_path):
    """Processes a list of data and writes results to a file.

    Args:
        input_list (list): A collection of raw data points that need to be cleaned,
            transformed, and analyzed. Each element should be a dictionary containing
            at least 'id' and 'value' keys.
        output_path (str): The full path to the file where processed results will be saved.
            Supports both relative and absolute paths, but ensure the parent directory
            exists before calling this function.

    Returns:
        bool: True if processing and writing completed successfully, False otherwise.
    """
    # 函数逻辑实现

关键注意点

  • 不管选哪种方式,整个项目里要统一格式,不要一会儿对齐参数名,一会儿用固定缩进,保持一致性比纠结哪种更“标准”重要。
  • 如果描述实在太长,不妨把内容拆分成更简洁的短句,避免过度换行,但如果必须跨行,严格遵守缩进规则就好。

内容的提问来源于stack exchange,提问作者Ivo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:41:57