When writing Python 3.x functions, I've often stumbled upon the problem of writing code that accepts both a single value and a number of values. Take this trivial function:
def capitalize(word):
return word.capitalize()
This function works well with a single string as input, but will fail if provided with multiple strings, or a list of strings.
We could turn the function to accept a list of strings, that way it would work if provided both with one value, or multiple. Even if I passed a single value, however, it would need to be wrapped in a list, and the return value would be a list as well, even when containing one element.
def capitalize(words):
return [word.capitalize() for word in words]
I recently read up on *args, and thought i could get around the awkwardness of having to pass a single string wrapped in a list as argument to the above function by rewriting it to be like the following:
def capitalize(*words):
return [word.capitalize() for word in words]
It seems to me that this is a legittimate use of *args. However, I found a pyCon 2015 talk in which the speaker claims "variable positional arguments are good for removing noise and make your code readable, not to turn a single call api into an N api"
https://www.youtube.com/watch?v=WjJUPxKB164 at the 5:30 mark.
Is this a reasonable use of *args? If this is not how *args is supposed to be used, is there an established way to handle single vs multi-parameter functions?
In addition to the accepted answer (which raises some good points), you also have to consider how your function is going to be used. Using os.path.join as an example, it makes sense for this function to accept *args because one is going to often be dealing with a set number of path elements, where each one might have its own name. For example:
dir_name = os.getcwd()
temp_folder = 'temp'
base_name = 'base_file.txt'
full_path = os.path.join(dir_name, temp_folder, base_name)
On the other hand, when you are working with indistinguishable elements that are naturally going to be grouped into a container object, it makes more sense to use args. Consider the following usage with str.join.
sentence = 'this is an example sentence'
words = sentence.split()
dash_separated = '-'.join(words)
Yes it's fine to use *args here.
You should use *args when it's easier to read than a list of argument (def foo(a,b,c,d,e...)) and does not make harder to know which argument is used for what.
Your example is the perfect example of a good usage of *args, all elements within args will be processed in the exacte same way.
You should avoid it when you need to perform different compute for each argument, like:
def foo(*args):
do_one_stuff(args[1], args[2])
if args[3]:
do_another_stuff(args[3], args[4])
if args[6] != args[7]:
return args[0]
else:
return None
This example should make you feel what a function could be if you make a bad usage of *args.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With