On 01/21/2014 11:03 AM, Gary Oberbrunner wrote: > > ----- Original Message ----- >> From: "Stefan Seefeld" <ste...@seefeld.name> >> To: "Development of Python/C++ integration" <cplusplus-sig@python.org> >> Sent: Tuesday, January 21, 2014 10:45:52 AM >> Subject: Re: [C++-sig] create a python object from scratch in boost.python? >> >> On 01/21/2014 10:37 AM, Gary Oberbrunner wrote: >>> So now it returns a python list of python dicts. All I want now is >>> to >>> override __getattr__ on each ppinfo dict so it returns the dict >>> value >>> as the attribute value, so in python I can reference >>> effect_list[i].name instead of effect_list[i]['name']. Or any >>> alternative way to get the same effect. Is that possible? >> There are many ways to do that elegantly in Python. For example: >> >> class Effect: >> def __init__(self, mydict): >> self.__dict__.update(mydict) > I like the idea of using self.__dict__. If that's accessible from C++, then > I suppose I could have an empty C++ class, export that as a simple container, > and dynamically add attributes to it by manipulating its __dict__. Does that > seem possible? >
Why do you want to export an empty C++ class ? Do you have an API that would take that as argument or return type ? If not, I see no point in doing that, as all that does is provide implicit type conversion for you. It seems what you really want is a way for your C++ code to manipulate pure Python objects, without any automatic type conversion. So the cleanest way to do that would be to use the above definition of the "Effect" class (in Python), import that into your C++ runtime (via bpl::import()), and then instantiate an "Effect" object with your pre-defined dict object. bpl::object effect_type = bpl::import("Effect"); // Define Effect type in C++ runtime bpl::object effect = effect_type(ppinfo); // Instantiate Effect object with pre-defined attribute dictionary ... Stefan -- ...ich hab' noch einen Koffer in Berlin... _______________________________________________ Cplusplus-sig mailing list Cplusplus-sig@python.org https://mail.python.org/mailman/listinfo/cplusplus-sig