Shallow vs Deep Copying of Python Objects

By realpython - 2020-12-06

Description

What's the difference between a shallow and a deep copy of a Python object? Learn how to clone arbitrary objects in Python, including your own custom classes.

Summary

  • But for working with mutable objects or collections of mutable objects, you might be looking for a way to create “real copies” or “clones” of these objects.
  • This time we’re going to create a deep copy using the deepcopy() function defined in the copy module instead: Using copy.copy() lets you indicate this fact.
  • However, for built-in collections it’s considered more Pythonic to simply use the list, dict, and set factory functions to create shallow copies.

 

Topics

  1. Coding (0.26)
  2. Machine_Learning (0.25)
  3. Backend (0.08)

Similar Articles

Pointers in Python: What's the Point

By realpython - 2021-03-09

In this step-by-step tutorial, you'll get a clearer understanding of Python's object model and learn why pointers don't really exist in Python. You'll also cover ways to simulate pointers in Python wi ...

PyPy: Faster Python With Minimal Effort

By realpython - 2020-12-28

In this tutorial, you'll learn how you can use PyPy to improve the speed of your applications. You'll see how PyPy compares with other Python implementations like CPython and learn about features that ...

Python enumerate(): Simplify Looping With Counters

By realpython - 2020-12-15

Once you learn about for loops in Python, you know that using an index to access items in a sequence isn't very Pythonic. So what do you do when you need that index value? In this tutorial, you'll lea ...

An introduction to object-oriented UX and how to do it

By Medium - 2020-10-21

Object-Oriented UX (OOUX) fell into my lap a couple years ago during a brief Lunch and Learn at work. At the time, I was still getting my feet wet in UX, but I knew this OOUX stuff was on to…