Python Glossary Русский

Python 3.14+compression.zstdterm

compression.zstd.ZstdDict

dictionaries

A Zstandard dictionary, a set of typical fragments shared by compression and decompression; it is passed as zstd_dict.

Syntax

compression.zstd.ZstdDict(dict_content, /, *, is_raw=False)

ZstdDict(content) expects a trained dictionary with a header; arbitrary bytes need is_raw=True, and then dict_id is 0. Content shorter than 8 bytes raises ValueError. The as_digested_dict and as_prefix properties change how it is loaded: as_prefix applies to one frame and must be passed to the decompressor as well, otherwise ZstdError. The object itself works for both compress and decompress and can be reused.

Examples

# Python 3.14+
from compression import zstd  # → ? нужна сборка CPython с libzstd
raw = zstd.ZstdDict(b'hello zstd hello zstd ', is_raw=True)
print(raw.dict_id)  # → 0
packed = zstd.compress(b'hello zstd!', zstd_dict=raw.as_prefix)
print(zstd.decompress(packed, zstd_dict=raw.as_prefix))  # → b'hello zstd!'
zstd.ZstdDict(b'short')  # → ValueError: Zstandard dictionary content too short (must have at least eight bytes)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy