3

Python3 には注釈機能があります。ドキュメント文字列の代わりに使用する必要がありますか? 私はe。正しい方法は何ですか:

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
4

1 に答える 1

1

No, they are an addition to docstrings. From the official function annotations 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.

But the Python community likes standartizing things (ref. PEPs). So they came up with the function annotations, which are primarily targeted at third-party libraries and tools - IDEs, code quality / static analysis tools and etc. As the PEP says itself:

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

Edit: A really good article about uses of annotations (early error detection, code completion and tooling support in general) can be found here.

于 2013-10-21T15:10:59.033 に答える