Instantiating Python class from C

Richard Jones richard at bizarsoftware.com.au
Mon Dec 10 18:21:29 EST 2001


On Tuesday 11 December 2001 9:40 am, Arno Puder wrote:
> Hi,
>
> assume I have a Python class, defined through the interpreter. How do I
> instantiate a new object of this class in C within an extension of the
> Python interpreter. E.g.,
>
> # Python
> class A:
>    #...
>
> // C
> PyObject* new_A_object = ???

Get a handle to the class and then call it, just like in Python code. The 
functions you want are:

Loading the module that defines the class:
  http://www.python.org/doc/current/api/importing.html
    PyImport_ImportModule

Getting the class and calling it:
  http://www.python.org/doc/current/api/object.html
    PyObject_GetAttrString
    PyObject_CallObject


      Richard




More information about the Python-list mailing list