Number of args in a function

Kragen Sitaker kragen at
Tue Oct 16 23:23:52 CEST 2001

In article <mailman.1003265356.27429.python-list at>,
Skip Montanaro  <skip at> wrote:
>    Mark> Given a function f, how many arguments does it take?
>>>> def foo(a,b,c,*args,**kwds):
>...   pass
>>>> foo.func_code
><code object foo at 0x8193168, file "<stdin>", line 1>
>>>> foo.func_code.co_nlocals
>>>> foo.func_code.co_varnames
>('a', 'b', 'c', 'args', 'kwds')

co_nlocals isn't right; Mark asked for co_argcount:
>>> def baz(a, *c, **d):                        
...     e = a + c + d.keys()
...     return e
>>> baz.func_code.co_argcount
>>> baz.func_code.co_varnames
('a', 'c', 'd', 'e')
>>> baz.func_code.co_nlocals 

baz.func_code.co_flags tells you whether the function accepts *arg
and/or **arg as well as the positional args in co_argcount.
<kragen at>       Kragen Sitaker     <>
Perilous to all of us are the devices of an art deeper than we possess
       -- Gandalf the White [J.R.R. Tolkien, "The Two Towers", Bk 3, Ch. XI]

More information about the Python-list mailing list