使用 __setstate__ 和 __getstate__ 的简单示例
我不知道 __setstate__ 和 __getstate__ 方法是做什么的,所以请帮我举一个简单的例子。
I don't know what the __setstate__
and __getstate__
methods do, so help me with a simple example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个非常简单的 Python 示例,它应该补充 pickle 文档。
输出:
Here's a very simple example for Python that should supplement the pickle docs.
Output:
最小示例
getstate
产生的任何内容都会进入setstate
。它不需要是一个字典。getstate
产生的任何内容都必须是可选取的,例如由int
、str
、list
等基本内置函数组成>。默认
__setstate__
默认
__setstate__
采用dict
。self.__dict__
是一个不错的选择,如 https://stackoverflow.com/a/1939384/895245< /a> ,但我们可以自己构造一个,以便更好地了解发生了什么:默认
__getstate__
类似于
__setstate__
。__slots__
对象不具有__dict__
如果对象具有
__slots__
,则它不具有__dict__
如果您要同时实现
get
和setstate
,默认的方式是:__slots__
default get and set 需要一个元组如果您想重用默认的
__getstate__
或__setstate__
,则必须将元组传递为:我不确定这是什么为了。
继承
首先看看pickling默认是有效的:
继承自定义
__getstate__
如果没有
__slots__
,它很容易,因为
包含D
的 >__dict__C
的__dict__
,因此我们不需要触及C
> 根本:继承和
__slots__
使用
__slots__
,我们需要转发到基类,并且可以传递元组:不幸的是,它不是可以重用基础的默认
__getstate__
和__setstate__
:https://groups.google.com/forum/#!topic/python-ideas/QkvOwa1-pHQ 我们被迫定义它们。在 Python 2.7.12 上测试。 GitHub 上游。
Minimal example
Whatever comes out of
getstate
, goes intosetstate
. It does not need to be a dict.Whatever comes out of
getstate
must be pickeable, e.g. made up of basic built-ins likeint
,str
,list
.Default
__setstate__
The default
__setstate__
takes adict
.self.__dict__
is a good choice as in https://stackoverflow.com/a/1939384/895245 , but we can construct one ourselves to better see what is going on:Default
__getstate__
Analogous to
__setstate__
.__slots__
objects don't have__dict__
If the object has
__slots__
, then it does not have__dict__
If you are going to implement both
get
andsetstate
, the default-ish way is:__slots__
default get and set expects a tupleIf you want to reuse the default
__getstate__
or__setstate__
, you will have to pass tuples around as:I'm not sure what this is for.
Inheritance
First see that pickling works by default:
Inheritance custom
__getstate__
Without
__slots__
it is easy, since the__dict__
forD
contains the__dict__
forC
, so we don't need to touchC
at all:Inheritance and
__slots__
With
__slots__
, we need to forward to the base class, and can pass tuples around:Unfortunately it is not possible to reuse the default
__getstate__
and__setstate__
of the base: https://groups.google.com/forum/#!topic/python-ideas/QkvOwa1-pHQ we are forced to define them.Tested on Python 2.7.12. GitHub upstream.
这些方法用于控制 pickle 如何对对象进行 pickle 和 unpickle模块。这通常是自动处理的,因此除非您需要覆盖类的 pickle 或 unpickled 方式,否则您不需要担心它。
These methods are used for controlling how objects are pickled and unpickled by the pickle module. This is usually handled automatically, so unless you need to override how a class is pickled or unpickled you shouldn't need to worry about it.
对@BrainCore 答案的澄清。实际上,您可能不想修改
__getstate__
内的self
。相反,构造一个将被腌制的新对象,保持原始对象不变以供进一步使用。看起来是这样的:A clarification to @BrainCore's answer. In practice, you probably won't want to modify
self
inside__getstate__
. Instead construct a new object that will get pickled, leaving the original unchanged for further use. Here's what that would look like: