Python standard library 05 storage objects (pickle package, cpickle Package)

Source: Internet
Author: User
Tags class definition pickle package

Vamei source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

Thank you Reverland correction

In the previous introduction to Python objects (the basic concept of object-oriented, the further development of object-oriented), I mentioned Python's philosophy of "all objects", in python, whether it is a variable or a function, is an object. When Python runs, the object is stored in memory and waits for the system to be called at any Time. however, the data in memory will shut down and disappear with the computer, how to save the object to a file and store it on the hard disk?

The Computer's memory is stored in a binary sequence (of course, in the Linux eye, it is a text stream). We can directly fetch the data from the location of an object, convert it to a text stream (the process is called serialize), and then deposit the text stream into a file. Since Python is referring to the class definition of an object when creating an object, when we read the object from the text, we must have the class definition of the object handy to 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), it is not necessary for us to define classes in the program because their class definitions are already loaded into memory. But for a user-defined object, the class must be defined before the object can be loaded from the file (such as an object in the basic object-oriented concept summer).

Pickle Bag

For the above procedure, the most common tool is the pickle package in Python.

1) convert an In-memory object into a text stream:

Import pickle# define Classclass Bird (object):    have_feather = True    way_of_reproduction  = ' egg ' summer     = Bird ()                 # construct an objectpicklestring = Pickle.dumps (summer)   # Serialize Object

You can use the Pickle.dumps () method to convert an object summer to 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.

Of course, We can also use the Pickle.dump () method to combine the above two parts:

Import Pickle

# define Classclass Bird (object): have_feather = True way_of_reproduction = ' egg ' summer = Bird () # construct an objectfn = ' a.pkl ' with open (fn, ' W ') as F: # Open file with Write-mode
picklestring = Pickle.dump (summer, f) # Serialize and save object

Object summer stored in file a.pkl

2) Rebuilding objects

first, we want 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.

In addition, we can use the Pickle.load () method to merge the above steps:

Import pickle# define the class before Unpickleclass Bird (object):    have_feather = True    way_of_reproduction  = ' egg ' fn     = ' a.pkl ' with open (fn, ' r ') as f:    summer = pickle.load (f)   # Read file and build object

Cpickle Bag

The functionality and usage of the Cpickle package is almost identical to the pickle package (where the difference is actually rarely used), except that the Cpickle is written in C and is 1000 times times faster than the pickle Package. For the above example, if you want to use the Cpickle package, we can change the import statement to:

Import Cpickle as Pickle

There is no need to make any more changes.

Summarize

Text-to-file with objects

Pickle.dump (), pickle.load (), cpickle

Python standard library 05 storage objects (pickle package, cpickle Package)

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.