Thursday, 23 September 2021

Python: deep copy vs shallow copy

Using ‘copy.deepcopy’ method, we can deep copy the list content, whereas ‘copy.copy’ method performs shallow copy.

 

Let’s see with an example and try to understand deepcopy.

 

Shallow Copy

A shallow copy just copies the values of the references in the class. Let’s see it by an example.

 

Let’s take a Student class, which contains 'marks' object and two fields(name, id).


If we shallow copy Student s1 as Student s2. Then fields other than reference variables are copied as separate variables, but the reference variable 'marks' is same for both student s1 and s2. I.e, a shallow copy just copies the values of the references in the class. So changes to the marks object by student s1 is reflected to s2 and vice versa.

 


shallow_copy.py

import copy

class Marks:
    def __init__(self, maths, physics, chemistry):
        self.maths = maths
        self.physics = physics
        self.chemistry = chemistry

    def __str__(self):
        return 'maths : ' + str(self.maths) + ', physics : ' + str(self.physics) + ', chemistry : ' + str(self.chemistry)

class Student:
    def __init__(self, id, name, marks):
        self.id = id
        self.name = name
        self.marks = marks

    def __str__(self):
        return 'id : ' + str(self.id) + ', name : ' + self.name + ', marks : [' + str(self.marks) + ']'

marks1 = Marks(75, 91, 87)
student1 = Student(1, 'Krishna', marks1)

student2 = copy.copy(student1)

print('student1 details')
print(student1)

print('\nstudent2 details')
print(student2)

print('\nChange marks, id and name of student2')
student2.marks.maths = 97
student2.marks.physics = 98
student2.marks.chemistry = 99

student2.id = 2
student2.name = 'Lahari'

print('\nstudent1 details')
print(student1)

print('\nstudent2 details')
print(student2)

 

Output

student1 details
id : 1, name : Krishna, marks : [maths : 75, physics : 91, chemistry : 87]

student2 details
id : 1, name : Krishna, marks : [maths : 75, physics : 91, chemistry : 87]

Change marks, id and name of student2

student1 details
id : 1, name : Krishna, marks : [maths : 97, physics : 98, chemistry : 99]

student2 details
id : 2, name : Lahari, marks : [maths : 97, physics : 98, chemistry : 99]

 

Observe the output, the marks changed by student2 are reflected to s1, but not the name and id(Since these are not references).

 

Deep copy

A shallow copy is a copy of the reference pointer to the object, whereas a deep copy is a copy of the object itself.

 

Let’s see it with below example.

 

deep_copy.py

import copy

class Marks:
    def __init__(self, maths, physics, chemistry):
        self.maths = maths
        self.physics = physics
        self.chemistry = chemistry

    def __str__(self):
        return 'maths : ' + str(self.maths) + ', physics : ' + str(self.physics) + ', chemistry : ' + str(self.chemistry)

class Student:
    def __init__(self, id, name, marks):
        self.id = id
        self.name = name
        self.marks = marks

    def __str__(self):
        return 'id : ' + str(self.id) + ', name : ' + self.name + ', marks : [' + str(self.marks) + ']'

marks1 = Marks(75, 91, 87)
student1 = Student(1, 'Krishna', marks1)

student2 = copy.deepcopy(student1)

print('student1 details')
print(student1)

print('\nstudent2 details')
print(student2)

print('\nChange marks, id and name of student2')
student2.marks.maths = 97
student2.marks.physics = 98
student2.marks.chemistry = 99

student2.id = 2
student2.name = 'Lahari'

print('\nstudent1 details')
print(student1)

print('\nstudent2 details')
print(student2)

 

Output

student1 details
id : 1, name : Krishna, marks : [maths : 75, physics : 91, chemistry : 87]

student2 details
id : 1, name : Krishna, marks : [maths : 75, physics : 91, chemistry : 87]

Change marks, id and name of student2

student1 details
id : 1, name : Krishna, marks : [maths : 75, physics : 91, chemistry : 87]

student2 details
id : 2, name : Lahari, marks : [maths : 97, physics : 98, chemistry : 99]

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment