Clone Python Objects with copy.deepcopy

A minimal example of cloning a Python object with copy.deepcopy(), showing that changes to the original do not affect the copy.

Python projects · Python fundamentals

Start here

Read the requirements and any configuration below, then follow the usage example. Source paths below are relative to the repository root; run commands from the working directory stated in Usage. This example’s source is in Object Clone/Console_Code/. Install any third-party packages in an isolated Python environment.

Overview

This script creates an instance of a custom class, clones it with copy.deepcopy(), then reassigns the original’s integer attribute. The copied instance retains its original value. The example demonstrates separate instances; it does not exercise nested mutable state or compare shallow copying directly.

Prerequisites and installation

No external dependencies. copy is part of the Python standard library.

Usage

From the Console_Code folder:

python CloneObject.py

Output:

20
10

How It Works

import copy

class MyClass:
    def __init__(self, x):
        self.x = x

original_object = MyClass(10)
cloned_object = copy.deepcopy(original_object)

original_object.x = 20

print(original_object.x)   # 20
print(cloned_object.x)     # 10 — unchanged

For this MyClass, deepcopy creates a separate instance; assigning 20 to the original instance’s integer attribute leaves the copied attribute at 10. This script does not itself compare nested mutable attributes or a shallow copy. Copy behavior for other classes can depend on their custom copy hooks.

Architecture

Object Clone/
└── Console_Code/
    └── CloneObject.py    # Main script

Troubleshooting and limitations

  • Use copy.copy() for a shallow copy, which only copies the top-level object (nested mutable objects are still shared).
  • Use copy.deepcopy() for a deep copy, which recursively copies supported mutable contents — suitable when the object contains lists, dicts, or other mutable attributes.
  • In this particular example, x is an integer (immutable), so copy.copy() would give the same result. The difference appears when an attribute is mutable — for example, if x were a list and you appended to original_object.x, a shallow copy would see the change and a deep copy would not.


Read all Python Projects and Code Examples guides online · Project overview · All developer projects