4

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!

John Smith
  • 187
  • 1
  • 9
  • 1
    What do you mean. What is it supposed to do? – sshashank124 May 02 '14 at 01:11
  • 1
    This is not an answer because (a) I don't know if it will help and (b) the question has unhelpfully been closed, but investigate [`PyType_Type`](https://docs.python.org/3/c-api/type.html#c.PyType_Type). – zwol May 02 '14 at 01:36
  • Thanks Zack! The following did the trick: `PyType_Type.tp_new(&PyType_Type, Py_BuildValue("s(){}", "NAME"), NULL);` – John Smith May 02 '14 at 02:16

1 Answers1

1

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!

Delimitry
  • 2,987
  • 4
  • 30
  • 39
John Smith
  • 187
  • 1
  • 9