[Tutor] Question About Structs

Rick Pasotto rickp@telocity.com
Fri, 22 Feb 2002 12:40:00 -0500

On Fri, Feb 22, 2002 at 01:39:35PM -0300, Hector A. Paterno wrote:
> Hi, Im recently programming in python and I'v one question :
> Who is the best form in python to replace what in c is called struct ?
> For example in C I have this struct :
> struct monitor {
>  char *model;
>  int number;
> };
> My best was to replace this with a DIC :
> monitor = {'model':'', 'number':}
> This is correct ? There are a best way to do a struct like this in python ?

Depends on what you want to do/how you need to deal with the data.
Sometimes a list (or tuple) will do the job, sometimes a dictionary is
more appropriate. If you actually need to deal with "the first four
bytes are a long int and the next four bytes are two short ints and the
next twenty bytes are a character array (ie, string)" then you can use
the struct library.

Most of the time lists or dictionaries are better suited.

> Pd: Python doesn't has the case statment ?

The case statement functionality can be express with:

if <test1>:
elif <test2>:
elif <test3>:
	<default action>

Within the limits of equity, everything is to be accomplished
through the free and perfectible initiative of man; nothing is to
be achieved by law or by force save universal justice.
	-- Frédéric Bastiat (1801-1850)
    Rick Pasotto    rickp@telocity.com    http://www.niof.net