7

Would passing in a list or dictionary of variables be more concise than passing in *args in Python methods?

For example,

def function(a, *argv):
   print('First variable:', a)
   for k in argv:
      print('Additional variable:',k)

is the same as

def function(a, list):
   print('First variable:', a)
   for k in list:
      print('Additional variable:',k)

except a list is passed in the second argument. What I think using *args would often do is to cause additional bugs in the program because the argument length only needs to be longer than the mandatory argument length. Would any please explain situations where *args would be really helpful? Thanks

2
  • You use *args when you don't know the number of arguments the python function will accept. Aug 1, 2019 at 13:02
  • In what circumstances would the number of arguments be unknown?
    – James Fang
    Aug 2, 2019 at 0:08

3 Answers 3

8

The first function accepts:

function('hello', 'I', 'am', 'a', 'function')

The second one won't. For the second you'd need:

function('hello', ['I', 'am', 'a', 'function'])

In principle, the first one is used when your function can have an arbitrary number of parameters (think: print), while the second one specifies that there's always a second parameter, which is an iterable (not necessarily a list, despite the name)

4

Passing *args is useful when you have to extract only some (or none) arguments in first level function and then pass others to other inner function without knowing about the details. e.g.

def inner_func(x, y, z, *args, **kwargs):
    # do something with x, y, and z
    return output

def outer_func(a, b, *args, **kwargs):
    # do something with a and b
    # pass the rest arguments to inner function without caring about details
    output = inner_func(*args, **kwargs)

    # do something with output
    return 
0

That is a fair ask as to why *args (or **kwargs) is essentially required when a list (or dict) could do the same task. The key reason to that is when a ** caller of a function does not know the number of arguments beforehand**. I'll try to explain this with reference to the particular scenario you have shared.

Lets suppose that we have the below function which finds the sum of all integers passed in. (I'm giving up sum builtin function for demonstration purpose, please bear with me :) )

def do_add(*int_args):
    total = 0
    for num in int_args:
        total += num
    return total

And you want to call this for an unknown number of arguments with an unknown number of times.

If in case you need to send a list argument, the do_add function might look like below:

def do_add(int_list):
    total = 0
    for num in int_list:
        total += 0
    return total

l1 = [1, 2, 3, 4, ... n]    # memory loaded with n int objects
do_add(l1)

l2 = [10, 20, ... n]
do_add(l2)

Firstly, you are loading the memory with an additional list object created just for the sake of function call. Secondly, if you have to add some more items to the list we may need to call another list method such as append or extend.

But if you follow the *args approach, you can avoid creating an extra list and focus only on the function call. If you need to add more arguments you can just add another argument separated by a comma rather than calling append or extend methods.

Assume that you want to call this function n times with 1000 arguments. It will result in n * 1000 new list objects to be created every time. But with the variable arguments approach, you can just call it directly.

do_add(1, 2, 3)            # call 1
do_add(10.0, 20.0, 30.0)   # call 2
...
do_add(x, y, z, ..., m)    # call n
1
  • In the second implementation of do_add, total += 0 should be total += num.
    – joseville
    Oct 17, 2021 at 12:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.