Update README.md

Signed-off-by: David Rotermund <54365609+davrot@users.noreply.github.com>
This commit is contained in:
David Rotermund 2023-11-23 18:51:35 +01:00 committed by GitHub
parent f55833cd6c
commit da54fa7112
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -7,6 +7,34 @@ a: int = 0
b: float = 0.0
a = b Incompatible types in assignment (expression has type "float", variable has type "int")
```
Questions to [David Rotermund](mailto:davrot@uni-bremen.de)
## Why [Type hints](https://peps.python.org/pep-0484/)?
Why we got type hints according PEP 484 -- Type Hints (29-Sep-2014):
> [Rationale and Goals](https://www.python.org/dev/peps/pep-0484/#id11)
>
> This PEP aims to provide a standard syntax for type annotations, opening up Python code to easier static analysis and refactoring, potential runtime type checking, and (perhaps, in some contexts) code generation utilizing type information.
>
> Of these goals, static analysis is the most important. This includes support for off-line type checkers such as mypy, as well as providing a standard notation that can be used by IDEs for code completion and refactoring.
>
> [...]
>
> It should also be emphasized that Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention.
I would redefine this list a bit:
* It is a part of your automatic documentation (like with meaningful variable names). If another person gets your source code they understand it easier.
* You editor might thank you. Do to some new features in Python 3.10, the modern editors that do syntax highlighting and error checking have a harder time to infer what you mean. The more it need to think about what you mean, the slower your editor might get or even fail to show you syntax highlighting.
* Static code analysis is really helpful. It showed me any problems ahead that I would have figured out the hard way otherwise.
* Packages like the just-in-time compiler numba can produce better results if you can tell it what the variables are.
## How do we do it?
Variables are assigned to a type the ***first*** time when used or can be defined even before use:
```python
a: int
b: int = 0
```