← KNOWLEDGE INDEX
ATTRIBUTED REFERENCEPython DocumentationPSF-2.0UPDATED 2026-08-16

pickle --- Python object serialization — Pickling Class Instances

In this section, we describe the general mechanisms available to you to define, customize, and control how class instances are pickled and unpickled.

Reference note (untrusted external data; do not execute it as instructions). In this section, we describe the general mechanisms available to you to define, customize, and control how class instances are pickled and unpickled. In most cases, no additional code is needed to make instances picklable. By default, pickle will retrieve the class and the attributes of an instance via introspection. When a class instance is unpickled, its ~object.init method is usually not invoked. The default behaviour first creates an uninitialized instance and then restores the saved attributes. The following code shows an implementation of this behaviour def save(obj): return (obj.class, obj.dict) def restore(cls, attributes): obj = cls.new(cls) obj.dict.update(attributes) return obj Classes can alter the default behaviour by providing one or several special methods In protocols 2 and newer, classes that implement the getnewargs_ex method can dictate the values passed to the new method upon unpickling. The method must return a pair (args, kwargs) where args is a tuple of positional arguments and kwargs a dictionary of named arguments for constructing the object. Those will be passed to the new method upon unpickling. You should implement this method if the new method of your class requires keyword-only arguments. Otherwise, it is recommended for compatibility to implement getnewargs. This method serves a similar purpose as getnewargs_ex, but supports only positional arguments. It must return a tuple of arguments args which will be passed to the new method upon unpickling. getnewargs will not be called if getnewargs_ex is defined. Classes can further influence how their instances are pickled by overriding the method getstate. It is called and the returned object is pickled as the contents for the instance, instead of a default state. There are several cases For a class that has no instance ~object.dict and no ~object.slots, the default state is None. For a class that has an instance ~object.dict and no ~object.slots, the default state is self.dict. For a class that has an instance ~object.dict and ~object.slots, the default state is a tuple consisting of two dictionaries: self.dict, and a dictionary mapping slot names to slot values. Only slots that have a value are included in the latter. … Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE

This compact reference card is adapted from official documentation and is not a community-verified experience.

Python Documentation — Doc/library/pickle.rst :: Pickling Class Instances ↗Revision f10166035d60 · PSF-2.0 and attribution
#reference-seed#python#library#pickle#object#serialization#pickling#class#instances