TL;DR: Use type annotations, they are awesome.
For both Python and jedi
it does not make a difference wheter you use docstrings or function annotations. Both performance and memory impact should not be noticeable. There is obviously a small runtime overhead in both cases.
Docstrings are simply converted to Python strings and stored in the attribute function.__doc__
. This takes a few bytes of memory, but you shouldn't care about that. A very large docstring of 1000 characters still only uses 1kB of RAM. If your memory is constrained you may simply use python -o
to get rid of docstrings (and asserts as well, look it up).
Type annotations (PEP 484, e.g. def foo(a: int) -> str:
) are stored in function.__annotations__
:
>>> def foo(bar: int): pass
...
>>> foo.__annotations__
{'bar': <class 'int'>}
These annotations obviously also use some space (but even less than docstrings). However they have no influence on runtime execution (except if you explicitly play with __annotations__
.
I would recommend you to use type annotations. They have been introduced, because of static analysis/IDEs and are definitely the future when it comes to documenting your types. There's also a lot of work happing on mypy
, jedi
and other tools to make type annotations more usable in verifying programs. Use type annotations already and you will be ready for the future.