How to access args as a list?

kj at
Sat Apr 3 19:52:42 EDT 2010

In <hp8h73$k17$1 at> kj < at> writes:

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

>I'm aware of locals(), but I want to preserve the order in which
>the arguments appear in the signature.

>My immediate aim is to set up a simple class that will allow me to
>iterate over the arguments passed to the constructor (plus letS me
>refer to these individual arguments by their names using an
>instance.attribute syntax, as usual).

The underlined portion explains why __init__(self, *args) fails to
fit the bill.

>P.S. this is just an example; the function I want to implement has
>more parameters in its signature, with longer, more informative

Andreas, perhaps this paragraph explains why I find your solution
unappealing:  it requires typing the same thing over and over,
which increases the chances of bugs.  That's the reason I avoid
such repetitiveness, not laziness, as you so were so quick to accuse
me of.


More information about the Python-list mailing list