On this page
copyreg.pickle
Prev: copyreg.constructor | Up: copyreg | Next: copyreg.remove_extension
Rel: copyreg.add_extension, copyreg.clear_extension_cache, copyreg.constructor, copyreg.remove_extension
Ref: docs.python.org — copyreg.pickle
Tags: #public #python3 #stdlib #copyreg
Import
import copyreg
from copyreg import pickle
Signature & Overview
copyreg.pickle(ob_type, pickle_function, constructor_ob=None)
copyreg.pickle() function.
Usage
import copyreg
import pickle
class Pair:
def __init__(self, a: int, b: int) -> None:
self.a, self.b = a, b
copyreg.pickle(Pair, lambda p: (Pair, (p.a, p.b)))
restored = pickle.loads(pickle.dumps(Pair(1, 2)))
assert (restored.a, restored.b) == (1, 2)
Gotchas
- Mutates the global
copyreg.dispatch_table: Prefer defining__reduce__directly on classes you own.
REPL Inspect
from copyreg import pickle
help(pickle)
Linked from
Related notes
- tuts/python3/stdlib_module/copyreg/add_extension Linked here; links to this page; shared tags: #copyreg, #stdlib
- tuts/python3/stdlib_module/copyreg/clear_extension_cache Linked here; links to this page; shared tags: #copyreg, #stdlib
- tuts/python3/stdlib_module/copyreg/constructor Linked here; links to this page; shared tags: #copyreg, #stdlib
- tuts/python3/stdlib_module/copyreg/remove_extension Linked here; links to this page; shared tags: #copyreg, #stdlib
- tuts/python3/stdlib_module/copyreg Linked here; links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib
- tuts/c/stdlib/assert/static_assert shared tags: #stdlib
- tuts/c/stdlib/ctype shared tags: #stdlib