Byte oriented data types in python

Grant Edwards grante at visi.com
Sun Jan 25 19:04:59 EST 2009


On 2009-01-25, Martin v. Löwis <martin at v.loewis.de> wrote:
>> It deals with variable sized fields just fine:
>> 
>> dtype = 18
>> dlength = 32
>> format = "!BB%ds" % dlength
>> 
>> rawdata = struct.pack(format, (dtype,dlength,data))
>
> I wouldn't call this "just fine", though - it involves
> a % operator to even compute the format string. IMO,
> it is *much* better not to use the struct module for this
> kind of problem, and instead rely on regular string
> concatenation.

If all you need to do is concatenate strings, then you're
correct, there's no advantage to using struct or ctypes.

If you need a generic way to deal with arbitrary data types,
then that's what the struct and ctypes modules are designed to
do.  The protocols I've implemented always required the ability
to deal with integers greater than 8 bits wide as well as
various other data types.

-- 
Grant




More information about the Python-list mailing list