The webpage provides an explanation of the differences between shallow copy, deep copy, and assignment in Python, detailing how each method handles object references and mutability.
Abstract
The article on the undefined website offers a comprehensive comparison of shallow copy, deep copy, and assignment operations in Python. It clarifies that assignment operations create references to the original object rather than copies, which affects how changes to mutable objects like lists, sets, and dictionaries are reflected. A shallow copy, created using the copy() method, duplicates the outer object but retains references to nested objects, leading to shared nested structures between the original and the copy. In contrast, a deep copy, made with the deepcopy() method, recursively copies all objects, ensuring that both the outer and nested objects are entirely independent. The article emphasizes the importance of understanding these concepts to manage object references effectively in Python programming, especially when dealing with complex data structures.
Opinions
The author suggests that using assignment operations in Python does not create true copies of objects but rather new references to the same object, which can lead to unexpected behavior when modifying mutable objects.
The article conveys that a shallow copy is sufficient when working with objects that do not contain nested objects or when changes to nested objects are not required.
It is implied that a deep copy should be used when a completely independent copy of an object is necessary, particularly for complex objects with nested structures to avoid unintended side effects.
The author emphasizes the significance of the copy module in Python for creating both shallow and deep copies, highlighting the copy() and deepcopy() functions.
The article is written with the assumption that readers are Python programmers or data scientists who need to understand the nuances of object copying to manage memory and data integrity effectively.
Shallow Copy vs Deep Copy vs Assignment in Python
A quick overview about shallow copy,deepcopy and assignment in python.
Copying objects can be done in three ways in Python:
Assignment Operationa=b
Shallow copy
copy()
a=copy(b)
deepcopy
a=deepcopy(b)
Assignment Operation:
Assignment Operation in python usually won’t create copies. It will create references to the existing object.
Immutable object
Example:int,string are immutable objects in python.
Assignment Operation(Image Source: Author)
If the above example,if we assign x=y, both x and y will point to same object in memory.
Image Source: Author
If we try to modify the value of y=10, only value of y is changed. x remains the same.y will point to another int object 10
Image Source: Author
Same for str object also.
2. Mutable objects
Example: List,Set,Dictionary
Image Source: Author
Both x and y will point to same object in memory.
Image Source: Author
If we modify x, bothx and y values are modified.
x.append(5)
Now also both x and y point to same List object in memory.
Image Source: Author
Example:
Same for dict object also.
Shallow Copy:
A shallow copy constructs a new compound object and then (to the extent possible) inserts references into it to the objects found in the original. — python docs
Shallow copy can be done by copy() method.
copy()
To copy objects using copy() method, we have to import copy module.
from copy import copy
Copying list object by using copy() function.
copying list using copy() (Image Source: Author)
After copying list using copy() function, both list x and y contains same elements.But both are different list objects. Their id(memory location) is different.
But the nested list inside list x and y will point to same list object in memory.Memory location( id ) of nested list objects is same.
Pictorial representation of list objects x and y in memory:
Image Source: Author
copy()- It will create shallow copy of the list.Shallow copy means , it creates only outer list copy, not the nested list copy.Both nested list objects is same for both list x and y. If you try to modify the nested list then it will modify the original list too as the nested list object is same for both lists.
Example.
After modifying elements in the outer list, it is not reflected in the copied list.
Pictorial representation of list objects x and y after modifying elements in the outer list.
Image Source: Author
If we modify elements in the nested list ,it will get reflected in the copied list also. Because both nested list objects are pointing to same list object in memory. Shallow copy is only one level deep copy.
Pictorial representation of list objects x and y after modifying elements in the nested list.
Image Source: Author
Deepcopy:
A deepcopy constructs a new compound object and then, recursively, inserts copies into it of the objects found in the original. — python docs
To copy objects using deepcopy() method, we have to import copy module.
from copy import deepcopy
Copying list object by using deepcopy() function.
deepcopy()(Image Source: Author)
After copying list using deepcopy() function, both list x and y contains same elements.But both are different list objects. Their id(memory location) is different.
The nested list inside list x and y will also point to different list objects in memory.Memory location( id ) of nested list objects is also different.
Pictorial representation of list objects x and y in memory:
Image Source: Author
deepcopy()- It will create deepcopy of the list.Deep copy means , it creates not only outer list copy, but also nested list copy.Nested list objects will be different for both list x and y. If you try to modify the nested list then it will not be reflected in the copied list ,because nested list objects is different for both lists.
After modifying elements in the outer list, it is not reflected in the deepcopied list.
Pictorial representation of list objects x and y after modifying elements in the outer list.
Image Source: Author
If we modify elements in the nested list ,it will not get reflected in the copied list. Because both nested list objects are pointing to different list object in memory.
Pictorial representation of list objects x and y after modifying elements in the nested list.
Image Source: Author
Conclusion:
deepcopy()
Copies everything.It copies object recursively.It means any changes made in the copy of object is not reflected in the original object
copy()
copy() function returns shallow copy of the object.If we modify elements in the outer list of copied list, it won’t be reflected in the original list.But if we modify nested list , it will be reflected in the original list.
Assignment Operation.
It won’t create copies.It creates only references to existing object.So If we modify immutable elements like int,str in the copied object,it won’t be reflected in the original object.But if we modify mutable objects like list, it will be reflected in the original object.