get function arguments

Steven Bethard steven.bethard at gmail.com
Tue Nov 16 15:46:28 EST 2004


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