← All notes

Python copyreg: register pickle reduction functions for custom types

Answer: copyreg registers reduction functions and constructors so pickle and copy can handle objects that are not ordinary classes.

Constructors and reduction functions

The module lets you define functions used when pickling particular objects.

The pickle and copy modules call those functions when they pickle or copy the objects.

It also records constructors that are not classes, such as factory functions or class instances.

constructor(object) declares that object is a valid constructor. If the object is not callable, TypeError is raised.

pickle(type, function, constructor_ob=None) names function as the reduction function for objects of that type.

The reduction function has to return a string, or a tuple that holds two to six elements. dispatch_table describes that function interface in more detail.

constructor_ob is a legacy feature and is ignored now; if you still pass it, it must be callable.

You can also attach reduction functions on a pickler dispatch_table, or on a pickle.Pickler subclass.

Example

The docs register pickle_c for a class C. pickle_c prints pickling a C instance and returns C, (c.a,).

After copyreg.pickle(C, pickle_c), both copy.copy(c) and pickle.dumps(c) run the registered function.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, copyreg — Register pickle support functions; CPython, copyreg documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, copyreg — Register pickle support functions; CPython, copyreg documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation.

Cite this

Permanent ID: iil:note:tgp0qwvb

iinventedlight. 2026. "Python copyreg: register pickle reduction functions for custom types." iinventedlight Library, iil:note:tgp0qwvb, version of 2026-11-27. https://iinventedlight.com/library/notes/python-copyreg/. License: CC-BY-4.0.

@misc{iil_note_tgp0qwvb,
  author = {{iinventedlight}},
  title = {{Python copyreg: register pickle reduction functions for custom types}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-27},
  url = {https://iinventedlight.com/library/notes/python-copyreg/},
  note = {Permanent ID iil:note:tgp0qwvb. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-copyreg.md.