None as a Default Parameter
Welcome back. In this video, I’ll tell you about using
None as a default parameter. The strongest argument in favor of using
None as a default parameter is that it avoids the need to use a mutable data type as a default parameter.
That’s a bit of a mouthful but it’s easier to understand if I show you with an example. Here we are in the REPL. I’ll show you why using
None as an input parameter is a good idea by showing you what happens when you don’t use
None as an input parameter. So, first of all, I’m going to create a function and I’ll call it
Next, there’s a
print() statement whose purpose I’ll show you in a minute. And finally, I return the
starter_list. Okay, let’s try playing around with this. I’m going to start by creating a list
So in this case, we created a new list and added
5 to it, and you can see that the
ID is different from the previous list. Okay, let’s call a function again and add the number
6, and here is the issue. If you look at the
ID, this is the same as the one we had when I called the function just before. The issue here is that since I’m using a mutable object, it’s being recycled rather than restarted each time.
So when I call the function again, I’m working with the same object instead of having a fresh one. So, how can we get around this? Well, I’ll show you by creating a new function. At first,
good_function() looks a lot like
bad_function(), but the key difference is here. Rather than setting the
starter_list to an empty list, like we did up here, in this case, we’re setting it equal to
print() this just to confirm that we’re using different starter lists, and then we return our
starter_list. Okay, let’s try running this, first with our list we had already created. This time I’m going to try adding a letter.
Okay, so here we go. That created an empty list and added
2 to it. Let’s call it again with a different number. And this time, rather than having a sticky list, we are getting a new list each time. You can see this, because the second time we call this, there’s only one number.
This requires a little bit of tweaking. What I’ll do, first of all, is I’m going to create a class and this class will help regulate the behavior of my function when it receives
None as an input. Let me show you what I mean.
First of all, let’s create this class, and this doesn’t really do anything. All it does is
pass. Next, I’m going to create
good_function() again, but with a few tweaks, and the first of these is that I’m going to set a default value for
new_elem, and that value is
DontAppend. I’ll add what I had above here again, so this check whether or not
starter_list is None.
And if it is
None, then let’s create an empty list. Now we’ll do something a little bit different. I’m going to check
if new_elem is not DontAppend, and if it isn’t, then what I’ll do is I’ll
append(). Okay, now we can return
starter_list, and let’s try running this.
So first of all, I’m going to run it only passing my list, the
starter_list from above, and that was added to an empty function, so that’s working. Now let’s see what happens if I add
None to it.
So, the key things to remember here are that you can use
None as a default parameter when using mutable data types would result in strange things happening, or unwanted things happening, and also that if you need to be able to add
None as a valid input to something, then you can create a class that you can use as a signal not to accept an input. In this way, you free
None to be used as an input if it comes up. Okay.
Become a Member to join the conversation.