PyArg_ParseTupleAndKeywords如何工作? [英] How does PyArg_ParseTupleAndKeywords work?

查看:186
本文介绍了PyArg_ParseTupleAndKeywords如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试学习如何为Python编写C扩展,并希望确保我了解PyArg_ParseTupleAndKeywords的工作方式.

I've been trying to learn how to write C-extensions for Python and want to be sure I understand how PyArg_ParseTupleAndKeywords works.

我相信第一个参数是PyObject指针,该指针指向按参数传递顺序传递给C-extension函数的参数数组.第二个参数是已传递的关键字的列表,它们被传递的位置以及很可能的某种指示标记,用于指示关键字从何处开始和位置变得无关紧要.

I believe that the first argument is a PyObject pointer that points to an array of the arguments being passed into the C-extension function in the order they were passed. The second argument is a list of keywords that were passed, the positions at which they were passed and, very likely, some sort of indicator flag telling at which position the keywords begin and position becomes irrelevant.

PyArg_ParseTupleAndKeywords然后使用其关键字列表(第4个参数)在用关键字指定的参数与格式字符串(第3个参数)和C变量的地址(第5个+参数)之间映射,适当的值应为复制.

PyArg_ParseTupleAndKeywords then uses its list of keywords (4th argument) to map between arguments specified with a keyword and both the format string (3rd argument) and addresses of C variables (5th & + arguments) to which the appropriate values should be copied.

我的理解正确吗?当我阅读在线文档时,所看到的只是对位置参数和关键字参数"的引用,这让我感到有些茫然.处理PyArg_ParseTupleAndKeywords的Python解释器的文件在哪里?

Is my understanding correct? When I read through the online documentation, all I see are references to "positional arguments and keyword arguments", which leave me feeling a little bit in the dark. Where is the file for the Python interpreter that handles PyArg_ParseTupleAndKeywords?

推荐答案

您是否已阅读 http上的开头说明://docs.python.org/c-api/arg.html ?它很好地解释了正在发生的事情.不要直接使用PyArg_ParseTupleAndKeywords的特定参考;它假定您已阅读上面的文字,并且本身并不是很有帮助.

Have you read through the opening explanation at http://docs.python.org/c-api/arg.html ? It does a pretty good job of explaining what's going on. Don't go right to the specific reference for PyArg_ParseTupleAndKeywords; it assumes you read the text above, and isn't very helpful by itself.

不过,您几乎已经掌握了它.第一个参数确实是传入的位置参数的列表.第二个是传入的关键字参数的映射(将给定的关键字名称映射到给定的值).第四个参数实际上是函数准备接受的关键字列表.是的,第3个参数是格式字符串,第5个及以后的参数是将值复制到的C指针.

You've almost got it, though. The first argument is indeed a list of incoming positional arguments. The second is a map of incoming keyword arguments (mapping given keyword name to given value). The fourth argument is actually the list of keywords which your function is prepared to accept. Yes, the 3rd argument is the format string and the 5th and later are C pointers into which the values are copied.

您会在Python/getargs.c下找到PyArg_ParseTupleAndKeywords().

这篇关于PyArg_ParseTupleAndKeywords如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆