Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Mutable Default Arguments

00:00 As we continue our look at places outside of a Boolean context where we can use the or operator, we’re now going to take a look at how the or operator can help us when we’re trying to write a function whose intent is to modify the argument value it’s given while at the same time providing a default value for that argument.

00:24 Here’s an illustrative example, perhaps not practical, but we’re just trying to get a sense for what the problem is and what the solution is. So, the intent of this mutable_default() is to append 1 to the end of the list that it’s provided, and if we’re not provided a list at all, create a new list and have 1 added to it.

00:49 This looks like it should work.

00:54 And so here’s our mutable_default().

00:58 Let’s go ahead and import this. We’ll call this python_or_da1 (default argument 1).

01:10 Let’s create a list

01:15 and call mutable_default() on my_list.

01:25 And we can see that it’s printed out that list, but more importantly, also that list has been changed. Now, if I call mutable_default() without a parameter, we should expect a new list to be created with 1 appended to it.

01:42 And that looks good—until we try it again.

01:47 What’s happened is that same default parameter value—when we look at it here—this name lst was created when I imported this function, and so every time we call this without a parameter, it’s going back and grabbing this list that was already created.

02:11 It was empty at the beginning, and then we added 1 to it. And then as we saw, we just appended 1 to it again. And I can continue this, and that’s not the effect that we want.

02:25 We want each time when we don’t provide it a parameter a new list to be created. What happened here is what we want to happen every time.

02:38 So, let’s take a look at a possible solution. Instead of creating an empty list, let’s just create the variable and say that it’s going to be equal to None if no list is provided.

02:53 I have that here in python_or_da2, and I’m going to call this mutable_default2() just so we don’t get all of our mutable_default() functions confused.

03:06 So now I’m going to import everything from python_or_da2. my_list already has 2, 3, and 1, so if I call mutable_default2() on my_list, it appended a 1 to it again.

03:32 We’re can see it just didn’t print out that. It’s actually been changed. And now if I don’t provide a parameter, the default one, lst, will be used.

03:46 Let’s take a closer look at this. Because each time we either use lst or an empty list, if no parameter was provided, lst is None, which evaluates to False.

04:00 So when the or encounters the left operand to be False, we use the right operand, which gives us an empty list. So each time we call this with an empty set of parentheses to use the default value, lst is assigned to None, it’s False, and so then we reassign it to be an empty list.

04:23 Now, there’s still a catch, and this is a possibility that perhaps using the or might not be the best solution. I mentioned that before. So, suppose that I create a new list, but I don’t have anything in it yet, and so I want to call mutable_default2() on my_new_list. Well, it looks like things worked but, in fact, they didn’t.

05:00 my_new_list wasn’t changed, and the reason it wasn’t changed was because lst had the parameter my_new_list. my_new_list was empty, and so this evaluated to be False, and so a new empty list was created.

05:20 And so if there’s a possibility that the parameter supplied—the meaningful parameter supplied—is going to be an empty list, this solution isn’t going to work.

05:33 In fact, you’re probably not going to be able to use an or statement at all. You want to explicitly check to see if the list is an empty list or if it’s None, because if it’s None, then you know a parameter wasn’t provided, but if it’s not None, then it’s a meaningful list that you can append to.

05:59 So, as mentioned before, sometimes or isn’t the best way to implement the solution to a problem but in many situations it can be and it’s important to know, if you’re going to use it, how to use it correctly.

06:17 In your next lesson, we’re going to see how it can help us prevent getting a division by zero error when we’re trying to do arithmetic.

Become a Member to join the conversation.