Extending Python - variable sized arrays?

Robert Amesz reqhye72zux at mailexpire.com
Tue Oct 16 21:27:15 EDT 2001


Tim Egbert wrote:


>    ret = Py_BuildValue("iiiiii",
>       arr[0],arr[1],arr[2],arr[3],arr[4],arr[5]);
> 
> I can't go the Py_BuildValue() route because the problems is, the
> next time around, there may be 17 or 127 elements in the array, the
> exact number only to be known during runtime.
> 
> Any thoughts on how to do this?

Py_BuildValue() is a convenience function, there is a more general way 
to build a tuple: look at section 7.2.4 of the Python manual (Python/C 
Api; Concrete Objects; Sequence Objects; Tuple Objects). It tells you 
all about tuples in C. I haven't tried it, but the PyTuple_New(N) 
function, followed by N calls to PyTuple_SetItem() should do the trick.

(Which just shows that tuples aren't as immutable as you might have 
thought, at least not from C. Use that privilege judiciously, though, 
it has all the power of a foot seeking missile.)


Robert Amesz



More information about the Python-list mailing list