[cctbxbb] controlling types over the C++/Python interface

David Waterman dgwaterman at gmail.com
Mon May 14 03:32:34 PDT 2012


Hi,

I'm starting to explore the use of boost.python in cctbx. I've written a
simple C++ class and exposed its accessor methods as attributes using the
.add_property syntax in the wrapper. In the C++ class my member data and
accessor return types are, for example, scitbx::vec3<double>. When I access
the attribute in Python with the usual dot operator I get a tuple. What I'd
really like though is an object of type <class 'scitbx.matrix.col'>, which
is what I passed to the constructor of the object in Python. So the
conversion from Python to C++ does what I want, but not the other way
round. How do I take control of the type of the attribute in Python,
without having to explicitly construct a scitbx.matrix.col from the tuple?
It's the same case if I use .def and expose the accessor as a method
instead of an attribute.

I admit I don't fully understand the details of conversions in either
direction. I found I had to add

from cctbx.array_family import flex

to the package __init__.py where my C++ extension is imported, so that I
could pass scitbx.matrix.col objects (or scitbx.matrix.rec, or tuples)
to the constructor in Python and not get a Boost.Python.ArgumentError. I've
not yet figured out what this import is doing though.

Apologies for the remedial question. These are still early days for me.

Thanks

-- David
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://phenix-online.org/pipermail/cctbxbb/attachments/20120514/d6138573/attachment.htm>


More information about the cctbxbb mailing list