How to access args as a list?

kj no.email at please.post
Mon Apr 5 14:49:08 EDT 2010


In <4bb802f7$0$8827$c3e8da3 at news.astraweb.com> Steven D'Aprano <steve at REMOVE-THIS-cybersource.com.au> writes:

>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')

That's very handy.  Thanks!

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

Indeed.  Why Python does not provide this elmentary form of
introspection as a built-in variable is extremely puzzling to me
(even--no, *more so*--after reading PEP 3130).

>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.

That's a good idea.  Thanks!

~K



More information about the Python-list mailing list