The pickle of Python object serialization

Source: Internet
Author: User
Tags compact object serialization readable serialization

This article is mainly to the Pickle official website reading record.

The pickle module implements binary protocols for serializing and de-serializing a Python object structure. "Pickling" is the process whereby a Python object hierarchy was converted into a byte stream, and "unpickling" is the inverse operation, whereby a byte stream (from a binary file or Bytes-like object) is Conve RTed back to an object hierarchy. Pickling (and unpickling) is alternatively known as "serialization", "marshalling," [1] or "flattening"; However, to avoid confusion, the terms used here is "pickling" and "unpickling".

Pickle is one of the Python standard modules and does not require additional installation.

The pickle is used to serialize and deserialize the Python object structure. In fact, it is a kind of data storage way, the data structure of Python is saved in a certain form. In addition, the data after Pickle serialization is not human-readable.

Here to mention the foreigner's naming habits of things, pickle is pickled meaning, then the "pickled" Python object, in fact, is a kind of data processing, as to the rules of data processing, here for the moment do not do further introduction.

" pickling" is the conversion of a hierarchical Python object into a byte stream;"Unpickling" is the opposite process.

Note: If encounter "pickling" "serialization", "marshalling," or "flattening", all are to express the same meaning, translated into "serialization" is good, if the word before the addition of UN, will be turned into "deserialization."

Warning:the pickle module is not secure against erroneous or maliciously constructed data. Never unpickle data received from an untrusted or unauthenticated source.

Do not serialize bad or malicious structured data, and do not deserialize untrusted or unauthorized data sources. This means that "serialization" and "deserialization" are performed according to the rules of the Pickle module.

Data stream Format

The data format used is pickle python-specific. This have the advantage that there is no restrictions imposed by external standards such as JSON or XDR (which can ' t repre Sent pointer sharing); However it means that Non-python programs is not being able to reconstruct pickled Python objects.

The data format used by Pickle is unique to the Python language. Non-Python programs may not be able to refactor the serialized data.

By default, the pickle data format uses a relatively compact binary representation. If you need optimal size characteristics, you can efficiently compress pickled data.

By default, the serialized data format of Pickle is a relatively compact binary representation. If you have higher data size requirements, you can compress the serialized data.

The module pickletools contains tools for analyzing data streams generated by pickle . pickletools Source code have extensive comments AB Out opcodes used by pickle protocols.

Pickletools contains a number of tools for parsing serialized data.

There is currently 5 different protocols which can be used for pickling. the higher the protocol used, the more recent the version of Python needed to read the pickle produced.

  • Protocol version 0 is the original "human-readable" Protocol and are backwards compatible with earlier versions of Python.
  • Protocol version 1 is a old binary format which was also compatible with earlier versions of Python.
  • Protocol version 2 is introduced in Python 2.3. It provides much more efficient pickling of new-style classes. Refer to PEP 307 For information on improvements brought by protocol 2.
  • Protocol version 3 is added in Python 3.0. It has explicit support for bytes objects and cannot is unpickled by Python 2.x. This is the default protocol, and the recommended protocol if compatibility with other Python 3 versions is required.
  • Protocol version 4 is added in Python 3.4. It adds support for very large objects, pickling more kinds of objects, and some data format optimizations. Refer to PEP 3154 For information on improvements brought by protocol 4.

Note:

serialization are a more primitive notion than persistence; Although pickle  reads and writes file objects, it does not handle the issue of NA Ming persistent objects, nor the (even more complicated) issue of concurrent access to persistent objects. The pickle  module can Transform a complex object into a byte stream and it can transform the byte stream to an object with the same internal s Tructure. Perhaps the most obvious thing to does with these byte streams are to write them onto a file, but it's also conceivable to s End them across a network or store them in a database. The shelve  module Provides a simple interface to pickle and Unpickle objects on Dbm-style database files.

Module Interface

The pickle of Python object serialization

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.