[C++-sig] Re: constructing Python objects in C++

David Abrahams dave at boost-consulting.com
Wed Jul 7 23:27:52 CEST 2004


Andrew Straw <strawman at astraw.com> writes:

> (Thanks much for providing guidance on previous questions. On with a
> simple? newbie question.)
>
> The tutorial (section "Derived Object types") nicely explains that
> class_<T> lets me make a Python object out of a C++ class. 
>
> However, if I've already defined class_<T> once in my
> BOOST_PYTHON_MODULE block, how can I create a Python object of this
> wrapped class elsewhere in the code without the class_<T> block? 

Just store the class_ in an object:

     object my_py_class;

     BOOST_PYTHON_MODULE("Foo")
     {
        my_py_class
           = class_<my_cpp_class>("my_py_class")
                 .def(...
                 ;
     }

     void f()
     {
        object my_py_instance = my_py_class();
     }

> (I feel that maybe I'm missing something pretty fundamental...)

[Replicating the class_<> declaration won't work].

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




More information about the Cplusplus-sig mailing list