getting number of arguments of builtin/C func

Brian Quinlan brian at sweetapp.com
Wed Mar 6 21:21:14 EST 2002


Chris Liechti wrote:

> i know these two:
> >>>numArgs = func.func_code.co_argcount
> >>>func.func_code.co_varnames[:numArgs]
> 
> >>>apply(inspect.formatargspec, inspect.getargspec(f))
> 
> both only work for python functions, but i would like to know the
> number of arguments a function takes regardles if its a type, builtin
> or python func.

There is no general way to determine the number of arguments that a
C-implemented function requires. 

> any ideas appart from calling with zero args and parsing the
> exception string (scanning: "TypeError: f() takes exactly 2 arguments
> (0 given)") ...

That would you do if you encountered an exception like this:
"TypeError: open() takes at least 1 argument (0 given)"? And what if
calling the function without arguments does something bad?

Cheers,
Brian





More information about the Python-list mailing list