Skip to content

Commit 80927bf

Browse files
committed
first steps into dunder methods
1 parent 5f77a31 commit 80927bf

File tree

9 files changed

+233
-18
lines changed

9 files changed

+233
-18
lines changed

bibliography/bibliography.bib

+61-2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ @string { a_brash_ron
5858
@string { a_brent_richard_p = "Richard P.\ Brent" }
5959
@string { a_brett_matthew = "Matthew Brett" }
6060
@string { a_bright_jonathan = "Jonathan Bright" }
61+
@string { a_broytman_oleg = "Oleg Broytman" }
6162
@string { a_brucher_matthieu = "Matthieu Brucher" }
6263
@string { a_bruhin_florian = "Florian Bruhin" }
6364
@string { a_bruneau_pierrix = "Pierrick Bruneau" }
@@ -157,6 +158,8 @@ @string { a_irving_geoffrey
157158
@string { a_isard_michael = "Michael Isard" }
158159
@string { a_ito_ysuaki = "Yasuaki Ito" }
159160
@string { a_jain_shantanu = "Shantanu Jain" }
161+
@string { a_james_mike = "Mike James" }
162+
@string { a_jewet_jim_j = "Jim J\. Jewett" }
160163
@string { a_joh_sangwoo = "Sangwoo Joh" }
161164
@string { a_johansson_robert = "Robert Johansson" }
162165
@string { a_johnson_stephen_curtis = "Stephen Curtis Johnson" }
@@ -345,6 +348,7 @@ @string { a_vizcaino_juan_antonio
345348
@string { a_wallach_hanna_m = "Hanna M.\ Wallach" }
346349
@string { a_wang_rui = "Rui Wang" }
347350
@string { a_warden_pete = "Pete Warden" }
351+
@string { a_warnes_gregory_r = "Gregory R.\ Warnes" }
348352
@string { a_warsaw_barry = "Barry Warsaw" }
349353
@string { a_weckesser_warren = "Warren Weckesser" }
350354
@string { a_weise_thomas = "Thomas Weise" }
@@ -473,6 +477,7 @@ @string { p_ietf
473477
@string { p_ifip = "{International Federation for Information Processing~{(IFIP)}}" }
474478
@string { p_infinite_skills = "Infinite Skills Inc" }
475479
@string { p_informs = "{The Institute for Operations Research and the Management Sciences~({INFORMS})}" }
480+
@string { p_io_press = "{{I/O}~Press}" }
476481
@string { p_iso = "{International Organization for Standardization~{(ISO)}}" }
477482
@string { p_leibniz_zentrum_fur_informatik = "{{Schloss Dagstuhl} -- {Leibniz-Zentrum f{\"u}r Informatik}}" }
478483
@string { p_logilab = "Logilab" }
@@ -1404,7 +1409,7 @@ @article{GRRX2001ACSOEHMFBDOOS
14041409

14051410
@inbook{H1997IS7FPN,
14061411
author = a_hollasch_steve,
1407-
title = {{IEEE} Standard 754 Floating Point Numbers},
1412+
title = {{IEEE} Standard~754 Floating Point Numbers},
14081413
booktitle = {{CSE401}:~{I}ntroduction to Compiler Construction},
14091414
publisher = p_university_of_washington,
14101415
address = pa_university_of_washington,
@@ -1475,7 +1480,7 @@ @article{HMvdWGVCWTBSKPHvKBHFdRWPGMSRWAGO2020APWN
14751480

14761481
@techreport{IEEE2019ISFFPA,
14771482
title = {{IEEE} Standard for Floating-Point Arithmetic},
1478-
number = {754\texttrademark-2019 {(Revision of IEEE~Std 754-2008)}},
1483+
number = {754\texttrademark-2019 {(Revision of IEEE~Std 754\nobreakdashes-2008)}},
14791484
xdata = {rep_ieee_standard},
14801485
date = {2019-06-13},
14811486
isbn = {978-1-5044-5925-9},
@@ -1517,6 +1522,14 @@ @book{J2018NPSCADSAWNSAM
15171522
isbn = {9781484242469}
15181523
}
15191524

1525+
@book{J2022PPEIAOSCD,
1526+
author = a_james_mike,
1527+
title = {Programmer's \python:~Everything is an Object~--~Something Completely Different},
1528+
edition = {2},
1529+
date = {2022-06-25},
1530+
publisher = p_io_press,
1531+
}
1532+
15201533
@book{J2024PTOGBSI8IS12E,
15211534
title = {{POSIX.1-2024}:~{T}he {O}pen {G}roup Base Specifications Issue~8, {IEEE}~{Std}~1003.1\texttrademark-2024~Edition},
15221535
publisher = pa_ieee # {:~} # p_ieee # and # pa_open_group # {:~} # p_open_group,
@@ -1921,6 +1934,16 @@ @techreport{PEP202
19211934
urldate = {2024-11-08}
19221935
}
19231936

1937+
@techreport{PEP207,
1938+
author = a_van_rossum_guido # and # a_ascher_david,
1939+
title = {Rich Comparisons},
1940+
number = {207},
1941+
date = {2000-07-25},
1942+
xdata = {rep_pep},
1943+
url = {https://peps.python.org/pep-0207},
1944+
urldate = {2024-12-08},
1945+
}
1946+
19241947
@techreport{PEP234,
19251948
author = a_yee_ka_ping # and # a_van_rossum_guido,
19261949
title = {\pythonilsIdx{Iterator}},
@@ -2071,6 +2094,26 @@ @techreport{PEP635
20712094
urldate = {2024-09-23},
20722095
}
20732096

2097+
@techreport{PEP754,
2098+
author = a_warnes_gregory_r,
2099+
title = {{IEEE~754} Floating Point Special Values~[Rejected]},
2100+
number = {754},
2101+
xdata = {rep_pep},
2102+
date = {2003-03-28},
2103+
url = {https://peps.python.org/pep-0754},
2104+
urldate = {2024-12-08},
2105+
}
2106+
2107+
@techreport{PEP3140,
2108+
authoer = a_broytman_oleg # and # a_jewet_jim_j,
2109+
title = {\pythonil{str(container)}\pythonIdx{str} should call~\pythonil{str(item)}, not~\pythonil{repr(item)}\pythonIdx{repr}~[Rejected]},
2110+
number = {3140},
2111+
xdata = {rep_pep},
2112+
date = {2008-05-27/2008-05-28},
2113+
url = {https://peps.python.org/pep-3140},
2114+
urldate = {2024-12-08},
2115+
}
2116+
20742117
@inproceedings{PGMLBCKLGADKYDRTCSFBC2019PAISHPDLL,
20752118
author = a_paszke_adam # and # a_gross_sam # and # a_massa_francisco # and # a_lerer_adam # and # a_bradbury_james # and # a_chanan_gregory # and # a_killeen_trevor # and # a_lin_zeming # and # a_gimelshein_natalia # and # a_antiga_luca # and # a_desmaison_alban # and # a_klopf_andreas # and # a_yang_edward_z # and # a_devito_zachary # and # a_raison_martin # and # a_tejani_alykhan # and # a_chilamkurthy_sasank # and # a_steiner_benoit # and # a_fang_lu # and # a_bai_junjie # and # a_chintala_soumith,
20762119
title = {\pytorch:~{A}n Imperative Style, High-Performance Deep Learning Library},
@@ -2264,6 +2307,14 @@ @inbook{PSF2024DTIPE
22642307
urldate = {2024-11-07},
22652308
}
22662309

2310+
@inbook{PSF2024FPAIAL,
2311+
title = {Floating-Point Arithmetic:~Issues and Limitations},
2312+
chapter = {15},
2313+
xdata = {PSF2024TPT},
2314+
url = {https://docs.python.org/3/tutorial/floatingpoint.html},
2315+
urldate = {2024-12-08},
2316+
}
2317+
22672318
@inbook{PSF2024FSL,
22682319
title = {Formatted String Literals},
22692320
chapter = {7.1.1},
@@ -2310,6 +2361,14 @@ @book{PSF2024P3D
23102361
urldate = {2024-07-05},
23112362
}
23122363

2364+
@inbook{PSF2024OVAT,
2365+
title = {Objects\pythonIdx{object}, Values and Types},
2366+
chapter = {3.1},
2367+
xdata = {PSF2024TPLR},
2368+
url = {https://docs.python.org/3/reference/datamodel.html#objects-values-and-types},
2369+
urldate = {2024-12-07}
2370+
}
2371+
23132372
@xdata{PSF2024PH,
23142373
volume = {\python~{HOWTOs}},
23152374
crossref = {PSF2024P3D}

text/main/basics/simpleDataTypesAndOperations/float/float.tex

+16-5
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
However, their accuracy is always limited to about 15~digits.
6666
In other words, regardless whether your \pythonilIdx{float} stores a very large or a very small number, you can have at most 15~digits of precision.
6767
For example, adding~1 to~$10^{16}$ would still yield~$10^{16}$, because only 15~digits are \inQuotes{stored} and the~1 will just \inQuotes{fall off.}
68-
You cannot represent numbers arbitrarily precisely.%
68+
You cannot represent numbers arbitrarily precisely~\cite{PSF2024FPAIAL}.%
6969
%
7070
\endhsection%
7171
%
@@ -99,7 +99,7 @@
9999
This brings us back to the previous section:
100100
$\sqrt{3.3}$~is not actually 1.816\decSep590\decSep212\decSep458\decSep495.
101101
It is an irrational number~\cite{S1988WPCHD,B1991IWNT} and irrational numbers cannot be expressed as fractions of integer numbers (by definition, otherwise they would be rational numbers).
102-
Since they cannot be expressed as integer fractions, we cannot write them in any way like $\frac{1\decSep816\decSep590\decSep212\decSep458\decSep495\dots}{1\decSep000\decSep000\decSep000\decSep000\decSep000\dots}$, regardless of how large a denominator we would pick.
102+
Since they cannot be expressed as integer fractions, we cannot write them down exactly in any way like $\frac{1\decSep816\decSep590\decSep212\decSep458\decSep495\dots}{1\decSep000\decSep000\decSep000\decSep000\decSep000\dots}$, regardless of how large a denominator we would pick.
103103
Hence, we cannot represent them exactly using discrete binary values of our computer's memory.
104104
Hence, the floating point representation cuts off somewhere.
105105
And this somewhere is after 15~decimal places.%
@@ -120,12 +120,23 @@
120120
When we then type \pythonilIdx{pi} and \pythonilIdx{e}, we can get to see their value in floating point representations: \pythonil{3.141592653589793} and \pythonil{2.718281828459045}, respectively.
121121
Again, these are not the exact values, but they are as close as we can get in this format.
122122

123-
Of course, \numberPi\ and~\numberE\ alone are not that much useful.
124-
If you reach back into your highschool days again, you will remember many interesting functions that are related to them.
123+
Surprisingly, we do not necessarily need irrational or transcendental numbers to experience this cut-off.
124+
There is no way to write down all the digits of fractions like~$\frac{1}{7}$ as decimals.
125+
We always need to cut off somewhere, e.g., we could write~$0.14285714285714285$, but that is not exactly the same as~$\frac{1}{7}$.
126+
As we discussed before, floating point numbers are stored in a binary format, i.e., represented by bits.
127+
In the binary system, we encounter this problem already for fractions like~$\frac{1}{10}=0.1$~\cite{PSF2024FPAIAL}.
128+
Essentially, we could write $\frac{1}{10}\approx\frac{1}{2^4}+\frac{1}{2^5}+\frac{1}{2^8}+\frac{1}{2^9}+\frac{1}{2^{12}}+\frac{1}{2^{13}}+\frac{1}{2^{16}}+\dots$, but we would never quite get there.
129+
This means that $0.1$~cannot be exactly represented as \pythonil{float}.
130+
Therefore, adding it up ten times also does not exactly equal~$1$.
131+
Indeed, adding~\pythonil{0.1} ten times and then subtracting~\pythonil{1.0} from the result yields~\pythonil{-1.1102230246251565e-16}.
132+
So even for \inQuotes{completely normal} numbers, floating point arithmetics may already cost us (a very tiny little bit of) precision.
133+
134+
Anyway, back to the constants \numberPi\ and~\numberE.
135+
Alone, they are not that much useful, but if you reach back into your high school days again, you will remember many interesting functions that are related to them.
125136
Let us import a few of them, again from the \pythonilIdx{math} module, via \pythonil{from math import sin, cos, tan, log}\pythonIdx{sin}\pythonIdx{cos}\pythonIdx{tan}\pythonIdx{log}.
126137
I think you can guess what these functions do.
127138

128-
From highschool, you may remember that~$\sin{\frac{\numberPi}{4}}=\frac{\sqrt{2}}{2}$ and thus~$\sin^2{\frac{\numberPi}{4}}=0.5$.
139+
From high school, you may remember that~$\sin{\frac{\numberPi}{4}}=\frac{\sqrt{2}}{2}$ and thus~$\sin^2{\frac{\numberPi}{4}}=0.5$.
129140
Let us compute this in \python\ by doing \pythonil{sin(0.25 * pi) ** 2}\pythonIdx{sin}.
130141
Surprisingly, we get \pythonil{0.4999999999999999} instead of \pythonil{0.5}.
131142
The reason is again the limited precision of \pythonilIdx{float}, which cannot represent~$\frac{\sqrt{2}}{2}$ exactly.

text/main/basics/variables/identity/identity.tex

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
\hsection{Object Equality and Identity}%
2+
\label{sec:equalityAndIdentity}%
23
%
34
\begin{figure}%
45
\centering%

text/main/classes/basics/basics.tex

+8-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
\end{pythonSyntax}
2424
%
2525
\hsection{A Simple Immutable Class for Points in the Two-Dimensional Euclidean Plane}%
26+
\label{sec:immutableClassPoints2D}%
2627
%
2728
\gitPython{\programmingWithPythonCodeRepo}{08_classes/point.py}{--args format}{classes:point}{%
2829
A class for representing points in the two-dimensional Euclidean plane.}%
@@ -71,14 +72,14 @@
7172
In other words, we later want to be able to create one instance of \pythonil{Point} with the x\nobreakdashes-coordinate~5 and the y\nobreakdashes-coordinate~10 and then another instance with the x\nobreakdashes-coordinate~2 and the y\nobreakdashes-coordinate~7.
7273

7374
Therefore, \pythonil{Point} needs a initializer, i.e., a special method that creates these attributes.
74-
This method is called~\pythonilIdx{\_\_init\_\_}.
75+
This method is called~\pythonilIdx{\_\_init\_\_}\pythonIdx{dunder!\_\_init\_\_}.
7576
As said, every method of a class must have a parameter~\pythonilIdx{self}, which is the instance of the class (the object) upon which the method is called.
76-
The initializer \pythonilIdx{\_\_init\_\_} is a special method, so it also has this parameter~\pythonilIdx{self}.
77+
The initializer \pythonilIdx{\_\_init\_\_}\pythonIdx{dunder!\_\_init\_\_} is a special method, so it also has this parameter~\pythonilIdx{self}.
7778
Additionally, we demand that two parameters~\pythonil{x} and~\pythonil{y} be passed in when we create an instance of~\pythonil{Point}.
7879
We allow the values to be either \pythonils{int} or \pythonils{float}.%
7980
%
8081
\bestPractice{attributes}{%
81-
Object attributes must only be created inside the initializer~\pythonilIdx{\_\_init\_\_}. %
82+
Object attributes must only be created inside the initializer~\pythonilIdx{\_\_init\_\_}\pythonIdx{dunder!\_\_init\_\_}. %
8283
An initial value must immediately be assigned to each attribute.%
8384
}
8485
%
@@ -97,7 +98,7 @@
9798
In other words, we do not allow the coordinates of our \pythonils{Point} to be change after creation.%
9899
%
99100
\bestPractice{attributeTypeHint}{%
100-
Every attribute of an object must be annotated with a \pgls{typeHint} and a documentation comment when created in the initializer~\pythonilIdx{\_\_init\_\_}. %
101+
Every attribute of an object must be annotated with a \pgls{typeHint} and a documentation comment when created in the initializer~\pythonilIdx{\_\_init\_\_}\pythonIdx{dunder!\_\_init\_\_}. %
101102
\pglspl{typeHint} work as with normal variables, but the documentation is slightly different: %
102103
In the line \emph{above} the attribute initialization, a \emph{comment} starting with \pythonilIdx{\#: } must be written which explains the meaning of the attribute.%
103104
}%
@@ -140,7 +141,7 @@
140141
For \pythonil{p1}, this returns~\pythonil{True}.
141142

142143
We now create a second instance, \pythonil{p2}, of the class \pythonil{Point}.
143-
We assign \pythonil{7} to \pythonil{p2.x} and \pythonil{8} to \pythonil{p2.y} via the \pythonilIdx{\_\_init\_\_} initializer, which is automatically invoked when we write~\pythonil{Point(7, 8)}.
144+
We assign \pythonil{7} to \pythonil{p2.x} and \pythonil{8} to \pythonil{p2.y} via the \pythonilIdx{\_\_init\_\_}\pythonIdx{dunder!\_\_init\_\_} initializer, which is automatically invoked when we write~\pythonil{Point(7, 8)}.
144145
We can again print the values of these attributes using an \pgls{fstring}.
145146
While \pythonil{isinstance(p2, Point)} is again \pythonil{True}, \pythonil{isinstance(5, Point)} returns \pythonil{False}.
146147

@@ -208,7 +209,7 @@
208209
This is illustrated in \cref{fig:addingFloatsError}:
209210
If we add~\pythonil{1} to~$10^{15}=\pythonil{1e15}$, the correct result is~\pythonil{1000000000000001.0}.
210211
However, if we add~\pythonil{1} to~$10^{16}=\pythonil{1e16}$, the result is still~\pythonil{1e16}.
211-
It is not possible to represent the number~$1+10^{16}$ correctly with the 64~bit double precision floating point numbers that \python\ offers.
212+
It is not possible to represent the number~$1+10^{16}$ correctly with the 64~bit double precision floating point numbers that \python\ offers~\cite{PSF2024FPAIAL}.
212213
Usually, that itself is totally fine:
213214
There are few application in \inQuotes{everyday programming} where we really need more than fifteen digits of precision.
214215

@@ -333,7 +334,7 @@
333334
%
334335
This is going to be the interface for our new class~\pythonil{KahanSum}, which, in turn, is based on~\cite{K2006AGKBSA}.
335336

336-
In the initializer~\pythonilIdx{\_\_init\_\_}, we create the three attributes~\pythonil{__sum}, \pythonil{__cs}, and \pythonil{__ccs} and initialize them to~\pythonil{0}.
337+
In the initializer~\pythonilIdx{\_\_init\_\_}\pythonIdx{dunder!\_\_init\_\_}, we create the three attributes~\pythonil{__sum}, \pythonil{__cs}, and \pythonil{__ccs} and initialize them to~\pythonil{0}.
337338
These names are directly taken from \cref{algo:kahanSum}.
338339
Notice the double leading underscores in front of the names.%
339340
%

text/main/classes/classes.tex

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
\hsection{Classes}%
22
%
3-
%
43
\hinput{basics}{basics}%
54
\hinput{inheritance}{inheritance}%
5+
\hinput{dunder}{dunder}%
66
%
77
\endhsection%

0 commit comments

Comments
 (0)