Which is more efficient: docs or names like Python?

I want to add some automatic completion support to my Python code using Jedi . This can be done using either the docstrings function or the hint type (or both).

def function_with_types_in_docstring(param1, param2):
    """Example function with types documented in the docstring.

    :type param1: int
    :type param2: str
    :rtype: bool
    """

def function_with_pep484_type_annotations(param1: int, param2: str) -> bool:
    """Example function with PEP 484 type annotations."""

Which type documentation method adds less overhead in terms of memory usage and runtime? First, I'm interested in the efficiency of the Python code itself, then Jedi.

+4
source share
2 answers

TL DR: Use type annotations, they are awesome.

Python, jedi , . . , .

Docstrings Python function.__doc__. , . 1000 - 1 . , python -o, docstrings ( , ).

(PEP 484, def foo(a: int) -> str:) function.__annotations__:

>>> def foo(bar: int): pass
... 
>>> foo.__annotations__
{'bar': <class 'int'>}

, , ( , docstrings). ( , __annotations__.

. - /IDE , , , . happing mypy, jedi , . type , .

+4

, , . , stdlib 3rd part "-" .

, FWIW: Jedi Emacs , , ...

+1

Source: https://habr.com/ru/post/1678622/


All Articles