What is actually happening when you make a variable assignment? This is an important question in Python, because the answer differs somewhat from what you’d find in many other programming languages. Python is a highly object-oriented language. In fact, virtually every item of data in a Python program is an object of a specific type or class.
00:08 These are the three topics that we’re going to talk about mostly. The most important one is that everything’s an object in Python. Then we’re going to talk about that variables are references, and we’re going to look at a couple of examples with some nice graphics as well.
00:21 Then we’re just going to mention what is an orphaned object and what happens in Python with orphaned objects. Let’s get going. So, the first and most important thing that I mentioned before is that everything in Python is an object. You should just keep that in mind, that’s why it’s in this big yellow box.
So just, everything’s an object, ha. Keep that in mind, and you’ll see why it’s important in just a second. So, what happens when we do this standard variable assignment that we talked about before? We’re saying
n = 300.
Let’s head over to the IPython shell and just give it a try, put it in here,
n = 300. Now I can reference
300 with the variable name
n. What actually happens is that I create a reference to an object.
I’m creating a Python object with the value
300 and then I’m pointing the variable name
n to that object. That’s what’s going on here. What happens now if I say
m = n? All right, so I say
m = n. And now I can access
300 also with
m, and I’m accessing the same object.
So what I’m creating here is I’m creating another pointer to the same object, so I’m saying
n points to the object with the value of
300, and now
m also points to that same object. That’s what’s happening when I say
m = n.
What we did is I moved the reference of
n to a different object, just as I did before with
m. At first,
m was pointing to
n was pointing to
300, and now both of them are pointing to different objects.
What happened to
300 is that it became an orphaned object. That means that we don’t have any way of accessing it anymore in our program. All the references to it have been changed, so it just sits there.
And what Python does in that case is called garbage collection. So, at some point in the life cycle of the program, Python notices that there is this object,
300, that there’s no way of it being accessed anymore, so it just comes around and collects it and just removes it out of memory.
04:03 So, as a quick recap about object references, the important thing to remember is that everything is an object. Be it an integer or whatever, anything that we create in Python is actually an object.
And then we talked about variable references. So, you can think of, if you do something like
n = 300, think of it as you’re creating a name for a reference that points to a specific object. And then we quickly talked about orphaned objects, that if you remove all the references to an object, there’s nothing pointing to it anymore. It becomes an orphaned object, and Python automatically at some point comes with its cleanup truck and does the garbage collection on those objects.
Become a Member to join the conversation.