🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Python - calling C++ function

Started by
3 comments, last by andrew_j_w 20 years ago
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...)
When I was younger, I used to solve problems with my AK-47. Times have changed. I must use something much more effective, killing, percise, pernicious, efficient, lethal and operative. C++ is my choice.
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
"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
SWIG will probably work fine.
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
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

This topic is closed to new replies.

Advertisement