Skip to content

Commit 08bf138

Browse files
committed
added functions as parameters, Callable, and lambda
1 parent 50de554 commit 08bf138

File tree

16 files changed

+360
-56
lines changed

16 files changed

+360
-56
lines changed

bibliography/bibliography.bib

+71
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ @string { a_duchesnay_edouard
9191
@string { a_dutka_jacques = "Jacques Dutka" }
9292
@string { a_editors_of_encyclopaedia_britannica = "The Editors of Encyclopaedia Britannica" }
9393
@string { a_eglen_stephen_j = "Stephen J.\ Eglen" }
94+
@string { a_epperson_james_f = "James F.\ Epperson" }
9495
@string { a_euclid = "{{Euclid of Alexandria}~{(\ensuremath{E{\acute\upsilon}\kappa\lambda\varepsilon{\acute\iota}\delta\eta\varsigma})}}" }
9596
@string { a_euler_leonhard = "Leonhard Euler" }
9697
@string { a_fang_lu = "Lu Fang" }
@@ -190,13 +191,15 @@ @string { a_matthias_meg
190191
@string { a_maxwell_aaron = "Aaron Maxwell" }
191192
@string { a_mayorov_nikolay = "Nikolay Mayorov" }
192193
@string { a_mcintosh_shane = "Shane McIntosh" }
194+
@string { a_mendoza_mark = "Mark Mendoza" }
193195
@string { a_michel_vincent = "Vincent Michel" }
194196
@string { a_millman_jarrod_k = "K.\ Jarrod Millman" }
195197
@string { a_mirjalili_vahid = "Vahid Mirjalili" }
196198
@string { a_monga_rajat = "Rajat Monga" }
197199
@string { a_moore_eric_w = "Eric W.\ Moore" }
198200
@string { a_moore_sherry = "Sherry Moore" }
199201
@string { a_morris_sidney_a = "Sidney A.\ Morris" }
202+
@string { a_moss_maggie = "Maggie Moss" }
200203
@string { a_murray_derek_gordon = "Derek Gordon Murray" }
201204
@string { a_naik_ganesh = "Ganesh Naik" }
202205
@string { a_nakano_koji = "Koji Nakano" }
@@ -226,6 +229,7 @@ @string { a_picus_matti
226229
@string { a_polat_ilhan = "Ilhan Polat" }
227230
@string { a_pollard_tom_j = "Tom J.\ Pollard" }
228231
@string { a_pomerance_carl = "Carl Pomerance" }
232+
@string { a_prados_philippe = "Philippe PRADOS" }
229233
@string { a_press_william_h = "William H.\ Press" }
230234
@string { a_prettenhofer_peter = "Peter Prettenhofer" }
231235
@string { a_pylint_contributors = "{{Pylint Contributors}}" }
@@ -451,6 +455,7 @@ @string { p_university_of_toronto_cs
451455
@string { p_university_of_washington = "{University of Washington}" }
452456
@string { p_usas = "{United States of America Standards Institute~{(USAS)}}" }
453457
@string { p_usenix = "{{USENIX} Association}" }
458+
@string { p_wiley = "{Wiley}" }
454459
@string { p_wiley_and_sons_ltd = "{John Wiley and Sons Ltd.}" }
455460
@string { p_wolfram_research = "{Wolfram Research, Inc.}" }
456461
@@ -518,6 +523,7 @@ @string { pa_university_of_toronto_cs
518523
@string { pa_university_of_washington = l_usa_seattle }
519524
@string { pa_usas = l_usa_new_york }
520525
@string { pa_usenix = l_usa_berkeley }
526+
@string { pa_wiley = l_usa_hoboken }
521527
@string { pa_wiley_and_sons_ltd = l_uk_chichester }
522528
@string { pa_wolfram_research = l_usa_champaign }
523529

@@ -1105,6 +1111,15 @@ @article{E1985AEOCF
11051111
addendum = {Translation of~\cite{E1737DFCD}.},
11061112
}
11071113

1114+
@book{E2013AITNMAA,
1115+
author = a_epperson_james_f,
1116+
title = {An Introduction to Numerical Methods and Analysis},
1117+
edition = {2},
1118+
date = {2013-10},
1119+
publisher = p_wiley_and_sons_ltd,
1120+
isbn = {9781118367599},
1121+
}
1122+
11081123
@book{EHF2008EEOGTGOJLH11FEEEELIEILHIATBG11EAPWMETBFR,
11091124
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,
11101125
author = a_euclid,
@@ -1560,6 +1575,17 @@ @techreport{PEP257
15601575
urldate = {2024-07-27},
15611576
}
15621577

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+
15631589
@techreport{PEP484,
15641590
title = {Type Hints},
15651591
author = a_van_rossum_guido # and # a_langa_lukasz,
@@ -1590,6 +1616,36 @@ @techreport{PEP515
15901616
urldate = {2024-09-23},
15911617
}
15921618

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+
15931649
@techreport{PEP635,
15941650
author = a_kohn_tobias # and # a_van_rossum_guido,
15951651
title = {Structural Pattern Matching: Motivation and Rationale},
@@ -1700,6 +1756,13 @@ @proceedings{PROC2023GECCO
17001756
date = {2023},
17011757
}
17021758

1759+
@inbook{PSF2024ACO,
1760+
title = {Annotating Callable Objects},
1761+
xdata = {PSF2024TPSL},
1762+
url = {https://docs.python.org/3/library/typing.html#annotating-callables},
1763+
urldate = {2024-10-09},
1764+
}
1765+
17031766
@inbook{PSF2024BIT,
17041767
title = {Built-in Types},
17051768
xdata = {PSF2024TPSL},
@@ -1745,6 +1808,14 @@ @inbook{PSF2024IPM
17451808
urldate = {2024-08-17},
17461809
}
17471810

1811+
@inbook{PSF2024L,
1812+
title = {Lambda},
1813+
chapter = {6.14.},
1814+
xdata = {PSF2024TPLR},
1815+
url = {https://docs.python.org/3/reference/expressions.html#lambda},
1816+
urldate = {2024-10-09},
1817+
}
1818+
17481819
@inbook{PSF2024NTIFC,
17491820
title = {Numeric Types -- \pythonilIdx{int}, \pythonilIdx{float}, \pythonilIdx{complex}},
17501821
xdata = {PSF2024TPSL},

notation/software.sty

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ name={Mypy},%
6161
sort={Mypy},%
6262
description={%
6363
\pythonIdx{Mypy}%
64-
is a static type checking tool for \python~\cite{LLHSVRZSJYYMC2024MOSTFP}. %
64+
is a static type checking tool for \python~\cite{LLHSVRZSJYYMC2024MOSTFP} that makes use of \pglspl{typeHint}. %
6565
Learn more at \url{https://github.com/python/mypy} or in \cref{sec:variableTypesAndTypeHints,ut:mypy}.%
6666
}%
6767
}%

notation/terms.sty

+29-3
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ See \cref{sec:howFloatingPointNumbersWork}.%
4545
%
4646
%
4747
\newglossaryentry{fstring}{%
48-
name={f-string},%
49-
plural={f-strings},%
48+
name={f\nobreakdashes-string},%
49+
plural={f\nobreakdashes-strings},%
5050
sort={f-string},
5151
description={%
5252
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}.%
5454
}%
5555
}%
5656
%
@@ -178,6 +178,22 @@ Under \ubuntu\ \linux, \ubuntuTerminal\ opens a terminal, which then runs a \bas
178178
}%
179179
%
180180
%
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+
%
181197
\newglossaryentry{unicode}{%
182198
name={Unicode},%
183199
description={%
@@ -198,3 +214,13 @@ The most popular distributed \gls{VCS} is \gls{git}.%
198214
}%
199215
}%
200216
%
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.%
224+
}%
225+
}%
226+
%

text/main/basics/collections/dictionaries/dictionaries.tex

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
\begin{sloppypar}%
1414
\cref{lst:dicts:dicts_1} shows some examples of how we can use dictionaries.
1515
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}.
1717
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}.
1818
The type-hint of the variable states that it can point only to dictionaries that have integers as keys and strings as values.
1919
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 @@
6363
We now create the empty dictionary \pythonil{str_num: dict[str, int] = \{\}}\pythonIdx{\textbraceleft\textbraceright}\pythonIdx{dict!empty}.\footnote{%
6464
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.%
6565
}
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.
6767
The \pythonilIdx{update}\pythonIdx{dict!update} method allows us to append the values of an existing dictionary to a given dictionary.
6868
\pythonil{str_num.update(\{"one": 1, "three": 3, "two": 2, "four": 4\})} therefore stores four key-value pairs into our new dictionary.
6969
This time, the keys are indeed strings and the values are integers.

text/main/basics/collections/lists/lists.tex

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
In \cref{lst:lists:lists_1}, we provide some first examples for using lists.
2020
A list can be defined by simply writing its contents, separated by~\pythonilIdx{,} inside square brackets~\pythonil{[...]}\pythonIdx{[\idxdots]}.
2121
\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}.
2323
\pythonil{fruits: list[str] = ["apple", "pear", "orange"]} therefore creates the list \pythonil{fruits} with the contents listed above.
2424
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.
2525

@@ -159,7 +159,7 @@
159159
However, upon closer inspection, we discover some issues.
160160
In a first step, we would apply \mypy~(as~\cref{ut:mypy}) to check for problems with the types of variables.
161161
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]}.
163163
However, we then set its value to be a list of three integer numbers (hence, three errors).
164164
As promised in the title of this section, we will also use another tool to analyze this program:~\ruff.
165165

@@ -204,7 +204,7 @@
204204
%
205205
%
206206
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.
208208
We remove the useless copying of the list as \ruff\ recommended.
209209
Finally, we add a proper \pgls{docstring} at the top of the file, in which we even document the changes we applied.
210210
The output \cref{exec:lists:lists_fixed} of the new program remains the same.
@@ -228,7 +228,7 @@
228228

229229
Well, this was only a two-line program.
230230
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?
232232
Did you see that we actually created a list and then copied it instead of using it directly?
233233
(The \pgls{docstring} I give you, no chance of seeing that as we did not mention it before.)
234234
Imagine that your job would be to work on a program with thousands of lines that was developed by a colleague.

text/main/basics/collections/sets/sets.tex

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252
\begin{sloppypar}%
5353
In \cref{lst:sets:sets_1}, we provide a first example for creating and working with sets.
5454
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}.
5656
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.%
5858
\end{sloppypar}%
5959
%
6060
With the method~\pythonilIdx{add}\pythonIdx{set!add}, we can add new elements to a set.
@@ -71,7 +71,7 @@
7171
%
7272
We can also create a set by passing a sequence into the constructor~\pythonilIdx{set}.
7373
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}.
7575
\pythonil{lower_tuple} contains the letter \pythonil{"i"} twice, but it will appear only once in the set that we have created.
7676
Notice: Just calling \pythonil{set()} without argument (or with an empty collection inside) creates an empty set\pythonIdx{set!empty}.
7777
We can delete an element of a set using the \pythonilIdx{remove}\pythonIdx{set!remove} function:

text/main/basics/collections/tuples/tuples.tex

+9-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
First, they are immutable.
1919
You cannot add, delete, or change the elements of a tuple.
2020
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.
2222
While the \python\ interpreter permits us to ignore this and still store arbitrary objects in that list, this would violate the idea behind lists.
2323
Tuples, on the other hand, are designed to contain elements of different types.
2424
Since they cannot be changed, it will always be clear which element of which type is at which location.
@@ -31,14 +31,14 @@
3131
\begin{sloppypar}%
3232
\Cref{lst:tuples:tuples_1} shows us some of the things we can do with tuples.
3333
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}.
3535
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.
3737
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"]}.%
3838
\end{sloppypar}%
3939
%
4040
\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}.
4242
\pythonil{tuple[str, ...]} denotes a tuple that can receive an arbitrary amount of strings.\pythonIdx{tuple!type hint!...}
4343
The line \pythonil{veggies: tuple[str, ...] = ("onion", "potato", "leek", "garlic")} is therefore correct and defined a tuple~\pythonil{veggies} consisting of four strings.%
4444
\end{sloppypar}%
@@ -57,7 +57,7 @@
5757
%
5858
\begin{sloppypar}%
5959
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.
6161
The line \pythonil{mixed: tuple[str, int, float] = ("apple", 12, 1e25)} stores such a tuple in the variable~\pythonil{mixed}.
6262
The first element of the tuple is the string~\pythonil{"apple"}.
6363
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 @@
6666
%
6767
\begin{sloppypar}%
6868
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}.
7070
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]]}.
7474
Having realized this, we can now create the list~\pythonil{tuples}.
7575
We use the square bracket syntax for this.
7676
As first element, we put the tuple stored in the variable~\pythonil{mixed}.

0 commit comments

Comments
 (0)