|
326 | 326 | The \pythonilIdx{float} function also extends the special values that a floating point value can take one. |
327 | 327 | Consequently, \pythonil{float("inf")} gives us~\pythonilIdx{inf} and \pythonil{float("nan")} returns~\pythonilIdx{nan}. |
328 | 328 |
|
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.% |
331 | 350 | % |
332 | 351 | \FloatBarrier% |
333 | 352 | \endhsection% |
|
494 | 513 | Converting strings to the other datatypes that we have discussed so far can, conveniently, be done by using functions of the same names: |
495 | 514 | The function \pythonilIdx{int} converts its argument string to an instance of \pythonilIdx{int}. |
496 | 515 | 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. |
498 | 519 |
|
499 | 520 | Sometimes we want to include characters in our strings that are dodgy. |
500 | 521 | For example, if our string is delimited by \pythonil{"} marking its begin and end, inserting such a \pythonil{"} inside the string would be awkward. |
|
0 commit comments