Create a instance of Python Class in C++?

David Abrahams david.abrahams at rcn.com
Thu Aug 22 23:06:29 EDT 2002


"Gerhard Häring" <gerhard.haering at gmx.de> wrote in message
news:slrnama98b.i6.gerhard.haering at lilith.my-fqdn.de...
> Arivazhagan wrote in comp.lang.python:
> > Hi
> >
> > Do any one know how to use a class defined in python in C++? Is it
> > possible?
>
> Call it with PyObject_CallObject. Get access to the class using
> PyObject_GetAttr, once you have its module object.


Or, in Boost.Python v2,

    double use(object some_class)
    {
        // Make an instance of some_class
        object inst = some_class(3, "hello");

        // set an attribute
        inst.attr("x") = 2;

        // call a method: inst.method_name(42)
        object result = instance.attr("method_name")(42);

        // Get the C++ value out
        return extract<double>(result);
    }

HTH,
Dave

-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave at boost-consulting.com * http://www.boost-consulting.com





More information about the Python-list mailing list