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

Unlock This Lesson

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

Unlock This Lesson

Experiment With Object References

00:00 The previous lesson, I was a little surprised that this is actually working, even though I’m overriding nouns with an empty list. So this variable nouns now points to an empty list and not anymore to this badger, horse, et cetera list.

00:12 But I actually built in a little safety with this words tool that I created where I’ve collected all of those different lists in there, right?

00:20 Because remember that the list objects exist in memory somewhere, and then all the variable names are just references to those objects in memory. So the list that contains badger, horse, et cetera, exists in memory.

00:33 And by doing line 17, overriding the variable nouns with a reference to an empty list object, all it does is just override the reference. So the list still exists, and because I’m accessing this list of nouns through my words tool, this still works out okay, but let’s look at that also with a little code on the side

00:56 to reproduce what happened there, just because I think it’s interesting and kind of gives a bit of this pointer also to how references work in Python, we defined a nouns list.

01:07 I’m going to call it n for short, and I’ll just put two numbers in there for now, the integers just one and two.

01:15 If you went ahead now and said nouns equals an empty list, then if you look at nouns, then it is an empty list because this is what the variable n now points to.

01:29 But this object still exists in memory. Currently, it’s just not accessible in that code, in the code inside of the REPL here. I can’t access this list that contains one and two anymore because there’s no references to it.

01:41 However, in the code that I wrote in the script, I created another way to access it. So I said, now let’s re-create this nouns placeholder list, right?

01:53 And then I created a words tool

01:56 that contained a reference to the list object.

02:02 Currently I can access, oops, right? I forgot the comma. Another thing to note is that if you don’t put a comma there, right? The parentheses are not what makes the tuple, but the comma is what makes the tuple.

02:18 So I need to, I need to do this to now actually create a tuple that in this case consists only of one element, but like this, it’s actually a tuple.

02:27 Now I can access the list through an index of the words tuple. This is one reference that I have to get to that list object. And currently n is another reference that I have to get to that list object.

02:44 But now if I override n so that another time now n points somewhere else to an empty list object, and I’ve lost that reference to that object in memory, but I still can access the same object in memory through this other reference that I have, which is the index position zero of the words tuple.

03:06 So this is what happened over here in the code, and that’s why the code works out the way it is, even though I would say it’s a little tricky, right? Because if I didn’t put in line 10 here and wrapped all of these lists inside of a tuple, then I would’ve lost my reference to nouns in line 17 by reassigning that variable to an empty list.

03:28 To avoid such potential pitfalls, what I am going to do now is I’m going to create another scope by writing the logic code that I’m working with inside of a function.

03:41 Then I can create new variable names that even have the same name, but they sit in a different scope, so they’re not going to override anything.

Become a Member to join the conversation.