Python: why pickle?

Pickle is unsafe because it constructs arbitrary Python objects by invoking arbitrary functions. However, this is also gives it the power to serialize almost any Python object, without any boilerplate or even white-/black-listing (in the common case). That’s very desirable for some use cases:

  • Quick & easy serialization, for example for pausing and resuming a long-running but simple script. None of the concerns matter here, you just want to dump the program’s state as-is and load it later.
  • Sending arbitrary Python data to other processes or computers, as in multiprocessing. The security concerns may apply (but mostly don’t), the generality is absolutely necessary, and humans won’t have to read it.

In other cases, none of the drawbacks is quite enough to justify the work of mapping your stuff to JSON or another restrictive data model. Maybe you don’t expect to need human readability/safety/cross-language compatibility or maybe you can do without. Remember, You Ain’t Gonna Need It. Using JSON would be the right thing™ but right doesn’t always equal good.

You’ll notice that I completely ignored the “slow” downside. That’s because it’s partially misleading: Pickle is indeed slower for data that fits the JSON model (strings, numbers, arrays, maps) perfectly, but if your data’s like that you should use JSON for other reasons anyway. If your data isn’t like that (very likely), you also need to take into account the custom code you’ll need to turn your objects into JSON data, and the custom code you’ll need to turn JSON data back into your objects. It adds both engineering effort and run-time overhead, which must be quantified on a case-by-case basis.

Leave a Comment