get class instance from C++ extension?

Martin von Loewis loewis at informatik.hu-berlin.de
Sat Jun 16 14:26:38 EDT 2001


"Toshiya Kawakami" <kawakami at lead.dion.ne.jp> writes:

> ### this is ordinary way to get instance of class c1 as x.
> x = c1()
> ### I want to call C++ extension and get calss c1 instance to y
> y = myext.meth1( 5, 6, 7.2 )
> ### and want to print attributes of the got y.
[...]
>   return Py_BuildValue( "i", 0 );   // <--- this is not what i want to do

First of all, you should stop using Py_BuildValue in cases like this;
this example would better use PyInt_FromLong.

In general, you do Python things in C the same way as in Python. To
create an instance of c1, you need to *call* c1. So your code should
read

c1 = ... // lookup c1
x = PyObject_CallFunction(c1, "()");

Regards,
Martin



More information about the Python-list mailing list