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
title = {Euclid's Elements of Geometry~(\ensuremath{\Sigma\tau{o}\iota\chi\varepsilon{\tilde{\iota}}\alpha}). The Greek Text of J.L.~Heiberg~(1883-1885) from Euclidis Elementa, Edidit et Latine Interpretatus est I.L.~Heiberg in Aedibus } # p_teubner_b_g # {i, 1883-1885. Edited, and provided with a modern English translation, by } # a_fitzpatrick_richard,
1110
1125
author = a_euclid,
@@ -1560,6 +1575,17 @@ @techreport{PEP257
1560
1575
urldate = {2024-07-27},
1561
1576
}
1562
1577
1578
+
@techreport{PEP482,
1579
+
author = a_langa_lukasz,
1580
+
title = {Literature Overview for Type Hints},
1581
+
number = {482},
1582
+
xdata = {rep_pep},
1583
+
date = {2015-01-08},
1584
+
url = {https://peps.python.org/pep-0482},
1585
+
urldate = {2024-10-09},
1586
+
}
1587
+
1588
+
1563
1589
@techreport{PEP484,
1564
1590
title = {Type Hints},
1565
1591
author = a_van_rossum_guido # and # a_langa_lukasz,
@@ -1590,6 +1616,36 @@ @techreport{PEP515
1590
1616
urldate = {2024-09-23},
1591
1617
}
1592
1618
1619
+
@techreport{PEP585,
1620
+
author = a_langa_lukasz,
1621
+
title = {Type Hinting Generics In Standard Collections},
1622
+
xdata = {rep_pep},
1623
+
number = {585},
1624
+
date = {2019-03-03},
1625
+
url = {https://peps.python.org/pep-0585},
1626
+
urldate = {2024-10-09},
1627
+
}
1628
+
1629
+
@techreport{PEP604,
1630
+
author = a_prados_philippe # and # a_moss_maggie,
1631
+
title = {Allow Writing Union Types as \pythonil{X | Y}},
1632
+
xdata = {rep_pep},
1633
+
number = {604},
1634
+
date = {2019-08-28},
1635
+
url = {https://peps.python.org/pep-0604},
1636
+
urldate = {2024-10-09},
1637
+
}
1638
+
1639
+
@techreport{PEP612,
1640
+
author = a_mendoza_mark,
1641
+
title = {Parameter Specification Variables},
1642
+
xdata = {rep_pep},
1643
+
number = {612},
1644
+
date = {2019-12-18/2020-07-13},
1645
+
url = {https://peps.python.org/pep-0612},
1646
+
urldate = {2024-10-09},
1647
+
}
1648
+
1593
1649
@techreport{PEP635,
1594
1650
author = a_kohn_tobias # and # a_van_rossum_guido,
1595
1651
title = {Structural Pattern Matching: Motivation and Rationale},
Copy file name to clipboardExpand all lines: notation/terms.sty
+29-3
Original file line number
Diff line number
Diff line change
@@ -45,12 +45,12 @@ See \cref{sec:howFloatingPointNumbersWork}.%
45
45
%
46
46
%
47
47
\newglossaryentry{fstring}{%
48
-
name={f-string},%
49
-
plural={f-strings},%
48
+
name={f\nobreakdashes-string},%
49
+
plural={f\nobreakdashes-strings},%
50
50
sort={f-string},
51
51
description={%
52
52
is a special string in \python, which delimited by \pythonil{f"..."}\pythonIdx{f\textquotedbl\idxdots\textquotedbl} which can contain expressions in curly braces like \pythonil{f"a\{6-1\}b"} that are then turned to text via \pgls{strinterpolation}, which turns the string to~\pythonil{"a5b"}. %
53
-
f\nobreakdash-strings are discussed in \cref{sec:fstrings}.%
53
+
f\nobreakdashes-strings are discussed in \cref{sec:fstrings}.%
54
54
}%
55
55
}%
56
56
%
@@ -178,6 +178,22 @@ Under \ubuntu\ \linux, \ubuntuTerminal\ opens a terminal, which then runs a \bas
178
178
}%
179
179
%
180
180
%
181
+
\newglossaryentry{typeHint}{%
182
+
name={type hint},%
183
+
plural={type hints},%
184
+
description={%
185
+
are annotations that help programmers and static code analysis tools such as \mypy\ to better understand what type a variable or function parameter is supposed to be~\cite{PEP484,PEP482}. %
186
+
\python\ is a dynamically typed programming language where you do not need to specify the type of, e.g., a variable. %
187
+
This creates problems for code analysis, both automated as well as manual: %
188
+
For example, it may not always be clear whether a variable or function parameter should be an integer or floating point number. %
189
+
The annotations allow us to explicitly state which type is expected. %
190
+
They are \emph{ignored} during the program execution. %
191
+
They are a basically a piece of documentation. %
192
+
See \cref{bp:typeHints,sec:varTypeHints}.%
193
+
}%
194
+
}%
195
+
%
196
+
%
181
197
\newglossaryentry{unicode}{%
182
198
name={Unicode},%
183
199
description={%
@@ -198,3 +214,13 @@ The most popular distributed \gls{VCS} is \gls{git}.%
198
214
}%
199
215
}%
200
216
%
217
+
%
218
+
\newglossaryentry{xAxis}{%
219
+
name={\ensuremath{x}\nobreakdashes-axis},%
220
+
plural={\ensuremath{x}\nobreakdashes-axes},%
221
+
sort={x-axis},
222
+
description={%
223
+
The \ensuremath{x}\nobreakdashes-axis is the horizontal axis of a two-dimensional coordinate system, often referred to abscissa.%
Copy file name to clipboardExpand all lines: text/main/basics/collections/dictionaries/dictionaries.tex
+2-2
Original file line number
Diff line number
Diff line change
@@ -13,7 +13,7 @@
13
13
\begin{sloppypar}%
14
14
\cref{lst:dicts:dicts_1} shows some examples of how we can use dictionaries.
15
15
A dictionary can be created again using the \pythonil{\{...\}}\pythonIdx{\textbraceleft\idxdots\textbraceright!dict} syntax, however, this time, we place comma-separated \pythonil{key-value} pairs within the curly braces.
16
-
The type hints for dictionaries\pythonIdx{dict!type hint} is \pythonil{dict[keyType][valueType]}, where \pythonil{keyType} is the type for the keys and \pythonil{valueType} is the type for the values.
16
+
The \pglspl{typeHint} for dictionaries\pythonIdx{dict!type hint} is \pythonil{dict[keyType][valueType]}, where \pythonil{keyType} is the type for the keys and \pythonil{valueType} is the type for the values~\cite{PEP585}.
17
17
The line \pythonil{num_str: dict[int, str] = \{2: "two", 1: "one", 3: "three", 4: "four"\}} therefore creates a new dictionary and stores it in the variable \pythonil{num_str}.
18
18
The type-hint of the variable states that it can point only to dictionaries that have integers as keys and strings as values.
19
19
The contents of the dictionary are the key-value pairs \pythonil{2: "two"}, \pythonil{1: "one"}, \pythonil{3: "three"}, and \pythonil{4: "four"}.
@@ -63,7 +63,7 @@
63
63
We now create the empty dictionary \pythonil{str_num: dict[str, int] = \{\}}\pythonIdx{\textbraceleft\textbraceright}\pythonIdx{dict!empty}.\footnote{%
64
64
Using the \pythonil{dict()}\pythonIdx{dict!empty} without arguments has the same effect, but several \pglspl{linter} discourage this and encourage using \pythonil{\{\}}\pythonIdx{\textbraceleft\textbraceright} instead.%
65
65
}
66
-
Notice that only due to the type hint\pythonil{dict[str, int]}, other programmers and static type-checking tools can know at this point that we intend to use strings as keys and integers as values in this new dictionary.
66
+
Notice that only due to the \pgls{typeHint}\pythonil{dict[str, int]}, other programmers and static type-checking tools can know at this point that we intend to use strings as keys and integers as values in this new dictionary.
67
67
The \pythonilIdx{update}\pythonIdx{dict!update} method allows us to append the values of an existing dictionary to a given dictionary.
68
68
\pythonil{str_num.update(\{"one": 1, "three": 3, "two": 2, "four": 4\})} therefore stores four key-value pairs into our new dictionary.
69
69
This time, the keys are indeed strings and the values are integers.
Copy file name to clipboardExpand all lines: text/main/basics/collections/lists/lists.tex
+4-4
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@
19
19
In \cref{lst:lists:lists_1}, we provide some first examples for using lists.
20
20
A list can be defined by simply writing its contents, separated by~\pythonilIdx{,} inside square brackets~\pythonil{[...]}\pythonIdx{[\idxdots]}.
21
21
\pythonil{["apple", "pear", "orange"]} creates a list with three elements, namely the strings \pythonil{"apple"}, \pythonil{"pear"}, and~\pythonil{"orange"}.
22
-
If we want to store a list in a variable, then we can use the type hint~\pythonil{list[elementType]}\pythonIdx{list!type hint} where \pythonil{elementType} is to be replaced with the type of the list elements.
22
+
If we want to store a list in a variable, then we can use the \pgls{typeHint}~\pythonil{list[elementType]}\pythonIdx{list!type hint} where \pythonil{elementType} is to be replaced with the type of the list elements~\cite{PEP585}.
23
23
\pythonil{fruits: list[str] = ["apple", "pear", "orange"]} therefore creates the list \pythonil{fruits} with the contents listed above.
24
24
It also tells any automated type checking tool and other programmers that we intent that only \pythonil{str} values should be stored inside the list.
25
25
@@ -159,7 +159,7 @@
159
159
However, upon closer inspection, we discover some issues.
160
160
In a first step, we would apply \mypy~(as~\cref{ut:mypy}) to check for problems with the types of variables.
161
161
And indeed, \cref{exec:lists:lists_error:mypy} shows us \emph{three} errors!
162
-
We defined \pythonil{my_list} as a list of strings by using the type hint\pythonil{list[str]}.
162
+
We defined \pythonil{my_list} as a list of strings by using the \pgls{typeHint}\pythonil{list[str]}.
163
163
However, we then set its value to be a list of three integer numbers (hence, three errors).
164
164
As promised in the title of this section, we will also use another tool to analyze this program:~\ruff.
165
165
@@ -204,7 +204,7 @@
204
204
%
205
205
%
206
206
In \cref{lst:lists:lists_fixed} we implement the three recommendations from the two tools.
207
-
We change the type hint of the list to \pythonil{list[int]}, which solves the type confusion that \mypy\ discovered.
207
+
We change the \pgls{typeHint} of the list to \pythonil{list[int]}, which solves the type confusion that \mypy\ discovered.
208
208
We remove the useless copying of the list as \ruff\ recommended.
209
209
Finally, we add a proper \pgls{docstring} at the top of the file, in which we even document the changes we applied.
210
210
The output \cref{exec:lists:lists_fixed} of the new program remains the same.
@@ -228,7 +228,7 @@
228
228
229
229
Well, this was only a two-line program.
230
230
But ask yourself:
231
-
Did you spot the incorrect type hint when you read the program?
231
+
Did you spot the incorrect \pgls{typeHint} when you read the program?
232
232
Did you see that we actually created a list and then copied it instead of using it directly?
233
233
(The \pgls{docstring} I give you, no chance of seeing that as we did not mention it before.)
234
234
Imagine that your job would be to work on a program with thousands of lines that was developed by a colleague.
Copy file name to clipboardExpand all lines: text/main/basics/collections/sets/sets.tex
+3-3
Original file line number
Diff line number
Diff line change
@@ -52,9 +52,9 @@
52
52
\begin{sloppypar}%
53
53
In \cref{lst:sets:sets_1}, we provide a first example for creating and working with sets.
54
54
Sets can be created by using curly braces, i.e, \pythonil{\{...\}}\pythonIdx{\textbraceleft\idxdots\textbraceright!set}.
55
-
They can be type-hinted using the notation \pythonil{set[elementType]}\pythonIdx{set!type hint} where \pythonil{elementType} is the type of elements to be stored in the set.
55
+
They can be type-hinted using the notation \pythonil{set[elementType]}\pythonIdx{set!type hint} where \pythonil{elementType} is the type of elements to be stored in the set~\cite{PEP585}.
56
56
The line \pythonil{upper: set[str] = \{"A", "G", "B", "T", "V"\}} creates the variable \pythonil{upper}, which points to a set of the five uppercase latin characters~\pythonil{"A"}, \pythonil{"G"}, \pythonil{"B"}, \pythonil{"T"}, and~\pythonil{"V"}.
57
-
The type hint~\pythonil{set[str]} states that this is a set that shall contain only strings.%
57
+
The \pgls{typeHint}~\pythonil{set[str]} states that this is a set that shall contain only strings.%
58
58
\end{sloppypar}%
59
59
%
60
60
With the method~\pythonilIdx{add}\pythonIdx{set!add}, we can add new elements to a set.
@@ -71,7 +71,7 @@
71
71
%
72
72
We can also create a set by passing a sequence into the constructor~\pythonilIdx{set}.
73
73
To demonstrate this, we first create the tuple~\pythonil{lower_tuple} to hold the lowercase characters \pythonil{("b", "i", "j", "c", "t", "i")}.
74
-
The command \pythonil{lower: set[str] = set(lower_tuple)} creates the set \pythonil{lower}, type hints it as set of strings, and lets it contain all the elements of~\pythonil{lower_tuple}.
74
+
The command \pythonil{lower: set[str] = set(lower_tuple)} creates the set \pythonil{lower}, \pglspl{typeHint} it as set of strings, and lets it contain all the elements of~\pythonil{lower_tuple}.
75
75
\pythonil{lower_tuple} contains the letter \pythonil{"i"} twice, but it will appear only once in the set that we have created.
76
76
Notice: Just calling \pythonil{set()} without argument (or with an empty collection inside) creates an empty set\pythonIdx{set!empty}.
77
77
We can delete an element of a set using the \pythonilIdx{remove}\pythonIdx{set!remove} function:
Copy file name to clipboardExpand all lines: text/main/basics/collections/tuples/tuples.tex
+9-9
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@
18
18
First, they are immutable.
19
19
You cannot add, delete, or change the elements of a tuple.
20
20
Second, on a semantic level, lists are intended to hold objects of the same type.
21
-
The type hint~\pythonil{list[int]} indicates that we want something to be list of integer numbers.
21
+
The \pgls{typeHint}~\pythonil{list[int]} indicates that we want something to be list of integer numbers.
22
22
While the \python\ interpreter permits us to ignore this and still store arbitrary objects in that list, this would violate the idea behind lists.
23
23
Tuples, on the other hand, are designed to contain elements of different types.
24
24
Since they cannot be changed, it will always be clear which element of which type is at which location.
@@ -31,14 +31,14 @@
31
31
\begin{sloppypar}%
32
32
\Cref{lst:tuples:tuples_1} shows us some of the things we can do with tuples.
33
33
We create a tuple \pythonil{fruits} to hold three strings.
34
-
The proper type hint for this is \pythonil{tuple[str, str, str]}\pythonIdx{tuple!type hint}.
34
+
The proper \pgls{typeHint} for this is \pythonil{tuple[str, str, str]}\pythonIdx{tuple!type hint}~\cite{PEP585}.
35
35
The line \pythonil{fruits: tuple[str, str, str] = ("apple", "pear", "orange")} thus creates a tuple~\pythonil{fruits} which contains three strings, namely \pythonil{"apple"}, \pythonil{"pear"}, and~\pythonil{"orange"}
36
-
We also annotated the variable with a type hint informing any static code analysis tool that we indeed intend to store three strings in the tuple.
36
+
We also annotated the variable with a \pgls{typeHint} informing any static code analysis tool that we indeed intend to store three strings in the tuple.
37
37
Notice that the tuple is defined using parentheses, whereas a list with the same content would have been defined using square brackets, e.g., as \pythonil{fruits: list[str] = ["apple", "pear", "orange"]}.%
38
38
\end{sloppypar}%
39
39
%
40
40
\begin{sloppypar}%
41
-
If we are not sure about the actual number of elements that we will put in a tuple but know that they are all of the same type, we can use the ellipsis~\pythonilIdx{...} in the type hint.
41
+
If we are not sure about the actual number of elements that we will put in a tuple but know that they are all of the same type, we can use the ellipsis~\pythonilIdx{...} in the \pgls{typeHint}.
42
42
\pythonil{tuple[str, ...]} denotes a tuple that can receive an arbitrary amount of strings.\pythonIdx{tuple!type hint!...}
43
43
The line \pythonil{veggies: tuple[str, ...] = ("onion", "potato", "leek", "garlic")} is therefore correct and defined a tuple~\pythonil{veggies} consisting of four strings.%
44
44
\end{sloppypar}%
@@ -57,7 +57,7 @@
57
57
%
58
58
\begin{sloppypar}%
59
59
In \cref{lst:tuples:tuples_2} we explore tuples containing elements of multiple types.
60
-
The type hint\pythonil{tuple[str, int, float]} states that we want to define a tuple where the first element is a string, the second element is an integer number, and the third element is a floating point number.
60
+
The \pgls{typeHint}\pythonil{tuple[str, int, float]} states that we want to define a tuple where the first element is a string, the second element is an integer number, and the third element is a floating point number.
61
61
The line \pythonil{mixed: tuple[str, int, float] = ("apple", 12, 1e25)} stores such a tuple in the variable~\pythonil{mixed}.
62
62
The first element of the tuple is the string~\pythonil{"apple"}.
63
63
The second element is the integer~\pythonil{12} and the third element is the floating point number~\pythonil{1e25}, i.e.,~$10^{25}$.
@@ -66,11 +66,11 @@
66
66
%
67
67
\begin{sloppypar}%
68
68
We now want to create a list of such tuples.
69
-
This list should, of course, also be annotated with the proper type hint.
69
+
This list should, of course, also be annotated with the proper \pgls{typeHint}.
70
70
This is easy:
71
-
The type hint for our kind of tuples is \pythonil{tuple[str, int, float]}.
72
-
The type hint for a list is \pythonil{list[elementType]}, where \pythonil{elementType} is the type of the elements.
73
-
If we want to create a list containing tuples of our kind, then the proper type hint would be \pythonil{list[tuple[str, int, float]]}.
71
+
The \pgls{typeHint} for our kind of tuples is \pythonil{tuple[str, int, float]}.
72
+
The \pgls{typeHint} for a list is \pythonil{list[elementType]}, where \pythonil{elementType} is the type of the elements.
73
+
If we want to create a list containing tuples of our kind, then the proper \pgls{typeHint} would be \pythonil{list[tuple[str, int, float]]}.
74
74
Having realized this, we can now create the list~\pythonil{tuples}.
75
75
We use the square bracket syntax for this.
76
76
As first element, we put the tuple stored in the variable~\pythonil{mixed}.
0 commit comments