[C++-sig] Check number of arguments of a function object ?

Stefan Seefeld seefeld at sympatico.ca
Wed Feb 20 15:31:10 CET 2008


Marcus Jannes wrote:
> Hi again,
> 
> if i have a callable function object (PyObject*), how can i check the number of arguments this object has?
> PyArg_ParseTuple does not seem to accomplish this.

(You keep asking questions about the Python C API. There must be other 
lists about that. This list is about Python <-> C++ integration, and in 
particular, boost.python. Please do yourself a favor and try using that.)

> def f(x): <- 1 argument
> def f(x,y): <- 2 arguments

What you mean here is 'parameter', not 'argument'. (A function parameter 
is part of the function's definition, while and argument is what gets 
passed to a function call.)

Also, what about

def f(x, y=None): pass

? The function has two parameters, but it is valid to call it with 
either one or two arguments. Further, inside the function implementation 
(no matter whether Python, C, or C++), you don't know whether an 
argument was actually passed or is a default value.

> I want to make sure only functions with one argument get processed.

Can you rephrase that ? Do you mean 'function calls' instead of 
'functions' ? And what do you mean by 'get processed' ? In particular, 
what should happen when there are multiple arguments ? Should they get 
ignored ? An exception raised ?

You may be interested into

http://boost.org/libs/python/doc/tutorial/doc/html/python/functions.html#python.overloading

but it isn't clear from your description whether that is what you are after.

HTH,
		Stefan

-- 

       ...ich hab' noch einen Koffer in Berlin...



More information about the Cplusplus-sig mailing list