Python 函数注释与文档

标签 python python-3.x

<分区>

Python3 有注释功能。我应该使用它来代替文档字符串吗? IE。什么是正确的方法:

def func(points: [{'x': int, 'y': int}]): -> int
    pass

对比

def func(points):
    ''' Take a list of dicts, dict present a point, and contain keys:
    'x' - int, x position
    'y' - int, y position
    Return int

    '''
    pass

最佳答案

不,它们是对文档字符串的补充。来自官方函数注解pep :

Because Python's 2.x series lacks a standard way of annotating a function's
parameters and return values, a variety of tools and libraries have appeared to
fill this gap.

但是 Python 社区喜欢将事物标准化(引用 PEPs)。所以他们提出了函数注释,主要针对第三方库和工具——IDE、代码质量/静态分析工具等。正如 PEP 所说:

By itself, Python does not attach any particular meaning or significance to
annotations.(...)meaning comes from third-party libraries alone.

编辑:可以找到一篇关于注释使用(早期错误检测、代码完成和一般工具支持)的非常好的文章 here .

关于Python 函数注释与文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19497630/

相关文章:

python - "QPainter::begin: Paint device returned engine == 0, type: 1"

python - 使用 matplotlib 绘制两行标签贴

python - 即使在 KeepAspectRatio 和 SmoothTransition 之后,如何在 QPixmap 上获得更好的图像质量

python - Boto3 S3,按上次修改对桶进行排序

python-3.x - 从集合列表中查找元素数量最少的集合

python - 为什么当我在 View 之间切换时,我的用户权限会消失?

python - 相当于 tensorflow 中的 np.add.at

python-3.x - 数据透视表中每个级别的小计

python - 将字节串发送到串口设备

python 3 : Shared Array with strings between processes