Knowing the signature of a function
Xavier.Decoret at imag.fr
Wed Jun 8 14:44:47 CEST 2005
Kent Johnson a écrit :
> Xavier Décoret wrote:
>> I have the following code:
>> def foo(x,y):
>> How can I query the function object foo to know the number of
>> parameters it expects. I can find it is a function using callable(f),
>> I can find some information (listed by dir(foo)) such as the name of
>> the function,etc.. but nowhere I can find the number of arguments.
>> I would like to know wether the function expects one or zero arguments.
> foo.func_code.co_argcount gives the count of named args.
> len(foo.func_code.co_varnames) gives the total number of arguments
> including *args and **kwds args. inspect.getargspec() might also be
Now I have the following issue: what if foo is not a function but a
callable? inspect.getargspec raises an exception.
I have to do something like:
if inspect.isfunction(f): return len(inspect.getargspec(f))
Is there any better way?
More information about the Python-list