another newbie question: why should you use "*args" ?

stef s.mientki at id.umcn.nl
Wed Jan 31 09:05:10 EST 2007


> It's bad practice to use built-ins like 'list' as a regular variable
> name.
>   
ok, but it was just an example (in practice, I always use very long 
names ;-)
>   
>> # calling method 1:
>> execute (S[0], S[4] )
>>
>> # calling method 2:
>> execute ( ( S[0], S[4] ) )
>>     
>
> Let's take a look at those side-by-side:
> execute (S[0], S[4] )
> execute ( ( S[0], S[4] ) )
>
> Now, which one *looks* better?
>
>   
>> # or *the extra flexibility)
>> mylist = ( S[0], S[4] )
>> execute ( mylist )
>>     
>
> Also, take into consideration the opposite end of the pole; you have
> your list of arguments (args), and your about to call a function that
> was declared something like this:
> def someFunction(arg1, arg2, arg3):
>   # etc.
> Which is clearer?
> someFunction(*args)
> someFunction(args[0], args[1], args[2])
>
> And if you've got a variable number of arguments, it becomes virtually
> impossible to avoid using the *args syntax.
>
>   
# So with this construct, I have all flavours:

def chunk_plot(*args):
    if len(args)==1: my_example_var = args[0]
    else:                 my_example_var = args
    for i in range  ( len ( my_example_var ) ):
        ... do something with  my_example_var [i]

# calling the procedure
chunk_plot (S[1], S[4])
chunk_plot ( ( S[1], S[4] ) )
my_action_list = ( S[1], S[2] )
chunk_plot ( my_action_list )


And sorry, no need for kwargs for now ;-)

thanks guys,
Stef
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070131/d3f994e0/attachment.html>


More information about the Python-list mailing list