Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Avoiding Conflicts

00:00 In this lesson, you will learn about avoiding conflicts with Python keywords and built-in names by using a trailing underscore. So what do I mean by that?

00:11 Well, shortly you will work through an example where you use list, which is a built-in data type, but you will use it for your own purposes to then discover that that causes an error.

00:21 And to avoid the error, you will use list_ instead of list.

00:27 So if you wouldn’t mind switching to your REPL

00:31 or your terminal if your REPL isn’t active yet, if you are in your terminal, please start your REPL by typing python or python3.

00:40 So in your REPL, let’s first have a look at what the list() function does when you apply it to a string. So list() is applied to a string and I’m going to use “Steven” because I lack imagination.

00:54 And what that gives me is a list of each individual character of that string. So that is what that list() function does and we will come back to that in a second.

01:07 Now let’s assume you want to create your own code and you need a list that has the integers 1, 2, 3, and 4. So you go, well my list is my list, which has 1, 2, 3, and 4.

01:22 There’s my list. So let’s have a look at what my list is. Well, my list is indeed 1, 2, 3, and 4. So, so far so good. But what if in your same code you now need to apply list() of “Steven” as we have done before?

01:38 What does that give me? It gives me an error message. Now why does that give me an error message? Because up here that was working fine, list() of “Steven”, this is exactly the same code, list() of “Steven” should have given me this list, whereas it’s now giving me an error message.

01:54 And what is this saying? It is saying the list object isn’t callable. So what just happened? Why can’t you call the list constructor?

02:03 Well, that is because your code, where you set list equal to this list of 1, 2, 3, and 4, that actually overwrote the built-in name list.

02:13 And now the name list applies to a list object, which is the object that you have created rather than the list class, which is what we would call the list() function.

02:24 Okay, so basically this list used to work, you have now changed it into a list object and therefore it’s saying, well this list object, I can’t call that because the list object isn’t callable.

02:37 How to fix that? Well that’s relatively straightforward. If we come out of the REPL to remove the list object from memory, and let’s start again. So Python Ctrl + L to clean the screen and now instead of typing list equals 1, 2, 3, 4.

02:56 So 1, 2, 3, and 4. We are going to type list_. We use a trailing underscore to create a different name, effectively. So now I’ve created that and now list() of “Steven” should still work because we have not created a list object to override the list class.

03:22 Well, there you go. That is how you avoid clashes with built-in names. And of course this doesn’t just apply to list, this applies to all other built-in names.

03:32 If you would like to use them for your own purposes, then you add a trailing underscore.

03:38 A quick note here that of course, instead of a trailing underscore, I could have used any other prefix or suffix as long as that created a valid name that was different from list.

03:49 So I could have used a_list or list1, for example.

03:54 Okay, that’s it for trailing underscores. In the next lesson you’ll discover some other uses of the underscore in Python.

Become a Member to join the conversation.