您可以为类定义两个方法__getstate__
和__setstate__
,以覆盖默认的酸洗行为。
http://docs.python.org/library/pickle.html#object.__getstate__
__getstate__
应该返回要腌制的属性的字典。
def __getstate__(self):
d = dict(self.__dict__)
del d['logger']
return d
__setstate__
应该使用提供的字典设置对象。
def __setstate__(self, d):
self.__dict__.update(d) # I *think* this is a safe way to do it