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.
So, without getting too deeply into the syntax of C, what is happening here is that two variables are being declared. The first word on each line is the
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
So if I do something like this, you can see that I’m getting a
None printed back. In fact, something curious is that of course
print() itself is a function.
I give it a string, I call
print(), and the string is printed. So,
print() has actually no return value, and this means that
print() is returning
None, but you don’t see it.
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 I can test this by saying
if match is None:,
And that’s what happens, we get a
"no match" printed out. Now, something which I would like to draw your attention to is that here I used the keyword
is. I didn’t use the quality
operator, two equal signs, but instead I use the identity operator. And this highlights something which is very important about
None. So, do you remember our friends
y from up here?
If I check whether or not they are equivalent, this is
True. They both have the same value. They have a list with a single item, which is the number
But what if I check if
x is y? In that case, this is
False. 74 These are two different instances of something with the same value.
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
You can confirm this by using
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.
None is falsy, so if you’re using it in, for instance, decision structures or anything like that, you can often assume that it will behave the way
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.
Bart Willems on July 28, 2020
I still struggle with “because
Noneis a singleton you have to use
is, and what I found it this:
In most cases
isreturn the same result, but it’s possible to have objects where the equality operator is overriden and might give a wrong result (simplified example):
Because the equality operator can give you the wrong answer you should use
is, but because
Noneis a singleton it might lead you to think you can use
==as well. Which will work in many cases—and then you’ll spend hours figuring out why your code doesn’t work.