inspecting a callable for the arguments it accepts

Gregory Ewing greg.ewing at canterbury.ac.nz
Wed Nov 15 00:34:20 EST 2017


Chris Angelico wrote:

>>>>wants_one_arg.__code__.co_argcount
> 
> 1
> 
>>>>wants_one_arg.__code__.co_varnames
> 
> ('x',)

That will give you some idea, but it's not foolproof -- e.g.
if the function has * or ** arguments, it's impossible to tell
in general how many arguments it accepts without studying the
code (and maybe not even then -- halting problem, etc.)

Some of the co_flags seem to indicate presence of * and **:

0x04 - has *args
0x08 - has **kwds

In Py3 there is also co_kwonlyargcount.

-- 
Greg



More information about the Python-list mailing list