3

I've embedded Python in my C++ application, and I've created several C functions which can be called from Python.

To get the arguments, I'm currently doing:

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

However, I want param2 to be optional. How can I check for the two of them separately?

1 Answer 1

3

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Sweet, I'll accept your answer once the 2 minutes are up -- thanks for the fast response!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.