我在我的 C++ 应用程序中嵌入了 Python,并且我创建了几个可以从 Python 调用的 C 函数。
为了获得参数,我目前正在做:
if (!PyArg_ParseTuple(args, "zk", ¶m1, ¶m2))
return NULL;
但是,我希望param2
是可选的。如何分别检查这两个?
最佳答案
|
Indicates that the remaining arguments in the Python argument list are optional. The C variables corresponding to optional arguments should be initialized to their default value — when an optional argument is not specified,
PyArg_ParseTuple()
does not touch the contents of the corresponding C variable(s).
param2 = 42;
if (!PyArg_ParseTuple(args, "z|k", ¶m1, ¶m2))
return NULL;
关于c++ - C++ 定义的函数中来自 Python 的默认参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9865539/