Answer the question
In order to leave comments, you need to log in
Why is serialization necessary?
Serialization (in programming) is the process of translating a data structure into a sequence of bits. The inverse to the serialization operation is the deserialization (structuring) operation - restoring the initial state of the data structure from the bit sequence.
Answer the question
In order to leave comments, you need to log in
Suppose you have a complex hierarchy of classes, each of which has a dozen fields. Some fields refer to objects of other classes or, even worse, contain collections of objects.
When using a serializer , you only need one line to save a dictionary containing objects from this hierarchy to a file: pickle version.
Serialization:
Serialization guarantees (for supported data structures) that after the serialization/deserialization process you will get the same data structures.
Without it, you would need to manually create your own protocols and methods for converting data into a form suitable for transmission somewhere.
Well, the pickle module is a built-in implementation of serialization, but at the same time - one of many. You can use e.g. json, or yaml. Anything that will translate ordinary data structures into a form suitable for transfer (string, byte string) and return (deserialize) back.
For example, you need to pass a data structure to a remote server.
The data structure looks something like this
struct {
int age=17;
stringname="Vasya";
date birthday="01/01/2001"
}
We are going to send it to a remote server with a regular HTTP request, in which, in principle, you can attach XML, or you can send it just in one line right in the header. To do this, we can serialize our data structure into a string, transfer it there, unpack it there and get the structure right away.
The difference is that on the other side we will not "parse" unknown data, but rather deserialize it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question