How to access args as a list?

Grant Edwards invalid at invalid.invalid
Sun Apr 4 03:14:24 CEST 2010


On 2010-04-03, kj <no.email at please.post> wrote:
> In <hp8h73$k17$1 at reader1.panix.com> kj <no.email at please.post> 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.

then add the line below:

   x,y,z = *args

-- 
Grant



More information about the Python-list mailing list