Python: serialization

Source: Internet
Author: User
#  Object serialization  
Import Pickle
D = { " A " : " China " }
A = pickle. dumps (D, 2) # Serialize binary
Print A
F = open (" Data " , " WB " )
F. Write ()
F. Close ()


# Object deserialization
Import Pickle
F = open ( " Data " ," RB " )
A = f. Read ()
D = pickle. Loads ()
Print D [ ' A ' ]

The value range of pickle. dumps (D, 2) is (0, 1, 2), and 2 is binary.

Note:

 

What if we serialize A Class Object (class instance? An object can be instantiated on machine A and can be correctly serialized and transmitted. An error occurs when deserialization is reached on machine B, the prompt is that the class of this object is not instantiated, but this class is defined at this time, the deserialization can be successful, and there is no problem with method calling. That is to say, during serialization, the object metadata is entered into the package, but an error occurs when the metadata cannot be found on machine B.

conclusion: If A and B have the same Lib, the class defined in this lib, their objects can be transferred and used across machines

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.