struct curiosity

Gabriel Genellina gagsl-py2 at
Fri Oct 16 16:59:24 CEST 2009

En Thu, 15 Oct 2009 15:07:04 -0300, pjcoup <pjcoup at> escribió:

>>>> import struct
>>>> struct.calcsize('BhhhhB')
> 11
>>>> struct.calcsize('@BhhhhB')
> 11
>>>> struct.calcsize('<BhhhhB')
> 10
>>>> struct.calcsize('>BhhhhB')
> 10
> I would have expected calcsize('BhhhhB') to be either 10 or 12
> (padding), but 11?

There are no pad bytes following the last member; from the docs:

"""Hint: to align the end of a structure to the alignment requirement of a  
particular type, end the format with the code for that type with a repeat  
count of zero. For example, the format 'llh0l' specifies two pad bytes at  
the end, assuming longs are aligned on 4-byte boundaries. This only works  
when native size and alignment are in effect; standard size and alignment  
does not enforce any alignment."""

py> calcsize("BhhhhB")
py> calcsize("BhhhhB0h")

Gabriel Genellina

More information about the Python-list mailing list