Python deserialize pickle

1  #If you need to process more complex data, use pickle. Pickle can only be used in Python, not other languages. 
2  #Serialization . 
3  import pickle
 4  def sayhi(name):
 5      print ( ' hello ' , name)
 6  
7 f = open( ' test.text ' , ' wb ' )
 8  
9 info = {
 10      ' name ' : ' alex ' ,
 11      ' age ' : 22 ,
 12     ' func ' : sayhi
 13  }
 14  
15 f.write(pickle.dumps(info))   # pickle will convert to binary by default, so the reading method should be wb instead of w. 
16  f.close()
 17  
18  
19  #Inverse Serialization 
20  import pickle
 21  def sayhi(name):
 22      print ( ' hello2 ' , name)   #Deserialize a different function (the name is the same, the method body is different), it's OK. ? If it has not been serialized, why Can be deserialized? 
 23  
24 f = open( ' test.text ' , ' rb ' )
 25 
26  
27 data = pickle.loads(f.read()) #Deserialization   . This will make an error, because the function sayhi will be released after it is used up, and the memory address will no longer exist. So this 
28  print (data)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325145039&siteId=291194637