[C++-sig] create a python object from scratch in boost.python?

Neal Becker ndbecker2 at gmail.com
Tue Jan 21 02:39:55 CET 2014


Gary Oberbrunner wrote:

> I'd like to create a python object with some attributes in C++, and return it
> to python.  Something like this:
> 
>     Plugin_Info *pinfo = get_plugin_info_i(i);
>     bp::object plugin;  // this creates None; how can I make an empty class?
>     plugin.attr("name") = std::string(pinfo->plugin_name);
>     plugin.attr("category") = std::string(pinfo->category);
>     plugin.attr("n_effects") = (int)pinfo->n_effects;
>     plugin.attr("n_inputs") = (int)pinfo->n_inputs;
>     plugin.attr("custom_p") = (int)pinfo->custom_p;
>     effect_list.append(plugin);
> 
> But this fails on the third line, because the "plugin" object is None, and
> None has no attributes.  Instead of None, I'd like to create a simple empty
> python class I can add attributes to.  How can I do that?  (Yes, I could use a
> dict instead but the python side would like to see attributes with values
> rather than a dict.)
> 

I think if you've exposed your c++ class, then there is a conversion from c++ 
object to python object.  So

bp::object plugin (Plugin_Info());

I believe would work.



More information about the Cplusplus-sig mailing list