简体   繁体   English

我们如何将C结构传递给Python?

[英]How can we pass a C structure to Python?

I'm a new in both C and Python. 我是C和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? 这些天,我正在学习在C中嵌入Python。当我从C调用Python函数时,我想知道,我们如何将C结构传递给Python?

thanks alot in advance! 非常感谢!

This documentation on extending and/or embedding Python might get you started. 有关扩展和/或嵌入Python的文档可能会帮助您入门。

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). 还有ctypes库可以动态加载本机DLL并将C类结构传入和传出(完全来自Python内部)。

最干净的事情可能是创建一个新类型并实现tp_getattr ,要么返回结构的相应成员,要么根据传递的参数引发异常。

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. 这可能不是你想要的,但我们使用LCM通过udp与多播共享c / c ++,python,java和matlab程序之间的c结构。 It's very handy for doing robotics. 这对于做机器人来说非常方便。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM