The syntax is the * and **, the names *args and **kwargs are only by convention and you can use any other words (not keyword) in place of args and kwargs.
You would use *args when you’re not sure how many arguments might be passed to your function, i.e. it allows you to pass an arbitrary number of arguments to your function. For example:
1 2 3 4 5 6 7 8 9 10 11 |
def myfun(*args): for count, thing in enumerate(args): print('{0}. {1}'.format(count, thing)) myfun('apple', 'banana', 'cabbage') ## Returns ## 0. apple ## 1. banana ## 2. cabbage |
Similarly, **kwargs allows you to handle named arguments that you have not defined in advance:
1 2 3 4 5 6 7 8 9 |
def myfun(**kwargs): for name, value in kwargs.items(): print('{0} = {1}'.format(name, value)) myfun(apple = 'fruit', cabbage = 'vegetable') ## Returns ## cabbage = vegetable ## apple = fruit |