|
18 | 18 | "# functions" |
19 | 19 | ] |
20 | 20 | }, |
| 21 | + { |
| 22 | + "cell_type": "raw", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "Functions are used to organize program flow, especially to allow us to easily do commonly needed tasks over and over again. We've already used a lot of functions, such as those that work on lists (`append()` and `pop()`) or strings (like `replace()`). Here we see how to write our own functions" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "markdown", |
| 30 | + "metadata": {}, |
| 31 | + "source": [ |
| 32 | + "A function takes arguments, listed in the `()` and returns a value. Even if you don't explictly give a return value, one will be return (e.g., `None`). \n", |
| 33 | + "\n", |
| 34 | + "Here's a simple example of a function that takes a single argument, `i`" |
| 35 | + ] |
| 36 | + }, |
21 | 37 | { |
22 | 38 | "cell_type": "code", |
23 | 39 | "execution_count": 2, |
|
46 | 62 | "cell_type": "markdown", |
47 | 63 | "metadata": {}, |
48 | 64 | "source": [ |
49 | | - "functions are one place where scope comes into play. A function has its own namespace. If a variable is not defined in that function, then it will look to the namespace from where it was called to see if that variable exists there. " |
| 65 | + "functions are one place where _scope_ comes into play. A function has its own _namespace_. If a variable is not defined in that function, then it will look to the namespace from where it was called to see if that variable exists there. \n", |
| 66 | + "\n", |
| 67 | + "However, you should avoid this as much as possible (variables that persist across namespaces are called global variables)." |
50 | 68 | ] |
51 | 69 | }, |
52 | 70 | { |
|
112 | 130 | "cell_type": "markdown", |
113 | 131 | "metadata": {}, |
114 | 132 | "source": [ |
115 | | - "functions always return a value -- if one is not explicitly given, then they return None, otherwise, they can return values (even multiple values) of any type" |
| 133 | + "By default, python will let you read from a global, but not update it." |
116 | 134 | ] |
117 | 135 | }, |
118 | 136 | { |
|
121 | 139 | "metadata": { |
122 | 140 | "collapsed": false |
123 | 141 | }, |
| 142 | + "outputs": [ |
| 143 | + { |
| 144 | + "name": "stdout", |
| 145 | + "output_type": "stream", |
| 146 | + "text": [ |
| 147 | + "in function outer = -100.0\n", |
| 148 | + "outside, outer = 1.0\n" |
| 149 | + ] |
| 150 | + } |
| 151 | + ], |
| 152 | + "source": [ |
| 153 | + "outer = 1.0\n", |
| 154 | + "\n", |
| 155 | + "def update():\n", |
| 156 | + " # uncomment this to allow us to access outer in the calling namespace\n", |
| 157 | + " # global outer\n", |
| 158 | + " outer = -100.0\n", |
| 159 | + " print(\"in function outer = {}\".format(outer))\n", |
| 160 | + " \n", |
| 161 | + "update()\n", |
| 162 | + "print(\"outside, outer = {}\".format(outer))" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "markdown", |
| 167 | + "metadata": {}, |
| 168 | + "source": [ |
| 169 | + "functions always return a value -- if one is not explicitly given, then they return None, otherwise, they can return values (even multiple values) of any type" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "code", |
| 174 | + "execution_count": 7, |
| 175 | + "metadata": { |
| 176 | + "collapsed": false |
| 177 | + }, |
124 | 178 | "outputs": [ |
125 | 179 | { |
126 | 180 | "name": "stdout", |
|
136 | 190 | "print(a)" |
137 | 191 | ] |
138 | 192 | }, |
| 193 | + { |
| 194 | + "cell_type": "markdown", |
| 195 | + "metadata": {}, |
| 196 | + "source": [ |
| 197 | + "None is a special quantity in python (analogous to `null` in some other languages). We can test on `None` -- the preferred manner is to use `is`:" |
| 198 | + ] |
| 199 | + }, |
139 | 200 | { |
140 | 201 | "cell_type": "code", |
141 | | - "execution_count": 7, |
| 202 | + "execution_count": 8, |
| 203 | + "metadata": { |
| 204 | + "collapsed": false |
| 205 | + }, |
| 206 | + "outputs": [ |
| 207 | + { |
| 208 | + "name": "stdout", |
| 209 | + "output_type": "stream", |
| 210 | + "text": [ |
| 211 | + "we didn't do anything\n" |
| 212 | + ] |
| 213 | + } |
| 214 | + ], |
| 215 | + "source": [ |
| 216 | + "def do_nothing():\n", |
| 217 | + " pass\n", |
| 218 | + "\n", |
| 219 | + "a = do_nothing()\n", |
| 220 | + "if a is None:\n", |
| 221 | + " print(\"we didn't do anything\")" |
| 222 | + ] |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "markdown", |
| 226 | + "metadata": {}, |
| 227 | + "source": [ |
| 228 | + "## More Complex Functions\n", |
| 229 | + "\n", |
| 230 | + "Here's a more complex example. We return a pair of variables -- in python this is done by packing them into a tuple and then unpacking on the calling end. Also note the _docstring_ here." |
| 231 | + ] |
| 232 | + }, |
| 233 | + { |
| 234 | + "cell_type": "code", |
| 235 | + "execution_count": 9, |
142 | 236 | "metadata": { |
143 | 237 | "collapsed": false |
144 | 238 | }, |
|
162 | 256 | " a, b = b, a+b\n", |
163 | 257 | " return result, len(result)\n", |
164 | 258 | "\n", |
165 | | - "list, n = fib2(250)\n", |
| 259 | + "fib, n = fib2(250)\n", |
166 | 260 | "print(n)\n", |
167 | | - "print(list)" |
| 261 | + "print(fib)" |
168 | 262 | ] |
169 | 263 | }, |
170 | 264 | { |
|
176 | 270 | }, |
177 | 271 | { |
178 | 272 | "cell_type": "code", |
179 | | - "execution_count": 8, |
| 273 | + "execution_count": 10, |
180 | 274 | "metadata": { |
181 | 275 | "collapsed": false |
182 | 276 | }, |
|
206 | 300 | }, |
207 | 301 | { |
208 | 302 | "cell_type": "code", |
209 | | - "execution_count": 9, |
| 303 | + "execution_count": 11, |
210 | 304 | "metadata": { |
211 | 305 | "collapsed": false |
212 | 306 | }, |
|
237 | 331 | "source": [ |
238 | 332 | "it is important to note that python evaluates the optional arguments once--when the function is defined. This means that if you make the default an empty object, for instance, it will persist across all calls.\n", |
239 | 333 | "\n", |
240 | | - "** This is one of the most common errors for beginners **" |
| 334 | + "** This is one of the most common errors for beginners **\n", |
| 335 | + "\n", |
| 336 | + "Here's an example of trying to initialize to an empty list:" |
241 | 337 | ] |
242 | 338 | }, |
243 | 339 | { |
244 | 340 | "cell_type": "code", |
245 | | - "execution_count": 10, |
| 341 | + "execution_count": 12, |
246 | 342 | "metadata": { |
247 | 343 | "collapsed": false |
248 | 344 | }, |
|
271 | 367 | "cell_type": "markdown", |
272 | 368 | "metadata": {}, |
273 | 369 | "source": [ |
274 | | - "instead do" |
| 370 | + "Notice that each call does not create its own separate list. Instead a single empty list was created when the function was first processed, and this list persists in memory as the default value for the optional argument `L`. \n", |
| 371 | + "\n", |
| 372 | + "If we want a unique list created each time (e.g., a separate place in memory), we instead do" |
275 | 373 | ] |
276 | 374 | }, |
277 | 375 | { |
278 | 376 | "cell_type": "code", |
279 | | - "execution_count": 11, |
| 377 | + "execution_count": 13, |
280 | 378 | "metadata": { |
281 | 379 | "collapsed": false |
282 | 380 | }, |
|
303 | 401 | "print(fnew(3))" |
304 | 402 | ] |
305 | 403 | }, |
| 404 | + { |
| 405 | + "cell_type": "markdown", |
| 406 | + "metadata": {}, |
| 407 | + "source": [ |
| 408 | + "Notice that the same `None` that we saw previously comes into play here. " |
| 409 | + ] |
| 410 | + }, |
306 | 411 | { |
307 | 412 | "cell_type": "markdown", |
308 | 413 | "metadata": { |
|
318 | 423 | "source": [ |
319 | 424 | "Lambdas are \"disposible\" functions. These are small, nameless functions that are often used as arguments in other functions.\n", |
320 | 425 | "\n", |
321 | | - "Ex, from the official tutorial:" |
| 426 | + "Ex, from the official tutorial: we have a list of tuples. We want to sort the list based on the second item in the tuple. The `sort` method can take a `key` optional argument that tells us how to interpret the list item for sorting" |
322 | 427 | ] |
323 | 428 | }, |
324 | 429 | { |
325 | 430 | "cell_type": "code", |
326 | | - "execution_count": 3, |
| 431 | + "execution_count": 14, |
327 | 432 | "metadata": { |
328 | 433 | "collapsed": true |
329 | 434 | }, |
|
335 | 440 | }, |
336 | 441 | { |
337 | 442 | "cell_type": "code", |
338 | | - "execution_count": 4, |
| 443 | + "execution_count": 15, |
339 | 444 | "metadata": { |
340 | 445 | "collapsed": false |
341 | 446 | }, |
|
346 | 451 | "[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]" |
347 | 452 | ] |
348 | 453 | }, |
349 | | - "execution_count": 4, |
| 454 | + "execution_count": 15, |
350 | 455 | "metadata": {}, |
351 | 456 | "output_type": "execute_result" |
352 | 457 | } |
|
364 | 469 | }, |
365 | 470 | { |
366 | 471 | "cell_type": "code", |
367 | | - "execution_count": 12, |
| 472 | + "execution_count": 16, |
368 | 473 | "metadata": { |
369 | 474 | "collapsed": false |
370 | 475 | }, |
|
391 | 496 | " 9216]" |
392 | 497 | ] |
393 | 498 | }, |
394 | | - "execution_count": 12, |
| 499 | + "execution_count": 16, |
395 | 500 | "metadata": {}, |
396 | 501 | "output_type": "execute_result" |
397 | 502 | } |
|
437 | 542 | "name": "python", |
438 | 543 | "nbconvert_exporter": "python", |
439 | 544 | "pygments_lexer": "ipython3", |
440 | | - "version": "3.4.3" |
| 545 | + "version": "3.5.2" |
441 | 546 | } |
442 | 547 | }, |
443 | 548 | "nbformat": 4, |
|
0 commit comments