In two previous posts we showed how to call C functions from other modules and Python methods from within a Boost.Python module. Here we’ll show that the techniques are easily combined and demonstrate the atexit module which we’ll use to prevent Python interpreter crashes upon exit.
In a previous post we already covered calling a C function from one Boost.Python module in another module.
What if instead we want to call a Python function directly from C? We’ll stick with the same Bird / Quacker metaphor as in the previous post, but we’ll now implement our duck class in PyDuck.py:
""" PyDuck.py """ def quack(): print "Python Quack"
Now how can we call Python from C++? It turns out that Boost.Python defines operator() for the object class, so calling Python is relatively easy. But things become more difficult if we want to maintain a default implementation in C.