How to Serialize a Python Object to Pickle Bytes in Memory
Serialize a Python object to pickle bytes in memory with pickle.dumps, then deserialize it back with pickle.loads and verify the roundtrip.
Python code
34 linesimport pickle
class Person:
def __init__(self, name, age, skills):
self.name = name
self.age = age
self.skills = skills
def main():
person = Person("Alice", 30, ["Python", "SQL", "Docker"])
# Serialize to bytes in memory
pickle_bytes = pickle.dumps(person)
print(f"Serialized bytes length: {len(pickle_bytes)} bytes")
print(f"First 20 bytes: {pickle_bytes[:20]}")
# Deserialize back from bytes
restored_person = pickle.loads(pickle_bytes)
print(f"\nRestored object attributes:")
print(f" Name: {restored_person.name}")
print(f" Age: {restored_person.age}")
print(f" Skills: {restored_person.skills}")
print(f" Type: {type(restored_person).__name__}")
# Verify equality of attribute values
assert person.name == restored_person.name
assert person.age == restored_person.age
assert person.skills == restored_person.skills
print("\nAll attributes match after roundtrip!")
if __name__ == "__main__":
main()
Output
Serialized bytes length: 67 bytes
First 20 bytes: b'\x80\x04\x95\x19\x00\x00\x00\x00\x00\x00\x00\x8c\x06__main__\x94\x8c\x06Person\x94\x93\x94'
Restored object attributes:
Name: Alice
Age: 30
Skills: ['Python', 'SQL', 'Docker']
Type: Person
All attributes match after roundtrip!
How it works
pickle.dumps() converts a Python object into a bytes object without writing to disk, which is useful for in-memory serialization, caching, or transmitting over a network. pickle.loads() reverses the process, reconstructing the original object from the byte stream. The byte output begins with protocol header bytes (e.g., \x80\x04 for protocol 4) followed by the opcodes and object data. Because the class Person is defined in __main__, the pickle format references it by module and qualname, so the class must be importable when unpickling.
Common mistakes
- Forgetting that pickle only works with classes that are importable in the unpickling environment
- Not setting a protocol for compatibility with older Python versions
- Attempting to pickle unpicklable objects (e.g., lambdas, file handles) leads to errors
- Confusing `pickle.dumps` with `pickle.dump` which writes to a file
Variations
- Use `pickle.dumps(person, protocol=pickle.HIGHEST_PROTOCOL)` for the latest serialization format
- Serialize to a file with `pickle.dump` and deserialize with `pickle.load` for disk persistence
Real-world use cases
- Caching complex objects (like machine learning models) in memory or a cache store such as Redis using pickle bytes.
- Sending serialized objects over a message queue (e.g., RabbitMQ) so a consumer can deserialize and process them.
- Persisting a user session object to a database blob column for later recovery.
Sponsored
More from Files & data
- Append a Line to a Log File in Python easy
- Audit File Permissions Across a Project in Python easy
- Automatically Detect Corrupted Files Using SHA-256 Checksums in Python easy
- Automatically Highlight Data Validation Errors Inside Excel Files in Python easy
- Build a Command-Line To-Do List Application with Data Persistence in Python easy
- Build a File Index by Relative Path Hash Map in Python easy
Keep learning
Related tutorials and quizzes for this topic.