[Tutor] Re: Listing function arguments

Kent Johnson kent_johnson at skillsoft.com
Wed Sep 1 20:28:23 CEST 2004


The help function gets its juice from the inspect module and the docstrings:
 >>> def foo(a=None, b=''):
...   pass
...
 >>> help(foo)
Help on function foo in module __main__:

foo(a=None, b='')

 >>> import inspect
 >>> inspect.getargspec(foo)
(['a', 'b'], None, None, (None, ''))
 >>> inspect.formatargspec(*inspect.getargspec(foo))
"(a=None, b='')"

Inspect is looking at attributes of the function object, you can look at 
the module source for details. Pretty amazing what you can find out just by 
digging into an object!

Kent

At 07:58 PM 9/1/2004 +0200, Andrei wrote:
>Bernard Lebel wrote on Wed, 1 Sep 2004 15:16:31 +0200:
>
> > For instance, the first line of a function looks like this:
> > def cmcopy( aClients = [], sSource = '', sTarget = '', iSource = 0 ):
> >
> > So I'd like to know the argument names, for
> > 1- Know the order at wich the arguments must be passed
> > 2- Know what variables to supply for arguments
> >
> > I don't know if I missed something in the documentation...
>
>1. Yep, you missed the help() function. If you type in the interactive
>interpreter help(myfunction), you'll get the declaration of myfunction plus
>the docstring of that function (if it's present). There are also IDE's
>which give you auto-complete proposals.
>
>2. That's what the docstrings are for, given the fact that Python has no
>type declarations.
>
>--
>Yours,
>
>Andrei



More information about the Tutor mailing list