Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pickle

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 image 720
Paul Hoffman Avatar asked Jul 14 '11 00:07

Paul Hoffman


Video Answer


1 Answers

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.

like image 135
jathanism Avatar answered Sep 22 '22 13:09

jathanism