ruk·si

🐍 Python
Structs

Updated at 2018-06-09 17:48

Structs are serializable data exchange format. Rarely used but might become handy in some cases.

from struct import Struct

MyStruct = Struct('i?f')

packed = MyStruct.pack(23, False, 42.00)
assert packed == b'\x17\x00\x00\x00\x00\x00\x00\x00\x00\x00(B'

unpacked = MyStruct.unpack(packed)
assert unpacked == (23, False, 42.0)

Source

  • Python Tricks The Book, Dan Bader
  • Fluent Python, Luciano Ramalho