Issue
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?
Solution
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
.
Answered By - Arount
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.