Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean for an object to be picklable (or pickle-able)?

Tags:

python

pickle

Python docs mention this word a lot and I want to know what it means! Googling doesn't help much..

like image 603
Paul Avatar asked Aug 30 '10 19:08

Paul


People also ask

What does Picklable mean in Python?

Pickle in Python is primarily used in serializing and deserializing a Python object structure. In other words, it's the process of converting a Python object into a byte stream to store it in a file/database, maintain program state across sessions, or transport data over the network.

How do you know if an object is Pickleable?

In python you can check if the object has some properties in two ways. Check if object is an instance of some Abstract Base Class. E.g. Number "The root of the numeric hierarchy. If you just want to check if an argument x is a number, without caring what kind, use isinstance(x, Number)."

What is pickling and Unpickling?

“Pickling” is the process whereby a Python object hierarchy is converted into a byte stream, and “unpickling” is the inverse operation, whereby a byte stream (from a binary file or bytes-like object) is converted back into an object hierarchy.

What is pickling how it is performed give example?

Pickling is a way to convert a python object (list, dict, etc.) into a character stream. The idea is that this character stream contains all the information necessary to reconstruct the object in another python script.


2 Answers

It simply means it can be serialized by the pickle module. For a basic explanation of this, see What can be pickled and unpickled?. Pickling Class Instances provides more details, and shows how classes can customize the process.

like image 147
Matthew Flaschen Avatar answered Sep 28 '22 06:09

Matthew Flaschen


Things that are usually not pickable are, for example, sockets, file(handler)s, database connections, and so on. Everything that's build up (recursively) from basic python types (dicts, lists, primitives, objects, object references, even circular) can be pickled by default.

You can implement custom pickling code that will, for example, store the configuration of a database connection and restore it afterwards, but you will need special, custom logic for this.

All of this makes pickling a lot more powerful than xml, json and yaml (but definitely not as readable)

like image 43
Ivo van der Wijk Avatar answered Sep 28 '22 06:09

Ivo van der Wijk