How to access args as a list?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Apr 3 23:09:43 EDT 2010


On Sat, 03 Apr 2010 22:58:43 +0000, kj wrote:

> Suppose I have a function with the following signature:
> 
> def spam(x, y, z):
>     # etc.
> 
> Is there a way to refer, within the function, to all its arguments as a
> single list?  (I.e. I'm looking for Python's equivalent of Perl's @_
> variable.)

Does this help?

>>> def spam(a, b, c=3, d=4):
...     pass
...
>>> spam.__code__.co_varnames
('a', 'b', 'c', 'd')

The hardest part is having the function know its own name.

I see that you are already using the inspect module. That almost 
certainly is the correct approach. I'd be surprised if inspect is too 
heavyweight, but if it is, you can pull out the bits you need into your 
own function.


-- 
Steven



More information about the Python-list mailing list