1

Python documentation gives a nice introduction to Extending and Embedding Python with C\C++. However, in many cases there's a need to transfer complex data structures between the languages.

What would be the best way to achieve this?

Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359

2 Answers2

2

Create your own type and fill the tp_*attr members appropriately.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • Do I really need to create a new type just to transfer a struct from C\C++ to Python and back? Isn't there a way to map it to a dictionary for instance? – Jonathan Livni May 10 '11 at 11:18
  • Also - If I do create a new type - would I need to convert the data between it and the C\C++ data structure in C\C++? – Jonathan Livni May 10 '11 at 11:20
  • You don't actually "transfer" anything between C/C++ and Python; a type instance holds the struct as a member, and the `tp_*attr` functions convert data back and forth. – Ignacio Vazquez-Abrams May 10 '11 at 11:35
1

IIRC Boost.Python http://www.boost.org/doc/libs/1_54_0/libs/python/doc/index.html will do a lot of the heavy lifting for you here.

Maxim Khesin
  • 597
  • 4
  • 10