alwinlin
alwinlin

Reputation: 713

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!

Upvotes: 3

Views: 6681

Answers (3)

ghorn
ghorn

Reputation: 614

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.

Upvotes: -1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

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.

Upvotes: 2

Santa
Santa

Reputation: 11547

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).

Upvotes: 2

Related Questions