PROGRAMMING
📝Python’s Type Annotations📝 — Why You Always Should Use It
Beauty and power of type annotations in Python or why I always use it
Python is a dynamically typed language and allows us to operate fairly freely on variables of different types. However, when writing code, we somehow assume which types of variables will be used (this may be caused by a limitation of the algorithm or business logic). And for the program to work correctly, it is important for us to find errors associated with transferring data of the wrong type as early as possible.
Keeping the idea of ​​dynamic duck typing in modern versions of Python (3.6+) supports annotations of variable types, class fields, arguments, and return values ​​of functions:
- PEP 3107 — Function Annotations
- PEP 484 — Type Hints
- PEP 526 — Syntax for Variable Annotations
typing
package
Type annotations are read by the Python interpreter and are not processed in any way but are available for use from third-party code and are primarily designed for static analyzers.
In this article, I want to explain the basics and give examples of using type annotations and eventually show why it made my life as a…