Список передачи как аргумент модулю Python C?

Я нашел этот хороший пример Модуля Python C, где единственное целое число проведено как единственный аргумент. Как я могу вместо этого передать список Python как аргумент?

7
задан c00kiemonster 15 July 2010 в 07:59
поделиться

2 ответа

From http://code.activestate.com/lists/python-list/31841/:

...
char * tok;         /* delimiter tokens for strtok */
int cols;           /* number of cols to parse, from the left */

int numLines;       /* how many lines we passed for parsing */
char * line;        /* pointer to the line as a string */
char * token;       /* token parsed by strtok */

PyObject * listObj; /* the list of strings */
PyObject * strObj;  /* one string in the list */

/* the O! parses for a Python object (listObj) checked
   to be of type PyList_Type */
if (! PyArg_ParseTuple( args, "O!is", &PyList_Type, &listObj, 
           &cols, &tok )) return NULL;

/* get the number of lines passed to us */
numLines = PyList_Size(listObj);

/* should raise an error here. */
if (numLines < 0)   return NULL; /* Not a list */

...

/* iterate over items of the list, grabbing strings, and parsing
   for numbers */
for (i=0; i<numLines; i++){

/* grab the string object from the next element of the list */
strObj = PyList_GetItem(listObj, i); /* Can't fail */

/* make it a string */
line = PyString_AsString( strObj );

/* now do the parsing */

См. Разбор аргументов и построение значений

15
ответ дан 6 December 2019 в 14:00
поделиться

Используйте один из аргументов O и протокола последовательности .

0
ответ дан 6 December 2019 в 14:00
поделиться
Другие вопросы по тегам:

Похожие вопросы: