c++ - C++ 定义的函数中来自 Python 的默认参数?

标签 c++ python

我在我的 C++ 应用程序中嵌入了 Python,并且我创建了几个可以从 Python 调用的 C 函数。

为了获得参数,我目前正在做:

if (!PyArg_ParseTuple(args, "zk", &param1, &param2))
  return NULL;

但是,我希望param2 是可选的。如何分别检查这两个?

最佳答案

You don't.

|

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", &param1, &param2))
  return NULL;

关于c++ - C++ 定义的函数中来自 Python 的默认参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9865539/

相关文章:

c++ - 未解析的外部符号 - 构造函数

c++ - char 数组在最后一次迭代时复制整个单词而不是单个字母

python - 存储一组四个(或更多)值的最佳数据结构是什么?

python - 我在 python 中提取 rarfile 时遇到错误(找不到工作工具)

c++ - 静态链接库不运行内部代码来设置静态变量

c++ - 为什么 boost::is_same<int const&, boost::add_const<int &>::value 等于 false?

c++ - 是否需要标准分配器来分配连续内存?

python - 设备代码流 - Microsoft Azure 身份验证

python - 计算其他列上给定条件的列类别的唯一值

Python:在后台进程上使用 popen poll