Search code examples
python-c-api

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


METH_VARARGS requires at least one argument; METH_NOARGS doesn't seem to let me pass any at all.

How can I define a function build() that can be called as either build() or build(True)/build(False)?

Calling a METH_VARARGS function with no arguments results in:

TypeError: function takes exactly 1 argument (0 given)

Solution

  • I was thinking about the problem wrong. It's not the definition, but rather the parsing that rose my TypeError.

    To prevent it, I just had to use "|O" instead of "O" in PyArg_ParseTuple!