[C++-sig] Re: How to turn a C++-Class-instance into a boost::python::object?

David Abrahams dave at boost-consulting.com
Wed Sep 22 23:12:45 CEST 2004


Dennis Brakhane <dennis at fsmath.zbt.uni-heidelberg.de> writes:

> Hi.
>
> What I want to do is the following:
> I have a class "Foo", already wrapped and exposed to Python.
> Now I have a instance "foo" in C++, and want to add this instance into
> Python's namespace. I could define a function get_foo:
>
> Foo& get_foo() { return foo; }
>
> and expose this to Python and write python code like "foo = get_foo",
> but I want to do it directly in C++.
>
> I tried the following:
>
> { Foo foo;
>     using namespace boost::python;
>    // get main_namespace of Python interpreter etc.
>    main_namespace["A"] = object(0);
>    main_namespace["B"] = object(foo);
> }
> the first assignment to A works as expected, the "object(foo)" parts
> throws a already_set exception (which mean that it cannot convert it,
> AFAIK)
>
> So, my question is: what is the correct way to do this? Or isn't it
> possible at all.

There are lots of ways.

http://tinyurl.com/5p266

should answer your questions, I hope.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com




More information about the Cplusplus-sig mailing list