Knowing the signature of a function
Steven D'Aprano
steve at REMOVETHIScyber.com.au
Wed Jun 8 12:10:03 EDT 2005
On Wed, 08 Jun 2005 08:35:39 -0400, Kent Johnson wrote:
>> 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
> helpful.
You guys are amazing, have you been spying on me? :-) I was just about to
write in with the same question. Thanks for answering before I asked.
--
Steven.
More information about the Python-list
mailing list