Thanks to visit codestin.com
Credit goes to www.rameshfadatare.com

Python copy.deepcopy Function

The copy.deepcopy function in Python’s copy module creates a deep copy of an object. This means it creates a new object and recursively copies all objects found in the original, instead of just copying references. This is useful when you need a completely independent copy of an object.

Table of Contents

  1. Introduction
  2. copy.deepcopy Function Syntax
  3. Examples
    • Basic Usage
    • Deep Copy of a List
    • Deep Copy of a Dictionary
    • Deep Copy of a Custom Object
  4. Real-World Use Case
  5. Conclusion

Introduction

The copy.deepcopy function creates a deep copy of an object. This means the new object created has its own copies of all objects found in the original, not just references to them. This is different from a shallow copy, which only copies the top-level object and references to the objects inside it.

copy.deepcopy Function Syntax

Here is how you use the copy.deepcopy function:

import copy

new_object = copy.deepcopy(original_object)

Parameters:

  • original_object: The object to be copied.

Returns:

  • A deep copy of the original_object.

Examples

Basic Usage

Create a deep copy of a simple object.

Example

import copy

original_list = [1, 2, 3]
deep_copy_list = copy.deepcopy(original_list)

print(f"Original list: {original_list}")
print(f"Deep copy list: {deep_copy_list}")

Output:

Original list: [1, 2, 3]
Deep copy list: [1, 2, 3]

Deep Copy of a List

Show how a deep copy affects a list with inner lists.

Example

import copy

original_list = [[1, 2, 3], [4, 5, 6]]
deep_copy_list = copy.deepcopy(original_list)

# Modify the original list
original_list[0][0] = 10

print(f"Original list: {original_list}")
print(f"Deep copy list: {deep_copy_list}")

Output:

Original list: [[10, 2, 3], [4, 5, 6]]
Deep copy list: [[1, 2, 3], [4, 5, 6]]

Deep Copy of a Dictionary

Show how a deep copy affects a dictionary with inner lists.

Example

import copy

original_dict = {'a': [1, 2, 3], 'b': [4, 5, 6]}
deep_copy_dict = copy.deepcopy(original_dict)

# Modify the original dictionary
original_dict['a'][0] = 10

print(f"Original dictionary: {original_dict}")
print(f"Deep copy dictionary: {deep_copy_dict}")

Output:

Original dictionary: {'a': [10, 2, 3], 'b': [4, 5, 6]}
Deep copy dictionary: {'a': [1, 2, 3], 'b': [4, 5, 6]}

Deep Copy of a Custom Object

Show how a deep copy affects a custom object with inner lists.

Example

import copy

class CustomObject:
    def __init__(self, value):
        self.value = value

original_object = CustomObject([1, 2, 3])
deep_copy_object = copy.deepcopy(original_object)

# Modify the original object's value
original_object.value[0] = 10

print(f"Original object value: {original_object.value}")
print(f"Deep copy object value: {deep_copy_object.value}")

Output:

Original object value: [10, 2, 3]
Deep copy object value: [1, 2, 3]

Real-World Use Case

Cloning Complex Configuration Objects

Use copy.deepcopy to clone complex configuration objects that contain nested lists and dictionaries.

Example

import copy

class Config:
    def __init__(self, settings):
        self.settings = settings

original_config = Config({'option1': [1, 2, 3], 'option2': [4, 5, 6]})
cloned_config = copy.deepcopy(original_config)

# Modify the original configuration
original_config.settings['option1'][0] = 10

print(f"Original config: {original_config.settings}")
print(f"Cloned config: {cloned_config.settings}")

Output:

Original config: {'option1': [10, 2, 3], 'option2': [4, 5, 6]}
Cloned config: {'option1': [1, 2, 3], 'option2': [4, 5, 6]}

Conclusion

The copy.deepcopy function is used for creating deep copies of objects in Python. It allows you to duplicate objects and all objects found within them, ensuring complete independence from the original objects. This can help manage memory and prevent unintended modifications to the original objects, improving the reliability of your applications.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top