4. Objects and References#

In Python, objects and References play different roles, and there are various ways to work with them.

  1. objects: They are the actual entities that store and manipulate data. They have attributes (data) and methods (functions) associated with them. Objects are created from classes and exist in memory.

  2. References: also known as variables, are identifiers that point to objects in memory. They act as labels or names that refer to objects. Multiple references can point to the same object, allowing multiple variables to access and manipulate the same data.

The data types that can be used in defining an object in python are shown below.

The following example illustrates the concept of object referencing in Python. By assigning one object to another, changes made to one object affect the other since they both point to the same object in memory. This highlights the behavior of objects as mutable entities that can be shared and modified through different variables.

person1 = {"name": "John"}
person2 = {"name": "Jane"}

# Assigning person1 to person2
person2 = person1

person1["name"] = "Michael"  # Modifying person1's "name" attribute

print(person1["name"])  # Output: Michael
print(person2["name"])  # Output: Michael (person2 reflects the change)
Michael
Michael

In this example, two dictionaries person1 and person2 are created, representing people with a name attribute. Assigning person1 to person2 makes both variables refer to the same dictionary object.

Consequently, modifications made to the dictionary through either variable will affect the other, as they both reference the same object. For instance, changing the value of the “name” key in person1 to “Michael” updates person2[“name”] to “Michael” as well.

Copying Objects#

When working with objects, it is important to understand the concepts of copying. Copying objects can be achieved using the copy module, which provides functions for both shallow and deep copying. This is done by typing import copy at the begining of your python project.

Understanding these techniques is important to create independent duplicates or references to the original object. The table below will highlight each copying method that can be used, notice that ‘copy’ and ‘shallow copy’ do the same thing:

Function

Description

How to use

copy

Creates a shallow copy of an object

copied_object = copy.copy(original_object) or copy()

shallow Copy

Creates a new object with shared data references

copied_object = original_object[:]

deepcopy

Creates a new object with independent data copies

copied_object = copy.deepcopy(original_object)

Here is an example to illustrate how we can use the 3 differenct copy functions:

import copy

original_list = [1, 2, [3, 4]]

# Example of copy
copied_list = copy.copy(original_list)
original_list[0] = 100
original_list[2].append(5)
print("Copy Example:", copied_list)  # Output: [1, 2, [3, 4, 5]]

# Example of deepcopy
deep_copied_list = copy.deepcopy(original_list)
original_list[2].append(6)
print("Deepcopy Example:", deep_copied_list)  # Output: [1, 2, [3, 4, 5]]

# Example of shallow copy
shallow_copied_list = original_list[:]
original_list[2].append(7)
print("Shallow Copy Example:", shallow_copied_list)  # Output: [1, 2, [3, 4, 5, 7]]
Copy Example: [1, 2, [3, 4, 5]]
Deepcopy Example: [100, 2, [3, 4, 5]]
Shallow Copy Example: [100, 2, [3, 4, 5, 6, 7]]

In the provided example, we start with an original list and create copies using copy, deepcopy, and shallow copy techniques. We then modify the original list and observe how these modifications impact the copied objects. This allows us to understand how each copying method handles data references and changes made to the original object.