Loading video player…

Deep vs Shallow Copies in Python (Overview)

When working with Python objects, you’ll often need to make copies rather than modify the originals. In this video course, you’ll explore various ways to copy objects in Python, including using the built-in copy module. You’ll also learn the key differences between shallow and deep copies, with practical examples so you can safely duplicate objects in your own code.

By the end of this video course, you’ll understand that:

  • Shallow copying creates a new object but references the same nested objects, leading to shared changes.
  • Deep copying recursively duplicates all objects, ensuring full independence from the original.
  • Python’s copy module provides the copy() function for shallow copies and deepcopy() for deep copies.
  • Custom classes can implement .__copy__() and .__deepcopy__() for specific copying behavior.
  • Assignment in Python binds variable names to objects without copying, unlike some lower-level languages.

Resource linked in this lesson: Object-Oriented Programming (OOP) in Python

Download

Course Slides (.pdf)

6.2 MB
Download

Sample Code (.zip)

4.2 KB

00:00 Hello, and welcome to Deep vs Shallow Copies in Python. I’m Joseph, or maybe an AI copy of someone named Joseph, and I’ll be your instructor for this video course.

00:11 Copying objects in Python. It sounds straightforward, but there’s more to it than you might think. As humans, we have certain intuitions around making copies of things in the physical world.

00:22 You know what to expect when you use a Xerox machine to copy a document or take a photograph to copy an image, or use a 3D printer to make realistic non-copyright-infringing miniatures for your favorite tabletop role-playing game.

00:36 The thing that these copies have in common is that they have no way to affect the originals. In programming, however, copying an object does not necessarily have that guarantee.

00:47 Because of the complexity of some objects in Python, you can actually end up creating copies of objects that are still linked to the originals in subtle ways, and this can lead to producing equally subtle bugs.

01:00 What it often comes down to is whether you’re dealing with deep or shallow copies. Throughout this course, you’ll learn various techniques for copying objects in Python, the differences between shallow and deep copying, how to distinguish between references and values, the benefits of Python’s built-in copy module, and ways to fine-tune the copy behavior of your own custom classes.

01:24 This course is focused on copying in depth, so we expect you to have some familiarity with intermediate Python concepts, like Python data structures such as lists and dictionaries, functions, classes and instances, as well as methods and attributes, essentially object-oriented programming in Python.

01:44 If you feel like you need a refresher, I recommend the tutorial on object oriented programming in Python. Otherwise, meet me in the next lesson where we’ll discuss some of the theory behind copying objects in Python.

Become a Member to join the conversation.