How can we pass a C structure to Python?

。_饼干妹妹 提交于 2019-12-22 10:43:24

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!