I’m going through python and I was wondering what are the advantages of using the *args as a parameter over just passing a list as a parameter, besides aesthetics?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Generally it’s used to either pass a list of arguments to a function that would normally take a fixed number of arguments, or in function definitions to allow a variable number of arguments to be passed in the style of normal arguments. For instance, the
print()function uses varargs so that you can do things likeprint(a,b,c).One example from a recent SO question: you can use it to pass a list of
range()result lists toitertools.product()without having to know the length of the list-of-lists.Sure, you could write every library function to look like this:
…but that defeats the point of having named positional argument variables, it’s basically exactly what
*argsdoes for you, and it results in redundant braces/parens, since you’d have to call a function like this:…which looks very similar to…
…except with unnecessary characters, as opposed to using
*args.