🐍 Python Advanced

What is Python's pickle module?

Why Interviewers Ask This

Senior Python engineers are expected to reason about architecture, performance, and edge cases. This question separates mid-level from senior candidates by testing deep system-level understanding.

Answer

The pickle module serializes Python objects to bytes (marshaling) and deserializes them back. import pickle; data = pickle.dumps(obj) (to bytes) and pickle.loads(data) (from bytes). File I/O: pickle.dump(obj, file) and pickle.load(file). Pickle can serialize almost any Python object — including lambdas, classes, and complex nested structures. Protocol versions (0-5) — higher means more efficient/compact. Security warning: never unpickle data from untrusted sources — a malicious pickle can execute arbitrary code during deserialization. Use JSON, MessagePack, or Protocol Buffers for data exchange with external systems. Legitimate uses: caching ML models (scikit-learn), saving game state, IPC between Python processes (multiprocessing uses pickle). Custom serialization: implement __getstate__ and __setstate__. copyreg module registers custom pickle functions for non-picklable types.

Pro Tip

Back up your answer with a specific project or situation. Saying 'In my last Python project, I used this when...' immediately makes your answer more credible and memorable.