Skip to content

Commit 77448bc

Browse files
committed
finished overview on dunder methods, added summaries to other class sections
1 parent 3bd81ca commit 77448bc

File tree

4 files changed

+148
-15
lines changed

4 files changed

+148
-15
lines changed

bibliography/bibliography.bib

+1-1
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ @string { p_pragmatic_ai_labs
593593
@string { p_pragmatic_bookshelf = "{{Pragmatic Bookshelf} {by} {The Pragmatic Programmers, {L.L.C.}}}" }
594594
@string { p_princeton_university_press = "{Princeton University Press}" }
595595
@string { p_project_gutenberg = "{Project Gutenberg Literary Archive Foundation}" }
596-
@string { p_python_morsels = "{Python Morsels}" }
596+
@string { p_python_morsels = "{\python\ Morsels}" }
597597
@string { p_python_software_foundation = "{\python\ Software Foundation~{(PSF)}}" }
598598
@string { p_readthedocs_inc = "{Read the Docs,~Inc.}" }
599599
@string { p_springer_cham = "{Springer}" }

text/main/classes/basics/basics.tex

+25-1
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,31 @@
469469
If we had a generator of a sequence of values and wanted to compute the sum of the values themselves as well as the sum of their squares, we could simply use two instances \pythonil{KahanSum} while iterating over the sequence.
470470
With \pythonilIdx{fsum}, we would need to iterate over the sequence twice, which may not be practical if the sequence is generated from some outside source and was too long to just store it in memory.%
471471
%
472-
\FloatBarrier%
473472
\endhsection%
474473
%
474+
\hsection{Summary}%
475+
Classes allow us to solve two problems in programming.
476+
First, we can semantically group data and the operations on the data together.
477+
Second, we can create define an interface, i.e., an \pgls{API}, that consists of multiple operations and implement it in different ways.
478+
For the first case, we have seen two examples in this section.
479+
480+
Instances of our \pythonil{Point} class store a pair of coordinates in the two-dimensional Euclidean plane.
481+
The operation \pythonil{distance} is inseparably linked to this data structure.
482+
When developing it, we also learned that it is generally a good idea to make objects \emph{immutable}.
483+
If the attributes of an object are set only during its initialization (in the \dunder{init} method) and never change, then there can never be any confusion about their value.
484+
It cannot happen that one part of our program has a reference to a \pythonil{Point} variable and \inQuotes{thinks} that it's coordinates are~$(0,1)$, but some other code changed the coordinates to something else.
485+
This cannot happen precisely because the values of the coordinates in instances of~\pythonil{Point} can never change.
486+
487+
If the attributes need to change, then it is often a good idea to \emph{encapsulate} them.
488+
Encapsulation means that the attributes of an object can only be changed via methods of the object.
489+
Our \pythonil{KahanSum} class is an example of this.
490+
This class allows us to add numbers more accurately by internally keeping track of errors resulting from normal summation.
491+
The user never gets to see these internal attributes and also can never modify them directly.
492+
Instead, they are changed by passing new numbers to the \pythonil{add}~method.
493+
Via the method~\pythonil{result}, the user can get a consistent view of the state of the summation without getting confused about its internal state.
494+
495+
In the next section, we will see how inheritance of classes in \python\ can be used to implement \pglspl{API}, which is the second big use-case of classes.%
496+
\endhsection%
497+
%
498+
\FloatBarrier%
475499
\endhsection%

0 commit comments

Comments
 (0)