|
340 | 340 | that yields~\pythonilIdx{False} or has a length and that length is zero\footnote{% |
341 | 341 | Via the dunder method \dunder{len}, see later in \cref{sec:dunderMethodsOverview}.}. |
342 | 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}) or the empty string. |
| 343 | + |
| 344 | +Strings by themselves do not support an explicit conversation to the type \pythonil{bool}. |
| 345 | +The function \pythonil{bool} thus has to figure out the truth value by itself. |
| 346 | +However, strings do have a length, as we already learned. |
| 347 | +You can get the length via the \pythonilIdx{len} function. |
343 | 348 | Now, \pythonil{\"False\"} is not an empty string. |
344 | 349 | It has a length greater than~0. |
345 | | -Therefore, it has truth value \pythonilIdx{True}, meaning that \pythonil{bool(\"False\")}\pythonIdx{bool({\textquotedbl}False{\textquotedbl})} actually yields \pythonilIdx{True}! |
| 350 | +\pythonil{len(\"False\")} gives us~5. |
| 351 | +Therefore, \pythonil{\"False\"} has truth value \pythonilIdx{True}, meaning that \pythonil{bool(\"False\")}\pythonIdx{bool({\textquotedbl}False{\textquotedbl})} actually yields \pythonilIdx{True}! |
| 352 | + |
346 | 353 | The empty string, on the other hand, has lengh zero. |
347 | 354 | Therefore, it has a truth value of \pythonilIdx{False}, i.e., \pythonil{bool(\"\")}\pythonIdx{bool(\textquotedbl\textquotedbl)} gives us \pythonilIdx{False}. |
| 355 | +Any other string, for example \pythonil{\"blabla\"} is also none-empty and thus has truth value \pythonil{True}. |
348 | 356 |
|
349 | 357 | It should be noted that these conversion functions also work with other datatypes. |
350 | 358 | 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}. |
|
0 commit comments