Special Method Protocols
__reduce__() — serialization protocol
Provide a reduction recipe for object serialization or copying. It may be a global name or a tuple containing reconstruction callable and arguments, plus optional state and population hooks. Prefer higher-level state hooks where practical. Never load untrusted pickle data. The compact example intentionally reconstructs a string surrogate rather than the original class.
Formal Syntax & Structure
python syntaxpython
def __reduce__(self):Return Value
str | tuple
Reduction recipe.
Working Example
Example Snippetpython
import pickleclass Label: def __reduce__(self): return (str, ("ready",))print(pickle.loads(pickle.dumps(Label())))