Passing values through arguments to parameters allows you to give information to a function. But so far, you've only seen the most basic way to do that. Python allows greater control and flexibility with the way you pass information, through default parameter values and keyword arguments.
The program Birthday Wishes, a sample run of which is pictured in Figure 6.6, sends birthday greetings through two very similar functions. The first function uses the type of parameters you saw in the last section, called positional parameters. The second version of the function uses default parameter values. The best way to appreciate the difference is to see examples of them in action.
Figure 6.6: Functions can be called in different ways with the flexibility of keyword arguments and default parameter values.
Here's the code for Birthday Wishes:
# Birthday Wishes # Demonstrates keyword arguments and default parameter values # Michael Dawson - 2/21/03 # positional parameters def birthday1(name, age): print "Happy birthday,", name, "!", "I hear you're", age, "today.\n" # parameters with default values def birthday2(name = "Jackson", age = 1): print "Happy birthday,", name, "!", "I hear you're", age, "today.\n" birthday1("Jackson", 1) birthday1(1, "Jackson") birthday1(name = "Jackson", age = 1) birthday1(age = 1, name = "Jackson") birthday2() birthday2(name = "Katherine") birthday2(age = 12) birthday2(name = "Katherine", age = 12) birthday2("Katherine", 12) raw_input("\n\nPress the enter key to exit.")
If you just list out a series of variable names in a function's header, you create positional parameters:
def birthday1(name, age):
If you call a function with just a series of values, you create positional arguments:
birthday1("Jackson", 1)
Using positional parameters and positional arguments means that parameters get their values based solely on the position of the values sent. The first parameter gets the first value sent, the second parameter gets the second value sent, and so on.
With this particular function call, it means that name gets "Jackson" and age gets 1. This results in the message: Happy Birthday, Jackson ! I hear you're 1 today. If you switch the positions of two arguments, the parameters get different values. So with the call
birthday1(1, "Jackson")
name gets the first value, 1, and age gets the second value, "Jackson". As a result, you end up with a message you probably didn't intend: Happy Birthday, 1 ! I hear you're Jackson today.
You've seen this way of creating and calling functions already. But there are other ways to create parameter and argument lists in your programs.
Positional parameters get values sent to them in order, unless you tell the function otherwise. You can tell the function to assign certain values to specific parameters, regardless of order, if you use keyword arguments. With keyword arguments, you use the actual parameter names from the function header to link a value to a parameter. So, by calling the same function birthday1() with
birthday1(name = "Jackson", age = 1)
name gets "Jackson" and age gets 1 and the function displays the message Happy Birthday, Jackson ! I hear you're 1 today. This isn't terribly impressive. You could achieve the same results without keyword arguments by just sending these values in this order. But the beauty of keyword arguments is that their order doesn't matter; it's the keywords that link values to parameters. So the call
birthday1(age = 1, name = "Jackson")
also produces the message Happy Birthday, Jackson ! I hear you're 1 today. even though the values are listed in opposite order.
Keyword arguments let you pass values in any order. But their biggest benefit is clarity. When you see a function call using keyword arguments, you get a much better understanding of what the values represent.
TRAP | You can combine keyword arguments and positional arguments in a single function call, but this can get tricky. Once you use a keyword argument, all the remaining arguments in the call must be keyword arguments, too. To keep things simple, try to use all keyword or all positional arguments in your function calls. |
Finally, you have the option to assign default values to your parameters, values that get assigned to the parameters if no value is passed to them. That's just what I did with the birthday2() function. I made changes in the header only:
def birthday2(name = "Jackson", age = 1):
This means that if no value is supplied to name, it gets "Jackson". And if no value is supplied for age, it gets 1. So the call
birthday2()
doesn't generate an error; instead, the default values are assigned to the parameters, and the function displays the message Happy Birthday, Jackson ! I hear you're 1 today.
TRAP | Once you assign a default value to a parameter in the list, you have to assign default values to all the parameters listed after it. So, this function header is perfectly fine:
def monkey_around(bananas = 100, barrel_of = "yes", uncle = "monkey's"): But this isn't:
def monkey_around(bananas = 100, barrel_of, uncle): The above header will generate an error. |
So far, so good. But you can add a wrinkle here by overriding the default values of any or all the parameters. With the call
birthday2(name = "Katherine")
the default value of name is overridden. name gets "Katherine", age still gets its default value of 1, and the message Happy Birthday, Katherine ! I hear you're 1 today. is displayed.
With this function call:
birthday2(age = 12)
the default value of age is overridden. age gets the value of 12. name gets it's default value of "Jackson". And the message Happy Birthday, Jackson ! I hear you're 12 today. is displayed.
With the call
birthday2(name = "Katherine", age = 12)
both default values are overridden. name gets "Katherine" and age gets 12. The message Happy Birthday, Katherine ! I hear you're 12 today. is displayed.
And with the call
birthday2("Katherine", 12)
you get the exact same results as you did with the previous call. Both default values are overridden. name gets "Katherine" and age gets 12. And the message Happy Birthday, Katherine ! I hear you're 12 today. is displayed.
TRICK | Default parameter values are great if you have a function where almost every time it's called, some parameter gets sent the same value. To save programmers using your function the trouble of typing this value every time, you could use a default parameter value instead. |