Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | .. _tut-morecontrol: |
| 2 | |
| 3 | *********************** |
| 4 | More Control Flow Tools |
| 5 | *********************** |
| 6 | |
| 7 | Besides the :keyword:`while` statement just introduced, Python knows the usual |
| 8 | control flow statements known from other languages, with some twists. |
| 9 | |
| 10 | |
| 11 | .. _tut-if: |
| 12 | |
| 13 | :keyword:`if` Statements |
| 14 | ======================== |
| 15 | |
| 16 | Perhaps the most well-known statement type is the :keyword:`if` statement. For |
| 17 | example:: |
| 18 | |
| 19 | >>> x = int(raw_input("Please enter an integer: ")) |
| 20 | >>> if x < 0: |
| 21 | ... x = 0 |
| 22 | ... print 'Negative changed to zero' |
| 23 | ... elif x == 0: |
| 24 | ... print 'Zero' |
| 25 | ... elif x == 1: |
| 26 | ... print 'Single' |
| 27 | ... else: |
| 28 | ... print 'More' |
| 29 | ... |
| 30 | |
| 31 | There can be zero or more :keyword:`elif` parts, and the :keyword:`else` part is |
| 32 | optional. The keyword ':keyword:`elif`' is short for 'else if', and is useful |
| 33 | to avoid excessive indentation. An :keyword:`if` ... :keyword:`elif` ... |
| 34 | :keyword:`elif` ... sequence is a substitute for the :keyword:`switch` or |
| 35 | :keyword:`case` statements found in other languages. |
| 36 | |
| 37 | .. % Weird spacings happen here if the wrapping of the source text |
| 38 | .. % gets changed in the wrong way. |
| 39 | |
| 40 | |
| 41 | .. _tut-for: |
| 42 | |
| 43 | :keyword:`for` Statements |
| 44 | ========================= |
| 45 | |
| 46 | .. index:: |
| 47 | statement: for |
| 48 | statement: for |
| 49 | |
| 50 | The :keyword:`for` statement in Python differs a bit from what you may be used |
| 51 | to in C or Pascal. Rather than always iterating over an arithmetic progression |
| 52 | of numbers (like in Pascal), or giving the user the ability to define both the |
| 53 | iteration step and halting condition (as C), Python's :keyword:`for` statement |
| 54 | iterates over the items of any sequence (a list or a string), in the order that |
| 55 | they appear in the sequence. For example (no pun intended): |
| 56 | |
| 57 | .. % One suggestion was to give a real C example here, but that may only |
| 58 | .. % serve to confuse non-C programmers. |
| 59 | |
| 60 | :: |
| 61 | |
| 62 | >>> # Measure some strings: |
| 63 | ... a = ['cat', 'window', 'defenestrate'] |
| 64 | >>> for x in a: |
| 65 | ... print x, len(x) |
| 66 | ... |
| 67 | cat 3 |
| 68 | window 6 |
| 69 | defenestrate 12 |
| 70 | |
| 71 | It is not safe to modify the sequence being iterated over in the loop (this can |
| 72 | only happen for mutable sequence types, such as lists). If you need to modify |
| 73 | the list you are iterating over (for example, to duplicate selected items) you |
| 74 | must iterate over a copy. The slice notation makes this particularly |
| 75 | convenient:: |
| 76 | |
| 77 | >>> for x in a[:]: # make a slice copy of the entire list |
| 78 | ... if len(x) > 6: a.insert(0, x) |
| 79 | ... |
| 80 | >>> a |
| 81 | ['defenestrate', 'cat', 'window', 'defenestrate'] |
| 82 | |
| 83 | |
| 84 | .. _tut-range: |
| 85 | |
| 86 | The :func:`range` Function |
| 87 | ========================== |
| 88 | |
| 89 | If you do need to iterate over a sequence of numbers, the built-in function |
| 90 | :func:`range` comes in handy. It generates lists containing arithmetic |
| 91 | progressions:: |
| 92 | |
| 93 | >>> range(10) |
| 94 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 95 | |
| 96 | The given end point is never part of the generated list; ``range(10)`` generates |
| 97 | a list of 10 values, the legal indices for items of a sequence of length 10. It |
| 98 | is possible to let the range start at another number, or to specify a different |
| 99 | increment (even negative; sometimes this is called the 'step'):: |
| 100 | |
| 101 | >>> range(5, 10) |
| 102 | [5, 6, 7, 8, 9] |
| 103 | >>> range(0, 10, 3) |
| 104 | [0, 3, 6, 9] |
| 105 | >>> range(-10, -100, -30) |
| 106 | [-10, -40, -70] |
| 107 | |
| 108 | To iterate over the indices of a sequence, combine :func:`range` and :func:`len` |
| 109 | as follows:: |
| 110 | |
| 111 | >>> a = ['Mary', 'had', 'a', 'little', 'lamb'] |
| 112 | >>> for i in range(len(a)): |
| 113 | ... print i, a[i] |
| 114 | ... |
| 115 | 0 Mary |
| 116 | 1 had |
| 117 | 2 a |
| 118 | 3 little |
| 119 | 4 lamb |
| 120 | |
| 121 | |
| 122 | .. _tut-break: |
| 123 | |
| 124 | :keyword:`break` and :keyword:`continue` Statements, and :keyword:`else` Clauses on Loops |
| 125 | ========================================================================================= |
| 126 | |
| 127 | The :keyword:`break` statement, like in C, breaks out of the smallest enclosing |
| 128 | :keyword:`for` or :keyword:`while` loop. |
| 129 | |
| 130 | The :keyword:`continue` statement, also borrowed from C, continues with the next |
| 131 | iteration of the loop. |
| 132 | |
| 133 | Loop statements may have an ``else`` clause; it is executed when the loop |
| 134 | terminates through exhaustion of the list (with :keyword:`for`) or when the |
| 135 | condition becomes false (with :keyword:`while`), but not when the loop is |
| 136 | terminated by a :keyword:`break` statement. This is exemplified by the |
| 137 | following loop, which searches for prime numbers:: |
| 138 | |
| 139 | >>> for n in range(2, 10): |
| 140 | ... for x in range(2, n): |
| 141 | ... if n % x == 0: |
| 142 | ... print n, 'equals', x, '*', n/x |
| 143 | ... break |
| 144 | ... else: |
| 145 | ... # loop fell through without finding a factor |
| 146 | ... print n, 'is a prime number' |
| 147 | ... |
| 148 | 2 is a prime number |
| 149 | 3 is a prime number |
| 150 | 4 equals 2 * 2 |
| 151 | 5 is a prime number |
| 152 | 6 equals 2 * 3 |
| 153 | 7 is a prime number |
| 154 | 8 equals 2 * 4 |
| 155 | 9 equals 3 * 3 |
| 156 | |
| 157 | |
| 158 | .. _tut-pass: |
| 159 | |
| 160 | :keyword:`pass` Statements |
| 161 | ========================== |
| 162 | |
| 163 | The :keyword:`pass` statement does nothing. It can be used when a statement is |
| 164 | required syntactically but the program requires no action. For example:: |
| 165 | |
| 166 | >>> while True: |
| 167 | ... pass # Busy-wait for keyboard interrupt |
| 168 | ... |
| 169 | |
| 170 | |
| 171 | .. _tut-functions: |
| 172 | |
| 173 | Defining Functions |
| 174 | ================== |
| 175 | |
| 176 | We can create a function that writes the Fibonacci series to an arbitrary |
| 177 | boundary:: |
| 178 | |
| 179 | >>> def fib(n): # write Fibonacci series up to n |
| 180 | ... """Print a Fibonacci series up to n.""" |
| 181 | ... a, b = 0, 1 |
| 182 | ... while b < n: |
| 183 | ... print b, |
| 184 | ... a, b = b, a+b |
| 185 | ... |
| 186 | >>> # Now call the function we just defined: |
| 187 | ... fib(2000) |
| 188 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 |
| 189 | |
| 190 | .. index:: |
| 191 | single: documentation strings |
| 192 | single: docstrings |
| 193 | single: strings, documentation |
| 194 | |
| 195 | The keyword :keyword:`def` introduces a function *definition*. It must be |
| 196 | followed by the function name and the parenthesized list of formal parameters. |
| 197 | The statements that form the body of the function start at the next line, and |
| 198 | must be indented. The first statement of the function body can optionally be a |
| 199 | string literal; this string literal is the function's documentation string, or |
| 200 | :dfn:`docstring`. |
| 201 | |
| 202 | There are tools which use docstrings to automatically produce online or printed |
| 203 | documentation, or to let the user interactively browse through code; it's good |
| 204 | practice to include docstrings in code that you write, so try to make a habit of |
| 205 | it. |
| 206 | |
| 207 | The *execution* of a function introduces a new symbol table used for the local |
| 208 | variables of the function. More precisely, all variable assignments in a |
| 209 | function store the value in the local symbol table; whereas variable references |
| 210 | first look in the local symbol table, then in the global symbol table, and then |
| 211 | in the table of built-in names. Thus, global variables cannot be directly |
| 212 | assigned a value within a function (unless named in a :keyword:`global` |
| 213 | statement), although they may be referenced. |
| 214 | |
| 215 | The actual parameters (arguments) to a function call are introduced in the local |
| 216 | symbol table of the called function when it is called; thus, arguments are |
| 217 | passed using *call by value* (where the *value* is always an object *reference*, |
| 218 | not the value of the object). [#]_ When a function calls another function, a new |
| 219 | local symbol table is created for that call. |
| 220 | |
| 221 | A function definition introduces the function name in the current symbol table. |
| 222 | The value of the function name has a type that is recognized by the interpreter |
| 223 | as a user-defined function. This value can be assigned to another name which |
| 224 | can then also be used as a function. This serves as a general renaming |
| 225 | mechanism:: |
| 226 | |
| 227 | >>> fib |
| 228 | <function fib at 10042ed0> |
| 229 | >>> f = fib |
| 230 | >>> f(100) |
| 231 | 1 1 2 3 5 8 13 21 34 55 89 |
| 232 | |
| 233 | You might object that ``fib`` is not a function but a procedure. In Python, |
| 234 | like in C, procedures are just functions that don't return a value. In fact, |
| 235 | technically speaking, procedures do return a value, albeit a rather boring one. |
| 236 | This value is called ``None`` (it's a built-in name). Writing the value |
| 237 | ``None`` is normally suppressed by the interpreter if it would be the only value |
| 238 | written. You can see it if you really want to:: |
| 239 | |
| 240 | >>> print fib(0) |
| 241 | None |
| 242 | |
| 243 | It is simple to write a function that returns a list of the numbers of the |
| 244 | Fibonacci series, instead of printing it:: |
| 245 | |
| 246 | >>> def fib2(n): # return Fibonacci series up to n |
| 247 | ... """Return a list containing the Fibonacci series up to n.""" |
| 248 | ... result = [] |
| 249 | ... a, b = 0, 1 |
| 250 | ... while b < n: |
| 251 | ... result.append(b) # see below |
| 252 | ... a, b = b, a+b |
| 253 | ... return result |
| 254 | ... |
| 255 | >>> f100 = fib2(100) # call it |
| 256 | >>> f100 # write the result |
| 257 | [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |
| 258 | |
| 259 | This example, as usual, demonstrates some new Python features: |
| 260 | |
| 261 | * The :keyword:`return` statement returns with a value from a function. |
| 262 | :keyword:`return` without an expression argument returns ``None``. Falling off |
| 263 | the end of a procedure also returns ``None``. |
| 264 | |
| 265 | * The statement ``result.append(b)`` calls a *method* of the list object |
| 266 | ``result``. A method is a function that 'belongs' to an object and is named |
| 267 | ``obj.methodname``, where ``obj`` is some object (this may be an expression), |
| 268 | and ``methodname`` is the name of a method that is defined by the object's type. |
| 269 | Different types define different methods. Methods of different types may have |
| 270 | the same name without causing ambiguity. (It is possible to define your own |
| 271 | object types and methods, using *classes*, as discussed later in this tutorial.) |
| 272 | The method :meth:`append` shown in the example is defined for list objects; it |
| 273 | adds a new element at the end of the list. In this example it is equivalent to |
| 274 | ``result = result + [b]``, but more efficient. |
| 275 | |
| 276 | |
| 277 | .. _tut-defining: |
| 278 | |
| 279 | More on Defining Functions |
| 280 | ========================== |
| 281 | |
| 282 | It is also possible to define functions with a variable number of arguments. |
| 283 | There are three forms, which can be combined. |
| 284 | |
| 285 | |
| 286 | .. _tut-defaultargs: |
| 287 | |
| 288 | Default Argument Values |
| 289 | ----------------------- |
| 290 | |
| 291 | The most useful form is to specify a default value for one or more arguments. |
| 292 | This creates a function that can be called with fewer arguments than it is |
| 293 | defined to allow. For example:: |
| 294 | |
| 295 | def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): |
| 296 | while True: |
| 297 | ok = raw_input(prompt) |
| 298 | if ok in ('y', 'ye', 'yes'): return True |
| 299 | if ok in ('n', 'no', 'nop', 'nope'): return False |
| 300 | retries = retries - 1 |
| 301 | if retries < 0: raise IOError, 'refusenik user' |
| 302 | print complaint |
| 303 | |
| 304 | This function can be called either like this: ``ask_ok('Do you really want to |
| 305 | quit?')`` or like this: ``ask_ok('OK to overwrite the file?', 2)``. |
| 306 | |
| 307 | This example also introduces the :keyword:`in` keyword. This tests whether or |
| 308 | not a sequence contains a certain value. |
| 309 | |
| 310 | The default values are evaluated at the point of function definition in the |
| 311 | *defining* scope, so that :: |
| 312 | |
| 313 | i = 5 |
| 314 | |
| 315 | def f(arg=i): |
| 316 | print arg |
| 317 | |
| 318 | i = 6 |
| 319 | f() |
| 320 | |
| 321 | will print ``5``. |
| 322 | |
| 323 | **Important warning:** The default value is evaluated only once. This makes a |
| 324 | difference when the default is a mutable object such as a list, dictionary, or |
| 325 | instances of most classes. For example, the following function accumulates the |
| 326 | arguments passed to it on subsequent calls:: |
| 327 | |
| 328 | def f(a, L=[]): |
| 329 | L.append(a) |
| 330 | return L |
| 331 | |
| 332 | print f(1) |
| 333 | print f(2) |
| 334 | print f(3) |
| 335 | |
| 336 | This will print :: |
| 337 | |
| 338 | [1] |
| 339 | [1, 2] |
| 340 | [1, 2, 3] |
| 341 | |
| 342 | If you don't want the default to be shared between subsequent calls, you can |
| 343 | write the function like this instead:: |
| 344 | |
| 345 | def f(a, L=None): |
| 346 | if L is None: |
| 347 | L = [] |
| 348 | L.append(a) |
| 349 | return L |
| 350 | |
| 351 | |
| 352 | .. _tut-keywordargs: |
| 353 | |
| 354 | Keyword Arguments |
| 355 | ----------------- |
| 356 | |
| 357 | Functions can also be called using keyword arguments of the form ``keyword = |
| 358 | value``. For instance, the following function:: |
| 359 | |
| 360 | def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'): |
| 361 | print "-- This parrot wouldn't", action, |
| 362 | print "if you put", voltage, "volts through it." |
| 363 | print "-- Lovely plumage, the", type |
| 364 | print "-- It's", state, "!" |
| 365 | |
| 366 | could be called in any of the following ways:: |
| 367 | |
| 368 | parrot(1000) |
| 369 | parrot(action = 'VOOOOOM', voltage = 1000000) |
| 370 | parrot('a thousand', state = 'pushing up the daisies') |
| 371 | parrot('a million', 'bereft of life', 'jump') |
| 372 | |
| 373 | but the following calls would all be invalid:: |
| 374 | |
| 375 | parrot() # required argument missing |
| 376 | parrot(voltage=5.0, 'dead') # non-keyword argument following keyword |
| 377 | parrot(110, voltage=220) # duplicate value for argument |
| 378 | parrot(actor='John Cleese') # unknown keyword |
| 379 | |
| 380 | In general, an argument list must have any positional arguments followed by any |
| 381 | keyword arguments, where the keywords must be chosen from the formal parameter |
| 382 | names. It's not important whether a formal parameter has a default value or |
| 383 | not. No argument may receive a value more than once --- formal parameter names |
| 384 | corresponding to positional arguments cannot be used as keywords in the same |
| 385 | calls. Here's an example that fails due to this restriction:: |
| 386 | |
| 387 | >>> def function(a): |
| 388 | ... pass |
| 389 | ... |
| 390 | >>> function(0, a=0) |
| 391 | Traceback (most recent call last): |
| 392 | File "<stdin>", line 1, in ? |
| 393 | TypeError: function() got multiple values for keyword argument 'a' |
| 394 | |
| 395 | When a final formal parameter of the form ``**name`` is present, it receives a |
| 396 | dictionary (see :ref:`typesmapping`) containing all keyword arguments except for |
| 397 | those corresponding to a formal parameter. This may be combined with a formal |
| 398 | parameter of the form ``*name`` (described in the next subsection) which |
| 399 | receives a tuple containing the positional arguments beyond the formal parameter |
| 400 | list. (``*name`` must occur before ``**name``.) For example, if we define a |
| 401 | function like this:: |
| 402 | |
| 403 | def cheeseshop(kind, *arguments, **keywords): |
| 404 | print "-- Do you have any", kind, '?' |
| 405 | print "-- I'm sorry, we're all out of", kind |
| 406 | for arg in arguments: print arg |
| 407 | print '-'*40 |
| 408 | keys = keywords.keys() |
| 409 | keys.sort() |
| 410 | for kw in keys: print kw, ':', keywords[kw] |
| 411 | |
| 412 | It could be called like this:: |
| 413 | |
| 414 | cheeseshop('Limburger', "It's very runny, sir.", |
| 415 | "It's really very, VERY runny, sir.", |
| 416 | client='John Cleese', |
| 417 | shopkeeper='Michael Palin', |
| 418 | sketch='Cheese Shop Sketch') |
| 419 | |
| 420 | and of course it would print:: |
| 421 | |
| 422 | -- Do you have any Limburger ? |
| 423 | -- I'm sorry, we're all out of Limburger |
| 424 | It's very runny, sir. |
| 425 | It's really very, VERY runny, sir. |
| 426 | ---------------------------------------- |
| 427 | client : John Cleese |
| 428 | shopkeeper : Michael Palin |
| 429 | sketch : Cheese Shop Sketch |
| 430 | |
| 431 | Note that the :meth:`sort` method of the list of keyword argument names is |
| 432 | called before printing the contents of the ``keywords`` dictionary; if this is |
| 433 | not done, the order in which the arguments are printed is undefined. |
| 434 | |
| 435 | |
| 436 | .. _tut-arbitraryargs: |
| 437 | |
| 438 | Arbitrary Argument Lists |
| 439 | ------------------------ |
| 440 | |
| 441 | Finally, the least frequently used option is to specify that a function can be |
| 442 | called with an arbitrary number of arguments. These arguments will be wrapped |
| 443 | up in a tuple. Before the variable number of arguments, zero or more normal |
| 444 | arguments may occur. :: |
| 445 | |
| 446 | def fprintf(file, format, *args): |
| 447 | file.write(format % args) |
| 448 | |
| 449 | |
| 450 | .. _tut-unpacking-arguments: |
| 451 | |
| 452 | Unpacking Argument Lists |
| 453 | ------------------------ |
| 454 | |
| 455 | The reverse situation occurs when the arguments are already in a list or tuple |
| 456 | but need to be unpacked for a function call requiring separate positional |
| 457 | arguments. For instance, the built-in :func:`range` function expects separate |
| 458 | *start* and *stop* arguments. If they are not available separately, write the |
| 459 | function call with the ``*``\ -operator to unpack the arguments out of a list |
| 460 | or tuple:: |
| 461 | |
| 462 | >>> range(3, 6) # normal call with separate arguments |
| 463 | [3, 4, 5] |
| 464 | >>> args = [3, 6] |
| 465 | >>> range(*args) # call with arguments unpacked from a list |
| 466 | [3, 4, 5] |
| 467 | |
| 468 | In the same fashion, dictionaries can deliver keyword arguments with the ``**``\ |
| 469 | -operator:: |
| 470 | |
| 471 | >>> def parrot(voltage, state='a stiff', action='voom'): |
| 472 | ... print "-- This parrot wouldn't", action, |
| 473 | ... print "if you put", voltage, "volts through it.", |
| 474 | ... print "E's", state, "!" |
| 475 | ... |
| 476 | >>> d = {"voltage": "four million", "state": "bleedin' demised", "action": "VOOM"} |
| 477 | >>> parrot(**d) |
| 478 | -- This parrot wouldn't VOOM if you put four million volts through it. E's bleedin' demised ! |
| 479 | |
| 480 | |
| 481 | .. _tut-lambda: |
| 482 | |
| 483 | Lambda Forms |
| 484 | ------------ |
| 485 | |
| 486 | By popular demand, a few features commonly found in functional programming |
| 487 | languages like Lisp have been added to Python. With the :keyword:`lambda` |
| 488 | keyword, small anonymous functions can be created. Here's a function that |
| 489 | returns the sum of its two arguments: ``lambda a, b: a+b``. Lambda forms can be |
| 490 | used wherever function objects are required. They are syntactically restricted |
| 491 | to a single expression. Semantically, they are just syntactic sugar for a |
| 492 | normal function definition. Like nested function definitions, lambda forms can |
| 493 | reference variables from the containing scope:: |
| 494 | |
| 495 | >>> def make_incrementor(n): |
| 496 | ... return lambda x: x + n |
| 497 | ... |
| 498 | >>> f = make_incrementor(42) |
| 499 | >>> f(0) |
| 500 | 42 |
| 501 | >>> f(1) |
| 502 | 43 |
| 503 | |
| 504 | |
| 505 | .. _tut-docstrings: |
| 506 | |
| 507 | Documentation Strings |
| 508 | --------------------- |
| 509 | |
| 510 | .. index:: |
| 511 | single: docstrings |
| 512 | single: documentation strings |
| 513 | single: strings, documentation |
| 514 | |
| 515 | There are emerging conventions about the content and formatting of documentation |
| 516 | strings. |
| 517 | |
| 518 | The first line should always be a short, concise summary of the object's |
| 519 | purpose. For brevity, it should not explicitly state the object's name or type, |
| 520 | since these are available by other means (except if the name happens to be a |
| 521 | verb describing a function's operation). This line should begin with a capital |
| 522 | letter and end with a period. |
| 523 | |
| 524 | If there are more lines in the documentation string, the second line should be |
| 525 | blank, visually separating the summary from the rest of the description. The |
| 526 | following lines should be one or more paragraphs describing the object's calling |
| 527 | conventions, its side effects, etc. |
| 528 | |
| 529 | The Python parser does not strip indentation from multi-line string literals in |
| 530 | Python, so tools that process documentation have to strip indentation if |
| 531 | desired. This is done using the following convention. The first non-blank line |
| 532 | *after* the first line of the string determines the amount of indentation for |
| 533 | the entire documentation string. (We can't use the first line since it is |
| 534 | generally adjacent to the string's opening quotes so its indentation is not |
| 535 | apparent in the string literal.) Whitespace "equivalent" to this indentation is |
| 536 | then stripped from the start of all lines of the string. Lines that are |
| 537 | indented less should not occur, but if they occur all their leading whitespace |
| 538 | should be stripped. Equivalence of whitespace should be tested after expansion |
| 539 | of tabs (to 8 spaces, normally). |
| 540 | |
| 541 | Here is an example of a multi-line docstring:: |
| 542 | |
| 543 | >>> def my_function(): |
| 544 | ... """Do nothing, but document it. |
| 545 | ... |
| 546 | ... No, really, it doesn't do anything. |
| 547 | ... """ |
| 548 | ... pass |
| 549 | ... |
| 550 | >>> print my_function.__doc__ |
| 551 | Do nothing, but document it. |
| 552 | |
| 553 | No, really, it doesn't do anything. |
| 554 | |
| 555 | |
| 556 | |
| 557 | .. rubric:: Footnotes |
| 558 | |
| 559 | .. [#] Actually, *call by object reference* would be a better description, since if a |
| 560 | mutable object is passed, the caller will see any changes the callee makes to it |
| 561 | (items inserted into a list). |
| 562 | |