Struct class random access
castironpi at gmail.com
Mon Aug 25 23:49:14 CEST 2008
On Aug 25, 4:25 pm, Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> On Mon, 25 Aug 2008 13:03:09 -0700, castironpi wrote:
> > struct.Struct lets you encode Python objects into structured memory. It
> > accepts a format string, and optionally a buffer and offset to/from
> > which to read/write the structure. What do you think of random access
> > for the results?
> > (unproduced)
> >>>> packer= struct.Struct( 'IIIf255p' )
> >>>> packer.pack_into( buf, off, 10, 20, 30, 0.5, 'abc' )
> >>>> packer.unpack_from( buf, off, 2 ) #reads field 2
> > 30
> I don't like it for the same reason I don't like index access on tuples
> or lists that represent a "record" -- the numbers are quite meaningless.
> Names for the components result in much easier to understand source code,
> so I would prefer to use `ctypes` or `construct` to create such a record.
> Marc 'BlackJack' Rintsch
I'm interested in the speed benefit, so you don't have to reconstruct
the entire 'record' just to read/write one 'field'. How in ctypes?
More information about the Python-list