Newbie:python equiv of c structures

Thomas Wouters thomas at xs4all.nl
Fri Oct 29 04:47:56 EDT 1999


On Fri, Oct 29, 1999 at 04:31:10AM +0000, tom mcdavid wrote:

>     I am learning Python from a background of mostly FORTRAN and some
> c.  I know how use structures in c, what is the analogue in Python?
> I've got Lutz' book, but it doesn't speak to my
> old-dog-learning-new-tricks background.

Objects, really. C structs group data together, objects group data &
functions together. But if you insist on only having data, you can easily
create an object with only data, it's all in the eye of the beholder. Python
is however very different from FORTRAN and C, not in the small
technicalities or loop statements or even syntax, but in how you need to
think to program effectively. It'll take some experimenting before you're
comfortable with your new tricks.

For instance, if you want a struct so you can pass a ton of data to a
function by value instead of by reference, you're out of luck. all Python
objects, all arguments, everything is passed and called by reference. You
need to explicitly copy to avoid it. On the plus side, you dont need to
group something in a struct to pass it along, you can very easily pass any
kind of data in any form.

If you're looking for a 'struct' to be able to pass it to a C function that
expects it, you need the 'struct' module. You use it to create a 'binary
string' according to a specification. Not as easy to use as C structs, but
it generates a block of data that can be read in as a C struct.

And if you're looking for a 'struct' to group related data + flags + function
pointers, you want Objects. You want objects anyway, but you _really_ want
them now :)

The tutorial on http://www.python.org/doc is a good place to start learning
the new tricks, by the way.

All-of-python-is-structured'ly y'rs,

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list