简体   繁体   中英

How do I know which versions of pickle a particular version of Python supports?

I have a script that requires Python 2.6. I will be adding a large-ish pickled database to the next version, and I want to use the fastest version of pickling. How can I tell which versions of pickling are available in every version of Python 2.6 and later?

Like so:

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

Edit

I think it's safe to rely on the latest documentation. For example the pickle documentation for Python 3.2.1 states:

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.

I think that makes it easy to confirm!

To explicitly answer your question, this means Python 2.6-2.7 support Pickle versions <= 2.0, and Python 3.0-3.2 support Pickle versions <= 3.0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM