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

Stefan Seefeld seefeld at sympatico.ca
Tue May 1 01:00:59 CEST 2007


Ralf W. Grosse-Kunstleve wrote:
>> This, together with your point further down that you want to add python
>> methods that are only ever used inside python make it sound as if
>> a C++ base class derived from in python may be the best approach.
> 
> FWIW: there is a very powerful alternative to this: injecting.
> For example:
> 
> class _histogram(boost.python.injector, ext.histogram):
> 
>   def show(self, f=None, prefix="", format_cutoffs="%.8g"):
>     if (f is None): f = sys.stdout
>     fmt = "%s" + format_cutoffs + " - " + format_cutoffs + ": %d"
>     for info in self.slot_infos():
>       print >> f, fmt % (prefix, info.low_cutoff, info.high_cutoff, info.n)
> 
> The boost.python.injector is a small utility class defined
> in boost_adaptbx (adaptor toolbox), boost/python.py.
> 
> The difference is that objects of ext.histogram will have the
> .show() method, even if they are created in C++.
> I.e. even if you only have a C++ constructor, you can have pure
> Python methods.

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).
What does boost.python.injector do that wouldn't be available without it ?

(I don't understand the 'only C++ constructor' part, in fact)

Thanks,
		Stefan


-- 

      ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list