Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 6 ■ ABSTRACTION 119 WHAT IF I REALLY WANT TO? If you really want to modify your parameter, you can use a little trick—wrap your value in a list: >>> def inc(x): x[0] = x[0] + 1 ... >>> foo = [10] >>> inc(foo) >>> foo [11] Simply returning the new value is generally considered a cleaner solution. Keyword Parameters and Defaults The parameters we’ve been using until now are called positional parameters because their positions are important—more important than their names, in fact. Consider the following two functions: def hello_1(greeting, name): print '%s, %s!' % (greeting, name) def hello_2(name, greeting): print '%s, %s!' % (name, greeting) They both do exactly the same thing, only with their parameter names reversed: >>> hello_1('Hello', 'world') Hello, world! >>> hello_2('Hello', 'world') Hello, world! Sometimes (especially if you have many parameters) the order may be hard to remember. To make things easier, you can supply the name of our parameter: >>> hello_1(greeting='Hello', name='world') Hello, world! The order here doesn’t matter at all: >>> hello_1(name='world', greeting='Hello') Hello, world! The names do, however (as you may have gathered): >>> hello_2(greeting='Hello', name='world') world, Hello!
120 CHAPTER 6 ■ ABSTRACTION The parameters that are supplied with a name like this are called keyword parameters. On their own, the key strength of keyword parameters is that they can help clarify the role of each parameter. Instead of having to use some odd and mysterious call like >>> store('Mr. Brainsample', 10, 20, 13, 5) you could use >>> store(patient='Mr. Brainsample', hour=10, minute=20, day=13, month=5) Even though it takes a bit more typing, it is absolutely clear what each parameter does. Also, if you get the order mixed up, it doesn’t matter. What really makes keyword arguments rock, however, is that you can give the parameters in the function default values: def hello_3(greeting='Hello', name='world'): print '%s, %s!' % (greeting, name) When a parameter has a default value like this, you don’t have to supply it when you call the function! You can supply none, some, or all, as the situation might dictate: >>> hello_3() Hello, world! >>> hello_3('Greetings') Greetings, world! >>> hello_3('Greetings', 'universe') Greetings, universe! As you can see, this works well with positional parameters, except that you have to supply the greeting if you want to supply the name. What if you want to supply only the name, leaving the default value for the greeting? I’m sure you’ve guessed it by now: >>> hello_3(name='Gumby') Hello, Gumby! Pretty nifty, huh? And that’s not all. You can combine positional and keyword parameters. The only requirement is that all the positional parameters come first. If they don’t, the interpreter won’t know which ones they are (that is, which position they are supposed to have). ■Note Unless you know what you’re doing, you might want to avoid such mixing. It is generally used when you have a small number of mandatory parameters and many modifying parameters with default values. For example, our hello function might require a name, but allow us to (optionally) specify the greeting and the punctuation: def hello_4(name, greeting='Hello', punctuation='!'): print '%s, %s%s' % (greeting, name, punctuation) This function can be called in many ways. Here are some of them:
- Page 100 and 101: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 102 and 103: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 104 and 105: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 106 and 107: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 108 and 109: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 110: CHAPTER 4 ■ DICTIONARIES: WHEN IN
- Page 113 and 114: 82 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 115 and 116: 84 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 117 and 118: 86 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 119 and 120: 88 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 121 and 122: 90 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 123 and 124: 92 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 125 and 126: 94 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 127 and 128: 96 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 129 and 130: 98 CHAPTER 5 ■ CONDITIONALS, LOOP
- Page 131 and 132: 100 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 133 and 134: 102 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 135 and 136: 104 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 137 and 138: 106 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 139 and 140: 108 CHAPTER 5 ■ CONDITIONALS, LOO
- Page 141 and 142: 110 CHAPTER 6 ■ ABSTRACTION But w
- Page 143 and 144: 112 CHAPTER 6 ■ ABSTRACTION ■Ti
- Page 145 and 146: 114 CHAPTER 6 ■ ABSTRACTION Can I
- Page 147 and 148: 116 CHAPTER 6 ■ ABSTRACTION stora
- Page 149: 118 CHAPTER 6 ■ ABSTRACTION 4. Yo
- Page 153 and 154: 122 CHAPTER 6 ■ ABSTRACTION >>> p
- Page 155 and 156: 124 CHAPTER 6 ■ ABSTRACTION Also,
- Page 157 and 158: 126 CHAPTER 6 ■ ABSTRACTION Feel
- Page 159 and 160: 128 CHAPTER 6 ■ ABSTRACTION ■No
- Page 161 and 162: 130 CHAPTER 6 ■ ABSTRACTION So yo
- Page 163 and 164: 132 CHAPTER 6 ■ ABSTRACTION numer
- Page 165 and 166: 134 CHAPTER 6 ■ ABSTRACTION LAMBD
- Page 167 and 168: 136 CHAPTER 6 ■ ABSTRACTION In th
- Page 169 and 170: 138 CHAPTER 6 ■ ABSTRACTION Scope
- Page 171 and 172: 140 CHAPTER 7 ■ MORE ABSTRACTION
- Page 173 and 174: 142 CHAPTER 7 ■ MORE ABSTRACTION
- Page 175 and 176: 144 CHAPTER 7 ■ MORE ABSTRACTION
- Page 177 and 178: 146 CHAPTER 7 ■ MORE ABSTRACTION
- Page 179 and 180: 148 CHAPTER 7 ■ MORE ABSTRACTION
- Page 181 and 182: 150 CHAPTER 7 ■ MORE ABSTRACTION
- Page 183 and 184: 152 CHAPTER 7 ■ MORE ABSTRACTION
- Page 185 and 186: 154 CHAPTER 7 ■ MORE ABSTRACTION
- Page 187 and 188: 156 CHAPTER 7 ■ MORE ABSTRACTION
- Page 189 and 190: 158 CHAPTER 7 ■ MORE ABSTRACTION
- Page 191 and 192: 160 CHAPTER 8 ■ EXCEPTIONS >>> im
- Page 193 and 194: 162 CHAPTER 8 ■ EXCEPTIONS Custom
- Page 195 and 196: 164 CHAPTER 8 ■ EXCEPTIONS More T
- Page 197 and 198: 166 CHAPTER 8 ■ EXCEPTIONS try: x
- Page 199 and 200: 168 CHAPTER 8 ■ EXCEPTIONS Invali
120 CHAPTER 6 ■ ABSTRACTION<br />
The parameters that are supplied with a name like this are called keyword parameters. On<br />
their own, the key strength of keyword parameters is that they can help clarify the role of each<br />
parameter. Instead of having <strong>to</strong> use some odd and mysterious call like<br />
>>> s<strong>to</strong>re('Mr. Brainsample', 10, 20, 13, 5)<br />
you could use<br />
>>> s<strong>to</strong>re(patient='Mr. Brainsample', hour=10, minute=20, day=13, month=5)<br />
Even though it takes a bit more typing, it is absolutely clear what each parameter does.<br />
Also, if you get the order mixed up, it doesn’t matter.<br />
What really makes keyword arguments rock, however, is that you can give the parameters<br />
in the function default values:<br />
def hello_3(greeting='Hello', name='world'):<br />
print '%s, %s!' % (greeting, name)<br />
When a parameter has a default value like this, you don’t have <strong>to</strong> supply it when you call<br />
the function! You can supply none, some, or all, as the situation might dictate:<br />
>>> hello_3()<br />
Hello, world!<br />
>>> hello_3('Greetings')<br />
Greetings, world!<br />
>>> hello_3('Greetings', 'universe')<br />
Greetings, universe!<br />
As you can see, this works well with positional parameters, except that you have <strong>to</strong> supply<br />
the greeting if you want <strong>to</strong> supply the name. What if you want <strong>to</strong> supply only the name, leaving<br />
the default value for the greeting? I’m sure you’ve guessed it by now:<br />
>>> hello_3(name='Gumby')<br />
Hello, Gumby!<br />
Pretty nifty, huh? And that’s not all. You can combine positional and keyword parameters.<br />
The only requirement is that all the positional parameters come first. If they don’t, the interpreter<br />
won’t know which ones they are (that is, which position they are supposed <strong>to</strong> have).<br />
■Note Unless you know what you’re doing, you might want <strong>to</strong> avoid such mixing. It is generally used when<br />
you have a small number of manda<strong>to</strong>ry parameters and many modifying parameters with default values.<br />
For example, our hello function might require a name, but allow us <strong>to</strong> (optionally) specify<br />
the greeting and the punctuation:<br />
def hello_4(name, greeting='Hello', punctuation='!'):<br />
print '%s, %s%s' % (greeting, name, punctuation)<br />
This function can be called in many ways. Here are some of them: