跳转至

函数文档字符串

文档字符串 #card

  • 文档字符串(Documentation Strings),也叫docstrings
  • DocStrings 本质是位于函数主体开头的多行注释,其目的是让函数更易于理解。
  • 可通过函数的__doc__属性来获取函数的文档字符串。 ^1662427240444

文档字符串约定

  • 文档字符串是一串多行字符串,第一行可简介函数功能。
  • 第二行为空行。
  • 后跟的第三行开始是任何详细的解释说明。

案例

def print_max(x, y):
    '''Prints the maximum of two numbers.

    The two values must be integers.'''
    # 如果可能,将其转换至整数类型
    x = int(x)
    y = int(y)

    if x > y:
        print(x, 'is maximum')
    else:
        print(y, 'is maximum')

print_max(3, 5)
print(print_max.__doc__)
输出

$ python function_docstring.py
5 is maximum
Prints the maximum of two numbers.

    The two values must be integers.

参考文献

1.


最后更新: 2022年10月15日 01:02:48
创建日期: 2022年9月5日 23:16:07
Contributers: yangjh