None and How to Test for It

00:00 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.

00:24 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 int keyword.

00:34 This is just telling us that these variables are going to hold integers. Next, we have the name of those variables, so height and 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.

01:01 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 None variable.

01:17 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 1,

01:33 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.

01:48 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,

02:06 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 print() statement.

02:25 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.

02:35 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.

02:45 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 print().

03:02 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.

03:19 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 "goodbye" and "hello". So we’ve created this object match and of course, in this case, there is no match. "goodbye" is not in "hello".

03:36 So I can test this by saying if match is None:,

03:41 then let’s print("no match").

03:46 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

03:57 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 x and y from up here?

04:09 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 1.

04:19 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.

04:28 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 x to None and y to None? So, are they still equivalent to each other? Yes, they are. They’re both None. Are they both the same?

04:52 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 None.

05:09 You can confirm this by using id().

05:12 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.

05:34 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.

06:02 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 False does.

06:10 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!

Bart Willems on July 28, 2020

I still struggle with “because None is a singleton you have to use is, and what I found it this:

In most cases == and is return the same result, but it’s possible to have objects where the equality operator is overriden and might give a wrong result (simplified example):

class Foo:
  def __eq__(self, other):
    return True

x = Foo()
x == None # True
x is None # False

Because the equality operator can give you the wrong answer you should use is, but because None is 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.

Become a Member to join the conversation.