__deepcopy__Easy Examples

Called by copy.deepcopy() for deep copying

Implementing __deepcopy__

Basic implementation of __deepcopy__ in a class.

python
class Example:
    def __deepcopy__(self):
        return "Example __deepcopy__"

obj = Example()
print(obj)

__deepcopy__ called by copy.deepcopy() for deep copying. Implementing it lets you customize how Python interacts with your objects.

__deepcopy__ in action

Seeing __deepcopy__ called by Python's built-in operations.

python
# How Python calls __deepcopy__ automatically
class Demo:
    def __init__(self, value):
        self.value = value

    def __deepcopy__(self):
        print(f"__deepcopy__ was called!")
        return self

d = Demo(42)
# This triggers __deepcopy__:
print(d)

Python automatically calls __deepcopy__ when you use the corresponding operator or function on your object.

Want to try these examples interactively?

Open Easy Playground