These special parameters allow functions to take arbitrary amounts of positional and keyword arguments. The names
kwargs are purely convention, and could be named any other valid variable name. The special functionality comes from the single and double asterisks (
*). If both are used in a function signature,
*args must appear before
*args will ingest an arbitrary amount of positional arguments, and store it in a tuple. If there are parameters after
*args in the parameter list with no default value, they will become required keyword arguments by default.
**kwargs will ingest an arbitrary amount of keyword arguments, and store it in a dictionary. There can be no additional parameters after
**kwargs in the parameter list.
- Decorators (see
- Inheritance (overriding methods)
- Future proofing (in the case of the first two bullet points, if the parameters change, your code won't break)
- Flexibility (writing functions that behave like
/tag positional-keyword for information about positional and keyword arguments