python 在python中从pickling中排除对象的字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2345944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exclude object's field from pickling in python
提问by Art
I would like to avoid pickling of certain fields in an instance of a class. Currently, before pickling I just set those fields to None, but I wonder whether there's more elegant solution?
我想避免在类的实例中对某些字段进行酸洗。目前,在酸洗之前,我只是将这些字段设置为无,但我想知道是否有更优雅的解决方案?
采纳答案by unutbu
There is an example in the docswhich solves your problem with __getstate__
and __setstate__
.
文档中有一个示例可以解决您的问题__getstate__
和__setstate__
。
回答by anijhaw
One way to handle instance attributes that are not picklable objects is to use the special methods available for modifying a class instance's state: __getstate__()
and __setstate__()
. Here is an example
处理不可picklable 对象的实例属性的一种方法是使用可用于修改类实例状态的特殊方法:__getstate__()
和__setstate__()
。这是一个例子
class Foo(object):
def __init__(self, value, filename):
self.value = value
self.logfile = file(filename, 'w')
def __getstate__(self):
"""Return state values to be pickled."""
f = self.logfile
return (self.value, f.name, f.tell())
def __setstate__(self, state):
"""Restore state from the unpickled state values."""
self.value, name, position = state
f = file(name, 'w')
f.seek(position)
self.logfile = f
When an instance of Foo
is pickled, Python will pickle only the values returned to it when it calls the instance's __getstate__()
method. Likewise, during unpickling, Python will supply the unpickled values as an argument to the instance's __setstate__()
method. Inside the __setstate__()
method we are able to recreate the file object based on the name and position information we pickled, and assign the file object to the instance's logfile attribute.
当一个 的实例Foo
被腌制时,Python 只会在调用实例的__getstate__()
方法时腌制返回给它的值。同样,在 unpickling 期间,Python 将提供 unpickled 值作为实例__setstate__()
方法的参数。在该__setstate__()
方法中,我们能够根据我们腌制的名称和位置信息重新创建文件对象,并将文件对象分配给实例的日志文件属性。
Reference: http://www.ibm.com/developerworks/library/l-pypers.html
回答by congusbongus
Pickling uses the object's __getstate__
and __setstate__
methods; you can override them and ignore the fields you want.
Pickling 使用对象的__getstate__
和__setstate__
方法;您可以覆盖它们并忽略您想要的字段。
# foo.py
class Foo:
def __init__(self):
self.bar = 1
self.baz = 2
def __getstate__(self):
state = self.__dict__.copy()
# Don't pickle baz
del state["baz"]
return state
def __setstate__(self, state):
self.__dict__.update(state)
# Add baz back since it doesn't exist in the pickle
self.baz = 0
# main.py
import pickle
from foo import Foo
foo = Foo()
print(f"Foo bar: {foo.bar} baz: {foo.baz}")
new_foo = pickle.loads(pickle.dumps(foo))
print(f"New bar: {new_foo.bar} baz: {new_foo.baz}")
Output:
输出:
Foo bar: 1 baz: 2
New bar: 1 baz: 0
You can find another example here: https://docs.python.org/3/library/pickle.html#handling-stateful-objects
你可以在这里找到另一个例子:https: //docs.python.org/3/library/pickle.html#handling-stateful-objects