Image for post
Image for post
Photo by Joe Green on Unsplash

Cloning Objects in Python — Beginner

shallow copies and deep copies in Python explained!!

Rachit Tayal
Jul 6, 2019 · 3 min read

If we think, that assignment operation = creates a new object; well in Python, it does not. It only creates a new variable that shares the reference of the original object. While programming, there are times when we would want copies of objects, that we can modify without modifying the original at the same time. In order to do that, we create copies of objects. In this article, we will cover on how to copy or clone objects in Python and some of the caveats involved with it.

In Python, there are two ways to create copies:

Shallow Copy: A shallow copy means constructing a new object and then populating it with references to the child objects found in the original object.

Deep Copy: A deep copy constructs a new object and then, recursively, inserts copies into it of the objects found in the original object.

In essence, a shallow copy is one level deep. The copying process does not recurse and therefore won’t create copies of the child object themselves. Whereas deep copying walks the whole object tree to create a fully independent clone of the original object and all of its children.

Enough with the theory, let’s jump to some code and understand the distinction via examples.

Let’s create a nested list and then shallowly copy it with the list() factory function.

Now, cpy is an independent object with same contents as org. To confirm that, let’s add a new sublist to cpy and check to make sure this modification is not reflected in org.

As expected, modifying the copied list at a “superficial” level was no problem at all. (superficial 😅, we will know why).
However since we created only a shallow copy of org, copied list cpy still contains references to the original child objects. The children are not copied. They are merely referenced again in the copied list.

If we modify one of the child objects in cpy, change will be reflected in org as well.

Again, this happened because we had only created a shallow copy of the original list so both list objects share the same child objects.

Had we created a deep copy, both the objects would’ve been fully independent. This is the distinction between shallow copy and a deep copy. So let’s dig a little deep into deep copy. 😄

Let’s repeat the previous list-copying example, this time we’re going to create a deep copy using the deepcopy() function defined in the copy module.

On inspection, both the list objects will look identical again — just like in the previous example.

However, modification to one of the child objects in cpy, won’t be reflected in the original list org.

Since both objects are fully independent this time, hence no changes in the org list object.
On a side note, we can also create shallow copies using a function in the copy module. The copy.copy() creates shallow copies.
To understand any concept, best way is to play with the examples in Python interpreter, so it’s best to take some time and start copying objects and play with the examples firsthand. 😄

  • Making a shallow copy of an object won’t clone child objects. Hence it won’t be fully independent of the original.
  • A deep copy of an object will recursively clone child objects. The clone is fully independent of the original, but creating a deep copy is slower.

Python Features

It covers important Python aspects with code snippets

Rachit Tayal

Written by

Sports Enthusiast | Senior Deep Learning Engineer. Python Blogger @ medium. Background in Machine Learning & Python. Linux and Vim Fan

Python Features

It covers important Python aspects with code snippets

Rachit Tayal

Written by

Sports Enthusiast | Senior Deep Learning Engineer. Python Blogger @ medium. Background in Machine Learning & Python. Linux and Vim Fan

Python Features

It covers important Python aspects with code snippets

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store