The provided web content discusses the concepts of shallow and deep copying in Python object-oriented programming (OOP), explaining how to create copies of objects, the differences between them, and how to use the identity operator to determine if objects are shallow or deep copies.
Abstract
The article "Shallow Copy and Deep Copy: Python OOP Complete Course — Part 9" is part of a comprehensive OOP course in Python. It delves into the nuances of object copying, a critical concept in programming. The author explains that a shallow copy, created using the assignment operator, results in two references pointing to the same memory location. Consequently, changes to one object affect the other. In contrast, a deep copy, created using the deepcopy() function from the copy module, produces a completely independent object. The article also introduces the identity operator is to verify the type of copy by checking if two objects share the same memory location. Practical code examples are provided to illustrate these concepts, and the article concludes with a summary of the key points learned, emphasizing the importance of understanding when and how to use shallow and deep copies in Python.
Opinions
The author emphasizes the importance of understanding the implications of object copying in Python to avoid unintended side effects in code.
The use of the deepcopy() function is recommended for creating stand-alone copies of objects, ensuring that changes to the original object do not affect the copied object.
The identity operator is is presented as a valuable tool for developers to distinguish between shallow and deep copies, ensuring clarity in object relationships within a program.
The article suggests that developers should be cautious when assuming object independence after copying, as Python's default behavior is to create references rather than independent objects.
The author encourages readers to subscribe and follow their work on Medium, as well as to consider Medium membership to support writers and access unlimited stories.
Shallow Copy and Deep Copy: Python OOP Complete Course — Part 9
Assume you have a class, and its name is DummyClass as the following:
obj1= DummyClass()
obj2= obj1
In this case, what happens in Python is that obj2 will be a reference to the same location in the memory that obj1 refers to. In other words, obj2 is not a stand-alone object. Refer to Figure 1.
Figure 1: Shallow copy example (Image By Author).
From this point, you can understand that using just an assignment operator (= sign) will create a reference to the same location in the memory. So, if you change the value of obj1, the value of obj2 will be changed also and vice versa. As a programmer, you should be careful because sometimes you could assume that you have created a stand-alone object copy when you assign one object to another, but this is not the case in Python. And this is what we called Shallow Copy.
Again, you got the same instance attribute value for both objects, That’s it Shallow Copy.
Then how to create a stand-alone copy from an object? let us see…
2. Deep Copy
Assume again that you have a class its name DummyClass and an object from that class obj1. To create a stand-alone copy from this object, which is called a deep copy, deepcopy() function is used. deepcopy() is a function from the built-in copy module.
The previous two examples were simple but what if you have a complex script with two different objects in the script obj1 and obj2 and you are not sure if obj2 is a shallow copy or deep copy to obj1.
So, to be 100% sure about what is the status of both objects, Identity Operator is used.
3. Identity Operator
The keyword that represents this operator in Python is (is). This operator is used to compare two variables. The comparison here means checking if they are referring to the same memory location, not checking if they are equal from a value perspective.
If the operator gives True, this means obj2 is a shallow copy of obj1.
If the operator gives False, this means obj2 is a deep copy of obj1.
Python Identity Operator details are:
Number of Operands: This operator takes two operands, which means it will compare one object to another.
Operands Data Type: It is an object, which means the objects of any class can be compared using the identity operator.
The Final Result Data Type: It is a number (Boolean) because the operator will give True(shallow copy) or False (deep coy).
Now, let us see how the identity operator works with a shallow copy object.
When you assign one object to another, you create a reference to the same object in the memory, a Shallow copy.
You can create a stand-alone copy, a deep copy, for an object by using the function deepcopy() from the built-in module copy.
When you assign one object to another, you create a new object equal to the other object, but it’s a stand-alone copy inside the memory in which changing one will not affect the other, a Deep copy.
Identity operator (is): is used to check if one object is a shallow copy or deep copy of another object.
P.S.: A million thanks for your time reading my story. Before you leave let me mention quickly two points:
First, to get my posts in your inbox directly, would you please subscribe here, and you can follow me here.
Second, writers made thousands of $$ on Medium. To get unlimited access to Medium stories and start earning, sign up now for Medium membershipwhichonly costs $5 per month. By signing up with this link, you can directly support me at no extra cost to you.