Website hosting service by Active-Venture.com
  

 Back to Index


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.6. If there is no __setstate__() method, the pickled state 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 want.3.7

 

Warning: For new-style classes, if __getstate__() returns a false value, the __setstate__() method will not be called.



Footnotes

... state3.6
These methods can also be used to implement copying class instances.
... want.3.7
This protocol is also used by the shallow and deep copying operations defined in the copy module.

 

    

 

© 2002-2004 Active-Venture.com Webhosting Service

 

Disclaimer: This documentation is provided only for the benefits of our hosting customers.
For authoritative source of the documentation, please refer to http://python.org/doc/

 

Active-Domain.com offers domain name registration, domain name transfer and domain search services  

Cheap domain registration : Register domain name or buy domain name, including free domain hosting services

 
 

Domain registration : Buy domain name or register domain name from $5.95/year only