首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将list作为参数传递给Python C模块?

将list作为参数传递给Python C模块?
EN

Stack Overflow用户
提问于 2010-07-15 15:59:12
回答 2查看 7.3K关注 0票数 7

我发现this是Python C模块的一个很好的例子,它只传递一个整数作为唯一的参数。我如何才能将python列表作为参数传递?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2010-07-15 16:06:46

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

代码语言:javascript
运行
复制
...
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 */

..。

代码语言:javascript
运行
复制
/* 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 */

请参阅Parsing arguments and building values

票数 16
EN

Stack Overflow用户

发布于 2010-07-15 16:09:46

使用O argumentssequence protocol中的一个。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3253563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档