Pickle & Unpickle

pickle unpickle

When we opened a file in Python and try to read inside its content we use the read() method which returns a sequence. A sequence is a positionally ordered collection of items. Sequences are a generic term for an ordered set of items, which means the order of items we read from this set will be exactly the same as the order we put the items in.

We cannot read or write more complicated Python data types like dictionaries, lists, or class instances so simple as the “read” method.

When we try to extend the io operation to these kinds of objects, we need different ways to handle these objects because the “read()” method will be insufficient.

For these, we can simply use pickle libraries to handle io operations easily.

We can convert the hierarchy of a Python object into a byte-stream and we say “pickling” to this operation.
In reverse, we convert a byte-stream back into a Python object hierarchy and we say “unpickling” to this.

Synonymly, you can find “serializing” and “marshaling” words in many articles on the internet. Also, there are some standard serializing libraries in Python:

  • pickle: serializes the objects as I mentioned above
  • marshall: primarily handles .pyc files
  • json: serializes text-based objects

To pickle an object, you can basically import the pickle library, open a file and make your serialization:

import pickle

class A:
    class_attr = 'a class_attr'

a = A()

pickle.dump(a, open("an_instance.p", "wb"))

a_loaded = pickle.load(open("an_instance.p", "rb"))

assert a.class_attr == a_loaded.class_attr

Source:
Dosya Okuma ve Yazma
pickle