[C++-sig] set python attribute from C++

Gennadiy Rozental rogeeff at gmail.com
Wed Jul 16 00:39:33 CEST 2008


Hi,

Trivial question (I hope)

Let's say I have class A, which I export;

class A { ... };

class_<A,...>( "A" );

In python I wrote another class inherited from B:

class B(A):
   pass

Now I've got the function in C++:

void foo( A& obj )
{
   // ?? question is what to do here
}

which is exported 

def( "foo", &foo );

and is called from python liek this:

o = B()

foo( o )


The question:

How can I set object attribute inside the function foo implementation. More
generally: how can I get access ot the underlying python object?

essentially I want something like this:

void foo( A& obj )
{
   boost::python::object o = get_py_object( obj );
   o.attr( "field" ) = 1;
}


Please advide,

Gennadiy








More information about the Cplusplus-sig mailing list