使用 Python C API,我如何编写一个接受任意数量参数(包括 none 的函数?

Using the Python C API, how can I write a function that accepts any number of arguments, including none at all?

METH_VARARGS 至少需要一个参数; METH_NOARGS 好像一点都不让我通过。

如何定义函数 build() 可以被调用为 build()build(True)/build(False)

调用不带参数的 METH_VARARGS 函数会导致:

TypeError: function takes exactly 1 argument (0 given)

我想的问题错了。引起我的类型错误的不是定义,而是解析。

为了防止它,我只需要在 PyArg_ParseTuple!

中使用 "|O" 而不是 "O"