您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何阻止在Python中pickle属性

如何阻止在Python中pickle属性

您可以为类定义两个方法__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

请注意,__init__解开时不会调用它,因此您必须在以下位置创建记录器__setstate__

python 2022/1/1 18:33:00 有206人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶