Search code examples
pythonc++boosttypesequivalence

What is the equivalence of python "type(<name>, <bases>, <dict>)" in c++?


Alright so, I'm embedding python 3.3 into a c++ application. I'm looking to dynamically create a python class on the c++ side exactly like if I was doing the following in python:

my_type = type("MyType", (object,), dict())

I know I could always import the "builtins" module but I'm trying to avoid imports on the c++ side in general.

Thanks!


Solution

  • The following seems to work just fine:

    PyObject *type(const char *name, boost::python::tuple bases, boost::python::dict dict) {
        return PyType_Type.tp_new(&PyType_Type,
            Py_BuildValue("sOO", name, bases.ptr(), dict.ptr()), NULL);
    }
    

    Thanks to Zack for pointing me in the right direction!