Pythonpickle class library introduction (object serialization and deserialization)-Python tutorial

Source: Internet
Author: User
This article mainly introduces the Pythonpickle Class Library (object serialization and deserialization ), this article describes the functions of the pickle Library, the running process of the pickle, the use of instances, and the modification of default behaviors of the picklable type. For more information, see I. pickle

The pickle module is used to serialize and deserialize python objects. Generally, pickle serializes a python object into a binary stream or file.

Serialization and deserialization between python objects and files:

The code is as follows:


Pickle. dump ()
Pickle. load ()


To serialize and deserialize python objects and strings, use:

The code is as follows:


Pickle. dumps ()
Pickle. loads ()



The following types can be serialized:
* None, True, and False;
* Integer, floating point, plural;
* String, byte stream, and byte array;
* Contains the tuples, lists, sets, and dictionaries of pickle objects;
* Function defined at the top layer of the module:
* Defines the built-in functions at the top layer of the module;
* Defines the class at the top layer of the module;
* Has a custom type of _ dict _ () or _ setstate;

Note: serialization of functions or classes is identified by names, so you need to import the corresponding module.

II. execution process of pickle

In most cases, we do not need additional code for the object picklable. By default, pickle intelligently checks the attributes of classes and instances. when a class instance is deserialized, its _ init _ () method is usually not called. Instead, create an uninitialized instance and then reply to the stored attributes.

However, you can modify the default behavior by implementing the following methods:

The code is as follows:


Object. _ getstate _ (): The _ dict __of the object is serialized by default. However, if you implement _ getstate _ (), _ getstate __() the value returned by the function will be serialized.
Object. _ setstate _ (state): If the type implements this method, this method is used to restore the attributes of the object during deserialization.
Object. _ getnewargs _ (): This function is required if a parameter is required during instance construction (_ new.


Note: If _ getstate _ () returns False, _ setstate _ () is not called during deserialization.

Sometimes, for efficiency, or the above three functions cannot meet the requirements, the _ reduce _ () function must be implemented.

III. instances

The code is as follows:


Import pickle

# An arbitrary collection of objects supported by pickle.
Data = {
'A': [1, 2.0, 3, 4 +],
'B': ("character string", B "byte string "),
'C': set ([None, True, False])
}

With open ('data. pickle', 'wb') as f:
# Pickle the 'data' dictionary using the highest protocol available.
Pickle. dump (data, f, pickle. HIGHEST_PROTOCOL)


With open ('data. pickle', 'RB') as f:
# The protocol version used is detected automatically, so we do not
# Have to specify it.
Data = pickle. load (f)
Print (str (data ))

4. modify the default behavior of the picklable type

The code is as follows:


Class TextReader:
"Print and number lines in a text file ."""

Def _ init _ (self, filename ):
Self. filename = filename
Self. file = open (filename)
Self. lineno = 0

Def readline (self ):
Self. lineno + = 1
Line = self. file. readline ()
If not line:
Return None
If line. endswith ('\ n '):
Line = line [:-1]
Return "% I: % s" % (self. lineno, line)

Def _ getstate _ (self ):
# Copy the object's state from self. _ dict _ which contains
# All our instance attributes. Always use the dict. copy ()
# Method to avoid modifying the original state.
State = self. _ dict _. copy ()
# Remove the unpicklable entries.
Del state ['file']
Return state

Def _ setstate _ (self, state ):
# Restore instance attributes (I. e., filename and lineno ).
Self. _ dict _. update (state)
# Restore the previusly opened file's state. To do so, we need
# Reopen it and read from it until the line count is restored.
File = open (self. filename)
For _ in range (self. lineno ):
File. readline ()
# Finally, save the file.
Self. file = file

Reader = TextReader ("hello.txt ")
Print (reader. readline ())
Print (reader. readline ())
S = pickle. dumps (reader)
# Print (s)
New_reader = pickle. loads (s)
Print (new_reader.readline ())

# The output is
#1: hello
#2: how are you
#3: goodbye

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.