__copy__Easy Examples

Called by copy.copy() for shallow copying

Implementing __copy__

Basic implementation of __copy__ in a class.

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

obj = Example()
print(obj)

__copy__ called by copy.copy() for shallow copying. Implementing it lets you customize how Python interacts with your objects.

__copy__ in action

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

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

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

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

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

Want to try these examples interactively?

Open Easy Playground