我如何知道特定版本的Python支持哪个版本的pickle?

2024-06-16 09:22:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个需要Python2.6的脚本。我将在下一个版本中添加一个大的ish pickle数据库,我想使用pickling的最快版本。在Python 2.6及更高版本的中,如何区分哪些版本的pickling可用?


Tags: 版本脚本数据库pickle区分ishpickling
1条回答
网友
1楼 · 发布于 2024-06-16 09:22:53

就像这样:

>>> import pickle
>>> pickle.compatible_formats
['1.0', '1.1', '1.2', '1.3', '2.0']

编辑

我认为依靠最新的文件是安全的。例如pickle documentation for Python 3.2.1状态:

There are currently 4 different protocols which can be used for pickling.

  • Protocol version 0 is the original human-readable protocol and is backwards compatible with earlier versions of Python.

  • Protocol version 1 is the old binary format which is also compatible with earlier versions of Python.

  • Protocol version 2 was introduced in Python 2.3. It provides much more efficient pickling of new-style classes.

  • Protocol version 3 was added in Python 3.0. It has explicit support for bytes and cannot be unpickled by Python 2.x pickle modules. This
    is the current recommended protocol, use it whenever it is possible.

我想这很容易确认!

为了明确回答您的问题,这意味着Python 2.6-2.7支持Pickle版本<;=2.0,Python 3.0-3.2支持Pickle版本<;=3.0。

相关问题 更多 >