@@ -16,17 +16,16 @@ separate the function from the function definition. The following simple example
1616inserts the code into a function so that you can call it to get the `factorial
1717<https://en.wikipedia.org/wiki/Factorial> `_ of a number:
1818
19- .. code-block :: pycon
19+ .. code-block :: python
2020 :linenos:
2121
22- >>> def fact(n):
23- ... """Return the factorial of the given number."""
24- ... f = 1
25- ... while n > 0:
26- ... f = f * n
27- ... n = n - 1
28- ... return f
29- ...
22+ def fact (n ):
23+ """ Return the factorial of the given number."""
24+ f = 1
25+ while n > 0 :
26+ f = f * n
27+ n = n - 1
28+ return f
3029
3130 Line 2
3231 This is an optional documentation string, or ``docstring ``. You can get its
@@ -62,10 +61,49 @@ value of a function is used:
6261 Line 1
6362 The return value is not linked to a variable.
6463Line 2
65- The value of the `` fact ` ` function is only output in the interpreter.
64+ The value of the :func: ` fact ` function is only output in the interpreter.
6665Line 3
6766 The return value is linked to the variable ``x ``.
6867
68+ Inspired by :doc: `Python4DataScience:productive/qa/mypy `, so-called *type
69+ hints * were introduced in Python, with which the types for :doc: `parameters
70+ <params>` and return values can be defined, in our :func: `fact ` example with:
71+
72+ .. blacken-docs:off
73+
74+ .. code-block :: python
75+
76+ def fact (n : int ) -> int :
77+ ...
78+
79+ .. blacken-docs:on
80+
81+ or:
82+
83+ .. blacken-docs:off
84+
85+ .. code-block :: python
86+
87+ def factlist (flist : list[float ]) -> list[float ]:
88+ ...
89+
90+ .. blacken-docs:on
91+
92+ We receive the types with the ``__annotations__ `` attribute at runtime:
93+
94+ .. code-block :: pycon
95+
96+ >>> fact.__annotations__
97+ {'n': <class 'int'>, 'return': <class 'int'>}
98+ >>> factlist.__annotations__
99+ {'list': list[float], 'return': list[float]}
100+
101+ However, there is **no ** type check at runtime.
102+
103+ .. seealso ::
104+ * :pep: `484 `
105+ * :doc: `python3:library/typing `
106+
69107.. toctree ::
70108 :titlesonly:
71109 :hidden:
0 commit comments