问题
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!
回答1:
The cleanest thing to do is probably to create a new type and implement tp_getattr
, either returning the appropriate member of the struct or raising an exception depending on the argument passed.
回答2:
This documentation on extending and/or embedding Python might get you started.
There is also the ctypes library to dynamically load native DLLs and passing C-like structures in and out of them (completely from within Python).
回答3:
This is probably not what you're looking for, but we use LCM to share c structs between c/c++, python, java, and matlab programs over udp with multicast. It's very handy for doing robotics.
来源:https://stackoverflow.com/questions/5734016/how-can-we-pass-a-c-structure-to-python