对象可腌制(或可腌制)意味着什么?
Python 文档多次提到这个词,我想知道它的含义。
Python docs mention this word a lot and I want to know what it means.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
Python 文档多次提到这个词,我想知道它的含义。
Python docs mention this word a lot and I want to know what it means.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(4)
它只是意味着它可以由
pickle
模块序列化。有关此内容的基本说明,请参阅什么可以腌制和未腌制?。 Pickling 类实例 提供了更多详细信息,并展示了类如何可以定制流程。It simply means it can be serialized by the
pickle
module. For a basic explanation of this, see What can be pickled and unpickled?. Pickling Class Instances provides more details, and shows how classes can customize the process.通常不可选择的东西有套接字、文件(处理程序)、数据库连接等。默认情况下,从基本 Python 类型(字典、列表、基元、对象、对象引用,甚至循环)构建(递归)的所有内容都可以进行 pickle。
您可以实现自定义酸洗代码,例如,存储数据库连接的配置并在之后恢复它,但您将需要特殊的自定义逻辑。
所有这些使得 pickling 比 xml、json 和 yaml 更强大(但绝对不那么可读)
Things that are usually not pickable are, for example, sockets, file(handler)s, database connections, and so on. Everything that's build up (recursively) from basic python types (dicts, lists, primitives, objects, object references, even circular) can be pickled by default.
You can implement custom pickling code that will, for example, store the configuration of a database connection and restore it afterwards, but you will need special, custom logic for this.
All of this makes pickling a lot more powerful than xml, json and yaml (but definitely not as readable)
这些都是很好的答案,但对于任何刚接触编程但仍然感到困惑的人来说,这里有一个简单的答案:
Pickling一个对象正在制作它,这样你就可以长期存储它,并按当前状态锁定(通常是为了硬盘)。有点像电子游戏中的保存。
因此,任何正在主动更改的内容(例如与数据库的实时连接)都无法直接存储(尽管您可能会找到一种方法来存储创建新连接所需的信息,并且您可以可以pickle)
额外定义:序列化正在将其打包成可以传递给另一个程序的形式。 反序列化它是对您发送的内容进行解包,以便您可以使用它
These are all great answers, but for anyone who's new to programming and still confused here's the simple answer:
Pickling an object is making it so you can store it long term, locked as it currently is (often to hard disk). A bit like Saving in a video game.
So anything that's actively changing (like a live connection to a database) can't be stored directly (though you could probably figure out a way to store the information needed to create a new connection, and that you could pickle)
Bonus definition: Serializing is packaging it in a form that can be handed off to another program. Unserializing it is unpacking something you got sent so that you can use it
Pickling 是将 python 中的对象转换为简单的二进制表示形式的过程,可用于将该对象写入可存储的文本文件中。这样做是为了存储 python 对象,也称为序列化。您可以从中推断出反序列化或unpickling的含义。
因此,当我们说一个对象是picklable时,意味着该对象可以使用Python的pickle模块序列化。
Pickling is the process in which the objects in python are converted into simple binary representation that can be used to write that object in a text file which can be stored. This is done to store the python objects and is also called as serialization. You can infer from this what de-serialization or unpickling means.
So when we say an object is picklable it means that the object can be serialized using the pickle module of python.