|
14 | 14 | \endhsection%
|
15 | 15 | %
|
16 | 16 | \hsection{Types and Confusion}%
|
| 17 | +\label{sec:typesAndConfusion}% |
17 | 18 | \gitPythonAndOutput{\programmingWithPythonCodeRepo}{01_variables}{variable_types_wrong.py}{--args format}{variables:types_wrong}{%
|
18 | 19 | An example for the confusing variable types.}
|
19 | 20 |
|
|
62 | 63 | %
|
63 | 64 | \begin{enumerate}%
|
64 | 65 | %
|
65 |
| -\item Use static type-checking tools to find such potential errors in our code.% |
| 66 | +\item Use static type checking tools to find such potential errors in our code.% |
66 | 67 | %
|
67 |
| -\item Use type hints to annotate variables with their types to \emph{a)}~make our intention clearer and \emph{b)}~support type-checking tools.% |
| 68 | +\item Use type hints to annotate variables with their types to \emph{a)}~make our intention clearer and \emph{b)}~support type checking tools.% |
68 | 69 | %
|
69 | 70 | \end{enumerate}%
|
70 | 71 | %
|
|
95 | 96 | In \python, which allows for dynamic typing and is an interpreted language, we will use a tool like \mypy~\cite{LLHSVRZSJYYMC2024MOSTFP}.
|
96 | 97 | You can install this tool by opening a terminal.
|
97 | 98 | Under \ubuntu, you therefore press \ubuntuTerminal, and under \windows, you \windowsTerminal.
|
98 |
| -Then type in \bashil{pip install mypy} and hit \keys{\enter}. |
| 99 | +Then type in \bashil{pip install mypy}\pythonIdx{Mypy}\pythonIdx{pip} and hit \keys{\enter}. |
99 | 100 | The \mypy\ tool will be installed as illustrated in \cref{fig:pipInstallMypy}.
|
100 | 101 |
|
101 | 102 | We can now apply the tool to the program from \cref{lst:variables:types_wrong}.
|
|
115 | 116 | It also cannot fix the errors, as it cannot what the programmer actually intended to do.
|
116 | 117 | But knowing that line~4 in \cref{lst:variables:types_wrong} is probably wrong will help the programmer to fix that error or oversight before passing the program on to someone else.%
|
117 | 118 | %
|
118 |
| -\bestPractice{staticTypeChecking}{Every program should pass static type-checking with tools such as \mypy. % |
| 119 | +\bestPractice{staticTypeChecking}{Every program should pass static type checking with tools such as \mypy. % |
119 | 120 | Any issue found by the tools should be fixed. %
|
120 |
| -In other words, type-check the program. % |
121 |
| -If there is an error, fix the error and \emph{type-check it again}. % |
122 |
| -Repeat this until no errors are found anyore.% |
| 121 | +In other words, type check the program. % |
| 122 | +If there is an error, fix the error and \emph{type check it again}. % |
| 123 | +Repeat this until no errors are found anymore.% |
123 | 124 | }%
|
124 | 125 | %
|
125 | 126 | \endhsection%
|
| 127 | +% |
| 128 | +\hsection{Type Hints}% |
| 129 | +% |
| 130 | +When we discussed \cref{lst:variables:types_wrong} in \cref{sec:typesAndConfusion}, we stated that there could be two reasons for the error in the code: |
| 131 | +Either, the author accidentally mixed-up two datatypes or operators~(\pythonilIdx{/}~vs.~\pythonilIdx{//}) or they chose a misleading name for their variable~\pythonil{int_var}. |
| 132 | +The problem that any type checking tool faces is that it cannot know the intention of the programmer. |
| 133 | +It can find that line~4 four is probably wrong, because the variable \pythonil{int_var}, which former contained an~\pythonil{int}, now gets a \pythonil{float} value assigned to it. |
| 134 | + |
| 135 | +Oddly enough, the problem of guessing the intention of the programmer does not exist in a statically typed language like~pgls{C}. |
| 136 | +Here, we \emph{need} to define the type of every variable before assigning a value to it. |
| 137 | +Therefore, if the programmer would have wanted \pythonil{int_var} to strictly be an integer, they would have declared it as an integer variable. |
| 138 | +If they wanted to store \pythonils{float} in it, they would have declared it as a \pythonil{float} variable. |
| 139 | +The compiler would have seen any malpractice right away and could tell us that either line~4 is wrong or our initial assignment of an \pythonil{int} to the variable in line~1. |
| 140 | + |
| 141 | +This is where the dynamic typing and lenience of \python\ comes back to bite us. |
| 142 | +It is very convenient for small projects, but as soon as the projects get bigger, it creates a mess. |
| 143 | +Remember that \inQuotes{real programs} are much more complex than \cref{lst:variables:types_wrong}. |
| 144 | +Imagine wading through thousands of lines of code to figure out what type a variable has, and, while doing so, remember that \python\ permits overwriting the contents of a variable with objects of an entirely different type whenever it pleases us. |
| 145 | + |
| 146 | +Realizing that dynamic typing can be a blessing but also a problem, \emph{optional} type hints were introduced into the \python\ language. |
| 147 | +We can now declare the type of a variable if we want. |
| 148 | +This solves the above problem basically entirely and allows us to tell type checking tools our intention.% |
| 149 | +% |
| 150 | +\endhsection% |
| 151 | +% |
126 | 152 | \FloatBarrier%
|
127 | 153 | \endhsection%
|
128 | 154 | %
|
0 commit comments