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
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:
- How to Convert a C++ Structure into C# Structure?Best solution by Stack Overflow
- How can I pass global variables into a function?Best solution by Stack Overflow
- how can I write this shell script in python?Best solution by Stack Overflow
- How can I burn a .rmvb file onto a DVD for viewing on a regular DVD player?Best solution by Yahoo! Answers
- How can I get a job at a resort for a Summer?Best solution by eHow old
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.