Number of args in a function
Kragen Sitaker
kragen at dnaco.net
Tue Oct 16 17:23:52 EDT 2001
In article <mailman.1003265356.27429.python-list at python.org>,
Skip Montanaro <skip at pobox.com> 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
>5
>>>> 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
1
>>> baz.func_code.co_varnames
('a', 'c', 'd', 'e')
>>> baz.func_code.co_nlocals
4
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 pobox.com> Kragen Sitaker <http://www.pobox.com/~kragen/>
Perilous to all of us are the devices of an art deeper than we possess
ourselves.
-- Gandalf the White [J.R.R. Tolkien, "The Two Towers", Bk 3, Ch. XI]
More information about the Python-list
mailing list