__reduce__Easy Examples

Helper for pickling; returns reconstruction info

Implementing __reduce__

Basic implementation of __reduce__ in a class.

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

obj = Example()
print(obj)

__reduce__ helper for pickling; returns reconstruction info. Implementing it lets you customize how Python interacts with your objects.

__reduce__ in action

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

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

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

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

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

Want to try these examples interactively?

Open Easy Playground