Fix the Bug
00:00 The last lesson, I successfully finished creating this script, and it works as expected. However, I also mentioned that there is an intentionally built-in issue that maybe you discovered, maybe not.
00:13 Let’s look at what the issue is.
00:16 If you run it once, this script does what you’d expect it to do. However, if you go ahead and run this a couple of times, let’s say we run it ten times,
00:28 you can still stop the video and think about what’s going to happen.
00:33 Now I’m going run it. Okay, it looks like you’ve created one of the poems, then you’ve created another poem, and then there’s a traceback error. So this only created two poems. Do you know why?
00:50
The error that you’re getting is IndexError: list index out
of range
, and it is related to the mutability of lists. What you’re doing in here is popping elements off the list or removing them from the list, right?
01:04
But down here, every time you call make_poem()
, you’re going to essentially work on the same objects in memory. It’s these lists up there. You may have changed the references to them.
01:16 You may access them with different references, but they’re still the same objects. So you are removing and popping from these lists, which means that eventually there’s not going to be enough items in there to give you the selection that you’re trying to get inside of your script, right?
01:33 Okay. So now that we know what the problem is, the solution is not that hard to come by, which is because we’ve learned about it in the associated course, right?
01:42 So what you need is to make a deep copy of those lists instead of just using these objects in there. Then every time you’re going to work with a different list and the items that you pop off or that you remove don’t really matter in the next run of the loop, because there’s going to be a fresh new list, okay?
01:59
And you can create that by importing the copy
module, import copy
, and then at the beginning of make_poem()
, I’m just going to say words
equals copy.deepcopy()
02:22
Create a deep copy of the words to pull. Because I’m creating a deep copy, it’s actually also going to create copies of all the lists in there, which is exactly what you want, because you want to have a new list for each time that you’re calling make_poem()
. Does it work? Give it a run.
02:41 Create another ten poems at once, and you can see that now we are not running into any error,
02:47 though the spacing is a little off, but you actually get ten poems that start with the title, and then a space, and then the actual poem. Okay. Just as a little reminder that this mutability of lists is a very cool thing.
03:02 You know all of the things that you’ve done in here, basically by shuffling the list, for example, you can only do with lists because they’re mutable, but they also have this downside to it, which is that you need to be aware of what’s the object you’re actually working with.
03:15 And in this example, are you depleting the object? So make sure that you have an actual copy that you’re working with in a situation like that, and then you’re not going to run into any troubles.
03:27 All right? But I’m not quite happy with this code yet. So before we start to phase out this course, join me for another refactoring session in the next lesson.
Become a Member to join the conversation.