In the following article, let’s take a look at what serialization in python is. Learn about python serialization and what role python serialization can play in python programming.
During the running of the program, all variables are in memory. For example, define a dict:
d = dict(name='Bob', age=20, score=88)
You can modify the variables at any time, such as changing the name to 'Bill', but Once the program ends, all the memory occupied by the variables is reclaimed by the operating system. If the modified 'Bill' is not stored on the disk, the next time you run the program again, the variable will be initialized to 'Bob' again.
We call the process of changing variables from memory to storable or transferable, which is called pickling in Python. It is also called serialization, marshalling, flattening, etc. in other languages. It means the same thing.
After serialization, the serialized content can be written to disk or transmitted to other machines through the network.
In turn, re-reading the variable content from the serialized object into the memory is called deserialization, that is, unpickling.
Python provides the pickle module to implement serialization.
First, we try to serialize an object and write it to a file:
>>> import pickle >>> d = dict(name='Bob', age=20, score=88) >>> pickle.dumps(d) b'\x80\x03}q\x00(X\x03\x00\x00\x00ageq\x01K\x14X\x05\x00\x00\x00scoreq\x02KXX\x04\x00\x00\x00nameq\x03X\x03\x00\x00\x00Bobq\x04u.'
The pickle.dumps() method serializes any object into a bytes, and then writes the bytes document. Or use another method pickle.dump() to directly serialize the object and write it to a file-like Object:
>>> f = open('dump.txt', 'wb') >>> pickle.dump(d, f) >>> f.close()
Look at the dump.txt file written, a bunch of messy content, these are Object internal information saved by Python.
When we want to read an object from disk to memory, we can first read the content into bytes, and then use the pickle.loads() method to deserialize the object, or we can directly use pickle.load() Method directly deserializes an object from a file-like Object. We open another Python command line to deserialize the object we just saved:
>>> f = open('dump.txt', 'rb') >>> d = pickle.load(f) >>> f.close() >>> d {'age': 20, 'score': 88, 'name': 'Bob'}
The contents of the variable are back!
Of course, this variable is completely unrelated to the original variable. They just have the same content.
The problem with Pickle is the same as the serialization problem specific to all other programming languages, that is, it can only be used with Python, and it is possible that different versions of Python are incompatible with each other. Therefore, you can only use Pickle to save those that are not important. It doesn't matter if the data cannot be successfully deserialized.
The above is all the content described in this article. This article mainly introduces the relevant knowledge of python serialization. I hope you can use the information to understand the above content. I hope what I have described in this article will be helpful to you and make it easier for you to learn python.
For more related knowledge, please visit the Python tutorial column on the php Chinese website.
The above is the detailed content of What is serialization in python? (Example analysis). For more information, please follow other related articles on the PHP Chinese website!