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

PythonPickle

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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)

Solution 3 - Python

Pickling is the process in which the objects in python are converted into simple binary representation that can be used to write that object in a text file which can be stored. This is done to store the python objects and is also called as serialization. You can infer from this what de-serialization or unpickling means.

So when we say an object is picklable it means that the object can be serialized using the pickle module of python.

Solution 4 - Python

These are all great answers, but for anyone who's new to programming and still confused here's the simple answer:

Pickling is making it so you can store it long term and get it later without it going bad. A bit like Saving in a video game.

So anything that's actively changing (like a live connection to a database) can't be stored directly (though you could probably figure out a way to store the information needed to create a new connection, and that you could pickle)

Bonus definition: Serializing is packaging it in a form that can be handed off to another program. Unserializing it is unpacking something you got sent so that you can use it

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaulView Question on Stackoverflow
Solution 1 - PythonMatthew FlaschenView Answer on Stackoverflow
Solution 2 - PythonIvo van der WijkView Answer on Stackoverflow
Solution 3 - PythonAnkit AgrawalView Answer on Stackoverflow
Solution 4 - PythonChris RuddView Answer on Stackoverflow