You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Python by default is a dynamically typed language but there are also some statically typed languages, for instance, C and Java. However, [PEP484](https://peps.python.org/pep-0484/) introduced typed hints, which make it possible to also do static type checking of Python code. Type hints by themselves don’t cause Python to enforce types. As the name says, type hints just suggest types. There are other tools, such as [Mypy](http://mypy-lang.org/), that will be discussed later in this section.
1334
+
1335
+
Here at Rootstrap, we suggest you use typing for functions (params and outputs). It isnot necessary to use it in variable annotations (as described in [PEP526](https://peps.python.org/pep-0526/)). We prefer to use typing as a guide for data type flow in our apps.
1336
+
1337
+
Now you will see a few examples of python code, this is how it will normally looks, no typing at all:
1338
+
1339
+
```python
1340
+
import math
1341
+
1342
+
def calculate_circumference(radius):
1343
+
return2* math.pi * radius
1344
+
```
1345
+
1346
+
Now this is how a code withtype comments will look like:
* Helps you catch certain errors by improving IDEs and linters.
1368
+
* Helps you document your code, changes doctstrings with types to a self documented code.
1369
+
* Build and mantain a cleaner architecture. This will enforce you to think about types in your code.
1370
+
* In bigger projects, type hints help you understand how types flow through your code, and are highly recommended. Even more so in projects where there are a lot of people involved.
1371
+
1372
+
### Cons of typing hints
1373
+
1374
+
* Will take developer time and effort to add it.
1375
+
* Work best in modern versions of Python. Python 2.7 could use typed comments, but it was introduced in Python 3.0.
1376
+
* It could introduce a slight penalty in start-up time, specially in short scripts if you need to use `import typing` module.
1377
+
1378
+
### Typing: please remember
1379
+
1380
+
This typing concepts does not mean you HAVE to use static type checking in your code, there is a concept call [Gradual typing inPEP483](https://peps.python.org/pep-0483/#summary-of-gradual-typing). This means that you can gradually introduce types into the code. Code without type hints will be ignored by the static type checker. Therefore, you can start adding types to critical components.
1381
+
1327
1382
# Django
1328
1383
To start a new Django project, we recomend to use [Django-Cookiecutter](https://github.com/pydanny/cookiecutter-django). Please go to our [guide to start a Django project](./cookiecutter-django.md) using this tool.
0 commit comments