How-to create a Pascal Record using Python

Alex Martelli aleaxit at yahoo.com
Thu Jan 4 17:24:13 EST 2001


"Daniel" <Daniel.Kinnaer at AdValvas.be> wrote in message
news:3a54e0c0.21998314 at news.skynet.be...
    [snip]
> Suppose I have something like
>
> TMyRecord = record
>      aName : string;
>      adate   :  TDateTime;
>     aZIP     : integer;
>   end; //record
>
> var MyRecs : array [1..100] of TMyRecord;
>       f             : file of TMyRecord;
>
> How is this handled in Python?  Sorry for these questions, but I
> really would like to learn about all of this.

Such fixed-structures are rarely used in Python.  When there is
a need, the struct module may be employed.  The 'p' format of
struct is exactly the one used to encode a Pascal string (1-byte
length, then up to 255 characters -- or, a different length if
explicitly specified).  'integer', depending on whether you mean
2 or 4 bytes, is either format 'h' (for 'half') or 'i'.  A 'TDateTime'
is not an elementary type and thus has no direct struct
format character.

As you can see, these are rather low-level constructs from
Python's viewpoint!  They will be needed if one has to handle
a file produced by some other program.  Else, one would
normally define a class to hold the data attributes, and use
the pickle (or cPickle) module to save to disk and reload any
number of such objects.


Alex








More information about the Python-list mailing list