在python中,如何写好docstring也是个值得学习的东西。
单行
示例:1
"""This is a quick summary line used as a description of the object."""
多行
多行docstring包含如下几个部分:
- 一行概要描述
- 概要描述后跟一个空行
- 更详细的描述
- docstring后跟一个空行
示例:1
2
3
4
5
6
7
8
9"""This is the summary line
This is the further elaboration of the docstring. Within this section,
you can elaborate further on details as appropriate for the situation.
Notice that the summary and the elaboration is separated by a blank new
line.
"""
# Notice the blank line above. Code should continue on this line.
docstring的种类
- Class Docstring
- Package and Module Docstring
- Scrip Docstring
Class Docstring
1 | class Animal: |
应包含如下信息:
- 简短描述类的目的和行为
- 公共方法,配上一个简短描述
- 类属性
- 给子类的接口