, 16 - . , short
, ( , ), long
.
( ), ( struct.calcsize("=l")
4 , struct.calcsize("=hl")
6 , 10, 8 long
s).
, , ctypes
, ctypes.Structure
_pack_
, ctypes.sizeof
, , :
from ctypes import Structure, c_long, c_short, sizeof
class HL(Structure):
_pack_ = 1
_fields_ = [("", c_short),
("", c_long)]
print(sizeof(HL))
10
.
, ( , struct
, ):
from ctypes import *
FMT_TO_TYPE = dict(zip("cb?hHiIlLqQnNfd",
(c_char, c_byte, c_bool, c_short, c_ushort, c_int, c_uint,
c_long, c_ulong, c_longlong, c_ulonglong,
c_ssize_t, c_size_t, c_float, c_double)))
def calcsize(fmt, pack=None):
'''Compute size of a format string with arbitrary padding (defaults to native)'''
class _(Structure):
if packis not None:
_pack_ = pack
_fields_ = [("", FMT_TO_TYPE[c]) for c in fmt]
return sizeof(_)
, , , :
>>> calcsize("hl")
16
>>> calcsize("hl", 1)
10