Help embedding python

David Abrahams dave at boost-consulting.com
Sat Aug 30 16:51:49 EDT 2003


Zora Honey <zhoney at wildmail.com> writes:

> I'm sorry if I was a bit curt.  The reason I didn't include rationale
> is because I have none.  I have been advocating extending python
> instead of embedding it, but since I have no experience with either,
> my only argument is that it seems more natural.  It seems like either
> method is as difficult to learn and implement.  It doesn't seem to me
> that there are lot of arguments one way or the other.  My husband, on
> the other hand, is strongly attached to the idea of having a c++ main
> program that will coordinate all the data-processing classes and
> python widgets. He believes it is easier to have c++ say to python,
> "here, I have these plots for you," rather than have python say to
> c++, "make these plots for me."  I don't believe this is true, but I
> don't have any reason *not* to believe it's true, so I aquiesced.
>
> So, I suppose instead of asking you to assume the rationale are well
> reasoned, I should be asking you why you believe extending python is
> easier/cheaper/faster/better than embedding it.

Here's why you're right and your husband is, well... the opposite
<wink>: embedding Python almost always includes all the problems of
extending it, and a little bit more.  In applications where Python is
embedded in C++, it's usually insufficient to leave the Python code
with access to nothing other than what comes with the Python
interpreter: instead you want to give it access to some C++ types and
functions.  Exposing those to Python is exactly the same job you do
when extending, only when extending you don't need to worry about
linking Python into your application (on most platforms), handling and
reporting Python exceptions from C++ which may emanate from the
interpreter, getting ahold of a module context in which to evaluate
Python code, etc.  Another way to think of it is that when you need
to embed, since you also usually need to extend, you need to manage
the transition across the C++ <-> Python language boundary in two
places instead of one.

On the other hand, embedding isn't *much* more difficult, so if it
keeps things peaceful at home, I'd just go along with it ;-)

Oh, and whichever you choose, I *strongly* recommend you don't do it
by hand with Python 'C' API.  There are too many ways to mess up
reference counting, exception-handling, etc., and way too much
boilerplate code repetition for it to be a very productive approach.
Besides, since you're using C++ anyway, it gives you all the power you
need to embed or extend simply and elegantly.  For more info, see
http://www.boost.org/libs/python.

HTH,
-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com




More information about the Python-list mailing list