tuple creation in C extensions

Jeremy Hylton jeremy at beopen.com
Tue Jun 13 18:10:02 EDT 2000


Just van Rossum <just at letterror.com> writes:

> At 11:15 PM +0200 10-06-2000, Peter Schneider-Kamp wrote:
> >t = PyTuple_New(2);
> >PyTuple_SET_ITEM(t,0,PyInt_FromLong(a));
> >PyTuple_SET_ITEM(t,1,PyInt_FromLong(b));
> >PyErr_SetObject(PyExc_IndexError,t);
> >
> >Is there "A Better Way(TM)"?
> 
> t = Py_BuildValue("(ii)", a, b);
> 
> Just

This is a shorter-to-write, slower-to-run version.  If you use
Py_BuildValue, the code has to parse the format string in order to
build the object.  If you are coding in C because you're trying to
speed up a speed-critical part of your program, the code you posted is
the better way.

Jeremy



More information about the Python-list mailing list