Discussion:
Knowing the signature of a function
Kent Johnson
2005-06-08 12:35:39 UTC
Permalink
Hello,
pass
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.

Kent
Xavier Décoret
2005-06-08 12:44:47 UTC
Permalink
Post by Kent Johnson
Hello,
pass
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.
Kent
Thanks.

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:


import inspect

def countargs(f):
if callable(f):
if inspect.isfunction(f): return len(inspect.getargspec(f)[0])
return len(inspect.getargspec(f.__call__)[0])-1
raise ValueError

class foo:
def __call__(self,a,b):
pass

def bar(x):
pass


print countargs(foo)
print countargs(bar)



Is there any better way?
Steven D'Aprano
2005-06-08 16:10:03 UTC
Permalink
Post by Kent Johnson
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.
Xavier Décoret
2005-06-08 12:21:54 UTC
Permalink
Hello,

I have the following code:

def foo(x,y):
pass

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.
Gerald Klix
2005-06-08 12:33:13 UTC
Permalink
... print "tf"
...
Post by Xavier Décoret
import inspect
inspect.getargspec( tf )
(['a', 'b', 'c'], 'arguments', 'keywordArguments', None)
Hello,
pass
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.
--
GPG-Key: http://keyserver.veridis.com:11371/search?q=0xA140D634
Loading...