Designing a class for reliable pickle processing

The __init__() method of a class is not actually used to unpickle an object. The __init__() method is bypassed by using __new__() and setting the pickled values into the object's __dict__ directly. This distinction matters when our class definition includes some processing in __init__(). For example, if __init__() opens external files, creates some part of a GUI, or performs some external update to a database, then this will not be performed during unpickling.

If we compute a new instance variable during the __init__() processing, there is no real problem. For example, consider a Blackjack Hand object that computes the total of the Card instances when the Hand is created. The ordinary pickle ...

Get Mastering Object-Oriented Python - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.