Take the 2-minute tour ×

How do I use a C struct foo, defined in a header file as a datatype in my Python code?

(This document does not seem to address the issue.)

typedef struct {


    /* Type-specific fields go here. */

     struct api_query query; /* instead of PyObject * type here */

} api_Request;
share|improve this question
What is your objective? What do you intend to do with the C structure? Pass to some shared library function calls, access it's contents? –  isedev Jan 31 '13 at 9:33
Pass the structs ( request )and get a response (another struct) to a socket, so that the server may give the required result –  qwrty Jan 31 '13 at 9:38
The document you link is indeed the right source. Read it carefully; it does answer your question. –  Martin v. Löwis Jan 31 '13 at 11:01
add comment

1 Answer

Building an extension module is not a trivial task (the document you linked to does explain how to do it in detail). To wrap a C structure that way, you need to define the new type and a Python's object usual methods (constructor, destructor, access methods, etc...).

You might find the ctypes package is an easier way to go about it.

Better still, if all you need to do is create the structure and send it (to a socket, as you say), and if the structure is simple enough, then the struct.pack function might prove easiest.

share|improve this answer
add comment

Your Answer


