The module Python pickle in data science is for serializing and de-serializing a Python object structure. An object in Python can be pickled so; it can be saved on disk. Pickle serializes the object before writing it to file. Pickling is a method which converts a Python object (list, dict, etc.) into character stream. The character stream consists of information necessary for reconstructing object in another Python script.
Pickling without a file:
Advantages of pickling module:
- Recursive objects – These are objects having reference to themselves. Pickle is known to keep track of the objects which it has serialize. This is the reason reference to the same object won’t be serialize again and again. The module marshal breaks for it.
- Object sharing – It shows reference to the same object in different places. It is similar to self referencing objects. Pickle stores object once and ensure all other reference points to master copy. The objects share will remain share, that can be important for mutable objects.
- User-defined classes and their instances – Marshal will not support these, but pickle can save and restore class instances transparently. The class definition is importable and lives in same module as when the object was store.