Python copyreg Module
Example
Register pickling support for a custom class:
import copyreg
import pickle
class Person:
def __init__(self, name):
self.name = name
def reduce_person(p):
return (Person, (p.name,))
copyreg.pickle(Person, reduce_person)
p = Person("Emil")
data = pickle.dumps(p)
q = pickle.loads(data)
print(type(q).__name__)
print(q.name)
Try it Yourself »
Definition and Usage
The copyreg module provides a mechanism for registering pickling functions for classes and types.
Use it to control how custom objects are serialized and deserialized by the pickle module.
Members
Member | Description |
---|---|
add_extension() | Register an extension code for a module and name. |
clear_extension_cache() | Clear the extension registration cache. |
constructor() | Register a callable as a valid constructor for pickling. |
pickle() | Register a reduction function for a type. |
remove_extension() | Unregister an extension code for a module and name. |