Skip to content

marshal Module

The marshal module serializes Python objects to a binary format optimized for use with compiled bytecode, faster than pickle but version-dependent.

Complexity Reference

Operation Time Space Notes
dumps() O(n) O(n) n = object size
loads() O(n) O(n) n = data size

Serializing Python Objects

Basic Marshaling

import marshal

# Serialize - O(n)
data = {'name': 'Alice', 'age': 30}
serialized = marshal.dumps(data)

# Deserialize - O(n)
restored = marshal.loads(serialized)
print(restored)  # {'name': 'Alice', 'age': 30}

# File I/O
with open('data.pyc', 'wb') as f:
    marshal.dump(data, f)

with open('data.pyc', 'rb') as f:
    data = marshal.load(f)