Python:pickle Module Learning

Source: Internet
Author: User

1. The role of the Pickle module

persist objects such as dictionaries, lists, strings, and so on to disk for later use

2. Pickle Object Serialization

Pickle module The process of converting any Python object into a system byte is called a serialized object.

3. Pickle and Cpickle

The Python standard library provides pickle and cpickle modules. Cpickle is encoded in C and is higher in efficiency than pickle , but the type defined in the Cpickle module cannot be inherited (most of the time, we do not need to inherit from these types, we recommend using Cpickle). The serialization/deserialization rules for cpickle and Pickle are the same, using pickle to serialize an object that can be deserialized using Cpickle .

4. Functions commonly used in pickle (1)pickle.dump (obj, file, [, Protocol])

Role: Data to be persisted "object", saved to "file", using 3 kinds of protocols, index 0 is ascii,1 for the old binary, 2 is the new binary protocol, the difference is that 2 is more efficient (by default, 0 protocol)

(2)pickle.load (file)

Role: Reading strings from "files" and deserializing them into Python data objects

(3)Pickle.dumps (obj[, Protocol])

Function: Serializes an Obj object into a string rather than depositing it in a file

    • Obj: obj object to serialize
    • Protocal: If the item is omitted, the default is 0. If negative or Highest_protocol, the highest protocol version is used
(4)pickle.loads (String)

Function: The Obj object before serialization is read from a string

Python:pickle Module Learning

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.