[C++-sig] Need strategic advice designing Boost/Python program

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Tue May 1 02:16:50 CEST 2007


> Sorry, can you elaborate on that a bit ? What is special in the code
> above ? It looks like a 'normal' definition of a derived python class
> ('_histogram') that derives from a (presumably) C++ class (ext.histogram).

If you just derive without the boost.python.injector, the C++ class ext.histogram
doesn't know about the .show() method, only _histogram does.

Maybe I should have shown the bare-bones approach first. Here it is again:



  def histogram_show(self, f=None, prefix="", format_cutoffs="%.8g"):
    # exact same code

  ext.histogram.show = histogram_show

Why is this cool?

If you have a C++ function returning a new histogram object, you want that
to have the .show() method:

C++:
  histogram make_histogram();

  def("make_histogram", make_histogram);

Python:

  h = make_histogram()
  h.show()

It doesn't help if you have the plain inheritance _histogram, since h above is just an ext.histogram.
To get the same result without injecting the .show() method, you'd have to def a Python wrapper
function, something like:

  def make_histogram():
      h = ext.make_histogram()
      return _histogram(h)

And that for every C++ function returning a histogram object.

Ralf




__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070430/59e57fe4/attachment.htm>


More information about the Cplusplus-sig mailing list