Python Learning Note 12: Object serialization of the standard library (pickle package, Cpickle package)

Source: Internet
Author: User
Tags class definition object serialization pickle package

A binary sequence is stored in the computer's memory.
We are able to directly fetch data from the corresponding location of an object, convert it to a text stream (the process is called serialize), and then deposit the text stream into a file.
Because Python is involved in the class definition of the object when it is created, so when we read the object from the text, we have to have the class definition of the object handy and know how to reconstruct the object.
When reading from a file, for Python's built-in (built-in) objects (such as integers, dictionaries, tables, and so on), because their class definitions already have memory loaded, it is not necessary for us to define classes in the program.
However, for a user-defined object, it is necessary to define the class first, then the ability to load the object from the file.

A pickle package for the above process, the most frequently used tool is the pickle package in Python.
1 Convert an in-memory object into a text stream:
Import Pickleclass Bird (object):    have_feather = True    way_of_reproduction  = ' egg ' summer = Bird () picklestring = Pickle.dumps (Summer) # Serialize Object

Use the Pickle.dumps () method to convert an object summer into a string picklestring (that is, a text stream).
We can then store the string in the file (the input and output of the text file) using the normal text storage method.
Import Pickleclass Bird (object):    have_feather = True    way_of_reproduction  = ' egg ' summer = Bird () FileName = ' Save.pkl ' with open (FileName, ' W ') as f: # Open file with write-mode    picklestring = Pickle.dump (Summer, F) # serialize and save Object
Object summer stored in file save.pkl
2 Rebuilding an object first, we are going to read the text from the text and store it in a string (the input and output of the text file).
The string is then converted to an object using the Pickle.loads (str) method.
Remember, at this point in our program we must already have the class definition for that object.
Import Pickle # define the class before Unpickleclass Bird (object):    have_feather = True    way_of_reproduction  = ' egg ' fileName = ' save.pkl ' with open (FileName, ' R ') as F:    summer = Pickle.load (f) # Read file and build object

The function and use of the Cpickle package of the two Cpickle package almost exactly the same as the pickle package, where the difference is actually very seldom used;
The difference is that Cpickle is based on the C language, which is 1000 times times faster than the pickle package.
For the example above, it is assumed that using the Cpickle package, we can change the import statement to:
Import Cpickle as Pickle
There is no need to make any changes.

Python Learning Note 12: Object serialization of the standard library (pickle package, Cpickle package)

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.