0

I am trying to learn how to use *args and came up with this.

alist =[5,2,6,1,2]
blist =[1,5,2,4,1]

def argstest(var1, var2, *args):
    print "var1", var1
    print "var2", var2

    temp = list(args)[0]

    for i in temp:
        if i > 3:
           print i

argstest(5, 8, alist, blist)
>>>>> var1 5
>>>>> var2 8
>>>>> 5
>>>>>>6
>>>>>>5
>>>>>>4

Though I see how this could be very useful my first concern was that

temp = list(args)[0]  

is a very odd way to convert *args into a list, and the second was that this is not a standard way of making use of *args.

My question what would be the standard way of converting *args to a list?

Also, is this even the correct way to write a function that performs a task in this manner?

  • 4
    `*args` is a tuple, why do you need to convert it to a list? You are also passing *in* a list as one argument, so `args[0]` is that list. – Martijn Pieters Nov 11 '13 at 21:22
  • Martijn, In the case of this function it has to be a list to iterate in the for loop. femtoRgon, these questions may be similar but they are not at all the same. – CodeHard_or_HardCode Nov 11 '13 at 21:27
  • 2
    @NickSimas: No it doesn't. You can iterate a tuple, or in fact _any_ kind of iterable, exactly the same way you can iterate a list. – abarnert Nov 11 '13 at 21:31
  • Meanwhile, why do you think calling `list` on something is "a very odd way to convert [it] to a list"? That's the standard, obvious way to convert anything to a list. – abarnert Nov 11 '13 at 21:32
  • 1
    @NickSimas: a `for` loop takes any iterable; list or tuple makes no odds here. – Martijn Pieters Nov 11 '13 at 21:32
  • 1
    On top of that, you're not actually iterating the list anyway, you're just indexing it with `[0]`. Which _also_ works exactly as well on a tuple, or any other kind of sequence, as on a list. – abarnert Nov 11 '13 at 21:33
  • 1
    @NickSimas: your confusion probably stems from mixing `*args` syntax *and* passing in a list object. Now you have a list inside of a tuple. Try calling your function with `argstest(1, 2, 3, 4)` and see what happens to `args`. – Martijn Pieters Nov 11 '13 at 21:34
  • 1
    Also, try adding `print args` in your function before calling it in various different ways. – abarnert Nov 11 '13 at 21:35
  • Does *args essentially do the same things as if I were to put alist and blist inside a list and then call the function as: for i in list[alist,blist]: argtest(i) – CodeHard_or_HardCode Nov 11 '13 at 21:58

4 Answers4

2

Iterates through each argument after the first two:

alist =[5,2,6,1,2]
blist =[1,5,2,4,1]

def argstest(*args):
    for i in args[2:]
        for j in i:
            if j > 3:
                print j

argstest(5, 8, alist, blist)

You'll see that *args allows you to pass in an arbitrary number of arguments. Now it flattens multiple lists into one.

0

You dont convert args into a list... args is a tupl and the first element is your list. Args is already a tupl. Just give the list to the function. You dont need to use the *.

0

'*' combines all the unnamed parameters into tuple and such is *args. You should also look into **args (writing args is just a convention) which converts all named parameters into dictionaries. This is a step towards crossing between beginners to intermediate programmers. These two things will be widely used when you do OOP and use inheritance.

Jack_of_All_Trades
  • 10,942
  • 18
  • 58
  • 88
0

You don't need to use the temp variable just iterate through the args and print out each list.

alist =[5,2,6,1,2]
blist =[1,5,2,4,1]

def argstest(var1, var2, *args):
    print "var1", var1
    print "var2", var2

    for i in args:
        if i > 3:
           print i

argstest(5, 8, alist, blist)
TJ Nel
  • 1
  • 1