15.2. 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 of a function definition, 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.

15.2.1. Keyword Parameters with .format

Earlier you learned how to use the format method for strings, which allows you to structure strings like fill-in-the-blank sentences. Now that you’ve learned about optional and keyword parameters, we can introduce a new way to use the format method.

This other option is to specifically refer to keywords for interpolation values, like below.

Sometimes, you may want to use the .format method to insert the same value into a string multiple times. You can do this by simply passing the same string into the format method, assuming you have included {} s in the string everywhere you want to interpolate them. But you can also use positional passing references to do this! The order in which you pass arguments into the format method matters: the first one is argument 0, the second is argument 1, and so on.

For example,

Check your understanding

    advfuncs-2-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.

    advfuncs-2-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.

    advfuncs-2-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.

    advfuncs-2-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.

    advfuncs-2-5: What value will be printed below?

    names = ["Alexey", "Catalina", "Mitsuki", "Pablo"]
    print("'{first}!' she yelled. 'Come here, {first}! {f_one}, {f_two}, and {f_three} are here!'".format(first = names[1], f_one = names[0], f_two = names[2], f_three = names[3]))
    
  • 'first!' she yelled. 'Come here, first! f_one, f_two, and f_three are here!'
  • Remember, the values inside of {} are variable names. The values of the variables will be used.
  • 'Alexey!' she yelled. 'Come here, Alexey! Catalina, Misuki, and Pablo are here!'
  • Look again at what value is set to the variable first.
  • 'Catalina!' she yelled. 'Come here, Catalina! Alexey, Misuki, and Pablo are here!'
  • Yes, the keyword parameters will determine the order of the strings.
  • There is an error. You cannot repeatedly use the keyword parameters.
  • This is not an error, you can do that in Python!
5. Define a function called multiply. It should have one required parameter, a string. It should also have one optional parameter, an integer, named mult_int, with a default value of 10. The function should return the string multiplied by the integer. (i.e.: Given inputs “Hello”, mult_int=3, the function should return “HelloHelloHello”)
6. Currently the function is supposed to take 1 required parameter, and 2 optional parameters, however the code doesn’t work. Fix the code so that it passes the test. This should only require changing one line of code.
Next Section - 15.3. Anonymous functions with lambda expressions