The copy.copy
function in Python’s copy
module creates a shallow copy of an object. This means it makes a new object but does not copy the objects inside it deeply. This is useful when you need to duplicate an object but want the inner objects to be shared between the original and the copy.
Table of Contents
- Introduction
copy.copy
Function Syntax- Examples
- Basic Usage
- Shallow Copy of a List
- Shallow Copy of a Dictionary
- Shallow Copy of a Custom Object
- Real-World Use Case
- Conclusion
Introduction
The copy.copy
function makes a shallow copy of an object. This means the new object created shares the inner objects with the original one, instead of making new copies of them.
copy.copy Function Syntax
Here is how you use the copy.copy
function:
import copy
new_object = copy.copy(original_object)
Parameters:
original_object
: The object to be copied.
Returns:
- A shallow copy of the
original_object
.
Examples
Basic Usage
Create a shallow copy of a simple object.
Example
import copy
original_list = [1, 2, 3]
shallow_copy_list = copy.copy(original_list)
print(f"Original list: {original_list}")
print(f"Shallow copy list: {shallow_copy_list}")
Output:
Original list: [1, 2, 3]
Shallow copy list: [1, 2, 3]
Shallow Copy of a List
Show how a shallow copy affects a list with inner lists.
Example
import copy
original_list = [[1, 2, 3], [4, 5, 6]]
shallow_copy_list = copy.copy(original_list)
# Modify the original list
original_list[0][0] = 10
print(f"Original list: {original_list}")
print(f"Shallow copy list: {shallow_copy_list}")
Output:
Original list: [[10, 2, 3], [4, 5, 6]]
Shallow copy list: [[10, 2, 3], [4, 5, 6]]
Shallow Copy of a Dictionary
Show how a shallow copy affects a dictionary with inner lists.
Example
import copy
original_dict = {'a': [1, 2, 3], 'b': [4, 5, 6]}
shallow_copy_dict = copy.copy(original_dict)
# Modify the original dictionary
original_dict['a'][0] = 10
print(f"Original dictionary: {original_dict}")
print(f"Shallow copy dictionary: {shallow_copy_dict}")
Output:
Original dictionary: {'a': [10, 2, 3], 'b': [4, 5, 6]}
Shallow copy dictionary: {'a': [10, 2, 3], 'b': [4, 5, 6]}
Shallow Copy of a Custom Object
Show how a shallow 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])
shallow_copy_object = copy.copy(original_object)
# Modify the original object's value
original_object.value[0] = 10
print(f"Original object value: {original_object.value}")
print(f"Shallow copy object value: {shallow_copy_object.value}")
Output:
Original object value: [10, 2, 3]
Shallow copy object value: [10, 2, 3]
Real-World Use Case
Cloning Configuration Objects
Use copy.copy
to clone configuration objects that contain nested lists.
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.copy(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': [10, 2, 3], 'option2': [4, 5, 6]}
Conclusion
The copy.copy
function is useful for making shallow copies of objects in Python. It allows you to duplicate objects without making deep copies of their inner elements. This can help manage memory and performance in your applications, and prevent unintended modifications to the original objects.