Skip to content

Commit 8187128

Browse files
committed
small fix
1 parent c138c26 commit 8187128

File tree

2 files changed

+13
-13
lines changed

2 files changed

+13
-13
lines changed

text/main/classes/basics/basics.tex

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"""Docstring of the class."""
1212

1313
def __init__(self) -> None:
14-
"""Docstring of the constructor __init__."""
14+
"""Docstring of the initializer __init__."""
1515
# initialize all attributes
1616
#: Meaning of attribute 1
1717
self.attribute_1: type hint = initial value
@@ -70,22 +70,22 @@
7070
A attribute is a variable that every single instance of the class has.
7171
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.
7272

73-
Therefore, \pythonil{Point} needs a constructor, i.e., a special method that creates these attributes.
73+
Therefore, \pythonil{Point} needs a initializer, i.e., a special method that creates these attributes.
7474
This method is called~\pythonilIdx{\_\_init\_\_}.
7575
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 constructor \pythonilIdx{\_\_init\_\_} is a special method, so it also has this parameter~\pythonilIdx{self}.
76+
The initializer \pythonilIdx{\_\_init\_\_} is a special method, so it also has this parameter~\pythonilIdx{self}.
7777
Additionally, we demand that two parameters~\pythonil{x} and~\pythonil{y} be passed in when we create an instance of~\pythonil{Point}.
7878
We allow the values to be either \pythonils{int} or \pythonils{float}.%
7979
%
8080
\bestPractice{attributes}{%
81-
Object attributes must only be created inside the constructor~\pythonilIdx{\_\_init\_\_}. %
81+
Object attributes must only be created inside the initializer~\pythonilIdx{\_\_init\_\_}. %
8282
An initial value must immediately be assigned to each attribute.%
8383
}
8484
%
8585
We only want to allow \pythonils{Point} that have finite coordinates.
8686
As explained in the \cref{sec:exceptions} on \pythonilsIdx{Exception}, it is better to immediately signal errors if we encounter invalid data.
8787
So we want to sort out non-finite coordinates right when someone attempts to create the \pythonil{Point} object.
88-
Thus, the first thing we do in the constructor is to use the \pythonilIdx{isfinite} function from the \pythonilIdx{math} module.
88+
Thus, the first thing we do in the initializer is to use the \pythonilIdx{isfinite} function from the \pythonilIdx{math} module.
8989
If either \pythonil{x} or \pythonil{y} is not finite, then we raise\pythonIdx{raise} a \pythonilIdx{ValueError}.
9090
Otherwise, we set \pythonil{self.x: Final[int | float] = x} and \pythonil{self.y: Final[int | float] = y}.\footnote{%
9191
Strictly speaking, it could also make sense to check whether \pythonil{x} and \pythonil{y} are indeed either \pythonil{int} or \pythonil{float}. %
@@ -97,7 +97,7 @@
9797
In other words, we do not allow the coordinates of our \pythonils{Point} to be change after creation.%
9898
%
9999
\bestPractice{attributeTypeHint}{%
100-
Every attribute of an object must be annotated with a \pgls{typeHint} and a documentation comment when created in the constructor~\pythonilIdx{\_\_init\_\_}. %
100+
Every attribute of an object must be annotated with a \pgls{typeHint} and a documentation comment when created in the initializer~\pythonilIdx{\_\_init\_\_}. %
101101
\pglspl{typeHint} work as with normal variables, but the documentation is slightly different: %
102102
In the line \emph{above} the attribute initialization, a \emph{comment} starting with \pythonilIdx{\#: } must be written which explains the meaning of the attribute.%
103103
}%
@@ -140,7 +140,7 @@
140140
For \pythonil{p1}, this returns~\pythonil{True}.
141141

142142
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\_\_} constructor, which is automatically invoked when we write~\pythonil{Point(7, 8)}.
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)}.
144144
We can again print the values of these attributes using an \pgls{fstring}.
145145
While \pythonil{isinstance(p2, Point)} is again \pythonil{True}, \pythonil{isinstance(5, Point)} returns \pythonil{False}.
146146

@@ -165,7 +165,7 @@
165165
So the x\nobreakdashes-~and y\nobreakdashes-attributes of instances of \pythonil{Point} can actually be changed. %
166166
However, tools like \mypy\ will detect such mistakes and report them~\cite{PEP591}.%
167167
}
168-
The x\nobreakdashes-~and y\nobreakdashes-attributes are initialized by the \pythonil{\_\_init\_\_} constructor.
168+
The x\nobreakdashes-~and y\nobreakdashes-attributes are initialized by the \pythonil{\_\_init\_\_} initializer.
169169
They are marked with the \pgls{typeHint} \pythonilIdx{Final} and thus changing them is an error.
170170
In many cases, making objects immutable is a good design pattern.%
171171
%
@@ -333,7 +333,7 @@
333333
%
334334
This is going to be the interface for our new class~\pythonil{KahanSum}, which, in turn, is based on~\cite{K2006AGKBSA}.
335335

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

text/main/classes/inheritance/inheritance.tex

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@
6565

6666
In \cref{lst:classes:circle}, we define the class\pythonIdx{class}~\pythonil{Circle}.
6767
By writing \pythonil{class Circle(Shape)}\pythonIdx{class}, we declare it as a subclass\pythonIdx{class} of \pythonil{Shape}.
68-
Its constructor \pythonilIdx{\_\_init\_\_}, we supply two parameters, \pythonil{center}, which is an instance of our class \pythonil{Point} from \cref{lst:classes:point}, and \pythonil{radius}, which can either be an \pythonil{int} or a \pythonil{float}.
69-
The constructor first checks if \pythonil{radius} is a finite and positive number.
68+
Its initializer \pythonilIdx{\_\_init\_\_}, we supply two parameters, \pythonil{center}, which is an instance of our class \pythonil{Point} from \cref{lst:classes:point}, and \pythonil{radius}, which can either be an \pythonil{int} or a \pythonil{float}.
69+
The initializer first checks if \pythonil{radius} is a finite and positive number.
7070
Otherwise, it raises\pythonIdx{raise} a \pythonilIdx{ValueError}.
71-
The constructor of the class\pythonIdx{class}~\pythonil{Point} already ensures that both coordinates of \pythonil{point} will be finite.
71+
The initializer of the class\pythonIdx{class}~\pythonil{Point} already ensures that both coordinates of \pythonil{point} will be finite.
7272
This ensures that we indeed create valid circles.
7373
We store \pythonil{center} and \pythonil{radius} in two attributes of the same names.
7474
We annotate them with the \pgls{typeHint} \pythonilIdx{Final}, which means that they should not be changed after the object is constructed.
@@ -118,7 +118,7 @@
118118
We now implement them as classes \pythonil{Rectangle} and \pythonil{Triangle} in \cref{lst:classes:rectangle,lst:classes:triangle}, respectively, which both are subclasses of~\pythonil{Polygon}.
119119

120120
A rectangle can be defined using its bottom-left and top-right corner point.
121-
In the constructor \pythonilIdx{\_\_init\_\_} of the class \pythonil{Rectangle}, we pass in two points~\pythonil{p1} and~\pythonil{p2}.
121+
In the initializer \pythonilIdx{\_\_init\_\_} of the class \pythonil{Rectangle}, we pass in two points~\pythonil{p1} and~\pythonil{p2}.
122122
We raise\pythonIdx{raise} a \pythonilIdx{ValueError} if these would form an empty rectangle.
123123
Otherwise, we store the minimum x\nobreakdashes-~and y\nobreakdashes-coordinate in the bottom-left point attribute~\pythonil{p1} and the maximum x\nobreakdashes-~and y\nobreakdashes-coordinate in the attribute~\pythonil{p2}, marking the top-right corner of our rectangle.
124124

0 commit comments

Comments
 (0)