What is Pickling and Unpickling?

Pickling: The process whereby a Python object hierarchy is converted into a byte stream.

Unpickling: Inverse operation of Pickling, whereby a byte stream is converted back into an object hierarchy.

Both are done using the pickle module.

import pickle
obj_orders = [{'product_id': '1233', 'amount': 108}, {'product_id': '1277', 'amount': 639 }]

# pickling
b_stream = pickle.dumps(obj_orders)   

# Unpickling
orders =  pickle.loads(b_stream) 
print(orders)

Output:

[{'product_id': '1233', 'amount': 108}, {'product_id': '1277', 'amount': 639}]

Leave a Reply

Your email address will not be published. Required fields are marked *