'Throw an error if *args is missing comma in function argument
I have a function with unknown length of inputs for a *args argument/parameter (I know I can use a list input instead but I want to know how to use *args). All works well but if I pass in scalar inputs without a comma separation it doesn't throw an error. I'd like to detect this circumstance and throw an error. How can I detect missing comma separator and throw an error for *args?
def my_fun(*args):
return args
## Has comma
my_fun('cat', 'fish')
## ('cat', 'fish')
## Missing comma (Want to detect and throw an error)
my_fun('cat' 'fish')
## ('catfish',)
len(my_fun('cat' 'fish'))
## 1
## Missing comma (Want to detect and throw an error)
my_fun('cat' 'fish', 'dog')
## ('catfish', 'dog')
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
