0 votes
in Python by
What is pickling and unpickling in Python?

3 Answers

0 votes
by

Pickling is just the Python way of saying serializing. Pickling lets you serialize an object into a string (or anything else you choose) in order to be persisted on storage or sent over network. Unpickling is the process of restoring the original object from a pickled string.

Pickle is not secure. Only unpickle objects from trusted sources

Python docs

Here is how you would pickle a basic data structure:

import pickle

cars = {"Subaru": "best car", "Toyota": "no i am the best car"}

cars_serialized = pickle.dumps(cars)

# cars_serialized is a byte string

new_cars = pickle.loads(cars_serialized)

What are *args and **kwargs in Python functions?

These deal closely with unpacking. If you put *args in function’s parameter list, all unnamed arguments will be stored in the args array. **kwargs works the same way, but for named parameters:

def func1(*args, **kwargs):

    print(args)

    print(kwargs)

func1(1, 'abc', lol='lmao')

> [1, 'abc']

> {"lol": "lmao"}

0 votes
by

Pickling is the process of converthing a Python object hierarachy into a byte stream for storing it into a database.It is also known as serialization. 

Unpickling is the reverse of pickling. The byte stream is conveted back into obhect hierarchy.

0 votes
by

The Pickle module in Python allows accepting any object and then converting it into a string representation. It then dumps the same into a file by means of the dump function. This process is known as pickling.

The reverse process of pickling is known as unpickling i.e. retrieving original Python objects from a stored string representation.

Related questions

0 votes
asked Oct 12, 2021 in Python by rajeshsharma
0 votes
asked Sep 21, 2021 in Python by sharadyadav1986
...