Python函数注释与文档

时间:2013-10-21 14:45:02

标签: python python-3.x

Python3有一个注释功能。我应该用它代替文件字符串吗? I. e。什么是正确的方式:

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

VS

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

1 个答案:

答案 0 :(得分:0)

不,它们是文档字符串的补充。来自官方功能注释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社区喜欢标准化的东西(参考文献PEP)。因此,他们提出了功能注释,主要针对第三方库和工具 - IDE,代码质量/静态分析工具等。正如PEP所说:

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

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