|
16 | 16 |
|
17 | 17 | \Cref{exec:none} illustrates some of the things we can do with \pythonilIdx{None}. |
18 | 18 | If we write \pythonilIdx{None} into the \python\ console, then nothing happens. |
19 | | -In the past, we just wrote values, such as \pythonil{34} and, after we hit \keys{\enter}, they would be printed again. |
| 19 | +In the past, we just wrote values, such as \pythonil{34} and, after we hit~\keys{\enter}, they would be printed again. |
20 | 20 | Not so~\pythonilIdx{None}. |
21 | | -If we want to print~\pythonilIdx{None}, we have to force it by using the function \pythonilIdx{print}. |
| 21 | +If we want to print~\pythonilIdx{None}, we have to force it by using the function~\pythonilIdx{print}. |
22 | 22 | \pythonil{print(None)} then indeed prints~\pythonilIdx{None}. |
23 | 23 |
|
24 | 24 | The value \pythonilIdx{None} has many use cases. |
|
29 | 29 | \pythonilIdx{None} would be much clearer, as no arithmetic calculation could ever return~\pythonilIdx{None}. |
30 | 30 |
|
31 | 31 | For this to work we need to be able to check whether a value is~\pythonilIdx{None}. |
32 | | -And we can do this using the operator~\pythonilIdx{is}.\footnote{% |
33 | | -\pythonilIdx{is} is a bit similar to \pythonilIdx{==}, but instead of comparing whether the contents of an object are the same, it compares whether two values reference the same object.}% |
34 | | -\pythonil{1 is None} gives us \pythonil{False} and so does \pythonil{"Hello World!" is None}. |
| 32 | +We can use the \pythonilIdx{==}~operator for it, but its use is discouraged~\cite{PEP8}. |
| 33 | +We do it here anyway, just for demonstration purposes, because you are already very familiar with~\pythonil{==}. |
| 34 | +\pythonil{1 == None} gives us \pythonil{False} and so does \pythonil{"Hello World!" == None}. |
| 35 | +\pythonil{None == None}, however, is \pythonil{True}. |
| 36 | +This is a bit interesting, because \pythonil{nan == nan} would give us~\pythonil{False}, but then again, \pythonil{nan} means \inQuotes{undefined} and \pythonil{None} means \inQuotes{nothing.}% |
| 37 | +% |
| 38 | +\bestPractice{cmpWithNone}{% |
| 39 | +Comparisons to singletons like \pythonil{None} should always be done with \pythonil{is} or \pythonil{is not}, never the equality operators~\python{==} or~\pythonil{!=}~\cite{PEP8}.}% |
| 40 | +% |
| 41 | +As stated above, the right way to compare values if \pythonil{None} may be involved is using the operator~\pythonilIdx{is}~\cite{PEP8}. |
| 42 | +\pythonilIdx{is}~is a bit similar to~\pythonilIdx{==}, but instead of comparing whether the contents of an object are the same, it compares whether two values reference the same object. |
| 43 | +As expected \pythonil{1 is None} gives us \pythonil{False} and so does \pythonil{"Hello World!" is None}. |
35 | 44 | \pythonil{None is None}, however, is \pythonil{True}. |
36 | 45 |
|
37 | 46 | Also, functions that do not return any result do, in fact, return~\pythonilIdx{None}. |
|
0 commit comments