3.14.5.1 Pickling and unpickling normal class instances

When a pickled class instance is unpickled, its __init__() method is normally not invoked. If it is desirable that the __init__() method be called on unpickling, a class can define a method __getinitargs__(), which should return a tuple containing the arguments to be passed to the class constructor (i.e. __init__()). The __getinitargs__() method is called at pickle time; the tuple it returns is incorporated in the pickle for the instance.   

  

Classes can further influence how their instances are pickled; if the class defines the method __getstate__(), it is called and the return state is pickled as the contents for the instance, instead of the contents of the instance's dictionary. If there is no __getstate__() method, the instance's __dict__ is pickled.

Upon unpickling, if the class also defines the method __setstate__(), it is called with the unpickled state3.7. If there is no __setstate__() method, the pickled object must be a dictionary and its items are assigned to the new instance's dictionary. If a class defines both __getstate__() and __setstate__(), the state object needn't be a dictionary and these methods can do what they want3.8.



Footnotes

... state3.7
These methods can also be used to implement copying class instances.
... want3.8
This protocol is also used by the shallow and deep copying operations defined in the copy module.
See About this document... for information on suggesting changes.