Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can we pass a C structure to Python?

Tags:

python

c

cpython

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!

like image 608
alwinlin Avatar asked Feb 01 '26 02:02

alwinlin


1 Answers

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.

like image 81
Ignacio Vazquez-Abrams Avatar answered Feb 02 '26 16:02

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!