954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PyArg_ParseTuple - is optional argument touched?

hi there,

i'm using the PyArg_ParseTuple() function in my python bindings for some C code. the code i'm wrapping has two functions, one that returns a default value instead of an error if an error would have occurred, and one that returns an error. i simply want to use one function, and an optional argument.

Using PyArg_ParseTuple(), how might I tell if the optional argument was filled? the only thing i could come up with was parsing twice with two different initial values (in case you initialized to the default value, and said "don't use the default, it didn't change!")

cheers
chardson

chardson
Newbie Poster
3 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

You have to initialize the variable to something else and check if it changed.

int x;
int y = 4;

if (!PyArg_ParseTuple(args, "i|i", &x, &y))
    return NULL;

if (y != 4)
{
    // non-default argument supplied.
}
scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You