🌐
Quora
quora.com › What-is-the-equivalent-of-a-struct-in-Python
What is the equivalent of a struct in Python? - Quora
Answer (1 of 4): Lack of `struct` is one of the major drawbacks in Python. It’s a scripting language with dynamic data model based on hash tables. Almost all items are looked up by a “name” at runtime. Languages with strictly typed structures prevent all sorts of bugs in your programs.
🌐
Python
docs.python.org › 3 › library › struct.html
struct — Interpret bytes as packed binary data
Source code: Lib/struct.py This module converts between Python values and C structs represented as Python bytes objects. Compact format strings describe the intended conversions to/from Python valu...
🌐
Reddit
reddit.com › r/programminglanguages › statically-typed equivalent of python's `struct` module?
r/ProgrammingLanguages on Reddit: Statically-typed equivalent of Python's `struct` module?
March 13, 2025 -

In the past, I've used Python's struct module as an example when asked if there are any benefits of dynamic typing. It provides functions to convert between sequences of bytes and Python values, controlled by a compact "format string". Lua also supports very similar conversions via the string.pack & unpack functions.

For example, these few lines of Python are all it takes to interpret the header of a BMP image file and output the image's dimensions. Of course for this particular example it's easier to use an image library, but this code is much more flexible - it can be changed to support custom file types, and iteratively modified to investigate files of unknown type:

file_name = input('File name: ')
with open(file_name, 'rb') as f:
    signature, _, _, header_size, width, height = struct.unpack_from('<2sI4xIIii', f.read())
assert signature == b'BM' and header_size == 40
print(f'Dimensions: {width}x{abs(height)}')

Are there statically-typed languages that can offer similarly concise code for binary manipulation? I can see a couple of ways it could work:

  • Require the format string to be a compile-time constant. The above call to unpack_from could then return Tuple<String, Int, Int, Int, Int, Int>

  • Allow fully general format strings, but return List<Object> and require the programmer to cast the Objects to the correct type:

      assert (signature as String) == 'BM' and (header_size as Int) == 40
      print(f'Dimensions: {width as Int}x{abs(height as Int)}')

Is it possible for a statically-typed language to support a function like struct.unpack_from? The ones I'm familiar with require much more verbose code (e.g. defining a dataclass for the header layout). Or is there a reason that it's not possible?

🌐
Snarky
snarky.ca › proposing-a-struct-syntax
Proposing a struct syntax for Python
June 8, 2023 - asdict(), to easily get the data out in another data structure (much like dataclasses.asdict()) replace(), to make it easy to construct new instances with minor changes without having to call asdict() on the original instance, updating the dict however you need to, and then construcitng a new instance; this method is much like dataclasses.replace() The equivalent Python code for our Point example would be: from typing import Any, Self, TypedDict, Unpack class _AsDictResult(TypedDict): """Return type of Point.asdict().""" x: int y: int class _ReplaceParams(TypedDict, total=False): """For typing Point.replace().""" x: int y: int class Point: __slots__ = ("x", "y") __match_args__ = ("x", "y") def __new__(cls, x: int, y: int) -> Self: """Create a new, immutable instance.""" # Pretend this makes everything immutable in the end.
🌐
Medium
medium.com › swlh › structures-in-python-ed199411b3e1
Structures in Python. In Python, there is a way where you can… | by Akshay Chavan | The Startup | Medium
September 14, 2020 - In Python, there is a way where you can define a collection that mimics properties similar to a structure in C++.
Find elsewhere
🌐
Kitchin Research Group
kitchingroup.cheme.cmu.edu › blog › 2013 › 02 › 27 › Some-basic-data-structures-in-python
Some basic data structures in python
February 27, 2013 - Python does not exactly have the same thing as a struct in Matlab. You can achieve something like it by defining an empty class and then defining attributes of the class.
🌐
GitHub
github.com › mrdor44 › stru
GitHub - mrdor44/stru: An IDE friendly alternative to Python's struct and construct · GitHub
Stru is a flexible and declarative binary structured data library, much like Python's struct module.
Author: mrdor44
🌐
Medium
kalami.medium.com › ypstruct-matlab-like-structure-data-type-for-python-5c60a3c04049
ypstruct: MATLAB-like Structure Data Type for Python | by Mostapha Kalami Heris | Medium
October 19, 2020 - In this post, the ypstruct package is introduced and discussed, which is a solution to have MATLAB-like structures in Python.
🌐
Real Python
realpython.com › python-data-structures
Common Python Data Structures (Guide) – Real Python
August 26, 2020 - Oddly enough, it’s also a recursive data structure—each character in a string is itself a str object of length 1. String objects are space efficient because they’re tightly packed and they specialize in a single data type. If you’re storing Unicode text, then you should use a string. Because strings are immutable in Python, modifying a string requires creating a modified copy. The closest equivalent to a mutable string is storing individual characters inside a list:
🌐
GeeksforGeeks
geeksforgeeks.org › struct-module-python
struct module in Python - GeeksforGeeks
May 10, 2025 - The struct module in Python allows you to work with binary data by providing functionality to convert between Python values and C-style binary data. This is particularly useful when dealing with binary file formats or network protocols.
🌐
GitHub
github.com › andreax79 › python-cstruct
GitHub - andreax79/python-cstruct: C-style structs for Python · GitHub
Convert C struct/union definitions into Python classes with methods for serializing/deserializing.
Author: andreax79
🌐
Scaler
scaler.com › home › topics › struct in python
Struct in Python| Scaler Topics
December 12, 2022 - The module of struct in Python refers to the module used for conversion among C structs and Python values. The struct in Python uses format string for conversion of C structs to Python values and vice versa.
🌐
DEV Community
dev.to › totally_chase › porting-c-structs-into-python-because-speed-2aio
Porting C structs into Python, because speed! - DEV Community
September 1, 2021 - First we have to make sure Python knows the structure/arrangement of the C struct we are porting, to do that, we make a Python class and pass the Structure parameter for ctypes into it. Then we have to declare the __fields__. Since our C struct had 3 int values we will refer to these with c_int in python.