How to find argument list of a callable?

David Eppstein eppstein at ics.uci.edu
Sun Oct 5 22:53:43 EDT 2003


Suppose I have a callable object x, and suppose for simplicity that it 
doesn't take any *- or **-arguments.  How can I get a list of the number 
and names of the arguments of x?

If x is a function, then apparently this list is in 
x.func_code.co_varnames[:x.func_code.co_argcount].  If x is an old-style 
class, it's in
x.__init__.func_code.co_varnames[1:x.__init__.func_code.co_argcount].  
If x is an object with a call method, it's in 
x.__call__.func_code.co_varnames[1:x.__call__.func_code.co_argcount].  
If x is a bound method or a new-style class, I have no idea how to find 
the argument names, and for all I know there are several other classes 
of callable that I haven't even thought of.

So far the only cases I actually need are functions and old-style 
classes, so I have working code, but I have the nagging suspicion that 
there's a cleaner way of doing this that I'm missing.

For that matter, is there a good way of testing whether x is an 
old-style class?  inspect.isclass(x) and not isinstance(x,type) seems to 
work but feels kind of clunky.

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list