• Advertisement

Archived

This topic is now archived and is closed to further replies.

Python - calling C++ function

This topic is 5058 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, I have my second big python/c question: How can I call C func from Python? AND how can I use C++ class in python code? (if it is possible, i hope it is...)

Share this post


Link to post
Share on other sites
Advertisement
How can I call C func from Python?

You expose it as an extension.

AND how can I use C++ class in python code?

The easy ways are to use either boost::python or SWIG.
The hard way is to manually define an extension type.


“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.”
— Brian W. Kernighan

Share this post


Link to post
Share on other sites
Wow. I guess you really do learn a new thing every day. I didn't know you could call C++ stuff in Python.. that's pretty cool. Hmm, maybe python deserves a second glace ;)


- CD

Share this post


Link to post
Share on other sites
I use Swig in my project and it works great, however it does have one serious flaw IMHO. When wrapping a C++ class in Python code (or any other language IIRC) then it creates a series of C-style functions and creates a Python class to wrap them.

This isn't a very nice way of doing it, and is a bit of a performance hit. I guess it stems from the olden days when you couldn't derive from a C type, but now that restriction has gone that is unneccessary.

Also Swig ignores namespaces, but that's not huge problem.

HTH,
Andrew

Share this post


Link to post
Share on other sites

  • Advertisement