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
Copy file name to clipboardExpand all lines: text/main/basics/variables/multiAndSwap/multiAndSwap.tex
+1Lines changed: 1 addition & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -25,5 +25,6 @@
25
25
\pythonil{x, y, z = z, y, x} assigns the present value of \pythonil{z} to become the new value of \pythonil{x}, the present value of \pythonil{y} to also be the new value of \pythonil{y}, and the present value of \pythonil{x} to become the new value of \pythonil{z}.
26
26
\pythonil{print(f"x={x}, y={y}, z={z}")} now gives us \textil{x=1, y=2, z=3}.
A variable is basically a name pointing to an object.
8
+
Each object has a type and we already learned about several of these datatypes in \cref{sec:simplyDataTypesAndOperations}.
9
+
We can obtain the type of an object stored in variable \pythonil{var} by invoking \pythonil{type(var)}\pythonIdx{type}.
10
+
\Cref{lst:variables:types} shows a program that does just that and the output of that program is given in \cref{lst:variables:types}.
11
+
It is obvious that the type of a variable that holds an integer value is \pythonilIdx{int}, the type of a variable that holds a floating point number is \pythonilIdx{float}, and so on.
You see, when you declare a variable in a language like \texttt{C}, you have to specify its \emph{type}.
21
+
You are then permitted to only assign values that have exactly this type to the variable.
22
+
In \python, you do not need to specify a type and you can assign whatever you want to a variable.
23
+
This has the advantage that the code is shorter (because you do not need to write the type), looks more elegant, and programming becomes easier.
24
+
At first glance.
25
+
However, there are also problems.
26
+
Let's take a look at \cref{lst:variables:types_wrong}.
27
+
We declare a variable named \pythonil{int_var} and store the integer~\pythonil{8} in it.
28
+
Then we update \pythonil{int_var} by computing \pythonil{int_var = int_var / 3}.
29
+
Back in \cref{sec:int}, you learned that the \pythonilIdx{//} operator performs an integer division with an \pythonil{int} result, whereas the division using the \pythonilIdx{/} operator always returns a \pythonil{float}.
30
+
This means that our variable \pythonil{int_var} now contains a \pythonil{float}, which is also visible in the output in \cref{exec:variables:types_wrong}.
31
+
32
+
From the perspective of \python, this is totally fine.
33
+
The program executes and the output appears without error.
34
+
However, from the perspective of programming, \cref{lst:variables:types_wrong} is \emph{wrong}.
35
+
Imagine that this was not just some random example without meaning.
36
+
Imagine that this was a part of a really useful program.
37
+
Imagine that you got this program from some source and try to understand it.
38
+
If you read this program, then you find that a variable named \pythonil{int_var} contains a \pythonil{float}.
39
+
This is not forbidden, but when reading the code, it must strike you as odd.
40
+
41
+
Indeed, there are at least two possible explanations for this:
42
+
Either, the original author of this code mistakenly mixed up the \pythonilIdx{/} operator for the \pythonilIdx{//}.
43
+
Maybe they wanted to do an integer division and accidentally did a floating point division.
44
+
Depending on what the code later on (in our imaginary larger program) does, it could be very hard to find such an error.
45
+
46
+
Or maybe the author fully well wanted to do a floating point division and expected a \pythonil{float} to be stored in \pythonil{int_var}, but chose a misleading name.
47
+
Choosing this name, however, can be very dangerous:
48
+
What if another programmer continues to work on this code and, based on the variable's name, expects it to contain an \pythonil{int} whereas it actually contains a \pythonil{float}.
49
+
This could again lead to all sorts of strange errors later on in her code.
50
+
51
+
Regardless of what is true, you will certainly agree that something is wrong with this program.
52
+
And since you are not the author of the program, you do not know what is wrong.
53
+
This code will cause some problem down the line.
54
+
Many such problems exist in many software projects and they indeed are hard to find~\cite{KCVM2022AESOTRDIPP}.
55
+
So here, the lenience of \python\ of allowing us to not specify types comes back to bite us.
56
+
57
+
Luckily, there are two things that we can do to prevent such situations:%
58
+
\begin{enumerate}%
59
+
\item Use static type checking tools to find such potential errors in our code.%
60
+
\item Use type hints to annotate variables with their types.%
61
+
\end{enumerate}%
62
+
And if you are in one of my classes, you better do both.
0 commit comments