Keyword Parameters

In the previous section, on Optional Parameters you learned how to define default values for formal parameters, which made it optional to provide values for those parameters when invoking the functions.

In this chapter, you’ll see one more way to invoke functions with optional parameters, with keyword-based parameter passing. This is particularly convenient when there are several optional parameters and you want to provide a value for one of the later parameters while not providing a value for the earlier ones.

The online official python documentation includes a tutorial on optional parameters which covers the topic quite well. Please read the content there: * Keyword arguments

Don’t worry about the def cheeseshop(kind, *arguments, **keywords): example. You should be able to get by without understanding *parameters and **parameters in this course. But do make sure you understand the stuff above that.

The basic idea of passing arguments by keyword is very simple. When invoking a function, inside the parentheses there are always 0 or more values, separated by commas. With keyword arguments, some of the values can be of the form paramname = <expr> instead of just <expr>. Note that when you have paramname = <expr> in a function definition, it is defining the default value for a parameter when no value is provided in the invocation; when you have paramname = <expr> in the invocation, it is supplying a value, overriding the default for that paramname.

To make it easier to follow the details of the examples in the official python tutorial, you can step through them in CodeLens.

(keyword_params_1)

As you step through it, each time the function is invoked, make a prediction about what each of the four parameter values will be during execution of lines 2-5. Then, look at the stack frame to see what they actually are during the execution.

Note

Note that we have yet another, slightly different use of the = sign here. As a stand-alone, top-level statement, x=3, the variable x is set to 3. Inside the parentheses that invoke a function, x=3 says that 3 should be bound to the local variable x in the stack frame for the function invocation. Inside the parentheses the define a function, x=3 says that 3 should be the value for x in every invocation of the function where no value is explicitly provided for x.

Check your understanding

    keyword-params-1: What value will be printed for z?

    initial = 7
    def f(x, y = 3, z = initial):
        print("x, y, z are:", x, y, z)
    
    f(2, 5)
    
  • 2
  • 2 is bound to x, not z
  • 3
  • 3 is the default value for y, not z
  • 5
  • 5 is bound to y, not z
  • 7
  • 2 is bound x, 5 to y, and z gets its default value, 7
  • Runtime error since not enough values are passed in the call to f
  • z has a default value in the function definition, so it's optional to pass a value for it.

    keyword-params-2: What value will be printed for y?

    initial = 7
    def f(x, y = 3, z = initial):
        print("x, y, z are:", x, y, z)
    
    f(2, z = 10)
    
  • 2
  • 2 is bound to x, not y
  • 3
  • 3 is the default value for y, and no value is specified for y,
  • 5
  • say what?
  • 10
  • 10 is the second value passed, but it is bound to z, not y.
  • Runtime error since no value is provided for y, which comes before z
  • That's the beauty of passing parameters with keywords; you can skip some parameters and they get their default values.

    keyword-params-3: What value will be printed for x?

    initial = 7
    def f(x, y = 3, z = initial):
        print("x, y, z are:", x, y, z)
    
    f(2, x=5)
    
  • 2
  • 2 is bound to x since it's the first value, but so is 5, based on keyword.
  • 3
  • 5
  • 5 is bound to x by keyword, but 2 is also bound to it by virtue of being the value and not having a keyword. In the online environment, it actually allows this, but not in a proper python interpreter.
  • 7
  • Runtime error since two different values are provided for x
  • 2 is bound to x since it's the first value, but so is 5, based on keyword.

    keyword-params-4: What value will be printed for z?

    initial = 7
    def f(x, y = 3, z = initial):
        print "x, y, z are:", x, y, z
    initial = 0
    f(2)
    
  • 2
  • 2 is bound to x, no z
  • 7
  • the default value for z is determined at the time the function is defined; at that time initial has the value 0.
  • 0
  • the default value for z is determined at the time the function is defined, not when it is invoked.
  • Runtime error since two different values are provided for initial.
  • there's nothing wrong with reassigning the value of a variable at a later time.
Next Section - Exercises