Paul's Python CoursePPC

*args and **kwargs

Not met yet

*lines in a def line gathers any number of unnamed arguments into a tuple, a list that can't be changed. **notes gathers the named arguments that match no other parameter into a dictionary, names as keys. A parameter after *lines can only be given by name. In a call, * and ** do the reverse: they spread a list or dictionary out into separate arguments.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks *args gathers the arguments into a list rather than a tuple.
  • Thinks a *args parameter takes exactly one argument, like any other parameter.
  • Thinks an unnamed argument after the others can fill a parameter that comes after *args.
  • Thinks **kwargs gathers only the values, not a dictionary of names and values.

Where it comes up