Does * have a special meaning in Python as it does in C? I saw a function like this in the Python Cookbook:
def get(self, *a, **kw)
Would you please explain it to me or point out where I can find an answer (Google interprets the * as wild card character and thus I cannot find a satisfactory answer).
See Function Definitions in the Language Reference.
Also, see Function Calls.
Assuming that one knows what positional and keyword arguments are, here are some examples:
Example 1:
As you can see in the above example, we only have parameters
a, b, cin the signature of thefoofunction. Sincedandkare not present, they are put into the args dictionary. The output of the program is:Example 2:
Here, since we’re testing positional arguments, the excess ones have to be on the end, and
*argspacks them into a tuple, so the output of this program is:You can also unpack a dictionary or a tuple into arguments of a function:
Prints:
And
Prints: