Python 对象可腌制(或可腌制)是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3603581/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:54:17  来源:igfitidea点击:

What does it mean for an object to be picklable (or pickle-able)?

pythonpickle

提问by Paul

Python docs mention this word a lot and I want to know what it means! Googling doesn't help much..

Python 文档经常提到这个词,我想知道它是什么意思!谷歌搜索没有多大帮助..

采纳答案by Matthew Flaschen

It simply means it can be serialized by the picklemodule. For a basic explanation of this, see What can be pickled and unpickled?. The pickle protocolprovides more details, and shows how classes can customize the process.

它只是意味着它可以被pickle模块序列化。有关此的基本解释,请参阅什么可以腌制和未腌制?. pickle 协议提供了更多详细信息,并展示了类如何自定义流程。

回答by Ivo van der Wijk

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.

通常无法选择的东西是,例如,套接字、文件(处理程序)、数据库连接等。默认情况下,从基本 Python 类型(字典、列表、原语、对象、对象引用,甚至循环)构建(递归)的所有内容都可以进行pickle。

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)

所有这一切使得酸洗比 xml、json 和 yaml 更强大(但绝对不可读)