On this page
struct.pack_into
Prev: struct.pack | Up: struct | Next: struct.Struct
Rel: struct.calcsize, struct.error, struct.iter_unpack, struct.pack
Ref: docs.python.org — struct.pack_into
Tags: #public #python3 #stdlib #struct
Import
import struct
from struct import pack_into
Signature & Overview
struct.pack_into(format, buffer, offset, v1, v2, ...)
Pack the values v1, v2, ... according to the format string and write the packed bytes into the writable buffer buf starting at offset. Note that the offset is a required argument. A negative offset counts from
Usage
import struct
buf = bytearray(8)
struct.pack_into("!II", buf, 0, 10, 20)
assert struct.unpack_from("!II", buf, 0) == (10, 20)
Gotchas
- Zero-allocation writes into an existing writable buffer (
bytearray/memoryview): Great for binary framing protocols.
REPL Inspect
from struct import pack_into
help(pack_into)
Linked from
Related notes
- tuts/python3/stdlib_module/struct/calcsize Linked here; links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/error Linked here; links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/iter_unpack Linked here; links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/pack Linked here; links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/Struct Linked here; links to this page; shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/struct/unpack shared tags: #stdlib, #struct
- tuts/python3/stdlib_module/struct/unpack_from shared tags: #stdlib, #struct
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib