Skip to content

Commit f05b948

Browse files
committed
fixed error in the description of the function bool
1 parent 1c6ba12 commit f05b948

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

bookbase

text/main/basics/simpleDataTypesAndOperations/str/str.tex

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,27 @@
326326
The \pythonilIdx{float} function also extends the special values that a floating point value can take one.
327327
Consequently, \pythonil{float("inf")} gives us~\pythonilIdx{inf} and \pythonil{float("nan")} returns~\pythonilIdx{nan}.
328328

329-
Finally, the function \pythonilIdx{bool}\pythonIdx{bool!function} converts the strings \pythonil{"True"} and \pythonil{"False"} to \pythonilIdx{True} and \pythonilIdx{False}, respectively.
330-
With this, you are also able to convert strings to data that you can use as input for your computations.%
329+
But the function \pythonilIdx{bool} works \emph{differently} from what one might expect~\cite{PSF:P3D:TPSL:BIF:B}.
330+
\pythonil{bool(\"True\")}\pythonIdx{bool({\textquotedbl}True{\textquotedbl})} indeed yields \pythonilIdx{True}.
331+
However, \pythonil{bool(\"False\")}\pythonIdx{bool({\textquotedbl}False{\textquotedbl})}, too, yields \pythonilIdx{True}.
332+
One would think that \pythonil{bool(\"False\")}\pythonIdx{bool({\textquotedbl}False{\textquotedbl})} should return \pythonilIdx{False}, but it does not.
333+
The reason is that \pythonilIdx{bool} does not compare the string it receives as argument to the string constants that \pythonil{str(True)} and \pythonil{str(False)} return.
334+
Instead, it performs a \emph{truth value testing} procedure~\cite{PSF:P3D:TPSL:BIF:B,PSF:P3D:TPSL:TVT}.
335+
336+
You see, in \python, we can test many objects for their truth value.
337+
\pythonilIdx{True} and \pythonil{False} obviously have truth values \pythonilIdx{True} and \pythonilIdx{False}, respectively.
338+
By default, other objects have truth value \pythonilIdx{True} unless it offers an explicit conversion to \pythonilIdx{bool}\footnote{%
339+
Via the dunder method \dunder{bool}, see later in \cref{sec:dunderMethodsOverview}.} %
340+
that yields~\pythonilIdx{False} or has a length and that length is zero\footnote{%
341+
Via the dunder method \dunder{len}, see later in \cref{sec:dunderMethodsOverview}.}.
342+
Other objects that have a truth value of \pythonilIdx{False} are numeric types which are zero, such as \pythonil{0} and \pythonil{0.0} as well as empty collections~(which we learn about a bit later in \cref{sec:collections}).
343+
Now, \pythonil{\"False\"} is not empty, i.e., has a length greater than~0.
344+
Therefore, it has truth value \pythonilIdx{True}, meaning that \pythonil{bool(\"False\")}\pythonIdx{bool({\textquotedbl}False{\textquotedbl})} actually yields \pythonilIdx{True}!
345+
The empty string, on the other hand, has a truth value of \pythonilIdx{False}, i.e., \pythonil{bool(\"\")}\pythonIdx{bool(\textquotedbl\textquotedbl)} gives us \pythonilIdx{False}.
346+
347+
It should be noted that these conversion functions also work with other datatypes.
348+
For example, \pythonil{float(0)} converts the integer~\pythonil{0} to the \pythonil{float} value~\pythonil{0.0} and \pythonilIdx{bool(0)} gives us \pythonilIdx{False}.
349+
Anyway, you are now also able to convert strings to data that you can use as input for your computations.%
331350
%
332351
\FloatBarrier%
333352
\endhsection%
@@ -494,7 +513,9 @@
494513
Converting strings to the other datatypes that we have discussed so far can, conveniently, be done by using functions of the same names:
495514
The function \pythonilIdx{int} converts its argument string to an instance of \pythonilIdx{int}.
496515
The function \pythonilIdx{float} converts its argument string to an instance of \pythonilIdx{float}.
497-
And the function \pythonilIdx{bool}, well, you guess it.
516+
But the function \pythonilIdx{bool} works very differently!
517+
\pythonil{bool(\"True\")}\pythonIdx{bool({\textquotedbl}True{\textquotedbl})} and \pythonil{bool(\"False\")}\pythonIdx{bool({\textquotedbl}False{\textquotedbl})} both yield \pythonilIdx{True}, while \pythonil{bool(\"\")}\pythonIdx{bool({\textquotedbl\textquotedbl})} gives us \pythonilIdx{False}.
518+
This is quite dangerous.
498519

499520
Sometimes we want to include characters in our strings that are dodgy.
500521
For example, if our string is delimited by \pythonil{"} marking its begin and end, inserting such a \pythonil{"} inside the string would be awkward.

text/main/classes/dunder/dunder.tex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,6 +1611,7 @@
16111611
\endhsection%
16121612
%
16131613
\hsection{Overview over Dunder Methods}%
1614+
\label{sec:dunderMethodsOverview}%
16141615
%
16151616
\begin{figure}%
16161617
\renewcommand*\DTstylecomment{\relax}%

0 commit comments

Comments
 (0)