You've seen exam papers or official booklets that have this page:
A blank page with the text "This page is intentionally left blank" in the middle. And, of course, the irony is that by having that text on the page, well, the page is no longer blank.
We face a similar "paradox" when we try to understand Python's None
or its equivalent in other languages. You can't have nothing. So, you have something that represents nothing. Just like the text on the page is text that's telling you that there's no text on the page.
Let's explore emptiness and nothingness in Python.
Not All Emptiness Is Equal
When teaching live courses, I often show the following line of code and ask my students: "What's stored in the variable greeting
?"
"Nothing" is a common reply. But this variable is not empty. It contains a string. The string is empty, but the variable isn't. The name greeting
refers to an object of type string and, therefore, has access to all the string methods. An empty string is something—it's not nothing.
How about other empty structures, such as the following empty list?
The same logic applies—numbers
is not empty. It contains a list that's empty. But numbers
and greeting
have different properties and have access to different methods since they're different data types. They're both empty, but they're not equal.
Let's understand this idea better with a fictitious "data type"—the bus. Imagine an empty bus (the vehicle, not the bus inside your computer!) There are plenty of seats, and they're all empty. This is an empty bus. But it's not "nothing". It's a bus, and it has "bus properties"—it can drive and open its doors and so on. And you can have passengers board the bus and sit in the seats. The bus is no longer empty now.
But what if what you want is not an empty bus but nothing whatsoever?
Coming up…
Understanding
None
When functions return
None
Using
None
as a default argument in functions, especially in place of mutable data typesSome other uses of
None