Python--pickle serialization and deserialization (1)

Source: Internet
Author: User
Tags serialization

JSON can be used between different languages, and pickle can only be used in Python

JSON cannot serialize functions, and pickle can. However, after the program uses the Pickle.dump serialization function, the memory that executes the function is freed, and the space that corresponds to the memory address read by another program with Pickle.load re-opened has been freed, so the function cannot be executed:

1 #_*_coding:utf-8_*_2 #__author__ = "Csy"3 ImportPickle4 defSayhi (name):5     Print("Hello,", name)6 7info = {8     'name':'Csy',9     ' Age':' to',Ten     'Job':'Workers', One     'func': Sayhi A } -f = open ("Test.text","WB") -Pickle.dump (INFO,F)#equivalent to F.write (Pickle.dumps (info)) theF.close ()

To demonstrate no error, the serialized function Sayhi is copied to the Python file that calls Pickle.load:

1 #_*_coding:utf-8_*_2 #__author__ = "Csy"3 ImportPickle4 defSayhi (name):5     Print("Hello,", name)6 7f = open ("Test.text","RB")8data = Pickle.load (f)#equivalent to data = Pickle.loads (F.read ())9 f.close ()Ten  One Print(data) A Print(data['func']('Csy'))

Output:

{' func ': <function sayhi at 0x000000000111fea0>, ' job ': ' Worker ', ' name ': ' Csy ', ' age ': ' 31 '}
Hello, Csy.
None

Python--pickle serialization and deserialization (1)

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.