In Python programming, objects hold a reference to manage the memory resources efficiently. In simple terms, an object reference is a memory location where the data of the object is stored. This tutorial will guide you on how to get an object reference in Python, which is useful when trying to understand how objects work in this popular programming language.
Before we dive into the steps, it’s important to note that Python is a highly object-oriented language, which means that almost everything in Python is an object. However, Python handles objects differently from other languages, such as C++ and Java. Even though Python objects have a unique memory address or location, the language provides a layer of abstraction that abstracts these addresses and memory management away from users.
To understand the object references better, let’s follow the steps below.
Step 1: Check the object reference using the id()
function
You can use the built-in id()
function in Python to check the object reference or memory address of any object. The id()
function takes an object as a parameter and returns its reference value.
Consider the following example.
1 2 3 4 |
num_1 = 42 num_2 = 21 print("Object reference of num_1:", id(num_1)) print("Object reference of num_2:", id(num_2)) |
Output:
Object reference of num_1: (some_memory_address1) Object reference of num_2: (some_memory_address2)
As you can see, the id()
function returns the memory addresses of both variables num_1
and num_2
.
Step 2: Check reference assignments
When you assign a variable to another variable in Python, both variables will point to the same object reference. This is because Python manages memory resources efficiently by reusing existing object references if a new variable contains the same value as the existing one.
Let’s see an example of this.
1 2 3 4 |
a = "Python" b = a print("Object reference of a:", id(a)) print("Object reference of b:", id(b)) |
Output:
Object reference of a: (some_memory_address3) Object reference of b: (some_memory_address3)
Notice how the object references for a
and b
are the same even though we assigned a
to b
. This is because Python intelligently reuses memory addresses for equal values.
Step 3: Check reference changes after value change
When the value of a variable is changed, Python creates a new object for the variable, and the reference address will change accordingly.
Consider the following example.
1 2 3 4 5 6 7 8 |
a = "Python" b = a print("Object reference of a before change:", id(a)) print("Object reference of b before change:", id(b)) b = "Programming" print("Object reference of a after change:", id(a)) print("Object reference of b after change:", id(b)) |
Output:
Object reference of a before change: (some_memory_address4) Object reference of b before change: (some_memory_address4) Object reference of a after change: (some_memory_address4) Object reference of b after change: (some_memory_address5)
After changing the value of b
to “Programming”, the object reference of b
is also changed, while the object reference of a
remains the same.
Now let’s see the full code of the above examples.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
num_1 = 42 num_2 = 21 print("Object reference of num_1:", id(num_1)) print("Object reference of num_2:", id(num_2)) a = "Python" b = a print("Object reference of a:", id(a)) print("Object reference of b:", id(b)) a = "Python" b = a print("Object reference of a before change:", id(a)) print("Object reference of b before change:", id(b)) b = "Programming" print("Object reference of a after change:", id(a)) print("Object reference of b after change:", id(b)) |
Conclusion
Understanding object references in Python is essential for learning memory management and the object-oriented nature of the language. In this tutorial, we learned about how to use the built-in id()
function to get object references and saw how object references change when their values change. With this knowledge, you should have a deeper understanding of how Python handles objects internally.