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)