get function arguments

Jean Brouwers mrjean1ATcomcastDOTnet at no.spam.net
Tue Nov 16 17:38:01 EST 2004


Check the getargspec() function in the inspect module.

>>> import inspect
>>> def f(a, b, c, d=4, *args, **kwds):
...  pass          
... 
>>> inspect.getargspec(f)
(['a', 'b', 'c', 'd'], 'args', 'kwds', (4,))

>>> def g(a, b, c):
...  print inspect.getargspec(g)
... 
>>> g(1, 2, 3)
(['a', 'b', 'c'], None, None, None)


/Jean Brouwers
 ProphICy Semiconductor, Inc.

In article <mailman.6455.1100638487.5135.python-list at python.org>,
Steven Bethard <steven.bethard at gmail.com> wrote:

> Batista, Facundo <FBatista <at> uniFON.com.ar> writes:
> > [Steven Bethard]
> > #- >>> def myFunc(a,b,c='default'):
> > #- ...  pass
> > #- ...
> > #- >>> myFunc.func_code.co_varnames
> > #- ('a', 'b', 'c')
> > #- 
> > How do you do that from *inside* the function?
> 
> I'm not clear exactly what it is you're looking to do here -- could you
> elaborate?  If you're writing the function yourself, don't you know what
> arguments you wrote up in the def statement?
> 
> I'm hesitant to guess what it is you intend, but if you're just looking for
> the
> string names, this might work for you:
> 
> >>> def myFunc(a,b,c='default'):
> ...    print locals().keys()
> ... 
> >>> myFunc(1, 2)
> ['a', 'c', 'b']
> 
> My guess though is that if this is what you want, you might have a design
> problem there...  You should also be wary that this only really works in the
> first line of the function (or at least until you create another name in the
> function).
> 
> Steve
> 
>



More information about the Python-list mailing list