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

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Wed Jul 16 00:57:20 CEST 2008

I'd rewrite foo (untested, rough):

void foo( boost::python::object& obj )
   obj.attr( "field" ) = 1;
  A& a = boost::python::extract<A&>(obj);

I'm not sure if the extract call is correct, but I hope it is useful as a start.

----- Original Message ----
From: Gennadiy Rozental <rogeeff at gmail.com>
To: c++-sig at python.org
Sent: Tuesday, July 15, 2008 3:39:33 PM
Subject: [C++-sig] set python attribute from C++


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):

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,


C++-sig mailing list
C++-sig at python.org

More information about the Cplusplus-sig mailing list