Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8943589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:48:45+00:00 2026-06-15T11:48:45+00:00

So I’m working on a little project in which I’m using Python as an

  • 0

So I’m working on a little project in which I’m using Python as an embedded scripting engine. So far I’ve not had much trouble with it using boost.python, but there’s something I’d like to do with it if it’s possible.

Basically, Python can be used to extend my C++ classes by adding functions and even data values to the class. I’d like to be able to have these persist in the C++ side, so one python function can add data members to a class, and then later the same instance passed to a different function will still have them. The goal here being to write a generic core engine in C++, and let users extend it in Python in any way they need without ever having to touch the C++.

So what I thought would work was that I would store a boost::python::object in the C++ class as a value self, and when calling the python from the C++, I’d send that python object through boost::python::ptr(), so that modifications on the python side would persist back to the C++ class. Unfortunately when I try this, I get the following error:

TypeError: No to_python (by-value) converter found for C++ type: boost::python::api::object

Is there any way of passing an object directly to a python function like that, or any other way I can go about this to achieve my desired result?

Thanks in advance for any help. 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T11:48:47+00:00Added an answer on June 15, 2026 at 11:48 am

    Got this fantastic solution from the c++sig mailing list.

    Implement a std::map<std::string, boost::python::object> in the C++ class, then overload __getattr__() and __setattr__() to read from and write to that std::map. Then just send it to the python with boost::python::ptr() as usual, no need to keep an object around on the C++ side or send one to the python. It works perfectly.

    Edit: I also found I had to override the __setattr__() function in a special way as it was breaking things I added with add_property(). Those things worked fine when getting them, since python checks a class’s attributes before calling __getattr__(), but there’s no such check with __setattr__(). It just calls it directly. So I had to make some changes to turn this into a full solution. Here’s the full implementation of the solution:

    First create a global variable:

    boost::python::object PyMyModule_global;
    

    Create a class as follows (with whatever other information you want to add to it):

    class MyClass
    {
    public:
       //Python checks the class attributes before it calls __getattr__ so we don't have to do anything special here.
       boost::python::object Py_GetAttr(std::string str)
       {
          if(dict.find(str) == dict.end())
          {
             PyErr_SetString(PyExc_AttributeError, JFormat::format("MyClass instance has no attribute '{0}'", str).c_str());
             throw boost::python::error_already_set();
          }
          return dict[str];
       }
    
       //However, with __setattr__, python doesn't do anything with the class attributes first, it just calls __setattr__.
       //Which means anything that's been defined as a class attribute won't be modified here - including things set with
       //add_property(), def_readwrite(), etc.
       void Py_SetAttr(std::string str, boost::python::object val)
       {
          try
          {
             //First we check to see if the class has an attribute by this name.
             boost::python::object obj = PyMyModule_global["MyClass"].attr(str.c_str());
             //If so, we call the old cached __setattr__ function.
             PyMyModule_global["MyClass"].attr("__setattr_old__")(ptr(this), str, val);
          }
          catch(boost::python::error_already_set &e)
          {
             //If it threw an exception, that means that there is no such attribute.
             //Put it on the persistent dict.
             PyErr_Clear();
             dict[str] = val;
          }
       }
    private:
       std::map<std::string, boost::python::object> dict;
    };
    

    Then define the python module as follows, adding whatever other defs and properties you want:

    BOOST_PYTHON_MODULE(MyModule)
    {
       boost::python::class_<MyClass>("MyClass", boost::python::no_init)
          .def("__getattr__", &MyClass::Py_GetAttr)
          .def("__setattr_new__", &MyClass::Py_SetAttr);
    }
    

    Then initialize python:

    void PyInit()
    {
       //Initialize module
       PyImport_AppendInittab( "MyModule", &initMyModule );
       //Initialize Python
       Py_Initialize();
    
       //Grab __main__ and its globals
       boost::python::object main = boost::python::import("__main__");
       boost::python::object global = main.attr("__dict__");
    
       //Import the module and grab its globals
       boost::python::object PyMyModule = boost::python::import("MyModule");
       global["MyModule"] = PyMyModule;
       PyMyModule_global = PyMyModule.attr("__dict__");
    
       //Overload MyClass's setattr, so that it will work with already defined attributes while persisting new ones
       PyMyModule_global["MyClass"].attr("__setattr_old__") = PyMyModule_global["MyClass"].attr("__setattr__");
       PyMyModule_global["MyClass"].attr("__setattr__") = PyMyModule_global["MyClass"].attr("__setattr_new__");
    }
    

    Once you’ve done all of this, you’ll be able to persist changes to the instance made in python over to the C++. Anything that’s defined in C++ as an attribute will be handled properly, and anything that’s not will be appended to dict instead of the class’s __dict__.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.