None and How to Test for It
Welcome back. This is the second video in this tutorial, and here I’m going to tell you a bit more about
None and how to test for it. Let’s jump right into the code editor. Okay, so we have some code here.
00:11 If this looks a bit strange, weird, and unpleasant, that’s because it’s not actually Python code, it’s C. And the reason I’m showing you this is because I’d like to draw your attention to two lines, lines 4 and 5.
This is just telling us that these variables are going to hold integers. Next, we have the name of those variables, so
weight, followed by a semicolon, and the semicolon ends the statement. But, there is no value. Each of these variables is being declared but don’t point to anything. So if we were to try to use them, we would find that they’re pointing to
null. In many languages this is treated as equivalent to
0, but in Python, things are a bit different.
So, switching over to Python, let’s go over to the REPL. Here I am at the command line, and I’m going to start the REPL. Now, although you can assign
None to a variable in Python, and we’ll see that in a few minutes, I can’t just create a
What I mean by that is that in Python, variables are created by assigning them, so there’s no such thing as a really empty variable, right? For instance, if I create a variable
x and assign it to a list with a single item
then another variable
y with the same value, then whenever I’m creating these variables, I’m also assigning something to them. And it’s this act of assigning which is creating the variables. These might seem an odd choice of example, 30 but we’ll get back to them in a second.
One of the most common situations where you’ll encounter
None in Python is when you have a function which has no return value. So for instance, if I create a function and call it
no_return() and all it does is have a
pass statement, so it doesn’t do anything at all, I can of course call this function,
but nothing happens. Except under the hood, something is happening. It’s returning a value and that value is a
None value, but
Nones are so common and so pervasive, that the REPLs often just hide them from you. You can force the REPL’s hand, you can force it to display the
None by using a
And you can see it by nesting a
print() in another
print(), like this, and then you’ll see that the inner
print() is printing
"hi" in this case, and the outer
print() is printing the
None value, which is being returned by the inner
And the outer
print(), in turn, is also returning a
None, which we’re not seeing but we could see with another
print(), and so on. Another situation where you might use
None is in comparative structures, so if you’re checking for something and you want to see if that is returning a match or a
None. I can tell you this with an example.
I’m going to start by importing regular expressions. Let’s create a variable here called
match and set this to the result of matching
"hello". So we’ve created this object
match and of course, in this case, there is no match.
"goodbye" is not in
So although it’s the same value, it’s two instances. Think of it as two $1 bills. They both have the same value, but you have two of them. But what happens if I set
None? So, are they still equivalent to each other? Yes, they are. They’re both
None. Are they both the same?
Yes, that is also
True, and this is a very important point, and that’s that there is only one
None in Python.
None is a singleton, so there is one
None in all of Python and whenever things are assigned the value
None, then they’re all pointing at the same
So this gives us the address in memory of
x, and if we check
y’s address in memory, it’s the same. They’re both pointing to exactly the same object. So
None is a singleton, it’s an object, it’s a constant, as you can tell by the fact that it’s capitalized. You can assign it to things, but you can’t assign anything to it, and you can’t subclass from it.
It is an object, it’s, let’s say, a full citizen of Python, but it does have certain particularities. So, the key things to remember here are that
None is the return value of functions that don’t have a return value, it’s an object, a constant, and a singleton. This last one,
None being a singleton, is particularly important because it means that if you want to check if something is
None, you should use the
is keyword and not simply the quality operator.
But if you want to be absolutely sure that something is
None, then you do need to use the
is keyword. Okay. That’s it for this video. In the next one, I’ll tell you about using
None as a default parameter. I’ll see you there!
Become a Member to join the conversation.