Python notes - syntax and language - type stuff
Syntaxish: syntax and language · type stuff · changes and py2/3 · decorators · importing, modules, packages · iterable stuff · concurrency · exceptions, warnings
IO: networking and web · filesystem Data: Numpy, scipy · pandas, dask · struct, buffer, array, bytes, memoryview · Python database notes Image, Visualization: PIL · Matplotlib, pylab · seaborn · bokeh · plotly
Stringy: strings, unicode, encodings · regexp · command line argument parsing · XML speed, memory, debugging, profiling · Python extensions · semi-sorted |
Type annotation
Since 3.5, 3.6, we got a syntax to annotate types, a module to help that, and .
There are now almost 20 sort-of-relevant PEPs, though probably most important are PEP 526, PEP 484 and perhaps PEP 483.
Python type annotation is primarily used on functions, e.g.
def greeting(name: str) -> str:
return 'Hello ' + name
You can also type variables, like
i:int = 1
...though there's less point, because...
In practice, this is type annotation, not type checking - it has absolutely no effect at runtime.
It's basically a comment - just one that that IDEs, linters, documentation will parse and show.
And while linters like MyPy can show some of the more egregious type-related mistakes,
the fact that python is dynamically typed means that fundamentally we can never show all of them
(arguably the real question becomes whether the things we don't catch are weird exceptions, or everyday cases).
Which means it is dangerous to consider this type checking.
If you want the safety of a statically typed language, use a statically typed language.
typing module
https://docs.python.org/3/library/typing.html