How to pass a C structure in Python?

How can we pass a C structure to Python?

  • I'm a new in both C and Python. These days, I'm learning to embed Python in C. As I am calling Python functions from C, I'd like to know, how can we pass a C structure to Python? thanks alot in advance!

  • Answer:

    The cleanest thing to do is probably to http://docs.python.org/extending/newtypes.html and implement tp_getattr, either returning the appropriate member of the struct or raising an exception depending on the argument passed.

alwinlin at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

This http://docs.python.org/extending/ on extending and/or embedding Python might get you started. There is also the http://docs.python.org/library/ctypes library to dynamically load native DLLs and passing C-like structures in and out of them (completely from within Python).

Santa

This is probably not what you're looking for, but we use http://code.google.com/p/lcm/ to share c structs between c/c++, python, java, and matlab programs over udp with multicast. It's very handy for doing robotics.

ghorn

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.