Skip to main content
Engineering LibreTexts

19.9: Gathering keyword args

  • Page ID
    42493
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In Section 12.4, we saw how to write a function that gathers its arguments into a tuple:

    def printall(*args):
        print(args)
    

    You can call this function with any number of positional arguments (that is, arguments that don’t have keywords):

    >>> printall(1, 2.0, '3')
    (1, 2.0, '3')
    

    But the * operator doesn’t gather keyword arguments:

    >>> printall(1, 2.0, third='3')
    TypeError: printall() got an unexpected keyword argument 'third'
    

    To gather keyword arguments, you can use the ** operator:

    def printall(*args, **kwargs):
        print(args, kwargs)
    

    You can call the keyword gathering parameter anything you want, but kwargs is a common choice. The result is a dictionary that maps keywords to values:

    >>> printall(1, 2.0, third='3')
    (1, 2.0) {'third': '3'}
    

    If you have a dictionary of keywords and values, you can use the scatter operator, ** to call a function:

    >>> d = dict(x=1, y=2)
    >>> Point(**d)
    Point(x=1, y=2)
    

    Without the scatter operator, the function would treat d as a single positional argument, so it would assign d to x and complain because there’s nothing to assign to y:

    >>> d = dict(x=1, y=2)
    >>> Point(d)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: __new__() missing 1 required positional argument: 'y'
    

    When you are working with functions that have a large number of parameters, it is often useful to create and pass around dictionaries that specify frequently used options.


    This page titled 19.9: Gathering keyword args is shared under a CC BY-NC 3.0 license and was authored, remixed, and/or curated by Allen B. Downey (Green Tea Press) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?