Martin Panter added the comment:

For the native alignment case (prefix code @), perhaps you can already use the 
“ctypes” module, which supports structures with more complicated embedded 

For the the unaligned modes (prefixes =, <, > and !), I am a little 
sympathetic. In the past, I wanted to make various structures that extended 
from a common base structure:

HSF_VOL_DESC = Struct("< B 5s B")

# Python 3's "Struct.format" is a byte string!
NSR_DESC = Struct(HSF_VOL_DESC.format.decode() + "B")

But I think if Issue 21071 was fixed (change Struct.format to text string, or 
perhaps add a new attribute), I would be happy enough writing

NSR_DESC = Struct(HSF_VOL_DESC.format_str + "B")

Transforming Aviv’s examples:

s3 = Struct(s1.format_str + s2.format_str[1:])
s3 = Struct(s1.format_str + "B")  # if s2 is not needed on its own
ver2 = Struct(ver1.format_str + "I")
version_a = Struct(start.format_str + union_b.format_str[1:])

nosy: +martin.panter

Python tracker <>
Python-bugs-list mailing list

Reply via email to