annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/pydoc_data/topics.py @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 # -*- coding: utf-8 -*-
jpayne@69 2 # Autogenerated by Sphinx on Wed Dec 18 18:17:58 2019
jpayne@69 3 topics = {'assert': 'The "assert" statement\n'
jpayne@69 4 '**********************\n'
jpayne@69 5 '\n'
jpayne@69 6 'Assert statements are a convenient way to insert debugging '
jpayne@69 7 'assertions\n'
jpayne@69 8 'into a program:\n'
jpayne@69 9 '\n'
jpayne@69 10 ' assert_stmt ::= "assert" expression ["," expression]\n'
jpayne@69 11 '\n'
jpayne@69 12 'The simple form, "assert expression", is equivalent to\n'
jpayne@69 13 '\n'
jpayne@69 14 ' if __debug__:\n'
jpayne@69 15 ' if not expression: raise AssertionError\n'
jpayne@69 16 '\n'
jpayne@69 17 'The extended form, "assert expression1, expression2", is '
jpayne@69 18 'equivalent to\n'
jpayne@69 19 '\n'
jpayne@69 20 ' if __debug__:\n'
jpayne@69 21 ' if not expression1: raise AssertionError(expression2)\n'
jpayne@69 22 '\n'
jpayne@69 23 'These equivalences assume that "__debug__" and "AssertionError" '
jpayne@69 24 'refer\n'
jpayne@69 25 'to the built-in variables with those names. In the current\n'
jpayne@69 26 'implementation, the built-in variable "__debug__" is "True" under\n'
jpayne@69 27 'normal circumstances, "False" when optimization is requested '
jpayne@69 28 '(command\n'
jpayne@69 29 'line option "-O"). The current code generator emits no code for '
jpayne@69 30 'an\n'
jpayne@69 31 'assert statement when optimization is requested at compile time. '
jpayne@69 32 'Note\n'
jpayne@69 33 'that it is unnecessary to include the source code for the '
jpayne@69 34 'expression\n'
jpayne@69 35 'that failed in the error message; it will be displayed as part of '
jpayne@69 36 'the\n'
jpayne@69 37 'stack trace.\n'
jpayne@69 38 '\n'
jpayne@69 39 'Assignments to "__debug__" are illegal. The value for the '
jpayne@69 40 'built-in\n'
jpayne@69 41 'variable is determined when the interpreter starts.\n',
jpayne@69 42 'assignment': 'Assignment statements\n'
jpayne@69 43 '*********************\n'
jpayne@69 44 '\n'
jpayne@69 45 'Assignment statements are used to (re)bind names to values and '
jpayne@69 46 'to\n'
jpayne@69 47 'modify attributes or items of mutable objects:\n'
jpayne@69 48 '\n'
jpayne@69 49 ' assignment_stmt ::= (target_list "=")+ (starred_expression '
jpayne@69 50 '| yield_expression)\n'
jpayne@69 51 ' target_list ::= target ("," target)* [","]\n'
jpayne@69 52 ' target ::= identifier\n'
jpayne@69 53 ' | "(" [target_list] ")"\n'
jpayne@69 54 ' | "[" [target_list] "]"\n'
jpayne@69 55 ' | attributeref\n'
jpayne@69 56 ' | subscription\n'
jpayne@69 57 ' | slicing\n'
jpayne@69 58 ' | "*" target\n'
jpayne@69 59 '\n'
jpayne@69 60 '(See section Primaries for the syntax definitions for '
jpayne@69 61 '*attributeref*,\n'
jpayne@69 62 '*subscription*, and *slicing*.)\n'
jpayne@69 63 '\n'
jpayne@69 64 'An assignment statement evaluates the expression list '
jpayne@69 65 '(remember that\n'
jpayne@69 66 'this can be a single expression or a comma-separated list, the '
jpayne@69 67 'latter\n'
jpayne@69 68 'yielding a tuple) and assigns the single resulting object to '
jpayne@69 69 'each of\n'
jpayne@69 70 'the target lists, from left to right.\n'
jpayne@69 71 '\n'
jpayne@69 72 'Assignment is defined recursively depending on the form of the '
jpayne@69 73 'target\n'
jpayne@69 74 '(list). When a target is part of a mutable object (an '
jpayne@69 75 'attribute\n'
jpayne@69 76 'reference, subscription or slicing), the mutable object must\n'
jpayne@69 77 'ultimately perform the assignment and decide about its '
jpayne@69 78 'validity, and\n'
jpayne@69 79 'may raise an exception if the assignment is unacceptable. The '
jpayne@69 80 'rules\n'
jpayne@69 81 'observed by various types and the exceptions raised are given '
jpayne@69 82 'with the\n'
jpayne@69 83 'definition of the object types (see section The standard type\n'
jpayne@69 84 'hierarchy).\n'
jpayne@69 85 '\n'
jpayne@69 86 'Assignment of an object to a target list, optionally enclosed '
jpayne@69 87 'in\n'
jpayne@69 88 'parentheses or square brackets, is recursively defined as '
jpayne@69 89 'follows.\n'
jpayne@69 90 '\n'
jpayne@69 91 '* If the target list is a single target with no trailing '
jpayne@69 92 'comma,\n'
jpayne@69 93 ' optionally in parentheses, the object is assigned to that '
jpayne@69 94 'target.\n'
jpayne@69 95 '\n'
jpayne@69 96 '* Else: The object must be an iterable with the same number of '
jpayne@69 97 'items\n'
jpayne@69 98 ' as there are targets in the target list, and the items are '
jpayne@69 99 'assigned,\n'
jpayne@69 100 ' from left to right, to the corresponding targets.\n'
jpayne@69 101 '\n'
jpayne@69 102 ' * If the target list contains one target prefixed with an\n'
jpayne@69 103 ' asterisk, called a “starred” target: The object must be '
jpayne@69 104 'an\n'
jpayne@69 105 ' iterable with at least as many items as there are targets '
jpayne@69 106 'in the\n'
jpayne@69 107 ' target list, minus one. The first items of the iterable '
jpayne@69 108 'are\n'
jpayne@69 109 ' assigned, from left to right, to the targets before the '
jpayne@69 110 'starred\n'
jpayne@69 111 ' target. The final items of the iterable are assigned to '
jpayne@69 112 'the\n'
jpayne@69 113 ' targets after the starred target. A list of the remaining '
jpayne@69 114 'items\n'
jpayne@69 115 ' in the iterable is then assigned to the starred target '
jpayne@69 116 '(the list\n'
jpayne@69 117 ' can be empty).\n'
jpayne@69 118 '\n'
jpayne@69 119 ' * Else: The object must be an iterable with the same number '
jpayne@69 120 'of\n'
jpayne@69 121 ' items as there are targets in the target list, and the '
jpayne@69 122 'items are\n'
jpayne@69 123 ' assigned, from left to right, to the corresponding '
jpayne@69 124 'targets.\n'
jpayne@69 125 '\n'
jpayne@69 126 'Assignment of an object to a single target is recursively '
jpayne@69 127 'defined as\n'
jpayne@69 128 'follows.\n'
jpayne@69 129 '\n'
jpayne@69 130 '* If the target is an identifier (name):\n'
jpayne@69 131 '\n'
jpayne@69 132 ' * If the name does not occur in a "global" or "nonlocal" '
jpayne@69 133 'statement\n'
jpayne@69 134 ' in the current code block: the name is bound to the object '
jpayne@69 135 'in the\n'
jpayne@69 136 ' current local namespace.\n'
jpayne@69 137 '\n'
jpayne@69 138 ' * Otherwise: the name is bound to the object in the global\n'
jpayne@69 139 ' namespace or the outer namespace determined by '
jpayne@69 140 '"nonlocal",\n'
jpayne@69 141 ' respectively.\n'
jpayne@69 142 '\n'
jpayne@69 143 ' The name is rebound if it was already bound. This may cause '
jpayne@69 144 'the\n'
jpayne@69 145 ' reference count for the object previously bound to the name '
jpayne@69 146 'to reach\n'
jpayne@69 147 ' zero, causing the object to be deallocated and its '
jpayne@69 148 'destructor (if it\n'
jpayne@69 149 ' has one) to be called.\n'
jpayne@69 150 '\n'
jpayne@69 151 '* If the target is an attribute reference: The primary '
jpayne@69 152 'expression in\n'
jpayne@69 153 ' the reference is evaluated. It should yield an object with\n'
jpayne@69 154 ' assignable attributes; if this is not the case, "TypeError" '
jpayne@69 155 'is\n'
jpayne@69 156 ' raised. That object is then asked to assign the assigned '
jpayne@69 157 'object to\n'
jpayne@69 158 ' the given attribute; if it cannot perform the assignment, it '
jpayne@69 159 'raises\n'
jpayne@69 160 ' an exception (usually but not necessarily '
jpayne@69 161 '"AttributeError").\n'
jpayne@69 162 '\n'
jpayne@69 163 ' Note: If the object is a class instance and the attribute '
jpayne@69 164 'reference\n'
jpayne@69 165 ' occurs on both sides of the assignment operator, the '
jpayne@69 166 'right-hand side\n'
jpayne@69 167 ' expression, "a.x" can access either an instance attribute or '
jpayne@69 168 '(if no\n'
jpayne@69 169 ' instance attribute exists) a class attribute. The left-hand '
jpayne@69 170 'side\n'
jpayne@69 171 ' target "a.x" is always set as an instance attribute, '
jpayne@69 172 'creating it if\n'
jpayne@69 173 ' necessary. Thus, the two occurrences of "a.x" do not '
jpayne@69 174 'necessarily\n'
jpayne@69 175 ' refer to the same attribute: if the right-hand side '
jpayne@69 176 'expression\n'
jpayne@69 177 ' refers to a class attribute, the left-hand side creates a '
jpayne@69 178 'new\n'
jpayne@69 179 ' instance attribute as the target of the assignment:\n'
jpayne@69 180 '\n'
jpayne@69 181 ' class Cls:\n'
jpayne@69 182 ' x = 3 # class variable\n'
jpayne@69 183 ' inst = Cls()\n'
jpayne@69 184 ' inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x '
jpayne@69 185 'as 3\n'
jpayne@69 186 '\n'
jpayne@69 187 ' This description does not necessarily apply to descriptor\n'
jpayne@69 188 ' attributes, such as properties created with "property()".\n'
jpayne@69 189 '\n'
jpayne@69 190 '* If the target is a subscription: The primary expression in '
jpayne@69 191 'the\n'
jpayne@69 192 ' reference is evaluated. It should yield either a mutable '
jpayne@69 193 'sequence\n'
jpayne@69 194 ' object (such as a list) or a mapping object (such as a '
jpayne@69 195 'dictionary).\n'
jpayne@69 196 ' Next, the subscript expression is evaluated.\n'
jpayne@69 197 '\n'
jpayne@69 198 ' If the primary is a mutable sequence object (such as a '
jpayne@69 199 'list), the\n'
jpayne@69 200 ' subscript must yield an integer. If it is negative, the '
jpayne@69 201 'sequence’s\n'
jpayne@69 202 ' length is added to it. The resulting value must be a '
jpayne@69 203 'nonnegative\n'
jpayne@69 204 ' integer less than the sequence’s length, and the sequence is '
jpayne@69 205 'asked\n'
jpayne@69 206 ' to assign the assigned object to its item with that index. '
jpayne@69 207 'If the\n'
jpayne@69 208 ' index is out of range, "IndexError" is raised (assignment to '
jpayne@69 209 'a\n'
jpayne@69 210 ' subscripted sequence cannot add new items to a list).\n'
jpayne@69 211 '\n'
jpayne@69 212 ' If the primary is a mapping object (such as a dictionary), '
jpayne@69 213 'the\n'
jpayne@69 214 ' subscript must have a type compatible with the mapping’s key '
jpayne@69 215 'type,\n'
jpayne@69 216 ' and the mapping is then asked to create a key/datum pair '
jpayne@69 217 'which maps\n'
jpayne@69 218 ' the subscript to the assigned object. This can either '
jpayne@69 219 'replace an\n'
jpayne@69 220 ' existing key/value pair with the same key value, or insert a '
jpayne@69 221 'new\n'
jpayne@69 222 ' key/value pair (if no key with the same value existed).\n'
jpayne@69 223 '\n'
jpayne@69 224 ' For user-defined objects, the "__setitem__()" method is '
jpayne@69 225 'called with\n'
jpayne@69 226 ' appropriate arguments.\n'
jpayne@69 227 '\n'
jpayne@69 228 '* If the target is a slicing: The primary expression in the\n'
jpayne@69 229 ' reference is evaluated. It should yield a mutable sequence '
jpayne@69 230 'object\n'
jpayne@69 231 ' (such as a list). The assigned object should be a sequence '
jpayne@69 232 'object\n'
jpayne@69 233 ' of the same type. Next, the lower and upper bound '
jpayne@69 234 'expressions are\n'
jpayne@69 235 ' evaluated, insofar they are present; defaults are zero and '
jpayne@69 236 'the\n'
jpayne@69 237 ' sequence’s length. The bounds should evaluate to integers. '
jpayne@69 238 'If\n'
jpayne@69 239 ' either bound is negative, the sequence’s length is added to '
jpayne@69 240 'it. The\n'
jpayne@69 241 ' resulting bounds are clipped to lie between zero and the '
jpayne@69 242 'sequence’s\n'
jpayne@69 243 ' length, inclusive. Finally, the sequence object is asked to '
jpayne@69 244 'replace\n'
jpayne@69 245 ' the slice with the items of the assigned sequence. The '
jpayne@69 246 'length of\n'
jpayne@69 247 ' the slice may be different from the length of the assigned '
jpayne@69 248 'sequence,\n'
jpayne@69 249 ' thus changing the length of the target sequence, if the '
jpayne@69 250 'target\n'
jpayne@69 251 ' sequence allows it.\n'
jpayne@69 252 '\n'
jpayne@69 253 '**CPython implementation detail:** In the current '
jpayne@69 254 'implementation, the\n'
jpayne@69 255 'syntax for targets is taken to be the same as for expressions, '
jpayne@69 256 'and\n'
jpayne@69 257 'invalid syntax is rejected during the code generation phase, '
jpayne@69 258 'causing\n'
jpayne@69 259 'less detailed error messages.\n'
jpayne@69 260 '\n'
jpayne@69 261 'Although the definition of assignment implies that overlaps '
jpayne@69 262 'between\n'
jpayne@69 263 'the left-hand side and the right-hand side are ‘simultaneous’ '
jpayne@69 264 '(for\n'
jpayne@69 265 'example "a, b = b, a" swaps two variables), overlaps *within* '
jpayne@69 266 'the\n'
jpayne@69 267 'collection of assigned-to variables occur left-to-right, '
jpayne@69 268 'sometimes\n'
jpayne@69 269 'resulting in confusion. For instance, the following program '
jpayne@69 270 'prints\n'
jpayne@69 271 '"[0, 2]":\n'
jpayne@69 272 '\n'
jpayne@69 273 ' x = [0, 1]\n'
jpayne@69 274 ' i = 0\n'
jpayne@69 275 ' i, x[i] = 1, 2 # i is updated, then x[i] is '
jpayne@69 276 'updated\n'
jpayne@69 277 ' print(x)\n'
jpayne@69 278 '\n'
jpayne@69 279 'See also:\n'
jpayne@69 280 '\n'
jpayne@69 281 ' **PEP 3132** - Extended Iterable Unpacking\n'
jpayne@69 282 ' The specification for the "*target" feature.\n'
jpayne@69 283 '\n'
jpayne@69 284 '\n'
jpayne@69 285 'Augmented assignment statements\n'
jpayne@69 286 '===============================\n'
jpayne@69 287 '\n'
jpayne@69 288 'Augmented assignment is the combination, in a single '
jpayne@69 289 'statement, of a\n'
jpayne@69 290 'binary operation and an assignment statement:\n'
jpayne@69 291 '\n'
jpayne@69 292 ' augmented_assignment_stmt ::= augtarget augop '
jpayne@69 293 '(expression_list | yield_expression)\n'
jpayne@69 294 ' augtarget ::= identifier | attributeref | '
jpayne@69 295 'subscription | slicing\n'
jpayne@69 296 ' augop ::= "+=" | "-=" | "*=" | "@=" | '
jpayne@69 297 '"/=" | "//=" | "%=" | "**="\n'
jpayne@69 298 ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n'
jpayne@69 299 '\n'
jpayne@69 300 '(See section Primaries for the syntax definitions of the last '
jpayne@69 301 'three\n'
jpayne@69 302 'symbols.)\n'
jpayne@69 303 '\n'
jpayne@69 304 'An augmented assignment evaluates the target (which, unlike '
jpayne@69 305 'normal\n'
jpayne@69 306 'assignment statements, cannot be an unpacking) and the '
jpayne@69 307 'expression\n'
jpayne@69 308 'list, performs the binary operation specific to the type of '
jpayne@69 309 'assignment\n'
jpayne@69 310 'on the two operands, and assigns the result to the original '
jpayne@69 311 'target.\n'
jpayne@69 312 'The target is only evaluated once.\n'
jpayne@69 313 '\n'
jpayne@69 314 'An augmented assignment expression like "x += 1" can be '
jpayne@69 315 'rewritten as\n'
jpayne@69 316 '"x = x + 1" to achieve a similar, but not exactly equal '
jpayne@69 317 'effect. In the\n'
jpayne@69 318 'augmented version, "x" is only evaluated once. Also, when '
jpayne@69 319 'possible,\n'
jpayne@69 320 'the actual operation is performed *in-place*, meaning that '
jpayne@69 321 'rather than\n'
jpayne@69 322 'creating a new object and assigning that to the target, the '
jpayne@69 323 'old object\n'
jpayne@69 324 'is modified instead.\n'
jpayne@69 325 '\n'
jpayne@69 326 'Unlike normal assignments, augmented assignments evaluate the '
jpayne@69 327 'left-\n'
jpayne@69 328 'hand side *before* evaluating the right-hand side. For '
jpayne@69 329 'example, "a[i]\n'
jpayne@69 330 '+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and '
jpayne@69 331 'performs\n'
jpayne@69 332 'the addition, and lastly, it writes the result back to '
jpayne@69 333 '"a[i]".\n'
jpayne@69 334 '\n'
jpayne@69 335 'With the exception of assigning to tuples and multiple targets '
jpayne@69 336 'in a\n'
jpayne@69 337 'single statement, the assignment done by augmented assignment\n'
jpayne@69 338 'statements is handled the same way as normal assignments. '
jpayne@69 339 'Similarly,\n'
jpayne@69 340 'with the exception of the possible *in-place* behavior, the '
jpayne@69 341 'binary\n'
jpayne@69 342 'operation performed by augmented assignment is the same as the '
jpayne@69 343 'normal\n'
jpayne@69 344 'binary operations.\n'
jpayne@69 345 '\n'
jpayne@69 346 'For targets which are attribute references, the same caveat '
jpayne@69 347 'about\n'
jpayne@69 348 'class and instance attributes applies as for regular '
jpayne@69 349 'assignments.\n'
jpayne@69 350 '\n'
jpayne@69 351 '\n'
jpayne@69 352 'Annotated assignment statements\n'
jpayne@69 353 '===============================\n'
jpayne@69 354 '\n'
jpayne@69 355 '*Annotation* assignment is the combination, in a single '
jpayne@69 356 'statement, of\n'
jpayne@69 357 'a variable or attribute annotation and an optional assignment\n'
jpayne@69 358 'statement:\n'
jpayne@69 359 '\n'
jpayne@69 360 ' annotated_assignment_stmt ::= augtarget ":" expression\n'
jpayne@69 361 ' ["=" (starred_expression | '
jpayne@69 362 'yield_expression)]\n'
jpayne@69 363 '\n'
jpayne@69 364 'The difference from normal Assignment statements is that only '
jpayne@69 365 'single\n'
jpayne@69 366 'target is allowed.\n'
jpayne@69 367 '\n'
jpayne@69 368 'For simple names as assignment targets, if in class or module '
jpayne@69 369 'scope,\n'
jpayne@69 370 'the annotations are evaluated and stored in a special class or '
jpayne@69 371 'module\n'
jpayne@69 372 'attribute "__annotations__" that is a dictionary mapping from '
jpayne@69 373 'variable\n'
jpayne@69 374 'names (mangled if private) to evaluated annotations. This '
jpayne@69 375 'attribute is\n'
jpayne@69 376 'writable and is automatically created at the start of class or '
jpayne@69 377 'module\n'
jpayne@69 378 'body execution, if annotations are found statically.\n'
jpayne@69 379 '\n'
jpayne@69 380 'For expressions as assignment targets, the annotations are '
jpayne@69 381 'evaluated\n'
jpayne@69 382 'if in class or module scope, but not stored.\n'
jpayne@69 383 '\n'
jpayne@69 384 'If a name is annotated in a function scope, then this name is '
jpayne@69 385 'local\n'
jpayne@69 386 'for that scope. Annotations are never evaluated and stored in '
jpayne@69 387 'function\n'
jpayne@69 388 'scopes.\n'
jpayne@69 389 '\n'
jpayne@69 390 'If the right hand side is present, an annotated assignment '
jpayne@69 391 'performs\n'
jpayne@69 392 'the actual assignment before evaluating annotations (where\n'
jpayne@69 393 'applicable). If the right hand side is not present for an '
jpayne@69 394 'expression\n'
jpayne@69 395 'target, then the interpreter evaluates the target except for '
jpayne@69 396 'the last\n'
jpayne@69 397 '"__setitem__()" or "__setattr__()" call.\n'
jpayne@69 398 '\n'
jpayne@69 399 'See also:\n'
jpayne@69 400 '\n'
jpayne@69 401 ' **PEP 526** - Syntax for Variable Annotations\n'
jpayne@69 402 ' The proposal that added syntax for annotating the types '
jpayne@69 403 'of\n'
jpayne@69 404 ' variables (including class variables and instance '
jpayne@69 405 'variables),\n'
jpayne@69 406 ' instead of expressing them through comments.\n'
jpayne@69 407 '\n'
jpayne@69 408 ' **PEP 484** - Type hints\n'
jpayne@69 409 ' The proposal that added the "typing" module to provide a '
jpayne@69 410 'standard\n'
jpayne@69 411 ' syntax for type annotations that can be used in static '
jpayne@69 412 'analysis\n'
jpayne@69 413 ' tools and IDEs.\n'
jpayne@69 414 '\n'
jpayne@69 415 'Changed in version 3.8: Now annotated assignments allow same\n'
jpayne@69 416 'expressions in the right hand side as the regular '
jpayne@69 417 'assignments.\n'
jpayne@69 418 'Previously, some expressions (like un-parenthesized tuple '
jpayne@69 419 'expressions)\n'
jpayne@69 420 'caused a syntax error.\n',
jpayne@69 421 'async': 'Coroutines\n'
jpayne@69 422 '**********\n'
jpayne@69 423 '\n'
jpayne@69 424 'New in version 3.5.\n'
jpayne@69 425 '\n'
jpayne@69 426 '\n'
jpayne@69 427 'Coroutine function definition\n'
jpayne@69 428 '=============================\n'
jpayne@69 429 '\n'
jpayne@69 430 ' async_funcdef ::= [decorators] "async" "def" funcname "(" '
jpayne@69 431 '[parameter_list] ")"\n'
jpayne@69 432 ' ["->" expression] ":" suite\n'
jpayne@69 433 '\n'
jpayne@69 434 'Execution of Python coroutines can be suspended and resumed at '
jpayne@69 435 'many\n'
jpayne@69 436 'points (see *coroutine*). Inside the body of a coroutine '
jpayne@69 437 'function,\n'
jpayne@69 438 '"await" and "async" identifiers become reserved keywords; "await"\n'
jpayne@69 439 'expressions, "async for" and "async with" can only be used in\n'
jpayne@69 440 'coroutine function bodies.\n'
jpayne@69 441 '\n'
jpayne@69 442 'Functions defined with "async def" syntax are always coroutine\n'
jpayne@69 443 'functions, even if they do not contain "await" or "async" '
jpayne@69 444 'keywords.\n'
jpayne@69 445 '\n'
jpayne@69 446 'It is a "SyntaxError" to use a "yield from" expression inside the '
jpayne@69 447 'body\n'
jpayne@69 448 'of a coroutine function.\n'
jpayne@69 449 '\n'
jpayne@69 450 'An example of a coroutine function:\n'
jpayne@69 451 '\n'
jpayne@69 452 ' async def func(param1, param2):\n'
jpayne@69 453 ' do_stuff()\n'
jpayne@69 454 ' await some_coroutine()\n'
jpayne@69 455 '\n'
jpayne@69 456 '\n'
jpayne@69 457 'The "async for" statement\n'
jpayne@69 458 '=========================\n'
jpayne@69 459 '\n'
jpayne@69 460 ' async_for_stmt ::= "async" for_stmt\n'
jpayne@69 461 '\n'
jpayne@69 462 'An *asynchronous iterable* is able to call asynchronous code in '
jpayne@69 463 'its\n'
jpayne@69 464 '*iter* implementation, and *asynchronous iterator* can call\n'
jpayne@69 465 'asynchronous code in its *next* method.\n'
jpayne@69 466 '\n'
jpayne@69 467 'The "async for" statement allows convenient iteration over\n'
jpayne@69 468 'asynchronous iterators.\n'
jpayne@69 469 '\n'
jpayne@69 470 'The following code:\n'
jpayne@69 471 '\n'
jpayne@69 472 ' async for TARGET in ITER:\n'
jpayne@69 473 ' BLOCK\n'
jpayne@69 474 ' else:\n'
jpayne@69 475 ' BLOCK2\n'
jpayne@69 476 '\n'
jpayne@69 477 'Is semantically equivalent to:\n'
jpayne@69 478 '\n'
jpayne@69 479 ' iter = (ITER)\n'
jpayne@69 480 ' iter = type(iter).__aiter__(iter)\n'
jpayne@69 481 ' running = True\n'
jpayne@69 482 ' while running:\n'
jpayne@69 483 ' try:\n'
jpayne@69 484 ' TARGET = await type(iter).__anext__(iter)\n'
jpayne@69 485 ' except StopAsyncIteration:\n'
jpayne@69 486 ' running = False\n'
jpayne@69 487 ' else:\n'
jpayne@69 488 ' BLOCK\n'
jpayne@69 489 ' else:\n'
jpayne@69 490 ' BLOCK2\n'
jpayne@69 491 '\n'
jpayne@69 492 'See also "__aiter__()" and "__anext__()" for details.\n'
jpayne@69 493 '\n'
jpayne@69 494 'It is a "SyntaxError" to use an "async for" statement outside the '
jpayne@69 495 'body\n'
jpayne@69 496 'of a coroutine function.\n'
jpayne@69 497 '\n'
jpayne@69 498 '\n'
jpayne@69 499 'The "async with" statement\n'
jpayne@69 500 '==========================\n'
jpayne@69 501 '\n'
jpayne@69 502 ' async_with_stmt ::= "async" with_stmt\n'
jpayne@69 503 '\n'
jpayne@69 504 'An *asynchronous context manager* is a *context manager* that is '
jpayne@69 505 'able\n'
jpayne@69 506 'to suspend execution in its *enter* and *exit* methods.\n'
jpayne@69 507 '\n'
jpayne@69 508 'The following code:\n'
jpayne@69 509 '\n'
jpayne@69 510 ' async with EXPR as VAR:\n'
jpayne@69 511 ' BLOCK\n'
jpayne@69 512 '\n'
jpayne@69 513 'Is semantically equivalent to:\n'
jpayne@69 514 '\n'
jpayne@69 515 ' mgr = (EXPR)\n'
jpayne@69 516 ' aexit = type(mgr).__aexit__\n'
jpayne@69 517 ' aenter = type(mgr).__aenter__(mgr)\n'
jpayne@69 518 '\n'
jpayne@69 519 ' VAR = await aenter\n'
jpayne@69 520 ' try:\n'
jpayne@69 521 ' BLOCK\n'
jpayne@69 522 ' except:\n'
jpayne@69 523 ' if not await aexit(mgr, *sys.exc_info()):\n'
jpayne@69 524 ' raise\n'
jpayne@69 525 ' else:\n'
jpayne@69 526 ' await aexit(mgr, None, None, None)\n'
jpayne@69 527 '\n'
jpayne@69 528 'See also "__aenter__()" and "__aexit__()" for details.\n'
jpayne@69 529 '\n'
jpayne@69 530 'It is a "SyntaxError" to use an "async with" statement outside the\n'
jpayne@69 531 'body of a coroutine function.\n'
jpayne@69 532 '\n'
jpayne@69 533 'See also:\n'
jpayne@69 534 '\n'
jpayne@69 535 ' **PEP 492** - Coroutines with async and await syntax\n'
jpayne@69 536 ' The proposal that made coroutines a proper standalone concept '
jpayne@69 537 'in\n'
jpayne@69 538 ' Python, and added supporting syntax.\n'
jpayne@69 539 '\n'
jpayne@69 540 '-[ Footnotes ]-\n'
jpayne@69 541 '\n'
jpayne@69 542 '[1] The exception is propagated to the invocation stack unless\n'
jpayne@69 543 ' there is a "finally" clause which happens to raise another\n'
jpayne@69 544 ' exception. That new exception causes the old one to be lost.\n'
jpayne@69 545 '\n'
jpayne@69 546 '[2] A string literal appearing as the first statement in the\n'
jpayne@69 547 ' function body is transformed into the function’s "__doc__"\n'
jpayne@69 548 ' attribute and therefore the function’s *docstring*.\n'
jpayne@69 549 '\n'
jpayne@69 550 '[3] A string literal appearing as the first statement in the class\n'
jpayne@69 551 ' body is transformed into the namespace’s "__doc__" item and\n'
jpayne@69 552 ' therefore the class’s *docstring*.\n',
jpayne@69 553 'atom-identifiers': 'Identifiers (Names)\n'
jpayne@69 554 '*******************\n'
jpayne@69 555 '\n'
jpayne@69 556 'An identifier occurring as an atom is a name. See '
jpayne@69 557 'section Identifiers\n'
jpayne@69 558 'and keywords for lexical definition and section Naming '
jpayne@69 559 'and binding for\n'
jpayne@69 560 'documentation of naming and binding.\n'
jpayne@69 561 '\n'
jpayne@69 562 'When the name is bound to an object, evaluation of the '
jpayne@69 563 'atom yields\n'
jpayne@69 564 'that object. When a name is not bound, an attempt to '
jpayne@69 565 'evaluate it\n'
jpayne@69 566 'raises a "NameError" exception.\n'
jpayne@69 567 '\n'
jpayne@69 568 '**Private name mangling:** When an identifier that '
jpayne@69 569 'textually occurs in\n'
jpayne@69 570 'a class definition begins with two or more underscore '
jpayne@69 571 'characters and\n'
jpayne@69 572 'does not end in two or more underscores, it is '
jpayne@69 573 'considered a *private\n'
jpayne@69 574 'name* of that class. Private names are transformed to a '
jpayne@69 575 'longer form\n'
jpayne@69 576 'before code is generated for them. The transformation '
jpayne@69 577 'inserts the\n'
jpayne@69 578 'class name, with leading underscores removed and a '
jpayne@69 579 'single underscore\n'
jpayne@69 580 'inserted, in front of the name. For example, the '
jpayne@69 581 'identifier "__spam"\n'
jpayne@69 582 'occurring in a class named "Ham" will be transformed to '
jpayne@69 583 '"_Ham__spam".\n'
jpayne@69 584 'This transformation is independent of the syntactical '
jpayne@69 585 'context in which\n'
jpayne@69 586 'the identifier is used. If the transformed name is '
jpayne@69 587 'extremely long\n'
jpayne@69 588 '(longer than 255 characters), implementation defined '
jpayne@69 589 'truncation may\n'
jpayne@69 590 'happen. If the class name consists only of underscores, '
jpayne@69 591 'no\n'
jpayne@69 592 'transformation is done.\n',
jpayne@69 593 'atom-literals': 'Literals\n'
jpayne@69 594 '********\n'
jpayne@69 595 '\n'
jpayne@69 596 'Python supports string and bytes literals and various '
jpayne@69 597 'numeric\n'
jpayne@69 598 'literals:\n'
jpayne@69 599 '\n'
jpayne@69 600 ' literal ::= stringliteral | bytesliteral\n'
jpayne@69 601 ' | integer | floatnumber | imagnumber\n'
jpayne@69 602 '\n'
jpayne@69 603 'Evaluation of a literal yields an object of the given type '
jpayne@69 604 '(string,\n'
jpayne@69 605 'bytes, integer, floating point number, complex number) with '
jpayne@69 606 'the given\n'
jpayne@69 607 'value. The value may be approximated in the case of '
jpayne@69 608 'floating point\n'
jpayne@69 609 'and imaginary (complex) literals. See section Literals for '
jpayne@69 610 'details.\n'
jpayne@69 611 '\n'
jpayne@69 612 'All literals correspond to immutable data types, and hence '
jpayne@69 613 'the\n'
jpayne@69 614 'object’s identity is less important than its value. '
jpayne@69 615 'Multiple\n'
jpayne@69 616 'evaluations of literals with the same value (either the '
jpayne@69 617 'same\n'
jpayne@69 618 'occurrence in the program text or a different occurrence) '
jpayne@69 619 'may obtain\n'
jpayne@69 620 'the same object or a different object with the same '
jpayne@69 621 'value.\n',
jpayne@69 622 'attribute-access': 'Customizing attribute access\n'
jpayne@69 623 '****************************\n'
jpayne@69 624 '\n'
jpayne@69 625 'The following methods can be defined to customize the '
jpayne@69 626 'meaning of\n'
jpayne@69 627 'attribute access (use of, assignment to, or deletion of '
jpayne@69 628 '"x.name") for\n'
jpayne@69 629 'class instances.\n'
jpayne@69 630 '\n'
jpayne@69 631 'object.__getattr__(self, name)\n'
jpayne@69 632 '\n'
jpayne@69 633 ' Called when the default attribute access fails with '
jpayne@69 634 'an\n'
jpayne@69 635 ' "AttributeError" (either "__getattribute__()" raises '
jpayne@69 636 'an\n'
jpayne@69 637 ' "AttributeError" because *name* is not an instance '
jpayne@69 638 'attribute or an\n'
jpayne@69 639 ' attribute in the class tree for "self"; or '
jpayne@69 640 '"__get__()" of a *name*\n'
jpayne@69 641 ' property raises "AttributeError"). This method '
jpayne@69 642 'should either\n'
jpayne@69 643 ' return the (computed) attribute value or raise an '
jpayne@69 644 '"AttributeError"\n'
jpayne@69 645 ' exception.\n'
jpayne@69 646 '\n'
jpayne@69 647 ' Note that if the attribute is found through the '
jpayne@69 648 'normal mechanism,\n'
jpayne@69 649 ' "__getattr__()" is not called. (This is an '
jpayne@69 650 'intentional asymmetry\n'
jpayne@69 651 ' between "__getattr__()" and "__setattr__()".) This is '
jpayne@69 652 'done both for\n'
jpayne@69 653 ' efficiency reasons and because otherwise '
jpayne@69 654 '"__getattr__()" would have\n'
jpayne@69 655 ' no way to access other attributes of the instance. '
jpayne@69 656 'Note that at\n'
jpayne@69 657 ' least for instance variables, you can fake total '
jpayne@69 658 'control by not\n'
jpayne@69 659 ' inserting any values in the instance attribute '
jpayne@69 660 'dictionary (but\n'
jpayne@69 661 ' instead inserting them in another object). See the\n'
jpayne@69 662 ' "__getattribute__()" method below for a way to '
jpayne@69 663 'actually get total\n'
jpayne@69 664 ' control over attribute access.\n'
jpayne@69 665 '\n'
jpayne@69 666 'object.__getattribute__(self, name)\n'
jpayne@69 667 '\n'
jpayne@69 668 ' Called unconditionally to implement attribute '
jpayne@69 669 'accesses for\n'
jpayne@69 670 ' instances of the class. If the class also defines '
jpayne@69 671 '"__getattr__()",\n'
jpayne@69 672 ' the latter will not be called unless '
jpayne@69 673 '"__getattribute__()" either\n'
jpayne@69 674 ' calls it explicitly or raises an "AttributeError". '
jpayne@69 675 'This method\n'
jpayne@69 676 ' should return the (computed) attribute value or raise '
jpayne@69 677 'an\n'
jpayne@69 678 ' "AttributeError" exception. In order to avoid '
jpayne@69 679 'infinite recursion in\n'
jpayne@69 680 ' this method, its implementation should always call '
jpayne@69 681 'the base class\n'
jpayne@69 682 ' method with the same name to access any attributes it '
jpayne@69 683 'needs, for\n'
jpayne@69 684 ' example, "object.__getattribute__(self, name)".\n'
jpayne@69 685 '\n'
jpayne@69 686 ' Note: This method may still be bypassed when looking '
jpayne@69 687 'up special\n'
jpayne@69 688 ' methods as the result of implicit invocation via '
jpayne@69 689 'language syntax\n'
jpayne@69 690 ' or built-in functions. See Special method lookup.\n'
jpayne@69 691 '\n'
jpayne@69 692 'object.__setattr__(self, name, value)\n'
jpayne@69 693 '\n'
jpayne@69 694 ' Called when an attribute assignment is attempted. '
jpayne@69 695 'This is called\n'
jpayne@69 696 ' instead of the normal mechanism (i.e. store the value '
jpayne@69 697 'in the\n'
jpayne@69 698 ' instance dictionary). *name* is the attribute name, '
jpayne@69 699 '*value* is the\n'
jpayne@69 700 ' value to be assigned to it.\n'
jpayne@69 701 '\n'
jpayne@69 702 ' If "__setattr__()" wants to assign to an instance '
jpayne@69 703 'attribute, it\n'
jpayne@69 704 ' should call the base class method with the same name, '
jpayne@69 705 'for example,\n'
jpayne@69 706 ' "object.__setattr__(self, name, value)".\n'
jpayne@69 707 '\n'
jpayne@69 708 'object.__delattr__(self, name)\n'
jpayne@69 709 '\n'
jpayne@69 710 ' Like "__setattr__()" but for attribute deletion '
jpayne@69 711 'instead of\n'
jpayne@69 712 ' assignment. This should only be implemented if "del '
jpayne@69 713 'obj.name" is\n'
jpayne@69 714 ' meaningful for the object.\n'
jpayne@69 715 '\n'
jpayne@69 716 'object.__dir__(self)\n'
jpayne@69 717 '\n'
jpayne@69 718 ' Called when "dir()" is called on the object. A '
jpayne@69 719 'sequence must be\n'
jpayne@69 720 ' returned. "dir()" converts the returned sequence to a '
jpayne@69 721 'list and\n'
jpayne@69 722 ' sorts it.\n'
jpayne@69 723 '\n'
jpayne@69 724 '\n'
jpayne@69 725 'Customizing module attribute access\n'
jpayne@69 726 '===================================\n'
jpayne@69 727 '\n'
jpayne@69 728 'Special names "__getattr__" and "__dir__" can be also '
jpayne@69 729 'used to\n'
jpayne@69 730 'customize access to module attributes. The "__getattr__" '
jpayne@69 731 'function at\n'
jpayne@69 732 'the module level should accept one argument which is the '
jpayne@69 733 'name of an\n'
jpayne@69 734 'attribute and return the computed value or raise an '
jpayne@69 735 '"AttributeError".\n'
jpayne@69 736 'If an attribute is not found on a module object through '
jpayne@69 737 'the normal\n'
jpayne@69 738 'lookup, i.e. "object.__getattribute__()", then '
jpayne@69 739 '"__getattr__" is\n'
jpayne@69 740 'searched in the module "__dict__" before raising an '
jpayne@69 741 '"AttributeError".\n'
jpayne@69 742 'If found, it is called with the attribute name and the '
jpayne@69 743 'result is\n'
jpayne@69 744 'returned.\n'
jpayne@69 745 '\n'
jpayne@69 746 'The "__dir__" function should accept no arguments, and '
jpayne@69 747 'return a\n'
jpayne@69 748 'sequence of strings that represents the names accessible '
jpayne@69 749 'on module. If\n'
jpayne@69 750 'present, this function overrides the standard "dir()" '
jpayne@69 751 'search on a\n'
jpayne@69 752 'module.\n'
jpayne@69 753 '\n'
jpayne@69 754 'For a more fine grained customization of the module '
jpayne@69 755 'behavior (setting\n'
jpayne@69 756 'attributes, properties, etc.), one can set the '
jpayne@69 757 '"__class__" attribute\n'
jpayne@69 758 'of a module object to a subclass of "types.ModuleType". '
jpayne@69 759 'For example:\n'
jpayne@69 760 '\n'
jpayne@69 761 ' import sys\n'
jpayne@69 762 ' from types import ModuleType\n'
jpayne@69 763 '\n'
jpayne@69 764 ' class VerboseModule(ModuleType):\n'
jpayne@69 765 ' def __repr__(self):\n'
jpayne@69 766 " return f'Verbose {self.__name__}'\n"
jpayne@69 767 '\n'
jpayne@69 768 ' def __setattr__(self, attr, value):\n'
jpayne@69 769 " print(f'Setting {attr}...')\n"
jpayne@69 770 ' super().__setattr__(attr, value)\n'
jpayne@69 771 '\n'
jpayne@69 772 ' sys.modules[__name__].__class__ = VerboseModule\n'
jpayne@69 773 '\n'
jpayne@69 774 'Note: Defining module "__getattr__" and setting module '
jpayne@69 775 '"__class__"\n'
jpayne@69 776 ' only affect lookups made using the attribute access '
jpayne@69 777 'syntax –\n'
jpayne@69 778 ' directly accessing the module globals (whether by code '
jpayne@69 779 'within the\n'
jpayne@69 780 ' module, or via a reference to the module’s globals '
jpayne@69 781 'dictionary) is\n'
jpayne@69 782 ' unaffected.\n'
jpayne@69 783 '\n'
jpayne@69 784 'Changed in version 3.5: "__class__" module attribute is '
jpayne@69 785 'now writable.\n'
jpayne@69 786 '\n'
jpayne@69 787 'New in version 3.7: "__getattr__" and "__dir__" module '
jpayne@69 788 'attributes.\n'
jpayne@69 789 '\n'
jpayne@69 790 'See also:\n'
jpayne@69 791 '\n'
jpayne@69 792 ' **PEP 562** - Module __getattr__ and __dir__\n'
jpayne@69 793 ' Describes the "__getattr__" and "__dir__" functions '
jpayne@69 794 'on modules.\n'
jpayne@69 795 '\n'
jpayne@69 796 '\n'
jpayne@69 797 'Implementing Descriptors\n'
jpayne@69 798 '========================\n'
jpayne@69 799 '\n'
jpayne@69 800 'The following methods only apply when an instance of the '
jpayne@69 801 'class\n'
jpayne@69 802 'containing the method (a so-called *descriptor* class) '
jpayne@69 803 'appears in an\n'
jpayne@69 804 '*owner* class (the descriptor must be in either the '
jpayne@69 805 'owner’s class\n'
jpayne@69 806 'dictionary or in the class dictionary for one of its '
jpayne@69 807 'parents). In the\n'
jpayne@69 808 'examples below, “the attribute” refers to the attribute '
jpayne@69 809 'whose name is\n'
jpayne@69 810 'the key of the property in the owner class’ "__dict__".\n'
jpayne@69 811 '\n'
jpayne@69 812 'object.__get__(self, instance, owner=None)\n'
jpayne@69 813 '\n'
jpayne@69 814 ' Called to get the attribute of the owner class (class '
jpayne@69 815 'attribute\n'
jpayne@69 816 ' access) or of an instance of that class (instance '
jpayne@69 817 'attribute\n'
jpayne@69 818 ' access). The optional *owner* argument is the owner '
jpayne@69 819 'class, while\n'
jpayne@69 820 ' *instance* is the instance that the attribute was '
jpayne@69 821 'accessed through,\n'
jpayne@69 822 ' or "None" when the attribute is accessed through the '
jpayne@69 823 '*owner*.\n'
jpayne@69 824 '\n'
jpayne@69 825 ' This method should return the computed attribute '
jpayne@69 826 'value or raise an\n'
jpayne@69 827 ' "AttributeError" exception.\n'
jpayne@69 828 '\n'
jpayne@69 829 ' **PEP 252** specifies that "__get__()" is callable '
jpayne@69 830 'with one or two\n'
jpayne@69 831 ' arguments. Python’s own built-in descriptors support '
jpayne@69 832 'this\n'
jpayne@69 833 ' specification; however, it is likely that some '
jpayne@69 834 'third-party tools\n'
jpayne@69 835 ' have descriptors that require both arguments. '
jpayne@69 836 'Python’s own\n'
jpayne@69 837 ' "__getattribute__()" implementation always passes in '
jpayne@69 838 'both arguments\n'
jpayne@69 839 ' whether they are required or not.\n'
jpayne@69 840 '\n'
jpayne@69 841 'object.__set__(self, instance, value)\n'
jpayne@69 842 '\n'
jpayne@69 843 ' Called to set the attribute on an instance *instance* '
jpayne@69 844 'of the owner\n'
jpayne@69 845 ' class to a new value, *value*.\n'
jpayne@69 846 '\n'
jpayne@69 847 ' Note, adding "__set__()" or "__delete__()" changes '
jpayne@69 848 'the kind of\n'
jpayne@69 849 ' descriptor to a “data descriptor”. See Invoking '
jpayne@69 850 'Descriptors for\n'
jpayne@69 851 ' more details.\n'
jpayne@69 852 '\n'
jpayne@69 853 'object.__delete__(self, instance)\n'
jpayne@69 854 '\n'
jpayne@69 855 ' Called to delete the attribute on an instance '
jpayne@69 856 '*instance* of the\n'
jpayne@69 857 ' owner class.\n'
jpayne@69 858 '\n'
jpayne@69 859 'object.__set_name__(self, owner, name)\n'
jpayne@69 860 '\n'
jpayne@69 861 ' Called at the time the owning class *owner* is '
jpayne@69 862 'created. The\n'
jpayne@69 863 ' descriptor has been assigned to *name*.\n'
jpayne@69 864 '\n'
jpayne@69 865 ' Note: "__set_name__()" is only called implicitly as '
jpayne@69 866 'part of the\n'
jpayne@69 867 ' "type" constructor, so it will need to be called '
jpayne@69 868 'explicitly with\n'
jpayne@69 869 ' the appropriate parameters when a descriptor is '
jpayne@69 870 'added to a class\n'
jpayne@69 871 ' after initial creation:\n'
jpayne@69 872 '\n'
jpayne@69 873 ' class A:\n'
jpayne@69 874 ' pass\n'
jpayne@69 875 ' descr = custom_descriptor()\n'
jpayne@69 876 ' A.attr = descr\n'
jpayne@69 877 " descr.__set_name__(A, 'attr')\n"
jpayne@69 878 '\n'
jpayne@69 879 ' See Creating the class object for more details.\n'
jpayne@69 880 '\n'
jpayne@69 881 ' New in version 3.6.\n'
jpayne@69 882 '\n'
jpayne@69 883 'The attribute "__objclass__" is interpreted by the '
jpayne@69 884 '"inspect" module as\n'
jpayne@69 885 'specifying the class where this object was defined '
jpayne@69 886 '(setting this\n'
jpayne@69 887 'appropriately can assist in runtime introspection of '
jpayne@69 888 'dynamic class\n'
jpayne@69 889 'attributes). For callables, it may indicate that an '
jpayne@69 890 'instance of the\n'
jpayne@69 891 'given type (or a subclass) is expected or required as '
jpayne@69 892 'the first\n'
jpayne@69 893 'positional argument (for example, CPython sets this '
jpayne@69 894 'attribute for\n'
jpayne@69 895 'unbound methods that are implemented in C).\n'
jpayne@69 896 '\n'
jpayne@69 897 '\n'
jpayne@69 898 'Invoking Descriptors\n'
jpayne@69 899 '====================\n'
jpayne@69 900 '\n'
jpayne@69 901 'In general, a descriptor is an object attribute with '
jpayne@69 902 '“binding\n'
jpayne@69 903 'behavior”, one whose attribute access has been '
jpayne@69 904 'overridden by methods\n'
jpayne@69 905 'in the descriptor protocol: "__get__()", "__set__()", '
jpayne@69 906 'and\n'
jpayne@69 907 '"__delete__()". If any of those methods are defined for '
jpayne@69 908 'an object, it\n'
jpayne@69 909 'is said to be a descriptor.\n'
jpayne@69 910 '\n'
jpayne@69 911 'The default behavior for attribute access is to get, '
jpayne@69 912 'set, or delete\n'
jpayne@69 913 'the attribute from an object’s dictionary. For instance, '
jpayne@69 914 '"a.x" has a\n'
jpayne@69 915 'lookup chain starting with "a.__dict__[\'x\']", then\n'
jpayne@69 916 '"type(a).__dict__[\'x\']", and continuing through the '
jpayne@69 917 'base classes of\n'
jpayne@69 918 '"type(a)" excluding metaclasses.\n'
jpayne@69 919 '\n'
jpayne@69 920 'However, if the looked-up value is an object defining '
jpayne@69 921 'one of the\n'
jpayne@69 922 'descriptor methods, then Python may override the default '
jpayne@69 923 'behavior and\n'
jpayne@69 924 'invoke the descriptor method instead. Where this occurs '
jpayne@69 925 'in the\n'
jpayne@69 926 'precedence chain depends on which descriptor methods '
jpayne@69 927 'were defined and\n'
jpayne@69 928 'how they were called.\n'
jpayne@69 929 '\n'
jpayne@69 930 'The starting point for descriptor invocation is a '
jpayne@69 931 'binding, "a.x". How\n'
jpayne@69 932 'the arguments are assembled depends on "a":\n'
jpayne@69 933 '\n'
jpayne@69 934 'Direct Call\n'
jpayne@69 935 ' The simplest and least common call is when user code '
jpayne@69 936 'directly\n'
jpayne@69 937 ' invokes a descriptor method: "x.__get__(a)".\n'
jpayne@69 938 '\n'
jpayne@69 939 'Instance Binding\n'
jpayne@69 940 ' If binding to an object instance, "a.x" is '
jpayne@69 941 'transformed into the\n'
jpayne@69 942 ' call: "type(a).__dict__[\'x\'].__get__(a, type(a))".\n'
jpayne@69 943 '\n'
jpayne@69 944 'Class Binding\n'
jpayne@69 945 ' If binding to a class, "A.x" is transformed into the '
jpayne@69 946 'call:\n'
jpayne@69 947 ' "A.__dict__[\'x\'].__get__(None, A)".\n'
jpayne@69 948 '\n'
jpayne@69 949 'Super Binding\n'
jpayne@69 950 ' If "a" is an instance of "super", then the binding '
jpayne@69 951 '"super(B,\n'
jpayne@69 952 ' obj).m()" searches "obj.__class__.__mro__" for the '
jpayne@69 953 'base class "A"\n'
jpayne@69 954 ' immediately preceding "B" and then invokes the '
jpayne@69 955 'descriptor with the\n'
jpayne@69 956 ' call: "A.__dict__[\'m\'].__get__(obj, '
jpayne@69 957 'obj.__class__)".\n'
jpayne@69 958 '\n'
jpayne@69 959 'For instance bindings, the precedence of descriptor '
jpayne@69 960 'invocation depends\n'
jpayne@69 961 'on the which descriptor methods are defined. A '
jpayne@69 962 'descriptor can define\n'
jpayne@69 963 'any combination of "__get__()", "__set__()" and '
jpayne@69 964 '"__delete__()". If it\n'
jpayne@69 965 'does not define "__get__()", then accessing the '
jpayne@69 966 'attribute will return\n'
jpayne@69 967 'the descriptor object itself unless there is a value in '
jpayne@69 968 'the object’s\n'
jpayne@69 969 'instance dictionary. If the descriptor defines '
jpayne@69 970 '"__set__()" and/or\n'
jpayne@69 971 '"__delete__()", it is a data descriptor; if it defines '
jpayne@69 972 'neither, it is\n'
jpayne@69 973 'a non-data descriptor. Normally, data descriptors '
jpayne@69 974 'define both\n'
jpayne@69 975 '"__get__()" and "__set__()", while non-data descriptors '
jpayne@69 976 'have just the\n'
jpayne@69 977 '"__get__()" method. Data descriptors with "__set__()" '
jpayne@69 978 'and "__get__()"\n'
jpayne@69 979 'defined always override a redefinition in an instance '
jpayne@69 980 'dictionary. In\n'
jpayne@69 981 'contrast, non-data descriptors can be overridden by '
jpayne@69 982 'instances.\n'
jpayne@69 983 '\n'
jpayne@69 984 'Python methods (including "staticmethod()" and '
jpayne@69 985 '"classmethod()") are\n'
jpayne@69 986 'implemented as non-data descriptors. Accordingly, '
jpayne@69 987 'instances can\n'
jpayne@69 988 'redefine and override methods. This allows individual '
jpayne@69 989 'instances to\n'
jpayne@69 990 'acquire behaviors that differ from other instances of '
jpayne@69 991 'the same class.\n'
jpayne@69 992 '\n'
jpayne@69 993 'The "property()" function is implemented as a data '
jpayne@69 994 'descriptor.\n'
jpayne@69 995 'Accordingly, instances cannot override the behavior of a '
jpayne@69 996 'property.\n'
jpayne@69 997 '\n'
jpayne@69 998 '\n'
jpayne@69 999 '__slots__\n'
jpayne@69 1000 '=========\n'
jpayne@69 1001 '\n'
jpayne@69 1002 '*__slots__* allow us to explicitly declare data members '
jpayne@69 1003 '(like\n'
jpayne@69 1004 'properties) and deny the creation of *__dict__* and '
jpayne@69 1005 '*__weakref__*\n'
jpayne@69 1006 '(unless explicitly declared in *__slots__* or available '
jpayne@69 1007 'in a parent.)\n'
jpayne@69 1008 '\n'
jpayne@69 1009 'The space saved over using *__dict__* can be '
jpayne@69 1010 'significant. Attribute\n'
jpayne@69 1011 'lookup speed can be significantly improved as well.\n'
jpayne@69 1012 '\n'
jpayne@69 1013 'object.__slots__\n'
jpayne@69 1014 '\n'
jpayne@69 1015 ' This class variable can be assigned a string, '
jpayne@69 1016 'iterable, or sequence\n'
jpayne@69 1017 ' of strings with variable names used by instances. '
jpayne@69 1018 '*__slots__*\n'
jpayne@69 1019 ' reserves space for the declared variables and '
jpayne@69 1020 'prevents the\n'
jpayne@69 1021 ' automatic creation of *__dict__* and *__weakref__* '
jpayne@69 1022 'for each\n'
jpayne@69 1023 ' instance.\n'
jpayne@69 1024 '\n'
jpayne@69 1025 '\n'
jpayne@69 1026 'Notes on using *__slots__*\n'
jpayne@69 1027 '--------------------------\n'
jpayne@69 1028 '\n'
jpayne@69 1029 '* When inheriting from a class without *__slots__*, the '
jpayne@69 1030 '*__dict__*\n'
jpayne@69 1031 ' and *__weakref__* attribute of the instances will '
jpayne@69 1032 'always be\n'
jpayne@69 1033 ' accessible.\n'
jpayne@69 1034 '\n'
jpayne@69 1035 '* Without a *__dict__* variable, instances cannot be '
jpayne@69 1036 'assigned new\n'
jpayne@69 1037 ' variables not listed in the *__slots__* definition. '
jpayne@69 1038 'Attempts to\n'
jpayne@69 1039 ' assign to an unlisted variable name raises '
jpayne@69 1040 '"AttributeError". If\n'
jpayne@69 1041 ' dynamic assignment of new variables is desired, then '
jpayne@69 1042 'add\n'
jpayne@69 1043 ' "\'__dict__\'" to the sequence of strings in the '
jpayne@69 1044 '*__slots__*\n'
jpayne@69 1045 ' declaration.\n'
jpayne@69 1046 '\n'
jpayne@69 1047 '* Without a *__weakref__* variable for each instance, '
jpayne@69 1048 'classes\n'
jpayne@69 1049 ' defining *__slots__* do not support weak references to '
jpayne@69 1050 'its\n'
jpayne@69 1051 ' instances. If weak reference support is needed, then '
jpayne@69 1052 'add\n'
jpayne@69 1053 ' "\'__weakref__\'" to the sequence of strings in the '
jpayne@69 1054 '*__slots__*\n'
jpayne@69 1055 ' declaration.\n'
jpayne@69 1056 '\n'
jpayne@69 1057 '* *__slots__* are implemented at the class level by '
jpayne@69 1058 'creating\n'
jpayne@69 1059 ' descriptors (Implementing Descriptors) for each '
jpayne@69 1060 'variable name. As a\n'
jpayne@69 1061 ' result, class attributes cannot be used to set default '
jpayne@69 1062 'values for\n'
jpayne@69 1063 ' instance variables defined by *__slots__*; otherwise, '
jpayne@69 1064 'the class\n'
jpayne@69 1065 ' attribute would overwrite the descriptor assignment.\n'
jpayne@69 1066 '\n'
jpayne@69 1067 '* The action of a *__slots__* declaration is not limited '
jpayne@69 1068 'to the\n'
jpayne@69 1069 ' class where it is defined. *__slots__* declared in '
jpayne@69 1070 'parents are\n'
jpayne@69 1071 ' available in child classes. However, child subclasses '
jpayne@69 1072 'will get a\n'
jpayne@69 1073 ' *__dict__* and *__weakref__* unless they also define '
jpayne@69 1074 '*__slots__*\n'
jpayne@69 1075 ' (which should only contain names of any *additional* '
jpayne@69 1076 'slots).\n'
jpayne@69 1077 '\n'
jpayne@69 1078 '* If a class defines a slot also defined in a base '
jpayne@69 1079 'class, the\n'
jpayne@69 1080 ' instance variable defined by the base class slot is '
jpayne@69 1081 'inaccessible\n'
jpayne@69 1082 ' (except by retrieving its descriptor directly from the '
jpayne@69 1083 'base class).\n'
jpayne@69 1084 ' This renders the meaning of the program undefined. In '
jpayne@69 1085 'the future, a\n'
jpayne@69 1086 ' check may be added to prevent this.\n'
jpayne@69 1087 '\n'
jpayne@69 1088 '* Nonempty *__slots__* does not work for classes derived '
jpayne@69 1089 'from\n'
jpayne@69 1090 ' “variable-length” built-in types such as "int", '
jpayne@69 1091 '"bytes" and "tuple".\n'
jpayne@69 1092 '\n'
jpayne@69 1093 '* Any non-string iterable may be assigned to '
jpayne@69 1094 '*__slots__*. Mappings\n'
jpayne@69 1095 ' may also be used; however, in the future, special '
jpayne@69 1096 'meaning may be\n'
jpayne@69 1097 ' assigned to the values corresponding to each key.\n'
jpayne@69 1098 '\n'
jpayne@69 1099 '* *__class__* assignment works only if both classes have '
jpayne@69 1100 'the same\n'
jpayne@69 1101 ' *__slots__*.\n'
jpayne@69 1102 '\n'
jpayne@69 1103 '* Multiple inheritance with multiple slotted parent '
jpayne@69 1104 'classes can be\n'
jpayne@69 1105 ' used, but only one parent is allowed to have '
jpayne@69 1106 'attributes created by\n'
jpayne@69 1107 ' slots (the other bases must have empty slot layouts) - '
jpayne@69 1108 'violations\n'
jpayne@69 1109 ' raise "TypeError".\n'
jpayne@69 1110 '\n'
jpayne@69 1111 '* If an iterator is used for *__slots__* then a '
jpayne@69 1112 'descriptor is\n'
jpayne@69 1113 ' created for each of the iterator’s values. However, '
jpayne@69 1114 'the *__slots__*\n'
jpayne@69 1115 ' attribute will be an empty iterator.\n',
jpayne@69 1116 'attribute-references': 'Attribute references\n'
jpayne@69 1117 '********************\n'
jpayne@69 1118 '\n'
jpayne@69 1119 'An attribute reference is a primary followed by a '
jpayne@69 1120 'period and a name:\n'
jpayne@69 1121 '\n'
jpayne@69 1122 ' attributeref ::= primary "." identifier\n'
jpayne@69 1123 '\n'
jpayne@69 1124 'The primary must evaluate to an object of a type '
jpayne@69 1125 'that supports\n'
jpayne@69 1126 'attribute references, which most objects do. This '
jpayne@69 1127 'object is then\n'
jpayne@69 1128 'asked to produce the attribute whose name is the '
jpayne@69 1129 'identifier. This\n'
jpayne@69 1130 'production can be customized by overriding the '
jpayne@69 1131 '"__getattr__()" method.\n'
jpayne@69 1132 'If this attribute is not available, the exception '
jpayne@69 1133 '"AttributeError" is\n'
jpayne@69 1134 'raised. Otherwise, the type and value of the object '
jpayne@69 1135 'produced is\n'
jpayne@69 1136 'determined by the object. Multiple evaluations of '
jpayne@69 1137 'the same attribute\n'
jpayne@69 1138 'reference may yield different objects.\n',
jpayne@69 1139 'augassign': 'Augmented assignment statements\n'
jpayne@69 1140 '*******************************\n'
jpayne@69 1141 '\n'
jpayne@69 1142 'Augmented assignment is the combination, in a single statement, '
jpayne@69 1143 'of a\n'
jpayne@69 1144 'binary operation and an assignment statement:\n'
jpayne@69 1145 '\n'
jpayne@69 1146 ' augmented_assignment_stmt ::= augtarget augop '
jpayne@69 1147 '(expression_list | yield_expression)\n'
jpayne@69 1148 ' augtarget ::= identifier | attributeref | '
jpayne@69 1149 'subscription | slicing\n'
jpayne@69 1150 ' augop ::= "+=" | "-=" | "*=" | "@=" | '
jpayne@69 1151 '"/=" | "//=" | "%=" | "**="\n'
jpayne@69 1152 ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n'
jpayne@69 1153 '\n'
jpayne@69 1154 '(See section Primaries for the syntax definitions of the last '
jpayne@69 1155 'three\n'
jpayne@69 1156 'symbols.)\n'
jpayne@69 1157 '\n'
jpayne@69 1158 'An augmented assignment evaluates the target (which, unlike '
jpayne@69 1159 'normal\n'
jpayne@69 1160 'assignment statements, cannot be an unpacking) and the '
jpayne@69 1161 'expression\n'
jpayne@69 1162 'list, performs the binary operation specific to the type of '
jpayne@69 1163 'assignment\n'
jpayne@69 1164 'on the two operands, and assigns the result to the original '
jpayne@69 1165 'target.\n'
jpayne@69 1166 'The target is only evaluated once.\n'
jpayne@69 1167 '\n'
jpayne@69 1168 'An augmented assignment expression like "x += 1" can be '
jpayne@69 1169 'rewritten as\n'
jpayne@69 1170 '"x = x + 1" to achieve a similar, but not exactly equal effect. '
jpayne@69 1171 'In the\n'
jpayne@69 1172 'augmented version, "x" is only evaluated once. Also, when '
jpayne@69 1173 'possible,\n'
jpayne@69 1174 'the actual operation is performed *in-place*, meaning that '
jpayne@69 1175 'rather than\n'
jpayne@69 1176 'creating a new object and assigning that to the target, the old '
jpayne@69 1177 'object\n'
jpayne@69 1178 'is modified instead.\n'
jpayne@69 1179 '\n'
jpayne@69 1180 'Unlike normal assignments, augmented assignments evaluate the '
jpayne@69 1181 'left-\n'
jpayne@69 1182 'hand side *before* evaluating the right-hand side. For '
jpayne@69 1183 'example, "a[i]\n'
jpayne@69 1184 '+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and '
jpayne@69 1185 'performs\n'
jpayne@69 1186 'the addition, and lastly, it writes the result back to "a[i]".\n'
jpayne@69 1187 '\n'
jpayne@69 1188 'With the exception of assigning to tuples and multiple targets '
jpayne@69 1189 'in a\n'
jpayne@69 1190 'single statement, the assignment done by augmented assignment\n'
jpayne@69 1191 'statements is handled the same way as normal assignments. '
jpayne@69 1192 'Similarly,\n'
jpayne@69 1193 'with the exception of the possible *in-place* behavior, the '
jpayne@69 1194 'binary\n'
jpayne@69 1195 'operation performed by augmented assignment is the same as the '
jpayne@69 1196 'normal\n'
jpayne@69 1197 'binary operations.\n'
jpayne@69 1198 '\n'
jpayne@69 1199 'For targets which are attribute references, the same caveat '
jpayne@69 1200 'about\n'
jpayne@69 1201 'class and instance attributes applies as for regular '
jpayne@69 1202 'assignments.\n',
jpayne@69 1203 'await': 'Await expression\n'
jpayne@69 1204 '****************\n'
jpayne@69 1205 '\n'
jpayne@69 1206 'Suspend the execution of *coroutine* on an *awaitable* object. Can\n'
jpayne@69 1207 'only be used inside a *coroutine function*.\n'
jpayne@69 1208 '\n'
jpayne@69 1209 ' await_expr ::= "await" primary\n'
jpayne@69 1210 '\n'
jpayne@69 1211 'New in version 3.5.\n',
jpayne@69 1212 'binary': 'Binary arithmetic operations\n'
jpayne@69 1213 '****************************\n'
jpayne@69 1214 '\n'
jpayne@69 1215 'The binary arithmetic operations have the conventional priority\n'
jpayne@69 1216 'levels. Note that some of these operations also apply to certain '
jpayne@69 1217 'non-\n'
jpayne@69 1218 'numeric types. Apart from the power operator, there are only two\n'
jpayne@69 1219 'levels, one for multiplicative operators and one for additive\n'
jpayne@69 1220 'operators:\n'
jpayne@69 1221 '\n'
jpayne@69 1222 ' m_expr ::= u_expr | m_expr "*" u_expr | m_expr "@" m_expr |\n'
jpayne@69 1223 ' m_expr "//" u_expr | m_expr "/" u_expr |\n'
jpayne@69 1224 ' m_expr "%" u_expr\n'
jpayne@69 1225 ' a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr\n'
jpayne@69 1226 '\n'
jpayne@69 1227 'The "*" (multiplication) operator yields the product of its '
jpayne@69 1228 'arguments.\n'
jpayne@69 1229 'The arguments must either both be numbers, or one argument must be '
jpayne@69 1230 'an\n'
jpayne@69 1231 'integer and the other must be a sequence. In the former case, the\n'
jpayne@69 1232 'numbers are converted to a common type and then multiplied '
jpayne@69 1233 'together.\n'
jpayne@69 1234 'In the latter case, sequence repetition is performed; a negative\n'
jpayne@69 1235 'repetition factor yields an empty sequence.\n'
jpayne@69 1236 '\n'
jpayne@69 1237 'The "@" (at) operator is intended to be used for matrix\n'
jpayne@69 1238 'multiplication. No builtin Python types implement this operator.\n'
jpayne@69 1239 '\n'
jpayne@69 1240 'New in version 3.5.\n'
jpayne@69 1241 '\n'
jpayne@69 1242 'The "/" (division) and "//" (floor division) operators yield the\n'
jpayne@69 1243 'quotient of their arguments. The numeric arguments are first\n'
jpayne@69 1244 'converted to a common type. Division of integers yields a float, '
jpayne@69 1245 'while\n'
jpayne@69 1246 'floor division of integers results in an integer; the result is '
jpayne@69 1247 'that\n'
jpayne@69 1248 'of mathematical division with the ‘floor’ function applied to the\n'
jpayne@69 1249 'result. Division by zero raises the "ZeroDivisionError" '
jpayne@69 1250 'exception.\n'
jpayne@69 1251 '\n'
jpayne@69 1252 'The "%" (modulo) operator yields the remainder from the division '
jpayne@69 1253 'of\n'
jpayne@69 1254 'the first argument by the second. The numeric arguments are '
jpayne@69 1255 'first\n'
jpayne@69 1256 'converted to a common type. A zero right argument raises the\n'
jpayne@69 1257 '"ZeroDivisionError" exception. The arguments may be floating '
jpayne@69 1258 'point\n'
jpayne@69 1259 'numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals '
jpayne@69 1260 '"4*0.7 +\n'
jpayne@69 1261 '0.34".) The modulo operator always yields a result with the same '
jpayne@69 1262 'sign\n'
jpayne@69 1263 'as its second operand (or zero); the absolute value of the result '
jpayne@69 1264 'is\n'
jpayne@69 1265 'strictly smaller than the absolute value of the second operand '
jpayne@69 1266 '[1].\n'
jpayne@69 1267 '\n'
jpayne@69 1268 'The floor division and modulo operators are connected by the '
jpayne@69 1269 'following\n'
jpayne@69 1270 'identity: "x == (x//y)*y + (x%y)". Floor division and modulo are '
jpayne@69 1271 'also\n'
jpayne@69 1272 'connected with the built-in function "divmod()": "divmod(x, y) ==\n'
jpayne@69 1273 '(x//y, x%y)". [2].\n'
jpayne@69 1274 '\n'
jpayne@69 1275 'In addition to performing the modulo operation on numbers, the '
jpayne@69 1276 '"%"\n'
jpayne@69 1277 'operator is also overloaded by string objects to perform '
jpayne@69 1278 'old-style\n'
jpayne@69 1279 'string formatting (also known as interpolation). The syntax for\n'
jpayne@69 1280 'string formatting is described in the Python Library Reference,\n'
jpayne@69 1281 'section printf-style String Formatting.\n'
jpayne@69 1282 '\n'
jpayne@69 1283 'The floor division operator, the modulo operator, and the '
jpayne@69 1284 '"divmod()"\n'
jpayne@69 1285 'function are not defined for complex numbers. Instead, convert to '
jpayne@69 1286 'a\n'
jpayne@69 1287 'floating point number using the "abs()" function if appropriate.\n'
jpayne@69 1288 '\n'
jpayne@69 1289 'The "+" (addition) operator yields the sum of its arguments. The\n'
jpayne@69 1290 'arguments must either both be numbers or both be sequences of the '
jpayne@69 1291 'same\n'
jpayne@69 1292 'type. In the former case, the numbers are converted to a common '
jpayne@69 1293 'type\n'
jpayne@69 1294 'and then added together. In the latter case, the sequences are\n'
jpayne@69 1295 'concatenated.\n'
jpayne@69 1296 '\n'
jpayne@69 1297 'The "-" (subtraction) operator yields the difference of its '
jpayne@69 1298 'arguments.\n'
jpayne@69 1299 'The numeric arguments are first converted to a common type.\n',
jpayne@69 1300 'bitwise': 'Binary bitwise operations\n'
jpayne@69 1301 '*************************\n'
jpayne@69 1302 '\n'
jpayne@69 1303 'Each of the three bitwise operations has a different priority '
jpayne@69 1304 'level:\n'
jpayne@69 1305 '\n'
jpayne@69 1306 ' and_expr ::= shift_expr | and_expr "&" shift_expr\n'
jpayne@69 1307 ' xor_expr ::= and_expr | xor_expr "^" and_expr\n'
jpayne@69 1308 ' or_expr ::= xor_expr | or_expr "|" xor_expr\n'
jpayne@69 1309 '\n'
jpayne@69 1310 'The "&" operator yields the bitwise AND of its arguments, which '
jpayne@69 1311 'must\n'
jpayne@69 1312 'be integers.\n'
jpayne@69 1313 '\n'
jpayne@69 1314 'The "^" operator yields the bitwise XOR (exclusive OR) of its\n'
jpayne@69 1315 'arguments, which must be integers.\n'
jpayne@69 1316 '\n'
jpayne@69 1317 'The "|" operator yields the bitwise (inclusive) OR of its '
jpayne@69 1318 'arguments,\n'
jpayne@69 1319 'which must be integers.\n',
jpayne@69 1320 'bltin-code-objects': 'Code Objects\n'
jpayne@69 1321 '************\n'
jpayne@69 1322 '\n'
jpayne@69 1323 'Code objects are used by the implementation to '
jpayne@69 1324 'represent “pseudo-\n'
jpayne@69 1325 'compiled” executable Python code such as a function '
jpayne@69 1326 'body. They differ\n'
jpayne@69 1327 'from function objects because they don’t contain a '
jpayne@69 1328 'reference to their\n'
jpayne@69 1329 'global execution environment. Code objects are '
jpayne@69 1330 'returned by the built-\n'
jpayne@69 1331 'in "compile()" function and can be extracted from '
jpayne@69 1332 'function objects\n'
jpayne@69 1333 'through their "__code__" attribute. See also the '
jpayne@69 1334 '"code" module.\n'
jpayne@69 1335 '\n'
jpayne@69 1336 'A code object can be executed or evaluated by passing '
jpayne@69 1337 'it (instead of a\n'
jpayne@69 1338 'source string) to the "exec()" or "eval()" built-in '
jpayne@69 1339 'functions.\n'
jpayne@69 1340 '\n'
jpayne@69 1341 'See The standard type hierarchy for more '
jpayne@69 1342 'information.\n',
jpayne@69 1343 'bltin-ellipsis-object': 'The Ellipsis Object\n'
jpayne@69 1344 '*******************\n'
jpayne@69 1345 '\n'
jpayne@69 1346 'This object is commonly used by slicing (see '
jpayne@69 1347 'Slicings). It supports\n'
jpayne@69 1348 'no special operations. There is exactly one '
jpayne@69 1349 'ellipsis object, named\n'
jpayne@69 1350 '"Ellipsis" (a built-in name). "type(Ellipsis)()" '
jpayne@69 1351 'produces the\n'
jpayne@69 1352 '"Ellipsis" singleton.\n'
jpayne@69 1353 '\n'
jpayne@69 1354 'It is written as "Ellipsis" or "...".\n',
jpayne@69 1355 'bltin-null-object': 'The Null Object\n'
jpayne@69 1356 '***************\n'
jpayne@69 1357 '\n'
jpayne@69 1358 'This object is returned by functions that don’t '
jpayne@69 1359 'explicitly return a\n'
jpayne@69 1360 'value. It supports no special operations. There is '
jpayne@69 1361 'exactly one null\n'
jpayne@69 1362 'object, named "None" (a built-in name). "type(None)()" '
jpayne@69 1363 'produces the\n'
jpayne@69 1364 'same singleton.\n'
jpayne@69 1365 '\n'
jpayne@69 1366 'It is written as "None".\n',
jpayne@69 1367 'bltin-type-objects': 'Type Objects\n'
jpayne@69 1368 '************\n'
jpayne@69 1369 '\n'
jpayne@69 1370 'Type objects represent the various object types. An '
jpayne@69 1371 'object’s type is\n'
jpayne@69 1372 'accessed by the built-in function "type()". There are '
jpayne@69 1373 'no special\n'
jpayne@69 1374 'operations on types. The standard module "types" '
jpayne@69 1375 'defines names for\n'
jpayne@69 1376 'all standard built-in types.\n'
jpayne@69 1377 '\n'
jpayne@69 1378 'Types are written like this: "<class \'int\'>".\n',
jpayne@69 1379 'booleans': 'Boolean operations\n'
jpayne@69 1380 '******************\n'
jpayne@69 1381 '\n'
jpayne@69 1382 ' or_test ::= and_test | or_test "or" and_test\n'
jpayne@69 1383 ' and_test ::= not_test | and_test "and" not_test\n'
jpayne@69 1384 ' not_test ::= comparison | "not" not_test\n'
jpayne@69 1385 '\n'
jpayne@69 1386 'In the context of Boolean operations, and also when expressions '
jpayne@69 1387 'are\n'
jpayne@69 1388 'used by control flow statements, the following values are '
jpayne@69 1389 'interpreted\n'
jpayne@69 1390 'as false: "False", "None", numeric zero of all types, and empty\n'
jpayne@69 1391 'strings and containers (including strings, tuples, lists,\n'
jpayne@69 1392 'dictionaries, sets and frozensets). All other values are '
jpayne@69 1393 'interpreted\n'
jpayne@69 1394 'as true. User-defined objects can customize their truth value '
jpayne@69 1395 'by\n'
jpayne@69 1396 'providing a "__bool__()" method.\n'
jpayne@69 1397 '\n'
jpayne@69 1398 'The operator "not" yields "True" if its argument is false, '
jpayne@69 1399 '"False"\n'
jpayne@69 1400 'otherwise.\n'
jpayne@69 1401 '\n'
jpayne@69 1402 'The expression "x and y" first evaluates *x*; if *x* is false, '
jpayne@69 1403 'its\n'
jpayne@69 1404 'value is returned; otherwise, *y* is evaluated and the resulting '
jpayne@69 1405 'value\n'
jpayne@69 1406 'is returned.\n'
jpayne@69 1407 '\n'
jpayne@69 1408 'The expression "x or y" first evaluates *x*; if *x* is true, its '
jpayne@69 1409 'value\n'
jpayne@69 1410 'is returned; otherwise, *y* is evaluated and the resulting value '
jpayne@69 1411 'is\n'
jpayne@69 1412 'returned.\n'
jpayne@69 1413 '\n'
jpayne@69 1414 'Note that neither "and" nor "or" restrict the value and type '
jpayne@69 1415 'they\n'
jpayne@69 1416 'return to "False" and "True", but rather return the last '
jpayne@69 1417 'evaluated\n'
jpayne@69 1418 'argument. This is sometimes useful, e.g., if "s" is a string '
jpayne@69 1419 'that\n'
jpayne@69 1420 'should be replaced by a default value if it is empty, the '
jpayne@69 1421 'expression\n'
jpayne@69 1422 '"s or \'foo\'" yields the desired value. Because "not" has to '
jpayne@69 1423 'create a\n'
jpayne@69 1424 'new value, it returns a boolean value regardless of the type of '
jpayne@69 1425 'its\n'
jpayne@69 1426 'argument (for example, "not \'foo\'" produces "False" rather '
jpayne@69 1427 'than "\'\'".)\n',
jpayne@69 1428 'break': 'The "break" statement\n'
jpayne@69 1429 '*********************\n'
jpayne@69 1430 '\n'
jpayne@69 1431 ' break_stmt ::= "break"\n'
jpayne@69 1432 '\n'
jpayne@69 1433 '"break" may only occur syntactically nested in a "for" or "while"\n'
jpayne@69 1434 'loop, but not nested in a function or class definition within that\n'
jpayne@69 1435 'loop.\n'
jpayne@69 1436 '\n'
jpayne@69 1437 'It terminates the nearest enclosing loop, skipping the optional '
jpayne@69 1438 '"else"\n'
jpayne@69 1439 'clause if the loop has one.\n'
jpayne@69 1440 '\n'
jpayne@69 1441 'If a "for" loop is terminated by "break", the loop control target\n'
jpayne@69 1442 'keeps its current value.\n'
jpayne@69 1443 '\n'
jpayne@69 1444 'When "break" passes control out of a "try" statement with a '
jpayne@69 1445 '"finally"\n'
jpayne@69 1446 'clause, that "finally" clause is executed before really leaving '
jpayne@69 1447 'the\n'
jpayne@69 1448 'loop.\n',
jpayne@69 1449 'callable-types': 'Emulating callable objects\n'
jpayne@69 1450 '**************************\n'
jpayne@69 1451 '\n'
jpayne@69 1452 'object.__call__(self[, args...])\n'
jpayne@69 1453 '\n'
jpayne@69 1454 ' Called when the instance is “called” as a function; if '
jpayne@69 1455 'this method\n'
jpayne@69 1456 ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n'
jpayne@69 1457 ' "x.__call__(arg1, arg2, ...)".\n',
jpayne@69 1458 'calls': 'Calls\n'
jpayne@69 1459 '*****\n'
jpayne@69 1460 '\n'
jpayne@69 1461 'A call calls a callable object (e.g., a *function*) with a '
jpayne@69 1462 'possibly\n'
jpayne@69 1463 'empty series of *arguments*:\n'
jpayne@69 1464 '\n'
jpayne@69 1465 ' call ::= primary "(" [argument_list [","] | '
jpayne@69 1466 'comprehension] ")"\n'
jpayne@69 1467 ' argument_list ::= positional_arguments ["," '
jpayne@69 1468 'starred_and_keywords]\n'
jpayne@69 1469 ' ["," keywords_arguments]\n'
jpayne@69 1470 ' | starred_and_keywords ["," '
jpayne@69 1471 'keywords_arguments]\n'
jpayne@69 1472 ' | keywords_arguments\n'
jpayne@69 1473 ' positional_arguments ::= ["*"] expression ("," ["*"] '
jpayne@69 1474 'expression)*\n'
jpayne@69 1475 ' starred_and_keywords ::= ("*" expression | keyword_item)\n'
jpayne@69 1476 ' ("," "*" expression | "," '
jpayne@69 1477 'keyword_item)*\n'
jpayne@69 1478 ' keywords_arguments ::= (keyword_item | "**" expression)\n'
jpayne@69 1479 ' ("," keyword_item | "," "**" '
jpayne@69 1480 'expression)*\n'
jpayne@69 1481 ' keyword_item ::= identifier "=" expression\n'
jpayne@69 1482 '\n'
jpayne@69 1483 'An optional trailing comma may be present after the positional and\n'
jpayne@69 1484 'keyword arguments but does not affect the semantics.\n'
jpayne@69 1485 '\n'
jpayne@69 1486 'The primary must evaluate to a callable object (user-defined\n'
jpayne@69 1487 'functions, built-in functions, methods of built-in objects, class\n'
jpayne@69 1488 'objects, methods of class instances, and all objects having a\n'
jpayne@69 1489 '"__call__()" method are callable). All argument expressions are\n'
jpayne@69 1490 'evaluated before the call is attempted. Please refer to section\n'
jpayne@69 1491 'Function definitions for the syntax of formal *parameter* lists.\n'
jpayne@69 1492 '\n'
jpayne@69 1493 'If keyword arguments are present, they are first converted to\n'
jpayne@69 1494 'positional arguments, as follows. First, a list of unfilled slots '
jpayne@69 1495 'is\n'
jpayne@69 1496 'created for the formal parameters. If there are N positional\n'
jpayne@69 1497 'arguments, they are placed in the first N slots. Next, for each\n'
jpayne@69 1498 'keyword argument, the identifier is used to determine the\n'
jpayne@69 1499 'corresponding slot (if the identifier is the same as the first '
jpayne@69 1500 'formal\n'
jpayne@69 1501 'parameter name, the first slot is used, and so on). If the slot '
jpayne@69 1502 'is\n'
jpayne@69 1503 'already filled, a "TypeError" exception is raised. Otherwise, the\n'
jpayne@69 1504 'value of the argument is placed in the slot, filling it (even if '
jpayne@69 1505 'the\n'
jpayne@69 1506 'expression is "None", it fills the slot). When all arguments have\n'
jpayne@69 1507 'been processed, the slots that are still unfilled are filled with '
jpayne@69 1508 'the\n'
jpayne@69 1509 'corresponding default value from the function definition. '
jpayne@69 1510 '(Default\n'
jpayne@69 1511 'values are calculated, once, when the function is defined; thus, a\n'
jpayne@69 1512 'mutable object such as a list or dictionary used as default value '
jpayne@69 1513 'will\n'
jpayne@69 1514 'be shared by all calls that don’t specify an argument value for '
jpayne@69 1515 'the\n'
jpayne@69 1516 'corresponding slot; this should usually be avoided.) If there are '
jpayne@69 1517 'any\n'
jpayne@69 1518 'unfilled slots for which no default value is specified, a '
jpayne@69 1519 '"TypeError"\n'
jpayne@69 1520 'exception is raised. Otherwise, the list of filled slots is used '
jpayne@69 1521 'as\n'
jpayne@69 1522 'the argument list for the call.\n'
jpayne@69 1523 '\n'
jpayne@69 1524 '**CPython implementation detail:** An implementation may provide\n'
jpayne@69 1525 'built-in functions whose positional parameters do not have names, '
jpayne@69 1526 'even\n'
jpayne@69 1527 'if they are ‘named’ for the purpose of documentation, and which\n'
jpayne@69 1528 'therefore cannot be supplied by keyword. In CPython, this is the '
jpayne@69 1529 'case\n'
jpayne@69 1530 'for functions implemented in C that use "PyArg_ParseTuple()" to '
jpayne@69 1531 'parse\n'
jpayne@69 1532 'their arguments.\n'
jpayne@69 1533 '\n'
jpayne@69 1534 'If there are more positional arguments than there are formal '
jpayne@69 1535 'parameter\n'
jpayne@69 1536 'slots, a "TypeError" exception is raised, unless a formal '
jpayne@69 1537 'parameter\n'
jpayne@69 1538 'using the syntax "*identifier" is present; in this case, that '
jpayne@69 1539 'formal\n'
jpayne@69 1540 'parameter receives a tuple containing the excess positional '
jpayne@69 1541 'arguments\n'
jpayne@69 1542 '(or an empty tuple if there were no excess positional arguments).\n'
jpayne@69 1543 '\n'
jpayne@69 1544 'If any keyword argument does not correspond to a formal parameter\n'
jpayne@69 1545 'name, a "TypeError" exception is raised, unless a formal parameter\n'
jpayne@69 1546 'using the syntax "**identifier" is present; in this case, that '
jpayne@69 1547 'formal\n'
jpayne@69 1548 'parameter receives a dictionary containing the excess keyword\n'
jpayne@69 1549 'arguments (using the keywords as keys and the argument values as\n'
jpayne@69 1550 'corresponding values), or a (new) empty dictionary if there were '
jpayne@69 1551 'no\n'
jpayne@69 1552 'excess keyword arguments.\n'
jpayne@69 1553 '\n'
jpayne@69 1554 'If the syntax "*expression" appears in the function call, '
jpayne@69 1555 '"expression"\n'
jpayne@69 1556 'must evaluate to an *iterable*. Elements from these iterables are\n'
jpayne@69 1557 'treated as if they were additional positional arguments. For the '
jpayne@69 1558 'call\n'
jpayne@69 1559 '"f(x1, x2, *y, x3, x4)", if *y* evaluates to a sequence *y1*, …, '
jpayne@69 1560 '*yM*,\n'
jpayne@69 1561 'this is equivalent to a call with M+4 positional arguments *x1*, '
jpayne@69 1562 '*x2*,\n'
jpayne@69 1563 '*y1*, …, *yM*, *x3*, *x4*.\n'
jpayne@69 1564 '\n'
jpayne@69 1565 'A consequence of this is that although the "*expression" syntax '
jpayne@69 1566 'may\n'
jpayne@69 1567 'appear *after* explicit keyword arguments, it is processed '
jpayne@69 1568 '*before*\n'
jpayne@69 1569 'the keyword arguments (and any "**expression" arguments – see '
jpayne@69 1570 'below).\n'
jpayne@69 1571 'So:\n'
jpayne@69 1572 '\n'
jpayne@69 1573 ' >>> def f(a, b):\n'
jpayne@69 1574 ' ... print(a, b)\n'
jpayne@69 1575 ' ...\n'
jpayne@69 1576 ' >>> f(b=1, *(2,))\n'
jpayne@69 1577 ' 2 1\n'
jpayne@69 1578 ' >>> f(a=1, *(2,))\n'
jpayne@69 1579 ' Traceback (most recent call last):\n'
jpayne@69 1580 ' File "<stdin>", line 1, in <module>\n'
jpayne@69 1581 " TypeError: f() got multiple values for keyword argument 'a'\n"
jpayne@69 1582 ' >>> f(1, *(2,))\n'
jpayne@69 1583 ' 1 2\n'
jpayne@69 1584 '\n'
jpayne@69 1585 'It is unusual for both keyword arguments and the "*expression" '
jpayne@69 1586 'syntax\n'
jpayne@69 1587 'to be used in the same call, so in practice this confusion does '
jpayne@69 1588 'not\n'
jpayne@69 1589 'arise.\n'
jpayne@69 1590 '\n'
jpayne@69 1591 'If the syntax "**expression" appears in the function call,\n'
jpayne@69 1592 '"expression" must evaluate to a *mapping*, the contents of which '
jpayne@69 1593 'are\n'
jpayne@69 1594 'treated as additional keyword arguments. If a keyword is already\n'
jpayne@69 1595 'present (as an explicit keyword argument, or from another '
jpayne@69 1596 'unpacking),\n'
jpayne@69 1597 'a "TypeError" exception is raised.\n'
jpayne@69 1598 '\n'
jpayne@69 1599 'Formal parameters using the syntax "*identifier" or "**identifier"\n'
jpayne@69 1600 'cannot be used as positional argument slots or as keyword argument\n'
jpayne@69 1601 'names.\n'
jpayne@69 1602 '\n'
jpayne@69 1603 'Changed in version 3.5: Function calls accept any number of "*" '
jpayne@69 1604 'and\n'
jpayne@69 1605 '"**" unpackings, positional arguments may follow iterable '
jpayne@69 1606 'unpackings\n'
jpayne@69 1607 '("*"), and keyword arguments may follow dictionary unpackings '
jpayne@69 1608 '("**").\n'
jpayne@69 1609 'Originally proposed by **PEP 448**.\n'
jpayne@69 1610 '\n'
jpayne@69 1611 'A call always returns some value, possibly "None", unless it raises '
jpayne@69 1612 'an\n'
jpayne@69 1613 'exception. How this value is computed depends on the type of the\n'
jpayne@69 1614 'callable object.\n'
jpayne@69 1615 '\n'
jpayne@69 1616 'If it is—\n'
jpayne@69 1617 '\n'
jpayne@69 1618 'a user-defined function:\n'
jpayne@69 1619 ' The code block for the function is executed, passing it the\n'
jpayne@69 1620 ' argument list. The first thing the code block will do is bind '
jpayne@69 1621 'the\n'
jpayne@69 1622 ' formal parameters to the arguments; this is described in '
jpayne@69 1623 'section\n'
jpayne@69 1624 ' Function definitions. When the code block executes a "return"\n'
jpayne@69 1625 ' statement, this specifies the return value of the function '
jpayne@69 1626 'call.\n'
jpayne@69 1627 '\n'
jpayne@69 1628 'a built-in function or method:\n'
jpayne@69 1629 ' The result is up to the interpreter; see Built-in Functions for '
jpayne@69 1630 'the\n'
jpayne@69 1631 ' descriptions of built-in functions and methods.\n'
jpayne@69 1632 '\n'
jpayne@69 1633 'a class object:\n'
jpayne@69 1634 ' A new instance of that class is returned.\n'
jpayne@69 1635 '\n'
jpayne@69 1636 'a class instance method:\n'
jpayne@69 1637 ' The corresponding user-defined function is called, with an '
jpayne@69 1638 'argument\n'
jpayne@69 1639 ' list that is one longer than the argument list of the call: the\n'
jpayne@69 1640 ' instance becomes the first argument.\n'
jpayne@69 1641 '\n'
jpayne@69 1642 'a class instance:\n'
jpayne@69 1643 ' The class must define a "__call__()" method; the effect is then '
jpayne@69 1644 'the\n'
jpayne@69 1645 ' same as if that method was called.\n',
jpayne@69 1646 'class': 'Class definitions\n'
jpayne@69 1647 '*****************\n'
jpayne@69 1648 '\n'
jpayne@69 1649 'A class definition defines a class object (see section The '
jpayne@69 1650 'standard\n'
jpayne@69 1651 'type hierarchy):\n'
jpayne@69 1652 '\n'
jpayne@69 1653 ' classdef ::= [decorators] "class" classname [inheritance] ":" '
jpayne@69 1654 'suite\n'
jpayne@69 1655 ' inheritance ::= "(" [argument_list] ")"\n'
jpayne@69 1656 ' classname ::= identifier\n'
jpayne@69 1657 '\n'
jpayne@69 1658 'A class definition is an executable statement. The inheritance '
jpayne@69 1659 'list\n'
jpayne@69 1660 'usually gives a list of base classes (see Metaclasses for more\n'
jpayne@69 1661 'advanced uses), so each item in the list should evaluate to a '
jpayne@69 1662 'class\n'
jpayne@69 1663 'object which allows subclassing. Classes without an inheritance '
jpayne@69 1664 'list\n'
jpayne@69 1665 'inherit, by default, from the base class "object"; hence,\n'
jpayne@69 1666 '\n'
jpayne@69 1667 ' class Foo:\n'
jpayne@69 1668 ' pass\n'
jpayne@69 1669 '\n'
jpayne@69 1670 'is equivalent to\n'
jpayne@69 1671 '\n'
jpayne@69 1672 ' class Foo(object):\n'
jpayne@69 1673 ' pass\n'
jpayne@69 1674 '\n'
jpayne@69 1675 'The class’s suite is then executed in a new execution frame (see\n'
jpayne@69 1676 'Naming and binding), using a newly created local namespace and the\n'
jpayne@69 1677 'original global namespace. (Usually, the suite contains mostly\n'
jpayne@69 1678 'function definitions.) When the class’s suite finishes execution, '
jpayne@69 1679 'its\n'
jpayne@69 1680 'execution frame is discarded but its local namespace is saved. [3] '
jpayne@69 1681 'A\n'
jpayne@69 1682 'class object is then created using the inheritance list for the '
jpayne@69 1683 'base\n'
jpayne@69 1684 'classes and the saved local namespace for the attribute '
jpayne@69 1685 'dictionary.\n'
jpayne@69 1686 'The class name is bound to this class object in the original local\n'
jpayne@69 1687 'namespace.\n'
jpayne@69 1688 '\n'
jpayne@69 1689 'The order in which attributes are defined in the class body is\n'
jpayne@69 1690 'preserved in the new class’s "__dict__". Note that this is '
jpayne@69 1691 'reliable\n'
jpayne@69 1692 'only right after the class is created and only for classes that '
jpayne@69 1693 'were\n'
jpayne@69 1694 'defined using the definition syntax.\n'
jpayne@69 1695 '\n'
jpayne@69 1696 'Class creation can be customized heavily using metaclasses.\n'
jpayne@69 1697 '\n'
jpayne@69 1698 'Classes can also be decorated: just like when decorating '
jpayne@69 1699 'functions,\n'
jpayne@69 1700 '\n'
jpayne@69 1701 ' @f1(arg)\n'
jpayne@69 1702 ' @f2\n'
jpayne@69 1703 ' class Foo: pass\n'
jpayne@69 1704 '\n'
jpayne@69 1705 'is roughly equivalent to\n'
jpayne@69 1706 '\n'
jpayne@69 1707 ' class Foo: pass\n'
jpayne@69 1708 ' Foo = f1(arg)(f2(Foo))\n'
jpayne@69 1709 '\n'
jpayne@69 1710 'The evaluation rules for the decorator expressions are the same as '
jpayne@69 1711 'for\n'
jpayne@69 1712 'function decorators. The result is then bound to the class name.\n'
jpayne@69 1713 '\n'
jpayne@69 1714 '**Programmer’s note:** Variables defined in the class definition '
jpayne@69 1715 'are\n'
jpayne@69 1716 'class attributes; they are shared by instances. Instance '
jpayne@69 1717 'attributes\n'
jpayne@69 1718 'can be set in a method with "self.name = value". Both class and\n'
jpayne@69 1719 'instance attributes are accessible through the notation '
jpayne@69 1720 '“"self.name"”,\n'
jpayne@69 1721 'and an instance attribute hides a class attribute with the same '
jpayne@69 1722 'name\n'
jpayne@69 1723 'when accessed in this way. Class attributes can be used as '
jpayne@69 1724 'defaults\n'
jpayne@69 1725 'for instance attributes, but using mutable values there can lead '
jpayne@69 1726 'to\n'
jpayne@69 1727 'unexpected results. Descriptors can be used to create instance\n'
jpayne@69 1728 'variables with different implementation details.\n'
jpayne@69 1729 '\n'
jpayne@69 1730 'See also:\n'
jpayne@69 1731 '\n'
jpayne@69 1732 ' **PEP 3115** - Metaclasses in Python 3000\n'
jpayne@69 1733 ' The proposal that changed the declaration of metaclasses to '
jpayne@69 1734 'the\n'
jpayne@69 1735 ' current syntax, and the semantics for how classes with\n'
jpayne@69 1736 ' metaclasses are constructed.\n'
jpayne@69 1737 '\n'
jpayne@69 1738 ' **PEP 3129** - Class Decorators\n'
jpayne@69 1739 ' The proposal that added class decorators. Function and '
jpayne@69 1740 'method\n'
jpayne@69 1741 ' decorators were introduced in **PEP 318**.\n',
jpayne@69 1742 'comparisons': 'Comparisons\n'
jpayne@69 1743 '***********\n'
jpayne@69 1744 '\n'
jpayne@69 1745 'Unlike C, all comparison operations in Python have the same '
jpayne@69 1746 'priority,\n'
jpayne@69 1747 'which is lower than that of any arithmetic, shifting or '
jpayne@69 1748 'bitwise\n'
jpayne@69 1749 'operation. Also unlike C, expressions like "a < b < c" have '
jpayne@69 1750 'the\n'
jpayne@69 1751 'interpretation that is conventional in mathematics:\n'
jpayne@69 1752 '\n'
jpayne@69 1753 ' comparison ::= or_expr (comp_operator or_expr)*\n'
jpayne@69 1754 ' comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!="\n'
jpayne@69 1755 ' | "is" ["not"] | ["not"] "in"\n'
jpayne@69 1756 '\n'
jpayne@69 1757 'Comparisons yield boolean values: "True" or "False".\n'
jpayne@69 1758 '\n'
jpayne@69 1759 'Comparisons can be chained arbitrarily, e.g., "x < y <= z" '
jpayne@69 1760 'is\n'
jpayne@69 1761 'equivalent to "x < y and y <= z", except that "y" is '
jpayne@69 1762 'evaluated only\n'
jpayne@69 1763 'once (but in both cases "z" is not evaluated at all when "x < '
jpayne@69 1764 'y" is\n'
jpayne@69 1765 'found to be false).\n'
jpayne@69 1766 '\n'
jpayne@69 1767 'Formally, if *a*, *b*, *c*, …, *y*, *z* are expressions and '
jpayne@69 1768 '*op1*,\n'
jpayne@69 1769 '*op2*, …, *opN* are comparison operators, then "a op1 b op2 c '
jpayne@69 1770 '... y\n'
jpayne@69 1771 'opN z" is equivalent to "a op1 b and b op2 c and ... y opN '
jpayne@69 1772 'z", except\n'
jpayne@69 1773 'that each expression is evaluated at most once.\n'
jpayne@69 1774 '\n'
jpayne@69 1775 'Note that "a op1 b op2 c" doesn’t imply any kind of '
jpayne@69 1776 'comparison between\n'
jpayne@69 1777 '*a* and *c*, so that, e.g., "x < y > z" is perfectly legal '
jpayne@69 1778 '(though\n'
jpayne@69 1779 'perhaps not pretty).\n'
jpayne@69 1780 '\n'
jpayne@69 1781 '\n'
jpayne@69 1782 'Value comparisons\n'
jpayne@69 1783 '=================\n'
jpayne@69 1784 '\n'
jpayne@69 1785 'The operators "<", ">", "==", ">=", "<=", and "!=" compare '
jpayne@69 1786 'the values\n'
jpayne@69 1787 'of two objects. The objects do not need to have the same '
jpayne@69 1788 'type.\n'
jpayne@69 1789 '\n'
jpayne@69 1790 'Chapter Objects, values and types states that objects have a '
jpayne@69 1791 'value (in\n'
jpayne@69 1792 'addition to type and identity). The value of an object is a '
jpayne@69 1793 'rather\n'
jpayne@69 1794 'abstract notion in Python: For example, there is no canonical '
jpayne@69 1795 'access\n'
jpayne@69 1796 'method for an object’s value. Also, there is no requirement '
jpayne@69 1797 'that the\n'
jpayne@69 1798 'value of an object should be constructed in a particular way, '
jpayne@69 1799 'e.g.\n'
jpayne@69 1800 'comprised of all its data attributes. Comparison operators '
jpayne@69 1801 'implement a\n'
jpayne@69 1802 'particular notion of what the value of an object is. One can '
jpayne@69 1803 'think of\n'
jpayne@69 1804 'them as defining the value of an object indirectly, by means '
jpayne@69 1805 'of their\n'
jpayne@69 1806 'comparison implementation.\n'
jpayne@69 1807 '\n'
jpayne@69 1808 'Because all types are (direct or indirect) subtypes of '
jpayne@69 1809 '"object", they\n'
jpayne@69 1810 'inherit the default comparison behavior from "object". Types '
jpayne@69 1811 'can\n'
jpayne@69 1812 'customize their comparison behavior by implementing *rich '
jpayne@69 1813 'comparison\n'
jpayne@69 1814 'methods* like "__lt__()", described in Basic customization.\n'
jpayne@69 1815 '\n'
jpayne@69 1816 'The default behavior for equality comparison ("==" and "!=") '
jpayne@69 1817 'is based\n'
jpayne@69 1818 'on the identity of the objects. Hence, equality comparison '
jpayne@69 1819 'of\n'
jpayne@69 1820 'instances with the same identity results in equality, and '
jpayne@69 1821 'equality\n'
jpayne@69 1822 'comparison of instances with different identities results in\n'
jpayne@69 1823 'inequality. A motivation for this default behavior is the '
jpayne@69 1824 'desire that\n'
jpayne@69 1825 'all objects should be reflexive (i.e. "x is y" implies "x == '
jpayne@69 1826 'y").\n'
jpayne@69 1827 '\n'
jpayne@69 1828 'A default order comparison ("<", ">", "<=", and ">=") is not '
jpayne@69 1829 'provided;\n'
jpayne@69 1830 'an attempt raises "TypeError". A motivation for this default '
jpayne@69 1831 'behavior\n'
jpayne@69 1832 'is the lack of a similar invariant as for equality.\n'
jpayne@69 1833 '\n'
jpayne@69 1834 'The behavior of the default equality comparison, that '
jpayne@69 1835 'instances with\n'
jpayne@69 1836 'different identities are always unequal, may be in contrast '
jpayne@69 1837 'to what\n'
jpayne@69 1838 'types will need that have a sensible definition of object '
jpayne@69 1839 'value and\n'
jpayne@69 1840 'value-based equality. Such types will need to customize '
jpayne@69 1841 'their\n'
jpayne@69 1842 'comparison behavior, and in fact, a number of built-in types '
jpayne@69 1843 'have done\n'
jpayne@69 1844 'that.\n'
jpayne@69 1845 '\n'
jpayne@69 1846 'The following list describes the comparison behavior of the '
jpayne@69 1847 'most\n'
jpayne@69 1848 'important built-in types.\n'
jpayne@69 1849 '\n'
jpayne@69 1850 '* Numbers of built-in numeric types (Numeric Types — int, '
jpayne@69 1851 'float,\n'
jpayne@69 1852 ' complex) and of the standard library types '
jpayne@69 1853 '"fractions.Fraction" and\n'
jpayne@69 1854 ' "decimal.Decimal" can be compared within and across their '
jpayne@69 1855 'types,\n'
jpayne@69 1856 ' with the restriction that complex numbers do not support '
jpayne@69 1857 'order\n'
jpayne@69 1858 ' comparison. Within the limits of the types involved, they '
jpayne@69 1859 'compare\n'
jpayne@69 1860 ' mathematically (algorithmically) correct without loss of '
jpayne@69 1861 'precision.\n'
jpayne@69 1862 '\n'
jpayne@69 1863 ' The not-a-number values "float(\'NaN\')" and '
jpayne@69 1864 '"decimal.Decimal(\'NaN\')"\n'
jpayne@69 1865 ' are special. Any ordered comparison of a number to a '
jpayne@69 1866 'not-a-number\n'
jpayne@69 1867 ' value is false. A counter-intuitive implication is that '
jpayne@69 1868 'not-a-number\n'
jpayne@69 1869 ' values are not equal to themselves. For example, if "x =\n'
jpayne@69 1870 ' float(\'NaN\')", "3 < x", "x < 3", "x == x", "x != x" are '
jpayne@69 1871 'all false.\n'
jpayne@69 1872 ' This behavior is compliant with IEEE 754.\n'
jpayne@69 1873 '\n'
jpayne@69 1874 '* "None" and "NotImplemented" are singletons. **PEP 8** '
jpayne@69 1875 'advises\n'
jpayne@69 1876 ' that comparisons for singletons should always be done with '
jpayne@69 1877 '"is" or\n'
jpayne@69 1878 ' "is not", never the equality operators.\n'
jpayne@69 1879 '\n'
jpayne@69 1880 '* Binary sequences (instances of "bytes" or "bytearray") can '
jpayne@69 1881 'be\n'
jpayne@69 1882 ' compared within and across their types. They compare\n'
jpayne@69 1883 ' lexicographically using the numeric values of their '
jpayne@69 1884 'elements.\n'
jpayne@69 1885 '\n'
jpayne@69 1886 '* Strings (instances of "str") compare lexicographically '
jpayne@69 1887 'using the\n'
jpayne@69 1888 ' numerical Unicode code points (the result of the built-in '
jpayne@69 1889 'function\n'
jpayne@69 1890 ' "ord()") of their characters. [3]\n'
jpayne@69 1891 '\n'
jpayne@69 1892 ' Strings and binary sequences cannot be directly compared.\n'
jpayne@69 1893 '\n'
jpayne@69 1894 '* Sequences (instances of "tuple", "list", or "range") can '
jpayne@69 1895 'be\n'
jpayne@69 1896 ' compared only within each of their types, with the '
jpayne@69 1897 'restriction that\n'
jpayne@69 1898 ' ranges do not support order comparison. Equality '
jpayne@69 1899 'comparison across\n'
jpayne@69 1900 ' these types results in inequality, and ordering comparison '
jpayne@69 1901 'across\n'
jpayne@69 1902 ' these types raises "TypeError".\n'
jpayne@69 1903 '\n'
jpayne@69 1904 ' Sequences compare lexicographically using comparison of\n'
jpayne@69 1905 ' corresponding elements. The built-in containers typically '
jpayne@69 1906 'assume\n'
jpayne@69 1907 ' identical objects are equal to themselves. That lets them '
jpayne@69 1908 'bypass\n'
jpayne@69 1909 ' equality tests for identical objects to improve performance '
jpayne@69 1910 'and to\n'
jpayne@69 1911 ' maintain their internal invariants.\n'
jpayne@69 1912 '\n'
jpayne@69 1913 ' Lexicographical comparison between built-in collections '
jpayne@69 1914 'works as\n'
jpayne@69 1915 ' follows:\n'
jpayne@69 1916 '\n'
jpayne@69 1917 ' * For two collections to compare equal, they must be of the '
jpayne@69 1918 'same\n'
jpayne@69 1919 ' type, have the same length, and each pair of '
jpayne@69 1920 'corresponding\n'
jpayne@69 1921 ' elements must compare equal (for example, "[1,2] == '
jpayne@69 1922 '(1,2)" is\n'
jpayne@69 1923 ' false because the type is not the same).\n'
jpayne@69 1924 '\n'
jpayne@69 1925 ' * Collections that support order comparison are ordered the '
jpayne@69 1926 'same\n'
jpayne@69 1927 ' as their first unequal elements (for example, "[1,2,x] <= '
jpayne@69 1928 '[1,2,y]"\n'
jpayne@69 1929 ' has the same value as "x <= y"). If a corresponding '
jpayne@69 1930 'element does\n'
jpayne@69 1931 ' not exist, the shorter collection is ordered first (for '
jpayne@69 1932 'example,\n'
jpayne@69 1933 ' "[1,2] < [1,2,3]" is true).\n'
jpayne@69 1934 '\n'
jpayne@69 1935 '* Mappings (instances of "dict") compare equal if and only if '
jpayne@69 1936 'they\n'
jpayne@69 1937 ' have equal *(key, value)* pairs. Equality comparison of the '
jpayne@69 1938 'keys and\n'
jpayne@69 1939 ' values enforces reflexivity.\n'
jpayne@69 1940 '\n'
jpayne@69 1941 ' Order comparisons ("<", ">", "<=", and ">=") raise '
jpayne@69 1942 '"TypeError".\n'
jpayne@69 1943 '\n'
jpayne@69 1944 '* Sets (instances of "set" or "frozenset") can be compared '
jpayne@69 1945 'within\n'
jpayne@69 1946 ' and across their types.\n'
jpayne@69 1947 '\n'
jpayne@69 1948 ' They define order comparison operators to mean subset and '
jpayne@69 1949 'superset\n'
jpayne@69 1950 ' tests. Those relations do not define total orderings (for '
jpayne@69 1951 'example,\n'
jpayne@69 1952 ' the two sets "{1,2}" and "{2,3}" are not equal, nor subsets '
jpayne@69 1953 'of one\n'
jpayne@69 1954 ' another, nor supersets of one another). Accordingly, sets '
jpayne@69 1955 'are not\n'
jpayne@69 1956 ' appropriate arguments for functions which depend on total '
jpayne@69 1957 'ordering\n'
jpayne@69 1958 ' (for example, "min()", "max()", and "sorted()" produce '
jpayne@69 1959 'undefined\n'
jpayne@69 1960 ' results given a list of sets as inputs).\n'
jpayne@69 1961 '\n'
jpayne@69 1962 ' Comparison of sets enforces reflexivity of its elements.\n'
jpayne@69 1963 '\n'
jpayne@69 1964 '* Most other built-in types have no comparison methods '
jpayne@69 1965 'implemented,\n'
jpayne@69 1966 ' so they inherit the default comparison behavior.\n'
jpayne@69 1967 '\n'
jpayne@69 1968 'User-defined classes that customize their comparison behavior '
jpayne@69 1969 'should\n'
jpayne@69 1970 'follow some consistency rules, if possible:\n'
jpayne@69 1971 '\n'
jpayne@69 1972 '* Equality comparison should be reflexive. In other words, '
jpayne@69 1973 'identical\n'
jpayne@69 1974 ' objects should compare equal:\n'
jpayne@69 1975 '\n'
jpayne@69 1976 ' "x is y" implies "x == y"\n'
jpayne@69 1977 '\n'
jpayne@69 1978 '* Comparison should be symmetric. In other words, the '
jpayne@69 1979 'following\n'
jpayne@69 1980 ' expressions should have the same result:\n'
jpayne@69 1981 '\n'
jpayne@69 1982 ' "x == y" and "y == x"\n'
jpayne@69 1983 '\n'
jpayne@69 1984 ' "x != y" and "y != x"\n'
jpayne@69 1985 '\n'
jpayne@69 1986 ' "x < y" and "y > x"\n'
jpayne@69 1987 '\n'
jpayne@69 1988 ' "x <= y" and "y >= x"\n'
jpayne@69 1989 '\n'
jpayne@69 1990 '* Comparison should be transitive. The following '
jpayne@69 1991 '(non-exhaustive)\n'
jpayne@69 1992 ' examples illustrate that:\n'
jpayne@69 1993 '\n'
jpayne@69 1994 ' "x > y and y > z" implies "x > z"\n'
jpayne@69 1995 '\n'
jpayne@69 1996 ' "x < y and y <= z" implies "x < z"\n'
jpayne@69 1997 '\n'
jpayne@69 1998 '* Inverse comparison should result in the boolean negation. '
jpayne@69 1999 'In other\n'
jpayne@69 2000 ' words, the following expressions should have the same '
jpayne@69 2001 'result:\n'
jpayne@69 2002 '\n'
jpayne@69 2003 ' "x == y" and "not x != y"\n'
jpayne@69 2004 '\n'
jpayne@69 2005 ' "x < y" and "not x >= y" (for total ordering)\n'
jpayne@69 2006 '\n'
jpayne@69 2007 ' "x > y" and "not x <= y" (for total ordering)\n'
jpayne@69 2008 '\n'
jpayne@69 2009 ' The last two expressions apply to totally ordered '
jpayne@69 2010 'collections (e.g.\n'
jpayne@69 2011 ' to sequences, but not to sets or mappings). See also the\n'
jpayne@69 2012 ' "total_ordering()" decorator.\n'
jpayne@69 2013 '\n'
jpayne@69 2014 '* The "hash()" result should be consistent with equality. '
jpayne@69 2015 'Objects\n'
jpayne@69 2016 ' that are equal should either have the same hash value, or '
jpayne@69 2017 'be marked\n'
jpayne@69 2018 ' as unhashable.\n'
jpayne@69 2019 '\n'
jpayne@69 2020 'Python does not enforce these consistency rules. In fact, '
jpayne@69 2021 'the\n'
jpayne@69 2022 'not-a-number values are an example for not following these '
jpayne@69 2023 'rules.\n'
jpayne@69 2024 '\n'
jpayne@69 2025 '\n'
jpayne@69 2026 'Membership test operations\n'
jpayne@69 2027 '==========================\n'
jpayne@69 2028 '\n'
jpayne@69 2029 'The operators "in" and "not in" test for membership. "x in '
jpayne@69 2030 's"\n'
jpayne@69 2031 'evaluates to "True" if *x* is a member of *s*, and "False" '
jpayne@69 2032 'otherwise.\n'
jpayne@69 2033 '"x not in s" returns the negation of "x in s". All built-in '
jpayne@69 2034 'sequences\n'
jpayne@69 2035 'and set types support this as well as dictionary, for which '
jpayne@69 2036 '"in" tests\n'
jpayne@69 2037 'whether the dictionary has a given key. For container types '
jpayne@69 2038 'such as\n'
jpayne@69 2039 'list, tuple, set, frozenset, dict, or collections.deque, the\n'
jpayne@69 2040 'expression "x in y" is equivalent to "any(x is e or x == e '
jpayne@69 2041 'for e in\n'
jpayne@69 2042 'y)".\n'
jpayne@69 2043 '\n'
jpayne@69 2044 'For the string and bytes types, "x in y" is "True" if and '
jpayne@69 2045 'only if *x*\n'
jpayne@69 2046 'is a substring of *y*. An equivalent test is "y.find(x) != '
jpayne@69 2047 '-1".\n'
jpayne@69 2048 'Empty strings are always considered to be a substring of any '
jpayne@69 2049 'other\n'
jpayne@69 2050 'string, so """ in "abc"" will return "True".\n'
jpayne@69 2051 '\n'
jpayne@69 2052 'For user-defined classes which define the "__contains__()" '
jpayne@69 2053 'method, "x\n'
jpayne@69 2054 'in y" returns "True" if "y.__contains__(x)" returns a true '
jpayne@69 2055 'value, and\n'
jpayne@69 2056 '"False" otherwise.\n'
jpayne@69 2057 '\n'
jpayne@69 2058 'For user-defined classes which do not define "__contains__()" '
jpayne@69 2059 'but do\n'
jpayne@69 2060 'define "__iter__()", "x in y" is "True" if some value "z", '
jpayne@69 2061 'for which\n'
jpayne@69 2062 'the expression "x is z or x == z" is true, is produced while '
jpayne@69 2063 'iterating\n'
jpayne@69 2064 'over "y". If an exception is raised during the iteration, it '
jpayne@69 2065 'is as if\n'
jpayne@69 2066 '"in" raised that exception.\n'
jpayne@69 2067 '\n'
jpayne@69 2068 'Lastly, the old-style iteration protocol is tried: if a class '
jpayne@69 2069 'defines\n'
jpayne@69 2070 '"__getitem__()", "x in y" is "True" if and only if there is a '
jpayne@69 2071 'non-\n'
jpayne@69 2072 'negative integer index *i* such that "x is y[i] or x == '
jpayne@69 2073 'y[i]", and no\n'
jpayne@69 2074 'lower integer index raises the "IndexError" exception. (If '
jpayne@69 2075 'any other\n'
jpayne@69 2076 'exception is raised, it is as if "in" raised that '
jpayne@69 2077 'exception).\n'
jpayne@69 2078 '\n'
jpayne@69 2079 'The operator "not in" is defined to have the inverse truth '
jpayne@69 2080 'value of\n'
jpayne@69 2081 '"in".\n'
jpayne@69 2082 '\n'
jpayne@69 2083 '\n'
jpayne@69 2084 'Identity comparisons\n'
jpayne@69 2085 '====================\n'
jpayne@69 2086 '\n'
jpayne@69 2087 'The operators "is" and "is not" test for an object’s '
jpayne@69 2088 'identity: "x is\n'
jpayne@69 2089 'y" is true if and only if *x* and *y* are the same object. '
jpayne@69 2090 'An\n'
jpayne@69 2091 'Object’s identity is determined using the "id()" function. '
jpayne@69 2092 '"x is not\n'
jpayne@69 2093 'y" yields the inverse truth value. [4]\n',
jpayne@69 2094 'compound': 'Compound statements\n'
jpayne@69 2095 '*******************\n'
jpayne@69 2096 '\n'
jpayne@69 2097 'Compound statements contain (groups of) other statements; they '
jpayne@69 2098 'affect\n'
jpayne@69 2099 'or control the execution of those other statements in some way. '
jpayne@69 2100 'In\n'
jpayne@69 2101 'general, compound statements span multiple lines, although in '
jpayne@69 2102 'simple\n'
jpayne@69 2103 'incarnations a whole compound statement may be contained in one '
jpayne@69 2104 'line.\n'
jpayne@69 2105 '\n'
jpayne@69 2106 'The "if", "while" and "for" statements implement traditional '
jpayne@69 2107 'control\n'
jpayne@69 2108 'flow constructs. "try" specifies exception handlers and/or '
jpayne@69 2109 'cleanup\n'
jpayne@69 2110 'code for a group of statements, while the "with" statement '
jpayne@69 2111 'allows the\n'
jpayne@69 2112 'execution of initialization and finalization code around a block '
jpayne@69 2113 'of\n'
jpayne@69 2114 'code. Function and class definitions are also syntactically '
jpayne@69 2115 'compound\n'
jpayne@69 2116 'statements.\n'
jpayne@69 2117 '\n'
jpayne@69 2118 'A compound statement consists of one or more ‘clauses.’ A '
jpayne@69 2119 'clause\n'
jpayne@69 2120 'consists of a header and a ‘suite.’ The clause headers of a\n'
jpayne@69 2121 'particular compound statement are all at the same indentation '
jpayne@69 2122 'level.\n'
jpayne@69 2123 'Each clause header begins with a uniquely identifying keyword '
jpayne@69 2124 'and ends\n'
jpayne@69 2125 'with a colon. A suite is a group of statements controlled by a\n'
jpayne@69 2126 'clause. A suite can be one or more semicolon-separated simple\n'
jpayne@69 2127 'statements on the same line as the header, following the '
jpayne@69 2128 'header’s\n'
jpayne@69 2129 'colon, or it can be one or more indented statements on '
jpayne@69 2130 'subsequent\n'
jpayne@69 2131 'lines. Only the latter form of a suite can contain nested '
jpayne@69 2132 'compound\n'
jpayne@69 2133 'statements; the following is illegal, mostly because it wouldn’t '
jpayne@69 2134 'be\n'
jpayne@69 2135 'clear to which "if" clause a following "else" clause would '
jpayne@69 2136 'belong:\n'
jpayne@69 2137 '\n'
jpayne@69 2138 ' if test1: if test2: print(x)\n'
jpayne@69 2139 '\n'
jpayne@69 2140 'Also note that the semicolon binds tighter than the colon in '
jpayne@69 2141 'this\n'
jpayne@69 2142 'context, so that in the following example, either all or none of '
jpayne@69 2143 'the\n'
jpayne@69 2144 '"print()" calls are executed:\n'
jpayne@69 2145 '\n'
jpayne@69 2146 ' if x < y < z: print(x); print(y); print(z)\n'
jpayne@69 2147 '\n'
jpayne@69 2148 'Summarizing:\n'
jpayne@69 2149 '\n'
jpayne@69 2150 ' compound_stmt ::= if_stmt\n'
jpayne@69 2151 ' | while_stmt\n'
jpayne@69 2152 ' | for_stmt\n'
jpayne@69 2153 ' | try_stmt\n'
jpayne@69 2154 ' | with_stmt\n'
jpayne@69 2155 ' | funcdef\n'
jpayne@69 2156 ' | classdef\n'
jpayne@69 2157 ' | async_with_stmt\n'
jpayne@69 2158 ' | async_for_stmt\n'
jpayne@69 2159 ' | async_funcdef\n'
jpayne@69 2160 ' suite ::= stmt_list NEWLINE | NEWLINE INDENT '
jpayne@69 2161 'statement+ DEDENT\n'
jpayne@69 2162 ' statement ::= stmt_list NEWLINE | compound_stmt\n'
jpayne@69 2163 ' stmt_list ::= simple_stmt (";" simple_stmt)* [";"]\n'
jpayne@69 2164 '\n'
jpayne@69 2165 'Note that statements always end in a "NEWLINE" possibly followed '
jpayne@69 2166 'by a\n'
jpayne@69 2167 '"DEDENT". Also note that optional continuation clauses always '
jpayne@69 2168 'begin\n'
jpayne@69 2169 'with a keyword that cannot start a statement, thus there are no\n'
jpayne@69 2170 'ambiguities (the ‘dangling "else"’ problem is solved in Python '
jpayne@69 2171 'by\n'
jpayne@69 2172 'requiring nested "if" statements to be indented).\n'
jpayne@69 2173 '\n'
jpayne@69 2174 'The formatting of the grammar rules in the following sections '
jpayne@69 2175 'places\n'
jpayne@69 2176 'each clause on a separate line for clarity.\n'
jpayne@69 2177 '\n'
jpayne@69 2178 '\n'
jpayne@69 2179 'The "if" statement\n'
jpayne@69 2180 '==================\n'
jpayne@69 2181 '\n'
jpayne@69 2182 'The "if" statement is used for conditional execution:\n'
jpayne@69 2183 '\n'
jpayne@69 2184 ' if_stmt ::= "if" expression ":" suite\n'
jpayne@69 2185 ' ("elif" expression ":" suite)*\n'
jpayne@69 2186 ' ["else" ":" suite]\n'
jpayne@69 2187 '\n'
jpayne@69 2188 'It selects exactly one of the suites by evaluating the '
jpayne@69 2189 'expressions one\n'
jpayne@69 2190 'by one until one is found to be true (see section Boolean '
jpayne@69 2191 'operations\n'
jpayne@69 2192 'for the definition of true and false); then that suite is '
jpayne@69 2193 'executed\n'
jpayne@69 2194 '(and no other part of the "if" statement is executed or '
jpayne@69 2195 'evaluated).\n'
jpayne@69 2196 'If all expressions are false, the suite of the "else" clause, '
jpayne@69 2197 'if\n'
jpayne@69 2198 'present, is executed.\n'
jpayne@69 2199 '\n'
jpayne@69 2200 '\n'
jpayne@69 2201 'The "while" statement\n'
jpayne@69 2202 '=====================\n'
jpayne@69 2203 '\n'
jpayne@69 2204 'The "while" statement is used for repeated execution as long as '
jpayne@69 2205 'an\n'
jpayne@69 2206 'expression is true:\n'
jpayne@69 2207 '\n'
jpayne@69 2208 ' while_stmt ::= "while" expression ":" suite\n'
jpayne@69 2209 ' ["else" ":" suite]\n'
jpayne@69 2210 '\n'
jpayne@69 2211 'This repeatedly tests the expression and, if it is true, '
jpayne@69 2212 'executes the\n'
jpayne@69 2213 'first suite; if the expression is false (which may be the first '
jpayne@69 2214 'time\n'
jpayne@69 2215 'it is tested) the suite of the "else" clause, if present, is '
jpayne@69 2216 'executed\n'
jpayne@69 2217 'and the loop terminates.\n'
jpayne@69 2218 '\n'
jpayne@69 2219 'A "break" statement executed in the first suite terminates the '
jpayne@69 2220 'loop\n'
jpayne@69 2221 'without executing the "else" clause’s suite. A "continue" '
jpayne@69 2222 'statement\n'
jpayne@69 2223 'executed in the first suite skips the rest of the suite and goes '
jpayne@69 2224 'back\n'
jpayne@69 2225 'to testing the expression.\n'
jpayne@69 2226 '\n'
jpayne@69 2227 '\n'
jpayne@69 2228 'The "for" statement\n'
jpayne@69 2229 '===================\n'
jpayne@69 2230 '\n'
jpayne@69 2231 'The "for" statement is used to iterate over the elements of a '
jpayne@69 2232 'sequence\n'
jpayne@69 2233 '(such as a string, tuple or list) or other iterable object:\n'
jpayne@69 2234 '\n'
jpayne@69 2235 ' for_stmt ::= "for" target_list "in" expression_list ":" '
jpayne@69 2236 'suite\n'
jpayne@69 2237 ' ["else" ":" suite]\n'
jpayne@69 2238 '\n'
jpayne@69 2239 'The expression list is evaluated once; it should yield an '
jpayne@69 2240 'iterable\n'
jpayne@69 2241 'object. An iterator is created for the result of the\n'
jpayne@69 2242 '"expression_list". The suite is then executed once for each '
jpayne@69 2243 'item\n'
jpayne@69 2244 'provided by the iterator, in the order returned by the '
jpayne@69 2245 'iterator. Each\n'
jpayne@69 2246 'item in turn is assigned to the target list using the standard '
jpayne@69 2247 'rules\n'
jpayne@69 2248 'for assignments (see Assignment statements), and then the suite '
jpayne@69 2249 'is\n'
jpayne@69 2250 'executed. When the items are exhausted (which is immediately '
jpayne@69 2251 'when the\n'
jpayne@69 2252 'sequence is empty or an iterator raises a "StopIteration" '
jpayne@69 2253 'exception),\n'
jpayne@69 2254 'the suite in the "else" clause, if present, is executed, and the '
jpayne@69 2255 'loop\n'
jpayne@69 2256 'terminates.\n'
jpayne@69 2257 '\n'
jpayne@69 2258 'A "break" statement executed in the first suite terminates the '
jpayne@69 2259 'loop\n'
jpayne@69 2260 'without executing the "else" clause’s suite. A "continue" '
jpayne@69 2261 'statement\n'
jpayne@69 2262 'executed in the first suite skips the rest of the suite and '
jpayne@69 2263 'continues\n'
jpayne@69 2264 'with the next item, or with the "else" clause if there is no '
jpayne@69 2265 'next\n'
jpayne@69 2266 'item.\n'
jpayne@69 2267 '\n'
jpayne@69 2268 'The for-loop makes assignments to the variables in the target '
jpayne@69 2269 'list.\n'
jpayne@69 2270 'This overwrites all previous assignments to those variables '
jpayne@69 2271 'including\n'
jpayne@69 2272 'those made in the suite of the for-loop:\n'
jpayne@69 2273 '\n'
jpayne@69 2274 ' for i in range(10):\n'
jpayne@69 2275 ' print(i)\n'
jpayne@69 2276 ' i = 5 # this will not affect the for-loop\n'
jpayne@69 2277 ' # because i will be overwritten with '
jpayne@69 2278 'the next\n'
jpayne@69 2279 ' # index in the range\n'
jpayne@69 2280 '\n'
jpayne@69 2281 'Names in the target list are not deleted when the loop is '
jpayne@69 2282 'finished,\n'
jpayne@69 2283 'but if the sequence is empty, they will not have been assigned '
jpayne@69 2284 'to at\n'
jpayne@69 2285 'all by the loop. Hint: the built-in function "range()" returns '
jpayne@69 2286 'an\n'
jpayne@69 2287 'iterator of integers suitable to emulate the effect of Pascal’s '
jpayne@69 2288 '"for i\n'
jpayne@69 2289 ':= a to b do"; e.g., "list(range(3))" returns the list "[0, 1, '
jpayne@69 2290 '2]".\n'
jpayne@69 2291 '\n'
jpayne@69 2292 'Note: There is a subtlety when the sequence is being modified by '
jpayne@69 2293 'the\n'
jpayne@69 2294 ' loop (this can only occur for mutable sequences, e.g. lists). '
jpayne@69 2295 'An\n'
jpayne@69 2296 ' internal counter is used to keep track of which item is used '
jpayne@69 2297 'next,\n'
jpayne@69 2298 ' and this is incremented on each iteration. When this counter '
jpayne@69 2299 'has\n'
jpayne@69 2300 ' reached the length of the sequence the loop terminates. This '
jpayne@69 2301 'means\n'
jpayne@69 2302 ' that if the suite deletes the current (or a previous) item '
jpayne@69 2303 'from the\n'
jpayne@69 2304 ' sequence, the next item will be skipped (since it gets the '
jpayne@69 2305 'index of\n'
jpayne@69 2306 ' the current item which has already been treated). Likewise, '
jpayne@69 2307 'if the\n'
jpayne@69 2308 ' suite inserts an item in the sequence before the current item, '
jpayne@69 2309 'the\n'
jpayne@69 2310 ' current item will be treated again the next time through the '
jpayne@69 2311 'loop.\n'
jpayne@69 2312 ' This can lead to nasty bugs that can be avoided by making a\n'
jpayne@69 2313 ' temporary copy using a slice of the whole sequence, e.g.,\n'
jpayne@69 2314 '\n'
jpayne@69 2315 ' for x in a[:]:\n'
jpayne@69 2316 ' if x < 0: a.remove(x)\n'
jpayne@69 2317 '\n'
jpayne@69 2318 '\n'
jpayne@69 2319 'The "try" statement\n'
jpayne@69 2320 '===================\n'
jpayne@69 2321 '\n'
jpayne@69 2322 'The "try" statement specifies exception handlers and/or cleanup '
jpayne@69 2323 'code\n'
jpayne@69 2324 'for a group of statements:\n'
jpayne@69 2325 '\n'
jpayne@69 2326 ' try_stmt ::= try1_stmt | try2_stmt\n'
jpayne@69 2327 ' try1_stmt ::= "try" ":" suite\n'
jpayne@69 2328 ' ("except" [expression ["as" identifier]] ":" '
jpayne@69 2329 'suite)+\n'
jpayne@69 2330 ' ["else" ":" suite]\n'
jpayne@69 2331 ' ["finally" ":" suite]\n'
jpayne@69 2332 ' try2_stmt ::= "try" ":" suite\n'
jpayne@69 2333 ' "finally" ":" suite\n'
jpayne@69 2334 '\n'
jpayne@69 2335 'The "except" clause(s) specify one or more exception handlers. '
jpayne@69 2336 'When no\n'
jpayne@69 2337 'exception occurs in the "try" clause, no exception handler is\n'
jpayne@69 2338 'executed. When an exception occurs in the "try" suite, a search '
jpayne@69 2339 'for an\n'
jpayne@69 2340 'exception handler is started. This search inspects the except '
jpayne@69 2341 'clauses\n'
jpayne@69 2342 'in turn until one is found that matches the exception. An '
jpayne@69 2343 'expression-\n'
jpayne@69 2344 'less except clause, if present, must be last; it matches any\n'
jpayne@69 2345 'exception. For an except clause with an expression, that '
jpayne@69 2346 'expression\n'
jpayne@69 2347 'is evaluated, and the clause matches the exception if the '
jpayne@69 2348 'resulting\n'
jpayne@69 2349 'object is “compatible” with the exception. An object is '
jpayne@69 2350 'compatible\n'
jpayne@69 2351 'with an exception if it is the class or a base class of the '
jpayne@69 2352 'exception\n'
jpayne@69 2353 'object or a tuple containing an item compatible with the '
jpayne@69 2354 'exception.\n'
jpayne@69 2355 '\n'
jpayne@69 2356 'If no except clause matches the exception, the search for an '
jpayne@69 2357 'exception\n'
jpayne@69 2358 'handler continues in the surrounding code and on the invocation '
jpayne@69 2359 'stack.\n'
jpayne@69 2360 '[1]\n'
jpayne@69 2361 '\n'
jpayne@69 2362 'If the evaluation of an expression in the header of an except '
jpayne@69 2363 'clause\n'
jpayne@69 2364 'raises an exception, the original search for a handler is '
jpayne@69 2365 'canceled and\n'
jpayne@69 2366 'a search starts for the new exception in the surrounding code '
jpayne@69 2367 'and on\n'
jpayne@69 2368 'the call stack (it is treated as if the entire "try" statement '
jpayne@69 2369 'raised\n'
jpayne@69 2370 'the exception).\n'
jpayne@69 2371 '\n'
jpayne@69 2372 'When a matching except clause is found, the exception is '
jpayne@69 2373 'assigned to\n'
jpayne@69 2374 'the target specified after the "as" keyword in that except '
jpayne@69 2375 'clause, if\n'
jpayne@69 2376 'present, and the except clause’s suite is executed. All except\n'
jpayne@69 2377 'clauses must have an executable block. When the end of this '
jpayne@69 2378 'block is\n'
jpayne@69 2379 'reached, execution continues normally after the entire try '
jpayne@69 2380 'statement.\n'
jpayne@69 2381 '(This means that if two nested handlers exist for the same '
jpayne@69 2382 'exception,\n'
jpayne@69 2383 'and the exception occurs in the try clause of the inner handler, '
jpayne@69 2384 'the\n'
jpayne@69 2385 'outer handler will not handle the exception.)\n'
jpayne@69 2386 '\n'
jpayne@69 2387 'When an exception has been assigned using "as target", it is '
jpayne@69 2388 'cleared\n'
jpayne@69 2389 'at the end of the except clause. This is as if\n'
jpayne@69 2390 '\n'
jpayne@69 2391 ' except E as N:\n'
jpayne@69 2392 ' foo\n'
jpayne@69 2393 '\n'
jpayne@69 2394 'was translated to\n'
jpayne@69 2395 '\n'
jpayne@69 2396 ' except E as N:\n'
jpayne@69 2397 ' try:\n'
jpayne@69 2398 ' foo\n'
jpayne@69 2399 ' finally:\n'
jpayne@69 2400 ' del N\n'
jpayne@69 2401 '\n'
jpayne@69 2402 'This means the exception must be assigned to a different name to '
jpayne@69 2403 'be\n'
jpayne@69 2404 'able to refer to it after the except clause. Exceptions are '
jpayne@69 2405 'cleared\n'
jpayne@69 2406 'because with the traceback attached to them, they form a '
jpayne@69 2407 'reference\n'
jpayne@69 2408 'cycle with the stack frame, keeping all locals in that frame '
jpayne@69 2409 'alive\n'
jpayne@69 2410 'until the next garbage collection occurs.\n'
jpayne@69 2411 '\n'
jpayne@69 2412 'Before an except clause’s suite is executed, details about the\n'
jpayne@69 2413 'exception are stored in the "sys" module and can be accessed '
jpayne@69 2414 'via\n'
jpayne@69 2415 '"sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting '
jpayne@69 2416 'of the\n'
jpayne@69 2417 'exception class, the exception instance and a traceback object '
jpayne@69 2418 '(see\n'
jpayne@69 2419 'section The standard type hierarchy) identifying the point in '
jpayne@69 2420 'the\n'
jpayne@69 2421 'program where the exception occurred. "sys.exc_info()" values '
jpayne@69 2422 'are\n'
jpayne@69 2423 'restored to their previous values (before the call) when '
jpayne@69 2424 'returning\n'
jpayne@69 2425 'from a function that handled an exception.\n'
jpayne@69 2426 '\n'
jpayne@69 2427 'The optional "else" clause is executed if the control flow '
jpayne@69 2428 'leaves the\n'
jpayne@69 2429 '"try" suite, no exception was raised, and no "return", '
jpayne@69 2430 '"continue", or\n'
jpayne@69 2431 '"break" statement was executed. Exceptions in the "else" clause '
jpayne@69 2432 'are\n'
jpayne@69 2433 'not handled by the preceding "except" clauses.\n'
jpayne@69 2434 '\n'
jpayne@69 2435 'If "finally" is present, it specifies a ‘cleanup’ handler. The '
jpayne@69 2436 '"try"\n'
jpayne@69 2437 'clause is executed, including any "except" and "else" clauses. '
jpayne@69 2438 'If an\n'
jpayne@69 2439 'exception occurs in any of the clauses and is not handled, the\n'
jpayne@69 2440 'exception is temporarily saved. The "finally" clause is '
jpayne@69 2441 'executed. If\n'
jpayne@69 2442 'there is a saved exception it is re-raised at the end of the '
jpayne@69 2443 '"finally"\n'
jpayne@69 2444 'clause. If the "finally" clause raises another exception, the '
jpayne@69 2445 'saved\n'
jpayne@69 2446 'exception is set as the context of the new exception. If the '
jpayne@69 2447 '"finally"\n'
jpayne@69 2448 'clause executes a "return", "break" or "continue" statement, the '
jpayne@69 2449 'saved\n'
jpayne@69 2450 'exception is discarded:\n'
jpayne@69 2451 '\n'
jpayne@69 2452 ' >>> def f():\n'
jpayne@69 2453 ' ... try:\n'
jpayne@69 2454 ' ... 1/0\n'
jpayne@69 2455 ' ... finally:\n'
jpayne@69 2456 ' ... return 42\n'
jpayne@69 2457 ' ...\n'
jpayne@69 2458 ' >>> f()\n'
jpayne@69 2459 ' 42\n'
jpayne@69 2460 '\n'
jpayne@69 2461 'The exception information is not available to the program '
jpayne@69 2462 'during\n'
jpayne@69 2463 'execution of the "finally" clause.\n'
jpayne@69 2464 '\n'
jpayne@69 2465 'When a "return", "break" or "continue" statement is executed in '
jpayne@69 2466 'the\n'
jpayne@69 2467 '"try" suite of a "try"…"finally" statement, the "finally" clause '
jpayne@69 2468 'is\n'
jpayne@69 2469 'also executed ‘on the way out.’\n'
jpayne@69 2470 '\n'
jpayne@69 2471 'The return value of a function is determined by the last '
jpayne@69 2472 '"return"\n'
jpayne@69 2473 'statement executed. Since the "finally" clause always executes, '
jpayne@69 2474 'a\n'
jpayne@69 2475 '"return" statement executed in the "finally" clause will always '
jpayne@69 2476 'be the\n'
jpayne@69 2477 'last one executed:\n'
jpayne@69 2478 '\n'
jpayne@69 2479 ' >>> def foo():\n'
jpayne@69 2480 ' ... try:\n'
jpayne@69 2481 " ... return 'try'\n"
jpayne@69 2482 ' ... finally:\n'
jpayne@69 2483 " ... return 'finally'\n"
jpayne@69 2484 ' ...\n'
jpayne@69 2485 ' >>> foo()\n'
jpayne@69 2486 " 'finally'\n"
jpayne@69 2487 '\n'
jpayne@69 2488 'Additional information on exceptions can be found in section\n'
jpayne@69 2489 'Exceptions, and information on using the "raise" statement to '
jpayne@69 2490 'generate\n'
jpayne@69 2491 'exceptions may be found in section The raise statement.\n'
jpayne@69 2492 '\n'
jpayne@69 2493 'Changed in version 3.8: Prior to Python 3.8, a "continue" '
jpayne@69 2494 'statement\n'
jpayne@69 2495 'was illegal in the "finally" clause due to a problem with the\n'
jpayne@69 2496 'implementation.\n'
jpayne@69 2497 '\n'
jpayne@69 2498 '\n'
jpayne@69 2499 'The "with" statement\n'
jpayne@69 2500 '====================\n'
jpayne@69 2501 '\n'
jpayne@69 2502 'The "with" statement is used to wrap the execution of a block '
jpayne@69 2503 'with\n'
jpayne@69 2504 'methods defined by a context manager (see section With '
jpayne@69 2505 'Statement\n'
jpayne@69 2506 'Context Managers). This allows common "try"…"except"…"finally" '
jpayne@69 2507 'usage\n'
jpayne@69 2508 'patterns to be encapsulated for convenient reuse.\n'
jpayne@69 2509 '\n'
jpayne@69 2510 ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n'
jpayne@69 2511 ' with_item ::= expression ["as" target]\n'
jpayne@69 2512 '\n'
jpayne@69 2513 'The execution of the "with" statement with one “item” proceeds '
jpayne@69 2514 'as\n'
jpayne@69 2515 'follows:\n'
jpayne@69 2516 '\n'
jpayne@69 2517 '1. The context expression (the expression given in the '
jpayne@69 2518 '"with_item")\n'
jpayne@69 2519 ' is evaluated to obtain a context manager.\n'
jpayne@69 2520 '\n'
jpayne@69 2521 '2. The context manager’s "__exit__()" is loaded for later use.\n'
jpayne@69 2522 '\n'
jpayne@69 2523 '3. The context manager’s "__enter__()" method is invoked.\n'
jpayne@69 2524 '\n'
jpayne@69 2525 '4. If a target was included in the "with" statement, the return\n'
jpayne@69 2526 ' value from "__enter__()" is assigned to it.\n'
jpayne@69 2527 '\n'
jpayne@69 2528 ' Note: The "with" statement guarantees that if the '
jpayne@69 2529 '"__enter__()"\n'
jpayne@69 2530 ' method returns without an error, then "__exit__()" will '
jpayne@69 2531 'always be\n'
jpayne@69 2532 ' called. Thus, if an error occurs during the assignment to '
jpayne@69 2533 'the\n'
jpayne@69 2534 ' target list, it will be treated the same as an error '
jpayne@69 2535 'occurring\n'
jpayne@69 2536 ' within the suite would be. See step 6 below.\n'
jpayne@69 2537 '\n'
jpayne@69 2538 '5. The suite is executed.\n'
jpayne@69 2539 '\n'
jpayne@69 2540 '6. The context manager’s "__exit__()" method is invoked. If an\n'
jpayne@69 2541 ' exception caused the suite to be exited, its type, value, '
jpayne@69 2542 'and\n'
jpayne@69 2543 ' traceback are passed as arguments to "__exit__()". Otherwise, '
jpayne@69 2544 'three\n'
jpayne@69 2545 ' "None" arguments are supplied.\n'
jpayne@69 2546 '\n'
jpayne@69 2547 ' If the suite was exited due to an exception, and the return '
jpayne@69 2548 'value\n'
jpayne@69 2549 ' from the "__exit__()" method was false, the exception is '
jpayne@69 2550 'reraised.\n'
jpayne@69 2551 ' If the return value was true, the exception is suppressed, '
jpayne@69 2552 'and\n'
jpayne@69 2553 ' execution continues with the statement following the "with"\n'
jpayne@69 2554 ' statement.\n'
jpayne@69 2555 '\n'
jpayne@69 2556 ' If the suite was exited for any reason other than an '
jpayne@69 2557 'exception, the\n'
jpayne@69 2558 ' return value from "__exit__()" is ignored, and execution '
jpayne@69 2559 'proceeds\n'
jpayne@69 2560 ' at the normal location for the kind of exit that was taken.\n'
jpayne@69 2561 '\n'
jpayne@69 2562 'With more than one item, the context managers are processed as '
jpayne@69 2563 'if\n'
jpayne@69 2564 'multiple "with" statements were nested:\n'
jpayne@69 2565 '\n'
jpayne@69 2566 ' with A() as a, B() as b:\n'
jpayne@69 2567 ' suite\n'
jpayne@69 2568 '\n'
jpayne@69 2569 'is equivalent to\n'
jpayne@69 2570 '\n'
jpayne@69 2571 ' with A() as a:\n'
jpayne@69 2572 ' with B() as b:\n'
jpayne@69 2573 ' suite\n'
jpayne@69 2574 '\n'
jpayne@69 2575 'Changed in version 3.1: Support for multiple context '
jpayne@69 2576 'expressions.\n'
jpayne@69 2577 '\n'
jpayne@69 2578 'See also:\n'
jpayne@69 2579 '\n'
jpayne@69 2580 ' **PEP 343** - The “with” statement\n'
jpayne@69 2581 ' The specification, background, and examples for the Python '
jpayne@69 2582 '"with"\n'
jpayne@69 2583 ' statement.\n'
jpayne@69 2584 '\n'
jpayne@69 2585 '\n'
jpayne@69 2586 'Function definitions\n'
jpayne@69 2587 '====================\n'
jpayne@69 2588 '\n'
jpayne@69 2589 'A function definition defines a user-defined function object '
jpayne@69 2590 '(see\n'
jpayne@69 2591 'section The standard type hierarchy):\n'
jpayne@69 2592 '\n'
jpayne@69 2593 ' funcdef ::= [decorators] "def" funcname "(" '
jpayne@69 2594 '[parameter_list] ")"\n'
jpayne@69 2595 ' ["->" expression] ":" suite\n'
jpayne@69 2596 ' decorators ::= decorator+\n'
jpayne@69 2597 ' decorator ::= "@" dotted_name ["(" '
jpayne@69 2598 '[argument_list [","]] ")"] NEWLINE\n'
jpayne@69 2599 ' dotted_name ::= identifier ("." identifier)*\n'
jpayne@69 2600 ' parameter_list ::= defparameter ("," '
jpayne@69 2601 'defparameter)* "," "/" ["," [parameter_list_no_posonly]]\n'
jpayne@69 2602 ' | parameter_list_no_posonly\n'
jpayne@69 2603 ' parameter_list_no_posonly ::= defparameter ("," '
jpayne@69 2604 'defparameter)* ["," [parameter_list_starargs]]\n'
jpayne@69 2605 ' | parameter_list_starargs\n'
jpayne@69 2606 ' parameter_list_starargs ::= "*" [parameter] ("," '
jpayne@69 2607 'defparameter)* ["," ["**" parameter [","]]]\n'
jpayne@69 2608 ' | "**" parameter [","]\n'
jpayne@69 2609 ' parameter ::= identifier [":" expression]\n'
jpayne@69 2610 ' defparameter ::= parameter ["=" expression]\n'
jpayne@69 2611 ' funcname ::= identifier\n'
jpayne@69 2612 '\n'
jpayne@69 2613 'A function definition is an executable statement. Its execution '
jpayne@69 2614 'binds\n'
jpayne@69 2615 'the function name in the current local namespace to a function '
jpayne@69 2616 'object\n'
jpayne@69 2617 '(a wrapper around the executable code for the function). This\n'
jpayne@69 2618 'function object contains a reference to the current global '
jpayne@69 2619 'namespace\n'
jpayne@69 2620 'as the global namespace to be used when the function is called.\n'
jpayne@69 2621 '\n'
jpayne@69 2622 'The function definition does not execute the function body; this '
jpayne@69 2623 'gets\n'
jpayne@69 2624 'executed only when the function is called. [2]\n'
jpayne@69 2625 '\n'
jpayne@69 2626 'A function definition may be wrapped by one or more *decorator*\n'
jpayne@69 2627 'expressions. Decorator expressions are evaluated when the '
jpayne@69 2628 'function is\n'
jpayne@69 2629 'defined, in the scope that contains the function definition. '
jpayne@69 2630 'The\n'
jpayne@69 2631 'result must be a callable, which is invoked with the function '
jpayne@69 2632 'object\n'
jpayne@69 2633 'as the only argument. The returned value is bound to the '
jpayne@69 2634 'function name\n'
jpayne@69 2635 'instead of the function object. Multiple decorators are applied '
jpayne@69 2636 'in\n'
jpayne@69 2637 'nested fashion. For example, the following code\n'
jpayne@69 2638 '\n'
jpayne@69 2639 ' @f1(arg)\n'
jpayne@69 2640 ' @f2\n'
jpayne@69 2641 ' def func(): pass\n'
jpayne@69 2642 '\n'
jpayne@69 2643 'is roughly equivalent to\n'
jpayne@69 2644 '\n'
jpayne@69 2645 ' def func(): pass\n'
jpayne@69 2646 ' func = f1(arg)(f2(func))\n'
jpayne@69 2647 '\n'
jpayne@69 2648 'except that the original function is not temporarily bound to '
jpayne@69 2649 'the name\n'
jpayne@69 2650 '"func".\n'
jpayne@69 2651 '\n'
jpayne@69 2652 'When one or more *parameters* have the form *parameter* "="\n'
jpayne@69 2653 '*expression*, the function is said to have “default parameter '
jpayne@69 2654 'values.”\n'
jpayne@69 2655 'For a parameter with a default value, the corresponding '
jpayne@69 2656 '*argument* may\n'
jpayne@69 2657 'be omitted from a call, in which case the parameter’s default '
jpayne@69 2658 'value is\n'
jpayne@69 2659 'substituted. If a parameter has a default value, all following\n'
jpayne@69 2660 'parameters up until the “"*"” must also have a default value — '
jpayne@69 2661 'this is\n'
jpayne@69 2662 'a syntactic restriction that is not expressed by the grammar.\n'
jpayne@69 2663 '\n'
jpayne@69 2664 '**Default parameter values are evaluated from left to right when '
jpayne@69 2665 'the\n'
jpayne@69 2666 'function definition is executed.** This means that the '
jpayne@69 2667 'expression is\n'
jpayne@69 2668 'evaluated once, when the function is defined, and that the same '
jpayne@69 2669 '“pre-\n'
jpayne@69 2670 'computed” value is used for each call. This is especially '
jpayne@69 2671 'important\n'
jpayne@69 2672 'to understand when a default parameter is a mutable object, such '
jpayne@69 2673 'as a\n'
jpayne@69 2674 'list or a dictionary: if the function modifies the object (e.g. '
jpayne@69 2675 'by\n'
jpayne@69 2676 'appending an item to a list), the default value is in effect '
jpayne@69 2677 'modified.\n'
jpayne@69 2678 'This is generally not what was intended. A way around this is '
jpayne@69 2679 'to use\n'
jpayne@69 2680 '"None" as the default, and explicitly test for it in the body of '
jpayne@69 2681 'the\n'
jpayne@69 2682 'function, e.g.:\n'
jpayne@69 2683 '\n'
jpayne@69 2684 ' def whats_on_the_telly(penguin=None):\n'
jpayne@69 2685 ' if penguin is None:\n'
jpayne@69 2686 ' penguin = []\n'
jpayne@69 2687 ' penguin.append("property of the zoo")\n'
jpayne@69 2688 ' return penguin\n'
jpayne@69 2689 '\n'
jpayne@69 2690 'Function call semantics are described in more detail in section '
jpayne@69 2691 'Calls.\n'
jpayne@69 2692 'A function call always assigns values to all parameters '
jpayne@69 2693 'mentioned in\n'
jpayne@69 2694 'the parameter list, either from position arguments, from '
jpayne@69 2695 'keyword\n'
jpayne@69 2696 'arguments, or from default values. If the form “"*identifier"” '
jpayne@69 2697 'is\n'
jpayne@69 2698 'present, it is initialized to a tuple receiving any excess '
jpayne@69 2699 'positional\n'
jpayne@69 2700 'parameters, defaulting to the empty tuple. If the form\n'
jpayne@69 2701 '“"**identifier"” is present, it is initialized to a new ordered\n'
jpayne@69 2702 'mapping receiving any excess keyword arguments, defaulting to a '
jpayne@69 2703 'new\n'
jpayne@69 2704 'empty mapping of the same type. Parameters after “"*"” or\n'
jpayne@69 2705 '“"*identifier"” are keyword-only parameters and may only be '
jpayne@69 2706 'passed\n'
jpayne@69 2707 'used keyword arguments.\n'
jpayne@69 2708 '\n'
jpayne@69 2709 'Parameters may have an *annotation* of the form “": '
jpayne@69 2710 'expression"”\n'
jpayne@69 2711 'following the parameter name. Any parameter may have an '
jpayne@69 2712 'annotation,\n'
jpayne@69 2713 'even those of the form "*identifier" or "**identifier". '
jpayne@69 2714 'Functions may\n'
jpayne@69 2715 'have “return” annotation of the form “"-> expression"” after '
jpayne@69 2716 'the\n'
jpayne@69 2717 'parameter list. These annotations can be any valid Python '
jpayne@69 2718 'expression.\n'
jpayne@69 2719 'The presence of annotations does not change the semantics of a\n'
jpayne@69 2720 'function. The annotation values are available as values of a\n'
jpayne@69 2721 'dictionary keyed by the parameters’ names in the '
jpayne@69 2722 '"__annotations__"\n'
jpayne@69 2723 'attribute of the function object. If the "annotations" import '
jpayne@69 2724 'from\n'
jpayne@69 2725 '"__future__" is used, annotations are preserved as strings at '
jpayne@69 2726 'runtime\n'
jpayne@69 2727 'which enables postponed evaluation. Otherwise, they are '
jpayne@69 2728 'evaluated\n'
jpayne@69 2729 'when the function definition is executed. In this case '
jpayne@69 2730 'annotations\n'
jpayne@69 2731 'may be evaluated in a different order than they appear in the '
jpayne@69 2732 'source\n'
jpayne@69 2733 'code.\n'
jpayne@69 2734 '\n'
jpayne@69 2735 'It is also possible to create anonymous functions (functions not '
jpayne@69 2736 'bound\n'
jpayne@69 2737 'to a name), for immediate use in expressions. This uses lambda\n'
jpayne@69 2738 'expressions, described in section Lambdas. Note that the '
jpayne@69 2739 'lambda\n'
jpayne@69 2740 'expression is merely a shorthand for a simplified function '
jpayne@69 2741 'definition;\n'
jpayne@69 2742 'a function defined in a “"def"” statement can be passed around '
jpayne@69 2743 'or\n'
jpayne@69 2744 'assigned to another name just like a function defined by a '
jpayne@69 2745 'lambda\n'
jpayne@69 2746 'expression. The “"def"” form is actually more powerful since '
jpayne@69 2747 'it\n'
jpayne@69 2748 'allows the execution of multiple statements and annotations.\n'
jpayne@69 2749 '\n'
jpayne@69 2750 '**Programmer’s note:** Functions are first-class objects. A '
jpayne@69 2751 '“"def"”\n'
jpayne@69 2752 'statement executed inside a function definition defines a local\n'
jpayne@69 2753 'function that can be returned or passed around. Free variables '
jpayne@69 2754 'used\n'
jpayne@69 2755 'in the nested function can access the local variables of the '
jpayne@69 2756 'function\n'
jpayne@69 2757 'containing the def. See section Naming and binding for '
jpayne@69 2758 'details.\n'
jpayne@69 2759 '\n'
jpayne@69 2760 'See also:\n'
jpayne@69 2761 '\n'
jpayne@69 2762 ' **PEP 3107** - Function Annotations\n'
jpayne@69 2763 ' The original specification for function annotations.\n'
jpayne@69 2764 '\n'
jpayne@69 2765 ' **PEP 484** - Type Hints\n'
jpayne@69 2766 ' Definition of a standard meaning for annotations: type '
jpayne@69 2767 'hints.\n'
jpayne@69 2768 '\n'
jpayne@69 2769 ' **PEP 526** - Syntax for Variable Annotations\n'
jpayne@69 2770 ' Ability to type hint variable declarations, including '
jpayne@69 2771 'class\n'
jpayne@69 2772 ' variables and instance variables\n'
jpayne@69 2773 '\n'
jpayne@69 2774 ' **PEP 563** - Postponed Evaluation of Annotations\n'
jpayne@69 2775 ' Support for forward references within annotations by '
jpayne@69 2776 'preserving\n'
jpayne@69 2777 ' annotations in a string form at runtime instead of eager\n'
jpayne@69 2778 ' evaluation.\n'
jpayne@69 2779 '\n'
jpayne@69 2780 '\n'
jpayne@69 2781 'Class definitions\n'
jpayne@69 2782 '=================\n'
jpayne@69 2783 '\n'
jpayne@69 2784 'A class definition defines a class object (see section The '
jpayne@69 2785 'standard\n'
jpayne@69 2786 'type hierarchy):\n'
jpayne@69 2787 '\n'
jpayne@69 2788 ' classdef ::= [decorators] "class" classname [inheritance] '
jpayne@69 2789 '":" suite\n'
jpayne@69 2790 ' inheritance ::= "(" [argument_list] ")"\n'
jpayne@69 2791 ' classname ::= identifier\n'
jpayne@69 2792 '\n'
jpayne@69 2793 'A class definition is an executable statement. The inheritance '
jpayne@69 2794 'list\n'
jpayne@69 2795 'usually gives a list of base classes (see Metaclasses for more\n'
jpayne@69 2796 'advanced uses), so each item in the list should evaluate to a '
jpayne@69 2797 'class\n'
jpayne@69 2798 'object which allows subclassing. Classes without an inheritance '
jpayne@69 2799 'list\n'
jpayne@69 2800 'inherit, by default, from the base class "object"; hence,\n'
jpayne@69 2801 '\n'
jpayne@69 2802 ' class Foo:\n'
jpayne@69 2803 ' pass\n'
jpayne@69 2804 '\n'
jpayne@69 2805 'is equivalent to\n'
jpayne@69 2806 '\n'
jpayne@69 2807 ' class Foo(object):\n'
jpayne@69 2808 ' pass\n'
jpayne@69 2809 '\n'
jpayne@69 2810 'The class’s suite is then executed in a new execution frame '
jpayne@69 2811 '(see\n'
jpayne@69 2812 'Naming and binding), using a newly created local namespace and '
jpayne@69 2813 'the\n'
jpayne@69 2814 'original global namespace. (Usually, the suite contains mostly\n'
jpayne@69 2815 'function definitions.) When the class’s suite finishes '
jpayne@69 2816 'execution, its\n'
jpayne@69 2817 'execution frame is discarded but its local namespace is saved. '
jpayne@69 2818 '[3] A\n'
jpayne@69 2819 'class object is then created using the inheritance list for the '
jpayne@69 2820 'base\n'
jpayne@69 2821 'classes and the saved local namespace for the attribute '
jpayne@69 2822 'dictionary.\n'
jpayne@69 2823 'The class name is bound to this class object in the original '
jpayne@69 2824 'local\n'
jpayne@69 2825 'namespace.\n'
jpayne@69 2826 '\n'
jpayne@69 2827 'The order in which attributes are defined in the class body is\n'
jpayne@69 2828 'preserved in the new class’s "__dict__". Note that this is '
jpayne@69 2829 'reliable\n'
jpayne@69 2830 'only right after the class is created and only for classes that '
jpayne@69 2831 'were\n'
jpayne@69 2832 'defined using the definition syntax.\n'
jpayne@69 2833 '\n'
jpayne@69 2834 'Class creation can be customized heavily using metaclasses.\n'
jpayne@69 2835 '\n'
jpayne@69 2836 'Classes can also be decorated: just like when decorating '
jpayne@69 2837 'functions,\n'
jpayne@69 2838 '\n'
jpayne@69 2839 ' @f1(arg)\n'
jpayne@69 2840 ' @f2\n'
jpayne@69 2841 ' class Foo: pass\n'
jpayne@69 2842 '\n'
jpayne@69 2843 'is roughly equivalent to\n'
jpayne@69 2844 '\n'
jpayne@69 2845 ' class Foo: pass\n'
jpayne@69 2846 ' Foo = f1(arg)(f2(Foo))\n'
jpayne@69 2847 '\n'
jpayne@69 2848 'The evaluation rules for the decorator expressions are the same '
jpayne@69 2849 'as for\n'
jpayne@69 2850 'function decorators. The result is then bound to the class '
jpayne@69 2851 'name.\n'
jpayne@69 2852 '\n'
jpayne@69 2853 '**Programmer’s note:** Variables defined in the class definition '
jpayne@69 2854 'are\n'
jpayne@69 2855 'class attributes; they are shared by instances. Instance '
jpayne@69 2856 'attributes\n'
jpayne@69 2857 'can be set in a method with "self.name = value". Both class '
jpayne@69 2858 'and\n'
jpayne@69 2859 'instance attributes are accessible through the notation '
jpayne@69 2860 '“"self.name"”,\n'
jpayne@69 2861 'and an instance attribute hides a class attribute with the same '
jpayne@69 2862 'name\n'
jpayne@69 2863 'when accessed in this way. Class attributes can be used as '
jpayne@69 2864 'defaults\n'
jpayne@69 2865 'for instance attributes, but using mutable values there can lead '
jpayne@69 2866 'to\n'
jpayne@69 2867 'unexpected results. Descriptors can be used to create instance\n'
jpayne@69 2868 'variables with different implementation details.\n'
jpayne@69 2869 '\n'
jpayne@69 2870 'See also:\n'
jpayne@69 2871 '\n'
jpayne@69 2872 ' **PEP 3115** - Metaclasses in Python 3000\n'
jpayne@69 2873 ' The proposal that changed the declaration of metaclasses to '
jpayne@69 2874 'the\n'
jpayne@69 2875 ' current syntax, and the semantics for how classes with\n'
jpayne@69 2876 ' metaclasses are constructed.\n'
jpayne@69 2877 '\n'
jpayne@69 2878 ' **PEP 3129** - Class Decorators\n'
jpayne@69 2879 ' The proposal that added class decorators. Function and '
jpayne@69 2880 'method\n'
jpayne@69 2881 ' decorators were introduced in **PEP 318**.\n'
jpayne@69 2882 '\n'
jpayne@69 2883 '\n'
jpayne@69 2884 'Coroutines\n'
jpayne@69 2885 '==========\n'
jpayne@69 2886 '\n'
jpayne@69 2887 'New in version 3.5.\n'
jpayne@69 2888 '\n'
jpayne@69 2889 '\n'
jpayne@69 2890 'Coroutine function definition\n'
jpayne@69 2891 '-----------------------------\n'
jpayne@69 2892 '\n'
jpayne@69 2893 ' async_funcdef ::= [decorators] "async" "def" funcname "(" '
jpayne@69 2894 '[parameter_list] ")"\n'
jpayne@69 2895 ' ["->" expression] ":" suite\n'
jpayne@69 2896 '\n'
jpayne@69 2897 'Execution of Python coroutines can be suspended and resumed at '
jpayne@69 2898 'many\n'
jpayne@69 2899 'points (see *coroutine*). Inside the body of a coroutine '
jpayne@69 2900 'function,\n'
jpayne@69 2901 '"await" and "async" identifiers become reserved keywords; '
jpayne@69 2902 '"await"\n'
jpayne@69 2903 'expressions, "async for" and "async with" can only be used in\n'
jpayne@69 2904 'coroutine function bodies.\n'
jpayne@69 2905 '\n'
jpayne@69 2906 'Functions defined with "async def" syntax are always coroutine\n'
jpayne@69 2907 'functions, even if they do not contain "await" or "async" '
jpayne@69 2908 'keywords.\n'
jpayne@69 2909 '\n'
jpayne@69 2910 'It is a "SyntaxError" to use a "yield from" expression inside '
jpayne@69 2911 'the body\n'
jpayne@69 2912 'of a coroutine function.\n'
jpayne@69 2913 '\n'
jpayne@69 2914 'An example of a coroutine function:\n'
jpayne@69 2915 '\n'
jpayne@69 2916 ' async def func(param1, param2):\n'
jpayne@69 2917 ' do_stuff()\n'
jpayne@69 2918 ' await some_coroutine()\n'
jpayne@69 2919 '\n'
jpayne@69 2920 '\n'
jpayne@69 2921 'The "async for" statement\n'
jpayne@69 2922 '-------------------------\n'
jpayne@69 2923 '\n'
jpayne@69 2924 ' async_for_stmt ::= "async" for_stmt\n'
jpayne@69 2925 '\n'
jpayne@69 2926 'An *asynchronous iterable* is able to call asynchronous code in '
jpayne@69 2927 'its\n'
jpayne@69 2928 '*iter* implementation, and *asynchronous iterator* can call\n'
jpayne@69 2929 'asynchronous code in its *next* method.\n'
jpayne@69 2930 '\n'
jpayne@69 2931 'The "async for" statement allows convenient iteration over\n'
jpayne@69 2932 'asynchronous iterators.\n'
jpayne@69 2933 '\n'
jpayne@69 2934 'The following code:\n'
jpayne@69 2935 '\n'
jpayne@69 2936 ' async for TARGET in ITER:\n'
jpayne@69 2937 ' BLOCK\n'
jpayne@69 2938 ' else:\n'
jpayne@69 2939 ' BLOCK2\n'
jpayne@69 2940 '\n'
jpayne@69 2941 'Is semantically equivalent to:\n'
jpayne@69 2942 '\n'
jpayne@69 2943 ' iter = (ITER)\n'
jpayne@69 2944 ' iter = type(iter).__aiter__(iter)\n'
jpayne@69 2945 ' running = True\n'
jpayne@69 2946 ' while running:\n'
jpayne@69 2947 ' try:\n'
jpayne@69 2948 ' TARGET = await type(iter).__anext__(iter)\n'
jpayne@69 2949 ' except StopAsyncIteration:\n'
jpayne@69 2950 ' running = False\n'
jpayne@69 2951 ' else:\n'
jpayne@69 2952 ' BLOCK\n'
jpayne@69 2953 ' else:\n'
jpayne@69 2954 ' BLOCK2\n'
jpayne@69 2955 '\n'
jpayne@69 2956 'See also "__aiter__()" and "__anext__()" for details.\n'
jpayne@69 2957 '\n'
jpayne@69 2958 'It is a "SyntaxError" to use an "async for" statement outside '
jpayne@69 2959 'the body\n'
jpayne@69 2960 'of a coroutine function.\n'
jpayne@69 2961 '\n'
jpayne@69 2962 '\n'
jpayne@69 2963 'The "async with" statement\n'
jpayne@69 2964 '--------------------------\n'
jpayne@69 2965 '\n'
jpayne@69 2966 ' async_with_stmt ::= "async" with_stmt\n'
jpayne@69 2967 '\n'
jpayne@69 2968 'An *asynchronous context manager* is a *context manager* that is '
jpayne@69 2969 'able\n'
jpayne@69 2970 'to suspend execution in its *enter* and *exit* methods.\n'
jpayne@69 2971 '\n'
jpayne@69 2972 'The following code:\n'
jpayne@69 2973 '\n'
jpayne@69 2974 ' async with EXPR as VAR:\n'
jpayne@69 2975 ' BLOCK\n'
jpayne@69 2976 '\n'
jpayne@69 2977 'Is semantically equivalent to:\n'
jpayne@69 2978 '\n'
jpayne@69 2979 ' mgr = (EXPR)\n'
jpayne@69 2980 ' aexit = type(mgr).__aexit__\n'
jpayne@69 2981 ' aenter = type(mgr).__aenter__(mgr)\n'
jpayne@69 2982 '\n'
jpayne@69 2983 ' VAR = await aenter\n'
jpayne@69 2984 ' try:\n'
jpayne@69 2985 ' BLOCK\n'
jpayne@69 2986 ' except:\n'
jpayne@69 2987 ' if not await aexit(mgr, *sys.exc_info()):\n'
jpayne@69 2988 ' raise\n'
jpayne@69 2989 ' else:\n'
jpayne@69 2990 ' await aexit(mgr, None, None, None)\n'
jpayne@69 2991 '\n'
jpayne@69 2992 'See also "__aenter__()" and "__aexit__()" for details.\n'
jpayne@69 2993 '\n'
jpayne@69 2994 'It is a "SyntaxError" to use an "async with" statement outside '
jpayne@69 2995 'the\n'
jpayne@69 2996 'body of a coroutine function.\n'
jpayne@69 2997 '\n'
jpayne@69 2998 'See also:\n'
jpayne@69 2999 '\n'
jpayne@69 3000 ' **PEP 492** - Coroutines with async and await syntax\n'
jpayne@69 3001 ' The proposal that made coroutines a proper standalone '
jpayne@69 3002 'concept in\n'
jpayne@69 3003 ' Python, and added supporting syntax.\n'
jpayne@69 3004 '\n'
jpayne@69 3005 '-[ Footnotes ]-\n'
jpayne@69 3006 '\n'
jpayne@69 3007 '[1] The exception is propagated to the invocation stack unless\n'
jpayne@69 3008 ' there is a "finally" clause which happens to raise another\n'
jpayne@69 3009 ' exception. That new exception causes the old one to be '
jpayne@69 3010 'lost.\n'
jpayne@69 3011 '\n'
jpayne@69 3012 '[2] A string literal appearing as the first statement in the\n'
jpayne@69 3013 ' function body is transformed into the function’s "__doc__"\n'
jpayne@69 3014 ' attribute and therefore the function’s *docstring*.\n'
jpayne@69 3015 '\n'
jpayne@69 3016 '[3] A string literal appearing as the first statement in the '
jpayne@69 3017 'class\n'
jpayne@69 3018 ' body is transformed into the namespace’s "__doc__" item and\n'
jpayne@69 3019 ' therefore the class’s *docstring*.\n',
jpayne@69 3020 'context-managers': 'With Statement Context Managers\n'
jpayne@69 3021 '*******************************\n'
jpayne@69 3022 '\n'
jpayne@69 3023 'A *context manager* is an object that defines the '
jpayne@69 3024 'runtime context to\n'
jpayne@69 3025 'be established when executing a "with" statement. The '
jpayne@69 3026 'context manager\n'
jpayne@69 3027 'handles the entry into, and the exit from, the desired '
jpayne@69 3028 'runtime context\n'
jpayne@69 3029 'for the execution of the block of code. Context '
jpayne@69 3030 'managers are normally\n'
jpayne@69 3031 'invoked using the "with" statement (described in section '
jpayne@69 3032 'The with\n'
jpayne@69 3033 'statement), but can also be used by directly invoking '
jpayne@69 3034 'their methods.\n'
jpayne@69 3035 '\n'
jpayne@69 3036 'Typical uses of context managers include saving and '
jpayne@69 3037 'restoring various\n'
jpayne@69 3038 'kinds of global state, locking and unlocking resources, '
jpayne@69 3039 'closing opened\n'
jpayne@69 3040 'files, etc.\n'
jpayne@69 3041 '\n'
jpayne@69 3042 'For more information on context managers, see Context '
jpayne@69 3043 'Manager Types.\n'
jpayne@69 3044 '\n'
jpayne@69 3045 'object.__enter__(self)\n'
jpayne@69 3046 '\n'
jpayne@69 3047 ' Enter the runtime context related to this object. The '
jpayne@69 3048 '"with"\n'
jpayne@69 3049 ' statement will bind this method’s return value to the '
jpayne@69 3050 'target(s)\n'
jpayne@69 3051 ' specified in the "as" clause of the statement, if '
jpayne@69 3052 'any.\n'
jpayne@69 3053 '\n'
jpayne@69 3054 'object.__exit__(self, exc_type, exc_value, traceback)\n'
jpayne@69 3055 '\n'
jpayne@69 3056 ' Exit the runtime context related to this object. The '
jpayne@69 3057 'parameters\n'
jpayne@69 3058 ' describe the exception that caused the context to be '
jpayne@69 3059 'exited. If the\n'
jpayne@69 3060 ' context was exited without an exception, all three '
jpayne@69 3061 'arguments will\n'
jpayne@69 3062 ' be "None".\n'
jpayne@69 3063 '\n'
jpayne@69 3064 ' If an exception is supplied, and the method wishes to '
jpayne@69 3065 'suppress the\n'
jpayne@69 3066 ' exception (i.e., prevent it from being propagated), '
jpayne@69 3067 'it should\n'
jpayne@69 3068 ' return a true value. Otherwise, the exception will be '
jpayne@69 3069 'processed\n'
jpayne@69 3070 ' normally upon exit from this method.\n'
jpayne@69 3071 '\n'
jpayne@69 3072 ' Note that "__exit__()" methods should not reraise the '
jpayne@69 3073 'passed-in\n'
jpayne@69 3074 ' exception; this is the caller’s responsibility.\n'
jpayne@69 3075 '\n'
jpayne@69 3076 'See also:\n'
jpayne@69 3077 '\n'
jpayne@69 3078 ' **PEP 343** - The “with” statement\n'
jpayne@69 3079 ' The specification, background, and examples for the '
jpayne@69 3080 'Python "with"\n'
jpayne@69 3081 ' statement.\n',
jpayne@69 3082 'continue': 'The "continue" statement\n'
jpayne@69 3083 '************************\n'
jpayne@69 3084 '\n'
jpayne@69 3085 ' continue_stmt ::= "continue"\n'
jpayne@69 3086 '\n'
jpayne@69 3087 '"continue" may only occur syntactically nested in a "for" or '
jpayne@69 3088 '"while"\n'
jpayne@69 3089 'loop, but not nested in a function or class definition within '
jpayne@69 3090 'that\n'
jpayne@69 3091 'loop. It continues with the next cycle of the nearest enclosing '
jpayne@69 3092 'loop.\n'
jpayne@69 3093 '\n'
jpayne@69 3094 'When "continue" passes control out of a "try" statement with a\n'
jpayne@69 3095 '"finally" clause, that "finally" clause is executed before '
jpayne@69 3096 'really\n'
jpayne@69 3097 'starting the next loop cycle.\n',
jpayne@69 3098 'conversions': 'Arithmetic conversions\n'
jpayne@69 3099 '**********************\n'
jpayne@69 3100 '\n'
jpayne@69 3101 'When a description of an arithmetic operator below uses the '
jpayne@69 3102 'phrase\n'
jpayne@69 3103 '“the numeric arguments are converted to a common type,” this '
jpayne@69 3104 'means\n'
jpayne@69 3105 'that the operator implementation for built-in types works as '
jpayne@69 3106 'follows:\n'
jpayne@69 3107 '\n'
jpayne@69 3108 '* If either argument is a complex number, the other is '
jpayne@69 3109 'converted to\n'
jpayne@69 3110 ' complex;\n'
jpayne@69 3111 '\n'
jpayne@69 3112 '* otherwise, if either argument is a floating point number, '
jpayne@69 3113 'the\n'
jpayne@69 3114 ' other is converted to floating point;\n'
jpayne@69 3115 '\n'
jpayne@69 3116 '* otherwise, both must be integers and no conversion is '
jpayne@69 3117 'necessary.\n'
jpayne@69 3118 '\n'
jpayne@69 3119 'Some additional rules apply for certain operators (e.g., a '
jpayne@69 3120 'string as a\n'
jpayne@69 3121 'left argument to the ‘%’ operator). Extensions must define '
jpayne@69 3122 'their own\n'
jpayne@69 3123 'conversion behavior.\n',
jpayne@69 3124 'customization': 'Basic customization\n'
jpayne@69 3125 '*******************\n'
jpayne@69 3126 '\n'
jpayne@69 3127 'object.__new__(cls[, ...])\n'
jpayne@69 3128 '\n'
jpayne@69 3129 ' Called to create a new instance of class *cls*. '
jpayne@69 3130 '"__new__()" is a\n'
jpayne@69 3131 ' static method (special-cased so you need not declare it '
jpayne@69 3132 'as such)\n'
jpayne@69 3133 ' that takes the class of which an instance was requested '
jpayne@69 3134 'as its\n'
jpayne@69 3135 ' first argument. The remaining arguments are those '
jpayne@69 3136 'passed to the\n'
jpayne@69 3137 ' object constructor expression (the call to the class). '
jpayne@69 3138 'The return\n'
jpayne@69 3139 ' value of "__new__()" should be the new object instance '
jpayne@69 3140 '(usually an\n'
jpayne@69 3141 ' instance of *cls*).\n'
jpayne@69 3142 '\n'
jpayne@69 3143 ' Typical implementations create a new instance of the '
jpayne@69 3144 'class by\n'
jpayne@69 3145 ' invoking the superclass’s "__new__()" method using\n'
jpayne@69 3146 ' "super().__new__(cls[, ...])" with appropriate arguments '
jpayne@69 3147 'and then\n'
jpayne@69 3148 ' modifying the newly-created instance as necessary before '
jpayne@69 3149 'returning\n'
jpayne@69 3150 ' it.\n'
jpayne@69 3151 '\n'
jpayne@69 3152 ' If "__new__()" is invoked during object construction and '
jpayne@69 3153 'it returns\n'
jpayne@69 3154 ' an instance or subclass of *cls*, then the new '
jpayne@69 3155 'instance’s\n'
jpayne@69 3156 ' "__init__()" method will be invoked like '
jpayne@69 3157 '"__init__(self[, ...])",\n'
jpayne@69 3158 ' where *self* is the new instance and the remaining '
jpayne@69 3159 'arguments are\n'
jpayne@69 3160 ' the same as were passed to the object constructor.\n'
jpayne@69 3161 '\n'
jpayne@69 3162 ' If "__new__()" does not return an instance of *cls*, '
jpayne@69 3163 'then the new\n'
jpayne@69 3164 ' instance’s "__init__()" method will not be invoked.\n'
jpayne@69 3165 '\n'
jpayne@69 3166 ' "__new__()" is intended mainly to allow subclasses of '
jpayne@69 3167 'immutable\n'
jpayne@69 3168 ' types (like int, str, or tuple) to customize instance '
jpayne@69 3169 'creation. It\n'
jpayne@69 3170 ' is also commonly overridden in custom metaclasses in '
jpayne@69 3171 'order to\n'
jpayne@69 3172 ' customize class creation.\n'
jpayne@69 3173 '\n'
jpayne@69 3174 'object.__init__(self[, ...])\n'
jpayne@69 3175 '\n'
jpayne@69 3176 ' Called after the instance has been created (by '
jpayne@69 3177 '"__new__()"), but\n'
jpayne@69 3178 ' before it is returned to the caller. The arguments are '
jpayne@69 3179 'those\n'
jpayne@69 3180 ' passed to the class constructor expression. If a base '
jpayne@69 3181 'class has an\n'
jpayne@69 3182 ' "__init__()" method, the derived class’s "__init__()" '
jpayne@69 3183 'method, if\n'
jpayne@69 3184 ' any, must explicitly call it to ensure proper '
jpayne@69 3185 'initialization of the\n'
jpayne@69 3186 ' base class part of the instance; for example:\n'
jpayne@69 3187 ' "super().__init__([args...])".\n'
jpayne@69 3188 '\n'
jpayne@69 3189 ' Because "__new__()" and "__init__()" work together in '
jpayne@69 3190 'constructing\n'
jpayne@69 3191 ' objects ("__new__()" to create it, and "__init__()" to '
jpayne@69 3192 'customize\n'
jpayne@69 3193 ' it), no non-"None" value may be returned by '
jpayne@69 3194 '"__init__()"; doing so\n'
jpayne@69 3195 ' will cause a "TypeError" to be raised at runtime.\n'
jpayne@69 3196 '\n'
jpayne@69 3197 'object.__del__(self)\n'
jpayne@69 3198 '\n'
jpayne@69 3199 ' Called when the instance is about to be destroyed. This '
jpayne@69 3200 'is also\n'
jpayne@69 3201 ' called a finalizer or (improperly) a destructor. If a '
jpayne@69 3202 'base class\n'
jpayne@69 3203 ' has a "__del__()" method, the derived class’s '
jpayne@69 3204 '"__del__()" method,\n'
jpayne@69 3205 ' if any, must explicitly call it to ensure proper '
jpayne@69 3206 'deletion of the\n'
jpayne@69 3207 ' base class part of the instance.\n'
jpayne@69 3208 '\n'
jpayne@69 3209 ' It is possible (though not recommended!) for the '
jpayne@69 3210 '"__del__()" method\n'
jpayne@69 3211 ' to postpone destruction of the instance by creating a '
jpayne@69 3212 'new reference\n'
jpayne@69 3213 ' to it. This is called object *resurrection*. It is\n'
jpayne@69 3214 ' implementation-dependent whether "__del__()" is called a '
jpayne@69 3215 'second\n'
jpayne@69 3216 ' time when a resurrected object is about to be destroyed; '
jpayne@69 3217 'the\n'
jpayne@69 3218 ' current *CPython* implementation only calls it once.\n'
jpayne@69 3219 '\n'
jpayne@69 3220 ' It is not guaranteed that "__del__()" methods are called '
jpayne@69 3221 'for\n'
jpayne@69 3222 ' objects that still exist when the interpreter exits.\n'
jpayne@69 3223 '\n'
jpayne@69 3224 ' Note: "del x" doesn’t directly call "x.__del__()" — the '
jpayne@69 3225 'former\n'
jpayne@69 3226 ' decrements the reference count for "x" by one, and the '
jpayne@69 3227 'latter is\n'
jpayne@69 3228 ' only called when "x"’s reference count reaches zero.\n'
jpayne@69 3229 '\n'
jpayne@69 3230 ' **CPython implementation detail:** It is possible for a '
jpayne@69 3231 'reference\n'
jpayne@69 3232 ' cycle to prevent the reference count of an object from '
jpayne@69 3233 'going to\n'
jpayne@69 3234 ' zero. In this case, the cycle will be later detected '
jpayne@69 3235 'and deleted\n'
jpayne@69 3236 ' by the *cyclic garbage collector*. A common cause of '
jpayne@69 3237 'reference\n'
jpayne@69 3238 ' cycles is when an exception has been caught in a local '
jpayne@69 3239 'variable.\n'
jpayne@69 3240 ' The frame’s locals then reference the exception, which '
jpayne@69 3241 'references\n'
jpayne@69 3242 ' its own traceback, which references the locals of all '
jpayne@69 3243 'frames caught\n'
jpayne@69 3244 ' in the traceback.\n'
jpayne@69 3245 '\n'
jpayne@69 3246 ' See also: Documentation for the "gc" module.\n'
jpayne@69 3247 '\n'
jpayne@69 3248 ' Warning: Due to the precarious circumstances under '
jpayne@69 3249 'which\n'
jpayne@69 3250 ' "__del__()" methods are invoked, exceptions that occur '
jpayne@69 3251 'during\n'
jpayne@69 3252 ' their execution are ignored, and a warning is printed '
jpayne@69 3253 'to\n'
jpayne@69 3254 ' "sys.stderr" instead. In particular:\n'
jpayne@69 3255 '\n'
jpayne@69 3256 ' * "__del__()" can be invoked when arbitrary code is '
jpayne@69 3257 'being\n'
jpayne@69 3258 ' executed, including from any arbitrary thread. If '
jpayne@69 3259 '"__del__()"\n'
jpayne@69 3260 ' needs to take a lock or invoke any other blocking '
jpayne@69 3261 'resource, it\n'
jpayne@69 3262 ' may deadlock as the resource may already be taken by '
jpayne@69 3263 'the code\n'
jpayne@69 3264 ' that gets interrupted to execute "__del__()".\n'
jpayne@69 3265 '\n'
jpayne@69 3266 ' * "__del__()" can be executed during interpreter '
jpayne@69 3267 'shutdown. As\n'
jpayne@69 3268 ' a consequence, the global variables it needs to '
jpayne@69 3269 'access\n'
jpayne@69 3270 ' (including other modules) may already have been '
jpayne@69 3271 'deleted or set\n'
jpayne@69 3272 ' to "None". Python guarantees that globals whose name '
jpayne@69 3273 'begins\n'
jpayne@69 3274 ' with a single underscore are deleted from their '
jpayne@69 3275 'module before\n'
jpayne@69 3276 ' other globals are deleted; if no other references to '
jpayne@69 3277 'such\n'
jpayne@69 3278 ' globals exist, this may help in assuring that '
jpayne@69 3279 'imported modules\n'
jpayne@69 3280 ' are still available at the time when the "__del__()" '
jpayne@69 3281 'method is\n'
jpayne@69 3282 ' called.\n'
jpayne@69 3283 '\n'
jpayne@69 3284 'object.__repr__(self)\n'
jpayne@69 3285 '\n'
jpayne@69 3286 ' Called by the "repr()" built-in function to compute the '
jpayne@69 3287 '“official”\n'
jpayne@69 3288 ' string representation of an object. If at all possible, '
jpayne@69 3289 'this\n'
jpayne@69 3290 ' should look like a valid Python expression that could be '
jpayne@69 3291 'used to\n'
jpayne@69 3292 ' recreate an object with the same value (given an '
jpayne@69 3293 'appropriate\n'
jpayne@69 3294 ' environment). If this is not possible, a string of the '
jpayne@69 3295 'form\n'
jpayne@69 3296 ' "<...some useful description...>" should be returned. '
jpayne@69 3297 'The return\n'
jpayne@69 3298 ' value must be a string object. If a class defines '
jpayne@69 3299 '"__repr__()" but\n'
jpayne@69 3300 ' not "__str__()", then "__repr__()" is also used when an '
jpayne@69 3301 '“informal”\n'
jpayne@69 3302 ' string representation of instances of that class is '
jpayne@69 3303 'required.\n'
jpayne@69 3304 '\n'
jpayne@69 3305 ' This is typically used for debugging, so it is important '
jpayne@69 3306 'that the\n'
jpayne@69 3307 ' representation is information-rich and unambiguous.\n'
jpayne@69 3308 '\n'
jpayne@69 3309 'object.__str__(self)\n'
jpayne@69 3310 '\n'
jpayne@69 3311 ' Called by "str(object)" and the built-in functions '
jpayne@69 3312 '"format()" and\n'
jpayne@69 3313 ' "print()" to compute the “informal” or nicely printable '
jpayne@69 3314 'string\n'
jpayne@69 3315 ' representation of an object. The return value must be a '
jpayne@69 3316 'string\n'
jpayne@69 3317 ' object.\n'
jpayne@69 3318 '\n'
jpayne@69 3319 ' This method differs from "object.__repr__()" in that '
jpayne@69 3320 'there is no\n'
jpayne@69 3321 ' expectation that "__str__()" return a valid Python '
jpayne@69 3322 'expression: a\n'
jpayne@69 3323 ' more convenient or concise representation can be used.\n'
jpayne@69 3324 '\n'
jpayne@69 3325 ' The default implementation defined by the built-in type '
jpayne@69 3326 '"object"\n'
jpayne@69 3327 ' calls "object.__repr__()".\n'
jpayne@69 3328 '\n'
jpayne@69 3329 'object.__bytes__(self)\n'
jpayne@69 3330 '\n'
jpayne@69 3331 ' Called by bytes to compute a byte-string representation '
jpayne@69 3332 'of an\n'
jpayne@69 3333 ' object. This should return a "bytes" object.\n'
jpayne@69 3334 '\n'
jpayne@69 3335 'object.__format__(self, format_spec)\n'
jpayne@69 3336 '\n'
jpayne@69 3337 ' Called by the "format()" built-in function, and by '
jpayne@69 3338 'extension,\n'
jpayne@69 3339 ' evaluation of formatted string literals and the '
jpayne@69 3340 '"str.format()"\n'
jpayne@69 3341 ' method, to produce a “formatted” string representation '
jpayne@69 3342 'of an\n'
jpayne@69 3343 ' object. The *format_spec* argument is a string that '
jpayne@69 3344 'contains a\n'
jpayne@69 3345 ' description of the formatting options desired. The '
jpayne@69 3346 'interpretation\n'
jpayne@69 3347 ' of the *format_spec* argument is up to the type '
jpayne@69 3348 'implementing\n'
jpayne@69 3349 ' "__format__()", however most classes will either '
jpayne@69 3350 'delegate\n'
jpayne@69 3351 ' formatting to one of the built-in types, or use a '
jpayne@69 3352 'similar\n'
jpayne@69 3353 ' formatting option syntax.\n'
jpayne@69 3354 '\n'
jpayne@69 3355 ' See Format Specification Mini-Language for a description '
jpayne@69 3356 'of the\n'
jpayne@69 3357 ' standard formatting syntax.\n'
jpayne@69 3358 '\n'
jpayne@69 3359 ' The return value must be a string object.\n'
jpayne@69 3360 '\n'
jpayne@69 3361 ' Changed in version 3.4: The __format__ method of '
jpayne@69 3362 '"object" itself\n'
jpayne@69 3363 ' raises a "TypeError" if passed any non-empty string.\n'
jpayne@69 3364 '\n'
jpayne@69 3365 ' Changed in version 3.7: "object.__format__(x, \'\')" is '
jpayne@69 3366 'now\n'
jpayne@69 3367 ' equivalent to "str(x)" rather than "format(str(self), '
jpayne@69 3368 '\'\')".\n'
jpayne@69 3369 '\n'
jpayne@69 3370 'object.__lt__(self, other)\n'
jpayne@69 3371 'object.__le__(self, other)\n'
jpayne@69 3372 'object.__eq__(self, other)\n'
jpayne@69 3373 'object.__ne__(self, other)\n'
jpayne@69 3374 'object.__gt__(self, other)\n'
jpayne@69 3375 'object.__ge__(self, other)\n'
jpayne@69 3376 '\n'
jpayne@69 3377 ' These are the so-called “rich comparison” methods. The\n'
jpayne@69 3378 ' correspondence between operator symbols and method names '
jpayne@69 3379 'is as\n'
jpayne@69 3380 ' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls '
jpayne@69 3381 '"x.__le__(y)",\n'
jpayne@69 3382 ' "x==y" calls "x.__eq__(y)", "x!=y" calls "x.__ne__(y)", '
jpayne@69 3383 '"x>y" calls\n'
jpayne@69 3384 ' "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".\n'
jpayne@69 3385 '\n'
jpayne@69 3386 ' A rich comparison method may return the singleton '
jpayne@69 3387 '"NotImplemented"\n'
jpayne@69 3388 ' if it does not implement the operation for a given pair '
jpayne@69 3389 'of\n'
jpayne@69 3390 ' arguments. By convention, "False" and "True" are '
jpayne@69 3391 'returned for a\n'
jpayne@69 3392 ' successful comparison. However, these methods can return '
jpayne@69 3393 'any value,\n'
jpayne@69 3394 ' so if the comparison operator is used in a Boolean '
jpayne@69 3395 'context (e.g.,\n'
jpayne@69 3396 ' in the condition of an "if" statement), Python will call '
jpayne@69 3397 '"bool()"\n'
jpayne@69 3398 ' on the value to determine if the result is true or '
jpayne@69 3399 'false.\n'
jpayne@69 3400 '\n'
jpayne@69 3401 ' By default, "__ne__()" delegates to "__eq__()" and '
jpayne@69 3402 'inverts the\n'
jpayne@69 3403 ' result unless it is "NotImplemented". There are no '
jpayne@69 3404 'other implied\n'
jpayne@69 3405 ' relationships among the comparison operators, for '
jpayne@69 3406 'example, the\n'
jpayne@69 3407 ' truth of "(x<y or x==y)" does not imply "x<=y". To '
jpayne@69 3408 'automatically\n'
jpayne@69 3409 ' generate ordering operations from a single root '
jpayne@69 3410 'operation, see\n'
jpayne@69 3411 ' "functools.total_ordering()".\n'
jpayne@69 3412 '\n'
jpayne@69 3413 ' See the paragraph on "__hash__()" for some important '
jpayne@69 3414 'notes on\n'
jpayne@69 3415 ' creating *hashable* objects which support custom '
jpayne@69 3416 'comparison\n'
jpayne@69 3417 ' operations and are usable as dictionary keys.\n'
jpayne@69 3418 '\n'
jpayne@69 3419 ' There are no swapped-argument versions of these methods '
jpayne@69 3420 '(to be used\n'
jpayne@69 3421 ' when the left argument does not support the operation '
jpayne@69 3422 'but the right\n'
jpayne@69 3423 ' argument does); rather, "__lt__()" and "__gt__()" are '
jpayne@69 3424 'each other’s\n'
jpayne@69 3425 ' reflection, "__le__()" and "__ge__()" are each other’s '
jpayne@69 3426 'reflection,\n'
jpayne@69 3427 ' and "__eq__()" and "__ne__()" are their own reflection. '
jpayne@69 3428 'If the\n'
jpayne@69 3429 ' operands are of different types, and right operand’s '
jpayne@69 3430 'type is a\n'
jpayne@69 3431 ' direct or indirect subclass of the left operand’s type, '
jpayne@69 3432 'the\n'
jpayne@69 3433 ' reflected method of the right operand has priority, '
jpayne@69 3434 'otherwise the\n'
jpayne@69 3435 ' left operand’s method has priority. Virtual subclassing '
jpayne@69 3436 'is not\n'
jpayne@69 3437 ' considered.\n'
jpayne@69 3438 '\n'
jpayne@69 3439 'object.__hash__(self)\n'
jpayne@69 3440 '\n'
jpayne@69 3441 ' Called by built-in function "hash()" and for operations '
jpayne@69 3442 'on members\n'
jpayne@69 3443 ' of hashed collections including "set", "frozenset", and '
jpayne@69 3444 '"dict".\n'
jpayne@69 3445 ' "__hash__()" should return an integer. The only required '
jpayne@69 3446 'property\n'
jpayne@69 3447 ' is that objects which compare equal have the same hash '
jpayne@69 3448 'value; it is\n'
jpayne@69 3449 ' advised to mix together the hash values of the '
jpayne@69 3450 'components of the\n'
jpayne@69 3451 ' object that also play a part in comparison of objects by '
jpayne@69 3452 'packing\n'
jpayne@69 3453 ' them into a tuple and hashing the tuple. Example:\n'
jpayne@69 3454 '\n'
jpayne@69 3455 ' def __hash__(self):\n'
jpayne@69 3456 ' return hash((self.name, self.nick, self.color))\n'
jpayne@69 3457 '\n'
jpayne@69 3458 ' Note: "hash()" truncates the value returned from an '
jpayne@69 3459 'object’s\n'
jpayne@69 3460 ' custom "__hash__()" method to the size of a '
jpayne@69 3461 '"Py_ssize_t". This\n'
jpayne@69 3462 ' is typically 8 bytes on 64-bit builds and 4 bytes on '
jpayne@69 3463 '32-bit\n'
jpayne@69 3464 ' builds. If an object’s "__hash__()" must '
jpayne@69 3465 'interoperate on builds\n'
jpayne@69 3466 ' of different bit sizes, be sure to check the width on '
jpayne@69 3467 'all\n'
jpayne@69 3468 ' supported builds. An easy way to do this is with '
jpayne@69 3469 '"python -c\n'
jpayne@69 3470 ' "import sys; print(sys.hash_info.width)"".\n'
jpayne@69 3471 '\n'
jpayne@69 3472 ' If a class does not define an "__eq__()" method it '
jpayne@69 3473 'should not\n'
jpayne@69 3474 ' define a "__hash__()" operation either; if it defines '
jpayne@69 3475 '"__eq__()"\n'
jpayne@69 3476 ' but not "__hash__()", its instances will not be usable '
jpayne@69 3477 'as items in\n'
jpayne@69 3478 ' hashable collections. If a class defines mutable '
jpayne@69 3479 'objects and\n'
jpayne@69 3480 ' implements an "__eq__()" method, it should not '
jpayne@69 3481 'implement\n'
jpayne@69 3482 ' "__hash__()", since the implementation of hashable '
jpayne@69 3483 'collections\n'
jpayne@69 3484 ' requires that a key’s hash value is immutable (if the '
jpayne@69 3485 'object’s hash\n'
jpayne@69 3486 ' value changes, it will be in the wrong hash bucket).\n'
jpayne@69 3487 '\n'
jpayne@69 3488 ' User-defined classes have "__eq__()" and "__hash__()" '
jpayne@69 3489 'methods by\n'
jpayne@69 3490 ' default; with them, all objects compare unequal (except '
jpayne@69 3491 'with\n'
jpayne@69 3492 ' themselves) and "x.__hash__()" returns an appropriate '
jpayne@69 3493 'value such\n'
jpayne@69 3494 ' that "x == y" implies both that "x is y" and "hash(x) == '
jpayne@69 3495 'hash(y)".\n'
jpayne@69 3496 '\n'
jpayne@69 3497 ' A class that overrides "__eq__()" and does not define '
jpayne@69 3498 '"__hash__()"\n'
jpayne@69 3499 ' will have its "__hash__()" implicitly set to "None". '
jpayne@69 3500 'When the\n'
jpayne@69 3501 ' "__hash__()" method of a class is "None", instances of '
jpayne@69 3502 'the class\n'
jpayne@69 3503 ' will raise an appropriate "TypeError" when a program '
jpayne@69 3504 'attempts to\n'
jpayne@69 3505 ' retrieve their hash value, and will also be correctly '
jpayne@69 3506 'identified as\n'
jpayne@69 3507 ' unhashable when checking "isinstance(obj,\n'
jpayne@69 3508 ' collections.abc.Hashable)".\n'
jpayne@69 3509 '\n'
jpayne@69 3510 ' If a class that overrides "__eq__()" needs to retain '
jpayne@69 3511 'the\n'
jpayne@69 3512 ' implementation of "__hash__()" from a parent class, the '
jpayne@69 3513 'interpreter\n'
jpayne@69 3514 ' must be told this explicitly by setting "__hash__ =\n'
jpayne@69 3515 ' <ParentClass>.__hash__".\n'
jpayne@69 3516 '\n'
jpayne@69 3517 ' If a class that does not override "__eq__()" wishes to '
jpayne@69 3518 'suppress\n'
jpayne@69 3519 ' hash support, it should include "__hash__ = None" in the '
jpayne@69 3520 'class\n'
jpayne@69 3521 ' definition. A class which defines its own "__hash__()" '
jpayne@69 3522 'that\n'
jpayne@69 3523 ' explicitly raises a "TypeError" would be incorrectly '
jpayne@69 3524 'identified as\n'
jpayne@69 3525 ' hashable by an "isinstance(obj, '
jpayne@69 3526 'collections.abc.Hashable)" call.\n'
jpayne@69 3527 '\n'
jpayne@69 3528 ' Note: By default, the "__hash__()" values of str and '
jpayne@69 3529 'bytes\n'
jpayne@69 3530 ' objects are “salted” with an unpredictable random '
jpayne@69 3531 'value.\n'
jpayne@69 3532 ' Although they remain constant within an individual '
jpayne@69 3533 'Python\n'
jpayne@69 3534 ' process, they are not predictable between repeated '
jpayne@69 3535 'invocations of\n'
jpayne@69 3536 ' Python.This is intended to provide protection against '
jpayne@69 3537 'a denial-\n'
jpayne@69 3538 ' of-service caused by carefully-chosen inputs that '
jpayne@69 3539 'exploit the\n'
jpayne@69 3540 ' worst case performance of a dict insertion, O(n^2) '
jpayne@69 3541 'complexity.\n'
jpayne@69 3542 ' See '
jpayne@69 3543 'http://www.ocert.org/advisories/ocert-2011-003.html for\n'
jpayne@69 3544 ' details.Changing hash values affects the iteration '
jpayne@69 3545 'order of sets.\n'
jpayne@69 3546 ' Python has never made guarantees about this ordering '
jpayne@69 3547 '(and it\n'
jpayne@69 3548 ' typically varies between 32-bit and 64-bit builds).See '
jpayne@69 3549 'also\n'
jpayne@69 3550 ' "PYTHONHASHSEED".\n'
jpayne@69 3551 '\n'
jpayne@69 3552 ' Changed in version 3.3: Hash randomization is enabled by '
jpayne@69 3553 'default.\n'
jpayne@69 3554 '\n'
jpayne@69 3555 'object.__bool__(self)\n'
jpayne@69 3556 '\n'
jpayne@69 3557 ' Called to implement truth value testing and the built-in '
jpayne@69 3558 'operation\n'
jpayne@69 3559 ' "bool()"; should return "False" or "True". When this '
jpayne@69 3560 'method is not\n'
jpayne@69 3561 ' defined, "__len__()" is called, if it is defined, and '
jpayne@69 3562 'the object is\n'
jpayne@69 3563 ' considered true if its result is nonzero. If a class '
jpayne@69 3564 'defines\n'
jpayne@69 3565 ' neither "__len__()" nor "__bool__()", all its instances '
jpayne@69 3566 'are\n'
jpayne@69 3567 ' considered true.\n',
jpayne@69 3568 'debugger': '"pdb" — The Python Debugger\n'
jpayne@69 3569 '***************************\n'
jpayne@69 3570 '\n'
jpayne@69 3571 '**Source code:** Lib/pdb.py\n'
jpayne@69 3572 '\n'
jpayne@69 3573 '======================================================================\n'
jpayne@69 3574 '\n'
jpayne@69 3575 'The module "pdb" defines an interactive source code debugger '
jpayne@69 3576 'for\n'
jpayne@69 3577 'Python programs. It supports setting (conditional) breakpoints '
jpayne@69 3578 'and\n'
jpayne@69 3579 'single stepping at the source line level, inspection of stack '
jpayne@69 3580 'frames,\n'
jpayne@69 3581 'source code listing, and evaluation of arbitrary Python code in '
jpayne@69 3582 'the\n'
jpayne@69 3583 'context of any stack frame. It also supports post-mortem '
jpayne@69 3584 'debugging\n'
jpayne@69 3585 'and can be called under program control.\n'
jpayne@69 3586 '\n'
jpayne@69 3587 'The debugger is extensible – it is actually defined as the '
jpayne@69 3588 'class\n'
jpayne@69 3589 '"Pdb". This is currently undocumented but easily understood by '
jpayne@69 3590 'reading\n'
jpayne@69 3591 'the source. The extension interface uses the modules "bdb" and '
jpayne@69 3592 '"cmd".\n'
jpayne@69 3593 '\n'
jpayne@69 3594 'The debugger’s prompt is "(Pdb)". Typical usage to run a program '
jpayne@69 3595 'under\n'
jpayne@69 3596 'control of the debugger is:\n'
jpayne@69 3597 '\n'
jpayne@69 3598 ' >>> import pdb\n'
jpayne@69 3599 ' >>> import mymodule\n'
jpayne@69 3600 " >>> pdb.run('mymodule.test()')\n"
jpayne@69 3601 ' > <string>(0)?()\n'
jpayne@69 3602 ' (Pdb) continue\n'
jpayne@69 3603 ' > <string>(1)?()\n'
jpayne@69 3604 ' (Pdb) continue\n'
jpayne@69 3605 " NameError: 'spam'\n"
jpayne@69 3606 ' > <string>(1)?()\n'
jpayne@69 3607 ' (Pdb)\n'
jpayne@69 3608 '\n'
jpayne@69 3609 'Changed in version 3.3: Tab-completion via the "readline" module '
jpayne@69 3610 'is\n'
jpayne@69 3611 'available for commands and command arguments, e.g. the current '
jpayne@69 3612 'global\n'
jpayne@69 3613 'and local names are offered as arguments of the "p" command.\n'
jpayne@69 3614 '\n'
jpayne@69 3615 '"pdb.py" can also be invoked as a script to debug other '
jpayne@69 3616 'scripts. For\n'
jpayne@69 3617 'example:\n'
jpayne@69 3618 '\n'
jpayne@69 3619 ' python3 -m pdb myscript.py\n'
jpayne@69 3620 '\n'
jpayne@69 3621 'When invoked as a script, pdb will automatically enter '
jpayne@69 3622 'post-mortem\n'
jpayne@69 3623 'debugging if the program being debugged exits abnormally. After '
jpayne@69 3624 'post-\n'
jpayne@69 3625 'mortem debugging (or after normal exit of the program), pdb '
jpayne@69 3626 'will\n'
jpayne@69 3627 'restart the program. Automatic restarting preserves pdb’s state '
jpayne@69 3628 '(such\n'
jpayne@69 3629 'as breakpoints) and in most cases is more useful than quitting '
jpayne@69 3630 'the\n'
jpayne@69 3631 'debugger upon program’s exit.\n'
jpayne@69 3632 '\n'
jpayne@69 3633 'New in version 3.2: "pdb.py" now accepts a "-c" option that '
jpayne@69 3634 'executes\n'
jpayne@69 3635 'commands as if given in a ".pdbrc" file, see Debugger Commands.\n'
jpayne@69 3636 '\n'
jpayne@69 3637 'New in version 3.7: "pdb.py" now accepts a "-m" option that '
jpayne@69 3638 'execute\n'
jpayne@69 3639 'modules similar to the way "python3 -m" does. As with a script, '
jpayne@69 3640 'the\n'
jpayne@69 3641 'debugger will pause execution just before the first line of the\n'
jpayne@69 3642 'module.\n'
jpayne@69 3643 '\n'
jpayne@69 3644 'The typical usage to break into the debugger from a running '
jpayne@69 3645 'program is\n'
jpayne@69 3646 'to insert\n'
jpayne@69 3647 '\n'
jpayne@69 3648 ' import pdb; pdb.set_trace()\n'
jpayne@69 3649 '\n'
jpayne@69 3650 'at the location you want to break into the debugger. You can '
jpayne@69 3651 'then\n'
jpayne@69 3652 'step through the code following this statement, and continue '
jpayne@69 3653 'running\n'
jpayne@69 3654 'without the debugger using the "continue" command.\n'
jpayne@69 3655 '\n'
jpayne@69 3656 'New in version 3.7: The built-in "breakpoint()", when called '
jpayne@69 3657 'with\n'
jpayne@69 3658 'defaults, can be used instead of "import pdb; pdb.set_trace()".\n'
jpayne@69 3659 '\n'
jpayne@69 3660 'The typical usage to inspect a crashed program is:\n'
jpayne@69 3661 '\n'
jpayne@69 3662 ' >>> import pdb\n'
jpayne@69 3663 ' >>> import mymodule\n'
jpayne@69 3664 ' >>> mymodule.test()\n'
jpayne@69 3665 ' Traceback (most recent call last):\n'
jpayne@69 3666 ' File "<stdin>", line 1, in <module>\n'
jpayne@69 3667 ' File "./mymodule.py", line 4, in test\n'
jpayne@69 3668 ' test2()\n'
jpayne@69 3669 ' File "./mymodule.py", line 3, in test2\n'
jpayne@69 3670 ' print(spam)\n'
jpayne@69 3671 ' NameError: spam\n'
jpayne@69 3672 ' >>> pdb.pm()\n'
jpayne@69 3673 ' > ./mymodule.py(3)test2()\n'
jpayne@69 3674 ' -> print(spam)\n'
jpayne@69 3675 ' (Pdb)\n'
jpayne@69 3676 '\n'
jpayne@69 3677 'The module defines the following functions; each enters the '
jpayne@69 3678 'debugger\n'
jpayne@69 3679 'in a slightly different way:\n'
jpayne@69 3680 '\n'
jpayne@69 3681 'pdb.run(statement, globals=None, locals=None)\n'
jpayne@69 3682 '\n'
jpayne@69 3683 ' Execute the *statement* (given as a string or a code object) '
jpayne@69 3684 'under\n'
jpayne@69 3685 ' debugger control. The debugger prompt appears before any '
jpayne@69 3686 'code is\n'
jpayne@69 3687 ' executed; you can set breakpoints and type "continue", or you '
jpayne@69 3688 'can\n'
jpayne@69 3689 ' step through the statement using "step" or "next" (all these\n'
jpayne@69 3690 ' commands are explained below). The optional *globals* and '
jpayne@69 3691 '*locals*\n'
jpayne@69 3692 ' arguments specify the environment in which the code is '
jpayne@69 3693 'executed; by\n'
jpayne@69 3694 ' default the dictionary of the module "__main__" is used. '
jpayne@69 3695 '(See the\n'
jpayne@69 3696 ' explanation of the built-in "exec()" or "eval()" functions.)\n'
jpayne@69 3697 '\n'
jpayne@69 3698 'pdb.runeval(expression, globals=None, locals=None)\n'
jpayne@69 3699 '\n'
jpayne@69 3700 ' Evaluate the *expression* (given as a string or a code '
jpayne@69 3701 'object)\n'
jpayne@69 3702 ' under debugger control. When "runeval()" returns, it returns '
jpayne@69 3703 'the\n'
jpayne@69 3704 ' value of the expression. Otherwise this function is similar '
jpayne@69 3705 'to\n'
jpayne@69 3706 ' "run()".\n'
jpayne@69 3707 '\n'
jpayne@69 3708 'pdb.runcall(function, *args, **kwds)\n'
jpayne@69 3709 '\n'
jpayne@69 3710 ' Call the *function* (a function or method object, not a '
jpayne@69 3711 'string)\n'
jpayne@69 3712 ' with the given arguments. When "runcall()" returns, it '
jpayne@69 3713 'returns\n'
jpayne@69 3714 ' whatever the function call returned. The debugger prompt '
jpayne@69 3715 'appears\n'
jpayne@69 3716 ' as soon as the function is entered.\n'
jpayne@69 3717 '\n'
jpayne@69 3718 'pdb.set_trace(*, header=None)\n'
jpayne@69 3719 '\n'
jpayne@69 3720 ' Enter the debugger at the calling stack frame. This is '
jpayne@69 3721 'useful to\n'
jpayne@69 3722 ' hard-code a breakpoint at a given point in a program, even if '
jpayne@69 3723 'the\n'
jpayne@69 3724 ' code is not otherwise being debugged (e.g. when an assertion\n'
jpayne@69 3725 ' fails). If given, *header* is printed to the console just '
jpayne@69 3726 'before\n'
jpayne@69 3727 ' debugging begins.\n'
jpayne@69 3728 '\n'
jpayne@69 3729 ' Changed in version 3.7: The keyword-only argument *header*.\n'
jpayne@69 3730 '\n'
jpayne@69 3731 'pdb.post_mortem(traceback=None)\n'
jpayne@69 3732 '\n'
jpayne@69 3733 ' Enter post-mortem debugging of the given *traceback* object. '
jpayne@69 3734 'If no\n'
jpayne@69 3735 ' *traceback* is given, it uses the one of the exception that '
jpayne@69 3736 'is\n'
jpayne@69 3737 ' currently being handled (an exception must be being handled '
jpayne@69 3738 'if the\n'
jpayne@69 3739 ' default is to be used).\n'
jpayne@69 3740 '\n'
jpayne@69 3741 'pdb.pm()\n'
jpayne@69 3742 '\n'
jpayne@69 3743 ' Enter post-mortem debugging of the traceback found in\n'
jpayne@69 3744 ' "sys.last_traceback".\n'
jpayne@69 3745 '\n'
jpayne@69 3746 'The "run*" functions and "set_trace()" are aliases for '
jpayne@69 3747 'instantiating\n'
jpayne@69 3748 'the "Pdb" class and calling the method of the same name. If you '
jpayne@69 3749 'want\n'
jpayne@69 3750 'to access further features, you have to do this yourself:\n'
jpayne@69 3751 '\n'
jpayne@69 3752 "class pdb.Pdb(completekey='tab', stdin=None, stdout=None, "
jpayne@69 3753 'skip=None, nosigint=False, readrc=True)\n'
jpayne@69 3754 '\n'
jpayne@69 3755 ' "Pdb" is the debugger class.\n'
jpayne@69 3756 '\n'
jpayne@69 3757 ' The *completekey*, *stdin* and *stdout* arguments are passed '
jpayne@69 3758 'to the\n'
jpayne@69 3759 ' underlying "cmd.Cmd" class; see the description there.\n'
jpayne@69 3760 '\n'
jpayne@69 3761 ' The *skip* argument, if given, must be an iterable of '
jpayne@69 3762 'glob-style\n'
jpayne@69 3763 ' module name patterns. The debugger will not step into frames '
jpayne@69 3764 'that\n'
jpayne@69 3765 ' originate in a module that matches one of these patterns. '
jpayne@69 3766 '[1]\n'
jpayne@69 3767 '\n'
jpayne@69 3768 ' By default, Pdb sets a handler for the SIGINT signal (which '
jpayne@69 3769 'is sent\n'
jpayne@69 3770 ' when the user presses "Ctrl-C" on the console) when you give '
jpayne@69 3771 'a\n'
jpayne@69 3772 ' "continue" command. This allows you to break into the '
jpayne@69 3773 'debugger\n'
jpayne@69 3774 ' again by pressing "Ctrl-C". If you want Pdb not to touch '
jpayne@69 3775 'the\n'
jpayne@69 3776 ' SIGINT handler, set *nosigint* to true.\n'
jpayne@69 3777 '\n'
jpayne@69 3778 ' The *readrc* argument defaults to true and controls whether '
jpayne@69 3779 'Pdb\n'
jpayne@69 3780 ' will load .pdbrc files from the filesystem.\n'
jpayne@69 3781 '\n'
jpayne@69 3782 ' Example call to enable tracing with *skip*:\n'
jpayne@69 3783 '\n'
jpayne@69 3784 " import pdb; pdb.Pdb(skip=['django.*']).set_trace()\n"
jpayne@69 3785 '\n'
jpayne@69 3786 ' Raises an auditing event "pdb.Pdb" with no arguments.\n'
jpayne@69 3787 '\n'
jpayne@69 3788 ' New in version 3.1: The *skip* argument.\n'
jpayne@69 3789 '\n'
jpayne@69 3790 ' New in version 3.2: The *nosigint* argument. Previously, a '
jpayne@69 3791 'SIGINT\n'
jpayne@69 3792 ' handler was never set by Pdb.\n'
jpayne@69 3793 '\n'
jpayne@69 3794 ' Changed in version 3.6: The *readrc* argument.\n'
jpayne@69 3795 '\n'
jpayne@69 3796 ' run(statement, globals=None, locals=None)\n'
jpayne@69 3797 ' runeval(expression, globals=None, locals=None)\n'
jpayne@69 3798 ' runcall(function, *args, **kwds)\n'
jpayne@69 3799 ' set_trace()\n'
jpayne@69 3800 '\n'
jpayne@69 3801 ' See the documentation for the functions explained above.\n'
jpayne@69 3802 '\n'
jpayne@69 3803 '\n'
jpayne@69 3804 'Debugger Commands\n'
jpayne@69 3805 '=================\n'
jpayne@69 3806 '\n'
jpayne@69 3807 'The commands recognized by the debugger are listed below. Most\n'
jpayne@69 3808 'commands can be abbreviated to one or two letters as indicated; '
jpayne@69 3809 'e.g.\n'
jpayne@69 3810 '"h(elp)" means that either "h" or "help" can be used to enter '
jpayne@69 3811 'the help\n'
jpayne@69 3812 'command (but not "he" or "hel", nor "H" or "Help" or "HELP").\n'
jpayne@69 3813 'Arguments to commands must be separated by whitespace (spaces '
jpayne@69 3814 'or\n'
jpayne@69 3815 'tabs). Optional arguments are enclosed in square brackets '
jpayne@69 3816 '("[]") in\n'
jpayne@69 3817 'the command syntax; the square brackets must not be typed.\n'
jpayne@69 3818 'Alternatives in the command syntax are separated by a vertical '
jpayne@69 3819 'bar\n'
jpayne@69 3820 '("|").\n'
jpayne@69 3821 '\n'
jpayne@69 3822 'Entering a blank line repeats the last command entered. '
jpayne@69 3823 'Exception: if\n'
jpayne@69 3824 'the last command was a "list" command, the next 11 lines are '
jpayne@69 3825 'listed.\n'
jpayne@69 3826 '\n'
jpayne@69 3827 'Commands that the debugger doesn’t recognize are assumed to be '
jpayne@69 3828 'Python\n'
jpayne@69 3829 'statements and are executed in the context of the program being\n'
jpayne@69 3830 'debugged. Python statements can also be prefixed with an '
jpayne@69 3831 'exclamation\n'
jpayne@69 3832 'point ("!"). This is a powerful way to inspect the program '
jpayne@69 3833 'being\n'
jpayne@69 3834 'debugged; it is even possible to change a variable or call a '
jpayne@69 3835 'function.\n'
jpayne@69 3836 'When an exception occurs in such a statement, the exception name '
jpayne@69 3837 'is\n'
jpayne@69 3838 'printed but the debugger’s state is not changed.\n'
jpayne@69 3839 '\n'
jpayne@69 3840 'The debugger supports aliases. Aliases can have parameters '
jpayne@69 3841 'which\n'
jpayne@69 3842 'allows one a certain level of adaptability to the context under\n'
jpayne@69 3843 'examination.\n'
jpayne@69 3844 '\n'
jpayne@69 3845 'Multiple commands may be entered on a single line, separated by '
jpayne@69 3846 '";;".\n'
jpayne@69 3847 '(A single ";" is not used as it is the separator for multiple '
jpayne@69 3848 'commands\n'
jpayne@69 3849 'in a line that is passed to the Python parser.) No intelligence '
jpayne@69 3850 'is\n'
jpayne@69 3851 'applied to separating the commands; the input is split at the '
jpayne@69 3852 'first\n'
jpayne@69 3853 '";;" pair, even if it is in the middle of a quoted string.\n'
jpayne@69 3854 '\n'
jpayne@69 3855 'If a file ".pdbrc" exists in the user’s home directory or in '
jpayne@69 3856 'the\n'
jpayne@69 3857 'current directory, it is read in and executed as if it had been '
jpayne@69 3858 'typed\n'
jpayne@69 3859 'at the debugger prompt. This is particularly useful for '
jpayne@69 3860 'aliases. If\n'
jpayne@69 3861 'both files exist, the one in the home directory is read first '
jpayne@69 3862 'and\n'
jpayne@69 3863 'aliases defined there can be overridden by the local file.\n'
jpayne@69 3864 '\n'
jpayne@69 3865 'Changed in version 3.2: ".pdbrc" can now contain commands that\n'
jpayne@69 3866 'continue debugging, such as "continue" or "next". Previously, '
jpayne@69 3867 'these\n'
jpayne@69 3868 'commands had no effect.\n'
jpayne@69 3869 '\n'
jpayne@69 3870 'h(elp) [command]\n'
jpayne@69 3871 '\n'
jpayne@69 3872 ' Without argument, print the list of available commands. With '
jpayne@69 3873 'a\n'
jpayne@69 3874 ' *command* as argument, print help about that command. "help '
jpayne@69 3875 'pdb"\n'
jpayne@69 3876 ' displays the full documentation (the docstring of the "pdb"\n'
jpayne@69 3877 ' module). Since the *command* argument must be an identifier, '
jpayne@69 3878 '"help\n'
jpayne@69 3879 ' exec" must be entered to get help on the "!" command.\n'
jpayne@69 3880 '\n'
jpayne@69 3881 'w(here)\n'
jpayne@69 3882 '\n'
jpayne@69 3883 ' Print a stack trace, with the most recent frame at the '
jpayne@69 3884 'bottom. An\n'
jpayne@69 3885 ' arrow indicates the current frame, which determines the '
jpayne@69 3886 'context of\n'
jpayne@69 3887 ' most commands.\n'
jpayne@69 3888 '\n'
jpayne@69 3889 'd(own) [count]\n'
jpayne@69 3890 '\n'
jpayne@69 3891 ' Move the current frame *count* (default one) levels down in '
jpayne@69 3892 'the\n'
jpayne@69 3893 ' stack trace (to a newer frame).\n'
jpayne@69 3894 '\n'
jpayne@69 3895 'u(p) [count]\n'
jpayne@69 3896 '\n'
jpayne@69 3897 ' Move the current frame *count* (default one) levels up in the '
jpayne@69 3898 'stack\n'
jpayne@69 3899 ' trace (to an older frame).\n'
jpayne@69 3900 '\n'
jpayne@69 3901 'b(reak) [([filename:]lineno | function) [, condition]]\n'
jpayne@69 3902 '\n'
jpayne@69 3903 ' With a *lineno* argument, set a break there in the current '
jpayne@69 3904 'file.\n'
jpayne@69 3905 ' With a *function* argument, set a break at the first '
jpayne@69 3906 'executable\n'
jpayne@69 3907 ' statement within that function. The line number may be '
jpayne@69 3908 'prefixed\n'
jpayne@69 3909 ' with a filename and a colon, to specify a breakpoint in '
jpayne@69 3910 'another\n'
jpayne@69 3911 ' file (probably one that hasn’t been loaded yet). The file '
jpayne@69 3912 'is\n'
jpayne@69 3913 ' searched on "sys.path". Note that each breakpoint is '
jpayne@69 3914 'assigned a\n'
jpayne@69 3915 ' number to which all the other breakpoint commands refer.\n'
jpayne@69 3916 '\n'
jpayne@69 3917 ' If a second argument is present, it is an expression which '
jpayne@69 3918 'must\n'
jpayne@69 3919 ' evaluate to true before the breakpoint is honored.\n'
jpayne@69 3920 '\n'
jpayne@69 3921 ' Without argument, list all breaks, including for each '
jpayne@69 3922 'breakpoint,\n'
jpayne@69 3923 ' the number of times that breakpoint has been hit, the '
jpayne@69 3924 'current\n'
jpayne@69 3925 ' ignore count, and the associated condition if any.\n'
jpayne@69 3926 '\n'
jpayne@69 3927 'tbreak [([filename:]lineno | function) [, condition]]\n'
jpayne@69 3928 '\n'
jpayne@69 3929 ' Temporary breakpoint, which is removed automatically when it '
jpayne@69 3930 'is\n'
jpayne@69 3931 ' first hit. The arguments are the same as for "break".\n'
jpayne@69 3932 '\n'
jpayne@69 3933 'cl(ear) [filename:lineno | bpnumber [bpnumber ...]]\n'
jpayne@69 3934 '\n'
jpayne@69 3935 ' With a *filename:lineno* argument, clear all the breakpoints '
jpayne@69 3936 'at\n'
jpayne@69 3937 ' this line. With a space separated list of breakpoint numbers, '
jpayne@69 3938 'clear\n'
jpayne@69 3939 ' those breakpoints. Without argument, clear all breaks (but '
jpayne@69 3940 'first\n'
jpayne@69 3941 ' ask confirmation).\n'
jpayne@69 3942 '\n'
jpayne@69 3943 'disable [bpnumber [bpnumber ...]]\n'
jpayne@69 3944 '\n'
jpayne@69 3945 ' Disable the breakpoints given as a space separated list of\n'
jpayne@69 3946 ' breakpoint numbers. Disabling a breakpoint means it cannot '
jpayne@69 3947 'cause\n'
jpayne@69 3948 ' the program to stop execution, but unlike clearing a '
jpayne@69 3949 'breakpoint, it\n'
jpayne@69 3950 ' remains in the list of breakpoints and can be (re-)enabled.\n'
jpayne@69 3951 '\n'
jpayne@69 3952 'enable [bpnumber [bpnumber ...]]\n'
jpayne@69 3953 '\n'
jpayne@69 3954 ' Enable the breakpoints specified.\n'
jpayne@69 3955 '\n'
jpayne@69 3956 'ignore bpnumber [count]\n'
jpayne@69 3957 '\n'
jpayne@69 3958 ' Set the ignore count for the given breakpoint number. If '
jpayne@69 3959 'count is\n'
jpayne@69 3960 ' omitted, the ignore count is set to 0. A breakpoint becomes '
jpayne@69 3961 'active\n'
jpayne@69 3962 ' when the ignore count is zero. When non-zero, the count is\n'
jpayne@69 3963 ' decremented each time the breakpoint is reached and the '
jpayne@69 3964 'breakpoint\n'
jpayne@69 3965 ' is not disabled and any associated condition evaluates to '
jpayne@69 3966 'true.\n'
jpayne@69 3967 '\n'
jpayne@69 3968 'condition bpnumber [condition]\n'
jpayne@69 3969 '\n'
jpayne@69 3970 ' Set a new *condition* for the breakpoint, an expression which '
jpayne@69 3971 'must\n'
jpayne@69 3972 ' evaluate to true before the breakpoint is honored. If '
jpayne@69 3973 '*condition*\n'
jpayne@69 3974 ' is absent, any existing condition is removed; i.e., the '
jpayne@69 3975 'breakpoint\n'
jpayne@69 3976 ' is made unconditional.\n'
jpayne@69 3977 '\n'
jpayne@69 3978 'commands [bpnumber]\n'
jpayne@69 3979 '\n'
jpayne@69 3980 ' Specify a list of commands for breakpoint number *bpnumber*. '
jpayne@69 3981 'The\n'
jpayne@69 3982 ' commands themselves appear on the following lines. Type a '
jpayne@69 3983 'line\n'
jpayne@69 3984 ' containing just "end" to terminate the commands. An example:\n'
jpayne@69 3985 '\n'
jpayne@69 3986 ' (Pdb) commands 1\n'
jpayne@69 3987 ' (com) p some_variable\n'
jpayne@69 3988 ' (com) end\n'
jpayne@69 3989 ' (Pdb)\n'
jpayne@69 3990 '\n'
jpayne@69 3991 ' To remove all commands from a breakpoint, type "commands" '
jpayne@69 3992 'and\n'
jpayne@69 3993 ' follow it immediately with "end"; that is, give no commands.\n'
jpayne@69 3994 '\n'
jpayne@69 3995 ' With no *bpnumber* argument, "commands" refers to the last\n'
jpayne@69 3996 ' breakpoint set.\n'
jpayne@69 3997 '\n'
jpayne@69 3998 ' You can use breakpoint commands to start your program up '
jpayne@69 3999 'again.\n'
jpayne@69 4000 ' Simply use the "continue" command, or "step", or any other '
jpayne@69 4001 'command\n'
jpayne@69 4002 ' that resumes execution.\n'
jpayne@69 4003 '\n'
jpayne@69 4004 ' Specifying any command resuming execution (currently '
jpayne@69 4005 '"continue",\n'
jpayne@69 4006 ' "step", "next", "return", "jump", "quit" and their '
jpayne@69 4007 'abbreviations)\n'
jpayne@69 4008 ' terminates the command list (as if that command was '
jpayne@69 4009 'immediately\n'
jpayne@69 4010 ' followed by end). This is because any time you resume '
jpayne@69 4011 'execution\n'
jpayne@69 4012 ' (even with a simple next or step), you may encounter another\n'
jpayne@69 4013 ' breakpoint—which could have its own command list, leading to\n'
jpayne@69 4014 ' ambiguities about which list to execute.\n'
jpayne@69 4015 '\n'
jpayne@69 4016 ' If you use the ‘silent’ command in the command list, the '
jpayne@69 4017 'usual\n'
jpayne@69 4018 ' message about stopping at a breakpoint is not printed. This '
jpayne@69 4019 'may be\n'
jpayne@69 4020 ' desirable for breakpoints that are to print a specific '
jpayne@69 4021 'message and\n'
jpayne@69 4022 ' then continue. If none of the other commands print anything, '
jpayne@69 4023 'you\n'
jpayne@69 4024 ' see no sign that the breakpoint was reached.\n'
jpayne@69 4025 '\n'
jpayne@69 4026 's(tep)\n'
jpayne@69 4027 '\n'
jpayne@69 4028 ' Execute the current line, stop at the first possible '
jpayne@69 4029 'occasion\n'
jpayne@69 4030 ' (either in a function that is called or on the next line in '
jpayne@69 4031 'the\n'
jpayne@69 4032 ' current function).\n'
jpayne@69 4033 '\n'
jpayne@69 4034 'n(ext)\n'
jpayne@69 4035 '\n'
jpayne@69 4036 ' Continue execution until the next line in the current '
jpayne@69 4037 'function is\n'
jpayne@69 4038 ' reached or it returns. (The difference between "next" and '
jpayne@69 4039 '"step"\n'
jpayne@69 4040 ' is that "step" stops inside a called function, while "next"\n'
jpayne@69 4041 ' executes called functions at (nearly) full speed, only '
jpayne@69 4042 'stopping at\n'
jpayne@69 4043 ' the next line in the current function.)\n'
jpayne@69 4044 '\n'
jpayne@69 4045 'unt(il) [lineno]\n'
jpayne@69 4046 '\n'
jpayne@69 4047 ' Without argument, continue execution until the line with a '
jpayne@69 4048 'number\n'
jpayne@69 4049 ' greater than the current one is reached.\n'
jpayne@69 4050 '\n'
jpayne@69 4051 ' With a line number, continue execution until a line with a '
jpayne@69 4052 'number\n'
jpayne@69 4053 ' greater or equal to that is reached. In both cases, also '
jpayne@69 4054 'stop when\n'
jpayne@69 4055 ' the current frame returns.\n'
jpayne@69 4056 '\n'
jpayne@69 4057 ' Changed in version 3.2: Allow giving an explicit line '
jpayne@69 4058 'number.\n'
jpayne@69 4059 '\n'
jpayne@69 4060 'r(eturn)\n'
jpayne@69 4061 '\n'
jpayne@69 4062 ' Continue execution until the current function returns.\n'
jpayne@69 4063 '\n'
jpayne@69 4064 'c(ont(inue))\n'
jpayne@69 4065 '\n'
jpayne@69 4066 ' Continue execution, only stop when a breakpoint is '
jpayne@69 4067 'encountered.\n'
jpayne@69 4068 '\n'
jpayne@69 4069 'j(ump) lineno\n'
jpayne@69 4070 '\n'
jpayne@69 4071 ' Set the next line that will be executed. Only available in '
jpayne@69 4072 'the\n'
jpayne@69 4073 ' bottom-most frame. This lets you jump back and execute code '
jpayne@69 4074 'again,\n'
jpayne@69 4075 ' or jump forward to skip code that you don’t want to run.\n'
jpayne@69 4076 '\n'
jpayne@69 4077 ' It should be noted that not all jumps are allowed – for '
jpayne@69 4078 'instance it\n'
jpayne@69 4079 ' is not possible to jump into the middle of a "for" loop or '
jpayne@69 4080 'out of a\n'
jpayne@69 4081 ' "finally" clause.\n'
jpayne@69 4082 '\n'
jpayne@69 4083 'l(ist) [first[, last]]\n'
jpayne@69 4084 '\n'
jpayne@69 4085 ' List source code for the current file. Without arguments, '
jpayne@69 4086 'list 11\n'
jpayne@69 4087 ' lines around the current line or continue the previous '
jpayne@69 4088 'listing.\n'
jpayne@69 4089 ' With "." as argument, list 11 lines around the current line. '
jpayne@69 4090 'With\n'
jpayne@69 4091 ' one argument, list 11 lines around at that line. With two\n'
jpayne@69 4092 ' arguments, list the given range; if the second argument is '
jpayne@69 4093 'less\n'
jpayne@69 4094 ' than the first, it is interpreted as a count.\n'
jpayne@69 4095 '\n'
jpayne@69 4096 ' The current line in the current frame is indicated by "->". '
jpayne@69 4097 'If an\n'
jpayne@69 4098 ' exception is being debugged, the line where the exception '
jpayne@69 4099 'was\n'
jpayne@69 4100 ' originally raised or propagated is indicated by ">>", if it '
jpayne@69 4101 'differs\n'
jpayne@69 4102 ' from the current line.\n'
jpayne@69 4103 '\n'
jpayne@69 4104 ' New in version 3.2: The ">>" marker.\n'
jpayne@69 4105 '\n'
jpayne@69 4106 'll | longlist\n'
jpayne@69 4107 '\n'
jpayne@69 4108 ' List all source code for the current function or frame.\n'
jpayne@69 4109 ' Interesting lines are marked as for "list".\n'
jpayne@69 4110 '\n'
jpayne@69 4111 ' New in version 3.2.\n'
jpayne@69 4112 '\n'
jpayne@69 4113 'a(rgs)\n'
jpayne@69 4114 '\n'
jpayne@69 4115 ' Print the argument list of the current function.\n'
jpayne@69 4116 '\n'
jpayne@69 4117 'p expression\n'
jpayne@69 4118 '\n'
jpayne@69 4119 ' Evaluate the *expression* in the current context and print '
jpayne@69 4120 'its\n'
jpayne@69 4121 ' value.\n'
jpayne@69 4122 '\n'
jpayne@69 4123 ' Note: "print()" can also be used, but is not a debugger '
jpayne@69 4124 'command —\n'
jpayne@69 4125 ' this executes the Python "print()" function.\n'
jpayne@69 4126 '\n'
jpayne@69 4127 'pp expression\n'
jpayne@69 4128 '\n'
jpayne@69 4129 ' Like the "p" command, except the value of the expression is '
jpayne@69 4130 'pretty-\n'
jpayne@69 4131 ' printed using the "pprint" module.\n'
jpayne@69 4132 '\n'
jpayne@69 4133 'whatis expression\n'
jpayne@69 4134 '\n'
jpayne@69 4135 ' Print the type of the *expression*.\n'
jpayne@69 4136 '\n'
jpayne@69 4137 'source expression\n'
jpayne@69 4138 '\n'
jpayne@69 4139 ' Try to get source code for the given object and display it.\n'
jpayne@69 4140 '\n'
jpayne@69 4141 ' New in version 3.2.\n'
jpayne@69 4142 '\n'
jpayne@69 4143 'display [expression]\n'
jpayne@69 4144 '\n'
jpayne@69 4145 ' Display the value of the expression if it changed, each time\n'
jpayne@69 4146 ' execution stops in the current frame.\n'
jpayne@69 4147 '\n'
jpayne@69 4148 ' Without expression, list all display expressions for the '
jpayne@69 4149 'current\n'
jpayne@69 4150 ' frame.\n'
jpayne@69 4151 '\n'
jpayne@69 4152 ' New in version 3.2.\n'
jpayne@69 4153 '\n'
jpayne@69 4154 'undisplay [expression]\n'
jpayne@69 4155 '\n'
jpayne@69 4156 ' Do not display the expression any more in the current frame.\n'
jpayne@69 4157 ' Without expression, clear all display expressions for the '
jpayne@69 4158 'current\n'
jpayne@69 4159 ' frame.\n'
jpayne@69 4160 '\n'
jpayne@69 4161 ' New in version 3.2.\n'
jpayne@69 4162 '\n'
jpayne@69 4163 'interact\n'
jpayne@69 4164 '\n'
jpayne@69 4165 ' Start an interactive interpreter (using the "code" module) '
jpayne@69 4166 'whose\n'
jpayne@69 4167 ' global namespace contains all the (global and local) names '
jpayne@69 4168 'found in\n'
jpayne@69 4169 ' the current scope.\n'
jpayne@69 4170 '\n'
jpayne@69 4171 ' New in version 3.2.\n'
jpayne@69 4172 '\n'
jpayne@69 4173 'alias [name [command]]\n'
jpayne@69 4174 '\n'
jpayne@69 4175 ' Create an alias called *name* that executes *command*. The '
jpayne@69 4176 'command\n'
jpayne@69 4177 ' must *not* be enclosed in quotes. Replaceable parameters can '
jpayne@69 4178 'be\n'
jpayne@69 4179 ' indicated by "%1", "%2", and so on, while "%*" is replaced by '
jpayne@69 4180 'all\n'
jpayne@69 4181 ' the parameters. If no command is given, the current alias '
jpayne@69 4182 'for\n'
jpayne@69 4183 ' *name* is shown. If no arguments are given, all aliases are '
jpayne@69 4184 'listed.\n'
jpayne@69 4185 '\n'
jpayne@69 4186 ' Aliases may be nested and can contain anything that can be '
jpayne@69 4187 'legally\n'
jpayne@69 4188 ' typed at the pdb prompt. Note that internal pdb commands '
jpayne@69 4189 '*can* be\n'
jpayne@69 4190 ' overridden by aliases. Such a command is then hidden until '
jpayne@69 4191 'the\n'
jpayne@69 4192 ' alias is removed. Aliasing is recursively applied to the '
jpayne@69 4193 'first\n'
jpayne@69 4194 ' word of the command line; all other words in the line are '
jpayne@69 4195 'left\n'
jpayne@69 4196 ' alone.\n'
jpayne@69 4197 '\n'
jpayne@69 4198 ' As an example, here are two useful aliases (especially when '
jpayne@69 4199 'placed\n'
jpayne@69 4200 ' in the ".pdbrc" file):\n'
jpayne@69 4201 '\n'
jpayne@69 4202 ' # Print instance variables (usage "pi classInst")\n'
jpayne@69 4203 ' alias pi for k in %1.__dict__.keys(): '
jpayne@69 4204 'print("%1.",k,"=",%1.__dict__[k])\n'
jpayne@69 4205 ' # Print instance variables in self\n'
jpayne@69 4206 ' alias ps pi self\n'
jpayne@69 4207 '\n'
jpayne@69 4208 'unalias name\n'
jpayne@69 4209 '\n'
jpayne@69 4210 ' Delete the specified alias.\n'
jpayne@69 4211 '\n'
jpayne@69 4212 '! statement\n'
jpayne@69 4213 '\n'
jpayne@69 4214 ' Execute the (one-line) *statement* in the context of the '
jpayne@69 4215 'current\n'
jpayne@69 4216 ' stack frame. The exclamation point can be omitted unless the '
jpayne@69 4217 'first\n'
jpayne@69 4218 ' word of the statement resembles a debugger command. To set '
jpayne@69 4219 'a\n'
jpayne@69 4220 ' global variable, you can prefix the assignment command with '
jpayne@69 4221 'a\n'
jpayne@69 4222 ' "global" statement on the same line, e.g.:\n'
jpayne@69 4223 '\n'
jpayne@69 4224 " (Pdb) global list_options; list_options = ['-l']\n"
jpayne@69 4225 ' (Pdb)\n'
jpayne@69 4226 '\n'
jpayne@69 4227 'run [args ...]\n'
jpayne@69 4228 'restart [args ...]\n'
jpayne@69 4229 '\n'
jpayne@69 4230 ' Restart the debugged Python program. If an argument is '
jpayne@69 4231 'supplied,\n'
jpayne@69 4232 ' it is split with "shlex" and the result is used as the new\n'
jpayne@69 4233 ' "sys.argv". History, breakpoints, actions and debugger '
jpayne@69 4234 'options are\n'
jpayne@69 4235 ' preserved. "restart" is an alias for "run".\n'
jpayne@69 4236 '\n'
jpayne@69 4237 'q(uit)\n'
jpayne@69 4238 '\n'
jpayne@69 4239 ' Quit from the debugger. The program being executed is '
jpayne@69 4240 'aborted.\n'
jpayne@69 4241 '\n'
jpayne@69 4242 'debug code\n'
jpayne@69 4243 '\n'
jpayne@69 4244 ' Enter a recursive debugger that steps through the code '
jpayne@69 4245 'argument\n'
jpayne@69 4246 ' (which is an arbitrary expression or statement to be executed '
jpayne@69 4247 'in\n'
jpayne@69 4248 ' the current environment).\n'
jpayne@69 4249 '\n'
jpayne@69 4250 'retval\n'
jpayne@69 4251 'Print the return value for the last return of a function.\n'
jpayne@69 4252 '\n'
jpayne@69 4253 '-[ Footnotes ]-\n'
jpayne@69 4254 '\n'
jpayne@69 4255 '[1] Whether a frame is considered to originate in a certain '
jpayne@69 4256 'module\n'
jpayne@69 4257 ' is determined by the "__name__" in the frame globals.\n',
jpayne@69 4258 'del': 'The "del" statement\n'
jpayne@69 4259 '*******************\n'
jpayne@69 4260 '\n'
jpayne@69 4261 ' del_stmt ::= "del" target_list\n'
jpayne@69 4262 '\n'
jpayne@69 4263 'Deletion is recursively defined very similar to the way assignment '
jpayne@69 4264 'is\n'
jpayne@69 4265 'defined. Rather than spelling it out in full details, here are some\n'
jpayne@69 4266 'hints.\n'
jpayne@69 4267 '\n'
jpayne@69 4268 'Deletion of a target list recursively deletes each target, from left\n'
jpayne@69 4269 'to right.\n'
jpayne@69 4270 '\n'
jpayne@69 4271 'Deletion of a name removes the binding of that name from the local '
jpayne@69 4272 'or\n'
jpayne@69 4273 'global namespace, depending on whether the name occurs in a "global"\n'
jpayne@69 4274 'statement in the same code block. If the name is unbound, a\n'
jpayne@69 4275 '"NameError" exception will be raised.\n'
jpayne@69 4276 '\n'
jpayne@69 4277 'Deletion of attribute references, subscriptions and slicings is '
jpayne@69 4278 'passed\n'
jpayne@69 4279 'to the primary object involved; deletion of a slicing is in general\n'
jpayne@69 4280 'equivalent to assignment of an empty slice of the right type (but '
jpayne@69 4281 'even\n'
jpayne@69 4282 'this is determined by the sliced object).\n'
jpayne@69 4283 '\n'
jpayne@69 4284 'Changed in version 3.2: Previously it was illegal to delete a name\n'
jpayne@69 4285 'from the local namespace if it occurs as a free variable in a nested\n'
jpayne@69 4286 'block.\n',
jpayne@69 4287 'dict': 'Dictionary displays\n'
jpayne@69 4288 '*******************\n'
jpayne@69 4289 '\n'
jpayne@69 4290 'A dictionary display is a possibly empty series of key/datum pairs\n'
jpayne@69 4291 'enclosed in curly braces:\n'
jpayne@69 4292 '\n'
jpayne@69 4293 ' dict_display ::= "{" [key_datum_list | dict_comprehension] '
jpayne@69 4294 '"}"\n'
jpayne@69 4295 ' key_datum_list ::= key_datum ("," key_datum)* [","]\n'
jpayne@69 4296 ' key_datum ::= expression ":" expression | "**" or_expr\n'
jpayne@69 4297 ' dict_comprehension ::= expression ":" expression comp_for\n'
jpayne@69 4298 '\n'
jpayne@69 4299 'A dictionary display yields a new dictionary object.\n'
jpayne@69 4300 '\n'
jpayne@69 4301 'If a comma-separated sequence of key/datum pairs is given, they are\n'
jpayne@69 4302 'evaluated from left to right to define the entries of the '
jpayne@69 4303 'dictionary:\n'
jpayne@69 4304 'each key object is used as a key into the dictionary to store the\n'
jpayne@69 4305 'corresponding datum. This means that you can specify the same key\n'
jpayne@69 4306 'multiple times in the key/datum list, and the final dictionary’s '
jpayne@69 4307 'value\n'
jpayne@69 4308 'for that key will be the last one given.\n'
jpayne@69 4309 '\n'
jpayne@69 4310 'A double asterisk "**" denotes *dictionary unpacking*. Its operand\n'
jpayne@69 4311 'must be a *mapping*. Each mapping item is added to the new\n'
jpayne@69 4312 'dictionary. Later values replace values already set by earlier\n'
jpayne@69 4313 'key/datum pairs and earlier dictionary unpackings.\n'
jpayne@69 4314 '\n'
jpayne@69 4315 'New in version 3.5: Unpacking into dictionary displays, originally\n'
jpayne@69 4316 'proposed by **PEP 448**.\n'
jpayne@69 4317 '\n'
jpayne@69 4318 'A dict comprehension, in contrast to list and set comprehensions,\n'
jpayne@69 4319 'needs two expressions separated with a colon followed by the usual\n'
jpayne@69 4320 '“for” and “if” clauses. When the comprehension is run, the '
jpayne@69 4321 'resulting\n'
jpayne@69 4322 'key and value elements are inserted in the new dictionary in the '
jpayne@69 4323 'order\n'
jpayne@69 4324 'they are produced.\n'
jpayne@69 4325 '\n'
jpayne@69 4326 'Restrictions on the types of the key values are listed earlier in\n'
jpayne@69 4327 'section The standard type hierarchy. (To summarize, the key type\n'
jpayne@69 4328 'should be *hashable*, which excludes all mutable objects.) Clashes\n'
jpayne@69 4329 'between duplicate keys are not detected; the last datum (textually\n'
jpayne@69 4330 'rightmost in the display) stored for a given key value prevails.\n'
jpayne@69 4331 '\n'
jpayne@69 4332 'Changed in version 3.8: Prior to Python 3.8, in dict '
jpayne@69 4333 'comprehensions,\n'
jpayne@69 4334 'the evaluation order of key and value was not well-defined. In\n'
jpayne@69 4335 'CPython, the value was evaluated before the key. Starting with '
jpayne@69 4336 '3.8,\n'
jpayne@69 4337 'the key is evaluated before the value, as proposed by **PEP 572**.\n',
jpayne@69 4338 'dynamic-features': 'Interaction with dynamic features\n'
jpayne@69 4339 '*********************************\n'
jpayne@69 4340 '\n'
jpayne@69 4341 'Name resolution of free variables occurs at runtime, not '
jpayne@69 4342 'at compile\n'
jpayne@69 4343 'time. This means that the following code will print 42:\n'
jpayne@69 4344 '\n'
jpayne@69 4345 ' i = 10\n'
jpayne@69 4346 ' def f():\n'
jpayne@69 4347 ' print(i)\n'
jpayne@69 4348 ' i = 42\n'
jpayne@69 4349 ' f()\n'
jpayne@69 4350 '\n'
jpayne@69 4351 'The "eval()" and "exec()" functions do not have access '
jpayne@69 4352 'to the full\n'
jpayne@69 4353 'environment for resolving names. Names may be resolved '
jpayne@69 4354 'in the local\n'
jpayne@69 4355 'and global namespaces of the caller. Free variables are '
jpayne@69 4356 'not resolved\n'
jpayne@69 4357 'in the nearest enclosing namespace, but in the global '
jpayne@69 4358 'namespace. [1]\n'
jpayne@69 4359 'The "exec()" and "eval()" functions have optional '
jpayne@69 4360 'arguments to\n'
jpayne@69 4361 'override the global and local namespace. If only one '
jpayne@69 4362 'namespace is\n'
jpayne@69 4363 'specified, it is used for both.\n',
jpayne@69 4364 'else': 'The "if" statement\n'
jpayne@69 4365 '******************\n'
jpayne@69 4366 '\n'
jpayne@69 4367 'The "if" statement is used for conditional execution:\n'
jpayne@69 4368 '\n'
jpayne@69 4369 ' if_stmt ::= "if" expression ":" suite\n'
jpayne@69 4370 ' ("elif" expression ":" suite)*\n'
jpayne@69 4371 ' ["else" ":" suite]\n'
jpayne@69 4372 '\n'
jpayne@69 4373 'It selects exactly one of the suites by evaluating the expressions '
jpayne@69 4374 'one\n'
jpayne@69 4375 'by one until one is found to be true (see section Boolean '
jpayne@69 4376 'operations\n'
jpayne@69 4377 'for the definition of true and false); then that suite is executed\n'
jpayne@69 4378 '(and no other part of the "if" statement is executed or evaluated).\n'
jpayne@69 4379 'If all expressions are false, the suite of the "else" clause, if\n'
jpayne@69 4380 'present, is executed.\n',
jpayne@69 4381 'exceptions': 'Exceptions\n'
jpayne@69 4382 '**********\n'
jpayne@69 4383 '\n'
jpayne@69 4384 'Exceptions are a means of breaking out of the normal flow of '
jpayne@69 4385 'control\n'
jpayne@69 4386 'of a code block in order to handle errors or other '
jpayne@69 4387 'exceptional\n'
jpayne@69 4388 'conditions. An exception is *raised* at the point where the '
jpayne@69 4389 'error is\n'
jpayne@69 4390 'detected; it may be *handled* by the surrounding code block or '
jpayne@69 4391 'by any\n'
jpayne@69 4392 'code block that directly or indirectly invoked the code block '
jpayne@69 4393 'where\n'
jpayne@69 4394 'the error occurred.\n'
jpayne@69 4395 '\n'
jpayne@69 4396 'The Python interpreter raises an exception when it detects a '
jpayne@69 4397 'run-time\n'
jpayne@69 4398 'error (such as division by zero). A Python program can also\n'
jpayne@69 4399 'explicitly raise an exception with the "raise" statement. '
jpayne@69 4400 'Exception\n'
jpayne@69 4401 'handlers are specified with the "try" … "except" statement. '
jpayne@69 4402 'The\n'
jpayne@69 4403 '"finally" clause of such a statement can be used to specify '
jpayne@69 4404 'cleanup\n'
jpayne@69 4405 'code which does not handle the exception, but is executed '
jpayne@69 4406 'whether an\n'
jpayne@69 4407 'exception occurred or not in the preceding code.\n'
jpayne@69 4408 '\n'
jpayne@69 4409 'Python uses the “termination” model of error handling: an '
jpayne@69 4410 'exception\n'
jpayne@69 4411 'handler can find out what happened and continue execution at '
jpayne@69 4412 'an outer\n'
jpayne@69 4413 'level, but it cannot repair the cause of the error and retry '
jpayne@69 4414 'the\n'
jpayne@69 4415 'failing operation (except by re-entering the offending piece '
jpayne@69 4416 'of code\n'
jpayne@69 4417 'from the top).\n'
jpayne@69 4418 '\n'
jpayne@69 4419 'When an exception is not handled at all, the interpreter '
jpayne@69 4420 'terminates\n'
jpayne@69 4421 'execution of the program, or returns to its interactive main '
jpayne@69 4422 'loop. In\n'
jpayne@69 4423 'either case, it prints a stack traceback, except when the '
jpayne@69 4424 'exception is\n'
jpayne@69 4425 '"SystemExit".\n'
jpayne@69 4426 '\n'
jpayne@69 4427 'Exceptions are identified by class instances. The "except" '
jpayne@69 4428 'clause is\n'
jpayne@69 4429 'selected depending on the class of the instance: it must '
jpayne@69 4430 'reference the\n'
jpayne@69 4431 'class of the instance or a base class thereof. The instance '
jpayne@69 4432 'can be\n'
jpayne@69 4433 'received by the handler and can carry additional information '
jpayne@69 4434 'about the\n'
jpayne@69 4435 'exceptional condition.\n'
jpayne@69 4436 '\n'
jpayne@69 4437 'Note: Exception messages are not part of the Python API. '
jpayne@69 4438 'Their\n'
jpayne@69 4439 ' contents may change from one version of Python to the next '
jpayne@69 4440 'without\n'
jpayne@69 4441 ' warning and should not be relied on by code which will run '
jpayne@69 4442 'under\n'
jpayne@69 4443 ' multiple versions of the interpreter.\n'
jpayne@69 4444 '\n'
jpayne@69 4445 'See also the description of the "try" statement in section The '
jpayne@69 4446 'try\n'
jpayne@69 4447 'statement and "raise" statement in section The raise '
jpayne@69 4448 'statement.\n'
jpayne@69 4449 '\n'
jpayne@69 4450 '-[ Footnotes ]-\n'
jpayne@69 4451 '\n'
jpayne@69 4452 '[1] This limitation occurs because the code that is executed '
jpayne@69 4453 'by\n'
jpayne@69 4454 ' these operations is not available at the time the module '
jpayne@69 4455 'is\n'
jpayne@69 4456 ' compiled.\n',
jpayne@69 4457 'execmodel': 'Execution model\n'
jpayne@69 4458 '***************\n'
jpayne@69 4459 '\n'
jpayne@69 4460 '\n'
jpayne@69 4461 'Structure of a program\n'
jpayne@69 4462 '======================\n'
jpayne@69 4463 '\n'
jpayne@69 4464 'A Python program is constructed from code blocks. A *block* is '
jpayne@69 4465 'a piece\n'
jpayne@69 4466 'of Python program text that is executed as a unit. The '
jpayne@69 4467 'following are\n'
jpayne@69 4468 'blocks: a module, a function body, and a class definition. '
jpayne@69 4469 'Each\n'
jpayne@69 4470 'command typed interactively is a block. A script file (a file '
jpayne@69 4471 'given\n'
jpayne@69 4472 'as standard input to the interpreter or specified as a command '
jpayne@69 4473 'line\n'
jpayne@69 4474 'argument to the interpreter) is a code block. A script command '
jpayne@69 4475 '(a\n'
jpayne@69 4476 'command specified on the interpreter command line with the '
jpayne@69 4477 '"-c"\n'
jpayne@69 4478 'option) is a code block. The string argument passed to the '
jpayne@69 4479 'built-in\n'
jpayne@69 4480 'functions "eval()" and "exec()" is a code block.\n'
jpayne@69 4481 '\n'
jpayne@69 4482 'A code block is executed in an *execution frame*. A frame '
jpayne@69 4483 'contains\n'
jpayne@69 4484 'some administrative information (used for debugging) and '
jpayne@69 4485 'determines\n'
jpayne@69 4486 'where and how execution continues after the code block’s '
jpayne@69 4487 'execution has\n'
jpayne@69 4488 'completed.\n'
jpayne@69 4489 '\n'
jpayne@69 4490 '\n'
jpayne@69 4491 'Naming and binding\n'
jpayne@69 4492 '==================\n'
jpayne@69 4493 '\n'
jpayne@69 4494 '\n'
jpayne@69 4495 'Binding of names\n'
jpayne@69 4496 '----------------\n'
jpayne@69 4497 '\n'
jpayne@69 4498 '*Names* refer to objects. Names are introduced by name '
jpayne@69 4499 'binding\n'
jpayne@69 4500 'operations.\n'
jpayne@69 4501 '\n'
jpayne@69 4502 'The following constructs bind names: formal parameters to '
jpayne@69 4503 'functions,\n'
jpayne@69 4504 '"import" statements, class and function definitions (these bind '
jpayne@69 4505 'the\n'
jpayne@69 4506 'class or function name in the defining block), and targets that '
jpayne@69 4507 'are\n'
jpayne@69 4508 'identifiers if occurring in an assignment, "for" loop header, '
jpayne@69 4509 'or after\n'
jpayne@69 4510 '"as" in a "with" statement or "except" clause. The "import" '
jpayne@69 4511 'statement\n'
jpayne@69 4512 'of the form "from ... import *" binds all names defined in the\n'
jpayne@69 4513 'imported module, except those beginning with an underscore. '
jpayne@69 4514 'This form\n'
jpayne@69 4515 'may only be used at the module level.\n'
jpayne@69 4516 '\n'
jpayne@69 4517 'A target occurring in a "del" statement is also considered '
jpayne@69 4518 'bound for\n'
jpayne@69 4519 'this purpose (though the actual semantics are to unbind the '
jpayne@69 4520 'name).\n'
jpayne@69 4521 '\n'
jpayne@69 4522 'Each assignment or import statement occurs within a block '
jpayne@69 4523 'defined by a\n'
jpayne@69 4524 'class or function definition or at the module level (the '
jpayne@69 4525 'top-level\n'
jpayne@69 4526 'code block).\n'
jpayne@69 4527 '\n'
jpayne@69 4528 'If a name is bound in a block, it is a local variable of that '
jpayne@69 4529 'block,\n'
jpayne@69 4530 'unless declared as "nonlocal" or "global". If a name is bound '
jpayne@69 4531 'at the\n'
jpayne@69 4532 'module level, it is a global variable. (The variables of the '
jpayne@69 4533 'module\n'
jpayne@69 4534 'code block are local and global.) If a variable is used in a '
jpayne@69 4535 'code\n'
jpayne@69 4536 'block but not defined there, it is a *free variable*.\n'
jpayne@69 4537 '\n'
jpayne@69 4538 'Each occurrence of a name in the program text refers to the '
jpayne@69 4539 '*binding*\n'
jpayne@69 4540 'of that name established by the following name resolution '
jpayne@69 4541 'rules.\n'
jpayne@69 4542 '\n'
jpayne@69 4543 '\n'
jpayne@69 4544 'Resolution of names\n'
jpayne@69 4545 '-------------------\n'
jpayne@69 4546 '\n'
jpayne@69 4547 'A *scope* defines the visibility of a name within a block. If '
jpayne@69 4548 'a local\n'
jpayne@69 4549 'variable is defined in a block, its scope includes that block. '
jpayne@69 4550 'If the\n'
jpayne@69 4551 'definition occurs in a function block, the scope extends to any '
jpayne@69 4552 'blocks\n'
jpayne@69 4553 'contained within the defining one, unless a contained block '
jpayne@69 4554 'introduces\n'
jpayne@69 4555 'a different binding for the name.\n'
jpayne@69 4556 '\n'
jpayne@69 4557 'When a name is used in a code block, it is resolved using the '
jpayne@69 4558 'nearest\n'
jpayne@69 4559 'enclosing scope. The set of all such scopes visible to a code '
jpayne@69 4560 'block\n'
jpayne@69 4561 'is called the block’s *environment*.\n'
jpayne@69 4562 '\n'
jpayne@69 4563 'When a name is not found at all, a "NameError" exception is '
jpayne@69 4564 'raised. If\n'
jpayne@69 4565 'the current scope is a function scope, and the name refers to a '
jpayne@69 4566 'local\n'
jpayne@69 4567 'variable that has not yet been bound to a value at the point '
jpayne@69 4568 'where the\n'
jpayne@69 4569 'name is used, an "UnboundLocalError" exception is raised.\n'
jpayne@69 4570 '"UnboundLocalError" is a subclass of "NameError".\n'
jpayne@69 4571 '\n'
jpayne@69 4572 'If a name binding operation occurs anywhere within a code '
jpayne@69 4573 'block, all\n'
jpayne@69 4574 'uses of the name within the block are treated as references to '
jpayne@69 4575 'the\n'
jpayne@69 4576 'current block. This can lead to errors when a name is used '
jpayne@69 4577 'within a\n'
jpayne@69 4578 'block before it is bound. This rule is subtle. Python lacks\n'
jpayne@69 4579 'declarations and allows name binding operations to occur '
jpayne@69 4580 'anywhere\n'
jpayne@69 4581 'within a code block. The local variables of a code block can '
jpayne@69 4582 'be\n'
jpayne@69 4583 'determined by scanning the entire text of the block for name '
jpayne@69 4584 'binding\n'
jpayne@69 4585 'operations.\n'
jpayne@69 4586 '\n'
jpayne@69 4587 'If the "global" statement occurs within a block, all uses of '
jpayne@69 4588 'the name\n'
jpayne@69 4589 'specified in the statement refer to the binding of that name in '
jpayne@69 4590 'the\n'
jpayne@69 4591 'top-level namespace. Names are resolved in the top-level '
jpayne@69 4592 'namespace by\n'
jpayne@69 4593 'searching the global namespace, i.e. the namespace of the '
jpayne@69 4594 'module\n'
jpayne@69 4595 'containing the code block, and the builtins namespace, the '
jpayne@69 4596 'namespace\n'
jpayne@69 4597 'of the module "builtins". The global namespace is searched '
jpayne@69 4598 'first. If\n'
jpayne@69 4599 'the name is not found there, the builtins namespace is '
jpayne@69 4600 'searched. The\n'
jpayne@69 4601 '"global" statement must precede all uses of the name.\n'
jpayne@69 4602 '\n'
jpayne@69 4603 'The "global" statement has the same scope as a name binding '
jpayne@69 4604 'operation\n'
jpayne@69 4605 'in the same block. If the nearest enclosing scope for a free '
jpayne@69 4606 'variable\n'
jpayne@69 4607 'contains a global statement, the free variable is treated as a '
jpayne@69 4608 'global.\n'
jpayne@69 4609 '\n'
jpayne@69 4610 'The "nonlocal" statement causes corresponding names to refer '
jpayne@69 4611 'to\n'
jpayne@69 4612 'previously bound variables in the nearest enclosing function '
jpayne@69 4613 'scope.\n'
jpayne@69 4614 '"SyntaxError" is raised at compile time if the given name does '
jpayne@69 4615 'not\n'
jpayne@69 4616 'exist in any enclosing function scope.\n'
jpayne@69 4617 '\n'
jpayne@69 4618 'The namespace for a module is automatically created the first '
jpayne@69 4619 'time a\n'
jpayne@69 4620 'module is imported. The main module for a script is always '
jpayne@69 4621 'called\n'
jpayne@69 4622 '"__main__".\n'
jpayne@69 4623 '\n'
jpayne@69 4624 'Class definition blocks and arguments to "exec()" and "eval()" '
jpayne@69 4625 'are\n'
jpayne@69 4626 'special in the context of name resolution. A class definition '
jpayne@69 4627 'is an\n'
jpayne@69 4628 'executable statement that may use and define names. These '
jpayne@69 4629 'references\n'
jpayne@69 4630 'follow the normal rules for name resolution with an exception '
jpayne@69 4631 'that\n'
jpayne@69 4632 'unbound local variables are looked up in the global namespace. '
jpayne@69 4633 'The\n'
jpayne@69 4634 'namespace of the class definition becomes the attribute '
jpayne@69 4635 'dictionary of\n'
jpayne@69 4636 'the class. The scope of names defined in a class block is '
jpayne@69 4637 'limited to\n'
jpayne@69 4638 'the class block; it does not extend to the code blocks of '
jpayne@69 4639 'methods –\n'
jpayne@69 4640 'this includes comprehensions and generator expressions since '
jpayne@69 4641 'they are\n'
jpayne@69 4642 'implemented using a function scope. This means that the '
jpayne@69 4643 'following\n'
jpayne@69 4644 'will fail:\n'
jpayne@69 4645 '\n'
jpayne@69 4646 ' class A:\n'
jpayne@69 4647 ' a = 42\n'
jpayne@69 4648 ' b = list(a + i for i in range(10))\n'
jpayne@69 4649 '\n'
jpayne@69 4650 '\n'
jpayne@69 4651 'Builtins and restricted execution\n'
jpayne@69 4652 '---------------------------------\n'
jpayne@69 4653 '\n'
jpayne@69 4654 '**CPython implementation detail:** Users should not touch\n'
jpayne@69 4655 '"__builtins__"; it is strictly an implementation detail. '
jpayne@69 4656 'Users\n'
jpayne@69 4657 'wanting to override values in the builtins namespace should '
jpayne@69 4658 '"import"\n'
jpayne@69 4659 'the "builtins" module and modify its attributes appropriately.\n'
jpayne@69 4660 '\n'
jpayne@69 4661 'The builtins namespace associated with the execution of a code '
jpayne@69 4662 'block\n'
jpayne@69 4663 'is actually found by looking up the name "__builtins__" in its '
jpayne@69 4664 'global\n'
jpayne@69 4665 'namespace; this should be a dictionary or a module (in the '
jpayne@69 4666 'latter case\n'
jpayne@69 4667 'the module’s dictionary is used). By default, when in the '
jpayne@69 4668 '"__main__"\n'
jpayne@69 4669 'module, "__builtins__" is the built-in module "builtins"; when '
jpayne@69 4670 'in any\n'
jpayne@69 4671 'other module, "__builtins__" is an alias for the dictionary of '
jpayne@69 4672 'the\n'
jpayne@69 4673 '"builtins" module itself.\n'
jpayne@69 4674 '\n'
jpayne@69 4675 '\n'
jpayne@69 4676 'Interaction with dynamic features\n'
jpayne@69 4677 '---------------------------------\n'
jpayne@69 4678 '\n'
jpayne@69 4679 'Name resolution of free variables occurs at runtime, not at '
jpayne@69 4680 'compile\n'
jpayne@69 4681 'time. This means that the following code will print 42:\n'
jpayne@69 4682 '\n'
jpayne@69 4683 ' i = 10\n'
jpayne@69 4684 ' def f():\n'
jpayne@69 4685 ' print(i)\n'
jpayne@69 4686 ' i = 42\n'
jpayne@69 4687 ' f()\n'
jpayne@69 4688 '\n'
jpayne@69 4689 'The "eval()" and "exec()" functions do not have access to the '
jpayne@69 4690 'full\n'
jpayne@69 4691 'environment for resolving names. Names may be resolved in the '
jpayne@69 4692 'local\n'
jpayne@69 4693 'and global namespaces of the caller. Free variables are not '
jpayne@69 4694 'resolved\n'
jpayne@69 4695 'in the nearest enclosing namespace, but in the global '
jpayne@69 4696 'namespace. [1]\n'
jpayne@69 4697 'The "exec()" and "eval()" functions have optional arguments to\n'
jpayne@69 4698 'override the global and local namespace. If only one namespace '
jpayne@69 4699 'is\n'
jpayne@69 4700 'specified, it is used for both.\n'
jpayne@69 4701 '\n'
jpayne@69 4702 '\n'
jpayne@69 4703 'Exceptions\n'
jpayne@69 4704 '==========\n'
jpayne@69 4705 '\n'
jpayne@69 4706 'Exceptions are a means of breaking out of the normal flow of '
jpayne@69 4707 'control\n'
jpayne@69 4708 'of a code block in order to handle errors or other exceptional\n'
jpayne@69 4709 'conditions. An exception is *raised* at the point where the '
jpayne@69 4710 'error is\n'
jpayne@69 4711 'detected; it may be *handled* by the surrounding code block or '
jpayne@69 4712 'by any\n'
jpayne@69 4713 'code block that directly or indirectly invoked the code block '
jpayne@69 4714 'where\n'
jpayne@69 4715 'the error occurred.\n'
jpayne@69 4716 '\n'
jpayne@69 4717 'The Python interpreter raises an exception when it detects a '
jpayne@69 4718 'run-time\n'
jpayne@69 4719 'error (such as division by zero). A Python program can also\n'
jpayne@69 4720 'explicitly raise an exception with the "raise" statement. '
jpayne@69 4721 'Exception\n'
jpayne@69 4722 'handlers are specified with the "try" … "except" statement. '
jpayne@69 4723 'The\n'
jpayne@69 4724 '"finally" clause of such a statement can be used to specify '
jpayne@69 4725 'cleanup\n'
jpayne@69 4726 'code which does not handle the exception, but is executed '
jpayne@69 4727 'whether an\n'
jpayne@69 4728 'exception occurred or not in the preceding code.\n'
jpayne@69 4729 '\n'
jpayne@69 4730 'Python uses the “termination” model of error handling: an '
jpayne@69 4731 'exception\n'
jpayne@69 4732 'handler can find out what happened and continue execution at an '
jpayne@69 4733 'outer\n'
jpayne@69 4734 'level, but it cannot repair the cause of the error and retry '
jpayne@69 4735 'the\n'
jpayne@69 4736 'failing operation (except by re-entering the offending piece of '
jpayne@69 4737 'code\n'
jpayne@69 4738 'from the top).\n'
jpayne@69 4739 '\n'
jpayne@69 4740 'When an exception is not handled at all, the interpreter '
jpayne@69 4741 'terminates\n'
jpayne@69 4742 'execution of the program, or returns to its interactive main '
jpayne@69 4743 'loop. In\n'
jpayne@69 4744 'either case, it prints a stack traceback, except when the '
jpayne@69 4745 'exception is\n'
jpayne@69 4746 '"SystemExit".\n'
jpayne@69 4747 '\n'
jpayne@69 4748 'Exceptions are identified by class instances. The "except" '
jpayne@69 4749 'clause is\n'
jpayne@69 4750 'selected depending on the class of the instance: it must '
jpayne@69 4751 'reference the\n'
jpayne@69 4752 'class of the instance or a base class thereof. The instance '
jpayne@69 4753 'can be\n'
jpayne@69 4754 'received by the handler and can carry additional information '
jpayne@69 4755 'about the\n'
jpayne@69 4756 'exceptional condition.\n'
jpayne@69 4757 '\n'
jpayne@69 4758 'Note: Exception messages are not part of the Python API. '
jpayne@69 4759 'Their\n'
jpayne@69 4760 ' contents may change from one version of Python to the next '
jpayne@69 4761 'without\n'
jpayne@69 4762 ' warning and should not be relied on by code which will run '
jpayne@69 4763 'under\n'
jpayne@69 4764 ' multiple versions of the interpreter.\n'
jpayne@69 4765 '\n'
jpayne@69 4766 'See also the description of the "try" statement in section The '
jpayne@69 4767 'try\n'
jpayne@69 4768 'statement and "raise" statement in section The raise '
jpayne@69 4769 'statement.\n'
jpayne@69 4770 '\n'
jpayne@69 4771 '-[ Footnotes ]-\n'
jpayne@69 4772 '\n'
jpayne@69 4773 '[1] This limitation occurs because the code that is executed '
jpayne@69 4774 'by\n'
jpayne@69 4775 ' these operations is not available at the time the module '
jpayne@69 4776 'is\n'
jpayne@69 4777 ' compiled.\n',
jpayne@69 4778 'exprlists': 'Expression lists\n'
jpayne@69 4779 '****************\n'
jpayne@69 4780 '\n'
jpayne@69 4781 ' expression_list ::= expression ("," expression)* [","]\n'
jpayne@69 4782 ' starred_list ::= starred_item ("," starred_item)* '
jpayne@69 4783 '[","]\n'
jpayne@69 4784 ' starred_expression ::= expression | (starred_item ",")* '
jpayne@69 4785 '[starred_item]\n'
jpayne@69 4786 ' starred_item ::= expression | "*" or_expr\n'
jpayne@69 4787 '\n'
jpayne@69 4788 'Except when part of a list or set display, an expression list\n'
jpayne@69 4789 'containing at least one comma yields a tuple. The length of '
jpayne@69 4790 'the tuple\n'
jpayne@69 4791 'is the number of expressions in the list. The expressions are\n'
jpayne@69 4792 'evaluated from left to right.\n'
jpayne@69 4793 '\n'
jpayne@69 4794 'An asterisk "*" denotes *iterable unpacking*. Its operand must '
jpayne@69 4795 'be an\n'
jpayne@69 4796 '*iterable*. The iterable is expanded into a sequence of items, '
jpayne@69 4797 'which\n'
jpayne@69 4798 'are included in the new tuple, list, or set, at the site of '
jpayne@69 4799 'the\n'
jpayne@69 4800 'unpacking.\n'
jpayne@69 4801 '\n'
jpayne@69 4802 'New in version 3.5: Iterable unpacking in expression lists, '
jpayne@69 4803 'originally\n'
jpayne@69 4804 'proposed by **PEP 448**.\n'
jpayne@69 4805 '\n'
jpayne@69 4806 'The trailing comma is required only to create a single tuple '
jpayne@69 4807 '(a.k.a. a\n'
jpayne@69 4808 '*singleton*); it is optional in all other cases. A single '
jpayne@69 4809 'expression\n'
jpayne@69 4810 'without a trailing comma doesn’t create a tuple, but rather '
jpayne@69 4811 'yields the\n'
jpayne@69 4812 'value of that expression. (To create an empty tuple, use an '
jpayne@69 4813 'empty pair\n'
jpayne@69 4814 'of parentheses: "()".)\n',
jpayne@69 4815 'floating': 'Floating point literals\n'
jpayne@69 4816 '***********************\n'
jpayne@69 4817 '\n'
jpayne@69 4818 'Floating point literals are described by the following lexical\n'
jpayne@69 4819 'definitions:\n'
jpayne@69 4820 '\n'
jpayne@69 4821 ' floatnumber ::= pointfloat | exponentfloat\n'
jpayne@69 4822 ' pointfloat ::= [digitpart] fraction | digitpart "."\n'
jpayne@69 4823 ' exponentfloat ::= (digitpart | pointfloat) exponent\n'
jpayne@69 4824 ' digitpart ::= digit (["_"] digit)*\n'
jpayne@69 4825 ' fraction ::= "." digitpart\n'
jpayne@69 4826 ' exponent ::= ("e" | "E") ["+" | "-"] digitpart\n'
jpayne@69 4827 '\n'
jpayne@69 4828 'Note that the integer and exponent parts are always interpreted '
jpayne@69 4829 'using\n'
jpayne@69 4830 'radix 10. For example, "077e010" is legal, and denotes the same '
jpayne@69 4831 'number\n'
jpayne@69 4832 'as "77e10". The allowed range of floating point literals is\n'
jpayne@69 4833 'implementation-dependent. As in integer literals, underscores '
jpayne@69 4834 'are\n'
jpayne@69 4835 'supported for digit grouping.\n'
jpayne@69 4836 '\n'
jpayne@69 4837 'Some examples of floating point literals:\n'
jpayne@69 4838 '\n'
jpayne@69 4839 ' 3.14 10. .001 1e100 3.14e-10 0e0 '
jpayne@69 4840 '3.14_15_93\n'
jpayne@69 4841 '\n'
jpayne@69 4842 'Changed in version 3.6: Underscores are now allowed for '
jpayne@69 4843 'grouping\n'
jpayne@69 4844 'purposes in literals.\n',
jpayne@69 4845 'for': 'The "for" statement\n'
jpayne@69 4846 '*******************\n'
jpayne@69 4847 '\n'
jpayne@69 4848 'The "for" statement is used to iterate over the elements of a '
jpayne@69 4849 'sequence\n'
jpayne@69 4850 '(such as a string, tuple or list) or other iterable object:\n'
jpayne@69 4851 '\n'
jpayne@69 4852 ' for_stmt ::= "for" target_list "in" expression_list ":" suite\n'
jpayne@69 4853 ' ["else" ":" suite]\n'
jpayne@69 4854 '\n'
jpayne@69 4855 'The expression list is evaluated once; it should yield an iterable\n'
jpayne@69 4856 'object. An iterator is created for the result of the\n'
jpayne@69 4857 '"expression_list". The suite is then executed once for each item\n'
jpayne@69 4858 'provided by the iterator, in the order returned by the iterator. '
jpayne@69 4859 'Each\n'
jpayne@69 4860 'item in turn is assigned to the target list using the standard rules\n'
jpayne@69 4861 'for assignments (see Assignment statements), and then the suite is\n'
jpayne@69 4862 'executed. When the items are exhausted (which is immediately when '
jpayne@69 4863 'the\n'
jpayne@69 4864 'sequence is empty or an iterator raises a "StopIteration" '
jpayne@69 4865 'exception),\n'
jpayne@69 4866 'the suite in the "else" clause, if present, is executed, and the '
jpayne@69 4867 'loop\n'
jpayne@69 4868 'terminates.\n'
jpayne@69 4869 '\n'
jpayne@69 4870 'A "break" statement executed in the first suite terminates the loop\n'
jpayne@69 4871 'without executing the "else" clause’s suite. A "continue" statement\n'
jpayne@69 4872 'executed in the first suite skips the rest of the suite and '
jpayne@69 4873 'continues\n'
jpayne@69 4874 'with the next item, or with the "else" clause if there is no next\n'
jpayne@69 4875 'item.\n'
jpayne@69 4876 '\n'
jpayne@69 4877 'The for-loop makes assignments to the variables in the target list.\n'
jpayne@69 4878 'This overwrites all previous assignments to those variables '
jpayne@69 4879 'including\n'
jpayne@69 4880 'those made in the suite of the for-loop:\n'
jpayne@69 4881 '\n'
jpayne@69 4882 ' for i in range(10):\n'
jpayne@69 4883 ' print(i)\n'
jpayne@69 4884 ' i = 5 # this will not affect the for-loop\n'
jpayne@69 4885 ' # because i will be overwritten with the '
jpayne@69 4886 'next\n'
jpayne@69 4887 ' # index in the range\n'
jpayne@69 4888 '\n'
jpayne@69 4889 'Names in the target list are not deleted when the loop is finished,\n'
jpayne@69 4890 'but if the sequence is empty, they will not have been assigned to at\n'
jpayne@69 4891 'all by the loop. Hint: the built-in function "range()" returns an\n'
jpayne@69 4892 'iterator of integers suitable to emulate the effect of Pascal’s "for '
jpayne@69 4893 'i\n'
jpayne@69 4894 ':= a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]".\n'
jpayne@69 4895 '\n'
jpayne@69 4896 'Note: There is a subtlety when the sequence is being modified by the\n'
jpayne@69 4897 ' loop (this can only occur for mutable sequences, e.g. lists). An\n'
jpayne@69 4898 ' internal counter is used to keep track of which item is used next,\n'
jpayne@69 4899 ' and this is incremented on each iteration. When this counter has\n'
jpayne@69 4900 ' reached the length of the sequence the loop terminates. This '
jpayne@69 4901 'means\n'
jpayne@69 4902 ' that if the suite deletes the current (or a previous) item from '
jpayne@69 4903 'the\n'
jpayne@69 4904 ' sequence, the next item will be skipped (since it gets the index '
jpayne@69 4905 'of\n'
jpayne@69 4906 ' the current item which has already been treated). Likewise, if '
jpayne@69 4907 'the\n'
jpayne@69 4908 ' suite inserts an item in the sequence before the current item, the\n'
jpayne@69 4909 ' current item will be treated again the next time through the loop.\n'
jpayne@69 4910 ' This can lead to nasty bugs that can be avoided by making a\n'
jpayne@69 4911 ' temporary copy using a slice of the whole sequence, e.g.,\n'
jpayne@69 4912 '\n'
jpayne@69 4913 ' for x in a[:]:\n'
jpayne@69 4914 ' if x < 0: a.remove(x)\n',
jpayne@69 4915 'formatstrings': 'Format String Syntax\n'
jpayne@69 4916 '********************\n'
jpayne@69 4917 '\n'
jpayne@69 4918 'The "str.format()" method and the "Formatter" class share '
jpayne@69 4919 'the same\n'
jpayne@69 4920 'syntax for format strings (although in the case of '
jpayne@69 4921 '"Formatter",\n'
jpayne@69 4922 'subclasses can define their own format string syntax). The '
jpayne@69 4923 'syntax is\n'
jpayne@69 4924 'related to that of formatted string literals, but there '
jpayne@69 4925 'are\n'
jpayne@69 4926 'differences.\n'
jpayne@69 4927 '\n'
jpayne@69 4928 'Format strings contain “replacement fields” surrounded by '
jpayne@69 4929 'curly braces\n'
jpayne@69 4930 '"{}". Anything that is not contained in braces is '
jpayne@69 4931 'considered literal\n'
jpayne@69 4932 'text, which is copied unchanged to the output. If you need '
jpayne@69 4933 'to include\n'
jpayne@69 4934 'a brace character in the literal text, it can be escaped by '
jpayne@69 4935 'doubling:\n'
jpayne@69 4936 '"{{" and "}}".\n'
jpayne@69 4937 '\n'
jpayne@69 4938 'The grammar for a replacement field is as follows:\n'
jpayne@69 4939 '\n'
jpayne@69 4940 ' replacement_field ::= "{" [field_name] ["!" '
jpayne@69 4941 'conversion] [":" format_spec] "}"\n'
jpayne@69 4942 ' field_name ::= arg_name ("." attribute_name | '
jpayne@69 4943 '"[" element_index "]")*\n'
jpayne@69 4944 ' arg_name ::= [identifier | digit+]\n'
jpayne@69 4945 ' attribute_name ::= identifier\n'
jpayne@69 4946 ' element_index ::= digit+ | index_string\n'
jpayne@69 4947 ' index_string ::= <any source character except '
jpayne@69 4948 '"]"> +\n'
jpayne@69 4949 ' conversion ::= "r" | "s" | "a"\n'
jpayne@69 4950 ' format_spec ::= <described in the next '
jpayne@69 4951 'section>\n'
jpayne@69 4952 '\n'
jpayne@69 4953 'In less formal terms, the replacement field can start with '
jpayne@69 4954 'a\n'
jpayne@69 4955 '*field_name* that specifies the object whose value is to be '
jpayne@69 4956 'formatted\n'
jpayne@69 4957 'and inserted into the output instead of the replacement '
jpayne@69 4958 'field. The\n'
jpayne@69 4959 '*field_name* is optionally followed by a *conversion* '
jpayne@69 4960 'field, which is\n'
jpayne@69 4961 'preceded by an exclamation point "\'!\'", and a '
jpayne@69 4962 '*format_spec*, which is\n'
jpayne@69 4963 'preceded by a colon "\':\'". These specify a non-default '
jpayne@69 4964 'format for the\n'
jpayne@69 4965 'replacement value.\n'
jpayne@69 4966 '\n'
jpayne@69 4967 'See also the Format Specification Mini-Language section.\n'
jpayne@69 4968 '\n'
jpayne@69 4969 'The *field_name* itself begins with an *arg_name* that is '
jpayne@69 4970 'either a\n'
jpayne@69 4971 'number or a keyword. If it’s a number, it refers to a '
jpayne@69 4972 'positional\n'
jpayne@69 4973 'argument, and if it’s a keyword, it refers to a named '
jpayne@69 4974 'keyword\n'
jpayne@69 4975 'argument. If the numerical arg_names in a format string '
jpayne@69 4976 'are 0, 1, 2,\n'
jpayne@69 4977 '… in sequence, they can all be omitted (not just some) and '
jpayne@69 4978 'the numbers\n'
jpayne@69 4979 '0, 1, 2, … will be automatically inserted in that order. '
jpayne@69 4980 'Because\n'
jpayne@69 4981 '*arg_name* is not quote-delimited, it is not possible to '
jpayne@69 4982 'specify\n'
jpayne@69 4983 'arbitrary dictionary keys (e.g., the strings "\'10\'" or '
jpayne@69 4984 '"\':-]\'") within\n'
jpayne@69 4985 'a format string. The *arg_name* can be followed by any '
jpayne@69 4986 'number of index\n'
jpayne@69 4987 'or attribute expressions. An expression of the form '
jpayne@69 4988 '"\'.name\'" selects\n'
jpayne@69 4989 'the named attribute using "getattr()", while an expression '
jpayne@69 4990 'of the form\n'
jpayne@69 4991 '"\'[index]\'" does an index lookup using "__getitem__()".\n'
jpayne@69 4992 '\n'
jpayne@69 4993 'Changed in version 3.1: The positional argument specifiers '
jpayne@69 4994 'can be\n'
jpayne@69 4995 'omitted for "str.format()", so "\'{} {}\'.format(a, b)" is '
jpayne@69 4996 'equivalent to\n'
jpayne@69 4997 '"\'{0} {1}\'.format(a, b)".\n'
jpayne@69 4998 '\n'
jpayne@69 4999 'Changed in version 3.4: The positional argument specifiers '
jpayne@69 5000 'can be\n'
jpayne@69 5001 'omitted for "Formatter".\n'
jpayne@69 5002 '\n'
jpayne@69 5003 'Some simple format string examples:\n'
jpayne@69 5004 '\n'
jpayne@69 5005 ' "First, thou shalt count to {0}" # References first '
jpayne@69 5006 'positional argument\n'
jpayne@69 5007 ' "Bring me a {}" # Implicitly '
jpayne@69 5008 'references the first positional argument\n'
jpayne@69 5009 ' "From {} to {}" # Same as "From {0} to '
jpayne@69 5010 '{1}"\n'
jpayne@69 5011 ' "My quest is {name}" # References keyword '
jpayne@69 5012 "argument 'name'\n"
jpayne@69 5013 ' "Weight in tons {0.weight}" # \'weight\' attribute '
jpayne@69 5014 'of first positional arg\n'
jpayne@69 5015 ' "Units destroyed: {players[0]}" # First element of '
jpayne@69 5016 "keyword argument 'players'.\n"
jpayne@69 5017 '\n'
jpayne@69 5018 'The *conversion* field causes a type coercion before '
jpayne@69 5019 'formatting.\n'
jpayne@69 5020 'Normally, the job of formatting a value is done by the '
jpayne@69 5021 '"__format__()"\n'
jpayne@69 5022 'method of the value itself. However, in some cases it is '
jpayne@69 5023 'desirable to\n'
jpayne@69 5024 'force a type to be formatted as a string, overriding its '
jpayne@69 5025 'own\n'
jpayne@69 5026 'definition of formatting. By converting the value to a '
jpayne@69 5027 'string before\n'
jpayne@69 5028 'calling "__format__()", the normal formatting logic is '
jpayne@69 5029 'bypassed.\n'
jpayne@69 5030 '\n'
jpayne@69 5031 'Three conversion flags are currently supported: "\'!s\'" '
jpayne@69 5032 'which calls\n'
jpayne@69 5033 '"str()" on the value, "\'!r\'" which calls "repr()" and '
jpayne@69 5034 '"\'!a\'" which\n'
jpayne@69 5035 'calls "ascii()".\n'
jpayne@69 5036 '\n'
jpayne@69 5037 'Some examples:\n'
jpayne@69 5038 '\n'
jpayne@69 5039 ' "Harold\'s a clever {0!s}" # Calls str() on the '
jpayne@69 5040 'argument first\n'
jpayne@69 5041 ' "Bring out the holy {name!r}" # Calls repr() on the '
jpayne@69 5042 'argument first\n'
jpayne@69 5043 ' "More {!a}" # Calls ascii() on the '
jpayne@69 5044 'argument first\n'
jpayne@69 5045 '\n'
jpayne@69 5046 'The *format_spec* field contains a specification of how the '
jpayne@69 5047 'value\n'
jpayne@69 5048 'should be presented, including such details as field width, '
jpayne@69 5049 'alignment,\n'
jpayne@69 5050 'padding, decimal precision and so on. Each value type can '
jpayne@69 5051 'define its\n'
jpayne@69 5052 'own “formatting mini-language” or interpretation of the '
jpayne@69 5053 '*format_spec*.\n'
jpayne@69 5054 '\n'
jpayne@69 5055 'Most built-in types support a common formatting '
jpayne@69 5056 'mini-language, which\n'
jpayne@69 5057 'is described in the next section.\n'
jpayne@69 5058 '\n'
jpayne@69 5059 'A *format_spec* field can also include nested replacement '
jpayne@69 5060 'fields\n'
jpayne@69 5061 'within it. These nested replacement fields may contain a '
jpayne@69 5062 'field name,\n'
jpayne@69 5063 'conversion flag and format specification, but deeper '
jpayne@69 5064 'nesting is not\n'
jpayne@69 5065 'allowed. The replacement fields within the format_spec '
jpayne@69 5066 'are\n'
jpayne@69 5067 'substituted before the *format_spec* string is interpreted. '
jpayne@69 5068 'This\n'
jpayne@69 5069 'allows the formatting of a value to be dynamically '
jpayne@69 5070 'specified.\n'
jpayne@69 5071 '\n'
jpayne@69 5072 'See the Format examples section for some examples.\n'
jpayne@69 5073 '\n'
jpayne@69 5074 '\n'
jpayne@69 5075 'Format Specification Mini-Language\n'
jpayne@69 5076 '==================================\n'
jpayne@69 5077 '\n'
jpayne@69 5078 '“Format specifications” are used within replacement fields '
jpayne@69 5079 'contained\n'
jpayne@69 5080 'within a format string to define how individual values are '
jpayne@69 5081 'presented\n'
jpayne@69 5082 '(see Format String Syntax and Formatted string literals). '
jpayne@69 5083 'They can\n'
jpayne@69 5084 'also be passed directly to the built-in "format()" '
jpayne@69 5085 'function. Each\n'
jpayne@69 5086 'formattable type may define how the format specification is '
jpayne@69 5087 'to be\n'
jpayne@69 5088 'interpreted.\n'
jpayne@69 5089 '\n'
jpayne@69 5090 'Most built-in types implement the following options for '
jpayne@69 5091 'format\n'
jpayne@69 5092 'specifications, although some of the formatting options are '
jpayne@69 5093 'only\n'
jpayne@69 5094 'supported by the numeric types.\n'
jpayne@69 5095 '\n'
jpayne@69 5096 'A general convention is that an empty format string ("""") '
jpayne@69 5097 'produces\n'
jpayne@69 5098 'the same result as if you had called "str()" on the value. '
jpayne@69 5099 'A non-empty\n'
jpayne@69 5100 'format string typically modifies the result.\n'
jpayne@69 5101 '\n'
jpayne@69 5102 'The general form of a *standard format specifier* is:\n'
jpayne@69 5103 '\n'
jpayne@69 5104 ' format_spec ::= '
jpayne@69 5105 '[[fill]align][sign][#][0][width][grouping_option][.precision][type]\n'
jpayne@69 5106 ' fill ::= <any character>\n'
jpayne@69 5107 ' align ::= "<" | ">" | "=" | "^"\n'
jpayne@69 5108 ' sign ::= "+" | "-" | " "\n'
jpayne@69 5109 ' width ::= digit+\n'
jpayne@69 5110 ' grouping_option ::= "_" | ","\n'
jpayne@69 5111 ' precision ::= digit+\n'
jpayne@69 5112 ' type ::= "b" | "c" | "d" | "e" | "E" | "f" | '
jpayne@69 5113 '"F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"\n'
jpayne@69 5114 '\n'
jpayne@69 5115 'If a valid *align* value is specified, it can be preceded '
jpayne@69 5116 'by a *fill*\n'
jpayne@69 5117 'character that can be any character and defaults to a space '
jpayne@69 5118 'if\n'
jpayne@69 5119 'omitted. It is not possible to use a literal curly brace '
jpayne@69 5120 '(“"{"” or\n'
jpayne@69 5121 '“"}"”) as the *fill* character in a formatted string '
jpayne@69 5122 'literal or when\n'
jpayne@69 5123 'using the "str.format()" method. However, it is possible '
jpayne@69 5124 'to insert a\n'
jpayne@69 5125 'curly brace with a nested replacement field. This '
jpayne@69 5126 'limitation doesn’t\n'
jpayne@69 5127 'affect the "format()" function.\n'
jpayne@69 5128 '\n'
jpayne@69 5129 'The meaning of the various alignment options is as '
jpayne@69 5130 'follows:\n'
jpayne@69 5131 '\n'
jpayne@69 5132 ' '
jpayne@69 5133 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5134 ' | Option | '
jpayne@69 5135 'Meaning '
jpayne@69 5136 '|\n'
jpayne@69 5137 ' '
jpayne@69 5138 '|===========|============================================================|\n'
jpayne@69 5139 ' | "\'<\'" | Forces the field to be left-aligned '
jpayne@69 5140 'within the available |\n'
jpayne@69 5141 ' | | space (this is the default for most '
jpayne@69 5142 'objects). |\n'
jpayne@69 5143 ' '
jpayne@69 5144 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5145 ' | "\'>\'" | Forces the field to be right-aligned '
jpayne@69 5146 'within the available |\n'
jpayne@69 5147 ' | | space (this is the default for '
jpayne@69 5148 'numbers). |\n'
jpayne@69 5149 ' '
jpayne@69 5150 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5151 ' | "\'=\'" | Forces the padding to be placed after '
jpayne@69 5152 'the sign (if any) |\n'
jpayne@69 5153 ' | | but before the digits. This is used for '
jpayne@69 5154 'printing fields |\n'
jpayne@69 5155 ' | | in the form ‘+000000120’. This alignment '
jpayne@69 5156 'option is only |\n'
jpayne@69 5157 ' | | valid for numeric types. It becomes the '
jpayne@69 5158 'default when ‘0’ |\n'
jpayne@69 5159 ' | | immediately precedes the field '
jpayne@69 5160 'width. |\n'
jpayne@69 5161 ' '
jpayne@69 5162 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5163 ' | "\'^\'" | Forces the field to be centered within '
jpayne@69 5164 'the available |\n'
jpayne@69 5165 ' | | '
jpayne@69 5166 'space. '
jpayne@69 5167 '|\n'
jpayne@69 5168 ' '
jpayne@69 5169 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5170 '\n'
jpayne@69 5171 'Note that unless a minimum field width is defined, the '
jpayne@69 5172 'field width\n'
jpayne@69 5173 'will always be the same size as the data to fill it, so '
jpayne@69 5174 'that the\n'
jpayne@69 5175 'alignment option has no meaning in this case.\n'
jpayne@69 5176 '\n'
jpayne@69 5177 'The *sign* option is only valid for number types, and can '
jpayne@69 5178 'be one of\n'
jpayne@69 5179 'the following:\n'
jpayne@69 5180 '\n'
jpayne@69 5181 ' '
jpayne@69 5182 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5183 ' | Option | '
jpayne@69 5184 'Meaning '
jpayne@69 5185 '|\n'
jpayne@69 5186 ' '
jpayne@69 5187 '|===========|============================================================|\n'
jpayne@69 5188 ' | "\'+\'" | indicates that a sign should be used for '
jpayne@69 5189 'both positive as |\n'
jpayne@69 5190 ' | | well as negative '
jpayne@69 5191 'numbers. |\n'
jpayne@69 5192 ' '
jpayne@69 5193 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5194 ' | "\'-\'" | indicates that a sign should be used '
jpayne@69 5195 'only for negative |\n'
jpayne@69 5196 ' | | numbers (this is the default '
jpayne@69 5197 'behavior). |\n'
jpayne@69 5198 ' '
jpayne@69 5199 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5200 ' | space | indicates that a leading space should be '
jpayne@69 5201 'used on positive |\n'
jpayne@69 5202 ' | | numbers, and a minus sign on negative '
jpayne@69 5203 'numbers. |\n'
jpayne@69 5204 ' '
jpayne@69 5205 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5206 '\n'
jpayne@69 5207 'The "\'#\'" option causes the “alternate form” to be used '
jpayne@69 5208 'for the\n'
jpayne@69 5209 'conversion. The alternate form is defined differently for '
jpayne@69 5210 'different\n'
jpayne@69 5211 'types. This option is only valid for integer, float, '
jpayne@69 5212 'complex and\n'
jpayne@69 5213 'Decimal types. For integers, when binary, octal, or '
jpayne@69 5214 'hexadecimal output\n'
jpayne@69 5215 'is used, this option adds the prefix respective "\'0b\'", '
jpayne@69 5216 '"\'0o\'", or\n'
jpayne@69 5217 '"\'0x\'" to the output value. For floats, complex and '
jpayne@69 5218 'Decimal the\n'
jpayne@69 5219 'alternate form causes the result of the conversion to '
jpayne@69 5220 'always contain a\n'
jpayne@69 5221 'decimal-point character, even if no digits follow it. '
jpayne@69 5222 'Normally, a\n'
jpayne@69 5223 'decimal-point character appears in the result of these '
jpayne@69 5224 'conversions\n'
jpayne@69 5225 'only if a digit follows it. In addition, for "\'g\'" and '
jpayne@69 5226 '"\'G\'"\n'
jpayne@69 5227 'conversions, trailing zeros are not removed from the '
jpayne@69 5228 'result.\n'
jpayne@69 5229 '\n'
jpayne@69 5230 'The "\',\'" option signals the use of a comma for a '
jpayne@69 5231 'thousands separator.\n'
jpayne@69 5232 'For a locale aware separator, use the "\'n\'" integer '
jpayne@69 5233 'presentation type\n'
jpayne@69 5234 'instead.\n'
jpayne@69 5235 '\n'
jpayne@69 5236 'Changed in version 3.1: Added the "\',\'" option (see also '
jpayne@69 5237 '**PEP 378**).\n'
jpayne@69 5238 '\n'
jpayne@69 5239 'The "\'_\'" option signals the use of an underscore for a '
jpayne@69 5240 'thousands\n'
jpayne@69 5241 'separator for floating point presentation types and for '
jpayne@69 5242 'integer\n'
jpayne@69 5243 'presentation type "\'d\'". For integer presentation types '
jpayne@69 5244 '"\'b\'", "\'o\'",\n'
jpayne@69 5245 '"\'x\'", and "\'X\'", underscores will be inserted every 4 '
jpayne@69 5246 'digits. For\n'
jpayne@69 5247 'other presentation types, specifying this option is an '
jpayne@69 5248 'error.\n'
jpayne@69 5249 '\n'
jpayne@69 5250 'Changed in version 3.6: Added the "\'_\'" option (see also '
jpayne@69 5251 '**PEP 515**).\n'
jpayne@69 5252 '\n'
jpayne@69 5253 '*width* is a decimal integer defining the minimum field '
jpayne@69 5254 'width. If not\n'
jpayne@69 5255 'specified, then the field width will be determined by the '
jpayne@69 5256 'content.\n'
jpayne@69 5257 '\n'
jpayne@69 5258 'When no explicit alignment is given, preceding the *width* '
jpayne@69 5259 'field by a\n'
jpayne@69 5260 'zero ("\'0\'") character enables sign-aware zero-padding '
jpayne@69 5261 'for numeric\n'
jpayne@69 5262 'types. This is equivalent to a *fill* character of "\'0\'" '
jpayne@69 5263 'with an\n'
jpayne@69 5264 '*alignment* type of "\'=\'".\n'
jpayne@69 5265 '\n'
jpayne@69 5266 'The *precision* is a decimal number indicating how many '
jpayne@69 5267 'digits should\n'
jpayne@69 5268 'be displayed after the decimal point for a floating point '
jpayne@69 5269 'value\n'
jpayne@69 5270 'formatted with "\'f\'" and "\'F\'", or before and after the '
jpayne@69 5271 'decimal point\n'
jpayne@69 5272 'for a floating point value formatted with "\'g\'" or '
jpayne@69 5273 '"\'G\'". For non-\n'
jpayne@69 5274 'number types the field indicates the maximum field size - '
jpayne@69 5275 'in other\n'
jpayne@69 5276 'words, how many characters will be used from the field '
jpayne@69 5277 'content. The\n'
jpayne@69 5278 '*precision* is not allowed for integer values.\n'
jpayne@69 5279 '\n'
jpayne@69 5280 'Finally, the *type* determines how the data should be '
jpayne@69 5281 'presented.\n'
jpayne@69 5282 '\n'
jpayne@69 5283 'The available string presentation types are:\n'
jpayne@69 5284 '\n'
jpayne@69 5285 ' '
jpayne@69 5286 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5287 ' | Type | '
jpayne@69 5288 'Meaning '
jpayne@69 5289 '|\n'
jpayne@69 5290 ' '
jpayne@69 5291 '|===========|============================================================|\n'
jpayne@69 5292 ' | "\'s\'" | String format. This is the default type '
jpayne@69 5293 'for strings and |\n'
jpayne@69 5294 ' | | may be '
jpayne@69 5295 'omitted. |\n'
jpayne@69 5296 ' '
jpayne@69 5297 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5298 ' | None | The same as '
jpayne@69 5299 '"\'s\'". |\n'
jpayne@69 5300 ' '
jpayne@69 5301 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5302 '\n'
jpayne@69 5303 'The available integer presentation types are:\n'
jpayne@69 5304 '\n'
jpayne@69 5305 ' '
jpayne@69 5306 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5307 ' | Type | '
jpayne@69 5308 'Meaning '
jpayne@69 5309 '|\n'
jpayne@69 5310 ' '
jpayne@69 5311 '|===========|============================================================|\n'
jpayne@69 5312 ' | "\'b\'" | Binary format. Outputs the number in '
jpayne@69 5313 'base 2. |\n'
jpayne@69 5314 ' '
jpayne@69 5315 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5316 ' | "\'c\'" | Character. Converts the integer to the '
jpayne@69 5317 'corresponding |\n'
jpayne@69 5318 ' | | unicode character before '
jpayne@69 5319 'printing. |\n'
jpayne@69 5320 ' '
jpayne@69 5321 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5322 ' | "\'d\'" | Decimal Integer. Outputs the number in '
jpayne@69 5323 'base 10. |\n'
jpayne@69 5324 ' '
jpayne@69 5325 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5326 ' | "\'o\'" | Octal format. Outputs the number in base '
jpayne@69 5327 '8. |\n'
jpayne@69 5328 ' '
jpayne@69 5329 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5330 ' | "\'x\'" | Hex format. Outputs the number in base '
jpayne@69 5331 '16, using lower- |\n'
jpayne@69 5332 ' | | case letters for the digits above '
jpayne@69 5333 '9. |\n'
jpayne@69 5334 ' '
jpayne@69 5335 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5336 ' | "\'X\'" | Hex format. Outputs the number in base '
jpayne@69 5337 '16, using upper- |\n'
jpayne@69 5338 ' | | case letters for the digits above '
jpayne@69 5339 '9. |\n'
jpayne@69 5340 ' '
jpayne@69 5341 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5342 ' | "\'n\'" | Number. This is the same as "\'d\'", '
jpayne@69 5343 'except that it uses the |\n'
jpayne@69 5344 ' | | current locale setting to insert the '
jpayne@69 5345 'appropriate number |\n'
jpayne@69 5346 ' | | separator '
jpayne@69 5347 'characters. |\n'
jpayne@69 5348 ' '
jpayne@69 5349 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5350 ' | None | The same as '
jpayne@69 5351 '"\'d\'". |\n'
jpayne@69 5352 ' '
jpayne@69 5353 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5354 '\n'
jpayne@69 5355 'In addition to the above presentation types, integers can '
jpayne@69 5356 'be formatted\n'
jpayne@69 5357 'with the floating point presentation types listed below '
jpayne@69 5358 '(except "\'n\'"\n'
jpayne@69 5359 'and "None"). When doing so, "float()" is used to convert '
jpayne@69 5360 'the integer\n'
jpayne@69 5361 'to a floating point number before formatting.\n'
jpayne@69 5362 '\n'
jpayne@69 5363 'The available presentation types for floating point and '
jpayne@69 5364 'decimal values\n'
jpayne@69 5365 'are:\n'
jpayne@69 5366 '\n'
jpayne@69 5367 ' '
jpayne@69 5368 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5369 ' | Type | '
jpayne@69 5370 'Meaning '
jpayne@69 5371 '|\n'
jpayne@69 5372 ' '
jpayne@69 5373 '|===========|============================================================|\n'
jpayne@69 5374 ' | "\'e\'" | Exponent notation. Prints the number in '
jpayne@69 5375 'scientific |\n'
jpayne@69 5376 ' | | notation using the letter ‘e’ to indicate '
jpayne@69 5377 'the exponent. |\n'
jpayne@69 5378 ' | | The default precision is '
jpayne@69 5379 '"6". |\n'
jpayne@69 5380 ' '
jpayne@69 5381 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5382 ' | "\'E\'" | Exponent notation. Same as "\'e\'" '
jpayne@69 5383 'except it uses an upper |\n'
jpayne@69 5384 ' | | case ‘E’ as the separator '
jpayne@69 5385 'character. |\n'
jpayne@69 5386 ' '
jpayne@69 5387 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5388 ' | "\'f\'" | Fixed-point notation. Displays the '
jpayne@69 5389 'number as a fixed-point |\n'
jpayne@69 5390 ' | | number. The default precision is '
jpayne@69 5391 '"6". |\n'
jpayne@69 5392 ' '
jpayne@69 5393 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5394 ' | "\'F\'" | Fixed-point notation. Same as "\'f\'", '
jpayne@69 5395 'but converts "nan" to |\n'
jpayne@69 5396 ' | | "NAN" and "inf" to '
jpayne@69 5397 '"INF". |\n'
jpayne@69 5398 ' '
jpayne@69 5399 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5400 ' | "\'g\'" | General format. For a given precision '
jpayne@69 5401 '"p >= 1", this |\n'
jpayne@69 5402 ' | | rounds the number to "p" significant '
jpayne@69 5403 'digits and then |\n'
jpayne@69 5404 ' | | formats the result in either fixed-point '
jpayne@69 5405 'format or in |\n'
jpayne@69 5406 ' | | scientific notation, depending on its '
jpayne@69 5407 'magnitude. The |\n'
jpayne@69 5408 ' | | precise rules are as follows: suppose that '
jpayne@69 5409 'the result |\n'
jpayne@69 5410 ' | | formatted with presentation type "\'e\'" '
jpayne@69 5411 'and precision "p-1" |\n'
jpayne@69 5412 ' | | would have exponent "exp". Then, if "m <= '
jpayne@69 5413 'exp < p", where |\n'
jpayne@69 5414 ' | | "m" is -4 for floats and -6 for '
jpayne@69 5415 '"Decimals", the number is |\n'
jpayne@69 5416 ' | | formatted with presentation type "\'f\'" '
jpayne@69 5417 'and precision |\n'
jpayne@69 5418 ' | | "p-1-exp". Otherwise, the number is '
jpayne@69 5419 'formatted with |\n'
jpayne@69 5420 ' | | presentation type "\'e\'" and precision '
jpayne@69 5421 '"p-1". In both cases |\n'
jpayne@69 5422 ' | | insignificant trailing zeros are removed '
jpayne@69 5423 'from the |\n'
jpayne@69 5424 ' | | significand, and the decimal point is also '
jpayne@69 5425 'removed if |\n'
jpayne@69 5426 ' | | there are no remaining digits following '
jpayne@69 5427 'it, unless the |\n'
jpayne@69 5428 ' | | "\'#\'" option is used. Positive and '
jpayne@69 5429 'negative infinity, |\n'
jpayne@69 5430 ' | | positive and negative zero, and nans, are '
jpayne@69 5431 'formatted as |\n'
jpayne@69 5432 ' | | "inf", "-inf", "0", "-0" and "nan" '
jpayne@69 5433 'respectively, |\n'
jpayne@69 5434 ' | | regardless of the precision. A precision '
jpayne@69 5435 'of "0" is |\n'
jpayne@69 5436 ' | | treated as equivalent to a precision of '
jpayne@69 5437 '"1". The default |\n'
jpayne@69 5438 ' | | precision is '
jpayne@69 5439 '"6". |\n'
jpayne@69 5440 ' '
jpayne@69 5441 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5442 ' | "\'G\'" | General format. Same as "\'g\'" except '
jpayne@69 5443 'switches to "\'E\'" if |\n'
jpayne@69 5444 ' | | the number gets too large. The '
jpayne@69 5445 'representations of infinity |\n'
jpayne@69 5446 ' | | and NaN are uppercased, '
jpayne@69 5447 'too. |\n'
jpayne@69 5448 ' '
jpayne@69 5449 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5450 ' | "\'n\'" | Number. This is the same as "\'g\'", '
jpayne@69 5451 'except that it uses the |\n'
jpayne@69 5452 ' | | current locale setting to insert the '
jpayne@69 5453 'appropriate number |\n'
jpayne@69 5454 ' | | separator '
jpayne@69 5455 'characters. |\n'
jpayne@69 5456 ' '
jpayne@69 5457 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5458 ' | "\'%\'" | Percentage. Multiplies the number by 100 '
jpayne@69 5459 'and displays in |\n'
jpayne@69 5460 ' | | fixed ("\'f\'") format, followed by a '
jpayne@69 5461 'percent sign. |\n'
jpayne@69 5462 ' '
jpayne@69 5463 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5464 ' | None | Similar to "\'g\'", except that '
jpayne@69 5465 'fixed-point notation, when |\n'
jpayne@69 5466 ' | | used, has at least one digit past the '
jpayne@69 5467 'decimal point. The |\n'
jpayne@69 5468 ' | | default precision is as high as needed to '
jpayne@69 5469 'represent the |\n'
jpayne@69 5470 ' | | particular value. The overall effect is to '
jpayne@69 5471 'match the |\n'
jpayne@69 5472 ' | | output of "str()" as altered by the other '
jpayne@69 5473 'format |\n'
jpayne@69 5474 ' | | '
jpayne@69 5475 'modifiers. '
jpayne@69 5476 '|\n'
jpayne@69 5477 ' '
jpayne@69 5478 '+-----------+------------------------------------------------------------+\n'
jpayne@69 5479 '\n'
jpayne@69 5480 '\n'
jpayne@69 5481 'Format examples\n'
jpayne@69 5482 '===============\n'
jpayne@69 5483 '\n'
jpayne@69 5484 'This section contains examples of the "str.format()" syntax '
jpayne@69 5485 'and\n'
jpayne@69 5486 'comparison with the old "%"-formatting.\n'
jpayne@69 5487 '\n'
jpayne@69 5488 'In most of the cases the syntax is similar to the old '
jpayne@69 5489 '"%"-formatting,\n'
jpayne@69 5490 'with the addition of the "{}" and with ":" used instead of '
jpayne@69 5491 '"%". For\n'
jpayne@69 5492 'example, "\'%03.2f\'" can be translated to "\'{:03.2f}\'".\n'
jpayne@69 5493 '\n'
jpayne@69 5494 'The new format syntax also supports new and different '
jpayne@69 5495 'options, shown\n'
jpayne@69 5496 'in the following examples.\n'
jpayne@69 5497 '\n'
jpayne@69 5498 'Accessing arguments by position:\n'
jpayne@69 5499 '\n'
jpayne@69 5500 " >>> '{0}, {1}, {2}'.format('a', 'b', 'c')\n"
jpayne@69 5501 " 'a, b, c'\n"
jpayne@69 5502 " >>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only\n"
jpayne@69 5503 " 'a, b, c'\n"
jpayne@69 5504 " >>> '{2}, {1}, {0}'.format('a', 'b', 'c')\n"
jpayne@69 5505 " 'c, b, a'\n"
jpayne@69 5506 " >>> '{2}, {1}, {0}'.format(*'abc') # unpacking "
jpayne@69 5507 'argument sequence\n'
jpayne@69 5508 " 'c, b, a'\n"
jpayne@69 5509 " >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' "
jpayne@69 5510 'indices can be repeated\n'
jpayne@69 5511 " 'abracadabra'\n"
jpayne@69 5512 '\n'
jpayne@69 5513 'Accessing arguments by name:\n'
jpayne@69 5514 '\n'
jpayne@69 5515 " >>> 'Coordinates: {latitude}, "
jpayne@69 5516 "{longitude}'.format(latitude='37.24N', "
jpayne@69 5517 "longitude='-115.81W')\n"
jpayne@69 5518 " 'Coordinates: 37.24N, -115.81W'\n"
jpayne@69 5519 " >>> coord = {'latitude': '37.24N', 'longitude': "
jpayne@69 5520 "'-115.81W'}\n"
jpayne@69 5521 " >>> 'Coordinates: {latitude}, "
jpayne@69 5522 "{longitude}'.format(**coord)\n"
jpayne@69 5523 " 'Coordinates: 37.24N, -115.81W'\n"
jpayne@69 5524 '\n'
jpayne@69 5525 'Accessing arguments’ attributes:\n'
jpayne@69 5526 '\n'
jpayne@69 5527 ' >>> c = 3-5j\n'
jpayne@69 5528 " >>> ('The complex number {0} is formed from the real "
jpayne@69 5529 "part {0.real} '\n"
jpayne@69 5530 " ... 'and the imaginary part {0.imag}.').format(c)\n"
jpayne@69 5531 " 'The complex number (3-5j) is formed from the real part "
jpayne@69 5532 "3.0 and the imaginary part -5.0.'\n"
jpayne@69 5533 ' >>> class Point:\n'
jpayne@69 5534 ' ... def __init__(self, x, y):\n'
jpayne@69 5535 ' ... self.x, self.y = x, y\n'
jpayne@69 5536 ' ... def __str__(self):\n'
jpayne@69 5537 " ... return 'Point({self.x}, "
jpayne@69 5538 "{self.y})'.format(self=self)\n"
jpayne@69 5539 ' ...\n'
jpayne@69 5540 ' >>> str(Point(4, 2))\n'
jpayne@69 5541 " 'Point(4, 2)'\n"
jpayne@69 5542 '\n'
jpayne@69 5543 'Accessing arguments’ items:\n'
jpayne@69 5544 '\n'
jpayne@69 5545 ' >>> coord = (3, 5)\n'
jpayne@69 5546 " >>> 'X: {0[0]}; Y: {0[1]}'.format(coord)\n"
jpayne@69 5547 " 'X: 3; Y: 5'\n"
jpayne@69 5548 '\n'
jpayne@69 5549 'Replacing "%s" and "%r":\n'
jpayne@69 5550 '\n'
jpayne@69 5551 ' >>> "repr() shows quotes: {!r}; str() doesn\'t: '
jpayne@69 5552 '{!s}".format(\'test1\', \'test2\')\n'
jpayne@69 5553 ' "repr() shows quotes: \'test1\'; str() doesn\'t: test2"\n'
jpayne@69 5554 '\n'
jpayne@69 5555 'Aligning the text and specifying a width:\n'
jpayne@69 5556 '\n'
jpayne@69 5557 " >>> '{:<30}'.format('left aligned')\n"
jpayne@69 5558 " 'left aligned '\n"
jpayne@69 5559 " >>> '{:>30}'.format('right aligned')\n"
jpayne@69 5560 " ' right aligned'\n"
jpayne@69 5561 " >>> '{:^30}'.format('centered')\n"
jpayne@69 5562 " ' centered '\n"
jpayne@69 5563 " >>> '{:*^30}'.format('centered') # use '*' as a fill "
jpayne@69 5564 'char\n'
jpayne@69 5565 " '***********centered***********'\n"
jpayne@69 5566 '\n'
jpayne@69 5567 'Replacing "%+f", "%-f", and "% f" and specifying a sign:\n'
jpayne@69 5568 '\n'
jpayne@69 5569 " >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it "
jpayne@69 5570 'always\n'
jpayne@69 5571 " '+3.140000; -3.140000'\n"
jpayne@69 5572 " >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space "
jpayne@69 5573 'for positive numbers\n'
jpayne@69 5574 " ' 3.140000; -3.140000'\n"
jpayne@69 5575 " >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the "
jpayne@69 5576 "minus -- same as '{:f}; {:f}'\n"
jpayne@69 5577 " '3.140000; -3.140000'\n"
jpayne@69 5578 '\n'
jpayne@69 5579 'Replacing "%x" and "%o" and converting the value to '
jpayne@69 5580 'different bases:\n'
jpayne@69 5581 '\n'
jpayne@69 5582 ' >>> # format also supports binary numbers\n'
jpayne@69 5583 ' >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: '
jpayne@69 5584 '{0:b}".format(42)\n'
jpayne@69 5585 " 'int: 42; hex: 2a; oct: 52; bin: 101010'\n"
jpayne@69 5586 ' >>> # with 0x, 0o, or 0b as prefix:\n'
jpayne@69 5587 ' >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: '
jpayne@69 5588 '{0:#b}".format(42)\n'
jpayne@69 5589 " 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'\n"
jpayne@69 5590 '\n'
jpayne@69 5591 'Using the comma as a thousands separator:\n'
jpayne@69 5592 '\n'
jpayne@69 5593 " >>> '{:,}'.format(1234567890)\n"
jpayne@69 5594 " '1,234,567,890'\n"
jpayne@69 5595 '\n'
jpayne@69 5596 'Expressing a percentage:\n'
jpayne@69 5597 '\n'
jpayne@69 5598 ' >>> points = 19\n'
jpayne@69 5599 ' >>> total = 22\n'
jpayne@69 5600 " >>> 'Correct answers: {:.2%}'.format(points/total)\n"
jpayne@69 5601 " 'Correct answers: 86.36%'\n"
jpayne@69 5602 '\n'
jpayne@69 5603 'Using type-specific formatting:\n'
jpayne@69 5604 '\n'
jpayne@69 5605 ' >>> import datetime\n'
jpayne@69 5606 ' >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)\n'
jpayne@69 5607 " >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)\n"
jpayne@69 5608 " '2010-07-04 12:15:58'\n"
jpayne@69 5609 '\n'
jpayne@69 5610 'Nesting arguments and more complex examples:\n'
jpayne@69 5611 '\n'
jpayne@69 5612 " >>> for align, text in zip('<^>', ['left', 'center', "
jpayne@69 5613 "'right']):\n"
jpayne@69 5614 " ... '{0:{fill}{align}16}'.format(text, fill=align, "
jpayne@69 5615 'align=align)\n'
jpayne@69 5616 ' ...\n'
jpayne@69 5617 " 'left<<<<<<<<<<<<'\n"
jpayne@69 5618 " '^^^^^center^^^^^'\n"
jpayne@69 5619 " '>>>>>>>>>>>right'\n"
jpayne@69 5620 ' >>>\n'
jpayne@69 5621 ' >>> octets = [192, 168, 0, 1]\n'
jpayne@69 5622 " >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)\n"
jpayne@69 5623 " 'C0A80001'\n"
jpayne@69 5624 ' >>> int(_, 16)\n'
jpayne@69 5625 ' 3232235521\n'
jpayne@69 5626 ' >>>\n'
jpayne@69 5627 ' >>> width = 5\n'
jpayne@69 5628 ' >>> for num in range(5,12): \n'
jpayne@69 5629 " ... for base in 'dXob':\n"
jpayne@69 5630 " ... print('{0:{width}{base}}'.format(num, "
jpayne@69 5631 "base=base, width=width), end=' ')\n"
jpayne@69 5632 ' ... print()\n'
jpayne@69 5633 ' ...\n'
jpayne@69 5634 ' 5 5 5 101\n'
jpayne@69 5635 ' 6 6 6 110\n'
jpayne@69 5636 ' 7 7 7 111\n'
jpayne@69 5637 ' 8 8 10 1000\n'
jpayne@69 5638 ' 9 9 11 1001\n'
jpayne@69 5639 ' 10 A 12 1010\n'
jpayne@69 5640 ' 11 B 13 1011\n',
jpayne@69 5641 'function': 'Function definitions\n'
jpayne@69 5642 '********************\n'
jpayne@69 5643 '\n'
jpayne@69 5644 'A function definition defines a user-defined function object '
jpayne@69 5645 '(see\n'
jpayne@69 5646 'section The standard type hierarchy):\n'
jpayne@69 5647 '\n'
jpayne@69 5648 ' funcdef ::= [decorators] "def" funcname "(" '
jpayne@69 5649 '[parameter_list] ")"\n'
jpayne@69 5650 ' ["->" expression] ":" suite\n'
jpayne@69 5651 ' decorators ::= decorator+\n'
jpayne@69 5652 ' decorator ::= "@" dotted_name ["(" '
jpayne@69 5653 '[argument_list [","]] ")"] NEWLINE\n'
jpayne@69 5654 ' dotted_name ::= identifier ("." identifier)*\n'
jpayne@69 5655 ' parameter_list ::= defparameter ("," '
jpayne@69 5656 'defparameter)* "," "/" ["," [parameter_list_no_posonly]]\n'
jpayne@69 5657 ' | parameter_list_no_posonly\n'
jpayne@69 5658 ' parameter_list_no_posonly ::= defparameter ("," '
jpayne@69 5659 'defparameter)* ["," [parameter_list_starargs]]\n'
jpayne@69 5660 ' | parameter_list_starargs\n'
jpayne@69 5661 ' parameter_list_starargs ::= "*" [parameter] ("," '
jpayne@69 5662 'defparameter)* ["," ["**" parameter [","]]]\n'
jpayne@69 5663 ' | "**" parameter [","]\n'
jpayne@69 5664 ' parameter ::= identifier [":" expression]\n'
jpayne@69 5665 ' defparameter ::= parameter ["=" expression]\n'
jpayne@69 5666 ' funcname ::= identifier\n'
jpayne@69 5667 '\n'
jpayne@69 5668 'A function definition is an executable statement. Its execution '
jpayne@69 5669 'binds\n'
jpayne@69 5670 'the function name in the current local namespace to a function '
jpayne@69 5671 'object\n'
jpayne@69 5672 '(a wrapper around the executable code for the function). This\n'
jpayne@69 5673 'function object contains a reference to the current global '
jpayne@69 5674 'namespace\n'
jpayne@69 5675 'as the global namespace to be used when the function is called.\n'
jpayne@69 5676 '\n'
jpayne@69 5677 'The function definition does not execute the function body; this '
jpayne@69 5678 'gets\n'
jpayne@69 5679 'executed only when the function is called. [2]\n'
jpayne@69 5680 '\n'
jpayne@69 5681 'A function definition may be wrapped by one or more *decorator*\n'
jpayne@69 5682 'expressions. Decorator expressions are evaluated when the '
jpayne@69 5683 'function is\n'
jpayne@69 5684 'defined, in the scope that contains the function definition. '
jpayne@69 5685 'The\n'
jpayne@69 5686 'result must be a callable, which is invoked with the function '
jpayne@69 5687 'object\n'
jpayne@69 5688 'as the only argument. The returned value is bound to the '
jpayne@69 5689 'function name\n'
jpayne@69 5690 'instead of the function object. Multiple decorators are applied '
jpayne@69 5691 'in\n'
jpayne@69 5692 'nested fashion. For example, the following code\n'
jpayne@69 5693 '\n'
jpayne@69 5694 ' @f1(arg)\n'
jpayne@69 5695 ' @f2\n'
jpayne@69 5696 ' def func(): pass\n'
jpayne@69 5697 '\n'
jpayne@69 5698 'is roughly equivalent to\n'
jpayne@69 5699 '\n'
jpayne@69 5700 ' def func(): pass\n'
jpayne@69 5701 ' func = f1(arg)(f2(func))\n'
jpayne@69 5702 '\n'
jpayne@69 5703 'except that the original function is not temporarily bound to '
jpayne@69 5704 'the name\n'
jpayne@69 5705 '"func".\n'
jpayne@69 5706 '\n'
jpayne@69 5707 'When one or more *parameters* have the form *parameter* "="\n'
jpayne@69 5708 '*expression*, the function is said to have “default parameter '
jpayne@69 5709 'values.”\n'
jpayne@69 5710 'For a parameter with a default value, the corresponding '
jpayne@69 5711 '*argument* may\n'
jpayne@69 5712 'be omitted from a call, in which case the parameter’s default '
jpayne@69 5713 'value is\n'
jpayne@69 5714 'substituted. If a parameter has a default value, all following\n'
jpayne@69 5715 'parameters up until the “"*"” must also have a default value — '
jpayne@69 5716 'this is\n'
jpayne@69 5717 'a syntactic restriction that is not expressed by the grammar.\n'
jpayne@69 5718 '\n'
jpayne@69 5719 '**Default parameter values are evaluated from left to right when '
jpayne@69 5720 'the\n'
jpayne@69 5721 'function definition is executed.** This means that the '
jpayne@69 5722 'expression is\n'
jpayne@69 5723 'evaluated once, when the function is defined, and that the same '
jpayne@69 5724 '“pre-\n'
jpayne@69 5725 'computed” value is used for each call. This is especially '
jpayne@69 5726 'important\n'
jpayne@69 5727 'to understand when a default parameter is a mutable object, such '
jpayne@69 5728 'as a\n'
jpayne@69 5729 'list or a dictionary: if the function modifies the object (e.g. '
jpayne@69 5730 'by\n'
jpayne@69 5731 'appending an item to a list), the default value is in effect '
jpayne@69 5732 'modified.\n'
jpayne@69 5733 'This is generally not what was intended. A way around this is '
jpayne@69 5734 'to use\n'
jpayne@69 5735 '"None" as the default, and explicitly test for it in the body of '
jpayne@69 5736 'the\n'
jpayne@69 5737 'function, e.g.:\n'
jpayne@69 5738 '\n'
jpayne@69 5739 ' def whats_on_the_telly(penguin=None):\n'
jpayne@69 5740 ' if penguin is None:\n'
jpayne@69 5741 ' penguin = []\n'
jpayne@69 5742 ' penguin.append("property of the zoo")\n'
jpayne@69 5743 ' return penguin\n'
jpayne@69 5744 '\n'
jpayne@69 5745 'Function call semantics are described in more detail in section '
jpayne@69 5746 'Calls.\n'
jpayne@69 5747 'A function call always assigns values to all parameters '
jpayne@69 5748 'mentioned in\n'
jpayne@69 5749 'the parameter list, either from position arguments, from '
jpayne@69 5750 'keyword\n'
jpayne@69 5751 'arguments, or from default values. If the form “"*identifier"” '
jpayne@69 5752 'is\n'
jpayne@69 5753 'present, it is initialized to a tuple receiving any excess '
jpayne@69 5754 'positional\n'
jpayne@69 5755 'parameters, defaulting to the empty tuple. If the form\n'
jpayne@69 5756 '“"**identifier"” is present, it is initialized to a new ordered\n'
jpayne@69 5757 'mapping receiving any excess keyword arguments, defaulting to a '
jpayne@69 5758 'new\n'
jpayne@69 5759 'empty mapping of the same type. Parameters after “"*"” or\n'
jpayne@69 5760 '“"*identifier"” are keyword-only parameters and may only be '
jpayne@69 5761 'passed\n'
jpayne@69 5762 'used keyword arguments.\n'
jpayne@69 5763 '\n'
jpayne@69 5764 'Parameters may have an *annotation* of the form “": '
jpayne@69 5765 'expression"”\n'
jpayne@69 5766 'following the parameter name. Any parameter may have an '
jpayne@69 5767 'annotation,\n'
jpayne@69 5768 'even those of the form "*identifier" or "**identifier". '
jpayne@69 5769 'Functions may\n'
jpayne@69 5770 'have “return” annotation of the form “"-> expression"” after '
jpayne@69 5771 'the\n'
jpayne@69 5772 'parameter list. These annotations can be any valid Python '
jpayne@69 5773 'expression.\n'
jpayne@69 5774 'The presence of annotations does not change the semantics of a\n'
jpayne@69 5775 'function. The annotation values are available as values of a\n'
jpayne@69 5776 'dictionary keyed by the parameters’ names in the '
jpayne@69 5777 '"__annotations__"\n'
jpayne@69 5778 'attribute of the function object. If the "annotations" import '
jpayne@69 5779 'from\n'
jpayne@69 5780 '"__future__" is used, annotations are preserved as strings at '
jpayne@69 5781 'runtime\n'
jpayne@69 5782 'which enables postponed evaluation. Otherwise, they are '
jpayne@69 5783 'evaluated\n'
jpayne@69 5784 'when the function definition is executed. In this case '
jpayne@69 5785 'annotations\n'
jpayne@69 5786 'may be evaluated in a different order than they appear in the '
jpayne@69 5787 'source\n'
jpayne@69 5788 'code.\n'
jpayne@69 5789 '\n'
jpayne@69 5790 'It is also possible to create anonymous functions (functions not '
jpayne@69 5791 'bound\n'
jpayne@69 5792 'to a name), for immediate use in expressions. This uses lambda\n'
jpayne@69 5793 'expressions, described in section Lambdas. Note that the '
jpayne@69 5794 'lambda\n'
jpayne@69 5795 'expression is merely a shorthand for a simplified function '
jpayne@69 5796 'definition;\n'
jpayne@69 5797 'a function defined in a “"def"” statement can be passed around '
jpayne@69 5798 'or\n'
jpayne@69 5799 'assigned to another name just like a function defined by a '
jpayne@69 5800 'lambda\n'
jpayne@69 5801 'expression. The “"def"” form is actually more powerful since '
jpayne@69 5802 'it\n'
jpayne@69 5803 'allows the execution of multiple statements and annotations.\n'
jpayne@69 5804 '\n'
jpayne@69 5805 '**Programmer’s note:** Functions are first-class objects. A '
jpayne@69 5806 '“"def"”\n'
jpayne@69 5807 'statement executed inside a function definition defines a local\n'
jpayne@69 5808 'function that can be returned or passed around. Free variables '
jpayne@69 5809 'used\n'
jpayne@69 5810 'in the nested function can access the local variables of the '
jpayne@69 5811 'function\n'
jpayne@69 5812 'containing the def. See section Naming and binding for '
jpayne@69 5813 'details.\n'
jpayne@69 5814 '\n'
jpayne@69 5815 'See also:\n'
jpayne@69 5816 '\n'
jpayne@69 5817 ' **PEP 3107** - Function Annotations\n'
jpayne@69 5818 ' The original specification for function annotations.\n'
jpayne@69 5819 '\n'
jpayne@69 5820 ' **PEP 484** - Type Hints\n'
jpayne@69 5821 ' Definition of a standard meaning for annotations: type '
jpayne@69 5822 'hints.\n'
jpayne@69 5823 '\n'
jpayne@69 5824 ' **PEP 526** - Syntax for Variable Annotations\n'
jpayne@69 5825 ' Ability to type hint variable declarations, including '
jpayne@69 5826 'class\n'
jpayne@69 5827 ' variables and instance variables\n'
jpayne@69 5828 '\n'
jpayne@69 5829 ' **PEP 563** - Postponed Evaluation of Annotations\n'
jpayne@69 5830 ' Support for forward references within annotations by '
jpayne@69 5831 'preserving\n'
jpayne@69 5832 ' annotations in a string form at runtime instead of eager\n'
jpayne@69 5833 ' evaluation.\n',
jpayne@69 5834 'global': 'The "global" statement\n'
jpayne@69 5835 '**********************\n'
jpayne@69 5836 '\n'
jpayne@69 5837 ' global_stmt ::= "global" identifier ("," identifier)*\n'
jpayne@69 5838 '\n'
jpayne@69 5839 'The "global" statement is a declaration which holds for the '
jpayne@69 5840 'entire\n'
jpayne@69 5841 'current code block. It means that the listed identifiers are to '
jpayne@69 5842 'be\n'
jpayne@69 5843 'interpreted as globals. It would be impossible to assign to a '
jpayne@69 5844 'global\n'
jpayne@69 5845 'variable without "global", although free variables may refer to\n'
jpayne@69 5846 'globals without being declared global.\n'
jpayne@69 5847 '\n'
jpayne@69 5848 'Names listed in a "global" statement must not be used in the same '
jpayne@69 5849 'code\n'
jpayne@69 5850 'block textually preceding that "global" statement.\n'
jpayne@69 5851 '\n'
jpayne@69 5852 'Names listed in a "global" statement must not be defined as '
jpayne@69 5853 'formal\n'
jpayne@69 5854 'parameters or in a "for" loop control target, "class" definition,\n'
jpayne@69 5855 'function definition, "import" statement, or variable annotation.\n'
jpayne@69 5856 '\n'
jpayne@69 5857 '**CPython implementation detail:** The current implementation does '
jpayne@69 5858 'not\n'
jpayne@69 5859 'enforce some of these restrictions, but programs should not abuse '
jpayne@69 5860 'this\n'
jpayne@69 5861 'freedom, as future implementations may enforce them or silently '
jpayne@69 5862 'change\n'
jpayne@69 5863 'the meaning of the program.\n'
jpayne@69 5864 '\n'
jpayne@69 5865 '**Programmer’s note:** "global" is a directive to the parser. It\n'
jpayne@69 5866 'applies only to code parsed at the same time as the "global"\n'
jpayne@69 5867 'statement. In particular, a "global" statement contained in a '
jpayne@69 5868 'string\n'
jpayne@69 5869 'or code object supplied to the built-in "exec()" function does '
jpayne@69 5870 'not\n'
jpayne@69 5871 'affect the code block *containing* the function call, and code\n'
jpayne@69 5872 'contained in such a string is unaffected by "global" statements in '
jpayne@69 5873 'the\n'
jpayne@69 5874 'code containing the function call. The same applies to the '
jpayne@69 5875 '"eval()"\n'
jpayne@69 5876 'and "compile()" functions.\n',
jpayne@69 5877 'id-classes': 'Reserved classes of identifiers\n'
jpayne@69 5878 '*******************************\n'
jpayne@69 5879 '\n'
jpayne@69 5880 'Certain classes of identifiers (besides keywords) have '
jpayne@69 5881 'special\n'
jpayne@69 5882 'meanings. These classes are identified by the patterns of '
jpayne@69 5883 'leading and\n'
jpayne@69 5884 'trailing underscore characters:\n'
jpayne@69 5885 '\n'
jpayne@69 5886 '"_*"\n'
jpayne@69 5887 ' Not imported by "from module import *". The special '
jpayne@69 5888 'identifier "_"\n'
jpayne@69 5889 ' is used in the interactive interpreter to store the result '
jpayne@69 5890 'of the\n'
jpayne@69 5891 ' last evaluation; it is stored in the "builtins" module. '
jpayne@69 5892 'When not\n'
jpayne@69 5893 ' in interactive mode, "_" has no special meaning and is not '
jpayne@69 5894 'defined.\n'
jpayne@69 5895 ' See section The import statement.\n'
jpayne@69 5896 '\n'
jpayne@69 5897 ' Note: The name "_" is often used in conjunction with\n'
jpayne@69 5898 ' internationalization; refer to the documentation for the\n'
jpayne@69 5899 ' "gettext" module for more information on this '
jpayne@69 5900 'convention.\n'
jpayne@69 5901 '\n'
jpayne@69 5902 '"__*__"\n'
jpayne@69 5903 ' System-defined names. These names are defined by the '
jpayne@69 5904 'interpreter\n'
jpayne@69 5905 ' and its implementation (including the standard library). '
jpayne@69 5906 'Current\n'
jpayne@69 5907 ' system names are discussed in the Special method names '
jpayne@69 5908 'section and\n'
jpayne@69 5909 ' elsewhere. More will likely be defined in future versions '
jpayne@69 5910 'of\n'
jpayne@69 5911 ' Python. *Any* use of "__*__" names, in any context, that '
jpayne@69 5912 'does not\n'
jpayne@69 5913 ' follow explicitly documented use, is subject to breakage '
jpayne@69 5914 'without\n'
jpayne@69 5915 ' warning.\n'
jpayne@69 5916 '\n'
jpayne@69 5917 '"__*"\n'
jpayne@69 5918 ' Class-private names. Names in this category, when used '
jpayne@69 5919 'within the\n'
jpayne@69 5920 ' context of a class definition, are re-written to use a '
jpayne@69 5921 'mangled form\n'
jpayne@69 5922 ' to help avoid name clashes between “private” attributes of '
jpayne@69 5923 'base and\n'
jpayne@69 5924 ' derived classes. See section Identifiers (Names).\n',
jpayne@69 5925 'identifiers': 'Identifiers and keywords\n'
jpayne@69 5926 '************************\n'
jpayne@69 5927 '\n'
jpayne@69 5928 'Identifiers (also referred to as *names*) are described by '
jpayne@69 5929 'the\n'
jpayne@69 5930 'following lexical definitions.\n'
jpayne@69 5931 '\n'
jpayne@69 5932 'The syntax of identifiers in Python is based on the Unicode '
jpayne@69 5933 'standard\n'
jpayne@69 5934 'annex UAX-31, with elaboration and changes as defined below; '
jpayne@69 5935 'see also\n'
jpayne@69 5936 '**PEP 3131** for further details.\n'
jpayne@69 5937 '\n'
jpayne@69 5938 'Within the ASCII range (U+0001..U+007F), the valid characters '
jpayne@69 5939 'for\n'
jpayne@69 5940 'identifiers are the same as in Python 2.x: the uppercase and '
jpayne@69 5941 'lowercase\n'
jpayne@69 5942 'letters "A" through "Z", the underscore "_" and, except for '
jpayne@69 5943 'the first\n'
jpayne@69 5944 'character, the digits "0" through "9".\n'
jpayne@69 5945 '\n'
jpayne@69 5946 'Python 3.0 introduces additional characters from outside the '
jpayne@69 5947 'ASCII\n'
jpayne@69 5948 'range (see **PEP 3131**). For these characters, the '
jpayne@69 5949 'classification\n'
jpayne@69 5950 'uses the version of the Unicode Character Database as '
jpayne@69 5951 'included in the\n'
jpayne@69 5952 '"unicodedata" module.\n'
jpayne@69 5953 '\n'
jpayne@69 5954 'Identifiers are unlimited in length. Case is significant.\n'
jpayne@69 5955 '\n'
jpayne@69 5956 ' identifier ::= xid_start xid_continue*\n'
jpayne@69 5957 ' id_start ::= <all characters in general categories Lu, '
jpayne@69 5958 'Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the '
jpayne@69 5959 'Other_ID_Start property>\n'
jpayne@69 5960 ' id_continue ::= <all characters in id_start, plus '
jpayne@69 5961 'characters in the categories Mn, Mc, Nd, Pc and others with '
jpayne@69 5962 'the Other_ID_Continue property>\n'
jpayne@69 5963 ' xid_start ::= <all characters in id_start whose NFKC '
jpayne@69 5964 'normalization is in "id_start xid_continue*">\n'
jpayne@69 5965 ' xid_continue ::= <all characters in id_continue whose NFKC '
jpayne@69 5966 'normalization is in "id_continue*">\n'
jpayne@69 5967 '\n'
jpayne@69 5968 'The Unicode category codes mentioned above stand for:\n'
jpayne@69 5969 '\n'
jpayne@69 5970 '* *Lu* - uppercase letters\n'
jpayne@69 5971 '\n'
jpayne@69 5972 '* *Ll* - lowercase letters\n'
jpayne@69 5973 '\n'
jpayne@69 5974 '* *Lt* - titlecase letters\n'
jpayne@69 5975 '\n'
jpayne@69 5976 '* *Lm* - modifier letters\n'
jpayne@69 5977 '\n'
jpayne@69 5978 '* *Lo* - other letters\n'
jpayne@69 5979 '\n'
jpayne@69 5980 '* *Nl* - letter numbers\n'
jpayne@69 5981 '\n'
jpayne@69 5982 '* *Mn* - nonspacing marks\n'
jpayne@69 5983 '\n'
jpayne@69 5984 '* *Mc* - spacing combining marks\n'
jpayne@69 5985 '\n'
jpayne@69 5986 '* *Nd* - decimal numbers\n'
jpayne@69 5987 '\n'
jpayne@69 5988 '* *Pc* - connector punctuations\n'
jpayne@69 5989 '\n'
jpayne@69 5990 '* *Other_ID_Start* - explicit list of characters in '
jpayne@69 5991 'PropList.txt to\n'
jpayne@69 5992 ' support backwards compatibility\n'
jpayne@69 5993 '\n'
jpayne@69 5994 '* *Other_ID_Continue* - likewise\n'
jpayne@69 5995 '\n'
jpayne@69 5996 'All identifiers are converted into the normal form NFKC while '
jpayne@69 5997 'parsing;\n'
jpayne@69 5998 'comparison of identifiers is based on NFKC.\n'
jpayne@69 5999 '\n'
jpayne@69 6000 'A non-normative HTML file listing all valid identifier '
jpayne@69 6001 'characters for\n'
jpayne@69 6002 'Unicode 4.1 can be found at https://www.dcl.hpi.uni-\n'
jpayne@69 6003 'potsdam.de/home/loewis/table-3131.html.\n'
jpayne@69 6004 '\n'
jpayne@69 6005 '\n'
jpayne@69 6006 'Keywords\n'
jpayne@69 6007 '========\n'
jpayne@69 6008 '\n'
jpayne@69 6009 'The following identifiers are used as reserved words, or '
jpayne@69 6010 '*keywords* of\n'
jpayne@69 6011 'the language, and cannot be used as ordinary identifiers. '
jpayne@69 6012 'They must\n'
jpayne@69 6013 'be spelled exactly as written here:\n'
jpayne@69 6014 '\n'
jpayne@69 6015 ' False await else import pass\n'
jpayne@69 6016 ' None break except in raise\n'
jpayne@69 6017 ' True class finally is return\n'
jpayne@69 6018 ' and continue for lambda try\n'
jpayne@69 6019 ' as def from nonlocal while\n'
jpayne@69 6020 ' assert del global not with\n'
jpayne@69 6021 ' async elif if or yield\n'
jpayne@69 6022 '\n'
jpayne@69 6023 '\n'
jpayne@69 6024 'Reserved classes of identifiers\n'
jpayne@69 6025 '===============================\n'
jpayne@69 6026 '\n'
jpayne@69 6027 'Certain classes of identifiers (besides keywords) have '
jpayne@69 6028 'special\n'
jpayne@69 6029 'meanings. These classes are identified by the patterns of '
jpayne@69 6030 'leading and\n'
jpayne@69 6031 'trailing underscore characters:\n'
jpayne@69 6032 '\n'
jpayne@69 6033 '"_*"\n'
jpayne@69 6034 ' Not imported by "from module import *". The special '
jpayne@69 6035 'identifier "_"\n'
jpayne@69 6036 ' is used in the interactive interpreter to store the result '
jpayne@69 6037 'of the\n'
jpayne@69 6038 ' last evaluation; it is stored in the "builtins" module. '
jpayne@69 6039 'When not\n'
jpayne@69 6040 ' in interactive mode, "_" has no special meaning and is not '
jpayne@69 6041 'defined.\n'
jpayne@69 6042 ' See section The import statement.\n'
jpayne@69 6043 '\n'
jpayne@69 6044 ' Note: The name "_" is often used in conjunction with\n'
jpayne@69 6045 ' internationalization; refer to the documentation for '
jpayne@69 6046 'the\n'
jpayne@69 6047 ' "gettext" module for more information on this '
jpayne@69 6048 'convention.\n'
jpayne@69 6049 '\n'
jpayne@69 6050 '"__*__"\n'
jpayne@69 6051 ' System-defined names. These names are defined by the '
jpayne@69 6052 'interpreter\n'
jpayne@69 6053 ' and its implementation (including the standard library). '
jpayne@69 6054 'Current\n'
jpayne@69 6055 ' system names are discussed in the Special method names '
jpayne@69 6056 'section and\n'
jpayne@69 6057 ' elsewhere. More will likely be defined in future versions '
jpayne@69 6058 'of\n'
jpayne@69 6059 ' Python. *Any* use of "__*__" names, in any context, that '
jpayne@69 6060 'does not\n'
jpayne@69 6061 ' follow explicitly documented use, is subject to breakage '
jpayne@69 6062 'without\n'
jpayne@69 6063 ' warning.\n'
jpayne@69 6064 '\n'
jpayne@69 6065 '"__*"\n'
jpayne@69 6066 ' Class-private names. Names in this category, when used '
jpayne@69 6067 'within the\n'
jpayne@69 6068 ' context of a class definition, are re-written to use a '
jpayne@69 6069 'mangled form\n'
jpayne@69 6070 ' to help avoid name clashes between “private” attributes of '
jpayne@69 6071 'base and\n'
jpayne@69 6072 ' derived classes. See section Identifiers (Names).\n',
jpayne@69 6073 'if': 'The "if" statement\n'
jpayne@69 6074 '******************\n'
jpayne@69 6075 '\n'
jpayne@69 6076 'The "if" statement is used for conditional execution:\n'
jpayne@69 6077 '\n'
jpayne@69 6078 ' if_stmt ::= "if" expression ":" suite\n'
jpayne@69 6079 ' ("elif" expression ":" suite)*\n'
jpayne@69 6080 ' ["else" ":" suite]\n'
jpayne@69 6081 '\n'
jpayne@69 6082 'It selects exactly one of the suites by evaluating the expressions '
jpayne@69 6083 'one\n'
jpayne@69 6084 'by one until one is found to be true (see section Boolean operations\n'
jpayne@69 6085 'for the definition of true and false); then that suite is executed\n'
jpayne@69 6086 '(and no other part of the "if" statement is executed or evaluated).\n'
jpayne@69 6087 'If all expressions are false, the suite of the "else" clause, if\n'
jpayne@69 6088 'present, is executed.\n',
jpayne@69 6089 'imaginary': 'Imaginary literals\n'
jpayne@69 6090 '******************\n'
jpayne@69 6091 '\n'
jpayne@69 6092 'Imaginary literals are described by the following lexical '
jpayne@69 6093 'definitions:\n'
jpayne@69 6094 '\n'
jpayne@69 6095 ' imagnumber ::= (floatnumber | digitpart) ("j" | "J")\n'
jpayne@69 6096 '\n'
jpayne@69 6097 'An imaginary literal yields a complex number with a real part '
jpayne@69 6098 'of 0.0.\n'
jpayne@69 6099 'Complex numbers are represented as a pair of floating point '
jpayne@69 6100 'numbers\n'
jpayne@69 6101 'and have the same restrictions on their range. To create a '
jpayne@69 6102 'complex\n'
jpayne@69 6103 'number with a nonzero real part, add a floating point number to '
jpayne@69 6104 'it,\n'
jpayne@69 6105 'e.g., "(3+4j)". Some examples of imaginary literals:\n'
jpayne@69 6106 '\n'
jpayne@69 6107 ' 3.14j 10.j 10j .001j 1e100j 3.14e-10j '
jpayne@69 6108 '3.14_15_93j\n',
jpayne@69 6109 'import': 'The "import" statement\n'
jpayne@69 6110 '**********************\n'
jpayne@69 6111 '\n'
jpayne@69 6112 ' import_stmt ::= "import" module ["as" identifier] ("," '
jpayne@69 6113 'module ["as" identifier])*\n'
jpayne@69 6114 ' | "from" relative_module "import" identifier '
jpayne@69 6115 '["as" identifier]\n'
jpayne@69 6116 ' ("," identifier ["as" identifier])*\n'
jpayne@69 6117 ' | "from" relative_module "import" "(" '
jpayne@69 6118 'identifier ["as" identifier]\n'
jpayne@69 6119 ' ("," identifier ["as" identifier])* [","] ")"\n'
jpayne@69 6120 ' | "from" module "import" "*"\n'
jpayne@69 6121 ' module ::= (identifier ".")* identifier\n'
jpayne@69 6122 ' relative_module ::= "."* module | "."+\n'
jpayne@69 6123 '\n'
jpayne@69 6124 'The basic import statement (no "from" clause) is executed in two\n'
jpayne@69 6125 'steps:\n'
jpayne@69 6126 '\n'
jpayne@69 6127 '1. find a module, loading and initializing it if necessary\n'
jpayne@69 6128 '\n'
jpayne@69 6129 '2. define a name or names in the local namespace for the scope\n'
jpayne@69 6130 ' where the "import" statement occurs.\n'
jpayne@69 6131 '\n'
jpayne@69 6132 'When the statement contains multiple clauses (separated by commas) '
jpayne@69 6133 'the\n'
jpayne@69 6134 'two steps are carried out separately for each clause, just as '
jpayne@69 6135 'though\n'
jpayne@69 6136 'the clauses had been separated out into individual import '
jpayne@69 6137 'statements.\n'
jpayne@69 6138 '\n'
jpayne@69 6139 'The details of the first step, finding and loading modules are\n'
jpayne@69 6140 'described in greater detail in the section on the import system, '
jpayne@69 6141 'which\n'
jpayne@69 6142 'also describes the various types of packages and modules that can '
jpayne@69 6143 'be\n'
jpayne@69 6144 'imported, as well as all the hooks that can be used to customize '
jpayne@69 6145 'the\n'
jpayne@69 6146 'import system. Note that failures in this step may indicate '
jpayne@69 6147 'either\n'
jpayne@69 6148 'that the module could not be located, *or* that an error occurred\n'
jpayne@69 6149 'while initializing the module, which includes execution of the\n'
jpayne@69 6150 'module’s code.\n'
jpayne@69 6151 '\n'
jpayne@69 6152 'If the requested module is retrieved successfully, it will be '
jpayne@69 6153 'made\n'
jpayne@69 6154 'available in the local namespace in one of three ways:\n'
jpayne@69 6155 '\n'
jpayne@69 6156 '* If the module name is followed by "as", then the name following\n'
jpayne@69 6157 ' "as" is bound directly to the imported module.\n'
jpayne@69 6158 '\n'
jpayne@69 6159 '* If no other name is specified, and the module being imported is '
jpayne@69 6160 'a\n'
jpayne@69 6161 ' top level module, the module’s name is bound in the local '
jpayne@69 6162 'namespace\n'
jpayne@69 6163 ' as a reference to the imported module\n'
jpayne@69 6164 '\n'
jpayne@69 6165 '* If the module being imported is *not* a top level module, then '
jpayne@69 6166 'the\n'
jpayne@69 6167 ' name of the top level package that contains the module is bound '
jpayne@69 6168 'in\n'
jpayne@69 6169 ' the local namespace as a reference to the top level package. '
jpayne@69 6170 'The\n'
jpayne@69 6171 ' imported module must be accessed using its full qualified name\n'
jpayne@69 6172 ' rather than directly\n'
jpayne@69 6173 '\n'
jpayne@69 6174 'The "from" form uses a slightly more complex process:\n'
jpayne@69 6175 '\n'
jpayne@69 6176 '1. find the module specified in the "from" clause, loading and\n'
jpayne@69 6177 ' initializing it if necessary;\n'
jpayne@69 6178 '\n'
jpayne@69 6179 '2. for each of the identifiers specified in the "import" clauses:\n'
jpayne@69 6180 '\n'
jpayne@69 6181 ' 1. check if the imported module has an attribute by that name\n'
jpayne@69 6182 '\n'
jpayne@69 6183 ' 2. if not, attempt to import a submodule with that name and '
jpayne@69 6184 'then\n'
jpayne@69 6185 ' check the imported module again for that attribute\n'
jpayne@69 6186 '\n'
jpayne@69 6187 ' 3. if the attribute is not found, "ImportError" is raised.\n'
jpayne@69 6188 '\n'
jpayne@69 6189 ' 4. otherwise, a reference to that value is stored in the local\n'
jpayne@69 6190 ' namespace, using the name in the "as" clause if it is '
jpayne@69 6191 'present,\n'
jpayne@69 6192 ' otherwise using the attribute name\n'
jpayne@69 6193 '\n'
jpayne@69 6194 'Examples:\n'
jpayne@69 6195 '\n'
jpayne@69 6196 ' import foo # foo imported and bound locally\n'
jpayne@69 6197 ' import foo.bar.baz # foo.bar.baz imported, foo bound '
jpayne@69 6198 'locally\n'
jpayne@69 6199 ' import foo.bar.baz as fbb # foo.bar.baz imported and bound as '
jpayne@69 6200 'fbb\n'
jpayne@69 6201 ' from foo.bar import baz # foo.bar.baz imported and bound as '
jpayne@69 6202 'baz\n'
jpayne@69 6203 ' from foo import attr # foo imported and foo.attr bound as '
jpayne@69 6204 'attr\n'
jpayne@69 6205 '\n'
jpayne@69 6206 'If the list of identifiers is replaced by a star ("\'*\'"), all '
jpayne@69 6207 'public\n'
jpayne@69 6208 'names defined in the module are bound in the local namespace for '
jpayne@69 6209 'the\n'
jpayne@69 6210 'scope where the "import" statement occurs.\n'
jpayne@69 6211 '\n'
jpayne@69 6212 'The *public names* defined by a module are determined by checking '
jpayne@69 6213 'the\n'
jpayne@69 6214 'module’s namespace for a variable named "__all__"; if defined, it '
jpayne@69 6215 'must\n'
jpayne@69 6216 'be a sequence of strings which are names defined or imported by '
jpayne@69 6217 'that\n'
jpayne@69 6218 'module. The names given in "__all__" are all considered public '
jpayne@69 6219 'and\n'
jpayne@69 6220 'are required to exist. If "__all__" is not defined, the set of '
jpayne@69 6221 'public\n'
jpayne@69 6222 'names includes all names found in the module’s namespace which do '
jpayne@69 6223 'not\n'
jpayne@69 6224 'begin with an underscore character ("\'_\'"). "__all__" should '
jpayne@69 6225 'contain\n'
jpayne@69 6226 'the entire public API. It is intended to avoid accidentally '
jpayne@69 6227 'exporting\n'
jpayne@69 6228 'items that are not part of the API (such as library modules which '
jpayne@69 6229 'were\n'
jpayne@69 6230 'imported and used within the module).\n'
jpayne@69 6231 '\n'
jpayne@69 6232 'The wild card form of import — "from module import *" — is only\n'
jpayne@69 6233 'allowed at the module level. Attempting to use it in class or\n'
jpayne@69 6234 'function definitions will raise a "SyntaxError".\n'
jpayne@69 6235 '\n'
jpayne@69 6236 'When specifying what module to import you do not have to specify '
jpayne@69 6237 'the\n'
jpayne@69 6238 'absolute name of the module. When a module or package is '
jpayne@69 6239 'contained\n'
jpayne@69 6240 'within another package it is possible to make a relative import '
jpayne@69 6241 'within\n'
jpayne@69 6242 'the same top package without having to mention the package name. '
jpayne@69 6243 'By\n'
jpayne@69 6244 'using leading dots in the specified module or package after "from" '
jpayne@69 6245 'you\n'
jpayne@69 6246 'can specify how high to traverse up the current package hierarchy\n'
jpayne@69 6247 'without specifying exact names. One leading dot means the current\n'
jpayne@69 6248 'package where the module making the import exists. Two dots means '
jpayne@69 6249 'up\n'
jpayne@69 6250 'one package level. Three dots is up two levels, etc. So if you '
jpayne@69 6251 'execute\n'
jpayne@69 6252 '"from . import mod" from a module in the "pkg" package then you '
jpayne@69 6253 'will\n'
jpayne@69 6254 'end up importing "pkg.mod". If you execute "from ..subpkg2 import '
jpayne@69 6255 'mod"\n'
jpayne@69 6256 'from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The\n'
jpayne@69 6257 'specification for relative imports is contained in the Package\n'
jpayne@69 6258 'Relative Imports section.\n'
jpayne@69 6259 '\n'
jpayne@69 6260 '"importlib.import_module()" is provided to support applications '
jpayne@69 6261 'that\n'
jpayne@69 6262 'determine dynamically the modules to be loaded.\n'
jpayne@69 6263 '\n'
jpayne@69 6264 'Raises an auditing event "import" with arguments "module", '
jpayne@69 6265 '"filename",\n'
jpayne@69 6266 '"sys.path", "sys.meta_path", "sys.path_hooks".\n'
jpayne@69 6267 '\n'
jpayne@69 6268 '\n'
jpayne@69 6269 'Future statements\n'
jpayne@69 6270 '=================\n'
jpayne@69 6271 '\n'
jpayne@69 6272 'A *future statement* is a directive to the compiler that a '
jpayne@69 6273 'particular\n'
jpayne@69 6274 'module should be compiled using syntax or semantics that will be\n'
jpayne@69 6275 'available in a specified future release of Python where the '
jpayne@69 6276 'feature\n'
jpayne@69 6277 'becomes standard.\n'
jpayne@69 6278 '\n'
jpayne@69 6279 'The future statement is intended to ease migration to future '
jpayne@69 6280 'versions\n'
jpayne@69 6281 'of Python that introduce incompatible changes to the language. '
jpayne@69 6282 'It\n'
jpayne@69 6283 'allows use of the new features on a per-module basis before the\n'
jpayne@69 6284 'release in which the feature becomes standard.\n'
jpayne@69 6285 '\n'
jpayne@69 6286 ' future_stmt ::= "from" "__future__" "import" feature ["as" '
jpayne@69 6287 'identifier]\n'
jpayne@69 6288 ' ("," feature ["as" identifier])*\n'
jpayne@69 6289 ' | "from" "__future__" "import" "(" feature '
jpayne@69 6290 '["as" identifier]\n'
jpayne@69 6291 ' ("," feature ["as" identifier])* [","] ")"\n'
jpayne@69 6292 ' feature ::= identifier\n'
jpayne@69 6293 '\n'
jpayne@69 6294 'A future statement must appear near the top of the module. The '
jpayne@69 6295 'only\n'
jpayne@69 6296 'lines that can appear before a future statement are:\n'
jpayne@69 6297 '\n'
jpayne@69 6298 '* the module docstring (if any),\n'
jpayne@69 6299 '\n'
jpayne@69 6300 '* comments,\n'
jpayne@69 6301 '\n'
jpayne@69 6302 '* blank lines, and\n'
jpayne@69 6303 '\n'
jpayne@69 6304 '* other future statements.\n'
jpayne@69 6305 '\n'
jpayne@69 6306 'The only feature in Python 3.7 that requires using the future\n'
jpayne@69 6307 'statement is "annotations".\n'
jpayne@69 6308 '\n'
jpayne@69 6309 'All historical features enabled by the future statement are still\n'
jpayne@69 6310 'recognized by Python 3. The list includes "absolute_import",\n'
jpayne@69 6311 '"division", "generators", "generator_stop", "unicode_literals",\n'
jpayne@69 6312 '"print_function", "nested_scopes" and "with_statement". They are '
jpayne@69 6313 'all\n'
jpayne@69 6314 'redundant because they are always enabled, and only kept for '
jpayne@69 6315 'backwards\n'
jpayne@69 6316 'compatibility.\n'
jpayne@69 6317 '\n'
jpayne@69 6318 'A future statement is recognized and treated specially at compile\n'
jpayne@69 6319 'time: Changes to the semantics of core constructs are often\n'
jpayne@69 6320 'implemented by generating different code. It may even be the '
jpayne@69 6321 'case\n'
jpayne@69 6322 'that a new feature introduces new incompatible syntax (such as a '
jpayne@69 6323 'new\n'
jpayne@69 6324 'reserved word), in which case the compiler may need to parse the\n'
jpayne@69 6325 'module differently. Such decisions cannot be pushed off until\n'
jpayne@69 6326 'runtime.\n'
jpayne@69 6327 '\n'
jpayne@69 6328 'For any given release, the compiler knows which feature names '
jpayne@69 6329 'have\n'
jpayne@69 6330 'been defined, and raises a compile-time error if a future '
jpayne@69 6331 'statement\n'
jpayne@69 6332 'contains a feature not known to it.\n'
jpayne@69 6333 '\n'
jpayne@69 6334 'The direct runtime semantics are the same as for any import '
jpayne@69 6335 'statement:\n'
jpayne@69 6336 'there is a standard module "__future__", described later, and it '
jpayne@69 6337 'will\n'
jpayne@69 6338 'be imported in the usual way at the time the future statement is\n'
jpayne@69 6339 'executed.\n'
jpayne@69 6340 '\n'
jpayne@69 6341 'The interesting runtime semantics depend on the specific feature\n'
jpayne@69 6342 'enabled by the future statement.\n'
jpayne@69 6343 '\n'
jpayne@69 6344 'Note that there is nothing special about the statement:\n'
jpayne@69 6345 '\n'
jpayne@69 6346 ' import __future__ [as name]\n'
jpayne@69 6347 '\n'
jpayne@69 6348 'That is not a future statement; it’s an ordinary import statement '
jpayne@69 6349 'with\n'
jpayne@69 6350 'no special semantics or syntax restrictions.\n'
jpayne@69 6351 '\n'
jpayne@69 6352 'Code compiled by calls to the built-in functions "exec()" and\n'
jpayne@69 6353 '"compile()" that occur in a module "M" containing a future '
jpayne@69 6354 'statement\n'
jpayne@69 6355 'will, by default, use the new syntax or semantics associated with '
jpayne@69 6356 'the\n'
jpayne@69 6357 'future statement. This can be controlled by optional arguments '
jpayne@69 6358 'to\n'
jpayne@69 6359 '"compile()" — see the documentation of that function for details.\n'
jpayne@69 6360 '\n'
jpayne@69 6361 'A future statement typed at an interactive interpreter prompt '
jpayne@69 6362 'will\n'
jpayne@69 6363 'take effect for the rest of the interpreter session. If an\n'
jpayne@69 6364 'interpreter is started with the "-i" option, is passed a script '
jpayne@69 6365 'name\n'
jpayne@69 6366 'to execute, and the script includes a future statement, it will be '
jpayne@69 6367 'in\n'
jpayne@69 6368 'effect in the interactive session started after the script is\n'
jpayne@69 6369 'executed.\n'
jpayne@69 6370 '\n'
jpayne@69 6371 'See also:\n'
jpayne@69 6372 '\n'
jpayne@69 6373 ' **PEP 236** - Back to the __future__\n'
jpayne@69 6374 ' The original proposal for the __future__ mechanism.\n',
jpayne@69 6375 'in': 'Membership test operations\n'
jpayne@69 6376 '**************************\n'
jpayne@69 6377 '\n'
jpayne@69 6378 'The operators "in" and "not in" test for membership. "x in s"\n'
jpayne@69 6379 'evaluates to "True" if *x* is a member of *s*, and "False" otherwise.\n'
jpayne@69 6380 '"x not in s" returns the negation of "x in s". All built-in '
jpayne@69 6381 'sequences\n'
jpayne@69 6382 'and set types support this as well as dictionary, for which "in" '
jpayne@69 6383 'tests\n'
jpayne@69 6384 'whether the dictionary has a given key. For container types such as\n'
jpayne@69 6385 'list, tuple, set, frozenset, dict, or collections.deque, the\n'
jpayne@69 6386 'expression "x in y" is equivalent to "any(x is e or x == e for e in\n'
jpayne@69 6387 'y)".\n'
jpayne@69 6388 '\n'
jpayne@69 6389 'For the string and bytes types, "x in y" is "True" if and only if *x*\n'
jpayne@69 6390 'is a substring of *y*. An equivalent test is "y.find(x) != -1".\n'
jpayne@69 6391 'Empty strings are always considered to be a substring of any other\n'
jpayne@69 6392 'string, so """ in "abc"" will return "True".\n'
jpayne@69 6393 '\n'
jpayne@69 6394 'For user-defined classes which define the "__contains__()" method, "x\n'
jpayne@69 6395 'in y" returns "True" if "y.__contains__(x)" returns a true value, and\n'
jpayne@69 6396 '"False" otherwise.\n'
jpayne@69 6397 '\n'
jpayne@69 6398 'For user-defined classes which do not define "__contains__()" but do\n'
jpayne@69 6399 'define "__iter__()", "x in y" is "True" if some value "z", for which\n'
jpayne@69 6400 'the expression "x is z or x == z" is true, is produced while '
jpayne@69 6401 'iterating\n'
jpayne@69 6402 'over "y". If an exception is raised during the iteration, it is as if\n'
jpayne@69 6403 '"in" raised that exception.\n'
jpayne@69 6404 '\n'
jpayne@69 6405 'Lastly, the old-style iteration protocol is tried: if a class defines\n'
jpayne@69 6406 '"__getitem__()", "x in y" is "True" if and only if there is a non-\n'
jpayne@69 6407 'negative integer index *i* such that "x is y[i] or x == y[i]", and no\n'
jpayne@69 6408 'lower integer index raises the "IndexError" exception. (If any other\n'
jpayne@69 6409 'exception is raised, it is as if "in" raised that exception).\n'
jpayne@69 6410 '\n'
jpayne@69 6411 'The operator "not in" is defined to have the inverse truth value of\n'
jpayne@69 6412 '"in".\n',
jpayne@69 6413 'integers': 'Integer literals\n'
jpayne@69 6414 '****************\n'
jpayne@69 6415 '\n'
jpayne@69 6416 'Integer literals are described by the following lexical '
jpayne@69 6417 'definitions:\n'
jpayne@69 6418 '\n'
jpayne@69 6419 ' integer ::= decinteger | bininteger | octinteger | '
jpayne@69 6420 'hexinteger\n'
jpayne@69 6421 ' decinteger ::= nonzerodigit (["_"] digit)* | "0"+ (["_"] '
jpayne@69 6422 '"0")*\n'
jpayne@69 6423 ' bininteger ::= "0" ("b" | "B") (["_"] bindigit)+\n'
jpayne@69 6424 ' octinteger ::= "0" ("o" | "O") (["_"] octdigit)+\n'
jpayne@69 6425 ' hexinteger ::= "0" ("x" | "X") (["_"] hexdigit)+\n'
jpayne@69 6426 ' nonzerodigit ::= "1"..."9"\n'
jpayne@69 6427 ' digit ::= "0"..."9"\n'
jpayne@69 6428 ' bindigit ::= "0" | "1"\n'
jpayne@69 6429 ' octdigit ::= "0"..."7"\n'
jpayne@69 6430 ' hexdigit ::= digit | "a"..."f" | "A"..."F"\n'
jpayne@69 6431 '\n'
jpayne@69 6432 'There is no limit for the length of integer literals apart from '
jpayne@69 6433 'what\n'
jpayne@69 6434 'can be stored in available memory.\n'
jpayne@69 6435 '\n'
jpayne@69 6436 'Underscores are ignored for determining the numeric value of '
jpayne@69 6437 'the\n'
jpayne@69 6438 'literal. They can be used to group digits for enhanced '
jpayne@69 6439 'readability.\n'
jpayne@69 6440 'One underscore can occur between digits, and after base '
jpayne@69 6441 'specifiers\n'
jpayne@69 6442 'like "0x".\n'
jpayne@69 6443 '\n'
jpayne@69 6444 'Note that leading zeros in a non-zero decimal number are not '
jpayne@69 6445 'allowed.\n'
jpayne@69 6446 'This is for disambiguation with C-style octal literals, which '
jpayne@69 6447 'Python\n'
jpayne@69 6448 'used before version 3.0.\n'
jpayne@69 6449 '\n'
jpayne@69 6450 'Some examples of integer literals:\n'
jpayne@69 6451 '\n'
jpayne@69 6452 ' 7 2147483647 0o177 0b100110111\n'
jpayne@69 6453 ' 3 79228162514264337593543950336 0o377 0xdeadbeef\n'
jpayne@69 6454 ' 100_000_000_000 0b_1110_0101\n'
jpayne@69 6455 '\n'
jpayne@69 6456 'Changed in version 3.6: Underscores are now allowed for '
jpayne@69 6457 'grouping\n'
jpayne@69 6458 'purposes in literals.\n',
jpayne@69 6459 'lambda': 'Lambdas\n'
jpayne@69 6460 '*******\n'
jpayne@69 6461 '\n'
jpayne@69 6462 ' lambda_expr ::= "lambda" [parameter_list] ":" '
jpayne@69 6463 'expression\n'
jpayne@69 6464 ' lambda_expr_nocond ::= "lambda" [parameter_list] ":" '
jpayne@69 6465 'expression_nocond\n'
jpayne@69 6466 '\n'
jpayne@69 6467 'Lambda expressions (sometimes called lambda forms) are used to '
jpayne@69 6468 'create\n'
jpayne@69 6469 'anonymous functions. The expression "lambda parameters: '
jpayne@69 6470 'expression"\n'
jpayne@69 6471 'yields a function object. The unnamed object behaves like a '
jpayne@69 6472 'function\n'
jpayne@69 6473 'object defined with:\n'
jpayne@69 6474 '\n'
jpayne@69 6475 ' def <lambda>(parameters):\n'
jpayne@69 6476 ' return expression\n'
jpayne@69 6477 '\n'
jpayne@69 6478 'See section Function definitions for the syntax of parameter '
jpayne@69 6479 'lists.\n'
jpayne@69 6480 'Note that functions created with lambda expressions cannot '
jpayne@69 6481 'contain\n'
jpayne@69 6482 'statements or annotations.\n',
jpayne@69 6483 'lists': 'List displays\n'
jpayne@69 6484 '*************\n'
jpayne@69 6485 '\n'
jpayne@69 6486 'A list display is a possibly empty series of expressions enclosed '
jpayne@69 6487 'in\n'
jpayne@69 6488 'square brackets:\n'
jpayne@69 6489 '\n'
jpayne@69 6490 ' list_display ::= "[" [starred_list | comprehension] "]"\n'
jpayne@69 6491 '\n'
jpayne@69 6492 'A list display yields a new list object, the contents being '
jpayne@69 6493 'specified\n'
jpayne@69 6494 'by either a list of expressions or a comprehension. When a comma-\n'
jpayne@69 6495 'separated list of expressions is supplied, its elements are '
jpayne@69 6496 'evaluated\n'
jpayne@69 6497 'from left to right and placed into the list object in that order.\n'
jpayne@69 6498 'When a comprehension is supplied, the list is constructed from the\n'
jpayne@69 6499 'elements resulting from the comprehension.\n',
jpayne@69 6500 'naming': 'Naming and binding\n'
jpayne@69 6501 '******************\n'
jpayne@69 6502 '\n'
jpayne@69 6503 '\n'
jpayne@69 6504 'Binding of names\n'
jpayne@69 6505 '================\n'
jpayne@69 6506 '\n'
jpayne@69 6507 '*Names* refer to objects. Names are introduced by name binding\n'
jpayne@69 6508 'operations.\n'
jpayne@69 6509 '\n'
jpayne@69 6510 'The following constructs bind names: formal parameters to '
jpayne@69 6511 'functions,\n'
jpayne@69 6512 '"import" statements, class and function definitions (these bind '
jpayne@69 6513 'the\n'
jpayne@69 6514 'class or function name in the defining block), and targets that '
jpayne@69 6515 'are\n'
jpayne@69 6516 'identifiers if occurring in an assignment, "for" loop header, or '
jpayne@69 6517 'after\n'
jpayne@69 6518 '"as" in a "with" statement or "except" clause. The "import" '
jpayne@69 6519 'statement\n'
jpayne@69 6520 'of the form "from ... import *" binds all names defined in the\n'
jpayne@69 6521 'imported module, except those beginning with an underscore. This '
jpayne@69 6522 'form\n'
jpayne@69 6523 'may only be used at the module level.\n'
jpayne@69 6524 '\n'
jpayne@69 6525 'A target occurring in a "del" statement is also considered bound '
jpayne@69 6526 'for\n'
jpayne@69 6527 'this purpose (though the actual semantics are to unbind the '
jpayne@69 6528 'name).\n'
jpayne@69 6529 '\n'
jpayne@69 6530 'Each assignment or import statement occurs within a block defined '
jpayne@69 6531 'by a\n'
jpayne@69 6532 'class or function definition or at the module level (the '
jpayne@69 6533 'top-level\n'
jpayne@69 6534 'code block).\n'
jpayne@69 6535 '\n'
jpayne@69 6536 'If a name is bound in a block, it is a local variable of that '
jpayne@69 6537 'block,\n'
jpayne@69 6538 'unless declared as "nonlocal" or "global". If a name is bound at '
jpayne@69 6539 'the\n'
jpayne@69 6540 'module level, it is a global variable. (The variables of the '
jpayne@69 6541 'module\n'
jpayne@69 6542 'code block are local and global.) If a variable is used in a '
jpayne@69 6543 'code\n'
jpayne@69 6544 'block but not defined there, it is a *free variable*.\n'
jpayne@69 6545 '\n'
jpayne@69 6546 'Each occurrence of a name in the program text refers to the '
jpayne@69 6547 '*binding*\n'
jpayne@69 6548 'of that name established by the following name resolution rules.\n'
jpayne@69 6549 '\n'
jpayne@69 6550 '\n'
jpayne@69 6551 'Resolution of names\n'
jpayne@69 6552 '===================\n'
jpayne@69 6553 '\n'
jpayne@69 6554 'A *scope* defines the visibility of a name within a block. If a '
jpayne@69 6555 'local\n'
jpayne@69 6556 'variable is defined in a block, its scope includes that block. If '
jpayne@69 6557 'the\n'
jpayne@69 6558 'definition occurs in a function block, the scope extends to any '
jpayne@69 6559 'blocks\n'
jpayne@69 6560 'contained within the defining one, unless a contained block '
jpayne@69 6561 'introduces\n'
jpayne@69 6562 'a different binding for the name.\n'
jpayne@69 6563 '\n'
jpayne@69 6564 'When a name is used in a code block, it is resolved using the '
jpayne@69 6565 'nearest\n'
jpayne@69 6566 'enclosing scope. The set of all such scopes visible to a code '
jpayne@69 6567 'block\n'
jpayne@69 6568 'is called the block’s *environment*.\n'
jpayne@69 6569 '\n'
jpayne@69 6570 'When a name is not found at all, a "NameError" exception is '
jpayne@69 6571 'raised. If\n'
jpayne@69 6572 'the current scope is a function scope, and the name refers to a '
jpayne@69 6573 'local\n'
jpayne@69 6574 'variable that has not yet been bound to a value at the point where '
jpayne@69 6575 'the\n'
jpayne@69 6576 'name is used, an "UnboundLocalError" exception is raised.\n'
jpayne@69 6577 '"UnboundLocalError" is a subclass of "NameError".\n'
jpayne@69 6578 '\n'
jpayne@69 6579 'If a name binding operation occurs anywhere within a code block, '
jpayne@69 6580 'all\n'
jpayne@69 6581 'uses of the name within the block are treated as references to '
jpayne@69 6582 'the\n'
jpayne@69 6583 'current block. This can lead to errors when a name is used within '
jpayne@69 6584 'a\n'
jpayne@69 6585 'block before it is bound. This rule is subtle. Python lacks\n'
jpayne@69 6586 'declarations and allows name binding operations to occur anywhere\n'
jpayne@69 6587 'within a code block. The local variables of a code block can be\n'
jpayne@69 6588 'determined by scanning the entire text of the block for name '
jpayne@69 6589 'binding\n'
jpayne@69 6590 'operations.\n'
jpayne@69 6591 '\n'
jpayne@69 6592 'If the "global" statement occurs within a block, all uses of the '
jpayne@69 6593 'name\n'
jpayne@69 6594 'specified in the statement refer to the binding of that name in '
jpayne@69 6595 'the\n'
jpayne@69 6596 'top-level namespace. Names are resolved in the top-level '
jpayne@69 6597 'namespace by\n'
jpayne@69 6598 'searching the global namespace, i.e. the namespace of the module\n'
jpayne@69 6599 'containing the code block, and the builtins namespace, the '
jpayne@69 6600 'namespace\n'
jpayne@69 6601 'of the module "builtins". The global namespace is searched '
jpayne@69 6602 'first. If\n'
jpayne@69 6603 'the name is not found there, the builtins namespace is searched. '
jpayne@69 6604 'The\n'
jpayne@69 6605 '"global" statement must precede all uses of the name.\n'
jpayne@69 6606 '\n'
jpayne@69 6607 'The "global" statement has the same scope as a name binding '
jpayne@69 6608 'operation\n'
jpayne@69 6609 'in the same block. If the nearest enclosing scope for a free '
jpayne@69 6610 'variable\n'
jpayne@69 6611 'contains a global statement, the free variable is treated as a '
jpayne@69 6612 'global.\n'
jpayne@69 6613 '\n'
jpayne@69 6614 'The "nonlocal" statement causes corresponding names to refer to\n'
jpayne@69 6615 'previously bound variables in the nearest enclosing function '
jpayne@69 6616 'scope.\n'
jpayne@69 6617 '"SyntaxError" is raised at compile time if the given name does '
jpayne@69 6618 'not\n'
jpayne@69 6619 'exist in any enclosing function scope.\n'
jpayne@69 6620 '\n'
jpayne@69 6621 'The namespace for a module is automatically created the first time '
jpayne@69 6622 'a\n'
jpayne@69 6623 'module is imported. The main module for a script is always '
jpayne@69 6624 'called\n'
jpayne@69 6625 '"__main__".\n'
jpayne@69 6626 '\n'
jpayne@69 6627 'Class definition blocks and arguments to "exec()" and "eval()" '
jpayne@69 6628 'are\n'
jpayne@69 6629 'special in the context of name resolution. A class definition is '
jpayne@69 6630 'an\n'
jpayne@69 6631 'executable statement that may use and define names. These '
jpayne@69 6632 'references\n'
jpayne@69 6633 'follow the normal rules for name resolution with an exception '
jpayne@69 6634 'that\n'
jpayne@69 6635 'unbound local variables are looked up in the global namespace. '
jpayne@69 6636 'The\n'
jpayne@69 6637 'namespace of the class definition becomes the attribute dictionary '
jpayne@69 6638 'of\n'
jpayne@69 6639 'the class. The scope of names defined in a class block is limited '
jpayne@69 6640 'to\n'
jpayne@69 6641 'the class block; it does not extend to the code blocks of methods '
jpayne@69 6642 '–\n'
jpayne@69 6643 'this includes comprehensions and generator expressions since they '
jpayne@69 6644 'are\n'
jpayne@69 6645 'implemented using a function scope. This means that the '
jpayne@69 6646 'following\n'
jpayne@69 6647 'will fail:\n'
jpayne@69 6648 '\n'
jpayne@69 6649 ' class A:\n'
jpayne@69 6650 ' a = 42\n'
jpayne@69 6651 ' b = list(a + i for i in range(10))\n'
jpayne@69 6652 '\n'
jpayne@69 6653 '\n'
jpayne@69 6654 'Builtins and restricted execution\n'
jpayne@69 6655 '=================================\n'
jpayne@69 6656 '\n'
jpayne@69 6657 '**CPython implementation detail:** Users should not touch\n'
jpayne@69 6658 '"__builtins__"; it is strictly an implementation detail. Users\n'
jpayne@69 6659 'wanting to override values in the builtins namespace should '
jpayne@69 6660 '"import"\n'
jpayne@69 6661 'the "builtins" module and modify its attributes appropriately.\n'
jpayne@69 6662 '\n'
jpayne@69 6663 'The builtins namespace associated with the execution of a code '
jpayne@69 6664 'block\n'
jpayne@69 6665 'is actually found by looking up the name "__builtins__" in its '
jpayne@69 6666 'global\n'
jpayne@69 6667 'namespace; this should be a dictionary or a module (in the latter '
jpayne@69 6668 'case\n'
jpayne@69 6669 'the module’s dictionary is used). By default, when in the '
jpayne@69 6670 '"__main__"\n'
jpayne@69 6671 'module, "__builtins__" is the built-in module "builtins"; when in '
jpayne@69 6672 'any\n'
jpayne@69 6673 'other module, "__builtins__" is an alias for the dictionary of '
jpayne@69 6674 'the\n'
jpayne@69 6675 '"builtins" module itself.\n'
jpayne@69 6676 '\n'
jpayne@69 6677 '\n'
jpayne@69 6678 'Interaction with dynamic features\n'
jpayne@69 6679 '=================================\n'
jpayne@69 6680 '\n'
jpayne@69 6681 'Name resolution of free variables occurs at runtime, not at '
jpayne@69 6682 'compile\n'
jpayne@69 6683 'time. This means that the following code will print 42:\n'
jpayne@69 6684 '\n'
jpayne@69 6685 ' i = 10\n'
jpayne@69 6686 ' def f():\n'
jpayne@69 6687 ' print(i)\n'
jpayne@69 6688 ' i = 42\n'
jpayne@69 6689 ' f()\n'
jpayne@69 6690 '\n'
jpayne@69 6691 'The "eval()" and "exec()" functions do not have access to the '
jpayne@69 6692 'full\n'
jpayne@69 6693 'environment for resolving names. Names may be resolved in the '
jpayne@69 6694 'local\n'
jpayne@69 6695 'and global namespaces of the caller. Free variables are not '
jpayne@69 6696 'resolved\n'
jpayne@69 6697 'in the nearest enclosing namespace, but in the global namespace. '
jpayne@69 6698 '[1]\n'
jpayne@69 6699 'The "exec()" and "eval()" functions have optional arguments to\n'
jpayne@69 6700 'override the global and local namespace. If only one namespace '
jpayne@69 6701 'is\n'
jpayne@69 6702 'specified, it is used for both.\n',
jpayne@69 6703 'nonlocal': 'The "nonlocal" statement\n'
jpayne@69 6704 '************************\n'
jpayne@69 6705 '\n'
jpayne@69 6706 ' nonlocal_stmt ::= "nonlocal" identifier ("," identifier)*\n'
jpayne@69 6707 '\n'
jpayne@69 6708 'The "nonlocal" statement causes the listed identifiers to refer '
jpayne@69 6709 'to\n'
jpayne@69 6710 'previously bound variables in the nearest enclosing scope '
jpayne@69 6711 'excluding\n'
jpayne@69 6712 'globals. This is important because the default behavior for '
jpayne@69 6713 'binding is\n'
jpayne@69 6714 'to search the local namespace first. The statement allows\n'
jpayne@69 6715 'encapsulated code to rebind variables outside of the local '
jpayne@69 6716 'scope\n'
jpayne@69 6717 'besides the global (module) scope.\n'
jpayne@69 6718 '\n'
jpayne@69 6719 'Names listed in a "nonlocal" statement, unlike those listed in '
jpayne@69 6720 'a\n'
jpayne@69 6721 '"global" statement, must refer to pre-existing bindings in an\n'
jpayne@69 6722 'enclosing scope (the scope in which a new binding should be '
jpayne@69 6723 'created\n'
jpayne@69 6724 'cannot be determined unambiguously).\n'
jpayne@69 6725 '\n'
jpayne@69 6726 'Names listed in a "nonlocal" statement must not collide with '
jpayne@69 6727 'pre-\n'
jpayne@69 6728 'existing bindings in the local scope.\n'
jpayne@69 6729 '\n'
jpayne@69 6730 'See also:\n'
jpayne@69 6731 '\n'
jpayne@69 6732 ' **PEP 3104** - Access to Names in Outer Scopes\n'
jpayne@69 6733 ' The specification for the "nonlocal" statement.\n',
jpayne@69 6734 'numbers': 'Numeric literals\n'
jpayne@69 6735 '****************\n'
jpayne@69 6736 '\n'
jpayne@69 6737 'There are three types of numeric literals: integers, floating '
jpayne@69 6738 'point\n'
jpayne@69 6739 'numbers, and imaginary numbers. There are no complex literals\n'
jpayne@69 6740 '(complex numbers can be formed by adding a real number and an\n'
jpayne@69 6741 'imaginary number).\n'
jpayne@69 6742 '\n'
jpayne@69 6743 'Note that numeric literals do not include a sign; a phrase like '
jpayne@69 6744 '"-1"\n'
jpayne@69 6745 'is actually an expression composed of the unary operator ‘"-"‘ '
jpayne@69 6746 'and the\n'
jpayne@69 6747 'literal "1".\n',
jpayne@69 6748 'numeric-types': 'Emulating numeric types\n'
jpayne@69 6749 '***********************\n'
jpayne@69 6750 '\n'
jpayne@69 6751 'The following methods can be defined to emulate numeric '
jpayne@69 6752 'objects.\n'
jpayne@69 6753 'Methods corresponding to operations that are not supported '
jpayne@69 6754 'by the\n'
jpayne@69 6755 'particular kind of number implemented (e.g., bitwise '
jpayne@69 6756 'operations for\n'
jpayne@69 6757 'non-integral numbers) should be left undefined.\n'
jpayne@69 6758 '\n'
jpayne@69 6759 'object.__add__(self, other)\n'
jpayne@69 6760 'object.__sub__(self, other)\n'
jpayne@69 6761 'object.__mul__(self, other)\n'
jpayne@69 6762 'object.__matmul__(self, other)\n'
jpayne@69 6763 'object.__truediv__(self, other)\n'
jpayne@69 6764 'object.__floordiv__(self, other)\n'
jpayne@69 6765 'object.__mod__(self, other)\n'
jpayne@69 6766 'object.__divmod__(self, other)\n'
jpayne@69 6767 'object.__pow__(self, other[, modulo])\n'
jpayne@69 6768 'object.__lshift__(self, other)\n'
jpayne@69 6769 'object.__rshift__(self, other)\n'
jpayne@69 6770 'object.__and__(self, other)\n'
jpayne@69 6771 'object.__xor__(self, other)\n'
jpayne@69 6772 'object.__or__(self, other)\n'
jpayne@69 6773 '\n'
jpayne@69 6774 ' These methods are called to implement the binary '
jpayne@69 6775 'arithmetic\n'
jpayne@69 6776 ' operations ("+", "-", "*", "@", "/", "//", "%", '
jpayne@69 6777 '"divmod()",\n'
jpayne@69 6778 ' "pow()", "**", "<<", ">>", "&", "^", "|"). For '
jpayne@69 6779 'instance, to\n'
jpayne@69 6780 ' evaluate the expression "x + y", where *x* is an '
jpayne@69 6781 'instance of a\n'
jpayne@69 6782 ' class that has an "__add__()" method, "x.__add__(y)" is '
jpayne@69 6783 'called.\n'
jpayne@69 6784 ' The "__divmod__()" method should be the equivalent to '
jpayne@69 6785 'using\n'
jpayne@69 6786 ' "__floordiv__()" and "__mod__()"; it should not be '
jpayne@69 6787 'related to\n'
jpayne@69 6788 ' "__truediv__()". Note that "__pow__()" should be '
jpayne@69 6789 'defined to accept\n'
jpayne@69 6790 ' an optional third argument if the ternary version of the '
jpayne@69 6791 'built-in\n'
jpayne@69 6792 ' "pow()" function is to be supported.\n'
jpayne@69 6793 '\n'
jpayne@69 6794 ' If one of those methods does not support the operation '
jpayne@69 6795 'with the\n'
jpayne@69 6796 ' supplied arguments, it should return "NotImplemented".\n'
jpayne@69 6797 '\n'
jpayne@69 6798 'object.__radd__(self, other)\n'
jpayne@69 6799 'object.__rsub__(self, other)\n'
jpayne@69 6800 'object.__rmul__(self, other)\n'
jpayne@69 6801 'object.__rmatmul__(self, other)\n'
jpayne@69 6802 'object.__rtruediv__(self, other)\n'
jpayne@69 6803 'object.__rfloordiv__(self, other)\n'
jpayne@69 6804 'object.__rmod__(self, other)\n'
jpayne@69 6805 'object.__rdivmod__(self, other)\n'
jpayne@69 6806 'object.__rpow__(self, other)\n'
jpayne@69 6807 'object.__rlshift__(self, other)\n'
jpayne@69 6808 'object.__rrshift__(self, other)\n'
jpayne@69 6809 'object.__rand__(self, other)\n'
jpayne@69 6810 'object.__rxor__(self, other)\n'
jpayne@69 6811 'object.__ror__(self, other)\n'
jpayne@69 6812 '\n'
jpayne@69 6813 ' These methods are called to implement the binary '
jpayne@69 6814 'arithmetic\n'
jpayne@69 6815 ' operations ("+", "-", "*", "@", "/", "//", "%", '
jpayne@69 6816 '"divmod()",\n'
jpayne@69 6817 ' "pow()", "**", "<<", ">>", "&", "^", "|") with reflected '
jpayne@69 6818 '(swapped)\n'
jpayne@69 6819 ' operands. These functions are only called if the left '
jpayne@69 6820 'operand does\n'
jpayne@69 6821 ' not support the corresponding operation [3] and the '
jpayne@69 6822 'operands are of\n'
jpayne@69 6823 ' different types. [4] For instance, to evaluate the '
jpayne@69 6824 'expression "x -\n'
jpayne@69 6825 ' y", where *y* is an instance of a class that has an '
jpayne@69 6826 '"__rsub__()"\n'
jpayne@69 6827 ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" '
jpayne@69 6828 'returns\n'
jpayne@69 6829 ' *NotImplemented*.\n'
jpayne@69 6830 '\n'
jpayne@69 6831 ' Note that ternary "pow()" will not try calling '
jpayne@69 6832 '"__rpow__()" (the\n'
jpayne@69 6833 ' coercion rules would become too complicated).\n'
jpayne@69 6834 '\n'
jpayne@69 6835 ' Note: If the right operand’s type is a subclass of the '
jpayne@69 6836 'left\n'
jpayne@69 6837 ' operand’s type and that subclass provides the '
jpayne@69 6838 'reflected method\n'
jpayne@69 6839 ' for the operation, this method will be called before '
jpayne@69 6840 'the left\n'
jpayne@69 6841 ' operand’s non-reflected method. This behavior allows '
jpayne@69 6842 'subclasses\n'
jpayne@69 6843 ' to override their ancestors’ operations.\n'
jpayne@69 6844 '\n'
jpayne@69 6845 'object.__iadd__(self, other)\n'
jpayne@69 6846 'object.__isub__(self, other)\n'
jpayne@69 6847 'object.__imul__(self, other)\n'
jpayne@69 6848 'object.__imatmul__(self, other)\n'
jpayne@69 6849 'object.__itruediv__(self, other)\n'
jpayne@69 6850 'object.__ifloordiv__(self, other)\n'
jpayne@69 6851 'object.__imod__(self, other)\n'
jpayne@69 6852 'object.__ipow__(self, other[, modulo])\n'
jpayne@69 6853 'object.__ilshift__(self, other)\n'
jpayne@69 6854 'object.__irshift__(self, other)\n'
jpayne@69 6855 'object.__iand__(self, other)\n'
jpayne@69 6856 'object.__ixor__(self, other)\n'
jpayne@69 6857 'object.__ior__(self, other)\n'
jpayne@69 6858 '\n'
jpayne@69 6859 ' These methods are called to implement the augmented '
jpayne@69 6860 'arithmetic\n'
jpayne@69 6861 ' assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", '
jpayne@69 6862 '"**=",\n'
jpayne@69 6863 ' "<<=", ">>=", "&=", "^=", "|="). These methods should '
jpayne@69 6864 'attempt to\n'
jpayne@69 6865 ' do the operation in-place (modifying *self*) and return '
jpayne@69 6866 'the result\n'
jpayne@69 6867 ' (which could be, but does not have to be, *self*). If a '
jpayne@69 6868 'specific\n'
jpayne@69 6869 ' method is not defined, the augmented assignment falls '
jpayne@69 6870 'back to the\n'
jpayne@69 6871 ' normal methods. For instance, if *x* is an instance of '
jpayne@69 6872 'a class\n'
jpayne@69 6873 ' with an "__iadd__()" method, "x += y" is equivalent to '
jpayne@69 6874 '"x =\n'
jpayne@69 6875 ' x.__iadd__(y)" . Otherwise, "x.__add__(y)" and '
jpayne@69 6876 '"y.__radd__(x)" are\n'
jpayne@69 6877 ' considered, as with the evaluation of "x + y". In '
jpayne@69 6878 'certain\n'
jpayne@69 6879 ' situations, augmented assignment can result in '
jpayne@69 6880 'unexpected errors\n'
jpayne@69 6881 ' (see Why does a_tuple[i] += [‘item’] raise an exception '
jpayne@69 6882 'when the\n'
jpayne@69 6883 ' addition works?), but this behavior is in fact part of '
jpayne@69 6884 'the data\n'
jpayne@69 6885 ' model.\n'
jpayne@69 6886 '\n'
jpayne@69 6887 'object.__neg__(self)\n'
jpayne@69 6888 'object.__pos__(self)\n'
jpayne@69 6889 'object.__abs__(self)\n'
jpayne@69 6890 'object.__invert__(self)\n'
jpayne@69 6891 '\n'
jpayne@69 6892 ' Called to implement the unary arithmetic operations '
jpayne@69 6893 '("-", "+",\n'
jpayne@69 6894 ' "abs()" and "~").\n'
jpayne@69 6895 '\n'
jpayne@69 6896 'object.__complex__(self)\n'
jpayne@69 6897 'object.__int__(self)\n'
jpayne@69 6898 'object.__float__(self)\n'
jpayne@69 6899 '\n'
jpayne@69 6900 ' Called to implement the built-in functions "complex()", '
jpayne@69 6901 '"int()" and\n'
jpayne@69 6902 ' "float()". Should return a value of the appropriate '
jpayne@69 6903 'type.\n'
jpayne@69 6904 '\n'
jpayne@69 6905 'object.__index__(self)\n'
jpayne@69 6906 '\n'
jpayne@69 6907 ' Called to implement "operator.index()", and whenever '
jpayne@69 6908 'Python needs\n'
jpayne@69 6909 ' to losslessly convert the numeric object to an integer '
jpayne@69 6910 'object (such\n'
jpayne@69 6911 ' as in slicing, or in the built-in "bin()", "hex()" and '
jpayne@69 6912 '"oct()"\n'
jpayne@69 6913 ' functions). Presence of this method indicates that the '
jpayne@69 6914 'numeric\n'
jpayne@69 6915 ' object is an integer type. Must return an integer.\n'
jpayne@69 6916 '\n'
jpayne@69 6917 ' If "__int__()", "__float__()" and "__complex__()" are '
jpayne@69 6918 'not defined\n'
jpayne@69 6919 ' then corresponding built-in functions "int()", "float()" '
jpayne@69 6920 'and\n'
jpayne@69 6921 ' "complex()" fall back to "__index__()".\n'
jpayne@69 6922 '\n'
jpayne@69 6923 'object.__round__(self[, ndigits])\n'
jpayne@69 6924 'object.__trunc__(self)\n'
jpayne@69 6925 'object.__floor__(self)\n'
jpayne@69 6926 'object.__ceil__(self)\n'
jpayne@69 6927 '\n'
jpayne@69 6928 ' Called to implement the built-in function "round()" and '
jpayne@69 6929 '"math"\n'
jpayne@69 6930 ' functions "trunc()", "floor()" and "ceil()". Unless '
jpayne@69 6931 '*ndigits* is\n'
jpayne@69 6932 ' passed to "__round__()" all these methods should return '
jpayne@69 6933 'the value\n'
jpayne@69 6934 ' of the object truncated to an "Integral" (typically an '
jpayne@69 6935 '"int").\n'
jpayne@69 6936 '\n'
jpayne@69 6937 ' If "__int__()" is not defined then the built-in function '
jpayne@69 6938 '"int()"\n'
jpayne@69 6939 ' falls back to "__trunc__()".\n',
jpayne@69 6940 'objects': 'Objects, values and types\n'
jpayne@69 6941 '*************************\n'
jpayne@69 6942 '\n'
jpayne@69 6943 '*Objects* are Python’s abstraction for data. All data in a '
jpayne@69 6944 'Python\n'
jpayne@69 6945 'program is represented by objects or by relations between '
jpayne@69 6946 'objects. (In\n'
jpayne@69 6947 'a sense, and in conformance to Von Neumann’s model of a “stored\n'
jpayne@69 6948 'program computer,” code is also represented by objects.)\n'
jpayne@69 6949 '\n'
jpayne@69 6950 'Every object has an identity, a type and a value. An object’s\n'
jpayne@69 6951 '*identity* never changes once it has been created; you may think '
jpayne@69 6952 'of it\n'
jpayne@69 6953 'as the object’s address in memory. The ‘"is"’ operator compares '
jpayne@69 6954 'the\n'
jpayne@69 6955 'identity of two objects; the "id()" function returns an integer\n'
jpayne@69 6956 'representing its identity.\n'
jpayne@69 6957 '\n'
jpayne@69 6958 '**CPython implementation detail:** For CPython, "id(x)" is the '
jpayne@69 6959 'memory\n'
jpayne@69 6960 'address where "x" is stored.\n'
jpayne@69 6961 '\n'
jpayne@69 6962 'An object’s type determines the operations that the object '
jpayne@69 6963 'supports\n'
jpayne@69 6964 '(e.g., “does it have a length?”) and also defines the possible '
jpayne@69 6965 'values\n'
jpayne@69 6966 'for objects of that type. The "type()" function returns an '
jpayne@69 6967 'object’s\n'
jpayne@69 6968 'type (which is an object itself). Like its identity, an '
jpayne@69 6969 'object’s\n'
jpayne@69 6970 '*type* is also unchangeable. [1]\n'
jpayne@69 6971 '\n'
jpayne@69 6972 'The *value* of some objects can change. Objects whose value can\n'
jpayne@69 6973 'change are said to be *mutable*; objects whose value is '
jpayne@69 6974 'unchangeable\n'
jpayne@69 6975 'once they are created are called *immutable*. (The value of an\n'
jpayne@69 6976 'immutable container object that contains a reference to a '
jpayne@69 6977 'mutable\n'
jpayne@69 6978 'object can change when the latter’s value is changed; however '
jpayne@69 6979 'the\n'
jpayne@69 6980 'container is still considered immutable, because the collection '
jpayne@69 6981 'of\n'
jpayne@69 6982 'objects it contains cannot be changed. So, immutability is not\n'
jpayne@69 6983 'strictly the same as having an unchangeable value, it is more '
jpayne@69 6984 'subtle.)\n'
jpayne@69 6985 'An object’s mutability is determined by its type; for instance,\n'
jpayne@69 6986 'numbers, strings and tuples are immutable, while dictionaries '
jpayne@69 6987 'and\n'
jpayne@69 6988 'lists are mutable.\n'
jpayne@69 6989 '\n'
jpayne@69 6990 'Objects are never explicitly destroyed; however, when they '
jpayne@69 6991 'become\n'
jpayne@69 6992 'unreachable they may be garbage-collected. An implementation is\n'
jpayne@69 6993 'allowed to postpone garbage collection or omit it altogether — it '
jpayne@69 6994 'is a\n'
jpayne@69 6995 'matter of implementation quality how garbage collection is\n'
jpayne@69 6996 'implemented, as long as no objects are collected that are still\n'
jpayne@69 6997 'reachable.\n'
jpayne@69 6998 '\n'
jpayne@69 6999 '**CPython implementation detail:** CPython currently uses a '
jpayne@69 7000 'reference-\n'
jpayne@69 7001 'counting scheme with (optional) delayed detection of cyclically '
jpayne@69 7002 'linked\n'
jpayne@69 7003 'garbage, which collects most objects as soon as they become\n'
jpayne@69 7004 'unreachable, but is not guaranteed to collect garbage containing\n'
jpayne@69 7005 'circular references. See the documentation of the "gc" module '
jpayne@69 7006 'for\n'
jpayne@69 7007 'information on controlling the collection of cyclic garbage. '
jpayne@69 7008 'Other\n'
jpayne@69 7009 'implementations act differently and CPython may change. Do not '
jpayne@69 7010 'depend\n'
jpayne@69 7011 'on immediate finalization of objects when they become unreachable '
jpayne@69 7012 '(so\n'
jpayne@69 7013 'you should always close files explicitly).\n'
jpayne@69 7014 '\n'
jpayne@69 7015 'Note that the use of the implementation’s tracing or debugging\n'
jpayne@69 7016 'facilities may keep objects alive that would normally be '
jpayne@69 7017 'collectable.\n'
jpayne@69 7018 'Also note that catching an exception with a ‘"try"…"except"’ '
jpayne@69 7019 'statement\n'
jpayne@69 7020 'may keep objects alive.\n'
jpayne@69 7021 '\n'
jpayne@69 7022 'Some objects contain references to “external” resources such as '
jpayne@69 7023 'open\n'
jpayne@69 7024 'files or windows. It is understood that these resources are '
jpayne@69 7025 'freed\n'
jpayne@69 7026 'when the object is garbage-collected, but since garbage '
jpayne@69 7027 'collection is\n'
jpayne@69 7028 'not guaranteed to happen, such objects also provide an explicit '
jpayne@69 7029 'way to\n'
jpayne@69 7030 'release the external resource, usually a "close()" method. '
jpayne@69 7031 'Programs\n'
jpayne@69 7032 'are strongly recommended to explicitly close such objects. The\n'
jpayne@69 7033 '‘"try"…"finally"’ statement and the ‘"with"’ statement provide\n'
jpayne@69 7034 'convenient ways to do this.\n'
jpayne@69 7035 '\n'
jpayne@69 7036 'Some objects contain references to other objects; these are '
jpayne@69 7037 'called\n'
jpayne@69 7038 '*containers*. Examples of containers are tuples, lists and\n'
jpayne@69 7039 'dictionaries. The references are part of a container’s value. '
jpayne@69 7040 'In\n'
jpayne@69 7041 'most cases, when we talk about the value of a container, we imply '
jpayne@69 7042 'the\n'
jpayne@69 7043 'values, not the identities of the contained objects; however, '
jpayne@69 7044 'when we\n'
jpayne@69 7045 'talk about the mutability of a container, only the identities of '
jpayne@69 7046 'the\n'
jpayne@69 7047 'immediately contained objects are implied. So, if an immutable\n'
jpayne@69 7048 'container (like a tuple) contains a reference to a mutable '
jpayne@69 7049 'object, its\n'
jpayne@69 7050 'value changes if that mutable object is changed.\n'
jpayne@69 7051 '\n'
jpayne@69 7052 'Types affect almost all aspects of object behavior. Even the\n'
jpayne@69 7053 'importance of object identity is affected in some sense: for '
jpayne@69 7054 'immutable\n'
jpayne@69 7055 'types, operations that compute new values may actually return a\n'
jpayne@69 7056 'reference to any existing object with the same type and value, '
jpayne@69 7057 'while\n'
jpayne@69 7058 'for mutable objects this is not allowed. E.g., after "a = 1; b = '
jpayne@69 7059 '1",\n'
jpayne@69 7060 '"a" and "b" may or may not refer to the same object with the '
jpayne@69 7061 'value\n'
jpayne@69 7062 'one, depending on the implementation, but after "c = []; d = []", '
jpayne@69 7063 '"c"\n'
jpayne@69 7064 'and "d" are guaranteed to refer to two different, unique, newly\n'
jpayne@69 7065 'created empty lists. (Note that "c = d = []" assigns the same '
jpayne@69 7066 'object\n'
jpayne@69 7067 'to both "c" and "d".)\n',
jpayne@69 7068 'operator-summary': 'Operator precedence\n'
jpayne@69 7069 '*******************\n'
jpayne@69 7070 '\n'
jpayne@69 7071 'The following table summarizes the operator precedence '
jpayne@69 7072 'in Python, from\n'
jpayne@69 7073 'lowest precedence (least binding) to highest precedence '
jpayne@69 7074 '(most\n'
jpayne@69 7075 'binding). Operators in the same box have the same '
jpayne@69 7076 'precedence. Unless\n'
jpayne@69 7077 'the syntax is explicitly given, operators are binary. '
jpayne@69 7078 'Operators in\n'
jpayne@69 7079 'the same box group left to right (except for '
jpayne@69 7080 'exponentiation, which\n'
jpayne@69 7081 'groups from right to left).\n'
jpayne@69 7082 '\n'
jpayne@69 7083 'Note that comparisons, membership tests, and identity '
jpayne@69 7084 'tests, all have\n'
jpayne@69 7085 'the same precedence and have a left-to-right chaining '
jpayne@69 7086 'feature as\n'
jpayne@69 7087 'described in the Comparisons section.\n'
jpayne@69 7088 '\n'
jpayne@69 7089 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7090 '| Operator | '
jpayne@69 7091 'Description |\n'
jpayne@69 7092 '|=================================================|=======================================|\n'
jpayne@69 7093 '| ":=" | '
jpayne@69 7094 'Assignment expression |\n'
jpayne@69 7095 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7096 '| "lambda" | '
jpayne@69 7097 'Lambda expression |\n'
jpayne@69 7098 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7099 '| "if" – "else" | '
jpayne@69 7100 'Conditional expression |\n'
jpayne@69 7101 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7102 '| "or" | '
jpayne@69 7103 'Boolean OR |\n'
jpayne@69 7104 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7105 '| "and" | '
jpayne@69 7106 'Boolean AND |\n'
jpayne@69 7107 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7108 '| "not" "x" | '
jpayne@69 7109 'Boolean NOT |\n'
jpayne@69 7110 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7111 '| "in", "not in", "is", "is not", "<", "<=", ">", | '
jpayne@69 7112 'Comparisons, including membership |\n'
jpayne@69 7113 '| ">=", "!=", "==" | '
jpayne@69 7114 'tests and identity tests |\n'
jpayne@69 7115 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7116 '| "|" | '
jpayne@69 7117 'Bitwise OR |\n'
jpayne@69 7118 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7119 '| "^" | '
jpayne@69 7120 'Bitwise XOR |\n'
jpayne@69 7121 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7122 '| "&" | '
jpayne@69 7123 'Bitwise AND |\n'
jpayne@69 7124 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7125 '| "<<", ">>" | '
jpayne@69 7126 'Shifts |\n'
jpayne@69 7127 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7128 '| "+", "-" | '
jpayne@69 7129 'Addition and subtraction |\n'
jpayne@69 7130 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7131 '| "*", "@", "/", "//", "%" | '
jpayne@69 7132 'Multiplication, matrix |\n'
jpayne@69 7133 '| | '
jpayne@69 7134 'multiplication, division, floor |\n'
jpayne@69 7135 '| | '
jpayne@69 7136 'division, remainder [5] |\n'
jpayne@69 7137 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7138 '| "+x", "-x", "~x" | '
jpayne@69 7139 'Positive, negative, bitwise NOT |\n'
jpayne@69 7140 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7141 '| "**" | '
jpayne@69 7142 'Exponentiation [6] |\n'
jpayne@69 7143 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7144 '| "await" "x" | '
jpayne@69 7145 'Await expression |\n'
jpayne@69 7146 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7147 '| "x[index]", "x[index:index]", | '
jpayne@69 7148 'Subscription, slicing, call, |\n'
jpayne@69 7149 '| "x(arguments...)", "x.attribute" | '
jpayne@69 7150 'attribute reference |\n'
jpayne@69 7151 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7152 '| "(expressions...)", "[expressions...]", "{key: | '
jpayne@69 7153 'Binding or parenthesized expression, |\n'
jpayne@69 7154 '| value...}", "{expressions...}" | list '
jpayne@69 7155 'display, dictionary display, set |\n'
jpayne@69 7156 '| | '
jpayne@69 7157 'display |\n'
jpayne@69 7158 '+-------------------------------------------------+---------------------------------------+\n'
jpayne@69 7159 '\n'
jpayne@69 7160 '-[ Footnotes ]-\n'
jpayne@69 7161 '\n'
jpayne@69 7162 '[1] While "abs(x%y) < abs(y)" is true mathematically, '
jpayne@69 7163 'for floats\n'
jpayne@69 7164 ' it may not be true numerically due to roundoff. For '
jpayne@69 7165 'example, and\n'
jpayne@69 7166 ' assuming a platform on which a Python float is an '
jpayne@69 7167 'IEEE 754 double-\n'
jpayne@69 7168 ' precision number, in order that "-1e-100 % 1e100" '
jpayne@69 7169 'have the same\n'
jpayne@69 7170 ' sign as "1e100", the computed result is "-1e-100 + '
jpayne@69 7171 '1e100", which\n'
jpayne@69 7172 ' is numerically exactly equal to "1e100". The '
jpayne@69 7173 'function\n'
jpayne@69 7174 ' "math.fmod()" returns a result whose sign matches '
jpayne@69 7175 'the sign of the\n'
jpayne@69 7176 ' first argument instead, and so returns "-1e-100" in '
jpayne@69 7177 'this case.\n'
jpayne@69 7178 ' Which approach is more appropriate depends on the '
jpayne@69 7179 'application.\n'
jpayne@69 7180 '\n'
jpayne@69 7181 '[2] If x is very close to an exact integer multiple of '
jpayne@69 7182 'y, it’s\n'
jpayne@69 7183 ' possible for "x//y" to be one larger than '
jpayne@69 7184 '"(x-x%y)//y" due to\n'
jpayne@69 7185 ' rounding. In such cases, Python returns the latter '
jpayne@69 7186 'result, in\n'
jpayne@69 7187 ' order to preserve that "divmod(x,y)[0] * y + x % y" '
jpayne@69 7188 'be very close\n'
jpayne@69 7189 ' to "x".\n'
jpayne@69 7190 '\n'
jpayne@69 7191 '[3] The Unicode standard distinguishes between *code '
jpayne@69 7192 'points* (e.g.\n'
jpayne@69 7193 ' U+0041) and *abstract characters* (e.g. “LATIN '
jpayne@69 7194 'CAPITAL LETTER A”).\n'
jpayne@69 7195 ' While most abstract characters in Unicode are only '
jpayne@69 7196 'represented\n'
jpayne@69 7197 ' using one code point, there is a number of abstract '
jpayne@69 7198 'characters\n'
jpayne@69 7199 ' that can in addition be represented using a sequence '
jpayne@69 7200 'of more than\n'
jpayne@69 7201 ' one code point. For example, the abstract character '
jpayne@69 7202 '“LATIN\n'
jpayne@69 7203 ' CAPITAL LETTER C WITH CEDILLA” can be represented as '
jpayne@69 7204 'a single\n'
jpayne@69 7205 ' *precomposed character* at code position U+00C7, or '
jpayne@69 7206 'as a sequence\n'
jpayne@69 7207 ' of a *base character* at code position U+0043 (LATIN '
jpayne@69 7208 'CAPITAL\n'
jpayne@69 7209 ' LETTER C), followed by a *combining character* at '
jpayne@69 7210 'code position\n'
jpayne@69 7211 ' U+0327 (COMBINING CEDILLA).\n'
jpayne@69 7212 '\n'
jpayne@69 7213 ' The comparison operators on strings compare at the '
jpayne@69 7214 'level of\n'
jpayne@69 7215 ' Unicode code points. This may be counter-intuitive '
jpayne@69 7216 'to humans. For\n'
jpayne@69 7217 ' example, ""\\u00C7" == "\\u0043\\u0327"" is "False", '
jpayne@69 7218 'even though both\n'
jpayne@69 7219 ' strings represent the same abstract character “LATIN '
jpayne@69 7220 'CAPITAL\n'
jpayne@69 7221 ' LETTER C WITH CEDILLA”.\n'
jpayne@69 7222 '\n'
jpayne@69 7223 ' To compare strings at the level of abstract '
jpayne@69 7224 'characters (that is,\n'
jpayne@69 7225 ' in a way intuitive to humans), use '
jpayne@69 7226 '"unicodedata.normalize()".\n'
jpayne@69 7227 '\n'
jpayne@69 7228 '[4] Due to automatic garbage-collection, free lists, and '
jpayne@69 7229 'the\n'
jpayne@69 7230 ' dynamic nature of descriptors, you may notice '
jpayne@69 7231 'seemingly unusual\n'
jpayne@69 7232 ' behaviour in certain uses of the "is" operator, like '
jpayne@69 7233 'those\n'
jpayne@69 7234 ' involving comparisons between instance methods, or '
jpayne@69 7235 'constants.\n'
jpayne@69 7236 ' Check their documentation for more info.\n'
jpayne@69 7237 '\n'
jpayne@69 7238 '[5] The "%" operator is also used for string formatting; '
jpayne@69 7239 'the same\n'
jpayne@69 7240 ' precedence applies.\n'
jpayne@69 7241 '\n'
jpayne@69 7242 '[6] The power operator "**" binds less tightly than an '
jpayne@69 7243 'arithmetic\n'
jpayne@69 7244 ' or bitwise unary operator on its right, that is, '
jpayne@69 7245 '"2**-1" is "0.5".\n',
jpayne@69 7246 'pass': 'The "pass" statement\n'
jpayne@69 7247 '********************\n'
jpayne@69 7248 '\n'
jpayne@69 7249 ' pass_stmt ::= "pass"\n'
jpayne@69 7250 '\n'
jpayne@69 7251 '"pass" is a null operation — when it is executed, nothing happens. '
jpayne@69 7252 'It\n'
jpayne@69 7253 'is useful as a placeholder when a statement is required '
jpayne@69 7254 'syntactically,\n'
jpayne@69 7255 'but no code needs to be executed, for example:\n'
jpayne@69 7256 '\n'
jpayne@69 7257 ' def f(arg): pass # a function that does nothing (yet)\n'
jpayne@69 7258 '\n'
jpayne@69 7259 ' class C: pass # a class with no methods (yet)\n',
jpayne@69 7260 'power': 'The power operator\n'
jpayne@69 7261 '******************\n'
jpayne@69 7262 '\n'
jpayne@69 7263 'The power operator binds more tightly than unary operators on its\n'
jpayne@69 7264 'left; it binds less tightly than unary operators on its right. '
jpayne@69 7265 'The\n'
jpayne@69 7266 'syntax is:\n'
jpayne@69 7267 '\n'
jpayne@69 7268 ' power ::= (await_expr | primary) ["**" u_expr]\n'
jpayne@69 7269 '\n'
jpayne@69 7270 'Thus, in an unparenthesized sequence of power and unary operators, '
jpayne@69 7271 'the\n'
jpayne@69 7272 'operators are evaluated from right to left (this does not '
jpayne@69 7273 'constrain\n'
jpayne@69 7274 'the evaluation order for the operands): "-1**2" results in "-1".\n'
jpayne@69 7275 '\n'
jpayne@69 7276 'The power operator has the same semantics as the built-in "pow()"\n'
jpayne@69 7277 'function, when called with two arguments: it yields its left '
jpayne@69 7278 'argument\n'
jpayne@69 7279 'raised to the power of its right argument. The numeric arguments '
jpayne@69 7280 'are\n'
jpayne@69 7281 'first converted to a common type, and the result is of that type.\n'
jpayne@69 7282 '\n'
jpayne@69 7283 'For int operands, the result has the same type as the operands '
jpayne@69 7284 'unless\n'
jpayne@69 7285 'the second argument is negative; in that case, all arguments are\n'
jpayne@69 7286 'converted to float and a float result is delivered. For example,\n'
jpayne@69 7287 '"10**2" returns "100", but "10**-2" returns "0.01".\n'
jpayne@69 7288 '\n'
jpayne@69 7289 'Raising "0.0" to a negative power results in a '
jpayne@69 7290 '"ZeroDivisionError".\n'
jpayne@69 7291 'Raising a negative number to a fractional power results in a '
jpayne@69 7292 '"complex"\n'
jpayne@69 7293 'number. (In earlier versions it raised a "ValueError".)\n',
jpayne@69 7294 'raise': 'The "raise" statement\n'
jpayne@69 7295 '*********************\n'
jpayne@69 7296 '\n'
jpayne@69 7297 ' raise_stmt ::= "raise" [expression ["from" expression]]\n'
jpayne@69 7298 '\n'
jpayne@69 7299 'If no expressions are present, "raise" re-raises the last '
jpayne@69 7300 'exception\n'
jpayne@69 7301 'that was active in the current scope. If no exception is active '
jpayne@69 7302 'in\n'
jpayne@69 7303 'the current scope, a "RuntimeError" exception is raised indicating\n'
jpayne@69 7304 'that this is an error.\n'
jpayne@69 7305 '\n'
jpayne@69 7306 'Otherwise, "raise" evaluates the first expression as the exception\n'
jpayne@69 7307 'object. It must be either a subclass or an instance of\n'
jpayne@69 7308 '"BaseException". If it is a class, the exception instance will be\n'
jpayne@69 7309 'obtained when needed by instantiating the class with no arguments.\n'
jpayne@69 7310 '\n'
jpayne@69 7311 'The *type* of the exception is the exception instance’s class, the\n'
jpayne@69 7312 '*value* is the instance itself.\n'
jpayne@69 7313 '\n'
jpayne@69 7314 'A traceback object is normally created automatically when an '
jpayne@69 7315 'exception\n'
jpayne@69 7316 'is raised and attached to it as the "__traceback__" attribute, '
jpayne@69 7317 'which\n'
jpayne@69 7318 'is writable. You can create an exception and set your own traceback '
jpayne@69 7319 'in\n'
jpayne@69 7320 'one step using the "with_traceback()" exception method (which '
jpayne@69 7321 'returns\n'
jpayne@69 7322 'the same exception instance, with its traceback set to its '
jpayne@69 7323 'argument),\n'
jpayne@69 7324 'like so:\n'
jpayne@69 7325 '\n'
jpayne@69 7326 ' raise Exception("foo occurred").with_traceback(tracebackobj)\n'
jpayne@69 7327 '\n'
jpayne@69 7328 'The "from" clause is used for exception chaining: if given, the '
jpayne@69 7329 'second\n'
jpayne@69 7330 '*expression* must be another exception class or instance, which '
jpayne@69 7331 'will\n'
jpayne@69 7332 'then be attached to the raised exception as the "__cause__" '
jpayne@69 7333 'attribute\n'
jpayne@69 7334 '(which is writable). If the raised exception is not handled, both\n'
jpayne@69 7335 'exceptions will be printed:\n'
jpayne@69 7336 '\n'
jpayne@69 7337 ' >>> try:\n'
jpayne@69 7338 ' ... print(1 / 0)\n'
jpayne@69 7339 ' ... except Exception as exc:\n'
jpayne@69 7340 ' ... raise RuntimeError("Something bad happened") from exc\n'
jpayne@69 7341 ' ...\n'
jpayne@69 7342 ' Traceback (most recent call last):\n'
jpayne@69 7343 ' File "<stdin>", line 2, in <module>\n'
jpayne@69 7344 ' ZeroDivisionError: division by zero\n'
jpayne@69 7345 '\n'
jpayne@69 7346 ' The above exception was the direct cause of the following '
jpayne@69 7347 'exception:\n'
jpayne@69 7348 '\n'
jpayne@69 7349 ' Traceback (most recent call last):\n'
jpayne@69 7350 ' File "<stdin>", line 4, in <module>\n'
jpayne@69 7351 ' RuntimeError: Something bad happened\n'
jpayne@69 7352 '\n'
jpayne@69 7353 'A similar mechanism works implicitly if an exception is raised '
jpayne@69 7354 'inside\n'
jpayne@69 7355 'an exception handler or a "finally" clause: the previous exception '
jpayne@69 7356 'is\n'
jpayne@69 7357 'then attached as the new exception’s "__context__" attribute:\n'
jpayne@69 7358 '\n'
jpayne@69 7359 ' >>> try:\n'
jpayne@69 7360 ' ... print(1 / 0)\n'
jpayne@69 7361 ' ... except:\n'
jpayne@69 7362 ' ... raise RuntimeError("Something bad happened")\n'
jpayne@69 7363 ' ...\n'
jpayne@69 7364 ' Traceback (most recent call last):\n'
jpayne@69 7365 ' File "<stdin>", line 2, in <module>\n'
jpayne@69 7366 ' ZeroDivisionError: division by zero\n'
jpayne@69 7367 '\n'
jpayne@69 7368 ' During handling of the above exception, another exception '
jpayne@69 7369 'occurred:\n'
jpayne@69 7370 '\n'
jpayne@69 7371 ' Traceback (most recent call last):\n'
jpayne@69 7372 ' File "<stdin>", line 4, in <module>\n'
jpayne@69 7373 ' RuntimeError: Something bad happened\n'
jpayne@69 7374 '\n'
jpayne@69 7375 'Exception chaining can be explicitly suppressed by specifying '
jpayne@69 7376 '"None"\n'
jpayne@69 7377 'in the "from" clause:\n'
jpayne@69 7378 '\n'
jpayne@69 7379 ' >>> try:\n'
jpayne@69 7380 ' ... print(1 / 0)\n'
jpayne@69 7381 ' ... except:\n'
jpayne@69 7382 ' ... raise RuntimeError("Something bad happened") from None\n'
jpayne@69 7383 ' ...\n'
jpayne@69 7384 ' Traceback (most recent call last):\n'
jpayne@69 7385 ' File "<stdin>", line 4, in <module>\n'
jpayne@69 7386 ' RuntimeError: Something bad happened\n'
jpayne@69 7387 '\n'
jpayne@69 7388 'Additional information on exceptions can be found in section\n'
jpayne@69 7389 'Exceptions, and information about handling exceptions is in '
jpayne@69 7390 'section\n'
jpayne@69 7391 'The try statement.\n'
jpayne@69 7392 '\n'
jpayne@69 7393 'Changed in version 3.3: "None" is now permitted as "Y" in "raise X\n'
jpayne@69 7394 'from Y".\n'
jpayne@69 7395 '\n'
jpayne@69 7396 'New in version 3.3: The "__suppress_context__" attribute to '
jpayne@69 7397 'suppress\n'
jpayne@69 7398 'automatic display of the exception context.\n',
jpayne@69 7399 'return': 'The "return" statement\n'
jpayne@69 7400 '**********************\n'
jpayne@69 7401 '\n'
jpayne@69 7402 ' return_stmt ::= "return" [expression_list]\n'
jpayne@69 7403 '\n'
jpayne@69 7404 '"return" may only occur syntactically nested in a function '
jpayne@69 7405 'definition,\n'
jpayne@69 7406 'not within a nested class definition.\n'
jpayne@69 7407 '\n'
jpayne@69 7408 'If an expression list is present, it is evaluated, else "None" is\n'
jpayne@69 7409 'substituted.\n'
jpayne@69 7410 '\n'
jpayne@69 7411 '"return" leaves the current function call with the expression list '
jpayne@69 7412 '(or\n'
jpayne@69 7413 '"None") as return value.\n'
jpayne@69 7414 '\n'
jpayne@69 7415 'When "return" passes control out of a "try" statement with a '
jpayne@69 7416 '"finally"\n'
jpayne@69 7417 'clause, that "finally" clause is executed before really leaving '
jpayne@69 7418 'the\n'
jpayne@69 7419 'function.\n'
jpayne@69 7420 '\n'
jpayne@69 7421 'In a generator function, the "return" statement indicates that '
jpayne@69 7422 'the\n'
jpayne@69 7423 'generator is done and will cause "StopIteration" to be raised. '
jpayne@69 7424 'The\n'
jpayne@69 7425 'returned value (if any) is used as an argument to construct\n'
jpayne@69 7426 '"StopIteration" and becomes the "StopIteration.value" attribute.\n'
jpayne@69 7427 '\n'
jpayne@69 7428 'In an asynchronous generator function, an empty "return" '
jpayne@69 7429 'statement\n'
jpayne@69 7430 'indicates that the asynchronous generator is done and will cause\n'
jpayne@69 7431 '"StopAsyncIteration" to be raised. A non-empty "return" statement '
jpayne@69 7432 'is\n'
jpayne@69 7433 'a syntax error in an asynchronous generator function.\n',
jpayne@69 7434 'sequence-types': 'Emulating container types\n'
jpayne@69 7435 '*************************\n'
jpayne@69 7436 '\n'
jpayne@69 7437 'The following methods can be defined to implement '
jpayne@69 7438 'container objects.\n'
jpayne@69 7439 'Containers usually are sequences (such as lists or tuples) '
jpayne@69 7440 'or mappings\n'
jpayne@69 7441 '(like dictionaries), but can represent other containers as '
jpayne@69 7442 'well. The\n'
jpayne@69 7443 'first set of methods is used either to emulate a sequence '
jpayne@69 7444 'or to\n'
jpayne@69 7445 'emulate a mapping; the difference is that for a sequence, '
jpayne@69 7446 'the\n'
jpayne@69 7447 'allowable keys should be the integers *k* for which "0 <= '
jpayne@69 7448 'k < N" where\n'
jpayne@69 7449 '*N* is the length of the sequence, or slice objects, which '
jpayne@69 7450 'define a\n'
jpayne@69 7451 'range of items. It is also recommended that mappings '
jpayne@69 7452 'provide the\n'
jpayne@69 7453 'methods "keys()", "values()", "items()", "get()", '
jpayne@69 7454 '"clear()",\n'
jpayne@69 7455 '"setdefault()", "pop()", "popitem()", "copy()", and '
jpayne@69 7456 '"update()"\n'
jpayne@69 7457 'behaving similar to those for Python’s standard dictionary '
jpayne@69 7458 'objects.\n'
jpayne@69 7459 'The "collections.abc" module provides a "MutableMapping" '
jpayne@69 7460 'abstract base\n'
jpayne@69 7461 'class to help create those methods from a base set of '
jpayne@69 7462 '"__getitem__()",\n'
jpayne@69 7463 '"__setitem__()", "__delitem__()", and "keys()". Mutable '
jpayne@69 7464 'sequences\n'
jpayne@69 7465 'should provide methods "append()", "count()", "index()", '
jpayne@69 7466 '"extend()",\n'
jpayne@69 7467 '"insert()", "pop()", "remove()", "reverse()" and "sort()", '
jpayne@69 7468 'like Python\n'
jpayne@69 7469 'standard list objects. Finally, sequence types should '
jpayne@69 7470 'implement\n'
jpayne@69 7471 'addition (meaning concatenation) and multiplication '
jpayne@69 7472 '(meaning\n'
jpayne@69 7473 'repetition) by defining the methods "__add__()", '
jpayne@69 7474 '"__radd__()",\n'
jpayne@69 7475 '"__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" '
jpayne@69 7476 'described\n'
jpayne@69 7477 'below; they should not define other numerical operators. '
jpayne@69 7478 'It is\n'
jpayne@69 7479 'recommended that both mappings and sequences implement '
jpayne@69 7480 'the\n'
jpayne@69 7481 '"__contains__()" method to allow efficient use of the "in" '
jpayne@69 7482 'operator;\n'
jpayne@69 7483 'for mappings, "in" should search the mapping’s keys; for '
jpayne@69 7484 'sequences, it\n'
jpayne@69 7485 'should search through the values. It is further '
jpayne@69 7486 'recommended that both\n'
jpayne@69 7487 'mappings and sequences implement the "__iter__()" method '
jpayne@69 7488 'to allow\n'
jpayne@69 7489 'efficient iteration through the container; for mappings, '
jpayne@69 7490 '"__iter__()"\n'
jpayne@69 7491 'should iterate through the object’s keys; for sequences, '
jpayne@69 7492 'it should\n'
jpayne@69 7493 'iterate through the values.\n'
jpayne@69 7494 '\n'
jpayne@69 7495 'object.__len__(self)\n'
jpayne@69 7496 '\n'
jpayne@69 7497 ' Called to implement the built-in function "len()". '
jpayne@69 7498 'Should return\n'
jpayne@69 7499 ' the length of the object, an integer ">=" 0. Also, an '
jpayne@69 7500 'object that\n'
jpayne@69 7501 ' doesn’t define a "__bool__()" method and whose '
jpayne@69 7502 '"__len__()" method\n'
jpayne@69 7503 ' returns zero is considered to be false in a Boolean '
jpayne@69 7504 'context.\n'
jpayne@69 7505 '\n'
jpayne@69 7506 ' **CPython implementation detail:** In CPython, the '
jpayne@69 7507 'length is\n'
jpayne@69 7508 ' required to be at most "sys.maxsize". If the length is '
jpayne@69 7509 'larger than\n'
jpayne@69 7510 ' "sys.maxsize" some features (such as "len()") may '
jpayne@69 7511 'raise\n'
jpayne@69 7512 ' "OverflowError". To prevent raising "OverflowError" by '
jpayne@69 7513 'truth value\n'
jpayne@69 7514 ' testing, an object must define a "__bool__()" method.\n'
jpayne@69 7515 '\n'
jpayne@69 7516 'object.__length_hint__(self)\n'
jpayne@69 7517 '\n'
jpayne@69 7518 ' Called to implement "operator.length_hint()". Should '
jpayne@69 7519 'return an\n'
jpayne@69 7520 ' estimated length for the object (which may be greater '
jpayne@69 7521 'or less than\n'
jpayne@69 7522 ' the actual length). The length must be an integer ">=" '
jpayne@69 7523 '0. The\n'
jpayne@69 7524 ' return value may also be "NotImplemented", which is '
jpayne@69 7525 'treated the\n'
jpayne@69 7526 ' same as if the "__length_hint__" method didn’t exist at '
jpayne@69 7527 'all. This\n'
jpayne@69 7528 ' method is purely an optimization and is never required '
jpayne@69 7529 'for\n'
jpayne@69 7530 ' correctness.\n'
jpayne@69 7531 '\n'
jpayne@69 7532 ' New in version 3.4.\n'
jpayne@69 7533 '\n'
jpayne@69 7534 'Note: Slicing is done exclusively with the following three '
jpayne@69 7535 'methods.\n'
jpayne@69 7536 ' A call like\n'
jpayne@69 7537 '\n'
jpayne@69 7538 ' a[1:2] = b\n'
jpayne@69 7539 '\n'
jpayne@69 7540 ' is translated to\n'
jpayne@69 7541 '\n'
jpayne@69 7542 ' a[slice(1, 2, None)] = b\n'
jpayne@69 7543 '\n'
jpayne@69 7544 ' and so forth. Missing slice items are always filled in '
jpayne@69 7545 'with "None".\n'
jpayne@69 7546 '\n'
jpayne@69 7547 'object.__getitem__(self, key)\n'
jpayne@69 7548 '\n'
jpayne@69 7549 ' Called to implement evaluation of "self[key]". For '
jpayne@69 7550 'sequence types,\n'
jpayne@69 7551 ' the accepted keys should be integers and slice '
jpayne@69 7552 'objects. Note that\n'
jpayne@69 7553 ' the special interpretation of negative indexes (if the '
jpayne@69 7554 'class wishes\n'
jpayne@69 7555 ' to emulate a sequence type) is up to the '
jpayne@69 7556 '"__getitem__()" method. If\n'
jpayne@69 7557 ' *key* is of an inappropriate type, "TypeError" may be '
jpayne@69 7558 'raised; if of\n'
jpayne@69 7559 ' a value outside the set of indexes for the sequence '
jpayne@69 7560 '(after any\n'
jpayne@69 7561 ' special interpretation of negative values), '
jpayne@69 7562 '"IndexError" should be\n'
jpayne@69 7563 ' raised. For mapping types, if *key* is missing (not in '
jpayne@69 7564 'the\n'
jpayne@69 7565 ' container), "KeyError" should be raised.\n'
jpayne@69 7566 '\n'
jpayne@69 7567 ' Note: "for" loops expect that an "IndexError" will be '
jpayne@69 7568 'raised for\n'
jpayne@69 7569 ' illegal indexes to allow proper detection of the end '
jpayne@69 7570 'of the\n'
jpayne@69 7571 ' sequence.\n'
jpayne@69 7572 '\n'
jpayne@69 7573 'object.__setitem__(self, key, value)\n'
jpayne@69 7574 '\n'
jpayne@69 7575 ' Called to implement assignment to "self[key]". Same '
jpayne@69 7576 'note as for\n'
jpayne@69 7577 ' "__getitem__()". This should only be implemented for '
jpayne@69 7578 'mappings if\n'
jpayne@69 7579 ' the objects support changes to the values for keys, or '
jpayne@69 7580 'if new keys\n'
jpayne@69 7581 ' can be added, or for sequences if elements can be '
jpayne@69 7582 'replaced. The\n'
jpayne@69 7583 ' same exceptions should be raised for improper *key* '
jpayne@69 7584 'values as for\n'
jpayne@69 7585 ' the "__getitem__()" method.\n'
jpayne@69 7586 '\n'
jpayne@69 7587 'object.__delitem__(self, key)\n'
jpayne@69 7588 '\n'
jpayne@69 7589 ' Called to implement deletion of "self[key]". Same note '
jpayne@69 7590 'as for\n'
jpayne@69 7591 ' "__getitem__()". This should only be implemented for '
jpayne@69 7592 'mappings if\n'
jpayne@69 7593 ' the objects support removal of keys, or for sequences '
jpayne@69 7594 'if elements\n'
jpayne@69 7595 ' can be removed from the sequence. The same exceptions '
jpayne@69 7596 'should be\n'
jpayne@69 7597 ' raised for improper *key* values as for the '
jpayne@69 7598 '"__getitem__()" method.\n'
jpayne@69 7599 '\n'
jpayne@69 7600 'object.__missing__(self, key)\n'
jpayne@69 7601 '\n'
jpayne@69 7602 ' Called by "dict"."__getitem__()" to implement '
jpayne@69 7603 '"self[key]" for dict\n'
jpayne@69 7604 ' subclasses when key is not in the dictionary.\n'
jpayne@69 7605 '\n'
jpayne@69 7606 'object.__iter__(self)\n'
jpayne@69 7607 '\n'
jpayne@69 7608 ' This method is called when an iterator is required for '
jpayne@69 7609 'a container.\n'
jpayne@69 7610 ' This method should return a new iterator object that '
jpayne@69 7611 'can iterate\n'
jpayne@69 7612 ' over all the objects in the container. For mappings, '
jpayne@69 7613 'it should\n'
jpayne@69 7614 ' iterate over the keys of the container.\n'
jpayne@69 7615 '\n'
jpayne@69 7616 ' Iterator objects also need to implement this method; '
jpayne@69 7617 'they are\n'
jpayne@69 7618 ' required to return themselves. For more information on '
jpayne@69 7619 'iterator\n'
jpayne@69 7620 ' objects, see Iterator Types.\n'
jpayne@69 7621 '\n'
jpayne@69 7622 'object.__reversed__(self)\n'
jpayne@69 7623 '\n'
jpayne@69 7624 ' Called (if present) by the "reversed()" built-in to '
jpayne@69 7625 'implement\n'
jpayne@69 7626 ' reverse iteration. It should return a new iterator '
jpayne@69 7627 'object that\n'
jpayne@69 7628 ' iterates over all the objects in the container in '
jpayne@69 7629 'reverse order.\n'
jpayne@69 7630 '\n'
jpayne@69 7631 ' If the "__reversed__()" method is not provided, the '
jpayne@69 7632 '"reversed()"\n'
jpayne@69 7633 ' built-in will fall back to using the sequence protocol '
jpayne@69 7634 '("__len__()"\n'
jpayne@69 7635 ' and "__getitem__()"). Objects that support the '
jpayne@69 7636 'sequence protocol\n'
jpayne@69 7637 ' should only provide "__reversed__()" if they can '
jpayne@69 7638 'provide an\n'
jpayne@69 7639 ' implementation that is more efficient than the one '
jpayne@69 7640 'provided by\n'
jpayne@69 7641 ' "reversed()".\n'
jpayne@69 7642 '\n'
jpayne@69 7643 'The membership test operators ("in" and "not in") are '
jpayne@69 7644 'normally\n'
jpayne@69 7645 'implemented as an iteration through a container. However, '
jpayne@69 7646 'container\n'
jpayne@69 7647 'objects can supply the following special method with a '
jpayne@69 7648 'more efficient\n'
jpayne@69 7649 'implementation, which also does not require the object be '
jpayne@69 7650 'iterable.\n'
jpayne@69 7651 '\n'
jpayne@69 7652 'object.__contains__(self, item)\n'
jpayne@69 7653 '\n'
jpayne@69 7654 ' Called to implement membership test operators. Should '
jpayne@69 7655 'return true\n'
jpayne@69 7656 ' if *item* is in *self*, false otherwise. For mapping '
jpayne@69 7657 'objects, this\n'
jpayne@69 7658 ' should consider the keys of the mapping rather than the '
jpayne@69 7659 'values or\n'
jpayne@69 7660 ' the key-item pairs.\n'
jpayne@69 7661 '\n'
jpayne@69 7662 ' For objects that don’t define "__contains__()", the '
jpayne@69 7663 'membership test\n'
jpayne@69 7664 ' first tries iteration via "__iter__()", then the old '
jpayne@69 7665 'sequence\n'
jpayne@69 7666 ' iteration protocol via "__getitem__()", see this '
jpayne@69 7667 'section in the\n'
jpayne@69 7668 ' language reference.\n',
jpayne@69 7669 'shifting': 'Shifting operations\n'
jpayne@69 7670 '*******************\n'
jpayne@69 7671 '\n'
jpayne@69 7672 'The shifting operations have lower priority than the arithmetic\n'
jpayne@69 7673 'operations:\n'
jpayne@69 7674 '\n'
jpayne@69 7675 ' shift_expr ::= a_expr | shift_expr ("<<" | ">>") a_expr\n'
jpayne@69 7676 '\n'
jpayne@69 7677 'These operators accept integers as arguments. They shift the '
jpayne@69 7678 'first\n'
jpayne@69 7679 'argument to the left or right by the number of bits given by '
jpayne@69 7680 'the\n'
jpayne@69 7681 'second argument.\n'
jpayne@69 7682 '\n'
jpayne@69 7683 'A right shift by *n* bits is defined as floor division by '
jpayne@69 7684 '"pow(2,n)".\n'
jpayne@69 7685 'A left shift by *n* bits is defined as multiplication with '
jpayne@69 7686 '"pow(2,n)".\n',
jpayne@69 7687 'slicings': 'Slicings\n'
jpayne@69 7688 '********\n'
jpayne@69 7689 '\n'
jpayne@69 7690 'A slicing selects a range of items in a sequence object (e.g., '
jpayne@69 7691 'a\n'
jpayne@69 7692 'string, tuple or list). Slicings may be used as expressions or '
jpayne@69 7693 'as\n'
jpayne@69 7694 'targets in assignment or "del" statements. The syntax for a '
jpayne@69 7695 'slicing:\n'
jpayne@69 7696 '\n'
jpayne@69 7697 ' slicing ::= primary "[" slice_list "]"\n'
jpayne@69 7698 ' slice_list ::= slice_item ("," slice_item)* [","]\n'
jpayne@69 7699 ' slice_item ::= expression | proper_slice\n'
jpayne@69 7700 ' proper_slice ::= [lower_bound] ":" [upper_bound] [ ":" '
jpayne@69 7701 '[stride] ]\n'
jpayne@69 7702 ' lower_bound ::= expression\n'
jpayne@69 7703 ' upper_bound ::= expression\n'
jpayne@69 7704 ' stride ::= expression\n'
jpayne@69 7705 '\n'
jpayne@69 7706 'There is ambiguity in the formal syntax here: anything that '
jpayne@69 7707 'looks like\n'
jpayne@69 7708 'an expression list also looks like a slice list, so any '
jpayne@69 7709 'subscription\n'
jpayne@69 7710 'can be interpreted as a slicing. Rather than further '
jpayne@69 7711 'complicating the\n'
jpayne@69 7712 'syntax, this is disambiguated by defining that in this case the\n'
jpayne@69 7713 'interpretation as a subscription takes priority over the\n'
jpayne@69 7714 'interpretation as a slicing (this is the case if the slice list\n'
jpayne@69 7715 'contains no proper slice).\n'
jpayne@69 7716 '\n'
jpayne@69 7717 'The semantics for a slicing are as follows. The primary is '
jpayne@69 7718 'indexed\n'
jpayne@69 7719 '(using the same "__getitem__()" method as normal subscription) '
jpayne@69 7720 'with a\n'
jpayne@69 7721 'key that is constructed from the slice list, as follows. If the '
jpayne@69 7722 'slice\n'
jpayne@69 7723 'list contains at least one comma, the key is a tuple containing '
jpayne@69 7724 'the\n'
jpayne@69 7725 'conversion of the slice items; otherwise, the conversion of the '
jpayne@69 7726 'lone\n'
jpayne@69 7727 'slice item is the key. The conversion of a slice item that is '
jpayne@69 7728 'an\n'
jpayne@69 7729 'expression is that expression. The conversion of a proper slice '
jpayne@69 7730 'is a\n'
jpayne@69 7731 'slice object (see section The standard type hierarchy) whose '
jpayne@69 7732 '"start",\n'
jpayne@69 7733 '"stop" and "step" attributes are the values of the expressions '
jpayne@69 7734 'given\n'
jpayne@69 7735 'as lower bound, upper bound and stride, respectively, '
jpayne@69 7736 'substituting\n'
jpayne@69 7737 '"None" for missing expressions.\n',
jpayne@69 7738 'specialattrs': 'Special Attributes\n'
jpayne@69 7739 '******************\n'
jpayne@69 7740 '\n'
jpayne@69 7741 'The implementation adds a few special read-only attributes '
jpayne@69 7742 'to several\n'
jpayne@69 7743 'object types, where they are relevant. Some of these are '
jpayne@69 7744 'not reported\n'
jpayne@69 7745 'by the "dir()" built-in function.\n'
jpayne@69 7746 '\n'
jpayne@69 7747 'object.__dict__\n'
jpayne@69 7748 '\n'
jpayne@69 7749 ' A dictionary or other mapping object used to store an '
jpayne@69 7750 'object’s\n'
jpayne@69 7751 ' (writable) attributes.\n'
jpayne@69 7752 '\n'
jpayne@69 7753 'instance.__class__\n'
jpayne@69 7754 '\n'
jpayne@69 7755 ' The class to which a class instance belongs.\n'
jpayne@69 7756 '\n'
jpayne@69 7757 'class.__bases__\n'
jpayne@69 7758 '\n'
jpayne@69 7759 ' The tuple of base classes of a class object.\n'
jpayne@69 7760 '\n'
jpayne@69 7761 'definition.__name__\n'
jpayne@69 7762 '\n'
jpayne@69 7763 ' The name of the class, function, method, descriptor, or '
jpayne@69 7764 'generator\n'
jpayne@69 7765 ' instance.\n'
jpayne@69 7766 '\n'
jpayne@69 7767 'definition.__qualname__\n'
jpayne@69 7768 '\n'
jpayne@69 7769 ' The *qualified name* of the class, function, method, '
jpayne@69 7770 'descriptor, or\n'
jpayne@69 7771 ' generator instance.\n'
jpayne@69 7772 '\n'
jpayne@69 7773 ' New in version 3.3.\n'
jpayne@69 7774 '\n'
jpayne@69 7775 'class.__mro__\n'
jpayne@69 7776 '\n'
jpayne@69 7777 ' This attribute is a tuple of classes that are considered '
jpayne@69 7778 'when\n'
jpayne@69 7779 ' looking for base classes during method resolution.\n'
jpayne@69 7780 '\n'
jpayne@69 7781 'class.mro()\n'
jpayne@69 7782 '\n'
jpayne@69 7783 ' This method can be overridden by a metaclass to customize '
jpayne@69 7784 'the\n'
jpayne@69 7785 ' method resolution order for its instances. It is called '
jpayne@69 7786 'at class\n'
jpayne@69 7787 ' instantiation, and its result is stored in "__mro__".\n'
jpayne@69 7788 '\n'
jpayne@69 7789 'class.__subclasses__()\n'
jpayne@69 7790 '\n'
jpayne@69 7791 ' Each class keeps a list of weak references to its '
jpayne@69 7792 'immediate\n'
jpayne@69 7793 ' subclasses. This method returns a list of all those '
jpayne@69 7794 'references\n'
jpayne@69 7795 ' still alive. Example:\n'
jpayne@69 7796 '\n'
jpayne@69 7797 ' >>> int.__subclasses__()\n'
jpayne@69 7798 " [<class 'bool'>]\n"
jpayne@69 7799 '\n'
jpayne@69 7800 '-[ Footnotes ]-\n'
jpayne@69 7801 '\n'
jpayne@69 7802 '[1] Additional information on these special methods may be '
jpayne@69 7803 'found\n'
jpayne@69 7804 ' in the Python Reference Manual (Basic customization).\n'
jpayne@69 7805 '\n'
jpayne@69 7806 '[2] As a consequence, the list "[1, 2]" is considered equal '
jpayne@69 7807 'to\n'
jpayne@69 7808 ' "[1.0, 2.0]", and similarly for tuples.\n'
jpayne@69 7809 '\n'
jpayne@69 7810 '[3] They must have since the parser can’t tell the type of '
jpayne@69 7811 'the\n'
jpayne@69 7812 ' operands.\n'
jpayne@69 7813 '\n'
jpayne@69 7814 '[4] Cased characters are those with general category '
jpayne@69 7815 'property\n'
jpayne@69 7816 ' being one of “Lu” (Letter, uppercase), “Ll” (Letter, '
jpayne@69 7817 'lowercase),\n'
jpayne@69 7818 ' or “Lt” (Letter, titlecase).\n'
jpayne@69 7819 '\n'
jpayne@69 7820 '[5] To format only a tuple you should therefore provide a\n'
jpayne@69 7821 ' singleton tuple whose only element is the tuple to be '
jpayne@69 7822 'formatted.\n',
jpayne@69 7823 'specialnames': 'Special method names\n'
jpayne@69 7824 '********************\n'
jpayne@69 7825 '\n'
jpayne@69 7826 'A class can implement certain operations that are invoked by '
jpayne@69 7827 'special\n'
jpayne@69 7828 'syntax (such as arithmetic operations or subscripting and '
jpayne@69 7829 'slicing) by\n'
jpayne@69 7830 'defining methods with special names. This is Python’s '
jpayne@69 7831 'approach to\n'
jpayne@69 7832 '*operator overloading*, allowing classes to define their own '
jpayne@69 7833 'behavior\n'
jpayne@69 7834 'with respect to language operators. For instance, if a '
jpayne@69 7835 'class defines\n'
jpayne@69 7836 'a method named "__getitem__()", and "x" is an instance of '
jpayne@69 7837 'this class,\n'
jpayne@69 7838 'then "x[i]" is roughly equivalent to "type(x).__getitem__(x, '
jpayne@69 7839 'i)".\n'
jpayne@69 7840 'Except where mentioned, attempts to execute an operation '
jpayne@69 7841 'raise an\n'
jpayne@69 7842 'exception when no appropriate method is defined (typically\n'
jpayne@69 7843 '"AttributeError" or "TypeError").\n'
jpayne@69 7844 '\n'
jpayne@69 7845 'Setting a special method to "None" indicates that the '
jpayne@69 7846 'corresponding\n'
jpayne@69 7847 'operation is not available. For example, if a class sets '
jpayne@69 7848 '"__iter__()"\n'
jpayne@69 7849 'to "None", the class is not iterable, so calling "iter()" on '
jpayne@69 7850 'its\n'
jpayne@69 7851 'instances will raise a "TypeError" (without falling back to\n'
jpayne@69 7852 '"__getitem__()"). [2]\n'
jpayne@69 7853 '\n'
jpayne@69 7854 'When implementing a class that emulates any built-in type, '
jpayne@69 7855 'it is\n'
jpayne@69 7856 'important that the emulation only be implemented to the '
jpayne@69 7857 'degree that it\n'
jpayne@69 7858 'makes sense for the object being modelled. For example, '
jpayne@69 7859 'some\n'
jpayne@69 7860 'sequences may work well with retrieval of individual '
jpayne@69 7861 'elements, but\n'
jpayne@69 7862 'extracting a slice may not make sense. (One example of this '
jpayne@69 7863 'is the\n'
jpayne@69 7864 '"NodeList" interface in the W3C’s Document Object Model.)\n'
jpayne@69 7865 '\n'
jpayne@69 7866 '\n'
jpayne@69 7867 'Basic customization\n'
jpayne@69 7868 '===================\n'
jpayne@69 7869 '\n'
jpayne@69 7870 'object.__new__(cls[, ...])\n'
jpayne@69 7871 '\n'
jpayne@69 7872 ' Called to create a new instance of class *cls*. '
jpayne@69 7873 '"__new__()" is a\n'
jpayne@69 7874 ' static method (special-cased so you need not declare it '
jpayne@69 7875 'as such)\n'
jpayne@69 7876 ' that takes the class of which an instance was requested '
jpayne@69 7877 'as its\n'
jpayne@69 7878 ' first argument. The remaining arguments are those passed '
jpayne@69 7879 'to the\n'
jpayne@69 7880 ' object constructor expression (the call to the class). '
jpayne@69 7881 'The return\n'
jpayne@69 7882 ' value of "__new__()" should be the new object instance '
jpayne@69 7883 '(usually an\n'
jpayne@69 7884 ' instance of *cls*).\n'
jpayne@69 7885 '\n'
jpayne@69 7886 ' Typical implementations create a new instance of the '
jpayne@69 7887 'class by\n'
jpayne@69 7888 ' invoking the superclass’s "__new__()" method using\n'
jpayne@69 7889 ' "super().__new__(cls[, ...])" with appropriate arguments '
jpayne@69 7890 'and then\n'
jpayne@69 7891 ' modifying the newly-created instance as necessary before '
jpayne@69 7892 'returning\n'
jpayne@69 7893 ' it.\n'
jpayne@69 7894 '\n'
jpayne@69 7895 ' If "__new__()" is invoked during object construction and '
jpayne@69 7896 'it returns\n'
jpayne@69 7897 ' an instance or subclass of *cls*, then the new '
jpayne@69 7898 'instance’s\n'
jpayne@69 7899 ' "__init__()" method will be invoked like "__init__(self[, '
jpayne@69 7900 '...])",\n'
jpayne@69 7901 ' where *self* is the new instance and the remaining '
jpayne@69 7902 'arguments are\n'
jpayne@69 7903 ' the same as were passed to the object constructor.\n'
jpayne@69 7904 '\n'
jpayne@69 7905 ' If "__new__()" does not return an instance of *cls*, then '
jpayne@69 7906 'the new\n'
jpayne@69 7907 ' instance’s "__init__()" method will not be invoked.\n'
jpayne@69 7908 '\n'
jpayne@69 7909 ' "__new__()" is intended mainly to allow subclasses of '
jpayne@69 7910 'immutable\n'
jpayne@69 7911 ' types (like int, str, or tuple) to customize instance '
jpayne@69 7912 'creation. It\n'
jpayne@69 7913 ' is also commonly overridden in custom metaclasses in '
jpayne@69 7914 'order to\n'
jpayne@69 7915 ' customize class creation.\n'
jpayne@69 7916 '\n'
jpayne@69 7917 'object.__init__(self[, ...])\n'
jpayne@69 7918 '\n'
jpayne@69 7919 ' Called after the instance has been created (by '
jpayne@69 7920 '"__new__()"), but\n'
jpayne@69 7921 ' before it is returned to the caller. The arguments are '
jpayne@69 7922 'those\n'
jpayne@69 7923 ' passed to the class constructor expression. If a base '
jpayne@69 7924 'class has an\n'
jpayne@69 7925 ' "__init__()" method, the derived class’s "__init__()" '
jpayne@69 7926 'method, if\n'
jpayne@69 7927 ' any, must explicitly call it to ensure proper '
jpayne@69 7928 'initialization of the\n'
jpayne@69 7929 ' base class part of the instance; for example:\n'
jpayne@69 7930 ' "super().__init__([args...])".\n'
jpayne@69 7931 '\n'
jpayne@69 7932 ' Because "__new__()" and "__init__()" work together in '
jpayne@69 7933 'constructing\n'
jpayne@69 7934 ' objects ("__new__()" to create it, and "__init__()" to '
jpayne@69 7935 'customize\n'
jpayne@69 7936 ' it), no non-"None" value may be returned by "__init__()"; '
jpayne@69 7937 'doing so\n'
jpayne@69 7938 ' will cause a "TypeError" to be raised at runtime.\n'
jpayne@69 7939 '\n'
jpayne@69 7940 'object.__del__(self)\n'
jpayne@69 7941 '\n'
jpayne@69 7942 ' Called when the instance is about to be destroyed. This '
jpayne@69 7943 'is also\n'
jpayne@69 7944 ' called a finalizer or (improperly) a destructor. If a '
jpayne@69 7945 'base class\n'
jpayne@69 7946 ' has a "__del__()" method, the derived class’s "__del__()" '
jpayne@69 7947 'method,\n'
jpayne@69 7948 ' if any, must explicitly call it to ensure proper deletion '
jpayne@69 7949 'of the\n'
jpayne@69 7950 ' base class part of the instance.\n'
jpayne@69 7951 '\n'
jpayne@69 7952 ' It is possible (though not recommended!) for the '
jpayne@69 7953 '"__del__()" method\n'
jpayne@69 7954 ' to postpone destruction of the instance by creating a new '
jpayne@69 7955 'reference\n'
jpayne@69 7956 ' to it. This is called object *resurrection*. It is\n'
jpayne@69 7957 ' implementation-dependent whether "__del__()" is called a '
jpayne@69 7958 'second\n'
jpayne@69 7959 ' time when a resurrected object is about to be destroyed; '
jpayne@69 7960 'the\n'
jpayne@69 7961 ' current *CPython* implementation only calls it once.\n'
jpayne@69 7962 '\n'
jpayne@69 7963 ' It is not guaranteed that "__del__()" methods are called '
jpayne@69 7964 'for\n'
jpayne@69 7965 ' objects that still exist when the interpreter exits.\n'
jpayne@69 7966 '\n'
jpayne@69 7967 ' Note: "del x" doesn’t directly call "x.__del__()" — the '
jpayne@69 7968 'former\n'
jpayne@69 7969 ' decrements the reference count for "x" by one, and the '
jpayne@69 7970 'latter is\n'
jpayne@69 7971 ' only called when "x"’s reference count reaches zero.\n'
jpayne@69 7972 '\n'
jpayne@69 7973 ' **CPython implementation detail:** It is possible for a '
jpayne@69 7974 'reference\n'
jpayne@69 7975 ' cycle to prevent the reference count of an object from '
jpayne@69 7976 'going to\n'
jpayne@69 7977 ' zero. In this case, the cycle will be later detected and '
jpayne@69 7978 'deleted\n'
jpayne@69 7979 ' by the *cyclic garbage collector*. A common cause of '
jpayne@69 7980 'reference\n'
jpayne@69 7981 ' cycles is when an exception has been caught in a local '
jpayne@69 7982 'variable.\n'
jpayne@69 7983 ' The frame’s locals then reference the exception, which '
jpayne@69 7984 'references\n'
jpayne@69 7985 ' its own traceback, which references the locals of all '
jpayne@69 7986 'frames caught\n'
jpayne@69 7987 ' in the traceback.\n'
jpayne@69 7988 '\n'
jpayne@69 7989 ' See also: Documentation for the "gc" module.\n'
jpayne@69 7990 '\n'
jpayne@69 7991 ' Warning: Due to the precarious circumstances under which\n'
jpayne@69 7992 ' "__del__()" methods are invoked, exceptions that occur '
jpayne@69 7993 'during\n'
jpayne@69 7994 ' their execution are ignored, and a warning is printed '
jpayne@69 7995 'to\n'
jpayne@69 7996 ' "sys.stderr" instead. In particular:\n'
jpayne@69 7997 '\n'
jpayne@69 7998 ' * "__del__()" can be invoked when arbitrary code is '
jpayne@69 7999 'being\n'
jpayne@69 8000 ' executed, including from any arbitrary thread. If '
jpayne@69 8001 '"__del__()"\n'
jpayne@69 8002 ' needs to take a lock or invoke any other blocking '
jpayne@69 8003 'resource, it\n'
jpayne@69 8004 ' may deadlock as the resource may already be taken by '
jpayne@69 8005 'the code\n'
jpayne@69 8006 ' that gets interrupted to execute "__del__()".\n'
jpayne@69 8007 '\n'
jpayne@69 8008 ' * "__del__()" can be executed during interpreter '
jpayne@69 8009 'shutdown. As\n'
jpayne@69 8010 ' a consequence, the global variables it needs to '
jpayne@69 8011 'access\n'
jpayne@69 8012 ' (including other modules) may already have been '
jpayne@69 8013 'deleted or set\n'
jpayne@69 8014 ' to "None". Python guarantees that globals whose name '
jpayne@69 8015 'begins\n'
jpayne@69 8016 ' with a single underscore are deleted from their '
jpayne@69 8017 'module before\n'
jpayne@69 8018 ' other globals are deleted; if no other references to '
jpayne@69 8019 'such\n'
jpayne@69 8020 ' globals exist, this may help in assuring that '
jpayne@69 8021 'imported modules\n'
jpayne@69 8022 ' are still available at the time when the "__del__()" '
jpayne@69 8023 'method is\n'
jpayne@69 8024 ' called.\n'
jpayne@69 8025 '\n'
jpayne@69 8026 'object.__repr__(self)\n'
jpayne@69 8027 '\n'
jpayne@69 8028 ' Called by the "repr()" built-in function to compute the '
jpayne@69 8029 '“official”\n'
jpayne@69 8030 ' string representation of an object. If at all possible, '
jpayne@69 8031 'this\n'
jpayne@69 8032 ' should look like a valid Python expression that could be '
jpayne@69 8033 'used to\n'
jpayne@69 8034 ' recreate an object with the same value (given an '
jpayne@69 8035 'appropriate\n'
jpayne@69 8036 ' environment). If this is not possible, a string of the '
jpayne@69 8037 'form\n'
jpayne@69 8038 ' "<...some useful description...>" should be returned. The '
jpayne@69 8039 'return\n'
jpayne@69 8040 ' value must be a string object. If a class defines '
jpayne@69 8041 '"__repr__()" but\n'
jpayne@69 8042 ' not "__str__()", then "__repr__()" is also used when an '
jpayne@69 8043 '“informal”\n'
jpayne@69 8044 ' string representation of instances of that class is '
jpayne@69 8045 'required.\n'
jpayne@69 8046 '\n'
jpayne@69 8047 ' This is typically used for debugging, so it is important '
jpayne@69 8048 'that the\n'
jpayne@69 8049 ' representation is information-rich and unambiguous.\n'
jpayne@69 8050 '\n'
jpayne@69 8051 'object.__str__(self)\n'
jpayne@69 8052 '\n'
jpayne@69 8053 ' Called by "str(object)" and the built-in functions '
jpayne@69 8054 '"format()" and\n'
jpayne@69 8055 ' "print()" to compute the “informal” or nicely printable '
jpayne@69 8056 'string\n'
jpayne@69 8057 ' representation of an object. The return value must be a '
jpayne@69 8058 'string\n'
jpayne@69 8059 ' object.\n'
jpayne@69 8060 '\n'
jpayne@69 8061 ' This method differs from "object.__repr__()" in that '
jpayne@69 8062 'there is no\n'
jpayne@69 8063 ' expectation that "__str__()" return a valid Python '
jpayne@69 8064 'expression: a\n'
jpayne@69 8065 ' more convenient or concise representation can be used.\n'
jpayne@69 8066 '\n'
jpayne@69 8067 ' The default implementation defined by the built-in type '
jpayne@69 8068 '"object"\n'
jpayne@69 8069 ' calls "object.__repr__()".\n'
jpayne@69 8070 '\n'
jpayne@69 8071 'object.__bytes__(self)\n'
jpayne@69 8072 '\n'
jpayne@69 8073 ' Called by bytes to compute a byte-string representation '
jpayne@69 8074 'of an\n'
jpayne@69 8075 ' object. This should return a "bytes" object.\n'
jpayne@69 8076 '\n'
jpayne@69 8077 'object.__format__(self, format_spec)\n'
jpayne@69 8078 '\n'
jpayne@69 8079 ' Called by the "format()" built-in function, and by '
jpayne@69 8080 'extension,\n'
jpayne@69 8081 ' evaluation of formatted string literals and the '
jpayne@69 8082 '"str.format()"\n'
jpayne@69 8083 ' method, to produce a “formatted” string representation of '
jpayne@69 8084 'an\n'
jpayne@69 8085 ' object. The *format_spec* argument is a string that '
jpayne@69 8086 'contains a\n'
jpayne@69 8087 ' description of the formatting options desired. The '
jpayne@69 8088 'interpretation\n'
jpayne@69 8089 ' of the *format_spec* argument is up to the type '
jpayne@69 8090 'implementing\n'
jpayne@69 8091 ' "__format__()", however most classes will either '
jpayne@69 8092 'delegate\n'
jpayne@69 8093 ' formatting to one of the built-in types, or use a '
jpayne@69 8094 'similar\n'
jpayne@69 8095 ' formatting option syntax.\n'
jpayne@69 8096 '\n'
jpayne@69 8097 ' See Format Specification Mini-Language for a description '
jpayne@69 8098 'of the\n'
jpayne@69 8099 ' standard formatting syntax.\n'
jpayne@69 8100 '\n'
jpayne@69 8101 ' The return value must be a string object.\n'
jpayne@69 8102 '\n'
jpayne@69 8103 ' Changed in version 3.4: The __format__ method of "object" '
jpayne@69 8104 'itself\n'
jpayne@69 8105 ' raises a "TypeError" if passed any non-empty string.\n'
jpayne@69 8106 '\n'
jpayne@69 8107 ' Changed in version 3.7: "object.__format__(x, \'\')" is '
jpayne@69 8108 'now\n'
jpayne@69 8109 ' equivalent to "str(x)" rather than "format(str(self), '
jpayne@69 8110 '\'\')".\n'
jpayne@69 8111 '\n'
jpayne@69 8112 'object.__lt__(self, other)\n'
jpayne@69 8113 'object.__le__(self, other)\n'
jpayne@69 8114 'object.__eq__(self, other)\n'
jpayne@69 8115 'object.__ne__(self, other)\n'
jpayne@69 8116 'object.__gt__(self, other)\n'
jpayne@69 8117 'object.__ge__(self, other)\n'
jpayne@69 8118 '\n'
jpayne@69 8119 ' These are the so-called “rich comparison” methods. The\n'
jpayne@69 8120 ' correspondence between operator symbols and method names '
jpayne@69 8121 'is as\n'
jpayne@69 8122 ' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls '
jpayne@69 8123 '"x.__le__(y)",\n'
jpayne@69 8124 ' "x==y" calls "x.__eq__(y)", "x!=y" calls "x.__ne__(y)", '
jpayne@69 8125 '"x>y" calls\n'
jpayne@69 8126 ' "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".\n'
jpayne@69 8127 '\n'
jpayne@69 8128 ' A rich comparison method may return the singleton '
jpayne@69 8129 '"NotImplemented"\n'
jpayne@69 8130 ' if it does not implement the operation for a given pair '
jpayne@69 8131 'of\n'
jpayne@69 8132 ' arguments. By convention, "False" and "True" are returned '
jpayne@69 8133 'for a\n'
jpayne@69 8134 ' successful comparison. However, these methods can return '
jpayne@69 8135 'any value,\n'
jpayne@69 8136 ' so if the comparison operator is used in a Boolean '
jpayne@69 8137 'context (e.g.,\n'
jpayne@69 8138 ' in the condition of an "if" statement), Python will call '
jpayne@69 8139 '"bool()"\n'
jpayne@69 8140 ' on the value to determine if the result is true or '
jpayne@69 8141 'false.\n'
jpayne@69 8142 '\n'
jpayne@69 8143 ' By default, "__ne__()" delegates to "__eq__()" and '
jpayne@69 8144 'inverts the\n'
jpayne@69 8145 ' result unless it is "NotImplemented". There are no other '
jpayne@69 8146 'implied\n'
jpayne@69 8147 ' relationships among the comparison operators, for '
jpayne@69 8148 'example, the\n'
jpayne@69 8149 ' truth of "(x<y or x==y)" does not imply "x<=y". To '
jpayne@69 8150 'automatically\n'
jpayne@69 8151 ' generate ordering operations from a single root '
jpayne@69 8152 'operation, see\n'
jpayne@69 8153 ' "functools.total_ordering()".\n'
jpayne@69 8154 '\n'
jpayne@69 8155 ' See the paragraph on "__hash__()" for some important '
jpayne@69 8156 'notes on\n'
jpayne@69 8157 ' creating *hashable* objects which support custom '
jpayne@69 8158 'comparison\n'
jpayne@69 8159 ' operations and are usable as dictionary keys.\n'
jpayne@69 8160 '\n'
jpayne@69 8161 ' There are no swapped-argument versions of these methods '
jpayne@69 8162 '(to be used\n'
jpayne@69 8163 ' when the left argument does not support the operation but '
jpayne@69 8164 'the right\n'
jpayne@69 8165 ' argument does); rather, "__lt__()" and "__gt__()" are '
jpayne@69 8166 'each other’s\n'
jpayne@69 8167 ' reflection, "__le__()" and "__ge__()" are each other’s '
jpayne@69 8168 'reflection,\n'
jpayne@69 8169 ' and "__eq__()" and "__ne__()" are their own reflection. '
jpayne@69 8170 'If the\n'
jpayne@69 8171 ' operands are of different types, and right operand’s type '
jpayne@69 8172 'is a\n'
jpayne@69 8173 ' direct or indirect subclass of the left operand’s type, '
jpayne@69 8174 'the\n'
jpayne@69 8175 ' reflected method of the right operand has priority, '
jpayne@69 8176 'otherwise the\n'
jpayne@69 8177 ' left operand’s method has priority. Virtual subclassing '
jpayne@69 8178 'is not\n'
jpayne@69 8179 ' considered.\n'
jpayne@69 8180 '\n'
jpayne@69 8181 'object.__hash__(self)\n'
jpayne@69 8182 '\n'
jpayne@69 8183 ' Called by built-in function "hash()" and for operations '
jpayne@69 8184 'on members\n'
jpayne@69 8185 ' of hashed collections including "set", "frozenset", and '
jpayne@69 8186 '"dict".\n'
jpayne@69 8187 ' "__hash__()" should return an integer. The only required '
jpayne@69 8188 'property\n'
jpayne@69 8189 ' is that objects which compare equal have the same hash '
jpayne@69 8190 'value; it is\n'
jpayne@69 8191 ' advised to mix together the hash values of the components '
jpayne@69 8192 'of the\n'
jpayne@69 8193 ' object that also play a part in comparison of objects by '
jpayne@69 8194 'packing\n'
jpayne@69 8195 ' them into a tuple and hashing the tuple. Example:\n'
jpayne@69 8196 '\n'
jpayne@69 8197 ' def __hash__(self):\n'
jpayne@69 8198 ' return hash((self.name, self.nick, self.color))\n'
jpayne@69 8199 '\n'
jpayne@69 8200 ' Note: "hash()" truncates the value returned from an '
jpayne@69 8201 'object’s\n'
jpayne@69 8202 ' custom "__hash__()" method to the size of a '
jpayne@69 8203 '"Py_ssize_t". This\n'
jpayne@69 8204 ' is typically 8 bytes on 64-bit builds and 4 bytes on '
jpayne@69 8205 '32-bit\n'
jpayne@69 8206 ' builds. If an object’s "__hash__()" must interoperate '
jpayne@69 8207 'on builds\n'
jpayne@69 8208 ' of different bit sizes, be sure to check the width on '
jpayne@69 8209 'all\n'
jpayne@69 8210 ' supported builds. An easy way to do this is with '
jpayne@69 8211 '"python -c\n'
jpayne@69 8212 ' "import sys; print(sys.hash_info.width)"".\n'
jpayne@69 8213 '\n'
jpayne@69 8214 ' If a class does not define an "__eq__()" method it should '
jpayne@69 8215 'not\n'
jpayne@69 8216 ' define a "__hash__()" operation either; if it defines '
jpayne@69 8217 '"__eq__()"\n'
jpayne@69 8218 ' but not "__hash__()", its instances will not be usable as '
jpayne@69 8219 'items in\n'
jpayne@69 8220 ' hashable collections. If a class defines mutable objects '
jpayne@69 8221 'and\n'
jpayne@69 8222 ' implements an "__eq__()" method, it should not implement\n'
jpayne@69 8223 ' "__hash__()", since the implementation of hashable '
jpayne@69 8224 'collections\n'
jpayne@69 8225 ' requires that a key’s hash value is immutable (if the '
jpayne@69 8226 'object’s hash\n'
jpayne@69 8227 ' value changes, it will be in the wrong hash bucket).\n'
jpayne@69 8228 '\n'
jpayne@69 8229 ' User-defined classes have "__eq__()" and "__hash__()" '
jpayne@69 8230 'methods by\n'
jpayne@69 8231 ' default; with them, all objects compare unequal (except '
jpayne@69 8232 'with\n'
jpayne@69 8233 ' themselves) and "x.__hash__()" returns an appropriate '
jpayne@69 8234 'value such\n'
jpayne@69 8235 ' that "x == y" implies both that "x is y" and "hash(x) == '
jpayne@69 8236 'hash(y)".\n'
jpayne@69 8237 '\n'
jpayne@69 8238 ' A class that overrides "__eq__()" and does not define '
jpayne@69 8239 '"__hash__()"\n'
jpayne@69 8240 ' will have its "__hash__()" implicitly set to "None". '
jpayne@69 8241 'When the\n'
jpayne@69 8242 ' "__hash__()" method of a class is "None", instances of '
jpayne@69 8243 'the class\n'
jpayne@69 8244 ' will raise an appropriate "TypeError" when a program '
jpayne@69 8245 'attempts to\n'
jpayne@69 8246 ' retrieve their hash value, and will also be correctly '
jpayne@69 8247 'identified as\n'
jpayne@69 8248 ' unhashable when checking "isinstance(obj,\n'
jpayne@69 8249 ' collections.abc.Hashable)".\n'
jpayne@69 8250 '\n'
jpayne@69 8251 ' If a class that overrides "__eq__()" needs to retain the\n'
jpayne@69 8252 ' implementation of "__hash__()" from a parent class, the '
jpayne@69 8253 'interpreter\n'
jpayne@69 8254 ' must be told this explicitly by setting "__hash__ =\n'
jpayne@69 8255 ' <ParentClass>.__hash__".\n'
jpayne@69 8256 '\n'
jpayne@69 8257 ' If a class that does not override "__eq__()" wishes to '
jpayne@69 8258 'suppress\n'
jpayne@69 8259 ' hash support, it should include "__hash__ = None" in the '
jpayne@69 8260 'class\n'
jpayne@69 8261 ' definition. A class which defines its own "__hash__()" '
jpayne@69 8262 'that\n'
jpayne@69 8263 ' explicitly raises a "TypeError" would be incorrectly '
jpayne@69 8264 'identified as\n'
jpayne@69 8265 ' hashable by an "isinstance(obj, '
jpayne@69 8266 'collections.abc.Hashable)" call.\n'
jpayne@69 8267 '\n'
jpayne@69 8268 ' Note: By default, the "__hash__()" values of str and '
jpayne@69 8269 'bytes\n'
jpayne@69 8270 ' objects are “salted” with an unpredictable random '
jpayne@69 8271 'value.\n'
jpayne@69 8272 ' Although they remain constant within an individual '
jpayne@69 8273 'Python\n'
jpayne@69 8274 ' process, they are not predictable between repeated '
jpayne@69 8275 'invocations of\n'
jpayne@69 8276 ' Python.This is intended to provide protection against a '
jpayne@69 8277 'denial-\n'
jpayne@69 8278 ' of-service caused by carefully-chosen inputs that '
jpayne@69 8279 'exploit the\n'
jpayne@69 8280 ' worst case performance of a dict insertion, O(n^2) '
jpayne@69 8281 'complexity.\n'
jpayne@69 8282 ' See http://www.ocert.org/advisories/ocert-2011-003.html '
jpayne@69 8283 'for\n'
jpayne@69 8284 ' details.Changing hash values affects the iteration '
jpayne@69 8285 'order of sets.\n'
jpayne@69 8286 ' Python has never made guarantees about this ordering '
jpayne@69 8287 '(and it\n'
jpayne@69 8288 ' typically varies between 32-bit and 64-bit builds).See '
jpayne@69 8289 'also\n'
jpayne@69 8290 ' "PYTHONHASHSEED".\n'
jpayne@69 8291 '\n'
jpayne@69 8292 ' Changed in version 3.3: Hash randomization is enabled by '
jpayne@69 8293 'default.\n'
jpayne@69 8294 '\n'
jpayne@69 8295 'object.__bool__(self)\n'
jpayne@69 8296 '\n'
jpayne@69 8297 ' Called to implement truth value testing and the built-in '
jpayne@69 8298 'operation\n'
jpayne@69 8299 ' "bool()"; should return "False" or "True". When this '
jpayne@69 8300 'method is not\n'
jpayne@69 8301 ' defined, "__len__()" is called, if it is defined, and the '
jpayne@69 8302 'object is\n'
jpayne@69 8303 ' considered true if its result is nonzero. If a class '
jpayne@69 8304 'defines\n'
jpayne@69 8305 ' neither "__len__()" nor "__bool__()", all its instances '
jpayne@69 8306 'are\n'
jpayne@69 8307 ' considered true.\n'
jpayne@69 8308 '\n'
jpayne@69 8309 '\n'
jpayne@69 8310 'Customizing attribute access\n'
jpayne@69 8311 '============================\n'
jpayne@69 8312 '\n'
jpayne@69 8313 'The following methods can be defined to customize the '
jpayne@69 8314 'meaning of\n'
jpayne@69 8315 'attribute access (use of, assignment to, or deletion of '
jpayne@69 8316 '"x.name") for\n'
jpayne@69 8317 'class instances.\n'
jpayne@69 8318 '\n'
jpayne@69 8319 'object.__getattr__(self, name)\n'
jpayne@69 8320 '\n'
jpayne@69 8321 ' Called when the default attribute access fails with an\n'
jpayne@69 8322 ' "AttributeError" (either "__getattribute__()" raises an\n'
jpayne@69 8323 ' "AttributeError" because *name* is not an instance '
jpayne@69 8324 'attribute or an\n'
jpayne@69 8325 ' attribute in the class tree for "self"; or "__get__()" of '
jpayne@69 8326 'a *name*\n'
jpayne@69 8327 ' property raises "AttributeError"). This method should '
jpayne@69 8328 'either\n'
jpayne@69 8329 ' return the (computed) attribute value or raise an '
jpayne@69 8330 '"AttributeError"\n'
jpayne@69 8331 ' exception.\n'
jpayne@69 8332 '\n'
jpayne@69 8333 ' Note that if the attribute is found through the normal '
jpayne@69 8334 'mechanism,\n'
jpayne@69 8335 ' "__getattr__()" is not called. (This is an intentional '
jpayne@69 8336 'asymmetry\n'
jpayne@69 8337 ' between "__getattr__()" and "__setattr__()".) This is '
jpayne@69 8338 'done both for\n'
jpayne@69 8339 ' efficiency reasons and because otherwise "__getattr__()" '
jpayne@69 8340 'would have\n'
jpayne@69 8341 ' no way to access other attributes of the instance. Note '
jpayne@69 8342 'that at\n'
jpayne@69 8343 ' least for instance variables, you can fake total control '
jpayne@69 8344 'by not\n'
jpayne@69 8345 ' inserting any values in the instance attribute dictionary '
jpayne@69 8346 '(but\n'
jpayne@69 8347 ' instead inserting them in another object). See the\n'
jpayne@69 8348 ' "__getattribute__()" method below for a way to actually '
jpayne@69 8349 'get total\n'
jpayne@69 8350 ' control over attribute access.\n'
jpayne@69 8351 '\n'
jpayne@69 8352 'object.__getattribute__(self, name)\n'
jpayne@69 8353 '\n'
jpayne@69 8354 ' Called unconditionally to implement attribute accesses '
jpayne@69 8355 'for\n'
jpayne@69 8356 ' instances of the class. If the class also defines '
jpayne@69 8357 '"__getattr__()",\n'
jpayne@69 8358 ' the latter will not be called unless "__getattribute__()" '
jpayne@69 8359 'either\n'
jpayne@69 8360 ' calls it explicitly or raises an "AttributeError". This '
jpayne@69 8361 'method\n'
jpayne@69 8362 ' should return the (computed) attribute value or raise an\n'
jpayne@69 8363 ' "AttributeError" exception. In order to avoid infinite '
jpayne@69 8364 'recursion in\n'
jpayne@69 8365 ' this method, its implementation should always call the '
jpayne@69 8366 'base class\n'
jpayne@69 8367 ' method with the same name to access any attributes it '
jpayne@69 8368 'needs, for\n'
jpayne@69 8369 ' example, "object.__getattribute__(self, name)".\n'
jpayne@69 8370 '\n'
jpayne@69 8371 ' Note: This method may still be bypassed when looking up '
jpayne@69 8372 'special\n'
jpayne@69 8373 ' methods as the result of implicit invocation via '
jpayne@69 8374 'language syntax\n'
jpayne@69 8375 ' or built-in functions. See Special method lookup.\n'
jpayne@69 8376 '\n'
jpayne@69 8377 'object.__setattr__(self, name, value)\n'
jpayne@69 8378 '\n'
jpayne@69 8379 ' Called when an attribute assignment is attempted. This '
jpayne@69 8380 'is called\n'
jpayne@69 8381 ' instead of the normal mechanism (i.e. store the value in '
jpayne@69 8382 'the\n'
jpayne@69 8383 ' instance dictionary). *name* is the attribute name, '
jpayne@69 8384 '*value* is the\n'
jpayne@69 8385 ' value to be assigned to it.\n'
jpayne@69 8386 '\n'
jpayne@69 8387 ' If "__setattr__()" wants to assign to an instance '
jpayne@69 8388 'attribute, it\n'
jpayne@69 8389 ' should call the base class method with the same name, for '
jpayne@69 8390 'example,\n'
jpayne@69 8391 ' "object.__setattr__(self, name, value)".\n'
jpayne@69 8392 '\n'
jpayne@69 8393 'object.__delattr__(self, name)\n'
jpayne@69 8394 '\n'
jpayne@69 8395 ' Like "__setattr__()" but for attribute deletion instead '
jpayne@69 8396 'of\n'
jpayne@69 8397 ' assignment. This should only be implemented if "del '
jpayne@69 8398 'obj.name" is\n'
jpayne@69 8399 ' meaningful for the object.\n'
jpayne@69 8400 '\n'
jpayne@69 8401 'object.__dir__(self)\n'
jpayne@69 8402 '\n'
jpayne@69 8403 ' Called when "dir()" is called on the object. A sequence '
jpayne@69 8404 'must be\n'
jpayne@69 8405 ' returned. "dir()" converts the returned sequence to a '
jpayne@69 8406 'list and\n'
jpayne@69 8407 ' sorts it.\n'
jpayne@69 8408 '\n'
jpayne@69 8409 '\n'
jpayne@69 8410 'Customizing module attribute access\n'
jpayne@69 8411 '-----------------------------------\n'
jpayne@69 8412 '\n'
jpayne@69 8413 'Special names "__getattr__" and "__dir__" can be also used '
jpayne@69 8414 'to\n'
jpayne@69 8415 'customize access to module attributes. The "__getattr__" '
jpayne@69 8416 'function at\n'
jpayne@69 8417 'the module level should accept one argument which is the '
jpayne@69 8418 'name of an\n'
jpayne@69 8419 'attribute and return the computed value or raise an '
jpayne@69 8420 '"AttributeError".\n'
jpayne@69 8421 'If an attribute is not found on a module object through the '
jpayne@69 8422 'normal\n'
jpayne@69 8423 'lookup, i.e. "object.__getattribute__()", then "__getattr__" '
jpayne@69 8424 'is\n'
jpayne@69 8425 'searched in the module "__dict__" before raising an '
jpayne@69 8426 '"AttributeError".\n'
jpayne@69 8427 'If found, it is called with the attribute name and the '
jpayne@69 8428 'result is\n'
jpayne@69 8429 'returned.\n'
jpayne@69 8430 '\n'
jpayne@69 8431 'The "__dir__" function should accept no arguments, and '
jpayne@69 8432 'return a\n'
jpayne@69 8433 'sequence of strings that represents the names accessible on '
jpayne@69 8434 'module. If\n'
jpayne@69 8435 'present, this function overrides the standard "dir()" search '
jpayne@69 8436 'on a\n'
jpayne@69 8437 'module.\n'
jpayne@69 8438 '\n'
jpayne@69 8439 'For a more fine grained customization of the module behavior '
jpayne@69 8440 '(setting\n'
jpayne@69 8441 'attributes, properties, etc.), one can set the "__class__" '
jpayne@69 8442 'attribute\n'
jpayne@69 8443 'of a module object to a subclass of "types.ModuleType". For '
jpayne@69 8444 'example:\n'
jpayne@69 8445 '\n'
jpayne@69 8446 ' import sys\n'
jpayne@69 8447 ' from types import ModuleType\n'
jpayne@69 8448 '\n'
jpayne@69 8449 ' class VerboseModule(ModuleType):\n'
jpayne@69 8450 ' def __repr__(self):\n'
jpayne@69 8451 " return f'Verbose {self.__name__}'\n"
jpayne@69 8452 '\n'
jpayne@69 8453 ' def __setattr__(self, attr, value):\n'
jpayne@69 8454 " print(f'Setting {attr}...')\n"
jpayne@69 8455 ' super().__setattr__(attr, value)\n'
jpayne@69 8456 '\n'
jpayne@69 8457 ' sys.modules[__name__].__class__ = VerboseModule\n'
jpayne@69 8458 '\n'
jpayne@69 8459 'Note: Defining module "__getattr__" and setting module '
jpayne@69 8460 '"__class__"\n'
jpayne@69 8461 ' only affect lookups made using the attribute access syntax '
jpayne@69 8462 '–\n'
jpayne@69 8463 ' directly accessing the module globals (whether by code '
jpayne@69 8464 'within the\n'
jpayne@69 8465 ' module, or via a reference to the module’s globals '
jpayne@69 8466 'dictionary) is\n'
jpayne@69 8467 ' unaffected.\n'
jpayne@69 8468 '\n'
jpayne@69 8469 'Changed in version 3.5: "__class__" module attribute is now '
jpayne@69 8470 'writable.\n'
jpayne@69 8471 '\n'
jpayne@69 8472 'New in version 3.7: "__getattr__" and "__dir__" module '
jpayne@69 8473 'attributes.\n'
jpayne@69 8474 '\n'
jpayne@69 8475 'See also:\n'
jpayne@69 8476 '\n'
jpayne@69 8477 ' **PEP 562** - Module __getattr__ and __dir__\n'
jpayne@69 8478 ' Describes the "__getattr__" and "__dir__" functions on '
jpayne@69 8479 'modules.\n'
jpayne@69 8480 '\n'
jpayne@69 8481 '\n'
jpayne@69 8482 'Implementing Descriptors\n'
jpayne@69 8483 '------------------------\n'
jpayne@69 8484 '\n'
jpayne@69 8485 'The following methods only apply when an instance of the '
jpayne@69 8486 'class\n'
jpayne@69 8487 'containing the method (a so-called *descriptor* class) '
jpayne@69 8488 'appears in an\n'
jpayne@69 8489 '*owner* class (the descriptor must be in either the owner’s '
jpayne@69 8490 'class\n'
jpayne@69 8491 'dictionary or in the class dictionary for one of its '
jpayne@69 8492 'parents). In the\n'
jpayne@69 8493 'examples below, “the attribute” refers to the attribute '
jpayne@69 8494 'whose name is\n'
jpayne@69 8495 'the key of the property in the owner class’ "__dict__".\n'
jpayne@69 8496 '\n'
jpayne@69 8497 'object.__get__(self, instance, owner=None)\n'
jpayne@69 8498 '\n'
jpayne@69 8499 ' Called to get the attribute of the owner class (class '
jpayne@69 8500 'attribute\n'
jpayne@69 8501 ' access) or of an instance of that class (instance '
jpayne@69 8502 'attribute\n'
jpayne@69 8503 ' access). The optional *owner* argument is the owner '
jpayne@69 8504 'class, while\n'
jpayne@69 8505 ' *instance* is the instance that the attribute was '
jpayne@69 8506 'accessed through,\n'
jpayne@69 8507 ' or "None" when the attribute is accessed through the '
jpayne@69 8508 '*owner*.\n'
jpayne@69 8509 '\n'
jpayne@69 8510 ' This method should return the computed attribute value or '
jpayne@69 8511 'raise an\n'
jpayne@69 8512 ' "AttributeError" exception.\n'
jpayne@69 8513 '\n'
jpayne@69 8514 ' **PEP 252** specifies that "__get__()" is callable with '
jpayne@69 8515 'one or two\n'
jpayne@69 8516 ' arguments. Python’s own built-in descriptors support '
jpayne@69 8517 'this\n'
jpayne@69 8518 ' specification; however, it is likely that some '
jpayne@69 8519 'third-party tools\n'
jpayne@69 8520 ' have descriptors that require both arguments. Python’s '
jpayne@69 8521 'own\n'
jpayne@69 8522 ' "__getattribute__()" implementation always passes in both '
jpayne@69 8523 'arguments\n'
jpayne@69 8524 ' whether they are required or not.\n'
jpayne@69 8525 '\n'
jpayne@69 8526 'object.__set__(self, instance, value)\n'
jpayne@69 8527 '\n'
jpayne@69 8528 ' Called to set the attribute on an instance *instance* of '
jpayne@69 8529 'the owner\n'
jpayne@69 8530 ' class to a new value, *value*.\n'
jpayne@69 8531 '\n'
jpayne@69 8532 ' Note, adding "__set__()" or "__delete__()" changes the '
jpayne@69 8533 'kind of\n'
jpayne@69 8534 ' descriptor to a “data descriptor”. See Invoking '
jpayne@69 8535 'Descriptors for\n'
jpayne@69 8536 ' more details.\n'
jpayne@69 8537 '\n'
jpayne@69 8538 'object.__delete__(self, instance)\n'
jpayne@69 8539 '\n'
jpayne@69 8540 ' Called to delete the attribute on an instance *instance* '
jpayne@69 8541 'of the\n'
jpayne@69 8542 ' owner class.\n'
jpayne@69 8543 '\n'
jpayne@69 8544 'object.__set_name__(self, owner, name)\n'
jpayne@69 8545 '\n'
jpayne@69 8546 ' Called at the time the owning class *owner* is created. '
jpayne@69 8547 'The\n'
jpayne@69 8548 ' descriptor has been assigned to *name*.\n'
jpayne@69 8549 '\n'
jpayne@69 8550 ' Note: "__set_name__()" is only called implicitly as part '
jpayne@69 8551 'of the\n'
jpayne@69 8552 ' "type" constructor, so it will need to be called '
jpayne@69 8553 'explicitly with\n'
jpayne@69 8554 ' the appropriate parameters when a descriptor is added '
jpayne@69 8555 'to a class\n'
jpayne@69 8556 ' after initial creation:\n'
jpayne@69 8557 '\n'
jpayne@69 8558 ' class A:\n'
jpayne@69 8559 ' pass\n'
jpayne@69 8560 ' descr = custom_descriptor()\n'
jpayne@69 8561 ' A.attr = descr\n'
jpayne@69 8562 " descr.__set_name__(A, 'attr')\n"
jpayne@69 8563 '\n'
jpayne@69 8564 ' See Creating the class object for more details.\n'
jpayne@69 8565 '\n'
jpayne@69 8566 ' New in version 3.6.\n'
jpayne@69 8567 '\n'
jpayne@69 8568 'The attribute "__objclass__" is interpreted by the "inspect" '
jpayne@69 8569 'module as\n'
jpayne@69 8570 'specifying the class where this object was defined (setting '
jpayne@69 8571 'this\n'
jpayne@69 8572 'appropriately can assist in runtime introspection of dynamic '
jpayne@69 8573 'class\n'
jpayne@69 8574 'attributes). For callables, it may indicate that an instance '
jpayne@69 8575 'of the\n'
jpayne@69 8576 'given type (or a subclass) is expected or required as the '
jpayne@69 8577 'first\n'
jpayne@69 8578 'positional argument (for example, CPython sets this '
jpayne@69 8579 'attribute for\n'
jpayne@69 8580 'unbound methods that are implemented in C).\n'
jpayne@69 8581 '\n'
jpayne@69 8582 '\n'
jpayne@69 8583 'Invoking Descriptors\n'
jpayne@69 8584 '--------------------\n'
jpayne@69 8585 '\n'
jpayne@69 8586 'In general, a descriptor is an object attribute with '
jpayne@69 8587 '“binding\n'
jpayne@69 8588 'behavior”, one whose attribute access has been overridden by '
jpayne@69 8589 'methods\n'
jpayne@69 8590 'in the descriptor protocol: "__get__()", "__set__()", and\n'
jpayne@69 8591 '"__delete__()". If any of those methods are defined for an '
jpayne@69 8592 'object, it\n'
jpayne@69 8593 'is said to be a descriptor.\n'
jpayne@69 8594 '\n'
jpayne@69 8595 'The default behavior for attribute access is to get, set, or '
jpayne@69 8596 'delete\n'
jpayne@69 8597 'the attribute from an object’s dictionary. For instance, '
jpayne@69 8598 '"a.x" has a\n'
jpayne@69 8599 'lookup chain starting with "a.__dict__[\'x\']", then\n'
jpayne@69 8600 '"type(a).__dict__[\'x\']", and continuing through the base '
jpayne@69 8601 'classes of\n'
jpayne@69 8602 '"type(a)" excluding metaclasses.\n'
jpayne@69 8603 '\n'
jpayne@69 8604 'However, if the looked-up value is an object defining one of '
jpayne@69 8605 'the\n'
jpayne@69 8606 'descriptor methods, then Python may override the default '
jpayne@69 8607 'behavior and\n'
jpayne@69 8608 'invoke the descriptor method instead. Where this occurs in '
jpayne@69 8609 'the\n'
jpayne@69 8610 'precedence chain depends on which descriptor methods were '
jpayne@69 8611 'defined and\n'
jpayne@69 8612 'how they were called.\n'
jpayne@69 8613 '\n'
jpayne@69 8614 'The starting point for descriptor invocation is a binding, '
jpayne@69 8615 '"a.x". How\n'
jpayne@69 8616 'the arguments are assembled depends on "a":\n'
jpayne@69 8617 '\n'
jpayne@69 8618 'Direct Call\n'
jpayne@69 8619 ' The simplest and least common call is when user code '
jpayne@69 8620 'directly\n'
jpayne@69 8621 ' invokes a descriptor method: "x.__get__(a)".\n'
jpayne@69 8622 '\n'
jpayne@69 8623 'Instance Binding\n'
jpayne@69 8624 ' If binding to an object instance, "a.x" is transformed '
jpayne@69 8625 'into the\n'
jpayne@69 8626 ' call: "type(a).__dict__[\'x\'].__get__(a, type(a))".\n'
jpayne@69 8627 '\n'
jpayne@69 8628 'Class Binding\n'
jpayne@69 8629 ' If binding to a class, "A.x" is transformed into the '
jpayne@69 8630 'call:\n'
jpayne@69 8631 ' "A.__dict__[\'x\'].__get__(None, A)".\n'
jpayne@69 8632 '\n'
jpayne@69 8633 'Super Binding\n'
jpayne@69 8634 ' If "a" is an instance of "super", then the binding '
jpayne@69 8635 '"super(B,\n'
jpayne@69 8636 ' obj).m()" searches "obj.__class__.__mro__" for the base '
jpayne@69 8637 'class "A"\n'
jpayne@69 8638 ' immediately preceding "B" and then invokes the descriptor '
jpayne@69 8639 'with the\n'
jpayne@69 8640 ' call: "A.__dict__[\'m\'].__get__(obj, obj.__class__)".\n'
jpayne@69 8641 '\n'
jpayne@69 8642 'For instance bindings, the precedence of descriptor '
jpayne@69 8643 'invocation depends\n'
jpayne@69 8644 'on the which descriptor methods are defined. A descriptor '
jpayne@69 8645 'can define\n'
jpayne@69 8646 'any combination of "__get__()", "__set__()" and '
jpayne@69 8647 '"__delete__()". If it\n'
jpayne@69 8648 'does not define "__get__()", then accessing the attribute '
jpayne@69 8649 'will return\n'
jpayne@69 8650 'the descriptor object itself unless there is a value in the '
jpayne@69 8651 'object’s\n'
jpayne@69 8652 'instance dictionary. If the descriptor defines "__set__()" '
jpayne@69 8653 'and/or\n'
jpayne@69 8654 '"__delete__()", it is a data descriptor; if it defines '
jpayne@69 8655 'neither, it is\n'
jpayne@69 8656 'a non-data descriptor. Normally, data descriptors define '
jpayne@69 8657 'both\n'
jpayne@69 8658 '"__get__()" and "__set__()", while non-data descriptors have '
jpayne@69 8659 'just the\n'
jpayne@69 8660 '"__get__()" method. Data descriptors with "__set__()" and '
jpayne@69 8661 '"__get__()"\n'
jpayne@69 8662 'defined always override a redefinition in an instance '
jpayne@69 8663 'dictionary. In\n'
jpayne@69 8664 'contrast, non-data descriptors can be overridden by '
jpayne@69 8665 'instances.\n'
jpayne@69 8666 '\n'
jpayne@69 8667 'Python methods (including "staticmethod()" and '
jpayne@69 8668 '"classmethod()") are\n'
jpayne@69 8669 'implemented as non-data descriptors. Accordingly, instances '
jpayne@69 8670 'can\n'
jpayne@69 8671 'redefine and override methods. This allows individual '
jpayne@69 8672 'instances to\n'
jpayne@69 8673 'acquire behaviors that differ from other instances of the '
jpayne@69 8674 'same class.\n'
jpayne@69 8675 '\n'
jpayne@69 8676 'The "property()" function is implemented as a data '
jpayne@69 8677 'descriptor.\n'
jpayne@69 8678 'Accordingly, instances cannot override the behavior of a '
jpayne@69 8679 'property.\n'
jpayne@69 8680 '\n'
jpayne@69 8681 '\n'
jpayne@69 8682 '__slots__\n'
jpayne@69 8683 '---------\n'
jpayne@69 8684 '\n'
jpayne@69 8685 '*__slots__* allow us to explicitly declare data members '
jpayne@69 8686 '(like\n'
jpayne@69 8687 'properties) and deny the creation of *__dict__* and '
jpayne@69 8688 '*__weakref__*\n'
jpayne@69 8689 '(unless explicitly declared in *__slots__* or available in a '
jpayne@69 8690 'parent.)\n'
jpayne@69 8691 '\n'
jpayne@69 8692 'The space saved over using *__dict__* can be significant. '
jpayne@69 8693 'Attribute\n'
jpayne@69 8694 'lookup speed can be significantly improved as well.\n'
jpayne@69 8695 '\n'
jpayne@69 8696 'object.__slots__\n'
jpayne@69 8697 '\n'
jpayne@69 8698 ' This class variable can be assigned a string, iterable, '
jpayne@69 8699 'or sequence\n'
jpayne@69 8700 ' of strings with variable names used by instances. '
jpayne@69 8701 '*__slots__*\n'
jpayne@69 8702 ' reserves space for the declared variables and prevents '
jpayne@69 8703 'the\n'
jpayne@69 8704 ' automatic creation of *__dict__* and *__weakref__* for '
jpayne@69 8705 'each\n'
jpayne@69 8706 ' instance.\n'
jpayne@69 8707 '\n'
jpayne@69 8708 '\n'
jpayne@69 8709 'Notes on using *__slots__*\n'
jpayne@69 8710 '~~~~~~~~~~~~~~~~~~~~~~~~~~\n'
jpayne@69 8711 '\n'
jpayne@69 8712 '* When inheriting from a class without *__slots__*, the '
jpayne@69 8713 '*__dict__*\n'
jpayne@69 8714 ' and *__weakref__* attribute of the instances will always '
jpayne@69 8715 'be\n'
jpayne@69 8716 ' accessible.\n'
jpayne@69 8717 '\n'
jpayne@69 8718 '* Without a *__dict__* variable, instances cannot be '
jpayne@69 8719 'assigned new\n'
jpayne@69 8720 ' variables not listed in the *__slots__* definition. '
jpayne@69 8721 'Attempts to\n'
jpayne@69 8722 ' assign to an unlisted variable name raises '
jpayne@69 8723 '"AttributeError". If\n'
jpayne@69 8724 ' dynamic assignment of new variables is desired, then add\n'
jpayne@69 8725 ' "\'__dict__\'" to the sequence of strings in the '
jpayne@69 8726 '*__slots__*\n'
jpayne@69 8727 ' declaration.\n'
jpayne@69 8728 '\n'
jpayne@69 8729 '* Without a *__weakref__* variable for each instance, '
jpayne@69 8730 'classes\n'
jpayne@69 8731 ' defining *__slots__* do not support weak references to '
jpayne@69 8732 'its\n'
jpayne@69 8733 ' instances. If weak reference support is needed, then add\n'
jpayne@69 8734 ' "\'__weakref__\'" to the sequence of strings in the '
jpayne@69 8735 '*__slots__*\n'
jpayne@69 8736 ' declaration.\n'
jpayne@69 8737 '\n'
jpayne@69 8738 '* *__slots__* are implemented at the class level by '
jpayne@69 8739 'creating\n'
jpayne@69 8740 ' descriptors (Implementing Descriptors) for each variable '
jpayne@69 8741 'name. As a\n'
jpayne@69 8742 ' result, class attributes cannot be used to set default '
jpayne@69 8743 'values for\n'
jpayne@69 8744 ' instance variables defined by *__slots__*; otherwise, the '
jpayne@69 8745 'class\n'
jpayne@69 8746 ' attribute would overwrite the descriptor assignment.\n'
jpayne@69 8747 '\n'
jpayne@69 8748 '* The action of a *__slots__* declaration is not limited to '
jpayne@69 8749 'the\n'
jpayne@69 8750 ' class where it is defined. *__slots__* declared in '
jpayne@69 8751 'parents are\n'
jpayne@69 8752 ' available in child classes. However, child subclasses will '
jpayne@69 8753 'get a\n'
jpayne@69 8754 ' *__dict__* and *__weakref__* unless they also define '
jpayne@69 8755 '*__slots__*\n'
jpayne@69 8756 ' (which should only contain names of any *additional* '
jpayne@69 8757 'slots).\n'
jpayne@69 8758 '\n'
jpayne@69 8759 '* If a class defines a slot also defined in a base class, '
jpayne@69 8760 'the\n'
jpayne@69 8761 ' instance variable defined by the base class slot is '
jpayne@69 8762 'inaccessible\n'
jpayne@69 8763 ' (except by retrieving its descriptor directly from the '
jpayne@69 8764 'base class).\n'
jpayne@69 8765 ' This renders the meaning of the program undefined. In the '
jpayne@69 8766 'future, a\n'
jpayne@69 8767 ' check may be added to prevent this.\n'
jpayne@69 8768 '\n'
jpayne@69 8769 '* Nonempty *__slots__* does not work for classes derived '
jpayne@69 8770 'from\n'
jpayne@69 8771 ' “variable-length” built-in types such as "int", "bytes" '
jpayne@69 8772 'and "tuple".\n'
jpayne@69 8773 '\n'
jpayne@69 8774 '* Any non-string iterable may be assigned to *__slots__*. '
jpayne@69 8775 'Mappings\n'
jpayne@69 8776 ' may also be used; however, in the future, special meaning '
jpayne@69 8777 'may be\n'
jpayne@69 8778 ' assigned to the values corresponding to each key.\n'
jpayne@69 8779 '\n'
jpayne@69 8780 '* *__class__* assignment works only if both classes have the '
jpayne@69 8781 'same\n'
jpayne@69 8782 ' *__slots__*.\n'
jpayne@69 8783 '\n'
jpayne@69 8784 '* Multiple inheritance with multiple slotted parent classes '
jpayne@69 8785 'can be\n'
jpayne@69 8786 ' used, but only one parent is allowed to have attributes '
jpayne@69 8787 'created by\n'
jpayne@69 8788 ' slots (the other bases must have empty slot layouts) - '
jpayne@69 8789 'violations\n'
jpayne@69 8790 ' raise "TypeError".\n'
jpayne@69 8791 '\n'
jpayne@69 8792 '* If an iterator is used for *__slots__* then a descriptor '
jpayne@69 8793 'is\n'
jpayne@69 8794 ' created for each of the iterator’s values. However, the '
jpayne@69 8795 '*__slots__*\n'
jpayne@69 8796 ' attribute will be an empty iterator.\n'
jpayne@69 8797 '\n'
jpayne@69 8798 '\n'
jpayne@69 8799 'Customizing class creation\n'
jpayne@69 8800 '==========================\n'
jpayne@69 8801 '\n'
jpayne@69 8802 'Whenever a class inherits from another class, '
jpayne@69 8803 '*__init_subclass__* is\n'
jpayne@69 8804 'called on that class. This way, it is possible to write '
jpayne@69 8805 'classes which\n'
jpayne@69 8806 'change the behavior of subclasses. This is closely related '
jpayne@69 8807 'to class\n'
jpayne@69 8808 'decorators, but where class decorators only affect the '
jpayne@69 8809 'specific class\n'
jpayne@69 8810 'they’re applied to, "__init_subclass__" solely applies to '
jpayne@69 8811 'future\n'
jpayne@69 8812 'subclasses of the class defining the method.\n'
jpayne@69 8813 '\n'
jpayne@69 8814 'classmethod object.__init_subclass__(cls)\n'
jpayne@69 8815 '\n'
jpayne@69 8816 ' This method is called whenever the containing class is '
jpayne@69 8817 'subclassed.\n'
jpayne@69 8818 ' *cls* is then the new subclass. If defined as a normal '
jpayne@69 8819 'instance\n'
jpayne@69 8820 ' method, this method is implicitly converted to a class '
jpayne@69 8821 'method.\n'
jpayne@69 8822 '\n'
jpayne@69 8823 ' Keyword arguments which are given to a new class are '
jpayne@69 8824 'passed to the\n'
jpayne@69 8825 ' parent’s class "__init_subclass__". For compatibility '
jpayne@69 8826 'with other\n'
jpayne@69 8827 ' classes using "__init_subclass__", one should take out '
jpayne@69 8828 'the needed\n'
jpayne@69 8829 ' keyword arguments and pass the others over to the base '
jpayne@69 8830 'class, as\n'
jpayne@69 8831 ' in:\n'
jpayne@69 8832 '\n'
jpayne@69 8833 ' class Philosopher:\n'
jpayne@69 8834 ' def __init_subclass__(cls, /, default_name, '
jpayne@69 8835 '**kwargs):\n'
jpayne@69 8836 ' super().__init_subclass__(**kwargs)\n'
jpayne@69 8837 ' cls.default_name = default_name\n'
jpayne@69 8838 '\n'
jpayne@69 8839 ' class AustralianPhilosopher(Philosopher, '
jpayne@69 8840 'default_name="Bruce"):\n'
jpayne@69 8841 ' pass\n'
jpayne@69 8842 '\n'
jpayne@69 8843 ' The default implementation "object.__init_subclass__" '
jpayne@69 8844 'does nothing,\n'
jpayne@69 8845 ' but raises an error if it is called with any arguments.\n'
jpayne@69 8846 '\n'
jpayne@69 8847 ' Note: The metaclass hint "metaclass" is consumed by the '
jpayne@69 8848 'rest of\n'
jpayne@69 8849 ' the type machinery, and is never passed to '
jpayne@69 8850 '"__init_subclass__"\n'
jpayne@69 8851 ' implementations. The actual metaclass (rather than the '
jpayne@69 8852 'explicit\n'
jpayne@69 8853 ' hint) can be accessed as "type(cls)".\n'
jpayne@69 8854 '\n'
jpayne@69 8855 ' New in version 3.6.\n'
jpayne@69 8856 '\n'
jpayne@69 8857 '\n'
jpayne@69 8858 'Metaclasses\n'
jpayne@69 8859 '-----------\n'
jpayne@69 8860 '\n'
jpayne@69 8861 'By default, classes are constructed using "type()". The '
jpayne@69 8862 'class body is\n'
jpayne@69 8863 'executed in a new namespace and the class name is bound '
jpayne@69 8864 'locally to the\n'
jpayne@69 8865 'result of "type(name, bases, namespace)".\n'
jpayne@69 8866 '\n'
jpayne@69 8867 'The class creation process can be customized by passing the\n'
jpayne@69 8868 '"metaclass" keyword argument in the class definition line, '
jpayne@69 8869 'or by\n'
jpayne@69 8870 'inheriting from an existing class that included such an '
jpayne@69 8871 'argument. In\n'
jpayne@69 8872 'the following example, both "MyClass" and "MySubclass" are '
jpayne@69 8873 'instances\n'
jpayne@69 8874 'of "Meta":\n'
jpayne@69 8875 '\n'
jpayne@69 8876 ' class Meta(type):\n'
jpayne@69 8877 ' pass\n'
jpayne@69 8878 '\n'
jpayne@69 8879 ' class MyClass(metaclass=Meta):\n'
jpayne@69 8880 ' pass\n'
jpayne@69 8881 '\n'
jpayne@69 8882 ' class MySubclass(MyClass):\n'
jpayne@69 8883 ' pass\n'
jpayne@69 8884 '\n'
jpayne@69 8885 'Any other keyword arguments that are specified in the class '
jpayne@69 8886 'definition\n'
jpayne@69 8887 'are passed through to all metaclass operations described '
jpayne@69 8888 'below.\n'
jpayne@69 8889 '\n'
jpayne@69 8890 'When a class definition is executed, the following steps '
jpayne@69 8891 'occur:\n'
jpayne@69 8892 '\n'
jpayne@69 8893 '* MRO entries are resolved;\n'
jpayne@69 8894 '\n'
jpayne@69 8895 '* the appropriate metaclass is determined;\n'
jpayne@69 8896 '\n'
jpayne@69 8897 '* the class namespace is prepared;\n'
jpayne@69 8898 '\n'
jpayne@69 8899 '* the class body is executed;\n'
jpayne@69 8900 '\n'
jpayne@69 8901 '* the class object is created.\n'
jpayne@69 8902 '\n'
jpayne@69 8903 '\n'
jpayne@69 8904 'Resolving MRO entries\n'
jpayne@69 8905 '---------------------\n'
jpayne@69 8906 '\n'
jpayne@69 8907 'If a base that appears in class definition is not an '
jpayne@69 8908 'instance of\n'
jpayne@69 8909 '"type", then an "__mro_entries__" method is searched on it. '
jpayne@69 8910 'If found,\n'
jpayne@69 8911 'it is called with the original bases tuple. This method must '
jpayne@69 8912 'return a\n'
jpayne@69 8913 'tuple of classes that will be used instead of this base. The '
jpayne@69 8914 'tuple may\n'
jpayne@69 8915 'be empty, in such case the original base is ignored.\n'
jpayne@69 8916 '\n'
jpayne@69 8917 'See also: **PEP 560** - Core support for typing module and '
jpayne@69 8918 'generic\n'
jpayne@69 8919 ' types\n'
jpayne@69 8920 '\n'
jpayne@69 8921 '\n'
jpayne@69 8922 'Determining the appropriate metaclass\n'
jpayne@69 8923 '-------------------------------------\n'
jpayne@69 8924 '\n'
jpayne@69 8925 'The appropriate metaclass for a class definition is '
jpayne@69 8926 'determined as\n'
jpayne@69 8927 'follows:\n'
jpayne@69 8928 '\n'
jpayne@69 8929 '* if no bases and no explicit metaclass are given, then '
jpayne@69 8930 '"type()" is\n'
jpayne@69 8931 ' used;\n'
jpayne@69 8932 '\n'
jpayne@69 8933 '* if an explicit metaclass is given and it is *not* an '
jpayne@69 8934 'instance of\n'
jpayne@69 8935 ' "type()", then it is used directly as the metaclass;\n'
jpayne@69 8936 '\n'
jpayne@69 8937 '* if an instance of "type()" is given as the explicit '
jpayne@69 8938 'metaclass, or\n'
jpayne@69 8939 ' bases are defined, then the most derived metaclass is '
jpayne@69 8940 'used.\n'
jpayne@69 8941 '\n'
jpayne@69 8942 'The most derived metaclass is selected from the explicitly '
jpayne@69 8943 'specified\n'
jpayne@69 8944 'metaclass (if any) and the metaclasses (i.e. "type(cls)") of '
jpayne@69 8945 'all\n'
jpayne@69 8946 'specified base classes. The most derived metaclass is one '
jpayne@69 8947 'which is a\n'
jpayne@69 8948 'subtype of *all* of these candidate metaclasses. If none of '
jpayne@69 8949 'the\n'
jpayne@69 8950 'candidate metaclasses meets that criterion, then the class '
jpayne@69 8951 'definition\n'
jpayne@69 8952 'will fail with "TypeError".\n'
jpayne@69 8953 '\n'
jpayne@69 8954 '\n'
jpayne@69 8955 'Preparing the class namespace\n'
jpayne@69 8956 '-----------------------------\n'
jpayne@69 8957 '\n'
jpayne@69 8958 'Once the appropriate metaclass has been identified, then the '
jpayne@69 8959 'class\n'
jpayne@69 8960 'namespace is prepared. If the metaclass has a "__prepare__" '
jpayne@69 8961 'attribute,\n'
jpayne@69 8962 'it is called as "namespace = metaclass.__prepare__(name, '
jpayne@69 8963 'bases,\n'
jpayne@69 8964 '**kwds)" (where the additional keyword arguments, if any, '
jpayne@69 8965 'come from\n'
jpayne@69 8966 'the class definition).\n'
jpayne@69 8967 '\n'
jpayne@69 8968 'If the metaclass has no "__prepare__" attribute, then the '
jpayne@69 8969 'class\n'
jpayne@69 8970 'namespace is initialised as an empty ordered mapping.\n'
jpayne@69 8971 '\n'
jpayne@69 8972 'See also:\n'
jpayne@69 8973 '\n'
jpayne@69 8974 ' **PEP 3115** - Metaclasses in Python 3000\n'
jpayne@69 8975 ' Introduced the "__prepare__" namespace hook\n'
jpayne@69 8976 '\n'
jpayne@69 8977 '\n'
jpayne@69 8978 'Executing the class body\n'
jpayne@69 8979 '------------------------\n'
jpayne@69 8980 '\n'
jpayne@69 8981 'The class body is executed (approximately) as "exec(body, '
jpayne@69 8982 'globals(),\n'
jpayne@69 8983 'namespace)". The key difference from a normal call to '
jpayne@69 8984 '"exec()" is that\n'
jpayne@69 8985 'lexical scoping allows the class body (including any '
jpayne@69 8986 'methods) to\n'
jpayne@69 8987 'reference names from the current and outer scopes when the '
jpayne@69 8988 'class\n'
jpayne@69 8989 'definition occurs inside a function.\n'
jpayne@69 8990 '\n'
jpayne@69 8991 'However, even when the class definition occurs inside the '
jpayne@69 8992 'function,\n'
jpayne@69 8993 'methods defined inside the class still cannot see names '
jpayne@69 8994 'defined at the\n'
jpayne@69 8995 'class scope. Class variables must be accessed through the '
jpayne@69 8996 'first\n'
jpayne@69 8997 'parameter of instance or class methods, or through the '
jpayne@69 8998 'implicit\n'
jpayne@69 8999 'lexically scoped "__class__" reference described in the next '
jpayne@69 9000 'section.\n'
jpayne@69 9001 '\n'
jpayne@69 9002 '\n'
jpayne@69 9003 'Creating the class object\n'
jpayne@69 9004 '-------------------------\n'
jpayne@69 9005 '\n'
jpayne@69 9006 'Once the class namespace has been populated by executing the '
jpayne@69 9007 'class\n'
jpayne@69 9008 'body, the class object is created by calling '
jpayne@69 9009 '"metaclass(name, bases,\n'
jpayne@69 9010 'namespace, **kwds)" (the additional keywords passed here are '
jpayne@69 9011 'the same\n'
jpayne@69 9012 'as those passed to "__prepare__").\n'
jpayne@69 9013 '\n'
jpayne@69 9014 'This class object is the one that will be referenced by the '
jpayne@69 9015 'zero-\n'
jpayne@69 9016 'argument form of "super()". "__class__" is an implicit '
jpayne@69 9017 'closure\n'
jpayne@69 9018 'reference created by the compiler if any methods in a class '
jpayne@69 9019 'body refer\n'
jpayne@69 9020 'to either "__class__" or "super". This allows the zero '
jpayne@69 9021 'argument form\n'
jpayne@69 9022 'of "super()" to correctly identify the class being defined '
jpayne@69 9023 'based on\n'
jpayne@69 9024 'lexical scoping, while the class or instance that was used '
jpayne@69 9025 'to make the\n'
jpayne@69 9026 'current call is identified based on the first argument '
jpayne@69 9027 'passed to the\n'
jpayne@69 9028 'method.\n'
jpayne@69 9029 '\n'
jpayne@69 9030 '**CPython implementation detail:** In CPython 3.6 and later, '
jpayne@69 9031 'the\n'
jpayne@69 9032 '"__class__" cell is passed to the metaclass as a '
jpayne@69 9033 '"__classcell__" entry\n'
jpayne@69 9034 'in the class namespace. If present, this must be propagated '
jpayne@69 9035 'up to the\n'
jpayne@69 9036 '"type.__new__" call in order for the class to be '
jpayne@69 9037 'initialised\n'
jpayne@69 9038 'correctly. Failing to do so will result in a "RuntimeError" '
jpayne@69 9039 'in Python\n'
jpayne@69 9040 '3.8.\n'
jpayne@69 9041 '\n'
jpayne@69 9042 'When using the default metaclass "type", or any metaclass '
jpayne@69 9043 'that\n'
jpayne@69 9044 'ultimately calls "type.__new__", the following additional\n'
jpayne@69 9045 'customisation steps are invoked after creating the class '
jpayne@69 9046 'object:\n'
jpayne@69 9047 '\n'
jpayne@69 9048 '* first, "type.__new__" collects all of the descriptors in '
jpayne@69 9049 'the class\n'
jpayne@69 9050 ' namespace that define a "__set_name__()" method;\n'
jpayne@69 9051 '\n'
jpayne@69 9052 '* second, all of these "__set_name__" methods are called '
jpayne@69 9053 'with the\n'
jpayne@69 9054 ' class being defined and the assigned name of that '
jpayne@69 9055 'particular\n'
jpayne@69 9056 ' descriptor;\n'
jpayne@69 9057 '\n'
jpayne@69 9058 '* finally, the "__init_subclass__()" hook is called on the '
jpayne@69 9059 'immediate\n'
jpayne@69 9060 ' parent of the new class in its method resolution order.\n'
jpayne@69 9061 '\n'
jpayne@69 9062 'After the class object is created, it is passed to the '
jpayne@69 9063 'class\n'
jpayne@69 9064 'decorators included in the class definition (if any) and the '
jpayne@69 9065 'resulting\n'
jpayne@69 9066 'object is bound in the local namespace as the defined '
jpayne@69 9067 'class.\n'
jpayne@69 9068 '\n'
jpayne@69 9069 'When a new class is created by "type.__new__", the object '
jpayne@69 9070 'provided as\n'
jpayne@69 9071 'the namespace parameter is copied to a new ordered mapping '
jpayne@69 9072 'and the\n'
jpayne@69 9073 'original object is discarded. The new copy is wrapped in a '
jpayne@69 9074 'read-only\n'
jpayne@69 9075 'proxy, which becomes the "__dict__" attribute of the class '
jpayne@69 9076 'object.\n'
jpayne@69 9077 '\n'
jpayne@69 9078 'See also:\n'
jpayne@69 9079 '\n'
jpayne@69 9080 ' **PEP 3135** - New super\n'
jpayne@69 9081 ' Describes the implicit "__class__" closure reference\n'
jpayne@69 9082 '\n'
jpayne@69 9083 '\n'
jpayne@69 9084 'Uses for metaclasses\n'
jpayne@69 9085 '--------------------\n'
jpayne@69 9086 '\n'
jpayne@69 9087 'The potential uses for metaclasses are boundless. Some ideas '
jpayne@69 9088 'that have\n'
jpayne@69 9089 'been explored include enum, logging, interface checking, '
jpayne@69 9090 'automatic\n'
jpayne@69 9091 'delegation, automatic property creation, proxies, '
jpayne@69 9092 'frameworks, and\n'
jpayne@69 9093 'automatic resource locking/synchronization.\n'
jpayne@69 9094 '\n'
jpayne@69 9095 '\n'
jpayne@69 9096 'Customizing instance and subclass checks\n'
jpayne@69 9097 '========================================\n'
jpayne@69 9098 '\n'
jpayne@69 9099 'The following methods are used to override the default '
jpayne@69 9100 'behavior of the\n'
jpayne@69 9101 '"isinstance()" and "issubclass()" built-in functions.\n'
jpayne@69 9102 '\n'
jpayne@69 9103 'In particular, the metaclass "abc.ABCMeta" implements these '
jpayne@69 9104 'methods in\n'
jpayne@69 9105 'order to allow the addition of Abstract Base Classes (ABCs) '
jpayne@69 9106 'as\n'
jpayne@69 9107 '“virtual base classes” to any class or type (including '
jpayne@69 9108 'built-in\n'
jpayne@69 9109 'types), including other ABCs.\n'
jpayne@69 9110 '\n'
jpayne@69 9111 'class.__instancecheck__(self, instance)\n'
jpayne@69 9112 '\n'
jpayne@69 9113 ' Return true if *instance* should be considered a (direct '
jpayne@69 9114 'or\n'
jpayne@69 9115 ' indirect) instance of *class*. If defined, called to '
jpayne@69 9116 'implement\n'
jpayne@69 9117 ' "isinstance(instance, class)".\n'
jpayne@69 9118 '\n'
jpayne@69 9119 'class.__subclasscheck__(self, subclass)\n'
jpayne@69 9120 '\n'
jpayne@69 9121 ' Return true if *subclass* should be considered a (direct '
jpayne@69 9122 'or\n'
jpayne@69 9123 ' indirect) subclass of *class*. If defined, called to '
jpayne@69 9124 'implement\n'
jpayne@69 9125 ' "issubclass(subclass, class)".\n'
jpayne@69 9126 '\n'
jpayne@69 9127 'Note that these methods are looked up on the type '
jpayne@69 9128 '(metaclass) of a\n'
jpayne@69 9129 'class. They cannot be defined as class methods in the '
jpayne@69 9130 'actual class.\n'
jpayne@69 9131 'This is consistent with the lookup of special methods that '
jpayne@69 9132 'are called\n'
jpayne@69 9133 'on instances, only in this case the instance is itself a '
jpayne@69 9134 'class.\n'
jpayne@69 9135 '\n'
jpayne@69 9136 'See also:\n'
jpayne@69 9137 '\n'
jpayne@69 9138 ' **PEP 3119** - Introducing Abstract Base Classes\n'
jpayne@69 9139 ' Includes the specification for customizing '
jpayne@69 9140 '"isinstance()" and\n'
jpayne@69 9141 ' "issubclass()" behavior through "__instancecheck__()" '
jpayne@69 9142 'and\n'
jpayne@69 9143 ' "__subclasscheck__()", with motivation for this '
jpayne@69 9144 'functionality in\n'
jpayne@69 9145 ' the context of adding Abstract Base Classes (see the '
jpayne@69 9146 '"abc"\n'
jpayne@69 9147 ' module) to the language.\n'
jpayne@69 9148 '\n'
jpayne@69 9149 '\n'
jpayne@69 9150 'Emulating generic types\n'
jpayne@69 9151 '=======================\n'
jpayne@69 9152 '\n'
jpayne@69 9153 'One can implement the generic class syntax as specified by '
jpayne@69 9154 '**PEP 484**\n'
jpayne@69 9155 '(for example "List[int]") by defining a special method:\n'
jpayne@69 9156 '\n'
jpayne@69 9157 'classmethod object.__class_getitem__(cls, key)\n'
jpayne@69 9158 '\n'
jpayne@69 9159 ' Return an object representing the specialization of a '
jpayne@69 9160 'generic class\n'
jpayne@69 9161 ' by type arguments found in *key*.\n'
jpayne@69 9162 '\n'
jpayne@69 9163 'This method is looked up on the class object itself, and '
jpayne@69 9164 'when defined\n'
jpayne@69 9165 'in the class body, this method is implicitly a class '
jpayne@69 9166 'method. Note,\n'
jpayne@69 9167 'this mechanism is primarily reserved for use with static '
jpayne@69 9168 'type hints,\n'
jpayne@69 9169 'other usage is discouraged.\n'
jpayne@69 9170 '\n'
jpayne@69 9171 'See also: **PEP 560** - Core support for typing module and '
jpayne@69 9172 'generic\n'
jpayne@69 9173 ' types\n'
jpayne@69 9174 '\n'
jpayne@69 9175 '\n'
jpayne@69 9176 'Emulating callable objects\n'
jpayne@69 9177 '==========================\n'
jpayne@69 9178 '\n'
jpayne@69 9179 'object.__call__(self[, args...])\n'
jpayne@69 9180 '\n'
jpayne@69 9181 ' Called when the instance is “called” as a function; if '
jpayne@69 9182 'this method\n'
jpayne@69 9183 ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n'
jpayne@69 9184 ' "x.__call__(arg1, arg2, ...)".\n'
jpayne@69 9185 '\n'
jpayne@69 9186 '\n'
jpayne@69 9187 'Emulating container types\n'
jpayne@69 9188 '=========================\n'
jpayne@69 9189 '\n'
jpayne@69 9190 'The following methods can be defined to implement container '
jpayne@69 9191 'objects.\n'
jpayne@69 9192 'Containers usually are sequences (such as lists or tuples) '
jpayne@69 9193 'or mappings\n'
jpayne@69 9194 '(like dictionaries), but can represent other containers as '
jpayne@69 9195 'well. The\n'
jpayne@69 9196 'first set of methods is used either to emulate a sequence or '
jpayne@69 9197 'to\n'
jpayne@69 9198 'emulate a mapping; the difference is that for a sequence, '
jpayne@69 9199 'the\n'
jpayne@69 9200 'allowable keys should be the integers *k* for which "0 <= k '
jpayne@69 9201 '< N" where\n'
jpayne@69 9202 '*N* is the length of the sequence, or slice objects, which '
jpayne@69 9203 'define a\n'
jpayne@69 9204 'range of items. It is also recommended that mappings '
jpayne@69 9205 'provide the\n'
jpayne@69 9206 'methods "keys()", "values()", "items()", "get()", '
jpayne@69 9207 '"clear()",\n'
jpayne@69 9208 '"setdefault()", "pop()", "popitem()", "copy()", and '
jpayne@69 9209 '"update()"\n'
jpayne@69 9210 'behaving similar to those for Python’s standard dictionary '
jpayne@69 9211 'objects.\n'
jpayne@69 9212 'The "collections.abc" module provides a "MutableMapping" '
jpayne@69 9213 'abstract base\n'
jpayne@69 9214 'class to help create those methods from a base set of '
jpayne@69 9215 '"__getitem__()",\n'
jpayne@69 9216 '"__setitem__()", "__delitem__()", and "keys()". Mutable '
jpayne@69 9217 'sequences\n'
jpayne@69 9218 'should provide methods "append()", "count()", "index()", '
jpayne@69 9219 '"extend()",\n'
jpayne@69 9220 '"insert()", "pop()", "remove()", "reverse()" and "sort()", '
jpayne@69 9221 'like Python\n'
jpayne@69 9222 'standard list objects. Finally, sequence types should '
jpayne@69 9223 'implement\n'
jpayne@69 9224 'addition (meaning concatenation) and multiplication '
jpayne@69 9225 '(meaning\n'
jpayne@69 9226 'repetition) by defining the methods "__add__()", '
jpayne@69 9227 '"__radd__()",\n'
jpayne@69 9228 '"__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" '
jpayne@69 9229 'described\n'
jpayne@69 9230 'below; they should not define other numerical operators. It '
jpayne@69 9231 'is\n'
jpayne@69 9232 'recommended that both mappings and sequences implement the\n'
jpayne@69 9233 '"__contains__()" method to allow efficient use of the "in" '
jpayne@69 9234 'operator;\n'
jpayne@69 9235 'for mappings, "in" should search the mapping’s keys; for '
jpayne@69 9236 'sequences, it\n'
jpayne@69 9237 'should search through the values. It is further recommended '
jpayne@69 9238 'that both\n'
jpayne@69 9239 'mappings and sequences implement the "__iter__()" method to '
jpayne@69 9240 'allow\n'
jpayne@69 9241 'efficient iteration through the container; for mappings, '
jpayne@69 9242 '"__iter__()"\n'
jpayne@69 9243 'should iterate through the object’s keys; for sequences, it '
jpayne@69 9244 'should\n'
jpayne@69 9245 'iterate through the values.\n'
jpayne@69 9246 '\n'
jpayne@69 9247 'object.__len__(self)\n'
jpayne@69 9248 '\n'
jpayne@69 9249 ' Called to implement the built-in function "len()". '
jpayne@69 9250 'Should return\n'
jpayne@69 9251 ' the length of the object, an integer ">=" 0. Also, an '
jpayne@69 9252 'object that\n'
jpayne@69 9253 ' doesn’t define a "__bool__()" method and whose '
jpayne@69 9254 '"__len__()" method\n'
jpayne@69 9255 ' returns zero is considered to be false in a Boolean '
jpayne@69 9256 'context.\n'
jpayne@69 9257 '\n'
jpayne@69 9258 ' **CPython implementation detail:** In CPython, the length '
jpayne@69 9259 'is\n'
jpayne@69 9260 ' required to be at most "sys.maxsize". If the length is '
jpayne@69 9261 'larger than\n'
jpayne@69 9262 ' "sys.maxsize" some features (such as "len()") may raise\n'
jpayne@69 9263 ' "OverflowError". To prevent raising "OverflowError" by '
jpayne@69 9264 'truth value\n'
jpayne@69 9265 ' testing, an object must define a "__bool__()" method.\n'
jpayne@69 9266 '\n'
jpayne@69 9267 'object.__length_hint__(self)\n'
jpayne@69 9268 '\n'
jpayne@69 9269 ' Called to implement "operator.length_hint()". Should '
jpayne@69 9270 'return an\n'
jpayne@69 9271 ' estimated length for the object (which may be greater or '
jpayne@69 9272 'less than\n'
jpayne@69 9273 ' the actual length). The length must be an integer ">=" 0. '
jpayne@69 9274 'The\n'
jpayne@69 9275 ' return value may also be "NotImplemented", which is '
jpayne@69 9276 'treated the\n'
jpayne@69 9277 ' same as if the "__length_hint__" method didn’t exist at '
jpayne@69 9278 'all. This\n'
jpayne@69 9279 ' method is purely an optimization and is never required '
jpayne@69 9280 'for\n'
jpayne@69 9281 ' correctness.\n'
jpayne@69 9282 '\n'
jpayne@69 9283 ' New in version 3.4.\n'
jpayne@69 9284 '\n'
jpayne@69 9285 'Note: Slicing is done exclusively with the following three '
jpayne@69 9286 'methods.\n'
jpayne@69 9287 ' A call like\n'
jpayne@69 9288 '\n'
jpayne@69 9289 ' a[1:2] = b\n'
jpayne@69 9290 '\n'
jpayne@69 9291 ' is translated to\n'
jpayne@69 9292 '\n'
jpayne@69 9293 ' a[slice(1, 2, None)] = b\n'
jpayne@69 9294 '\n'
jpayne@69 9295 ' and so forth. Missing slice items are always filled in '
jpayne@69 9296 'with "None".\n'
jpayne@69 9297 '\n'
jpayne@69 9298 'object.__getitem__(self, key)\n'
jpayne@69 9299 '\n'
jpayne@69 9300 ' Called to implement evaluation of "self[key]". For '
jpayne@69 9301 'sequence types,\n'
jpayne@69 9302 ' the accepted keys should be integers and slice objects. '
jpayne@69 9303 'Note that\n'
jpayne@69 9304 ' the special interpretation of negative indexes (if the '
jpayne@69 9305 'class wishes\n'
jpayne@69 9306 ' to emulate a sequence type) is up to the "__getitem__()" '
jpayne@69 9307 'method. If\n'
jpayne@69 9308 ' *key* is of an inappropriate type, "TypeError" may be '
jpayne@69 9309 'raised; if of\n'
jpayne@69 9310 ' a value outside the set of indexes for the sequence '
jpayne@69 9311 '(after any\n'
jpayne@69 9312 ' special interpretation of negative values), "IndexError" '
jpayne@69 9313 'should be\n'
jpayne@69 9314 ' raised. For mapping types, if *key* is missing (not in '
jpayne@69 9315 'the\n'
jpayne@69 9316 ' container), "KeyError" should be raised.\n'
jpayne@69 9317 '\n'
jpayne@69 9318 ' Note: "for" loops expect that an "IndexError" will be '
jpayne@69 9319 'raised for\n'
jpayne@69 9320 ' illegal indexes to allow proper detection of the end of '
jpayne@69 9321 'the\n'
jpayne@69 9322 ' sequence.\n'
jpayne@69 9323 '\n'
jpayne@69 9324 'object.__setitem__(self, key, value)\n'
jpayne@69 9325 '\n'
jpayne@69 9326 ' Called to implement assignment to "self[key]". Same note '
jpayne@69 9327 'as for\n'
jpayne@69 9328 ' "__getitem__()". This should only be implemented for '
jpayne@69 9329 'mappings if\n'
jpayne@69 9330 ' the objects support changes to the values for keys, or if '
jpayne@69 9331 'new keys\n'
jpayne@69 9332 ' can be added, or for sequences if elements can be '
jpayne@69 9333 'replaced. The\n'
jpayne@69 9334 ' same exceptions should be raised for improper *key* '
jpayne@69 9335 'values as for\n'
jpayne@69 9336 ' the "__getitem__()" method.\n'
jpayne@69 9337 '\n'
jpayne@69 9338 'object.__delitem__(self, key)\n'
jpayne@69 9339 '\n'
jpayne@69 9340 ' Called to implement deletion of "self[key]". Same note '
jpayne@69 9341 'as for\n'
jpayne@69 9342 ' "__getitem__()". This should only be implemented for '
jpayne@69 9343 'mappings if\n'
jpayne@69 9344 ' the objects support removal of keys, or for sequences if '
jpayne@69 9345 'elements\n'
jpayne@69 9346 ' can be removed from the sequence. The same exceptions '
jpayne@69 9347 'should be\n'
jpayne@69 9348 ' raised for improper *key* values as for the '
jpayne@69 9349 '"__getitem__()" method.\n'
jpayne@69 9350 '\n'
jpayne@69 9351 'object.__missing__(self, key)\n'
jpayne@69 9352 '\n'
jpayne@69 9353 ' Called by "dict"."__getitem__()" to implement "self[key]" '
jpayne@69 9354 'for dict\n'
jpayne@69 9355 ' subclasses when key is not in the dictionary.\n'
jpayne@69 9356 '\n'
jpayne@69 9357 'object.__iter__(self)\n'
jpayne@69 9358 '\n'
jpayne@69 9359 ' This method is called when an iterator is required for a '
jpayne@69 9360 'container.\n'
jpayne@69 9361 ' This method should return a new iterator object that can '
jpayne@69 9362 'iterate\n'
jpayne@69 9363 ' over all the objects in the container. For mappings, it '
jpayne@69 9364 'should\n'
jpayne@69 9365 ' iterate over the keys of the container.\n'
jpayne@69 9366 '\n'
jpayne@69 9367 ' Iterator objects also need to implement this method; they '
jpayne@69 9368 'are\n'
jpayne@69 9369 ' required to return themselves. For more information on '
jpayne@69 9370 'iterator\n'
jpayne@69 9371 ' objects, see Iterator Types.\n'
jpayne@69 9372 '\n'
jpayne@69 9373 'object.__reversed__(self)\n'
jpayne@69 9374 '\n'
jpayne@69 9375 ' Called (if present) by the "reversed()" built-in to '
jpayne@69 9376 'implement\n'
jpayne@69 9377 ' reverse iteration. It should return a new iterator '
jpayne@69 9378 'object that\n'
jpayne@69 9379 ' iterates over all the objects in the container in reverse '
jpayne@69 9380 'order.\n'
jpayne@69 9381 '\n'
jpayne@69 9382 ' If the "__reversed__()" method is not provided, the '
jpayne@69 9383 '"reversed()"\n'
jpayne@69 9384 ' built-in will fall back to using the sequence protocol '
jpayne@69 9385 '("__len__()"\n'
jpayne@69 9386 ' and "__getitem__()"). Objects that support the sequence '
jpayne@69 9387 'protocol\n'
jpayne@69 9388 ' should only provide "__reversed__()" if they can provide '
jpayne@69 9389 'an\n'
jpayne@69 9390 ' implementation that is more efficient than the one '
jpayne@69 9391 'provided by\n'
jpayne@69 9392 ' "reversed()".\n'
jpayne@69 9393 '\n'
jpayne@69 9394 'The membership test operators ("in" and "not in") are '
jpayne@69 9395 'normally\n'
jpayne@69 9396 'implemented as an iteration through a container. However, '
jpayne@69 9397 'container\n'
jpayne@69 9398 'objects can supply the following special method with a more '
jpayne@69 9399 'efficient\n'
jpayne@69 9400 'implementation, which also does not require the object be '
jpayne@69 9401 'iterable.\n'
jpayne@69 9402 '\n'
jpayne@69 9403 'object.__contains__(self, item)\n'
jpayne@69 9404 '\n'
jpayne@69 9405 ' Called to implement membership test operators. Should '
jpayne@69 9406 'return true\n'
jpayne@69 9407 ' if *item* is in *self*, false otherwise. For mapping '
jpayne@69 9408 'objects, this\n'
jpayne@69 9409 ' should consider the keys of the mapping rather than the '
jpayne@69 9410 'values or\n'
jpayne@69 9411 ' the key-item pairs.\n'
jpayne@69 9412 '\n'
jpayne@69 9413 ' For objects that don’t define "__contains__()", the '
jpayne@69 9414 'membership test\n'
jpayne@69 9415 ' first tries iteration via "__iter__()", then the old '
jpayne@69 9416 'sequence\n'
jpayne@69 9417 ' iteration protocol via "__getitem__()", see this section '
jpayne@69 9418 'in the\n'
jpayne@69 9419 ' language reference.\n'
jpayne@69 9420 '\n'
jpayne@69 9421 '\n'
jpayne@69 9422 'Emulating numeric types\n'
jpayne@69 9423 '=======================\n'
jpayne@69 9424 '\n'
jpayne@69 9425 'The following methods can be defined to emulate numeric '
jpayne@69 9426 'objects.\n'
jpayne@69 9427 'Methods corresponding to operations that are not supported '
jpayne@69 9428 'by the\n'
jpayne@69 9429 'particular kind of number implemented (e.g., bitwise '
jpayne@69 9430 'operations for\n'
jpayne@69 9431 'non-integral numbers) should be left undefined.\n'
jpayne@69 9432 '\n'
jpayne@69 9433 'object.__add__(self, other)\n'
jpayne@69 9434 'object.__sub__(self, other)\n'
jpayne@69 9435 'object.__mul__(self, other)\n'
jpayne@69 9436 'object.__matmul__(self, other)\n'
jpayne@69 9437 'object.__truediv__(self, other)\n'
jpayne@69 9438 'object.__floordiv__(self, other)\n'
jpayne@69 9439 'object.__mod__(self, other)\n'
jpayne@69 9440 'object.__divmod__(self, other)\n'
jpayne@69 9441 'object.__pow__(self, other[, modulo])\n'
jpayne@69 9442 'object.__lshift__(self, other)\n'
jpayne@69 9443 'object.__rshift__(self, other)\n'
jpayne@69 9444 'object.__and__(self, other)\n'
jpayne@69 9445 'object.__xor__(self, other)\n'
jpayne@69 9446 'object.__or__(self, other)\n'
jpayne@69 9447 '\n'
jpayne@69 9448 ' These methods are called to implement the binary '
jpayne@69 9449 'arithmetic\n'
jpayne@69 9450 ' operations ("+", "-", "*", "@", "/", "//", "%", '
jpayne@69 9451 '"divmod()",\n'
jpayne@69 9452 ' "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, '
jpayne@69 9453 'to\n'
jpayne@69 9454 ' evaluate the expression "x + y", where *x* is an instance '
jpayne@69 9455 'of a\n'
jpayne@69 9456 ' class that has an "__add__()" method, "x.__add__(y)" is '
jpayne@69 9457 'called.\n'
jpayne@69 9458 ' The "__divmod__()" method should be the equivalent to '
jpayne@69 9459 'using\n'
jpayne@69 9460 ' "__floordiv__()" and "__mod__()"; it should not be '
jpayne@69 9461 'related to\n'
jpayne@69 9462 ' "__truediv__()". Note that "__pow__()" should be defined '
jpayne@69 9463 'to accept\n'
jpayne@69 9464 ' an optional third argument if the ternary version of the '
jpayne@69 9465 'built-in\n'
jpayne@69 9466 ' "pow()" function is to be supported.\n'
jpayne@69 9467 '\n'
jpayne@69 9468 ' If one of those methods does not support the operation '
jpayne@69 9469 'with the\n'
jpayne@69 9470 ' supplied arguments, it should return "NotImplemented".\n'
jpayne@69 9471 '\n'
jpayne@69 9472 'object.__radd__(self, other)\n'
jpayne@69 9473 'object.__rsub__(self, other)\n'
jpayne@69 9474 'object.__rmul__(self, other)\n'
jpayne@69 9475 'object.__rmatmul__(self, other)\n'
jpayne@69 9476 'object.__rtruediv__(self, other)\n'
jpayne@69 9477 'object.__rfloordiv__(self, other)\n'
jpayne@69 9478 'object.__rmod__(self, other)\n'
jpayne@69 9479 'object.__rdivmod__(self, other)\n'
jpayne@69 9480 'object.__rpow__(self, other)\n'
jpayne@69 9481 'object.__rlshift__(self, other)\n'
jpayne@69 9482 'object.__rrshift__(self, other)\n'
jpayne@69 9483 'object.__rand__(self, other)\n'
jpayne@69 9484 'object.__rxor__(self, other)\n'
jpayne@69 9485 'object.__ror__(self, other)\n'
jpayne@69 9486 '\n'
jpayne@69 9487 ' These methods are called to implement the binary '
jpayne@69 9488 'arithmetic\n'
jpayne@69 9489 ' operations ("+", "-", "*", "@", "/", "//", "%", '
jpayne@69 9490 '"divmod()",\n'
jpayne@69 9491 ' "pow()", "**", "<<", ">>", "&", "^", "|") with reflected '
jpayne@69 9492 '(swapped)\n'
jpayne@69 9493 ' operands. These functions are only called if the left '
jpayne@69 9494 'operand does\n'
jpayne@69 9495 ' not support the corresponding operation [3] and the '
jpayne@69 9496 'operands are of\n'
jpayne@69 9497 ' different types. [4] For instance, to evaluate the '
jpayne@69 9498 'expression "x -\n'
jpayne@69 9499 ' y", where *y* is an instance of a class that has an '
jpayne@69 9500 '"__rsub__()"\n'
jpayne@69 9501 ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" '
jpayne@69 9502 'returns\n'
jpayne@69 9503 ' *NotImplemented*.\n'
jpayne@69 9504 '\n'
jpayne@69 9505 ' Note that ternary "pow()" will not try calling '
jpayne@69 9506 '"__rpow__()" (the\n'
jpayne@69 9507 ' coercion rules would become too complicated).\n'
jpayne@69 9508 '\n'
jpayne@69 9509 ' Note: If the right operand’s type is a subclass of the '
jpayne@69 9510 'left\n'
jpayne@69 9511 ' operand’s type and that subclass provides the reflected '
jpayne@69 9512 'method\n'
jpayne@69 9513 ' for the operation, this method will be called before '
jpayne@69 9514 'the left\n'
jpayne@69 9515 ' operand’s non-reflected method. This behavior allows '
jpayne@69 9516 'subclasses\n'
jpayne@69 9517 ' to override their ancestors’ operations.\n'
jpayne@69 9518 '\n'
jpayne@69 9519 'object.__iadd__(self, other)\n'
jpayne@69 9520 'object.__isub__(self, other)\n'
jpayne@69 9521 'object.__imul__(self, other)\n'
jpayne@69 9522 'object.__imatmul__(self, other)\n'
jpayne@69 9523 'object.__itruediv__(self, other)\n'
jpayne@69 9524 'object.__ifloordiv__(self, other)\n'
jpayne@69 9525 'object.__imod__(self, other)\n'
jpayne@69 9526 'object.__ipow__(self, other[, modulo])\n'
jpayne@69 9527 'object.__ilshift__(self, other)\n'
jpayne@69 9528 'object.__irshift__(self, other)\n'
jpayne@69 9529 'object.__iand__(self, other)\n'
jpayne@69 9530 'object.__ixor__(self, other)\n'
jpayne@69 9531 'object.__ior__(self, other)\n'
jpayne@69 9532 '\n'
jpayne@69 9533 ' These methods are called to implement the augmented '
jpayne@69 9534 'arithmetic\n'
jpayne@69 9535 ' assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", '
jpayne@69 9536 '"**=",\n'
jpayne@69 9537 ' "<<=", ">>=", "&=", "^=", "|="). These methods should '
jpayne@69 9538 'attempt to\n'
jpayne@69 9539 ' do the operation in-place (modifying *self*) and return '
jpayne@69 9540 'the result\n'
jpayne@69 9541 ' (which could be, but does not have to be, *self*). If a '
jpayne@69 9542 'specific\n'
jpayne@69 9543 ' method is not defined, the augmented assignment falls '
jpayne@69 9544 'back to the\n'
jpayne@69 9545 ' normal methods. For instance, if *x* is an instance of a '
jpayne@69 9546 'class\n'
jpayne@69 9547 ' with an "__iadd__()" method, "x += y" is equivalent to "x '
jpayne@69 9548 '=\n'
jpayne@69 9549 ' x.__iadd__(y)" . Otherwise, "x.__add__(y)" and '
jpayne@69 9550 '"y.__radd__(x)" are\n'
jpayne@69 9551 ' considered, as with the evaluation of "x + y". In '
jpayne@69 9552 'certain\n'
jpayne@69 9553 ' situations, augmented assignment can result in unexpected '
jpayne@69 9554 'errors\n'
jpayne@69 9555 ' (see Why does a_tuple[i] += [‘item’] raise an exception '
jpayne@69 9556 'when the\n'
jpayne@69 9557 ' addition works?), but this behavior is in fact part of '
jpayne@69 9558 'the data\n'
jpayne@69 9559 ' model.\n'
jpayne@69 9560 '\n'
jpayne@69 9561 'object.__neg__(self)\n'
jpayne@69 9562 'object.__pos__(self)\n'
jpayne@69 9563 'object.__abs__(self)\n'
jpayne@69 9564 'object.__invert__(self)\n'
jpayne@69 9565 '\n'
jpayne@69 9566 ' Called to implement the unary arithmetic operations ("-", '
jpayne@69 9567 '"+",\n'
jpayne@69 9568 ' "abs()" and "~").\n'
jpayne@69 9569 '\n'
jpayne@69 9570 'object.__complex__(self)\n'
jpayne@69 9571 'object.__int__(self)\n'
jpayne@69 9572 'object.__float__(self)\n'
jpayne@69 9573 '\n'
jpayne@69 9574 ' Called to implement the built-in functions "complex()", '
jpayne@69 9575 '"int()" and\n'
jpayne@69 9576 ' "float()". Should return a value of the appropriate '
jpayne@69 9577 'type.\n'
jpayne@69 9578 '\n'
jpayne@69 9579 'object.__index__(self)\n'
jpayne@69 9580 '\n'
jpayne@69 9581 ' Called to implement "operator.index()", and whenever '
jpayne@69 9582 'Python needs\n'
jpayne@69 9583 ' to losslessly convert the numeric object to an integer '
jpayne@69 9584 'object (such\n'
jpayne@69 9585 ' as in slicing, or in the built-in "bin()", "hex()" and '
jpayne@69 9586 '"oct()"\n'
jpayne@69 9587 ' functions). Presence of this method indicates that the '
jpayne@69 9588 'numeric\n'
jpayne@69 9589 ' object is an integer type. Must return an integer.\n'
jpayne@69 9590 '\n'
jpayne@69 9591 ' If "__int__()", "__float__()" and "__complex__()" are not '
jpayne@69 9592 'defined\n'
jpayne@69 9593 ' then corresponding built-in functions "int()", "float()" '
jpayne@69 9594 'and\n'
jpayne@69 9595 ' "complex()" fall back to "__index__()".\n'
jpayne@69 9596 '\n'
jpayne@69 9597 'object.__round__(self[, ndigits])\n'
jpayne@69 9598 'object.__trunc__(self)\n'
jpayne@69 9599 'object.__floor__(self)\n'
jpayne@69 9600 'object.__ceil__(self)\n'
jpayne@69 9601 '\n'
jpayne@69 9602 ' Called to implement the built-in function "round()" and '
jpayne@69 9603 '"math"\n'
jpayne@69 9604 ' functions "trunc()", "floor()" and "ceil()". Unless '
jpayne@69 9605 '*ndigits* is\n'
jpayne@69 9606 ' passed to "__round__()" all these methods should return '
jpayne@69 9607 'the value\n'
jpayne@69 9608 ' of the object truncated to an "Integral" (typically an '
jpayne@69 9609 '"int").\n'
jpayne@69 9610 '\n'
jpayne@69 9611 ' If "__int__()" is not defined then the built-in function '
jpayne@69 9612 '"int()"\n'
jpayne@69 9613 ' falls back to "__trunc__()".\n'
jpayne@69 9614 '\n'
jpayne@69 9615 '\n'
jpayne@69 9616 'With Statement Context Managers\n'
jpayne@69 9617 '===============================\n'
jpayne@69 9618 '\n'
jpayne@69 9619 'A *context manager* is an object that defines the runtime '
jpayne@69 9620 'context to\n'
jpayne@69 9621 'be established when executing a "with" statement. The '
jpayne@69 9622 'context manager\n'
jpayne@69 9623 'handles the entry into, and the exit from, the desired '
jpayne@69 9624 'runtime context\n'
jpayne@69 9625 'for the execution of the block of code. Context managers '
jpayne@69 9626 'are normally\n'
jpayne@69 9627 'invoked using the "with" statement (described in section The '
jpayne@69 9628 'with\n'
jpayne@69 9629 'statement), but can also be used by directly invoking their '
jpayne@69 9630 'methods.\n'
jpayne@69 9631 '\n'
jpayne@69 9632 'Typical uses of context managers include saving and '
jpayne@69 9633 'restoring various\n'
jpayne@69 9634 'kinds of global state, locking and unlocking resources, '
jpayne@69 9635 'closing opened\n'
jpayne@69 9636 'files, etc.\n'
jpayne@69 9637 '\n'
jpayne@69 9638 'For more information on context managers, see Context '
jpayne@69 9639 'Manager Types.\n'
jpayne@69 9640 '\n'
jpayne@69 9641 'object.__enter__(self)\n'
jpayne@69 9642 '\n'
jpayne@69 9643 ' Enter the runtime context related to this object. The '
jpayne@69 9644 '"with"\n'
jpayne@69 9645 ' statement will bind this method’s return value to the '
jpayne@69 9646 'target(s)\n'
jpayne@69 9647 ' specified in the "as" clause of the statement, if any.\n'
jpayne@69 9648 '\n'
jpayne@69 9649 'object.__exit__(self, exc_type, exc_value, traceback)\n'
jpayne@69 9650 '\n'
jpayne@69 9651 ' Exit the runtime context related to this object. The '
jpayne@69 9652 'parameters\n'
jpayne@69 9653 ' describe the exception that caused the context to be '
jpayne@69 9654 'exited. If the\n'
jpayne@69 9655 ' context was exited without an exception, all three '
jpayne@69 9656 'arguments will\n'
jpayne@69 9657 ' be "None".\n'
jpayne@69 9658 '\n'
jpayne@69 9659 ' If an exception is supplied, and the method wishes to '
jpayne@69 9660 'suppress the\n'
jpayne@69 9661 ' exception (i.e., prevent it from being propagated), it '
jpayne@69 9662 'should\n'
jpayne@69 9663 ' return a true value. Otherwise, the exception will be '
jpayne@69 9664 'processed\n'
jpayne@69 9665 ' normally upon exit from this method.\n'
jpayne@69 9666 '\n'
jpayne@69 9667 ' Note that "__exit__()" methods should not reraise the '
jpayne@69 9668 'passed-in\n'
jpayne@69 9669 ' exception; this is the caller’s responsibility.\n'
jpayne@69 9670 '\n'
jpayne@69 9671 'See also:\n'
jpayne@69 9672 '\n'
jpayne@69 9673 ' **PEP 343** - The “with” statement\n'
jpayne@69 9674 ' The specification, background, and examples for the '
jpayne@69 9675 'Python "with"\n'
jpayne@69 9676 ' statement.\n'
jpayne@69 9677 '\n'
jpayne@69 9678 '\n'
jpayne@69 9679 'Special method lookup\n'
jpayne@69 9680 '=====================\n'
jpayne@69 9681 '\n'
jpayne@69 9682 'For custom classes, implicit invocations of special methods '
jpayne@69 9683 'are only\n'
jpayne@69 9684 'guaranteed to work correctly if defined on an object’s type, '
jpayne@69 9685 'not in\n'
jpayne@69 9686 'the object’s instance dictionary. That behaviour is the '
jpayne@69 9687 'reason why\n'
jpayne@69 9688 'the following code raises an exception:\n'
jpayne@69 9689 '\n'
jpayne@69 9690 ' >>> class C:\n'
jpayne@69 9691 ' ... pass\n'
jpayne@69 9692 ' ...\n'
jpayne@69 9693 ' >>> c = C()\n'
jpayne@69 9694 ' >>> c.__len__ = lambda: 5\n'
jpayne@69 9695 ' >>> len(c)\n'
jpayne@69 9696 ' Traceback (most recent call last):\n'
jpayne@69 9697 ' File "<stdin>", line 1, in <module>\n'
jpayne@69 9698 " TypeError: object of type 'C' has no len()\n"
jpayne@69 9699 '\n'
jpayne@69 9700 'The rationale behind this behaviour lies with a number of '
jpayne@69 9701 'special\n'
jpayne@69 9702 'methods such as "__hash__()" and "__repr__()" that are '
jpayne@69 9703 'implemented by\n'
jpayne@69 9704 'all objects, including type objects. If the implicit lookup '
jpayne@69 9705 'of these\n'
jpayne@69 9706 'methods used the conventional lookup process, they would '
jpayne@69 9707 'fail when\n'
jpayne@69 9708 'invoked on the type object itself:\n'
jpayne@69 9709 '\n'
jpayne@69 9710 ' >>> 1 .__hash__() == hash(1)\n'
jpayne@69 9711 ' True\n'
jpayne@69 9712 ' >>> int.__hash__() == hash(int)\n'
jpayne@69 9713 ' Traceback (most recent call last):\n'
jpayne@69 9714 ' File "<stdin>", line 1, in <module>\n'
jpayne@69 9715 " TypeError: descriptor '__hash__' of 'int' object needs an "
jpayne@69 9716 'argument\n'
jpayne@69 9717 '\n'
jpayne@69 9718 'Incorrectly attempting to invoke an unbound method of a '
jpayne@69 9719 'class in this\n'
jpayne@69 9720 'way is sometimes referred to as ‘metaclass confusion’, and '
jpayne@69 9721 'is avoided\n'
jpayne@69 9722 'by bypassing the instance when looking up special methods:\n'
jpayne@69 9723 '\n'
jpayne@69 9724 ' >>> type(1).__hash__(1) == hash(1)\n'
jpayne@69 9725 ' True\n'
jpayne@69 9726 ' >>> type(int).__hash__(int) == hash(int)\n'
jpayne@69 9727 ' True\n'
jpayne@69 9728 '\n'
jpayne@69 9729 'In addition to bypassing any instance attributes in the '
jpayne@69 9730 'interest of\n'
jpayne@69 9731 'correctness, implicit special method lookup generally also '
jpayne@69 9732 'bypasses\n'
jpayne@69 9733 'the "__getattribute__()" method even of the object’s '
jpayne@69 9734 'metaclass:\n'
jpayne@69 9735 '\n'
jpayne@69 9736 ' >>> class Meta(type):\n'
jpayne@69 9737 ' ... def __getattribute__(*args):\n'
jpayne@69 9738 ' ... print("Metaclass getattribute invoked")\n'
jpayne@69 9739 ' ... return type.__getattribute__(*args)\n'
jpayne@69 9740 ' ...\n'
jpayne@69 9741 ' >>> class C(object, metaclass=Meta):\n'
jpayne@69 9742 ' ... def __len__(self):\n'
jpayne@69 9743 ' ... return 10\n'
jpayne@69 9744 ' ... def __getattribute__(*args):\n'
jpayne@69 9745 ' ... print("Class getattribute invoked")\n'
jpayne@69 9746 ' ... return object.__getattribute__(*args)\n'
jpayne@69 9747 ' ...\n'
jpayne@69 9748 ' >>> c = C()\n'
jpayne@69 9749 ' >>> c.__len__() # Explicit lookup via '
jpayne@69 9750 'instance\n'
jpayne@69 9751 ' Class getattribute invoked\n'
jpayne@69 9752 ' 10\n'
jpayne@69 9753 ' >>> type(c).__len__(c) # Explicit lookup via '
jpayne@69 9754 'type\n'
jpayne@69 9755 ' Metaclass getattribute invoked\n'
jpayne@69 9756 ' 10\n'
jpayne@69 9757 ' >>> len(c) # Implicit lookup\n'
jpayne@69 9758 ' 10\n'
jpayne@69 9759 '\n'
jpayne@69 9760 'Bypassing the "__getattribute__()" machinery in this fashion '
jpayne@69 9761 'provides\n'
jpayne@69 9762 'significant scope for speed optimisations within the '
jpayne@69 9763 'interpreter, at\n'
jpayne@69 9764 'the cost of some flexibility in the handling of special '
jpayne@69 9765 'methods (the\n'
jpayne@69 9766 'special method *must* be set on the class object itself in '
jpayne@69 9767 'order to be\n'
jpayne@69 9768 'consistently invoked by the interpreter).\n',
jpayne@69 9769 'string-methods': 'String Methods\n'
jpayne@69 9770 '**************\n'
jpayne@69 9771 '\n'
jpayne@69 9772 'Strings implement all of the common sequence operations, '
jpayne@69 9773 'along with\n'
jpayne@69 9774 'the additional methods described below.\n'
jpayne@69 9775 '\n'
jpayne@69 9776 'Strings also support two styles of string formatting, one '
jpayne@69 9777 'providing a\n'
jpayne@69 9778 'large degree of flexibility and customization (see '
jpayne@69 9779 '"str.format()",\n'
jpayne@69 9780 'Format String Syntax and Custom String Formatting) and the '
jpayne@69 9781 'other based\n'
jpayne@69 9782 'on C "printf" style formatting that handles a narrower '
jpayne@69 9783 'range of types\n'
jpayne@69 9784 'and is slightly harder to use correctly, but is often '
jpayne@69 9785 'faster for the\n'
jpayne@69 9786 'cases it can handle (printf-style String Formatting).\n'
jpayne@69 9787 '\n'
jpayne@69 9788 'The Text Processing Services section of the standard '
jpayne@69 9789 'library covers a\n'
jpayne@69 9790 'number of other modules that provide various text related '
jpayne@69 9791 'utilities\n'
jpayne@69 9792 '(including regular expression support in the "re" '
jpayne@69 9793 'module).\n'
jpayne@69 9794 '\n'
jpayne@69 9795 'str.capitalize()\n'
jpayne@69 9796 '\n'
jpayne@69 9797 ' Return a copy of the string with its first character '
jpayne@69 9798 'capitalized\n'
jpayne@69 9799 ' and the rest lowercased.\n'
jpayne@69 9800 '\n'
jpayne@69 9801 ' Changed in version 3.8: The first character is now put '
jpayne@69 9802 'into\n'
jpayne@69 9803 ' titlecase rather than uppercase. This means that '
jpayne@69 9804 'characters like\n'
jpayne@69 9805 ' digraphs will only have their first letter capitalized, '
jpayne@69 9806 'instead of\n'
jpayne@69 9807 ' the full character.\n'
jpayne@69 9808 '\n'
jpayne@69 9809 'str.casefold()\n'
jpayne@69 9810 '\n'
jpayne@69 9811 ' Return a casefolded copy of the string. Casefolded '
jpayne@69 9812 'strings may be\n'
jpayne@69 9813 ' used for caseless matching.\n'
jpayne@69 9814 '\n'
jpayne@69 9815 ' Casefolding is similar to lowercasing but more '
jpayne@69 9816 'aggressive because\n'
jpayne@69 9817 ' it is intended to remove all case distinctions in a '
jpayne@69 9818 'string. For\n'
jpayne@69 9819 ' example, the German lowercase letter "\'ß\'" is '
jpayne@69 9820 'equivalent to ""ss"".\n'
jpayne@69 9821 ' Since it is already lowercase, "lower()" would do '
jpayne@69 9822 'nothing to "\'ß\'";\n'
jpayne@69 9823 ' "casefold()" converts it to ""ss"".\n'
jpayne@69 9824 '\n'
jpayne@69 9825 ' The casefolding algorithm is described in section 3.13 '
jpayne@69 9826 'of the\n'
jpayne@69 9827 ' Unicode Standard.\n'
jpayne@69 9828 '\n'
jpayne@69 9829 ' New in version 3.3.\n'
jpayne@69 9830 '\n'
jpayne@69 9831 'str.center(width[, fillchar])\n'
jpayne@69 9832 '\n'
jpayne@69 9833 ' Return centered in a string of length *width*. Padding '
jpayne@69 9834 'is done\n'
jpayne@69 9835 ' using the specified *fillchar* (default is an ASCII '
jpayne@69 9836 'space). The\n'
jpayne@69 9837 ' original string is returned if *width* is less than or '
jpayne@69 9838 'equal to\n'
jpayne@69 9839 ' "len(s)".\n'
jpayne@69 9840 '\n'
jpayne@69 9841 'str.count(sub[, start[, end]])\n'
jpayne@69 9842 '\n'
jpayne@69 9843 ' Return the number of non-overlapping occurrences of '
jpayne@69 9844 'substring *sub*\n'
jpayne@69 9845 ' in the range [*start*, *end*]. Optional arguments '
jpayne@69 9846 '*start* and\n'
jpayne@69 9847 ' *end* are interpreted as in slice notation.\n'
jpayne@69 9848 '\n'
jpayne@69 9849 'str.encode(encoding="utf-8", errors="strict")\n'
jpayne@69 9850 '\n'
jpayne@69 9851 ' Return an encoded version of the string as a bytes '
jpayne@69 9852 'object. Default\n'
jpayne@69 9853 ' encoding is "\'utf-8\'". *errors* may be given to set a '
jpayne@69 9854 'different\n'
jpayne@69 9855 ' error handling scheme. The default for *errors* is '
jpayne@69 9856 '"\'strict\'",\n'
jpayne@69 9857 ' meaning that encoding errors raise a "UnicodeError". '
jpayne@69 9858 'Other possible\n'
jpayne@69 9859 ' values are "\'ignore\'", "\'replace\'", '
jpayne@69 9860 '"\'xmlcharrefreplace\'",\n'
jpayne@69 9861 ' "\'backslashreplace\'" and any other name registered '
jpayne@69 9862 'via\n'
jpayne@69 9863 ' "codecs.register_error()", see section Error Handlers. '
jpayne@69 9864 'For a list\n'
jpayne@69 9865 ' of possible encodings, see section Standard Encodings.\n'
jpayne@69 9866 '\n'
jpayne@69 9867 ' Changed in version 3.1: Support for keyword arguments '
jpayne@69 9868 'added.\n'
jpayne@69 9869 '\n'
jpayne@69 9870 'str.endswith(suffix[, start[, end]])\n'
jpayne@69 9871 '\n'
jpayne@69 9872 ' Return "True" if the string ends with the specified '
jpayne@69 9873 '*suffix*,\n'
jpayne@69 9874 ' otherwise return "False". *suffix* can also be a tuple '
jpayne@69 9875 'of suffixes\n'
jpayne@69 9876 ' to look for. With optional *start*, test beginning at '
jpayne@69 9877 'that\n'
jpayne@69 9878 ' position. With optional *end*, stop comparing at that '
jpayne@69 9879 'position.\n'
jpayne@69 9880 '\n'
jpayne@69 9881 'str.expandtabs(tabsize=8)\n'
jpayne@69 9882 '\n'
jpayne@69 9883 ' Return a copy of the string where all tab characters '
jpayne@69 9884 'are replaced\n'
jpayne@69 9885 ' by one or more spaces, depending on the current column '
jpayne@69 9886 'and the\n'
jpayne@69 9887 ' given tab size. Tab positions occur every *tabsize* '
jpayne@69 9888 'characters\n'
jpayne@69 9889 ' (default is 8, giving tab positions at columns 0, 8, 16 '
jpayne@69 9890 'and so on).\n'
jpayne@69 9891 ' To expand the string, the current column is set to zero '
jpayne@69 9892 'and the\n'
jpayne@69 9893 ' string is examined character by character. If the '
jpayne@69 9894 'character is a\n'
jpayne@69 9895 ' tab ("\\t"), one or more space characters are inserted '
jpayne@69 9896 'in the result\n'
jpayne@69 9897 ' until the current column is equal to the next tab '
jpayne@69 9898 'position. (The\n'
jpayne@69 9899 ' tab character itself is not copied.) If the character '
jpayne@69 9900 'is a newline\n'
jpayne@69 9901 ' ("\\n") or return ("\\r"), it is copied and the current '
jpayne@69 9902 'column is\n'
jpayne@69 9903 ' reset to zero. Any other character is copied unchanged '
jpayne@69 9904 'and the\n'
jpayne@69 9905 ' current column is incremented by one regardless of how '
jpayne@69 9906 'the\n'
jpayne@69 9907 ' character is represented when printed.\n'
jpayne@69 9908 '\n'
jpayne@69 9909 " >>> '01\\t012\\t0123\\t01234'.expandtabs()\n"
jpayne@69 9910 " '01 012 0123 01234'\n"
jpayne@69 9911 " >>> '01\\t012\\t0123\\t01234'.expandtabs(4)\n"
jpayne@69 9912 " '01 012 0123 01234'\n"
jpayne@69 9913 '\n'
jpayne@69 9914 'str.find(sub[, start[, end]])\n'
jpayne@69 9915 '\n'
jpayne@69 9916 ' Return the lowest index in the string where substring '
jpayne@69 9917 '*sub* is\n'
jpayne@69 9918 ' found within the slice "s[start:end]". Optional '
jpayne@69 9919 'arguments *start*\n'
jpayne@69 9920 ' and *end* are interpreted as in slice notation. Return '
jpayne@69 9921 '"-1" if\n'
jpayne@69 9922 ' *sub* is not found.\n'
jpayne@69 9923 '\n'
jpayne@69 9924 ' Note: The "find()" method should be used only if you '
jpayne@69 9925 'need to know\n'
jpayne@69 9926 ' the position of *sub*. To check if *sub* is a '
jpayne@69 9927 'substring or not,\n'
jpayne@69 9928 ' use the "in" operator:\n'
jpayne@69 9929 '\n'
jpayne@69 9930 " >>> 'Py' in 'Python'\n"
jpayne@69 9931 ' True\n'
jpayne@69 9932 '\n'
jpayne@69 9933 'str.format(*args, **kwargs)\n'
jpayne@69 9934 '\n'
jpayne@69 9935 ' Perform a string formatting operation. The string on '
jpayne@69 9936 'which this\n'
jpayne@69 9937 ' method is called can contain literal text or '
jpayne@69 9938 'replacement fields\n'
jpayne@69 9939 ' delimited by braces "{}". Each replacement field '
jpayne@69 9940 'contains either\n'
jpayne@69 9941 ' the numeric index of a positional argument, or the name '
jpayne@69 9942 'of a\n'
jpayne@69 9943 ' keyword argument. Returns a copy of the string where '
jpayne@69 9944 'each\n'
jpayne@69 9945 ' replacement field is replaced with the string value of '
jpayne@69 9946 'the\n'
jpayne@69 9947 ' corresponding argument.\n'
jpayne@69 9948 '\n'
jpayne@69 9949 ' >>> "The sum of 1 + 2 is {0}".format(1+2)\n'
jpayne@69 9950 " 'The sum of 1 + 2 is 3'\n"
jpayne@69 9951 '\n'
jpayne@69 9952 ' See Format String Syntax for a description of the '
jpayne@69 9953 'various\n'
jpayne@69 9954 ' formatting options that can be specified in format '
jpayne@69 9955 'strings.\n'
jpayne@69 9956 '\n'
jpayne@69 9957 ' Note: When formatting a number ("int", "float", '
jpayne@69 9958 '"complex",\n'
jpayne@69 9959 ' "decimal.Decimal" and subclasses) with the "n" type '
jpayne@69 9960 '(ex:\n'
jpayne@69 9961 ' "\'{:n}\'.format(1234)"), the function temporarily '
jpayne@69 9962 'sets the\n'
jpayne@69 9963 ' "LC_CTYPE" locale to the "LC_NUMERIC" locale to '
jpayne@69 9964 'decode\n'
jpayne@69 9965 ' "decimal_point" and "thousands_sep" fields of '
jpayne@69 9966 '"localeconv()" if\n'
jpayne@69 9967 ' they are non-ASCII or longer than 1 byte, and the '
jpayne@69 9968 '"LC_NUMERIC"\n'
jpayne@69 9969 ' locale is different than the "LC_CTYPE" locale. This '
jpayne@69 9970 'temporary\n'
jpayne@69 9971 ' change affects other threads.\n'
jpayne@69 9972 '\n'
jpayne@69 9973 ' Changed in version 3.7: When formatting a number with '
jpayne@69 9974 'the "n" type,\n'
jpayne@69 9975 ' the function sets temporarily the "LC_CTYPE" locale to '
jpayne@69 9976 'the\n'
jpayne@69 9977 ' "LC_NUMERIC" locale in some cases.\n'
jpayne@69 9978 '\n'
jpayne@69 9979 'str.format_map(mapping)\n'
jpayne@69 9980 '\n'
jpayne@69 9981 ' Similar to "str.format(**mapping)", except that '
jpayne@69 9982 '"mapping" is used\n'
jpayne@69 9983 ' directly and not copied to a "dict". This is useful if '
jpayne@69 9984 'for example\n'
jpayne@69 9985 ' "mapping" is a dict subclass:\n'
jpayne@69 9986 '\n'
jpayne@69 9987 ' >>> class Default(dict):\n'
jpayne@69 9988 ' ... def __missing__(self, key):\n'
jpayne@69 9989 ' ... return key\n'
jpayne@69 9990 ' ...\n'
jpayne@69 9991 " >>> '{name} was born in "
jpayne@69 9992 "{country}'.format_map(Default(name='Guido'))\n"
jpayne@69 9993 " 'Guido was born in country'\n"
jpayne@69 9994 '\n'
jpayne@69 9995 ' New in version 3.2.\n'
jpayne@69 9996 '\n'
jpayne@69 9997 'str.index(sub[, start[, end]])\n'
jpayne@69 9998 '\n'
jpayne@69 9999 ' Like "find()", but raise "ValueError" when the '
jpayne@69 10000 'substring is not\n'
jpayne@69 10001 ' found.\n'
jpayne@69 10002 '\n'
jpayne@69 10003 'str.isalnum()\n'
jpayne@69 10004 '\n'
jpayne@69 10005 ' Return "True" if all characters in the string are '
jpayne@69 10006 'alphanumeric and\n'
jpayne@69 10007 ' there is at least one character, "False" otherwise. A '
jpayne@69 10008 'character\n'
jpayne@69 10009 ' "c" is alphanumeric if one of the following returns '
jpayne@69 10010 '"True":\n'
jpayne@69 10011 ' "c.isalpha()", "c.isdecimal()", "c.isdigit()", or '
jpayne@69 10012 '"c.isnumeric()".\n'
jpayne@69 10013 '\n'
jpayne@69 10014 'str.isalpha()\n'
jpayne@69 10015 '\n'
jpayne@69 10016 ' Return "True" if all characters in the string are '
jpayne@69 10017 'alphabetic and\n'
jpayne@69 10018 ' there is at least one character, "False" otherwise. '
jpayne@69 10019 'Alphabetic\n'
jpayne@69 10020 ' characters are those characters defined in the Unicode '
jpayne@69 10021 'character\n'
jpayne@69 10022 ' database as “Letter”, i.e., those with general category '
jpayne@69 10023 'property\n'
jpayne@69 10024 ' being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note '
jpayne@69 10025 'that this is\n'
jpayne@69 10026 ' different from the “Alphabetic” property defined in the '
jpayne@69 10027 'Unicode\n'
jpayne@69 10028 ' Standard.\n'
jpayne@69 10029 '\n'
jpayne@69 10030 'str.isascii()\n'
jpayne@69 10031 '\n'
jpayne@69 10032 ' Return "True" if the string is empty or all characters '
jpayne@69 10033 'in the\n'
jpayne@69 10034 ' string are ASCII, "False" otherwise. ASCII characters '
jpayne@69 10035 'have code\n'
jpayne@69 10036 ' points in the range U+0000-U+007F.\n'
jpayne@69 10037 '\n'
jpayne@69 10038 ' New in version 3.7.\n'
jpayne@69 10039 '\n'
jpayne@69 10040 'str.isdecimal()\n'
jpayne@69 10041 '\n'
jpayne@69 10042 ' Return "True" if all characters in the string are '
jpayne@69 10043 'decimal\n'
jpayne@69 10044 ' characters and there is at least one character, "False" '
jpayne@69 10045 'otherwise.\n'
jpayne@69 10046 ' Decimal characters are those that can be used to form '
jpayne@69 10047 'numbers in\n'
jpayne@69 10048 ' base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. '
jpayne@69 10049 'Formally a decimal\n'
jpayne@69 10050 ' character is a character in the Unicode General '
jpayne@69 10051 'Category “Nd”.\n'
jpayne@69 10052 '\n'
jpayne@69 10053 'str.isdigit()\n'
jpayne@69 10054 '\n'
jpayne@69 10055 ' Return "True" if all characters in the string are '
jpayne@69 10056 'digits and there\n'
jpayne@69 10057 ' is at least one character, "False" otherwise. Digits '
jpayne@69 10058 'include\n'
jpayne@69 10059 ' decimal characters and digits that need special '
jpayne@69 10060 'handling, such as\n'
jpayne@69 10061 ' the compatibility superscript digits. This covers '
jpayne@69 10062 'digits which\n'
jpayne@69 10063 ' cannot be used to form numbers in base 10, like the '
jpayne@69 10064 'Kharosthi\n'
jpayne@69 10065 ' numbers. Formally, a digit is a character that has the '
jpayne@69 10066 'property\n'
jpayne@69 10067 ' value Numeric_Type=Digit or Numeric_Type=Decimal.\n'
jpayne@69 10068 '\n'
jpayne@69 10069 'str.isidentifier()\n'
jpayne@69 10070 '\n'
jpayne@69 10071 ' Return "True" if the string is a valid identifier '
jpayne@69 10072 'according to the\n'
jpayne@69 10073 ' language definition, section Identifiers and keywords.\n'
jpayne@69 10074 '\n'
jpayne@69 10075 ' Call "keyword.iskeyword()" to test whether string "s" '
jpayne@69 10076 'is a reserved\n'
jpayne@69 10077 ' identifier, such as "def" and "class".\n'
jpayne@69 10078 '\n'
jpayne@69 10079 ' Example:\n'
jpayne@69 10080 '\n'
jpayne@69 10081 ' >>> from keyword import iskeyword\n'
jpayne@69 10082 '\n'
jpayne@69 10083 " >>> 'hello'.isidentifier(), iskeyword('hello')\n"
jpayne@69 10084 ' True, False\n'
jpayne@69 10085 " >>> 'def'.isidentifier(), iskeyword('def')\n"
jpayne@69 10086 ' True, True\n'
jpayne@69 10087 '\n'
jpayne@69 10088 'str.islower()\n'
jpayne@69 10089 '\n'
jpayne@69 10090 ' Return "True" if all cased characters [4] in the string '
jpayne@69 10091 'are\n'
jpayne@69 10092 ' lowercase and there is at least one cased character, '
jpayne@69 10093 '"False"\n'
jpayne@69 10094 ' otherwise.\n'
jpayne@69 10095 '\n'
jpayne@69 10096 'str.isnumeric()\n'
jpayne@69 10097 '\n'
jpayne@69 10098 ' Return "True" if all characters in the string are '
jpayne@69 10099 'numeric\n'
jpayne@69 10100 ' characters, and there is at least one character, '
jpayne@69 10101 '"False" otherwise.\n'
jpayne@69 10102 ' Numeric characters include digit characters, and all '
jpayne@69 10103 'characters\n'
jpayne@69 10104 ' that have the Unicode numeric value property, e.g. '
jpayne@69 10105 'U+2155, VULGAR\n'
jpayne@69 10106 ' FRACTION ONE FIFTH. Formally, numeric characters are '
jpayne@69 10107 'those with\n'
jpayne@69 10108 ' the property value Numeric_Type=Digit, '
jpayne@69 10109 'Numeric_Type=Decimal or\n'
jpayne@69 10110 ' Numeric_Type=Numeric.\n'
jpayne@69 10111 '\n'
jpayne@69 10112 'str.isprintable()\n'
jpayne@69 10113 '\n'
jpayne@69 10114 ' Return "True" if all characters in the string are '
jpayne@69 10115 'printable or the\n'
jpayne@69 10116 ' string is empty, "False" otherwise. Nonprintable '
jpayne@69 10117 'characters are\n'
jpayne@69 10118 ' those characters defined in the Unicode character '
jpayne@69 10119 'database as\n'
jpayne@69 10120 ' “Other” or “Separator”, excepting the ASCII space '
jpayne@69 10121 '(0x20) which is\n'
jpayne@69 10122 ' considered printable. (Note that printable characters '
jpayne@69 10123 'in this\n'
jpayne@69 10124 ' context are those which should not be escaped when '
jpayne@69 10125 '"repr()" is\n'
jpayne@69 10126 ' invoked on a string. It has no bearing on the handling '
jpayne@69 10127 'of strings\n'
jpayne@69 10128 ' written to "sys.stdout" or "sys.stderr".)\n'
jpayne@69 10129 '\n'
jpayne@69 10130 'str.isspace()\n'
jpayne@69 10131 '\n'
jpayne@69 10132 ' Return "True" if there are only whitespace characters '
jpayne@69 10133 'in the string\n'
jpayne@69 10134 ' and there is at least one character, "False" '
jpayne@69 10135 'otherwise.\n'
jpayne@69 10136 '\n'
jpayne@69 10137 ' A character is *whitespace* if in the Unicode character '
jpayne@69 10138 'database\n'
jpayne@69 10139 ' (see "unicodedata"), either its general category is '
jpayne@69 10140 '"Zs"\n'
jpayne@69 10141 ' (“Separator, space”), or its bidirectional class is one '
jpayne@69 10142 'of "WS",\n'
jpayne@69 10143 ' "B", or "S".\n'
jpayne@69 10144 '\n'
jpayne@69 10145 'str.istitle()\n'
jpayne@69 10146 '\n'
jpayne@69 10147 ' Return "True" if the string is a titlecased string and '
jpayne@69 10148 'there is at\n'
jpayne@69 10149 ' least one character, for example uppercase characters '
jpayne@69 10150 'may only\n'
jpayne@69 10151 ' follow uncased characters and lowercase characters only '
jpayne@69 10152 'cased ones.\n'
jpayne@69 10153 ' Return "False" otherwise.\n'
jpayne@69 10154 '\n'
jpayne@69 10155 'str.isupper()\n'
jpayne@69 10156 '\n'
jpayne@69 10157 ' Return "True" if all cased characters [4] in the string '
jpayne@69 10158 'are\n'
jpayne@69 10159 ' uppercase and there is at least one cased character, '
jpayne@69 10160 '"False"\n'
jpayne@69 10161 ' otherwise.\n'
jpayne@69 10162 '\n'
jpayne@69 10163 'str.join(iterable)\n'
jpayne@69 10164 '\n'
jpayne@69 10165 ' Return a string which is the concatenation of the '
jpayne@69 10166 'strings in\n'
jpayne@69 10167 ' *iterable*. A "TypeError" will be raised if there are '
jpayne@69 10168 'any non-\n'
jpayne@69 10169 ' string values in *iterable*, including "bytes" '
jpayne@69 10170 'objects. The\n'
jpayne@69 10171 ' separator between elements is the string providing this '
jpayne@69 10172 'method.\n'
jpayne@69 10173 '\n'
jpayne@69 10174 'str.ljust(width[, fillchar])\n'
jpayne@69 10175 '\n'
jpayne@69 10176 ' Return the string left justified in a string of length '
jpayne@69 10177 '*width*.\n'
jpayne@69 10178 ' Padding is done using the specified *fillchar* (default '
jpayne@69 10179 'is an ASCII\n'
jpayne@69 10180 ' space). The original string is returned if *width* is '
jpayne@69 10181 'less than or\n'
jpayne@69 10182 ' equal to "len(s)".\n'
jpayne@69 10183 '\n'
jpayne@69 10184 'str.lower()\n'
jpayne@69 10185 '\n'
jpayne@69 10186 ' Return a copy of the string with all the cased '
jpayne@69 10187 'characters [4]\n'
jpayne@69 10188 ' converted to lowercase.\n'
jpayne@69 10189 '\n'
jpayne@69 10190 ' The lowercasing algorithm used is described in section '
jpayne@69 10191 '3.13 of the\n'
jpayne@69 10192 ' Unicode Standard.\n'
jpayne@69 10193 '\n'
jpayne@69 10194 'str.lstrip([chars])\n'
jpayne@69 10195 '\n'
jpayne@69 10196 ' Return a copy of the string with leading characters '
jpayne@69 10197 'removed. The\n'
jpayne@69 10198 ' *chars* argument is a string specifying the set of '
jpayne@69 10199 'characters to be\n'
jpayne@69 10200 ' removed. If omitted or "None", the *chars* argument '
jpayne@69 10201 'defaults to\n'
jpayne@69 10202 ' removing whitespace. The *chars* argument is not a '
jpayne@69 10203 'prefix; rather,\n'
jpayne@69 10204 ' all combinations of its values are stripped:\n'
jpayne@69 10205 '\n'
jpayne@69 10206 " >>> ' spacious '.lstrip()\n"
jpayne@69 10207 " 'spacious '\n"
jpayne@69 10208 " >>> 'www.example.com'.lstrip('cmowz.')\n"
jpayne@69 10209 " 'example.com'\n"
jpayne@69 10210 '\n'
jpayne@69 10211 'static str.maketrans(x[, y[, z]])\n'
jpayne@69 10212 '\n'
jpayne@69 10213 ' This static method returns a translation table usable '
jpayne@69 10214 'for\n'
jpayne@69 10215 ' "str.translate()".\n'
jpayne@69 10216 '\n'
jpayne@69 10217 ' If there is only one argument, it must be a dictionary '
jpayne@69 10218 'mapping\n'
jpayne@69 10219 ' Unicode ordinals (integers) or characters (strings of '
jpayne@69 10220 'length 1) to\n'
jpayne@69 10221 ' Unicode ordinals, strings (of arbitrary lengths) or '
jpayne@69 10222 '"None".\n'
jpayne@69 10223 ' Character keys will then be converted to ordinals.\n'
jpayne@69 10224 '\n'
jpayne@69 10225 ' If there are two arguments, they must be strings of '
jpayne@69 10226 'equal length,\n'
jpayne@69 10227 ' and in the resulting dictionary, each character in x '
jpayne@69 10228 'will be mapped\n'
jpayne@69 10229 ' to the character at the same position in y. If there '
jpayne@69 10230 'is a third\n'
jpayne@69 10231 ' argument, it must be a string, whose characters will be '
jpayne@69 10232 'mapped to\n'
jpayne@69 10233 ' "None" in the result.\n'
jpayne@69 10234 '\n'
jpayne@69 10235 'str.partition(sep)\n'
jpayne@69 10236 '\n'
jpayne@69 10237 ' Split the string at the first occurrence of *sep*, and '
jpayne@69 10238 'return a\n'
jpayne@69 10239 ' 3-tuple containing the part before the separator, the '
jpayne@69 10240 'separator\n'
jpayne@69 10241 ' itself, and the part after the separator. If the '
jpayne@69 10242 'separator is not\n'
jpayne@69 10243 ' found, return a 3-tuple containing the string itself, '
jpayne@69 10244 'followed by\n'
jpayne@69 10245 ' two empty strings.\n'
jpayne@69 10246 '\n'
jpayne@69 10247 'str.replace(old, new[, count])\n'
jpayne@69 10248 '\n'
jpayne@69 10249 ' Return a copy of the string with all occurrences of '
jpayne@69 10250 'substring *old*\n'
jpayne@69 10251 ' replaced by *new*. If the optional argument *count* is '
jpayne@69 10252 'given, only\n'
jpayne@69 10253 ' the first *count* occurrences are replaced.\n'
jpayne@69 10254 '\n'
jpayne@69 10255 'str.rfind(sub[, start[, end]])\n'
jpayne@69 10256 '\n'
jpayne@69 10257 ' Return the highest index in the string where substring '
jpayne@69 10258 '*sub* is\n'
jpayne@69 10259 ' found, such that *sub* is contained within '
jpayne@69 10260 '"s[start:end]".\n'
jpayne@69 10261 ' Optional arguments *start* and *end* are interpreted as '
jpayne@69 10262 'in slice\n'
jpayne@69 10263 ' notation. Return "-1" on failure.\n'
jpayne@69 10264 '\n'
jpayne@69 10265 'str.rindex(sub[, start[, end]])\n'
jpayne@69 10266 '\n'
jpayne@69 10267 ' Like "rfind()" but raises "ValueError" when the '
jpayne@69 10268 'substring *sub* is\n'
jpayne@69 10269 ' not found.\n'
jpayne@69 10270 '\n'
jpayne@69 10271 'str.rjust(width[, fillchar])\n'
jpayne@69 10272 '\n'
jpayne@69 10273 ' Return the string right justified in a string of length '
jpayne@69 10274 '*width*.\n'
jpayne@69 10275 ' Padding is done using the specified *fillchar* (default '
jpayne@69 10276 'is an ASCII\n'
jpayne@69 10277 ' space). The original string is returned if *width* is '
jpayne@69 10278 'less than or\n'
jpayne@69 10279 ' equal to "len(s)".\n'
jpayne@69 10280 '\n'
jpayne@69 10281 'str.rpartition(sep)\n'
jpayne@69 10282 '\n'
jpayne@69 10283 ' Split the string at the last occurrence of *sep*, and '
jpayne@69 10284 'return a\n'
jpayne@69 10285 ' 3-tuple containing the part before the separator, the '
jpayne@69 10286 'separator\n'
jpayne@69 10287 ' itself, and the part after the separator. If the '
jpayne@69 10288 'separator is not\n'
jpayne@69 10289 ' found, return a 3-tuple containing two empty strings, '
jpayne@69 10290 'followed by\n'
jpayne@69 10291 ' the string itself.\n'
jpayne@69 10292 '\n'
jpayne@69 10293 'str.rsplit(sep=None, maxsplit=-1)\n'
jpayne@69 10294 '\n'
jpayne@69 10295 ' Return a list of the words in the string, using *sep* '
jpayne@69 10296 'as the\n'
jpayne@69 10297 ' delimiter string. If *maxsplit* is given, at most '
jpayne@69 10298 '*maxsplit* splits\n'
jpayne@69 10299 ' are done, the *rightmost* ones. If *sep* is not '
jpayne@69 10300 'specified or\n'
jpayne@69 10301 ' "None", any whitespace string is a separator. Except '
jpayne@69 10302 'for splitting\n'
jpayne@69 10303 ' from the right, "rsplit()" behaves like "split()" which '
jpayne@69 10304 'is\n'
jpayne@69 10305 ' described in detail below.\n'
jpayne@69 10306 '\n'
jpayne@69 10307 'str.rstrip([chars])\n'
jpayne@69 10308 '\n'
jpayne@69 10309 ' Return a copy of the string with trailing characters '
jpayne@69 10310 'removed. The\n'
jpayne@69 10311 ' *chars* argument is a string specifying the set of '
jpayne@69 10312 'characters to be\n'
jpayne@69 10313 ' removed. If omitted or "None", the *chars* argument '
jpayne@69 10314 'defaults to\n'
jpayne@69 10315 ' removing whitespace. The *chars* argument is not a '
jpayne@69 10316 'suffix; rather,\n'
jpayne@69 10317 ' all combinations of its values are stripped:\n'
jpayne@69 10318 '\n'
jpayne@69 10319 " >>> ' spacious '.rstrip()\n"
jpayne@69 10320 " ' spacious'\n"
jpayne@69 10321 " >>> 'mississippi'.rstrip('ipz')\n"
jpayne@69 10322 " 'mississ'\n"
jpayne@69 10323 '\n'
jpayne@69 10324 'str.split(sep=None, maxsplit=-1)\n'
jpayne@69 10325 '\n'
jpayne@69 10326 ' Return a list of the words in the string, using *sep* '
jpayne@69 10327 'as the\n'
jpayne@69 10328 ' delimiter string. If *maxsplit* is given, at most '
jpayne@69 10329 '*maxsplit*\n'
jpayne@69 10330 ' splits are done (thus, the list will have at most '
jpayne@69 10331 '"maxsplit+1"\n'
jpayne@69 10332 ' elements). If *maxsplit* is not specified or "-1", '
jpayne@69 10333 'then there is\n'
jpayne@69 10334 ' no limit on the number of splits (all possible splits '
jpayne@69 10335 'are made).\n'
jpayne@69 10336 '\n'
jpayne@69 10337 ' If *sep* is given, consecutive delimiters are not '
jpayne@69 10338 'grouped together\n'
jpayne@69 10339 ' and are deemed to delimit empty strings (for example,\n'
jpayne@69 10340 ' "\'1,,2\'.split(\',\')" returns "[\'1\', \'\', '
jpayne@69 10341 '\'2\']"). The *sep* argument\n'
jpayne@69 10342 ' may consist of multiple characters (for example,\n'
jpayne@69 10343 ' "\'1<>2<>3\'.split(\'<>\')" returns "[\'1\', \'2\', '
jpayne@69 10344 '\'3\']"). Splitting an\n'
jpayne@69 10345 ' empty string with a specified separator returns '
jpayne@69 10346 '"[\'\']".\n'
jpayne@69 10347 '\n'
jpayne@69 10348 ' For example:\n'
jpayne@69 10349 '\n'
jpayne@69 10350 " >>> '1,2,3'.split(',')\n"
jpayne@69 10351 " ['1', '2', '3']\n"
jpayne@69 10352 " >>> '1,2,3'.split(',', maxsplit=1)\n"
jpayne@69 10353 " ['1', '2,3']\n"
jpayne@69 10354 " >>> '1,2,,3,'.split(',')\n"
jpayne@69 10355 " ['1', '2', '', '3', '']\n"
jpayne@69 10356 '\n'
jpayne@69 10357 ' If *sep* is not specified or is "None", a different '
jpayne@69 10358 'splitting\n'
jpayne@69 10359 ' algorithm is applied: runs of consecutive whitespace '
jpayne@69 10360 'are regarded\n'
jpayne@69 10361 ' as a single separator, and the result will contain no '
jpayne@69 10362 'empty strings\n'
jpayne@69 10363 ' at the start or end if the string has leading or '
jpayne@69 10364 'trailing\n'
jpayne@69 10365 ' whitespace. Consequently, splitting an empty string or '
jpayne@69 10366 'a string\n'
jpayne@69 10367 ' consisting of just whitespace with a "None" separator '
jpayne@69 10368 'returns "[]".\n'
jpayne@69 10369 '\n'
jpayne@69 10370 ' For example:\n'
jpayne@69 10371 '\n'
jpayne@69 10372 " >>> '1 2 3'.split()\n"
jpayne@69 10373 " ['1', '2', '3']\n"
jpayne@69 10374 " >>> '1 2 3'.split(maxsplit=1)\n"
jpayne@69 10375 " ['1', '2 3']\n"
jpayne@69 10376 " >>> ' 1 2 3 '.split()\n"
jpayne@69 10377 " ['1', '2', '3']\n"
jpayne@69 10378 '\n'
jpayne@69 10379 'str.splitlines([keepends])\n'
jpayne@69 10380 '\n'
jpayne@69 10381 ' Return a list of the lines in the string, breaking at '
jpayne@69 10382 'line\n'
jpayne@69 10383 ' boundaries. Line breaks are not included in the '
jpayne@69 10384 'resulting list\n'
jpayne@69 10385 ' unless *keepends* is given and true.\n'
jpayne@69 10386 '\n'
jpayne@69 10387 ' This method splits on the following line boundaries. '
jpayne@69 10388 'In\n'
jpayne@69 10389 ' particular, the boundaries are a superset of *universal '
jpayne@69 10390 'newlines*.\n'
jpayne@69 10391 '\n'
jpayne@69 10392 ' '
jpayne@69 10393 '+-------------------------+-------------------------------+\n'
jpayne@69 10394 ' | Representation | '
jpayne@69 10395 'Description |\n'
jpayne@69 10396 ' '
jpayne@69 10397 '|=========================|===============================|\n'
jpayne@69 10398 ' | "\\n" | Line '
jpayne@69 10399 'Feed |\n'
jpayne@69 10400 ' '
jpayne@69 10401 '+-------------------------+-------------------------------+\n'
jpayne@69 10402 ' | "\\r" | Carriage '
jpayne@69 10403 'Return |\n'
jpayne@69 10404 ' '
jpayne@69 10405 '+-------------------------+-------------------------------+\n'
jpayne@69 10406 ' | "\\r\\n" | Carriage Return + Line '
jpayne@69 10407 'Feed |\n'
jpayne@69 10408 ' '
jpayne@69 10409 '+-------------------------+-------------------------------+\n'
jpayne@69 10410 ' | "\\v" or "\\x0b" | Line '
jpayne@69 10411 'Tabulation |\n'
jpayne@69 10412 ' '
jpayne@69 10413 '+-------------------------+-------------------------------+\n'
jpayne@69 10414 ' | "\\f" or "\\x0c" | Form '
jpayne@69 10415 'Feed |\n'
jpayne@69 10416 ' '
jpayne@69 10417 '+-------------------------+-------------------------------+\n'
jpayne@69 10418 ' | "\\x1c" | File '
jpayne@69 10419 'Separator |\n'
jpayne@69 10420 ' '
jpayne@69 10421 '+-------------------------+-------------------------------+\n'
jpayne@69 10422 ' | "\\x1d" | Group '
jpayne@69 10423 'Separator |\n'
jpayne@69 10424 ' '
jpayne@69 10425 '+-------------------------+-------------------------------+\n'
jpayne@69 10426 ' | "\\x1e" | Record '
jpayne@69 10427 'Separator |\n'
jpayne@69 10428 ' '
jpayne@69 10429 '+-------------------------+-------------------------------+\n'
jpayne@69 10430 ' | "\\x85" | Next Line (C1 Control '
jpayne@69 10431 'Code) |\n'
jpayne@69 10432 ' '
jpayne@69 10433 '+-------------------------+-------------------------------+\n'
jpayne@69 10434 ' | "\\u2028" | Line '
jpayne@69 10435 'Separator |\n'
jpayne@69 10436 ' '
jpayne@69 10437 '+-------------------------+-------------------------------+\n'
jpayne@69 10438 ' | "\\u2029" | Paragraph '
jpayne@69 10439 'Separator |\n'
jpayne@69 10440 ' '
jpayne@69 10441 '+-------------------------+-------------------------------+\n'
jpayne@69 10442 '\n'
jpayne@69 10443 ' Changed in version 3.2: "\\v" and "\\f" added to list '
jpayne@69 10444 'of line\n'
jpayne@69 10445 ' boundaries.\n'
jpayne@69 10446 '\n'
jpayne@69 10447 ' For example:\n'
jpayne@69 10448 '\n'
jpayne@69 10449 " >>> 'ab c\\n\\nde fg\\rkl\\r\\n'.splitlines()\n"
jpayne@69 10450 " ['ab c', '', 'de fg', 'kl']\n"
jpayne@69 10451 " >>> 'ab c\\n\\nde "
jpayne@69 10452 "fg\\rkl\\r\\n'.splitlines(keepends=True)\n"
jpayne@69 10453 " ['ab c\\n', '\\n', 'de fg\\r', 'kl\\r\\n']\n"
jpayne@69 10454 '\n'
jpayne@69 10455 ' Unlike "split()" when a delimiter string *sep* is '
jpayne@69 10456 'given, this\n'
jpayne@69 10457 ' method returns an empty list for the empty string, and '
jpayne@69 10458 'a terminal\n'
jpayne@69 10459 ' line break does not result in an extra line:\n'
jpayne@69 10460 '\n'
jpayne@69 10461 ' >>> "".splitlines()\n'
jpayne@69 10462 ' []\n'
jpayne@69 10463 ' >>> "One line\\n".splitlines()\n'
jpayne@69 10464 " ['One line']\n"
jpayne@69 10465 '\n'
jpayne@69 10466 ' For comparison, "split(\'\\n\')" gives:\n'
jpayne@69 10467 '\n'
jpayne@69 10468 " >>> ''.split('\\n')\n"
jpayne@69 10469 " ['']\n"
jpayne@69 10470 " >>> 'Two lines\\n'.split('\\n')\n"
jpayne@69 10471 " ['Two lines', '']\n"
jpayne@69 10472 '\n'
jpayne@69 10473 'str.startswith(prefix[, start[, end]])\n'
jpayne@69 10474 '\n'
jpayne@69 10475 ' Return "True" if string starts with the *prefix*, '
jpayne@69 10476 'otherwise return\n'
jpayne@69 10477 ' "False". *prefix* can also be a tuple of prefixes to '
jpayne@69 10478 'look for.\n'
jpayne@69 10479 ' With optional *start*, test string beginning at that '
jpayne@69 10480 'position.\n'
jpayne@69 10481 ' With optional *end*, stop comparing string at that '
jpayne@69 10482 'position.\n'
jpayne@69 10483 '\n'
jpayne@69 10484 'str.strip([chars])\n'
jpayne@69 10485 '\n'
jpayne@69 10486 ' Return a copy of the string with the leading and '
jpayne@69 10487 'trailing\n'
jpayne@69 10488 ' characters removed. The *chars* argument is a string '
jpayne@69 10489 'specifying the\n'
jpayne@69 10490 ' set of characters to be removed. If omitted or "None", '
jpayne@69 10491 'the *chars*\n'
jpayne@69 10492 ' argument defaults to removing whitespace. The *chars* '
jpayne@69 10493 'argument is\n'
jpayne@69 10494 ' not a prefix or suffix; rather, all combinations of its '
jpayne@69 10495 'values are\n'
jpayne@69 10496 ' stripped:\n'
jpayne@69 10497 '\n'
jpayne@69 10498 " >>> ' spacious '.strip()\n"
jpayne@69 10499 " 'spacious'\n"
jpayne@69 10500 " >>> 'www.example.com'.strip('cmowz.')\n"
jpayne@69 10501 " 'example'\n"
jpayne@69 10502 '\n'
jpayne@69 10503 ' The outermost leading and trailing *chars* argument '
jpayne@69 10504 'values are\n'
jpayne@69 10505 ' stripped from the string. Characters are removed from '
jpayne@69 10506 'the leading\n'
jpayne@69 10507 ' end until reaching a string character that is not '
jpayne@69 10508 'contained in the\n'
jpayne@69 10509 ' set of characters in *chars*. A similar action takes '
jpayne@69 10510 'place on the\n'
jpayne@69 10511 ' trailing end. For example:\n'
jpayne@69 10512 '\n'
jpayne@69 10513 " >>> comment_string = '#....... Section 3.2.1 Issue "
jpayne@69 10514 "#32 .......'\n"
jpayne@69 10515 " >>> comment_string.strip('.#! ')\n"
jpayne@69 10516 " 'Section 3.2.1 Issue #32'\n"
jpayne@69 10517 '\n'
jpayne@69 10518 'str.swapcase()\n'
jpayne@69 10519 '\n'
jpayne@69 10520 ' Return a copy of the string with uppercase characters '
jpayne@69 10521 'converted to\n'
jpayne@69 10522 ' lowercase and vice versa. Note that it is not '
jpayne@69 10523 'necessarily true that\n'
jpayne@69 10524 ' "s.swapcase().swapcase() == s".\n'
jpayne@69 10525 '\n'
jpayne@69 10526 'str.title()\n'
jpayne@69 10527 '\n'
jpayne@69 10528 ' Return a titlecased version of the string where words '
jpayne@69 10529 'start with an\n'
jpayne@69 10530 ' uppercase character and the remaining characters are '
jpayne@69 10531 'lowercase.\n'
jpayne@69 10532 '\n'
jpayne@69 10533 ' For example:\n'
jpayne@69 10534 '\n'
jpayne@69 10535 " >>> 'Hello world'.title()\n"
jpayne@69 10536 " 'Hello World'\n"
jpayne@69 10537 '\n'
jpayne@69 10538 ' The algorithm uses a simple language-independent '
jpayne@69 10539 'definition of a\n'
jpayne@69 10540 ' word as groups of consecutive letters. The definition '
jpayne@69 10541 'works in\n'
jpayne@69 10542 ' many contexts but it means that apostrophes in '
jpayne@69 10543 'contractions and\n'
jpayne@69 10544 ' possessives form word boundaries, which may not be the '
jpayne@69 10545 'desired\n'
jpayne@69 10546 ' result:\n'
jpayne@69 10547 '\n'
jpayne@69 10548 ' >>> "they\'re bill\'s friends from the UK".title()\n'
jpayne@69 10549 ' "They\'Re Bill\'S Friends From The Uk"\n'
jpayne@69 10550 '\n'
jpayne@69 10551 ' A workaround for apostrophes can be constructed using '
jpayne@69 10552 'regular\n'
jpayne@69 10553 ' expressions:\n'
jpayne@69 10554 '\n'
jpayne@69 10555 ' >>> import re\n'
jpayne@69 10556 ' >>> def titlecase(s):\n'
jpayne@69 10557 ' ... return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n'
jpayne@69 10558 ' ... lambda mo: '
jpayne@69 10559 'mo.group(0).capitalize(),\n'
jpayne@69 10560 ' ... s)\n'
jpayne@69 10561 ' ...\n'
jpayne@69 10562 ' >>> titlecase("they\'re bill\'s friends.")\n'
jpayne@69 10563 ' "They\'re Bill\'s Friends."\n'
jpayne@69 10564 '\n'
jpayne@69 10565 'str.translate(table)\n'
jpayne@69 10566 '\n'
jpayne@69 10567 ' Return a copy of the string in which each character has '
jpayne@69 10568 'been mapped\n'
jpayne@69 10569 ' through the given translation table. The table must be '
jpayne@69 10570 'an object\n'
jpayne@69 10571 ' that implements indexing via "__getitem__()", typically '
jpayne@69 10572 'a *mapping*\n'
jpayne@69 10573 ' or *sequence*. When indexed by a Unicode ordinal (an '
jpayne@69 10574 'integer), the\n'
jpayne@69 10575 ' table object can do any of the following: return a '
jpayne@69 10576 'Unicode ordinal\n'
jpayne@69 10577 ' or a string, to map the character to one or more other '
jpayne@69 10578 'characters;\n'
jpayne@69 10579 ' return "None", to delete the character from the return '
jpayne@69 10580 'string; or\n'
jpayne@69 10581 ' raise a "LookupError" exception, to map the character '
jpayne@69 10582 'to itself.\n'
jpayne@69 10583 '\n'
jpayne@69 10584 ' You can use "str.maketrans()" to create a translation '
jpayne@69 10585 'map from\n'
jpayne@69 10586 ' character-to-character mappings in different formats.\n'
jpayne@69 10587 '\n'
jpayne@69 10588 ' See also the "codecs" module for a more flexible '
jpayne@69 10589 'approach to custom\n'
jpayne@69 10590 ' character mappings.\n'
jpayne@69 10591 '\n'
jpayne@69 10592 'str.upper()\n'
jpayne@69 10593 '\n'
jpayne@69 10594 ' Return a copy of the string with all the cased '
jpayne@69 10595 'characters [4]\n'
jpayne@69 10596 ' converted to uppercase. Note that '
jpayne@69 10597 '"s.upper().isupper()" might be\n'
jpayne@69 10598 ' "False" if "s" contains uncased characters or if the '
jpayne@69 10599 'Unicode\n'
jpayne@69 10600 ' category of the resulting character(s) is not “Lu” '
jpayne@69 10601 '(Letter,\n'
jpayne@69 10602 ' uppercase), but e.g. “Lt” (Letter, titlecase).\n'
jpayne@69 10603 '\n'
jpayne@69 10604 ' The uppercasing algorithm used is described in section '
jpayne@69 10605 '3.13 of the\n'
jpayne@69 10606 ' Unicode Standard.\n'
jpayne@69 10607 '\n'
jpayne@69 10608 'str.zfill(width)\n'
jpayne@69 10609 '\n'
jpayne@69 10610 ' Return a copy of the string left filled with ASCII '
jpayne@69 10611 '"\'0\'" digits to\n'
jpayne@69 10612 ' make a string of length *width*. A leading sign prefix\n'
jpayne@69 10613 ' ("\'+\'"/"\'-\'") is handled by inserting the padding '
jpayne@69 10614 '*after* the sign\n'
jpayne@69 10615 ' character rather than before. The original string is '
jpayne@69 10616 'returned if\n'
jpayne@69 10617 ' *width* is less than or equal to "len(s)".\n'
jpayne@69 10618 '\n'
jpayne@69 10619 ' For example:\n'
jpayne@69 10620 '\n'
jpayne@69 10621 ' >>> "42".zfill(5)\n'
jpayne@69 10622 " '00042'\n"
jpayne@69 10623 ' >>> "-42".zfill(5)\n'
jpayne@69 10624 " '-0042'\n",
jpayne@69 10625 'strings': 'String and Bytes literals\n'
jpayne@69 10626 '*************************\n'
jpayne@69 10627 '\n'
jpayne@69 10628 'String literals are described by the following lexical '
jpayne@69 10629 'definitions:\n'
jpayne@69 10630 '\n'
jpayne@69 10631 ' stringliteral ::= [stringprefix](shortstring | longstring)\n'
jpayne@69 10632 ' stringprefix ::= "r" | "u" | "R" | "U" | "f" | "F"\n'
jpayne@69 10633 ' | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | '
jpayne@69 10634 '"Rf" | "RF"\n'
jpayne@69 10635 ' shortstring ::= "\'" shortstringitem* "\'" | \'"\' '
jpayne@69 10636 'shortstringitem* \'"\'\n'
jpayne@69 10637 ' longstring ::= "\'\'\'" longstringitem* "\'\'\'" | '
jpayne@69 10638 '\'"""\' longstringitem* \'"""\'\n'
jpayne@69 10639 ' shortstringitem ::= shortstringchar | stringescapeseq\n'
jpayne@69 10640 ' longstringitem ::= longstringchar | stringescapeseq\n'
jpayne@69 10641 ' shortstringchar ::= <any source character except "\\" or '
jpayne@69 10642 'newline or the quote>\n'
jpayne@69 10643 ' longstringchar ::= <any source character except "\\">\n'
jpayne@69 10644 ' stringescapeseq ::= "\\" <any source character>\n'
jpayne@69 10645 '\n'
jpayne@69 10646 ' bytesliteral ::= bytesprefix(shortbytes | longbytes)\n'
jpayne@69 10647 ' bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" | '
jpayne@69 10648 '"rb" | "rB" | "Rb" | "RB"\n'
jpayne@69 10649 ' shortbytes ::= "\'" shortbytesitem* "\'" | \'"\' '
jpayne@69 10650 'shortbytesitem* \'"\'\n'
jpayne@69 10651 ' longbytes ::= "\'\'\'" longbytesitem* "\'\'\'" | \'"""\' '
jpayne@69 10652 'longbytesitem* \'"""\'\n'
jpayne@69 10653 ' shortbytesitem ::= shortbyteschar | bytesescapeseq\n'
jpayne@69 10654 ' longbytesitem ::= longbyteschar | bytesescapeseq\n'
jpayne@69 10655 ' shortbyteschar ::= <any ASCII character except "\\" or newline '
jpayne@69 10656 'or the quote>\n'
jpayne@69 10657 ' longbyteschar ::= <any ASCII character except "\\">\n'
jpayne@69 10658 ' bytesescapeseq ::= "\\" <any ASCII character>\n'
jpayne@69 10659 '\n'
jpayne@69 10660 'One syntactic restriction not indicated by these productions is '
jpayne@69 10661 'that\n'
jpayne@69 10662 'whitespace is not allowed between the "stringprefix" or '
jpayne@69 10663 '"bytesprefix"\n'
jpayne@69 10664 'and the rest of the literal. The source character set is defined '
jpayne@69 10665 'by\n'
jpayne@69 10666 'the encoding declaration; it is UTF-8 if no encoding declaration '
jpayne@69 10667 'is\n'
jpayne@69 10668 'given in the source file; see section Encoding declarations.\n'
jpayne@69 10669 '\n'
jpayne@69 10670 'In plain English: Both types of literals can be enclosed in '
jpayne@69 10671 'matching\n'
jpayne@69 10672 'single quotes ("\'") or double quotes ("""). They can also be '
jpayne@69 10673 'enclosed\n'
jpayne@69 10674 'in matching groups of three single or double quotes (these are\n'
jpayne@69 10675 'generally referred to as *triple-quoted strings*). The '
jpayne@69 10676 'backslash\n'
jpayne@69 10677 '("\\") character is used to escape characters that otherwise have '
jpayne@69 10678 'a\n'
jpayne@69 10679 'special meaning, such as newline, backslash itself, or the quote\n'
jpayne@69 10680 'character.\n'
jpayne@69 10681 '\n'
jpayne@69 10682 'Bytes literals are always prefixed with "\'b\'" or "\'B\'"; they '
jpayne@69 10683 'produce\n'
jpayne@69 10684 'an instance of the "bytes" type instead of the "str" type. They '
jpayne@69 10685 'may\n'
jpayne@69 10686 'only contain ASCII characters; bytes with a numeric value of 128 '
jpayne@69 10687 'or\n'
jpayne@69 10688 'greater must be expressed with escapes.\n'
jpayne@69 10689 '\n'
jpayne@69 10690 'Both string and bytes literals may optionally be prefixed with a\n'
jpayne@69 10691 'letter "\'r\'" or "\'R\'"; such strings are called *raw strings* '
jpayne@69 10692 'and treat\n'
jpayne@69 10693 'backslashes as literal characters. As a result, in string '
jpayne@69 10694 'literals,\n'
jpayne@69 10695 '"\'\\U\'" and "\'\\u\'" escapes in raw strings are not treated '
jpayne@69 10696 'specially.\n'
jpayne@69 10697 'Given that Python 2.x’s raw unicode literals behave differently '
jpayne@69 10698 'than\n'
jpayne@69 10699 'Python 3.x’s the "\'ur\'" syntax is not supported.\n'
jpayne@69 10700 '\n'
jpayne@69 10701 'New in version 3.3: The "\'rb\'" prefix of raw bytes literals has '
jpayne@69 10702 'been\n'
jpayne@69 10703 'added as a synonym of "\'br\'".\n'
jpayne@69 10704 '\n'
jpayne@69 10705 'New in version 3.3: Support for the unicode legacy literal\n'
jpayne@69 10706 '("u\'value\'") was reintroduced to simplify the maintenance of '
jpayne@69 10707 'dual\n'
jpayne@69 10708 'Python 2.x and 3.x codebases. See **PEP 414** for more '
jpayne@69 10709 'information.\n'
jpayne@69 10710 '\n'
jpayne@69 10711 'A string literal with "\'f\'" or "\'F\'" in its prefix is a '
jpayne@69 10712 '*formatted\n'
jpayne@69 10713 'string literal*; see Formatted string literals. The "\'f\'" may '
jpayne@69 10714 'be\n'
jpayne@69 10715 'combined with "\'r\'", but not with "\'b\'" or "\'u\'", therefore '
jpayne@69 10716 'raw\n'
jpayne@69 10717 'formatted strings are possible, but formatted bytes literals are '
jpayne@69 10718 'not.\n'
jpayne@69 10719 '\n'
jpayne@69 10720 'In triple-quoted literals, unescaped newlines and quotes are '
jpayne@69 10721 'allowed\n'
jpayne@69 10722 '(and are retained), except that three unescaped quotes in a row\n'
jpayne@69 10723 'terminate the literal. (A “quote” is the character used to open '
jpayne@69 10724 'the\n'
jpayne@69 10725 'literal, i.e. either "\'" or """.)\n'
jpayne@69 10726 '\n'
jpayne@69 10727 'Unless an "\'r\'" or "\'R\'" prefix is present, escape sequences '
jpayne@69 10728 'in string\n'
jpayne@69 10729 'and bytes literals are interpreted according to rules similar to '
jpayne@69 10730 'those\n'
jpayne@69 10731 'used by Standard C. The recognized escape sequences are:\n'
jpayne@69 10732 '\n'
jpayne@69 10733 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10734 '| Escape Sequence | Meaning | Notes '
jpayne@69 10735 '|\n'
jpayne@69 10736 '|===================|===================================|=========|\n'
jpayne@69 10737 '| "\\newline" | Backslash and newline ignored '
jpayne@69 10738 '| |\n'
jpayne@69 10739 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10740 '| "\\\\" | Backslash ("\\") '
jpayne@69 10741 '| |\n'
jpayne@69 10742 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10743 '| "\\\'" | Single quote ("\'") '
jpayne@69 10744 '| |\n'
jpayne@69 10745 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10746 '| "\\"" | Double quote (""") '
jpayne@69 10747 '| |\n'
jpayne@69 10748 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10749 '| "\\a" | ASCII Bell (BEL) '
jpayne@69 10750 '| |\n'
jpayne@69 10751 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10752 '| "\\b" | ASCII Backspace (BS) '
jpayne@69 10753 '| |\n'
jpayne@69 10754 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10755 '| "\\f" | ASCII Formfeed (FF) '
jpayne@69 10756 '| |\n'
jpayne@69 10757 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10758 '| "\\n" | ASCII Linefeed (LF) '
jpayne@69 10759 '| |\n'
jpayne@69 10760 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10761 '| "\\r" | ASCII Carriage Return (CR) '
jpayne@69 10762 '| |\n'
jpayne@69 10763 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10764 '| "\\t" | ASCII Horizontal Tab (TAB) '
jpayne@69 10765 '| |\n'
jpayne@69 10766 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10767 '| "\\v" | ASCII Vertical Tab (VT) '
jpayne@69 10768 '| |\n'
jpayne@69 10769 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10770 '| "\\ooo" | Character with octal value *ooo* | '
jpayne@69 10771 '(1,3) |\n'
jpayne@69 10772 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10773 '| "\\xhh" | Character with hex value *hh* | '
jpayne@69 10774 '(2,3) |\n'
jpayne@69 10775 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10776 '\n'
jpayne@69 10777 'Escape sequences only recognized in string literals are:\n'
jpayne@69 10778 '\n'
jpayne@69 10779 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10780 '| Escape Sequence | Meaning | Notes '
jpayne@69 10781 '|\n'
jpayne@69 10782 '|===================|===================================|=========|\n'
jpayne@69 10783 '| "\\N{name}" | Character named *name* in the | '
jpayne@69 10784 '(4) |\n'
jpayne@69 10785 '| | Unicode database | '
jpayne@69 10786 '|\n'
jpayne@69 10787 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10788 '| "\\uxxxx" | Character with 16-bit hex value | '
jpayne@69 10789 '(5) |\n'
jpayne@69 10790 '| | *xxxx* | '
jpayne@69 10791 '|\n'
jpayne@69 10792 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10793 '| "\\Uxxxxxxxx" | Character with 32-bit hex value | '
jpayne@69 10794 '(6) |\n'
jpayne@69 10795 '| | *xxxxxxxx* | '
jpayne@69 10796 '|\n'
jpayne@69 10797 '+-------------------+-----------------------------------+---------+\n'
jpayne@69 10798 '\n'
jpayne@69 10799 'Notes:\n'
jpayne@69 10800 '\n'
jpayne@69 10801 '1. As in Standard C, up to three octal digits are accepted.\n'
jpayne@69 10802 '\n'
jpayne@69 10803 '2. Unlike in Standard C, exactly two hex digits are required.\n'
jpayne@69 10804 '\n'
jpayne@69 10805 '3. In a bytes literal, hexadecimal and octal escapes denote the\n'
jpayne@69 10806 ' byte with the given value. In a string literal, these escapes\n'
jpayne@69 10807 ' denote a Unicode character with the given value.\n'
jpayne@69 10808 '\n'
jpayne@69 10809 '4. Changed in version 3.3: Support for name aliases [1] has been\n'
jpayne@69 10810 ' added.\n'
jpayne@69 10811 '\n'
jpayne@69 10812 '5. Exactly four hex digits are required.\n'
jpayne@69 10813 '\n'
jpayne@69 10814 '6. Any Unicode character can be encoded this way. Exactly eight\n'
jpayne@69 10815 ' hex digits are required.\n'
jpayne@69 10816 '\n'
jpayne@69 10817 'Unlike Standard C, all unrecognized escape sequences are left in '
jpayne@69 10818 'the\n'
jpayne@69 10819 'string unchanged, i.e., *the backslash is left in the result*. '
jpayne@69 10820 '(This\n'
jpayne@69 10821 'behavior is useful when debugging: if an escape sequence is '
jpayne@69 10822 'mistyped,\n'
jpayne@69 10823 'the resulting output is more easily recognized as broken.) It is '
jpayne@69 10824 'also\n'
jpayne@69 10825 'important to note that the escape sequences only recognized in '
jpayne@69 10826 'string\n'
jpayne@69 10827 'literals fall into the category of unrecognized escapes for '
jpayne@69 10828 'bytes\n'
jpayne@69 10829 'literals.\n'
jpayne@69 10830 '\n'
jpayne@69 10831 ' Changed in version 3.6: Unrecognized escape sequences produce '
jpayne@69 10832 'a\n'
jpayne@69 10833 ' "DeprecationWarning". In a future Python version they will be '
jpayne@69 10834 'a\n'
jpayne@69 10835 ' "SyntaxWarning" and eventually a "SyntaxError".\n'
jpayne@69 10836 '\n'
jpayne@69 10837 'Even in a raw literal, quotes can be escaped with a backslash, '
jpayne@69 10838 'but the\n'
jpayne@69 10839 'backslash remains in the result; for example, "r"\\""" is a '
jpayne@69 10840 'valid\n'
jpayne@69 10841 'string literal consisting of two characters: a backslash and a '
jpayne@69 10842 'double\n'
jpayne@69 10843 'quote; "r"\\"" is not a valid string literal (even a raw string '
jpayne@69 10844 'cannot\n'
jpayne@69 10845 'end in an odd number of backslashes). Specifically, *a raw '
jpayne@69 10846 'literal\n'
jpayne@69 10847 'cannot end in a single backslash* (since the backslash would '
jpayne@69 10848 'escape\n'
jpayne@69 10849 'the following quote character). Note also that a single '
jpayne@69 10850 'backslash\n'
jpayne@69 10851 'followed by a newline is interpreted as those two characters as '
jpayne@69 10852 'part\n'
jpayne@69 10853 'of the literal, *not* as a line continuation.\n',
jpayne@69 10854 'subscriptions': 'Subscriptions\n'
jpayne@69 10855 '*************\n'
jpayne@69 10856 '\n'
jpayne@69 10857 'A subscription selects an item of a sequence (string, tuple '
jpayne@69 10858 'or list)\n'
jpayne@69 10859 'or mapping (dictionary) object:\n'
jpayne@69 10860 '\n'
jpayne@69 10861 ' subscription ::= primary "[" expression_list "]"\n'
jpayne@69 10862 '\n'
jpayne@69 10863 'The primary must evaluate to an object that supports '
jpayne@69 10864 'subscription\n'
jpayne@69 10865 '(lists or dictionaries for example). User-defined objects '
jpayne@69 10866 'can support\n'
jpayne@69 10867 'subscription by defining a "__getitem__()" method.\n'
jpayne@69 10868 '\n'
jpayne@69 10869 'For built-in objects, there are two types of objects that '
jpayne@69 10870 'support\n'
jpayne@69 10871 'subscription:\n'
jpayne@69 10872 '\n'
jpayne@69 10873 'If the primary is a mapping, the expression list must '
jpayne@69 10874 'evaluate to an\n'
jpayne@69 10875 'object whose value is one of the keys of the mapping, and '
jpayne@69 10876 'the\n'
jpayne@69 10877 'subscription selects the value in the mapping that '
jpayne@69 10878 'corresponds to that\n'
jpayne@69 10879 'key. (The expression list is a tuple except if it has '
jpayne@69 10880 'exactly one\n'
jpayne@69 10881 'item.)\n'
jpayne@69 10882 '\n'
jpayne@69 10883 'If the primary is a sequence, the expression list must '
jpayne@69 10884 'evaluate to an\n'
jpayne@69 10885 'integer or a slice (as discussed in the following '
jpayne@69 10886 'section).\n'
jpayne@69 10887 '\n'
jpayne@69 10888 'The formal syntax makes no special provision for negative '
jpayne@69 10889 'indices in\n'
jpayne@69 10890 'sequences; however, built-in sequences all provide a '
jpayne@69 10891 '"__getitem__()"\n'
jpayne@69 10892 'method that interprets negative indices by adding the '
jpayne@69 10893 'length of the\n'
jpayne@69 10894 'sequence to the index (so that "x[-1]" selects the last '
jpayne@69 10895 'item of "x").\n'
jpayne@69 10896 'The resulting value must be a nonnegative integer less than '
jpayne@69 10897 'the number\n'
jpayne@69 10898 'of items in the sequence, and the subscription selects the '
jpayne@69 10899 'item whose\n'
jpayne@69 10900 'index is that value (counting from zero). Since the support '
jpayne@69 10901 'for\n'
jpayne@69 10902 'negative indices and slicing occurs in the object’s '
jpayne@69 10903 '"__getitem__()"\n'
jpayne@69 10904 'method, subclasses overriding this method will need to '
jpayne@69 10905 'explicitly add\n'
jpayne@69 10906 'that support.\n'
jpayne@69 10907 '\n'
jpayne@69 10908 'A string’s items are characters. A character is not a '
jpayne@69 10909 'separate data\n'
jpayne@69 10910 'type but a string of exactly one character.\n',
jpayne@69 10911 'truth': 'Truth Value Testing\n'
jpayne@69 10912 '*******************\n'
jpayne@69 10913 '\n'
jpayne@69 10914 'Any object can be tested for truth value, for use in an "if" or\n'
jpayne@69 10915 '"while" condition or as operand of the Boolean operations below.\n'
jpayne@69 10916 '\n'
jpayne@69 10917 'By default, an object is considered true unless its class defines\n'
jpayne@69 10918 'either a "__bool__()" method that returns "False" or a "__len__()"\n'
jpayne@69 10919 'method that returns zero, when called with the object. [1] Here '
jpayne@69 10920 'are\n'
jpayne@69 10921 'most of the built-in objects considered false:\n'
jpayne@69 10922 '\n'
jpayne@69 10923 '* constants defined to be false: "None" and "False".\n'
jpayne@69 10924 '\n'
jpayne@69 10925 '* zero of any numeric type: "0", "0.0", "0j", "Decimal(0)",\n'
jpayne@69 10926 ' "Fraction(0, 1)"\n'
jpayne@69 10927 '\n'
jpayne@69 10928 '* empty sequences and collections: "\'\'", "()", "[]", "{}", '
jpayne@69 10929 '"set()",\n'
jpayne@69 10930 ' "range(0)"\n'
jpayne@69 10931 '\n'
jpayne@69 10932 'Operations and built-in functions that have a Boolean result '
jpayne@69 10933 'always\n'
jpayne@69 10934 'return "0" or "False" for false and "1" or "True" for true, unless\n'
jpayne@69 10935 'otherwise stated. (Important exception: the Boolean operations '
jpayne@69 10936 '"or"\n'
jpayne@69 10937 'and "and" always return one of their operands.)\n',
jpayne@69 10938 'try': 'The "try" statement\n'
jpayne@69 10939 '*******************\n'
jpayne@69 10940 '\n'
jpayne@69 10941 'The "try" statement specifies exception handlers and/or cleanup code\n'
jpayne@69 10942 'for a group of statements:\n'
jpayne@69 10943 '\n'
jpayne@69 10944 ' try_stmt ::= try1_stmt | try2_stmt\n'
jpayne@69 10945 ' try1_stmt ::= "try" ":" suite\n'
jpayne@69 10946 ' ("except" [expression ["as" identifier]] ":" '
jpayne@69 10947 'suite)+\n'
jpayne@69 10948 ' ["else" ":" suite]\n'
jpayne@69 10949 ' ["finally" ":" suite]\n'
jpayne@69 10950 ' try2_stmt ::= "try" ":" suite\n'
jpayne@69 10951 ' "finally" ":" suite\n'
jpayne@69 10952 '\n'
jpayne@69 10953 'The "except" clause(s) specify one or more exception handlers. When '
jpayne@69 10954 'no\n'
jpayne@69 10955 'exception occurs in the "try" clause, no exception handler is\n'
jpayne@69 10956 'executed. When an exception occurs in the "try" suite, a search for '
jpayne@69 10957 'an\n'
jpayne@69 10958 'exception handler is started. This search inspects the except '
jpayne@69 10959 'clauses\n'
jpayne@69 10960 'in turn until one is found that matches the exception. An '
jpayne@69 10961 'expression-\n'
jpayne@69 10962 'less except clause, if present, must be last; it matches any\n'
jpayne@69 10963 'exception. For an except clause with an expression, that expression\n'
jpayne@69 10964 'is evaluated, and the clause matches the exception if the resulting\n'
jpayne@69 10965 'object is “compatible” with the exception. An object is compatible\n'
jpayne@69 10966 'with an exception if it is the class or a base class of the '
jpayne@69 10967 'exception\n'
jpayne@69 10968 'object or a tuple containing an item compatible with the exception.\n'
jpayne@69 10969 '\n'
jpayne@69 10970 'If no except clause matches the exception, the search for an '
jpayne@69 10971 'exception\n'
jpayne@69 10972 'handler continues in the surrounding code and on the invocation '
jpayne@69 10973 'stack.\n'
jpayne@69 10974 '[1]\n'
jpayne@69 10975 '\n'
jpayne@69 10976 'If the evaluation of an expression in the header of an except clause\n'
jpayne@69 10977 'raises an exception, the original search for a handler is canceled '
jpayne@69 10978 'and\n'
jpayne@69 10979 'a search starts for the new exception in the surrounding code and on\n'
jpayne@69 10980 'the call stack (it is treated as if the entire "try" statement '
jpayne@69 10981 'raised\n'
jpayne@69 10982 'the exception).\n'
jpayne@69 10983 '\n'
jpayne@69 10984 'When a matching except clause is found, the exception is assigned to\n'
jpayne@69 10985 'the target specified after the "as" keyword in that except clause, '
jpayne@69 10986 'if\n'
jpayne@69 10987 'present, and the except clause’s suite is executed. All except\n'
jpayne@69 10988 'clauses must have an executable block. When the end of this block '
jpayne@69 10989 'is\n'
jpayne@69 10990 'reached, execution continues normally after the entire try '
jpayne@69 10991 'statement.\n'
jpayne@69 10992 '(This means that if two nested handlers exist for the same '
jpayne@69 10993 'exception,\n'
jpayne@69 10994 'and the exception occurs in the try clause of the inner handler, the\n'
jpayne@69 10995 'outer handler will not handle the exception.)\n'
jpayne@69 10996 '\n'
jpayne@69 10997 'When an exception has been assigned using "as target", it is cleared\n'
jpayne@69 10998 'at the end of the except clause. This is as if\n'
jpayne@69 10999 '\n'
jpayne@69 11000 ' except E as N:\n'
jpayne@69 11001 ' foo\n'
jpayne@69 11002 '\n'
jpayne@69 11003 'was translated to\n'
jpayne@69 11004 '\n'
jpayne@69 11005 ' except E as N:\n'
jpayne@69 11006 ' try:\n'
jpayne@69 11007 ' foo\n'
jpayne@69 11008 ' finally:\n'
jpayne@69 11009 ' del N\n'
jpayne@69 11010 '\n'
jpayne@69 11011 'This means the exception must be assigned to a different name to be\n'
jpayne@69 11012 'able to refer to it after the except clause. Exceptions are cleared\n'
jpayne@69 11013 'because with the traceback attached to them, they form a reference\n'
jpayne@69 11014 'cycle with the stack frame, keeping all locals in that frame alive\n'
jpayne@69 11015 'until the next garbage collection occurs.\n'
jpayne@69 11016 '\n'
jpayne@69 11017 'Before an except clause’s suite is executed, details about the\n'
jpayne@69 11018 'exception are stored in the "sys" module and can be accessed via\n'
jpayne@69 11019 '"sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting of '
jpayne@69 11020 'the\n'
jpayne@69 11021 'exception class, the exception instance and a traceback object (see\n'
jpayne@69 11022 'section The standard type hierarchy) identifying the point in the\n'
jpayne@69 11023 'program where the exception occurred. "sys.exc_info()" values are\n'
jpayne@69 11024 'restored to their previous values (before the call) when returning\n'
jpayne@69 11025 'from a function that handled an exception.\n'
jpayne@69 11026 '\n'
jpayne@69 11027 'The optional "else" clause is executed if the control flow leaves '
jpayne@69 11028 'the\n'
jpayne@69 11029 '"try" suite, no exception was raised, and no "return", "continue", '
jpayne@69 11030 'or\n'
jpayne@69 11031 '"break" statement was executed. Exceptions in the "else" clause are\n'
jpayne@69 11032 'not handled by the preceding "except" clauses.\n'
jpayne@69 11033 '\n'
jpayne@69 11034 'If "finally" is present, it specifies a ‘cleanup’ handler. The '
jpayne@69 11035 '"try"\n'
jpayne@69 11036 'clause is executed, including any "except" and "else" clauses. If '
jpayne@69 11037 'an\n'
jpayne@69 11038 'exception occurs in any of the clauses and is not handled, the\n'
jpayne@69 11039 'exception is temporarily saved. The "finally" clause is executed. '
jpayne@69 11040 'If\n'
jpayne@69 11041 'there is a saved exception it is re-raised at the end of the '
jpayne@69 11042 '"finally"\n'
jpayne@69 11043 'clause. If the "finally" clause raises another exception, the saved\n'
jpayne@69 11044 'exception is set as the context of the new exception. If the '
jpayne@69 11045 '"finally"\n'
jpayne@69 11046 'clause executes a "return", "break" or "continue" statement, the '
jpayne@69 11047 'saved\n'
jpayne@69 11048 'exception is discarded:\n'
jpayne@69 11049 '\n'
jpayne@69 11050 ' >>> def f():\n'
jpayne@69 11051 ' ... try:\n'
jpayne@69 11052 ' ... 1/0\n'
jpayne@69 11053 ' ... finally:\n'
jpayne@69 11054 ' ... return 42\n'
jpayne@69 11055 ' ...\n'
jpayne@69 11056 ' >>> f()\n'
jpayne@69 11057 ' 42\n'
jpayne@69 11058 '\n'
jpayne@69 11059 'The exception information is not available to the program during\n'
jpayne@69 11060 'execution of the "finally" clause.\n'
jpayne@69 11061 '\n'
jpayne@69 11062 'When a "return", "break" or "continue" statement is executed in the\n'
jpayne@69 11063 '"try" suite of a "try"…"finally" statement, the "finally" clause is\n'
jpayne@69 11064 'also executed ‘on the way out.’\n'
jpayne@69 11065 '\n'
jpayne@69 11066 'The return value of a function is determined by the last "return"\n'
jpayne@69 11067 'statement executed. Since the "finally" clause always executes, a\n'
jpayne@69 11068 '"return" statement executed in the "finally" clause will always be '
jpayne@69 11069 'the\n'
jpayne@69 11070 'last one executed:\n'
jpayne@69 11071 '\n'
jpayne@69 11072 ' >>> def foo():\n'
jpayne@69 11073 ' ... try:\n'
jpayne@69 11074 " ... return 'try'\n"
jpayne@69 11075 ' ... finally:\n'
jpayne@69 11076 " ... return 'finally'\n"
jpayne@69 11077 ' ...\n'
jpayne@69 11078 ' >>> foo()\n'
jpayne@69 11079 " 'finally'\n"
jpayne@69 11080 '\n'
jpayne@69 11081 'Additional information on exceptions can be found in section\n'
jpayne@69 11082 'Exceptions, and information on using the "raise" statement to '
jpayne@69 11083 'generate\n'
jpayne@69 11084 'exceptions may be found in section The raise statement.\n'
jpayne@69 11085 '\n'
jpayne@69 11086 'Changed in version 3.8: Prior to Python 3.8, a "continue" statement\n'
jpayne@69 11087 'was illegal in the "finally" clause due to a problem with the\n'
jpayne@69 11088 'implementation.\n',
jpayne@69 11089 'types': 'The standard type hierarchy\n'
jpayne@69 11090 '***************************\n'
jpayne@69 11091 '\n'
jpayne@69 11092 'Below is a list of the types that are built into Python. '
jpayne@69 11093 'Extension\n'
jpayne@69 11094 'modules (written in C, Java, or other languages, depending on the\n'
jpayne@69 11095 'implementation) can define additional types. Future versions of\n'
jpayne@69 11096 'Python may add types to the type hierarchy (e.g., rational '
jpayne@69 11097 'numbers,\n'
jpayne@69 11098 'efficiently stored arrays of integers, etc.), although such '
jpayne@69 11099 'additions\n'
jpayne@69 11100 'will often be provided via the standard library instead.\n'
jpayne@69 11101 '\n'
jpayne@69 11102 'Some of the type descriptions below contain a paragraph listing\n'
jpayne@69 11103 '‘special attributes.’ These are attributes that provide access to '
jpayne@69 11104 'the\n'
jpayne@69 11105 'implementation and are not intended for general use. Their '
jpayne@69 11106 'definition\n'
jpayne@69 11107 'may change in the future.\n'
jpayne@69 11108 '\n'
jpayne@69 11109 'None\n'
jpayne@69 11110 ' This type has a single value. There is a single object with '
jpayne@69 11111 'this\n'
jpayne@69 11112 ' value. This object is accessed through the built-in name "None". '
jpayne@69 11113 'It\n'
jpayne@69 11114 ' is used to signify the absence of a value in many situations, '
jpayne@69 11115 'e.g.,\n'
jpayne@69 11116 ' it is returned from functions that don’t explicitly return\n'
jpayne@69 11117 ' anything. Its truth value is false.\n'
jpayne@69 11118 '\n'
jpayne@69 11119 'NotImplemented\n'
jpayne@69 11120 ' This type has a single value. There is a single object with '
jpayne@69 11121 'this\n'
jpayne@69 11122 ' value. This object is accessed through the built-in name\n'
jpayne@69 11123 ' "NotImplemented". Numeric methods and rich comparison methods\n'
jpayne@69 11124 ' should return this value if they do not implement the operation '
jpayne@69 11125 'for\n'
jpayne@69 11126 ' the operands provided. (The interpreter will then try the\n'
jpayne@69 11127 ' reflected operation, or some other fallback, depending on the\n'
jpayne@69 11128 ' operator.) Its truth value is true.\n'
jpayne@69 11129 '\n'
jpayne@69 11130 ' See Implementing the arithmetic operations for more details.\n'
jpayne@69 11131 '\n'
jpayne@69 11132 'Ellipsis\n'
jpayne@69 11133 ' This type has a single value. There is a single object with '
jpayne@69 11134 'this\n'
jpayne@69 11135 ' value. This object is accessed through the literal "..." or the\n'
jpayne@69 11136 ' built-in name "Ellipsis". Its truth value is true.\n'
jpayne@69 11137 '\n'
jpayne@69 11138 '"numbers.Number"\n'
jpayne@69 11139 ' These are created by numeric literals and returned as results '
jpayne@69 11140 'by\n'
jpayne@69 11141 ' arithmetic operators and arithmetic built-in functions. '
jpayne@69 11142 'Numeric\n'
jpayne@69 11143 ' objects are immutable; once created their value never changes.\n'
jpayne@69 11144 ' Python numbers are of course strongly related to mathematical\n'
jpayne@69 11145 ' numbers, but subject to the limitations of numerical '
jpayne@69 11146 'representation\n'
jpayne@69 11147 ' in computers.\n'
jpayne@69 11148 '\n'
jpayne@69 11149 ' Python distinguishes between integers, floating point numbers, '
jpayne@69 11150 'and\n'
jpayne@69 11151 ' complex numbers:\n'
jpayne@69 11152 '\n'
jpayne@69 11153 ' "numbers.Integral"\n'
jpayne@69 11154 ' These represent elements from the mathematical set of '
jpayne@69 11155 'integers\n'
jpayne@69 11156 ' (positive and negative).\n'
jpayne@69 11157 '\n'
jpayne@69 11158 ' There are two types of integers:\n'
jpayne@69 11159 '\n'
jpayne@69 11160 ' Integers ("int")\n'
jpayne@69 11161 '\n'
jpayne@69 11162 ' These represent numbers in an unlimited range, subject to\n'
jpayne@69 11163 ' available (virtual) memory only. For the purpose of '
jpayne@69 11164 'shift\n'
jpayne@69 11165 ' and mask operations, a binary representation is assumed, '
jpayne@69 11166 'and\n'
jpayne@69 11167 ' negative numbers are represented in a variant of 2’s\n'
jpayne@69 11168 ' complement which gives the illusion of an infinite string '
jpayne@69 11169 'of\n'
jpayne@69 11170 ' sign bits extending to the left.\n'
jpayne@69 11171 '\n'
jpayne@69 11172 ' Booleans ("bool")\n'
jpayne@69 11173 ' These represent the truth values False and True. The two\n'
jpayne@69 11174 ' objects representing the values "False" and "True" are '
jpayne@69 11175 'the\n'
jpayne@69 11176 ' only Boolean objects. The Boolean type is a subtype of '
jpayne@69 11177 'the\n'
jpayne@69 11178 ' integer type, and Boolean values behave like the values 0 '
jpayne@69 11179 'and\n'
jpayne@69 11180 ' 1, respectively, in almost all contexts, the exception '
jpayne@69 11181 'being\n'
jpayne@69 11182 ' that when converted to a string, the strings ""False"" or\n'
jpayne@69 11183 ' ""True"" are returned, respectively.\n'
jpayne@69 11184 '\n'
jpayne@69 11185 ' The rules for integer representation are intended to give '
jpayne@69 11186 'the\n'
jpayne@69 11187 ' most meaningful interpretation of shift and mask operations\n'
jpayne@69 11188 ' involving negative integers.\n'
jpayne@69 11189 '\n'
jpayne@69 11190 ' "numbers.Real" ("float")\n'
jpayne@69 11191 ' These represent machine-level double precision floating '
jpayne@69 11192 'point\n'
jpayne@69 11193 ' numbers. You are at the mercy of the underlying machine\n'
jpayne@69 11194 ' architecture (and C or Java implementation) for the accepted\n'
jpayne@69 11195 ' range and handling of overflow. Python does not support '
jpayne@69 11196 'single-\n'
jpayne@69 11197 ' precision floating point numbers; the savings in processor '
jpayne@69 11198 'and\n'
jpayne@69 11199 ' memory usage that are usually the reason for using these are\n'
jpayne@69 11200 ' dwarfed by the overhead of using objects in Python, so there '
jpayne@69 11201 'is\n'
jpayne@69 11202 ' no reason to complicate the language with two kinds of '
jpayne@69 11203 'floating\n'
jpayne@69 11204 ' point numbers.\n'
jpayne@69 11205 '\n'
jpayne@69 11206 ' "numbers.Complex" ("complex")\n'
jpayne@69 11207 ' These represent complex numbers as a pair of machine-level\n'
jpayne@69 11208 ' double precision floating point numbers. The same caveats '
jpayne@69 11209 'apply\n'
jpayne@69 11210 ' as for floating point numbers. The real and imaginary parts '
jpayne@69 11211 'of a\n'
jpayne@69 11212 ' complex number "z" can be retrieved through the read-only\n'
jpayne@69 11213 ' attributes "z.real" and "z.imag".\n'
jpayne@69 11214 '\n'
jpayne@69 11215 'Sequences\n'
jpayne@69 11216 ' These represent finite ordered sets indexed by non-negative\n'
jpayne@69 11217 ' numbers. The built-in function "len()" returns the number of '
jpayne@69 11218 'items\n'
jpayne@69 11219 ' of a sequence. When the length of a sequence is *n*, the index '
jpayne@69 11220 'set\n'
jpayne@69 11221 ' contains the numbers 0, 1, …, *n*-1. Item *i* of sequence *a* '
jpayne@69 11222 'is\n'
jpayne@69 11223 ' selected by "a[i]".\n'
jpayne@69 11224 '\n'
jpayne@69 11225 ' Sequences also support slicing: "a[i:j]" selects all items with\n'
jpayne@69 11226 ' index *k* such that *i* "<=" *k* "<" *j*. When used as an\n'
jpayne@69 11227 ' expression, a slice is a sequence of the same type. This '
jpayne@69 11228 'implies\n'
jpayne@69 11229 ' that the index set is renumbered so that it starts at 0.\n'
jpayne@69 11230 '\n'
jpayne@69 11231 ' Some sequences also support “extended slicing” with a third '
jpayne@69 11232 '“step”\n'
jpayne@69 11233 ' parameter: "a[i:j:k]" selects all items of *a* with index *x* '
jpayne@69 11234 'where\n'
jpayne@69 11235 ' "x = i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*.\n'
jpayne@69 11236 '\n'
jpayne@69 11237 ' Sequences are distinguished according to their mutability:\n'
jpayne@69 11238 '\n'
jpayne@69 11239 ' Immutable sequences\n'
jpayne@69 11240 ' An object of an immutable sequence type cannot change once it '
jpayne@69 11241 'is\n'
jpayne@69 11242 ' created. (If the object contains references to other '
jpayne@69 11243 'objects,\n'
jpayne@69 11244 ' these other objects may be mutable and may be changed; '
jpayne@69 11245 'however,\n'
jpayne@69 11246 ' the collection of objects directly referenced by an '
jpayne@69 11247 'immutable\n'
jpayne@69 11248 ' object cannot change.)\n'
jpayne@69 11249 '\n'
jpayne@69 11250 ' The following types are immutable sequences:\n'
jpayne@69 11251 '\n'
jpayne@69 11252 ' Strings\n'
jpayne@69 11253 ' A string is a sequence of values that represent Unicode '
jpayne@69 11254 'code\n'
jpayne@69 11255 ' points. All the code points in the range "U+0000 - '
jpayne@69 11256 'U+10FFFF"\n'
jpayne@69 11257 ' can be represented in a string. Python doesn’t have a '
jpayne@69 11258 '"char"\n'
jpayne@69 11259 ' type; instead, every code point in the string is '
jpayne@69 11260 'represented\n'
jpayne@69 11261 ' as a string object with length "1". The built-in '
jpayne@69 11262 'function\n'
jpayne@69 11263 ' "ord()" converts a code point from its string form to an\n'
jpayne@69 11264 ' integer in the range "0 - 10FFFF"; "chr()" converts an\n'
jpayne@69 11265 ' integer in the range "0 - 10FFFF" to the corresponding '
jpayne@69 11266 'length\n'
jpayne@69 11267 ' "1" string object. "str.encode()" can be used to convert '
jpayne@69 11268 'a\n'
jpayne@69 11269 ' "str" to "bytes" using the given text encoding, and\n'
jpayne@69 11270 ' "bytes.decode()" can be used to achieve the opposite.\n'
jpayne@69 11271 '\n'
jpayne@69 11272 ' Tuples\n'
jpayne@69 11273 ' The items of a tuple are arbitrary Python objects. Tuples '
jpayne@69 11274 'of\n'
jpayne@69 11275 ' two or more items are formed by comma-separated lists of\n'
jpayne@69 11276 ' expressions. A tuple of one item (a ‘singleton’) can be\n'
jpayne@69 11277 ' formed by affixing a comma to an expression (an expression '
jpayne@69 11278 'by\n'
jpayne@69 11279 ' itself does not create a tuple, since parentheses must be\n'
jpayne@69 11280 ' usable for grouping of expressions). An empty tuple can '
jpayne@69 11281 'be\n'
jpayne@69 11282 ' formed by an empty pair of parentheses.\n'
jpayne@69 11283 '\n'
jpayne@69 11284 ' Bytes\n'
jpayne@69 11285 ' A bytes object is an immutable array. The items are '
jpayne@69 11286 '8-bit\n'
jpayne@69 11287 ' bytes, represented by integers in the range 0 <= x < 256.\n'
jpayne@69 11288 ' Bytes literals (like "b\'abc\'") and the built-in '
jpayne@69 11289 '"bytes()"\n'
jpayne@69 11290 ' constructor can be used to create bytes objects. Also, '
jpayne@69 11291 'bytes\n'
jpayne@69 11292 ' objects can be decoded to strings via the "decode()" '
jpayne@69 11293 'method.\n'
jpayne@69 11294 '\n'
jpayne@69 11295 ' Mutable sequences\n'
jpayne@69 11296 ' Mutable sequences can be changed after they are created. '
jpayne@69 11297 'The\n'
jpayne@69 11298 ' subscription and slicing notations can be used as the target '
jpayne@69 11299 'of\n'
jpayne@69 11300 ' assignment and "del" (delete) statements.\n'
jpayne@69 11301 '\n'
jpayne@69 11302 ' There are currently two intrinsic mutable sequence types:\n'
jpayne@69 11303 '\n'
jpayne@69 11304 ' Lists\n'
jpayne@69 11305 ' The items of a list are arbitrary Python objects. Lists '
jpayne@69 11306 'are\n'
jpayne@69 11307 ' formed by placing a comma-separated list of expressions '
jpayne@69 11308 'in\n'
jpayne@69 11309 ' square brackets. (Note that there are no special cases '
jpayne@69 11310 'needed\n'
jpayne@69 11311 ' to form lists of length 0 or 1.)\n'
jpayne@69 11312 '\n'
jpayne@69 11313 ' Byte Arrays\n'
jpayne@69 11314 ' A bytearray object is a mutable array. They are created '
jpayne@69 11315 'by\n'
jpayne@69 11316 ' the built-in "bytearray()" constructor. Aside from being\n'
jpayne@69 11317 ' mutable (and hence unhashable), byte arrays otherwise '
jpayne@69 11318 'provide\n'
jpayne@69 11319 ' the same interface and functionality as immutable "bytes"\n'
jpayne@69 11320 ' objects.\n'
jpayne@69 11321 '\n'
jpayne@69 11322 ' The extension module "array" provides an additional example '
jpayne@69 11323 'of a\n'
jpayne@69 11324 ' mutable sequence type, as does the "collections" module.\n'
jpayne@69 11325 '\n'
jpayne@69 11326 'Set types\n'
jpayne@69 11327 ' These represent unordered, finite sets of unique, immutable\n'
jpayne@69 11328 ' objects. As such, they cannot be indexed by any subscript. '
jpayne@69 11329 'However,\n'
jpayne@69 11330 ' they can be iterated over, and the built-in function "len()"\n'
jpayne@69 11331 ' returns the number of items in a set. Common uses for sets are '
jpayne@69 11332 'fast\n'
jpayne@69 11333 ' membership testing, removing duplicates from a sequence, and\n'
jpayne@69 11334 ' computing mathematical operations such as intersection, union,\n'
jpayne@69 11335 ' difference, and symmetric difference.\n'
jpayne@69 11336 '\n'
jpayne@69 11337 ' For set elements, the same immutability rules apply as for\n'
jpayne@69 11338 ' dictionary keys. Note that numeric types obey the normal rules '
jpayne@69 11339 'for\n'
jpayne@69 11340 ' numeric comparison: if two numbers compare equal (e.g., "1" and\n'
jpayne@69 11341 ' "1.0"), only one of them can be contained in a set.\n'
jpayne@69 11342 '\n'
jpayne@69 11343 ' There are currently two intrinsic set types:\n'
jpayne@69 11344 '\n'
jpayne@69 11345 ' Sets\n'
jpayne@69 11346 ' These represent a mutable set. They are created by the '
jpayne@69 11347 'built-in\n'
jpayne@69 11348 ' "set()" constructor and can be modified afterwards by '
jpayne@69 11349 'several\n'
jpayne@69 11350 ' methods, such as "add()".\n'
jpayne@69 11351 '\n'
jpayne@69 11352 ' Frozen sets\n'
jpayne@69 11353 ' These represent an immutable set. They are created by the\n'
jpayne@69 11354 ' built-in "frozenset()" constructor. As a frozenset is '
jpayne@69 11355 'immutable\n'
jpayne@69 11356 ' and *hashable*, it can be used again as an element of '
jpayne@69 11357 'another\n'
jpayne@69 11358 ' set, or as a dictionary key.\n'
jpayne@69 11359 '\n'
jpayne@69 11360 'Mappings\n'
jpayne@69 11361 ' These represent finite sets of objects indexed by arbitrary '
jpayne@69 11362 'index\n'
jpayne@69 11363 ' sets. The subscript notation "a[k]" selects the item indexed by '
jpayne@69 11364 '"k"\n'
jpayne@69 11365 ' from the mapping "a"; this can be used in expressions and as '
jpayne@69 11366 'the\n'
jpayne@69 11367 ' target of assignments or "del" statements. The built-in '
jpayne@69 11368 'function\n'
jpayne@69 11369 ' "len()" returns the number of items in a mapping.\n'
jpayne@69 11370 '\n'
jpayne@69 11371 ' There is currently a single intrinsic mapping type:\n'
jpayne@69 11372 '\n'
jpayne@69 11373 ' Dictionaries\n'
jpayne@69 11374 ' These represent finite sets of objects indexed by nearly\n'
jpayne@69 11375 ' arbitrary values. The only types of values not acceptable '
jpayne@69 11376 'as\n'
jpayne@69 11377 ' keys are values containing lists or dictionaries or other\n'
jpayne@69 11378 ' mutable types that are compared by value rather than by '
jpayne@69 11379 'object\n'
jpayne@69 11380 ' identity, the reason being that the efficient implementation '
jpayne@69 11381 'of\n'
jpayne@69 11382 ' dictionaries requires a key’s hash value to remain constant.\n'
jpayne@69 11383 ' Numeric types used for keys obey the normal rules for '
jpayne@69 11384 'numeric\n'
jpayne@69 11385 ' comparison: if two numbers compare equal (e.g., "1" and '
jpayne@69 11386 '"1.0")\n'
jpayne@69 11387 ' then they can be used interchangeably to index the same\n'
jpayne@69 11388 ' dictionary entry.\n'
jpayne@69 11389 '\n'
jpayne@69 11390 ' Dictionaries are mutable; they can be created by the "{...}"\n'
jpayne@69 11391 ' notation (see section Dictionary displays).\n'
jpayne@69 11392 '\n'
jpayne@69 11393 ' The extension modules "dbm.ndbm" and "dbm.gnu" provide\n'
jpayne@69 11394 ' additional examples of mapping types, as does the '
jpayne@69 11395 '"collections"\n'
jpayne@69 11396 ' module.\n'
jpayne@69 11397 '\n'
jpayne@69 11398 'Callable types\n'
jpayne@69 11399 ' These are the types to which the function call operation (see\n'
jpayne@69 11400 ' section Calls) can be applied:\n'
jpayne@69 11401 '\n'
jpayne@69 11402 ' User-defined functions\n'
jpayne@69 11403 ' A user-defined function object is created by a function\n'
jpayne@69 11404 ' definition (see section Function definitions). It should be\n'
jpayne@69 11405 ' called with an argument list containing the same number of '
jpayne@69 11406 'items\n'
jpayne@69 11407 ' as the function’s formal parameter list.\n'
jpayne@69 11408 '\n'
jpayne@69 11409 ' Special attributes:\n'
jpayne@69 11410 '\n'
jpayne@69 11411 ' '
jpayne@69 11412 '+---------------------------+---------------------------------+-------------+\n'
jpayne@69 11413 ' | Attribute | Meaning '
jpayne@69 11414 '| |\n'
jpayne@69 11415 ' '
jpayne@69 11416 '|===========================|=================================|=============|\n'
jpayne@69 11417 ' | "__doc__" | The function’s documentation '
jpayne@69 11418 '| Writable |\n'
jpayne@69 11419 ' | | string, or "None" if '
jpayne@69 11420 '| |\n'
jpayne@69 11421 ' | | unavailable; not inherited by '
jpayne@69 11422 '| |\n'
jpayne@69 11423 ' | | subclasses. '
jpayne@69 11424 '| |\n'
jpayne@69 11425 ' '
jpayne@69 11426 '+---------------------------+---------------------------------+-------------+\n'
jpayne@69 11427 ' | "__name__" | The function’s name. '
jpayne@69 11428 '| Writable |\n'
jpayne@69 11429 ' '
jpayne@69 11430 '+---------------------------+---------------------------------+-------------+\n'
jpayne@69 11431 ' | "__qualname__" | The function’s *qualified '
jpayne@69 11432 '| Writable |\n'
jpayne@69 11433 ' | | name*. New in version 3.3. '
jpayne@69 11434 '| |\n'
jpayne@69 11435 ' '
jpayne@69 11436 '+---------------------------+---------------------------------+-------------+\n'
jpayne@69 11437 ' | "__module__" | The name of the module the '
jpayne@69 11438 '| Writable |\n'
jpayne@69 11439 ' | | function was defined in, or '
jpayne@69 11440 '| |\n'
jpayne@69 11441 ' | | "None" if unavailable. '
jpayne@69 11442 '| |\n'
jpayne@69 11443 ' '
jpayne@69 11444 '+---------------------------+---------------------------------+-------------+\n'
jpayne@69 11445 ' | "__defaults__" | A tuple containing default '
jpayne@69 11446 '| Writable |\n'
jpayne@69 11447 ' | | argument values for those '
jpayne@69 11448 '| |\n'
jpayne@69 11449 ' | | arguments that have defaults, '
jpayne@69 11450 '| |\n'
jpayne@69 11451 ' | | or "None" if no arguments have '
jpayne@69 11452 '| |\n'
jpayne@69 11453 ' | | a default value. '
jpayne@69 11454 '| |\n'
jpayne@69 11455 ' '
jpayne@69 11456 '+---------------------------+---------------------------------+-------------+\n'
jpayne@69 11457 ' | "__code__" | The code object representing '
jpayne@69 11458 '| Writable |\n'
jpayne@69 11459 ' | | the compiled function body. '
jpayne@69 11460 '| |\n'
jpayne@69 11461 ' '
jpayne@69 11462 '+---------------------------+---------------------------------+-------------+\n'
jpayne@69 11463 ' | "__globals__" | A reference to the dictionary '
jpayne@69 11464 '| Read-only |\n'
jpayne@69 11465 ' | | that holds the function’s '
jpayne@69 11466 '| |\n'
jpayne@69 11467 ' | | global variables — the global '
jpayne@69 11468 '| |\n'
jpayne@69 11469 ' | | namespace of the module in '
jpayne@69 11470 '| |\n'
jpayne@69 11471 ' | | which the function was defined. '
jpayne@69 11472 '| |\n'
jpayne@69 11473 ' '
jpayne@69 11474 '+---------------------------+---------------------------------+-------------+\n'
jpayne@69 11475 ' | "__dict__" | The namespace supporting '
jpayne@69 11476 '| Writable |\n'
jpayne@69 11477 ' | | arbitrary function attributes. '
jpayne@69 11478 '| |\n'
jpayne@69 11479 ' '
jpayne@69 11480 '+---------------------------+---------------------------------+-------------+\n'
jpayne@69 11481 ' | "__closure__" | "None" or a tuple of cells that '
jpayne@69 11482 '| Read-only |\n'
jpayne@69 11483 ' | | contain bindings for the '
jpayne@69 11484 '| |\n'
jpayne@69 11485 ' | | function’s free variables. See '
jpayne@69 11486 '| |\n'
jpayne@69 11487 ' | | below for information on the '
jpayne@69 11488 '| |\n'
jpayne@69 11489 ' | | "cell_contents" attribute. '
jpayne@69 11490 '| |\n'
jpayne@69 11491 ' '
jpayne@69 11492 '+---------------------------+---------------------------------+-------------+\n'
jpayne@69 11493 ' | "__annotations__" | A dict containing annotations '
jpayne@69 11494 '| Writable |\n'
jpayne@69 11495 ' | | of parameters. The keys of the '
jpayne@69 11496 '| |\n'
jpayne@69 11497 ' | | dict are the parameter names, '
jpayne@69 11498 '| |\n'
jpayne@69 11499 ' | | and "\'return\'" for the '
jpayne@69 11500 'return | |\n'
jpayne@69 11501 ' | | annotation, if provided. '
jpayne@69 11502 '| |\n'
jpayne@69 11503 ' '
jpayne@69 11504 '+---------------------------+---------------------------------+-------------+\n'
jpayne@69 11505 ' | "__kwdefaults__" | A dict containing defaults for '
jpayne@69 11506 '| Writable |\n'
jpayne@69 11507 ' | | keyword-only parameters. '
jpayne@69 11508 '| |\n'
jpayne@69 11509 ' '
jpayne@69 11510 '+---------------------------+---------------------------------+-------------+\n'
jpayne@69 11511 '\n'
jpayne@69 11512 ' Most of the attributes labelled “Writable” check the type of '
jpayne@69 11513 'the\n'
jpayne@69 11514 ' assigned value.\n'
jpayne@69 11515 '\n'
jpayne@69 11516 ' Function objects also support getting and setting arbitrary\n'
jpayne@69 11517 ' attributes, which can be used, for example, to attach '
jpayne@69 11518 'metadata\n'
jpayne@69 11519 ' to functions. Regular attribute dot-notation is used to get '
jpayne@69 11520 'and\n'
jpayne@69 11521 ' set such attributes. *Note that the current implementation '
jpayne@69 11522 'only\n'
jpayne@69 11523 ' supports function attributes on user-defined functions. '
jpayne@69 11524 'Function\n'
jpayne@69 11525 ' attributes on built-in functions may be supported in the\n'
jpayne@69 11526 ' future.*\n'
jpayne@69 11527 '\n'
jpayne@69 11528 ' A cell object has the attribute "cell_contents". This can be\n'
jpayne@69 11529 ' used to get the value of the cell, as well as set the value.\n'
jpayne@69 11530 '\n'
jpayne@69 11531 ' Additional information about a function’s definition can be\n'
jpayne@69 11532 ' retrieved from its code object; see the description of '
jpayne@69 11533 'internal\n'
jpayne@69 11534 ' types below. The "cell" type can be accessed in the "types"\n'
jpayne@69 11535 ' module.\n'
jpayne@69 11536 '\n'
jpayne@69 11537 ' Instance methods\n'
jpayne@69 11538 ' An instance method object combines a class, a class instance '
jpayne@69 11539 'and\n'
jpayne@69 11540 ' any callable object (normally a user-defined function).\n'
jpayne@69 11541 '\n'
jpayne@69 11542 ' Special read-only attributes: "__self__" is the class '
jpayne@69 11543 'instance\n'
jpayne@69 11544 ' object, "__func__" is the function object; "__doc__" is the\n'
jpayne@69 11545 ' method’s documentation (same as "__func__.__doc__"); '
jpayne@69 11546 '"__name__"\n'
jpayne@69 11547 ' is the method name (same as "__func__.__name__"); '
jpayne@69 11548 '"__module__"\n'
jpayne@69 11549 ' is the name of the module the method was defined in, or '
jpayne@69 11550 '"None"\n'
jpayne@69 11551 ' if unavailable.\n'
jpayne@69 11552 '\n'
jpayne@69 11553 ' Methods also support accessing (but not setting) the '
jpayne@69 11554 'arbitrary\n'
jpayne@69 11555 ' function attributes on the underlying function object.\n'
jpayne@69 11556 '\n'
jpayne@69 11557 ' User-defined method objects may be created when getting an\n'
jpayne@69 11558 ' attribute of a class (perhaps via an instance of that class), '
jpayne@69 11559 'if\n'
jpayne@69 11560 ' that attribute is a user-defined function object or a class\n'
jpayne@69 11561 ' method object.\n'
jpayne@69 11562 '\n'
jpayne@69 11563 ' When an instance method object is created by retrieving a '
jpayne@69 11564 'user-\n'
jpayne@69 11565 ' defined function object from a class via one of its '
jpayne@69 11566 'instances,\n'
jpayne@69 11567 ' its "__self__" attribute is the instance, and the method '
jpayne@69 11568 'object\n'
jpayne@69 11569 ' is said to be bound. The new method’s "__func__" attribute '
jpayne@69 11570 'is\n'
jpayne@69 11571 ' the original function object.\n'
jpayne@69 11572 '\n'
jpayne@69 11573 ' When an instance method object is created by retrieving a '
jpayne@69 11574 'class\n'
jpayne@69 11575 ' method object from a class or instance, its "__self__" '
jpayne@69 11576 'attribute\n'
jpayne@69 11577 ' is the class itself, and its "__func__" attribute is the\n'
jpayne@69 11578 ' function object underlying the class method.\n'
jpayne@69 11579 '\n'
jpayne@69 11580 ' When an instance method object is called, the underlying\n'
jpayne@69 11581 ' function ("__func__") is called, inserting the class '
jpayne@69 11582 'instance\n'
jpayne@69 11583 ' ("__self__") in front of the argument list. For instance, '
jpayne@69 11584 'when\n'
jpayne@69 11585 ' "C" is a class which contains a definition for a function '
jpayne@69 11586 '"f()",\n'
jpayne@69 11587 ' and "x" is an instance of "C", calling "x.f(1)" is equivalent '
jpayne@69 11588 'to\n'
jpayne@69 11589 ' calling "C.f(x, 1)".\n'
jpayne@69 11590 '\n'
jpayne@69 11591 ' When an instance method object is derived from a class '
jpayne@69 11592 'method\n'
jpayne@69 11593 ' object, the “class instance” stored in "__self__" will '
jpayne@69 11594 'actually\n'
jpayne@69 11595 ' be the class itself, so that calling either "x.f(1)" or '
jpayne@69 11596 '"C.f(1)"\n'
jpayne@69 11597 ' is equivalent to calling "f(C,1)" where "f" is the '
jpayne@69 11598 'underlying\n'
jpayne@69 11599 ' function.\n'
jpayne@69 11600 '\n'
jpayne@69 11601 ' Note that the transformation from function object to '
jpayne@69 11602 'instance\n'
jpayne@69 11603 ' method object happens each time the attribute is retrieved '
jpayne@69 11604 'from\n'
jpayne@69 11605 ' the instance. In some cases, a fruitful optimization is to\n'
jpayne@69 11606 ' assign the attribute to a local variable and call that local\n'
jpayne@69 11607 ' variable. Also notice that this transformation only happens '
jpayne@69 11608 'for\n'
jpayne@69 11609 ' user-defined functions; other callable objects (and all non-\n'
jpayne@69 11610 ' callable objects) are retrieved without transformation. It '
jpayne@69 11611 'is\n'
jpayne@69 11612 ' also important to note that user-defined functions which are\n'
jpayne@69 11613 ' attributes of a class instance are not converted to bound\n'
jpayne@69 11614 ' methods; this *only* happens when the function is an '
jpayne@69 11615 'attribute\n'
jpayne@69 11616 ' of the class.\n'
jpayne@69 11617 '\n'
jpayne@69 11618 ' Generator functions\n'
jpayne@69 11619 ' A function or method which uses the "yield" statement (see\n'
jpayne@69 11620 ' section The yield statement) is called a *generator '
jpayne@69 11621 'function*.\n'
jpayne@69 11622 ' Such a function, when called, always returns an iterator '
jpayne@69 11623 'object\n'
jpayne@69 11624 ' which can be used to execute the body of the function: '
jpayne@69 11625 'calling\n'
jpayne@69 11626 ' the iterator’s "iterator.__next__()" method will cause the\n'
jpayne@69 11627 ' function to execute until it provides a value using the '
jpayne@69 11628 '"yield"\n'
jpayne@69 11629 ' statement. When the function executes a "return" statement '
jpayne@69 11630 'or\n'
jpayne@69 11631 ' falls off the end, a "StopIteration" exception is raised and '
jpayne@69 11632 'the\n'
jpayne@69 11633 ' iterator will have reached the end of the set of values to '
jpayne@69 11634 'be\n'
jpayne@69 11635 ' returned.\n'
jpayne@69 11636 '\n'
jpayne@69 11637 ' Coroutine functions\n'
jpayne@69 11638 ' A function or method which is defined using "async def" is\n'
jpayne@69 11639 ' called a *coroutine function*. Such a function, when '
jpayne@69 11640 'called,\n'
jpayne@69 11641 ' returns a *coroutine* object. It may contain "await"\n'
jpayne@69 11642 ' expressions, as well as "async with" and "async for" '
jpayne@69 11643 'statements.\n'
jpayne@69 11644 ' See also the Coroutine Objects section.\n'
jpayne@69 11645 '\n'
jpayne@69 11646 ' Asynchronous generator functions\n'
jpayne@69 11647 ' A function or method which is defined using "async def" and\n'
jpayne@69 11648 ' which uses the "yield" statement is called a *asynchronous\n'
jpayne@69 11649 ' generator function*. Such a function, when called, returns '
jpayne@69 11650 'an\n'
jpayne@69 11651 ' asynchronous iterator object which can be used in an "async '
jpayne@69 11652 'for"\n'
jpayne@69 11653 ' statement to execute the body of the function.\n'
jpayne@69 11654 '\n'
jpayne@69 11655 ' Calling the asynchronous iterator’s "aiterator.__anext__()"\n'
jpayne@69 11656 ' method will return an *awaitable* which when awaited will\n'
jpayne@69 11657 ' execute until it provides a value using the "yield" '
jpayne@69 11658 'expression.\n'
jpayne@69 11659 ' When the function executes an empty "return" statement or '
jpayne@69 11660 'falls\n'
jpayne@69 11661 ' off the end, a "StopAsyncIteration" exception is raised and '
jpayne@69 11662 'the\n'
jpayne@69 11663 ' asynchronous iterator will have reached the end of the set '
jpayne@69 11664 'of\n'
jpayne@69 11665 ' values to be yielded.\n'
jpayne@69 11666 '\n'
jpayne@69 11667 ' Built-in functions\n'
jpayne@69 11668 ' A built-in function object is a wrapper around a C function.\n'
jpayne@69 11669 ' Examples of built-in functions are "len()" and "math.sin()"\n'
jpayne@69 11670 ' ("math" is a standard built-in module). The number and type '
jpayne@69 11671 'of\n'
jpayne@69 11672 ' the arguments are determined by the C function. Special '
jpayne@69 11673 'read-\n'
jpayne@69 11674 ' only attributes: "__doc__" is the function’s documentation\n'
jpayne@69 11675 ' string, or "None" if unavailable; "__name__" is the '
jpayne@69 11676 'function’s\n'
jpayne@69 11677 ' name; "__self__" is set to "None" (but see the next item);\n'
jpayne@69 11678 ' "__module__" is the name of the module the function was '
jpayne@69 11679 'defined\n'
jpayne@69 11680 ' in or "None" if unavailable.\n'
jpayne@69 11681 '\n'
jpayne@69 11682 ' Built-in methods\n'
jpayne@69 11683 ' This is really a different disguise of a built-in function, '
jpayne@69 11684 'this\n'
jpayne@69 11685 ' time containing an object passed to the C function as an\n'
jpayne@69 11686 ' implicit extra argument. An example of a built-in method is\n'
jpayne@69 11687 ' "alist.append()", assuming *alist* is a list object. In this\n'
jpayne@69 11688 ' case, the special read-only attribute "__self__" is set to '
jpayne@69 11689 'the\n'
jpayne@69 11690 ' object denoted by *alist*.\n'
jpayne@69 11691 '\n'
jpayne@69 11692 ' Classes\n'
jpayne@69 11693 ' Classes are callable. These objects normally act as '
jpayne@69 11694 'factories\n'
jpayne@69 11695 ' for new instances of themselves, but variations are possible '
jpayne@69 11696 'for\n'
jpayne@69 11697 ' class types that override "__new__()". The arguments of the\n'
jpayne@69 11698 ' call are passed to "__new__()" and, in the typical case, to\n'
jpayne@69 11699 ' "__init__()" to initialize the new instance.\n'
jpayne@69 11700 '\n'
jpayne@69 11701 ' Class Instances\n'
jpayne@69 11702 ' Instances of arbitrary classes can be made callable by '
jpayne@69 11703 'defining\n'
jpayne@69 11704 ' a "__call__()" method in their class.\n'
jpayne@69 11705 '\n'
jpayne@69 11706 'Modules\n'
jpayne@69 11707 ' Modules are a basic organizational unit of Python code, and are\n'
jpayne@69 11708 ' created by the import system as invoked either by the "import"\n'
jpayne@69 11709 ' statement, or by calling functions such as\n'
jpayne@69 11710 ' "importlib.import_module()" and built-in "__import__()". A '
jpayne@69 11711 'module\n'
jpayne@69 11712 ' object has a namespace implemented by a dictionary object (this '
jpayne@69 11713 'is\n'
jpayne@69 11714 ' the dictionary referenced by the "__globals__" attribute of\n'
jpayne@69 11715 ' functions defined in the module). Attribute references are\n'
jpayne@69 11716 ' translated to lookups in this dictionary, e.g., "m.x" is '
jpayne@69 11717 'equivalent\n'
jpayne@69 11718 ' to "m.__dict__["x"]". A module object does not contain the code\n'
jpayne@69 11719 ' object used to initialize the module (since it isn’t needed '
jpayne@69 11720 'once\n'
jpayne@69 11721 ' the initialization is done).\n'
jpayne@69 11722 '\n'
jpayne@69 11723 ' Attribute assignment updates the module’s namespace dictionary,\n'
jpayne@69 11724 ' e.g., "m.x = 1" is equivalent to "m.__dict__["x"] = 1".\n'
jpayne@69 11725 '\n'
jpayne@69 11726 ' Predefined (writable) attributes: "__name__" is the module’s '
jpayne@69 11727 'name;\n'
jpayne@69 11728 ' "__doc__" is the module’s documentation string, or "None" if\n'
jpayne@69 11729 ' unavailable; "__annotations__" (optional) is a dictionary\n'
jpayne@69 11730 ' containing *variable annotations* collected during module body\n'
jpayne@69 11731 ' execution; "__file__" is the pathname of the file from which '
jpayne@69 11732 'the\n'
jpayne@69 11733 ' module was loaded, if it was loaded from a file. The "__file__"\n'
jpayne@69 11734 ' attribute may be missing for certain types of modules, such as '
jpayne@69 11735 'C\n'
jpayne@69 11736 ' modules that are statically linked into the interpreter; for\n'
jpayne@69 11737 ' extension modules loaded dynamically from a shared library, it '
jpayne@69 11738 'is\n'
jpayne@69 11739 ' the pathname of the shared library file.\n'
jpayne@69 11740 '\n'
jpayne@69 11741 ' Special read-only attribute: "__dict__" is the module’s '
jpayne@69 11742 'namespace\n'
jpayne@69 11743 ' as a dictionary object.\n'
jpayne@69 11744 '\n'
jpayne@69 11745 ' **CPython implementation detail:** Because of the way CPython\n'
jpayne@69 11746 ' clears module dictionaries, the module dictionary will be '
jpayne@69 11747 'cleared\n'
jpayne@69 11748 ' when the module falls out of scope even if the dictionary still '
jpayne@69 11749 'has\n'
jpayne@69 11750 ' live references. To avoid this, copy the dictionary or keep '
jpayne@69 11751 'the\n'
jpayne@69 11752 ' module around while using its dictionary directly.\n'
jpayne@69 11753 '\n'
jpayne@69 11754 'Custom classes\n'
jpayne@69 11755 ' Custom class types are typically created by class definitions '
jpayne@69 11756 '(see\n'
jpayne@69 11757 ' section Class definitions). A class has a namespace implemented '
jpayne@69 11758 'by\n'
jpayne@69 11759 ' a dictionary object. Class attribute references are translated '
jpayne@69 11760 'to\n'
jpayne@69 11761 ' lookups in this dictionary, e.g., "C.x" is translated to\n'
jpayne@69 11762 ' "C.__dict__["x"]" (although there are a number of hooks which '
jpayne@69 11763 'allow\n'
jpayne@69 11764 ' for other means of locating attributes). When the attribute name '
jpayne@69 11765 'is\n'
jpayne@69 11766 ' not found there, the attribute search continues in the base\n'
jpayne@69 11767 ' classes. This search of the base classes uses the C3 method\n'
jpayne@69 11768 ' resolution order which behaves correctly even in the presence '
jpayne@69 11769 'of\n'
jpayne@69 11770 ' ‘diamond’ inheritance structures where there are multiple\n'
jpayne@69 11771 ' inheritance paths leading back to a common ancestor. Additional\n'
jpayne@69 11772 ' details on the C3 MRO used by Python can be found in the\n'
jpayne@69 11773 ' documentation accompanying the 2.3 release at\n'
jpayne@69 11774 ' https://www.python.org/download/releases/2.3/mro/.\n'
jpayne@69 11775 '\n'
jpayne@69 11776 ' When a class attribute reference (for class "C", say) would '
jpayne@69 11777 'yield a\n'
jpayne@69 11778 ' class method object, it is transformed into an instance method\n'
jpayne@69 11779 ' object whose "__self__" attribute is "C". When it would yield '
jpayne@69 11780 'a\n'
jpayne@69 11781 ' static method object, it is transformed into the object wrapped '
jpayne@69 11782 'by\n'
jpayne@69 11783 ' the static method object. See section Implementing Descriptors '
jpayne@69 11784 'for\n'
jpayne@69 11785 ' another way in which attributes retrieved from a class may '
jpayne@69 11786 'differ\n'
jpayne@69 11787 ' from those actually contained in its "__dict__".\n'
jpayne@69 11788 '\n'
jpayne@69 11789 ' Class attribute assignments update the class’s dictionary, '
jpayne@69 11790 'never\n'
jpayne@69 11791 ' the dictionary of a base class.\n'
jpayne@69 11792 '\n'
jpayne@69 11793 ' A class object can be called (see above) to yield a class '
jpayne@69 11794 'instance\n'
jpayne@69 11795 ' (see below).\n'
jpayne@69 11796 '\n'
jpayne@69 11797 ' Special attributes: "__name__" is the class name; "__module__" '
jpayne@69 11798 'is\n'
jpayne@69 11799 ' the module name in which the class was defined; "__dict__" is '
jpayne@69 11800 'the\n'
jpayne@69 11801 ' dictionary containing the class’s namespace; "__bases__" is a '
jpayne@69 11802 'tuple\n'
jpayne@69 11803 ' containing the base classes, in the order of their occurrence '
jpayne@69 11804 'in\n'
jpayne@69 11805 ' the base class list; "__doc__" is the class’s documentation '
jpayne@69 11806 'string,\n'
jpayne@69 11807 ' or "None" if undefined; "__annotations__" (optional) is a\n'
jpayne@69 11808 ' dictionary containing *variable annotations* collected during '
jpayne@69 11809 'class\n'
jpayne@69 11810 ' body execution.\n'
jpayne@69 11811 '\n'
jpayne@69 11812 'Class instances\n'
jpayne@69 11813 ' A class instance is created by calling a class object (see '
jpayne@69 11814 'above).\n'
jpayne@69 11815 ' A class instance has a namespace implemented as a dictionary '
jpayne@69 11816 'which\n'
jpayne@69 11817 ' is the first place in which attribute references are searched.\n'
jpayne@69 11818 ' When an attribute is not found there, and the instance’s class '
jpayne@69 11819 'has\n'
jpayne@69 11820 ' an attribute by that name, the search continues with the class\n'
jpayne@69 11821 ' attributes. If a class attribute is found that is a '
jpayne@69 11822 'user-defined\n'
jpayne@69 11823 ' function object, it is transformed into an instance method '
jpayne@69 11824 'object\n'
jpayne@69 11825 ' whose "__self__" attribute is the instance. Static method and\n'
jpayne@69 11826 ' class method objects are also transformed; see above under\n'
jpayne@69 11827 ' “Classes”. See section Implementing Descriptors for another way '
jpayne@69 11828 'in\n'
jpayne@69 11829 ' which attributes of a class retrieved via its instances may '
jpayne@69 11830 'differ\n'
jpayne@69 11831 ' from the objects actually stored in the class’s "__dict__". If '
jpayne@69 11832 'no\n'
jpayne@69 11833 ' class attribute is found, and the object’s class has a\n'
jpayne@69 11834 ' "__getattr__()" method, that is called to satisfy the lookup.\n'
jpayne@69 11835 '\n'
jpayne@69 11836 ' Attribute assignments and deletions update the instance’s\n'
jpayne@69 11837 ' dictionary, never a class’s dictionary. If the class has a\n'
jpayne@69 11838 ' "__setattr__()" or "__delattr__()" method, this is called '
jpayne@69 11839 'instead\n'
jpayne@69 11840 ' of updating the instance dictionary directly.\n'
jpayne@69 11841 '\n'
jpayne@69 11842 ' Class instances can pretend to be numbers, sequences, or '
jpayne@69 11843 'mappings\n'
jpayne@69 11844 ' if they have methods with certain special names. See section\n'
jpayne@69 11845 ' Special method names.\n'
jpayne@69 11846 '\n'
jpayne@69 11847 ' Special attributes: "__dict__" is the attribute dictionary;\n'
jpayne@69 11848 ' "__class__" is the instance’s class.\n'
jpayne@69 11849 '\n'
jpayne@69 11850 'I/O objects (also known as file objects)\n'
jpayne@69 11851 ' A *file object* represents an open file. Various shortcuts are\n'
jpayne@69 11852 ' available to create file objects: the "open()" built-in '
jpayne@69 11853 'function,\n'
jpayne@69 11854 ' and also "os.popen()", "os.fdopen()", and the "makefile()" '
jpayne@69 11855 'method\n'
jpayne@69 11856 ' of socket objects (and perhaps by other functions or methods\n'
jpayne@69 11857 ' provided by extension modules).\n'
jpayne@69 11858 '\n'
jpayne@69 11859 ' The objects "sys.stdin", "sys.stdout" and "sys.stderr" are\n'
jpayne@69 11860 ' initialized to file objects corresponding to the interpreter’s\n'
jpayne@69 11861 ' standard input, output and error streams; they are all open in '
jpayne@69 11862 'text\n'
jpayne@69 11863 ' mode and therefore follow the interface defined by the\n'
jpayne@69 11864 ' "io.TextIOBase" abstract class.\n'
jpayne@69 11865 '\n'
jpayne@69 11866 'Internal types\n'
jpayne@69 11867 ' A few types used internally by the interpreter are exposed to '
jpayne@69 11868 'the\n'
jpayne@69 11869 ' user. Their definitions may change with future versions of the\n'
jpayne@69 11870 ' interpreter, but they are mentioned here for completeness.\n'
jpayne@69 11871 '\n'
jpayne@69 11872 ' Code objects\n'
jpayne@69 11873 ' Code objects represent *byte-compiled* executable Python '
jpayne@69 11874 'code,\n'
jpayne@69 11875 ' or *bytecode*. The difference between a code object and a\n'
jpayne@69 11876 ' function object is that the function object contains an '
jpayne@69 11877 'explicit\n'
jpayne@69 11878 ' reference to the function’s globals (the module in which it '
jpayne@69 11879 'was\n'
jpayne@69 11880 ' defined), while a code object contains no context; also the\n'
jpayne@69 11881 ' default argument values are stored in the function object, '
jpayne@69 11882 'not\n'
jpayne@69 11883 ' in the code object (because they represent values calculated '
jpayne@69 11884 'at\n'
jpayne@69 11885 ' run-time). Unlike function objects, code objects are '
jpayne@69 11886 'immutable\n'
jpayne@69 11887 ' and contain no references (directly or indirectly) to '
jpayne@69 11888 'mutable\n'
jpayne@69 11889 ' objects.\n'
jpayne@69 11890 '\n'
jpayne@69 11891 ' Special read-only attributes: "co_name" gives the function '
jpayne@69 11892 'name;\n'
jpayne@69 11893 ' "co_argcount" is the total number of positional arguments\n'
jpayne@69 11894 ' (including positional-only arguments and arguments with '
jpayne@69 11895 'default\n'
jpayne@69 11896 ' values); "co_posonlyargcount" is the number of '
jpayne@69 11897 'positional-only\n'
jpayne@69 11898 ' arguments (including arguments with default values);\n'
jpayne@69 11899 ' "co_kwonlyargcount" is the number of keyword-only arguments\n'
jpayne@69 11900 ' (including arguments with default values); "co_nlocals" is '
jpayne@69 11901 'the\n'
jpayne@69 11902 ' number of local variables used by the function (including\n'
jpayne@69 11903 ' arguments); "co_varnames" is a tuple containing the names of '
jpayne@69 11904 'the\n'
jpayne@69 11905 ' local variables (starting with the argument names);\n'
jpayne@69 11906 ' "co_cellvars" is a tuple containing the names of local '
jpayne@69 11907 'variables\n'
jpayne@69 11908 ' that are referenced by nested functions; "co_freevars" is a\n'
jpayne@69 11909 ' tuple containing the names of free variables; "co_code" is a\n'
jpayne@69 11910 ' string representing the sequence of bytecode instructions;\n'
jpayne@69 11911 ' "co_consts" is a tuple containing the literals used by the\n'
jpayne@69 11912 ' bytecode; "co_names" is a tuple containing the names used by '
jpayne@69 11913 'the\n'
jpayne@69 11914 ' bytecode; "co_filename" is the filename from which the code '
jpayne@69 11915 'was\n'
jpayne@69 11916 ' compiled; "co_firstlineno" is the first line number of the\n'
jpayne@69 11917 ' function; "co_lnotab" is a string encoding the mapping from\n'
jpayne@69 11918 ' bytecode offsets to line numbers (for details see the source\n'
jpayne@69 11919 ' code of the interpreter); "co_stacksize" is the required '
jpayne@69 11920 'stack\n'
jpayne@69 11921 ' size (including local variables); "co_flags" is an integer\n'
jpayne@69 11922 ' encoding a number of flags for the interpreter.\n'
jpayne@69 11923 '\n'
jpayne@69 11924 ' The following flag bits are defined for "co_flags": bit '
jpayne@69 11925 '"0x04"\n'
jpayne@69 11926 ' is set if the function uses the "*arguments" syntax to accept '
jpayne@69 11927 'an\n'
jpayne@69 11928 ' arbitrary number of positional arguments; bit "0x08" is set '
jpayne@69 11929 'if\n'
jpayne@69 11930 ' the function uses the "**keywords" syntax to accept '
jpayne@69 11931 'arbitrary\n'
jpayne@69 11932 ' keyword arguments; bit "0x20" is set if the function is a\n'
jpayne@69 11933 ' generator.\n'
jpayne@69 11934 '\n'
jpayne@69 11935 ' Future feature declarations ("from __future__ import '
jpayne@69 11936 'division")\n'
jpayne@69 11937 ' also use bits in "co_flags" to indicate whether a code '
jpayne@69 11938 'object\n'
jpayne@69 11939 ' was compiled with a particular feature enabled: bit "0x2000" '
jpayne@69 11940 'is\n'
jpayne@69 11941 ' set if the function was compiled with future division '
jpayne@69 11942 'enabled;\n'
jpayne@69 11943 ' bits "0x10" and "0x1000" were used in earlier versions of\n'
jpayne@69 11944 ' Python.\n'
jpayne@69 11945 '\n'
jpayne@69 11946 ' Other bits in "co_flags" are reserved for internal use.\n'
jpayne@69 11947 '\n'
jpayne@69 11948 ' If a code object represents a function, the first item in\n'
jpayne@69 11949 ' "co_consts" is the documentation string of the function, or\n'
jpayne@69 11950 ' "None" if undefined.\n'
jpayne@69 11951 '\n'
jpayne@69 11952 ' Frame objects\n'
jpayne@69 11953 ' Frame objects represent execution frames. They may occur in\n'
jpayne@69 11954 ' traceback objects (see below), and are also passed to '
jpayne@69 11955 'registered\n'
jpayne@69 11956 ' trace functions.\n'
jpayne@69 11957 '\n'
jpayne@69 11958 ' Special read-only attributes: "f_back" is to the previous '
jpayne@69 11959 'stack\n'
jpayne@69 11960 ' frame (towards the caller), or "None" if this is the bottom\n'
jpayne@69 11961 ' stack frame; "f_code" is the code object being executed in '
jpayne@69 11962 'this\n'
jpayne@69 11963 ' frame; "f_locals" is the dictionary used to look up local\n'
jpayne@69 11964 ' variables; "f_globals" is used for global variables;\n'
jpayne@69 11965 ' "f_builtins" is used for built-in (intrinsic) names; '
jpayne@69 11966 '"f_lasti"\n'
jpayne@69 11967 ' gives the precise instruction (this is an index into the\n'
jpayne@69 11968 ' bytecode string of the code object).\n'
jpayne@69 11969 '\n'
jpayne@69 11970 ' Special writable attributes: "f_trace", if not "None", is a\n'
jpayne@69 11971 ' function called for various events during code execution '
jpayne@69 11972 '(this\n'
jpayne@69 11973 ' is used by the debugger). Normally an event is triggered for\n'
jpayne@69 11974 ' each new source line - this can be disabled by setting\n'
jpayne@69 11975 ' "f_trace_lines" to "False".\n'
jpayne@69 11976 '\n'
jpayne@69 11977 ' Implementations *may* allow per-opcode events to be requested '
jpayne@69 11978 'by\n'
jpayne@69 11979 ' setting "f_trace_opcodes" to "True". Note that this may lead '
jpayne@69 11980 'to\n'
jpayne@69 11981 ' undefined interpreter behaviour if exceptions raised by the\n'
jpayne@69 11982 ' trace function escape to the function being traced.\n'
jpayne@69 11983 '\n'
jpayne@69 11984 ' "f_lineno" is the current line number of the frame — writing '
jpayne@69 11985 'to\n'
jpayne@69 11986 ' this from within a trace function jumps to the given line '
jpayne@69 11987 '(only\n'
jpayne@69 11988 ' for the bottom-most frame). A debugger can implement a Jump\n'
jpayne@69 11989 ' command (aka Set Next Statement) by writing to f_lineno.\n'
jpayne@69 11990 '\n'
jpayne@69 11991 ' Frame objects support one method:\n'
jpayne@69 11992 '\n'
jpayne@69 11993 ' frame.clear()\n'
jpayne@69 11994 '\n'
jpayne@69 11995 ' This method clears all references to local variables held '
jpayne@69 11996 'by\n'
jpayne@69 11997 ' the frame. Also, if the frame belonged to a generator, '
jpayne@69 11998 'the\n'
jpayne@69 11999 ' generator is finalized. This helps break reference '
jpayne@69 12000 'cycles\n'
jpayne@69 12001 ' involving frame objects (for example when catching an\n'
jpayne@69 12002 ' exception and storing its traceback for later use).\n'
jpayne@69 12003 '\n'
jpayne@69 12004 ' "RuntimeError" is raised if the frame is currently '
jpayne@69 12005 'executing.\n'
jpayne@69 12006 '\n'
jpayne@69 12007 ' New in version 3.4.\n'
jpayne@69 12008 '\n'
jpayne@69 12009 ' Traceback objects\n'
jpayne@69 12010 ' Traceback objects represent a stack trace of an exception. '
jpayne@69 12011 'A\n'
jpayne@69 12012 ' traceback object is implicitly created when an exception '
jpayne@69 12013 'occurs,\n'
jpayne@69 12014 ' and may also be explicitly created by calling\n'
jpayne@69 12015 ' "types.TracebackType".\n'
jpayne@69 12016 '\n'
jpayne@69 12017 ' For implicitly created tracebacks, when the search for an\n'
jpayne@69 12018 ' exception handler unwinds the execution stack, at each '
jpayne@69 12019 'unwound\n'
jpayne@69 12020 ' level a traceback object is inserted in front of the current\n'
jpayne@69 12021 ' traceback. When an exception handler is entered, the stack\n'
jpayne@69 12022 ' trace is made available to the program. (See section The try\n'
jpayne@69 12023 ' statement.) It is accessible as the third item of the tuple\n'
jpayne@69 12024 ' returned by "sys.exc_info()", and as the "__traceback__"\n'
jpayne@69 12025 ' attribute of the caught exception.\n'
jpayne@69 12026 '\n'
jpayne@69 12027 ' When the program contains no suitable handler, the stack '
jpayne@69 12028 'trace\n'
jpayne@69 12029 ' is written (nicely formatted) to the standard error stream; '
jpayne@69 12030 'if\n'
jpayne@69 12031 ' the interpreter is interactive, it is also made available to '
jpayne@69 12032 'the\n'
jpayne@69 12033 ' user as "sys.last_traceback".\n'
jpayne@69 12034 '\n'
jpayne@69 12035 ' For explicitly created tracebacks, it is up to the creator '
jpayne@69 12036 'of\n'
jpayne@69 12037 ' the traceback to determine how the "tb_next" attributes '
jpayne@69 12038 'should\n'
jpayne@69 12039 ' be linked to form a full stack trace.\n'
jpayne@69 12040 '\n'
jpayne@69 12041 ' Special read-only attributes: "tb_frame" points to the '
jpayne@69 12042 'execution\n'
jpayne@69 12043 ' frame of the current level; "tb_lineno" gives the line '
jpayne@69 12044 'number\n'
jpayne@69 12045 ' where the exception occurred; "tb_lasti" indicates the '
jpayne@69 12046 'precise\n'
jpayne@69 12047 ' instruction. The line number and last instruction in the\n'
jpayne@69 12048 ' traceback may differ from the line number of its frame object '
jpayne@69 12049 'if\n'
jpayne@69 12050 ' the exception occurred in a "try" statement with no matching\n'
jpayne@69 12051 ' except clause or with a finally clause.\n'
jpayne@69 12052 '\n'
jpayne@69 12053 ' Special writable attribute: "tb_next" is the next level in '
jpayne@69 12054 'the\n'
jpayne@69 12055 ' stack trace (towards the frame where the exception occurred), '
jpayne@69 12056 'or\n'
jpayne@69 12057 ' "None" if there is no next level.\n'
jpayne@69 12058 '\n'
jpayne@69 12059 ' Changed in version 3.7: Traceback objects can now be '
jpayne@69 12060 'explicitly\n'
jpayne@69 12061 ' instantiated from Python code, and the "tb_next" attribute '
jpayne@69 12062 'of\n'
jpayne@69 12063 ' existing instances can be updated.\n'
jpayne@69 12064 '\n'
jpayne@69 12065 ' Slice objects\n'
jpayne@69 12066 ' Slice objects are used to represent slices for '
jpayne@69 12067 '"__getitem__()"\n'
jpayne@69 12068 ' methods. They are also created by the built-in "slice()"\n'
jpayne@69 12069 ' function.\n'
jpayne@69 12070 '\n'
jpayne@69 12071 ' Special read-only attributes: "start" is the lower bound; '
jpayne@69 12072 '"stop"\n'
jpayne@69 12073 ' is the upper bound; "step" is the step value; each is "None" '
jpayne@69 12074 'if\n'
jpayne@69 12075 ' omitted. These attributes can have any type.\n'
jpayne@69 12076 '\n'
jpayne@69 12077 ' Slice objects support one method:\n'
jpayne@69 12078 '\n'
jpayne@69 12079 ' slice.indices(self, length)\n'
jpayne@69 12080 '\n'
jpayne@69 12081 ' This method takes a single integer argument *length* and\n'
jpayne@69 12082 ' computes information about the slice that the slice '
jpayne@69 12083 'object\n'
jpayne@69 12084 ' would describe if applied to a sequence of *length* '
jpayne@69 12085 'items.\n'
jpayne@69 12086 ' It returns a tuple of three integers; respectively these '
jpayne@69 12087 'are\n'
jpayne@69 12088 ' the *start* and *stop* indices and the *step* or stride\n'
jpayne@69 12089 ' length of the slice. Missing or out-of-bounds indices are\n'
jpayne@69 12090 ' handled in a manner consistent with regular slices.\n'
jpayne@69 12091 '\n'
jpayne@69 12092 ' Static method objects\n'
jpayne@69 12093 ' Static method objects provide a way of defeating the\n'
jpayne@69 12094 ' transformation of function objects to method objects '
jpayne@69 12095 'described\n'
jpayne@69 12096 ' above. A static method object is a wrapper around any other\n'
jpayne@69 12097 ' object, usually a user-defined method object. When a static\n'
jpayne@69 12098 ' method object is retrieved from a class or a class instance, '
jpayne@69 12099 'the\n'
jpayne@69 12100 ' object actually returned is the wrapped object, which is not\n'
jpayne@69 12101 ' subject to any further transformation. Static method objects '
jpayne@69 12102 'are\n'
jpayne@69 12103 ' not themselves callable, although the objects they wrap '
jpayne@69 12104 'usually\n'
jpayne@69 12105 ' are. Static method objects are created by the built-in\n'
jpayne@69 12106 ' "staticmethod()" constructor.\n'
jpayne@69 12107 '\n'
jpayne@69 12108 ' Class method objects\n'
jpayne@69 12109 ' A class method object, like a static method object, is a '
jpayne@69 12110 'wrapper\n'
jpayne@69 12111 ' around another object that alters the way in which that '
jpayne@69 12112 'object\n'
jpayne@69 12113 ' is retrieved from classes and class instances. The behaviour '
jpayne@69 12114 'of\n'
jpayne@69 12115 ' class method objects upon such retrieval is described above,\n'
jpayne@69 12116 ' under “User-defined methods”. Class method objects are '
jpayne@69 12117 'created\n'
jpayne@69 12118 ' by the built-in "classmethod()" constructor.\n',
jpayne@69 12119 'typesfunctions': 'Functions\n'
jpayne@69 12120 '*********\n'
jpayne@69 12121 '\n'
jpayne@69 12122 'Function objects are created by function definitions. The '
jpayne@69 12123 'only\n'
jpayne@69 12124 'operation on a function object is to call it: '
jpayne@69 12125 '"func(argument-list)".\n'
jpayne@69 12126 '\n'
jpayne@69 12127 'There are really two flavors of function objects: built-in '
jpayne@69 12128 'functions\n'
jpayne@69 12129 'and user-defined functions. Both support the same '
jpayne@69 12130 'operation (to call\n'
jpayne@69 12131 'the function), but the implementation is different, hence '
jpayne@69 12132 'the\n'
jpayne@69 12133 'different object types.\n'
jpayne@69 12134 '\n'
jpayne@69 12135 'See Function definitions for more information.\n',
jpayne@69 12136 'typesmapping': 'Mapping Types — "dict"\n'
jpayne@69 12137 '**********************\n'
jpayne@69 12138 '\n'
jpayne@69 12139 'A *mapping* object maps *hashable* values to arbitrary '
jpayne@69 12140 'objects.\n'
jpayne@69 12141 'Mappings are mutable objects. There is currently only one '
jpayne@69 12142 'standard\n'
jpayne@69 12143 'mapping type, the *dictionary*. (For other containers see '
jpayne@69 12144 'the built-\n'
jpayne@69 12145 'in "list", "set", and "tuple" classes, and the "collections" '
jpayne@69 12146 'module.)\n'
jpayne@69 12147 '\n'
jpayne@69 12148 'A dictionary’s keys are *almost* arbitrary values. Values '
jpayne@69 12149 'that are\n'
jpayne@69 12150 'not *hashable*, that is, values containing lists, '
jpayne@69 12151 'dictionaries or\n'
jpayne@69 12152 'other mutable types (that are compared by value rather than '
jpayne@69 12153 'by object\n'
jpayne@69 12154 'identity) may not be used as keys. Numeric types used for '
jpayne@69 12155 'keys obey\n'
jpayne@69 12156 'the normal rules for numeric comparison: if two numbers '
jpayne@69 12157 'compare equal\n'
jpayne@69 12158 '(such as "1" and "1.0") then they can be used '
jpayne@69 12159 'interchangeably to index\n'
jpayne@69 12160 'the same dictionary entry. (Note however, that since '
jpayne@69 12161 'computers store\n'
jpayne@69 12162 'floating-point numbers as approximations it is usually '
jpayne@69 12163 'unwise to use\n'
jpayne@69 12164 'them as dictionary keys.)\n'
jpayne@69 12165 '\n'
jpayne@69 12166 'Dictionaries can be created by placing a comma-separated '
jpayne@69 12167 'list of "key:\n'
jpayne@69 12168 'value" pairs within braces, for example: "{\'jack\': 4098, '
jpayne@69 12169 "'sjoerd':\n"
jpayne@69 12170 '4127}" or "{4098: \'jack\', 4127: \'sjoerd\'}", or by the '
jpayne@69 12171 '"dict"\n'
jpayne@69 12172 'constructor.\n'
jpayne@69 12173 '\n'
jpayne@69 12174 'class dict(**kwarg)\n'
jpayne@69 12175 'class dict(mapping, **kwarg)\n'
jpayne@69 12176 'class dict(iterable, **kwarg)\n'
jpayne@69 12177 '\n'
jpayne@69 12178 ' Return a new dictionary initialized from an optional '
jpayne@69 12179 'positional\n'
jpayne@69 12180 ' argument and a possibly empty set of keyword arguments.\n'
jpayne@69 12181 '\n'
jpayne@69 12182 ' If no positional argument is given, an empty dictionary '
jpayne@69 12183 'is created.\n'
jpayne@69 12184 ' If a positional argument is given and it is a mapping '
jpayne@69 12185 'object, a\n'
jpayne@69 12186 ' dictionary is created with the same key-value pairs as '
jpayne@69 12187 'the mapping\n'
jpayne@69 12188 ' object. Otherwise, the positional argument must be an '
jpayne@69 12189 '*iterable*\n'
jpayne@69 12190 ' object. Each item in the iterable must itself be an '
jpayne@69 12191 'iterable with\n'
jpayne@69 12192 ' exactly two objects. The first object of each item '
jpayne@69 12193 'becomes a key\n'
jpayne@69 12194 ' in the new dictionary, and the second object the '
jpayne@69 12195 'corresponding\n'
jpayne@69 12196 ' value. If a key occurs more than once, the last value '
jpayne@69 12197 'for that key\n'
jpayne@69 12198 ' becomes the corresponding value in the new dictionary.\n'
jpayne@69 12199 '\n'
jpayne@69 12200 ' If keyword arguments are given, the keyword arguments and '
jpayne@69 12201 'their\n'
jpayne@69 12202 ' values are added to the dictionary created from the '
jpayne@69 12203 'positional\n'
jpayne@69 12204 ' argument. If a key being added is already present, the '
jpayne@69 12205 'value from\n'
jpayne@69 12206 ' the keyword argument replaces the value from the '
jpayne@69 12207 'positional\n'
jpayne@69 12208 ' argument.\n'
jpayne@69 12209 '\n'
jpayne@69 12210 ' To illustrate, the following examples all return a '
jpayne@69 12211 'dictionary equal\n'
jpayne@69 12212 ' to "{"one": 1, "two": 2, "three": 3}":\n'
jpayne@69 12213 '\n'
jpayne@69 12214 ' >>> a = dict(one=1, two=2, three=3)\n'
jpayne@69 12215 " >>> b = {'one': 1, 'two': 2, 'three': 3}\n"
jpayne@69 12216 " >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))\n"
jpayne@69 12217 " >>> d = dict([('two', 2), ('one', 1), ('three', 3)])\n"
jpayne@69 12218 " >>> e = dict({'three': 3, 'one': 1, 'two': 2})\n"
jpayne@69 12219 ' >>> a == b == c == d == e\n'
jpayne@69 12220 ' True\n'
jpayne@69 12221 '\n'
jpayne@69 12222 ' Providing keyword arguments as in the first example only '
jpayne@69 12223 'works for\n'
jpayne@69 12224 ' keys that are valid Python identifiers. Otherwise, any '
jpayne@69 12225 'valid keys\n'
jpayne@69 12226 ' can be used.\n'
jpayne@69 12227 '\n'
jpayne@69 12228 ' These are the operations that dictionaries support (and '
jpayne@69 12229 'therefore,\n'
jpayne@69 12230 ' custom mapping types should support too):\n'
jpayne@69 12231 '\n'
jpayne@69 12232 ' list(d)\n'
jpayne@69 12233 '\n'
jpayne@69 12234 ' Return a list of all the keys used in the dictionary '
jpayne@69 12235 '*d*.\n'
jpayne@69 12236 '\n'
jpayne@69 12237 ' len(d)\n'
jpayne@69 12238 '\n'
jpayne@69 12239 ' Return the number of items in the dictionary *d*.\n'
jpayne@69 12240 '\n'
jpayne@69 12241 ' d[key]\n'
jpayne@69 12242 '\n'
jpayne@69 12243 ' Return the item of *d* with key *key*. Raises a '
jpayne@69 12244 '"KeyError" if\n'
jpayne@69 12245 ' *key* is not in the map.\n'
jpayne@69 12246 '\n'
jpayne@69 12247 ' If a subclass of dict defines a method "__missing__()" '
jpayne@69 12248 'and *key*\n'
jpayne@69 12249 ' is not present, the "d[key]" operation calls that '
jpayne@69 12250 'method with\n'
jpayne@69 12251 ' the key *key* as argument. The "d[key]" operation '
jpayne@69 12252 'then returns\n'
jpayne@69 12253 ' or raises whatever is returned or raised by the\n'
jpayne@69 12254 ' "__missing__(key)" call. No other operations or '
jpayne@69 12255 'methods invoke\n'
jpayne@69 12256 ' "__missing__()". If "__missing__()" is not defined, '
jpayne@69 12257 '"KeyError"\n'
jpayne@69 12258 ' is raised. "__missing__()" must be a method; it cannot '
jpayne@69 12259 'be an\n'
jpayne@69 12260 ' instance variable:\n'
jpayne@69 12261 '\n'
jpayne@69 12262 ' >>> class Counter(dict):\n'
jpayne@69 12263 ' ... def __missing__(self, key):\n'
jpayne@69 12264 ' ... return 0\n'
jpayne@69 12265 ' >>> c = Counter()\n'
jpayne@69 12266 " >>> c['red']\n"
jpayne@69 12267 ' 0\n'
jpayne@69 12268 " >>> c['red'] += 1\n"
jpayne@69 12269 " >>> c['red']\n"
jpayne@69 12270 ' 1\n'
jpayne@69 12271 '\n'
jpayne@69 12272 ' The example above shows part of the implementation of\n'
jpayne@69 12273 ' "collections.Counter". A different "__missing__" '
jpayne@69 12274 'method is used\n'
jpayne@69 12275 ' by "collections.defaultdict".\n'
jpayne@69 12276 '\n'
jpayne@69 12277 ' d[key] = value\n'
jpayne@69 12278 '\n'
jpayne@69 12279 ' Set "d[key]" to *value*.\n'
jpayne@69 12280 '\n'
jpayne@69 12281 ' del d[key]\n'
jpayne@69 12282 '\n'
jpayne@69 12283 ' Remove "d[key]" from *d*. Raises a "KeyError" if '
jpayne@69 12284 '*key* is not\n'
jpayne@69 12285 ' in the map.\n'
jpayne@69 12286 '\n'
jpayne@69 12287 ' key in d\n'
jpayne@69 12288 '\n'
jpayne@69 12289 ' Return "True" if *d* has a key *key*, else "False".\n'
jpayne@69 12290 '\n'
jpayne@69 12291 ' key not in d\n'
jpayne@69 12292 '\n'
jpayne@69 12293 ' Equivalent to "not key in d".\n'
jpayne@69 12294 '\n'
jpayne@69 12295 ' iter(d)\n'
jpayne@69 12296 '\n'
jpayne@69 12297 ' Return an iterator over the keys of the dictionary. '
jpayne@69 12298 'This is a\n'
jpayne@69 12299 ' shortcut for "iter(d.keys())".\n'
jpayne@69 12300 '\n'
jpayne@69 12301 ' clear()\n'
jpayne@69 12302 '\n'
jpayne@69 12303 ' Remove all items from the dictionary.\n'
jpayne@69 12304 '\n'
jpayne@69 12305 ' copy()\n'
jpayne@69 12306 '\n'
jpayne@69 12307 ' Return a shallow copy of the dictionary.\n'
jpayne@69 12308 '\n'
jpayne@69 12309 ' classmethod fromkeys(iterable[, value])\n'
jpayne@69 12310 '\n'
jpayne@69 12311 ' Create a new dictionary with keys from *iterable* and '
jpayne@69 12312 'values set\n'
jpayne@69 12313 ' to *value*.\n'
jpayne@69 12314 '\n'
jpayne@69 12315 ' "fromkeys()" is a class method that returns a new '
jpayne@69 12316 'dictionary.\n'
jpayne@69 12317 ' *value* defaults to "None". All of the values refer '
jpayne@69 12318 'to just a\n'
jpayne@69 12319 ' single instance, so it generally doesn’t make sense '
jpayne@69 12320 'for *value*\n'
jpayne@69 12321 ' to be a mutable object such as an empty list. To get '
jpayne@69 12322 'distinct\n'
jpayne@69 12323 ' values, use a dict comprehension instead.\n'
jpayne@69 12324 '\n'
jpayne@69 12325 ' get(key[, default])\n'
jpayne@69 12326 '\n'
jpayne@69 12327 ' Return the value for *key* if *key* is in the '
jpayne@69 12328 'dictionary, else\n'
jpayne@69 12329 ' *default*. If *default* is not given, it defaults to '
jpayne@69 12330 '"None", so\n'
jpayne@69 12331 ' that this method never raises a "KeyError".\n'
jpayne@69 12332 '\n'
jpayne@69 12333 ' items()\n'
jpayne@69 12334 '\n'
jpayne@69 12335 ' Return a new view of the dictionary’s items ("(key, '
jpayne@69 12336 'value)"\n'
jpayne@69 12337 ' pairs). See the documentation of view objects.\n'
jpayne@69 12338 '\n'
jpayne@69 12339 ' keys()\n'
jpayne@69 12340 '\n'
jpayne@69 12341 ' Return a new view of the dictionary’s keys. See the\n'
jpayne@69 12342 ' documentation of view objects.\n'
jpayne@69 12343 '\n'
jpayne@69 12344 ' pop(key[, default])\n'
jpayne@69 12345 '\n'
jpayne@69 12346 ' If *key* is in the dictionary, remove it and return '
jpayne@69 12347 'its value,\n'
jpayne@69 12348 ' else return *default*. If *default* is not given and '
jpayne@69 12349 '*key* is\n'
jpayne@69 12350 ' not in the dictionary, a "KeyError" is raised.\n'
jpayne@69 12351 '\n'
jpayne@69 12352 ' popitem()\n'
jpayne@69 12353 '\n'
jpayne@69 12354 ' Remove and return a "(key, value)" pair from the '
jpayne@69 12355 'dictionary.\n'
jpayne@69 12356 ' Pairs are returned in LIFO (last-in, first-out) '
jpayne@69 12357 'order.\n'
jpayne@69 12358 '\n'
jpayne@69 12359 ' "popitem()" is useful to destructively iterate over a\n'
jpayne@69 12360 ' dictionary, as often used in set algorithms. If the '
jpayne@69 12361 'dictionary\n'
jpayne@69 12362 ' is empty, calling "popitem()" raises a "KeyError".\n'
jpayne@69 12363 '\n'
jpayne@69 12364 ' Changed in version 3.7: LIFO order is now guaranteed. '
jpayne@69 12365 'In prior\n'
jpayne@69 12366 ' versions, "popitem()" would return an arbitrary '
jpayne@69 12367 'key/value pair.\n'
jpayne@69 12368 '\n'
jpayne@69 12369 ' reversed(d)\n'
jpayne@69 12370 '\n'
jpayne@69 12371 ' Return a reverse iterator over the keys of the '
jpayne@69 12372 'dictionary. This\n'
jpayne@69 12373 ' is a shortcut for "reversed(d.keys())".\n'
jpayne@69 12374 '\n'
jpayne@69 12375 ' setdefault(key[, default])\n'
jpayne@69 12376 '\n'
jpayne@69 12377 ' If *key* is in the dictionary, return its value. If '
jpayne@69 12378 'not, insert\n'
jpayne@69 12379 ' *key* with a value of *default* and return *default*. '
jpayne@69 12380 '*default*\n'
jpayne@69 12381 ' defaults to "None".\n'
jpayne@69 12382 '\n'
jpayne@69 12383 ' update([other])\n'
jpayne@69 12384 '\n'
jpayne@69 12385 ' Update the dictionary with the key/value pairs from '
jpayne@69 12386 '*other*,\n'
jpayne@69 12387 ' overwriting existing keys. Return "None".\n'
jpayne@69 12388 '\n'
jpayne@69 12389 ' "update()" accepts either another dictionary object or '
jpayne@69 12390 'an\n'
jpayne@69 12391 ' iterable of key/value pairs (as tuples or other '
jpayne@69 12392 'iterables of\n'
jpayne@69 12393 ' length two). If keyword arguments are specified, the '
jpayne@69 12394 'dictionary\n'
jpayne@69 12395 ' is then updated with those key/value pairs: '
jpayne@69 12396 '"d.update(red=1,\n'
jpayne@69 12397 ' blue=2)".\n'
jpayne@69 12398 '\n'
jpayne@69 12399 ' values()\n'
jpayne@69 12400 '\n'
jpayne@69 12401 ' Return a new view of the dictionary’s values. See '
jpayne@69 12402 'the\n'
jpayne@69 12403 ' documentation of view objects.\n'
jpayne@69 12404 '\n'
jpayne@69 12405 ' An equality comparison between one "dict.values()" '
jpayne@69 12406 'view and\n'
jpayne@69 12407 ' another will always return "False". This also applies '
jpayne@69 12408 'when\n'
jpayne@69 12409 ' comparing "dict.values()" to itself:\n'
jpayne@69 12410 '\n'
jpayne@69 12411 " >>> d = {'a': 1}\n"
jpayne@69 12412 ' >>> d.values() == d.values()\n'
jpayne@69 12413 ' False\n'
jpayne@69 12414 '\n'
jpayne@69 12415 ' Dictionaries compare equal if and only if they have the '
jpayne@69 12416 'same "(key,\n'
jpayne@69 12417 ' value)" pairs (regardless of ordering). Order comparisons '
jpayne@69 12418 '(‘<’,\n'
jpayne@69 12419 ' ‘<=’, ‘>=’, ‘>’) raise "TypeError".\n'
jpayne@69 12420 '\n'
jpayne@69 12421 ' Dictionaries preserve insertion order. Note that '
jpayne@69 12422 'updating a key\n'
jpayne@69 12423 ' does not affect the order. Keys added after deletion are '
jpayne@69 12424 'inserted\n'
jpayne@69 12425 ' at the end.\n'
jpayne@69 12426 '\n'
jpayne@69 12427 ' >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}\n'
jpayne@69 12428 ' >>> d\n'
jpayne@69 12429 " {'one': 1, 'two': 2, 'three': 3, 'four': 4}\n"
jpayne@69 12430 ' >>> list(d)\n'
jpayne@69 12431 " ['one', 'two', 'three', 'four']\n"
jpayne@69 12432 ' >>> list(d.values())\n'
jpayne@69 12433 ' [1, 2, 3, 4]\n'
jpayne@69 12434 ' >>> d["one"] = 42\n'
jpayne@69 12435 ' >>> d\n'
jpayne@69 12436 " {'one': 42, 'two': 2, 'three': 3, 'four': 4}\n"
jpayne@69 12437 ' >>> del d["two"]\n'
jpayne@69 12438 ' >>> d["two"] = None\n'
jpayne@69 12439 ' >>> d\n'
jpayne@69 12440 " {'one': 42, 'three': 3, 'four': 4, 'two': None}\n"
jpayne@69 12441 '\n'
jpayne@69 12442 ' Changed in version 3.7: Dictionary order is guaranteed to '
jpayne@69 12443 'be\n'
jpayne@69 12444 ' insertion order. This behavior was an implementation '
jpayne@69 12445 'detail of\n'
jpayne@69 12446 ' CPython from 3.6.\n'
jpayne@69 12447 '\n'
jpayne@69 12448 ' Dictionaries and dictionary views are reversible.\n'
jpayne@69 12449 '\n'
jpayne@69 12450 ' >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}\n'
jpayne@69 12451 ' >>> d\n'
jpayne@69 12452 " {'one': 1, 'two': 2, 'three': 3, 'four': 4}\n"
jpayne@69 12453 ' >>> list(reversed(d))\n'
jpayne@69 12454 " ['four', 'three', 'two', 'one']\n"
jpayne@69 12455 ' >>> list(reversed(d.values()))\n'
jpayne@69 12456 ' [4, 3, 2, 1]\n'
jpayne@69 12457 ' >>> list(reversed(d.items()))\n'
jpayne@69 12458 " [('four', 4), ('three', 3), ('two', 2), ('one', 1)]\n"
jpayne@69 12459 '\n'
jpayne@69 12460 ' Changed in version 3.8: Dictionaries are now reversible.\n'
jpayne@69 12461 '\n'
jpayne@69 12462 'See also: "types.MappingProxyType" can be used to create a '
jpayne@69 12463 'read-only\n'
jpayne@69 12464 ' view of a "dict".\n'
jpayne@69 12465 '\n'
jpayne@69 12466 '\n'
jpayne@69 12467 'Dictionary view objects\n'
jpayne@69 12468 '=======================\n'
jpayne@69 12469 '\n'
jpayne@69 12470 'The objects returned by "dict.keys()", "dict.values()" and\n'
jpayne@69 12471 '"dict.items()" are *view objects*. They provide a dynamic '
jpayne@69 12472 'view on the\n'
jpayne@69 12473 'dictionary’s entries, which means that when the dictionary '
jpayne@69 12474 'changes,\n'
jpayne@69 12475 'the view reflects these changes.\n'
jpayne@69 12476 '\n'
jpayne@69 12477 'Dictionary views can be iterated over to yield their '
jpayne@69 12478 'respective data,\n'
jpayne@69 12479 'and support membership tests:\n'
jpayne@69 12480 '\n'
jpayne@69 12481 'len(dictview)\n'
jpayne@69 12482 '\n'
jpayne@69 12483 ' Return the number of entries in the dictionary.\n'
jpayne@69 12484 '\n'
jpayne@69 12485 'iter(dictview)\n'
jpayne@69 12486 '\n'
jpayne@69 12487 ' Return an iterator over the keys, values or items '
jpayne@69 12488 '(represented as\n'
jpayne@69 12489 ' tuples of "(key, value)") in the dictionary.\n'
jpayne@69 12490 '\n'
jpayne@69 12491 ' Keys and values are iterated over in insertion order. '
jpayne@69 12492 'This allows\n'
jpayne@69 12493 ' the creation of "(value, key)" pairs using "zip()": '
jpayne@69 12494 '"pairs =\n'
jpayne@69 12495 ' zip(d.values(), d.keys())". Another way to create the '
jpayne@69 12496 'same list is\n'
jpayne@69 12497 ' "pairs = [(v, k) for (k, v) in d.items()]".\n'
jpayne@69 12498 '\n'
jpayne@69 12499 ' Iterating views while adding or deleting entries in the '
jpayne@69 12500 'dictionary\n'
jpayne@69 12501 ' may raise a "RuntimeError" or fail to iterate over all '
jpayne@69 12502 'entries.\n'
jpayne@69 12503 '\n'
jpayne@69 12504 ' Changed in version 3.7: Dictionary order is guaranteed to '
jpayne@69 12505 'be\n'
jpayne@69 12506 ' insertion order.\n'
jpayne@69 12507 '\n'
jpayne@69 12508 'x in dictview\n'
jpayne@69 12509 '\n'
jpayne@69 12510 ' Return "True" if *x* is in the underlying dictionary’s '
jpayne@69 12511 'keys, values\n'
jpayne@69 12512 ' or items (in the latter case, *x* should be a "(key, '
jpayne@69 12513 'value)"\n'
jpayne@69 12514 ' tuple).\n'
jpayne@69 12515 '\n'
jpayne@69 12516 'reversed(dictview)\n'
jpayne@69 12517 '\n'
jpayne@69 12518 ' Return a reverse iterator over the keys, values or items '
jpayne@69 12519 'of the\n'
jpayne@69 12520 ' dictionary. The view will be iterated in reverse order of '
jpayne@69 12521 'the\n'
jpayne@69 12522 ' insertion.\n'
jpayne@69 12523 '\n'
jpayne@69 12524 ' Changed in version 3.8: Dictionary views are now '
jpayne@69 12525 'reversible.\n'
jpayne@69 12526 '\n'
jpayne@69 12527 'Keys views are set-like since their entries are unique and '
jpayne@69 12528 'hashable.\n'
jpayne@69 12529 'If all values are hashable, so that "(key, value)" pairs are '
jpayne@69 12530 'unique\n'
jpayne@69 12531 'and hashable, then the items view is also set-like. (Values '
jpayne@69 12532 'views are\n'
jpayne@69 12533 'not treated as set-like since the entries are generally not '
jpayne@69 12534 'unique.)\n'
jpayne@69 12535 'For set-like views, all of the operations defined for the '
jpayne@69 12536 'abstract\n'
jpayne@69 12537 'base class "collections.abc.Set" are available (for example, '
jpayne@69 12538 '"==",\n'
jpayne@69 12539 '"<", or "^").\n'
jpayne@69 12540 '\n'
jpayne@69 12541 'An example of dictionary view usage:\n'
jpayne@69 12542 '\n'
jpayne@69 12543 " >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, "
jpayne@69 12544 "'spam': 500}\n"
jpayne@69 12545 ' >>> keys = dishes.keys()\n'
jpayne@69 12546 ' >>> values = dishes.values()\n'
jpayne@69 12547 '\n'
jpayne@69 12548 ' >>> # iteration\n'
jpayne@69 12549 ' >>> n = 0\n'
jpayne@69 12550 ' >>> for val in values:\n'
jpayne@69 12551 ' ... n += val\n'
jpayne@69 12552 ' >>> print(n)\n'
jpayne@69 12553 ' 504\n'
jpayne@69 12554 '\n'
jpayne@69 12555 ' >>> # keys and values are iterated over in the same order '
jpayne@69 12556 '(insertion order)\n'
jpayne@69 12557 ' >>> list(keys)\n'
jpayne@69 12558 " ['eggs', 'sausage', 'bacon', 'spam']\n"
jpayne@69 12559 ' >>> list(values)\n'
jpayne@69 12560 ' [2, 1, 1, 500]\n'
jpayne@69 12561 '\n'
jpayne@69 12562 ' >>> # view objects are dynamic and reflect dict changes\n'
jpayne@69 12563 " >>> del dishes['eggs']\n"
jpayne@69 12564 " >>> del dishes['sausage']\n"
jpayne@69 12565 ' >>> list(keys)\n'
jpayne@69 12566 " ['bacon', 'spam']\n"
jpayne@69 12567 '\n'
jpayne@69 12568 ' >>> # set operations\n'
jpayne@69 12569 " >>> keys & {'eggs', 'bacon', 'salad'}\n"
jpayne@69 12570 " {'bacon'}\n"
jpayne@69 12571 " >>> keys ^ {'sausage', 'juice'}\n"
jpayne@69 12572 " {'juice', 'sausage', 'bacon', 'spam'}\n",
jpayne@69 12573 'typesmethods': 'Methods\n'
jpayne@69 12574 '*******\n'
jpayne@69 12575 '\n'
jpayne@69 12576 'Methods are functions that are called using the attribute '
jpayne@69 12577 'notation.\n'
jpayne@69 12578 'There are two flavors: built-in methods (such as "append()" '
jpayne@69 12579 'on lists)\n'
jpayne@69 12580 'and class instance methods. Built-in methods are described '
jpayne@69 12581 'with the\n'
jpayne@69 12582 'types that support them.\n'
jpayne@69 12583 '\n'
jpayne@69 12584 'If you access a method (a function defined in a class '
jpayne@69 12585 'namespace)\n'
jpayne@69 12586 'through an instance, you get a special object: a *bound '
jpayne@69 12587 'method* (also\n'
jpayne@69 12588 'called *instance method*) object. When called, it will add '
jpayne@69 12589 'the "self"\n'
jpayne@69 12590 'argument to the argument list. Bound methods have two '
jpayne@69 12591 'special read-\n'
jpayne@69 12592 'only attributes: "m.__self__" is the object on which the '
jpayne@69 12593 'method\n'
jpayne@69 12594 'operates, and "m.__func__" is the function implementing the '
jpayne@69 12595 'method.\n'
jpayne@69 12596 'Calling "m(arg-1, arg-2, ..., arg-n)" is completely '
jpayne@69 12597 'equivalent to\n'
jpayne@69 12598 'calling "m.__func__(m.__self__, arg-1, arg-2, ..., arg-n)".\n'
jpayne@69 12599 '\n'
jpayne@69 12600 'Like function objects, bound method objects support getting '
jpayne@69 12601 'arbitrary\n'
jpayne@69 12602 'attributes. However, since method attributes are actually '
jpayne@69 12603 'stored on\n'
jpayne@69 12604 'the underlying function object ("meth.__func__"), setting '
jpayne@69 12605 'method\n'
jpayne@69 12606 'attributes on bound methods is disallowed. Attempting to '
jpayne@69 12607 'set an\n'
jpayne@69 12608 'attribute on a method results in an "AttributeError" being '
jpayne@69 12609 'raised. In\n'
jpayne@69 12610 'order to set a method attribute, you need to explicitly set '
jpayne@69 12611 'it on the\n'
jpayne@69 12612 'underlying function object:\n'
jpayne@69 12613 '\n'
jpayne@69 12614 ' >>> class C:\n'
jpayne@69 12615 ' ... def method(self):\n'
jpayne@69 12616 ' ... pass\n'
jpayne@69 12617 ' ...\n'
jpayne@69 12618 ' >>> c = C()\n'
jpayne@69 12619 " >>> c.method.whoami = 'my name is method' # can't set on "
jpayne@69 12620 'the method\n'
jpayne@69 12621 ' Traceback (most recent call last):\n'
jpayne@69 12622 ' File "<stdin>", line 1, in <module>\n'
jpayne@69 12623 " AttributeError: 'method' object has no attribute "
jpayne@69 12624 "'whoami'\n"
jpayne@69 12625 " >>> c.method.__func__.whoami = 'my name is method'\n"
jpayne@69 12626 ' >>> c.method.whoami\n'
jpayne@69 12627 " 'my name is method'\n"
jpayne@69 12628 '\n'
jpayne@69 12629 'See The standard type hierarchy for more information.\n',
jpayne@69 12630 'typesmodules': 'Modules\n'
jpayne@69 12631 '*******\n'
jpayne@69 12632 '\n'
jpayne@69 12633 'The only special operation on a module is attribute access: '
jpayne@69 12634 '"m.name",\n'
jpayne@69 12635 'where *m* is a module and *name* accesses a name defined in '
jpayne@69 12636 '*m*’s\n'
jpayne@69 12637 'symbol table. Module attributes can be assigned to. (Note '
jpayne@69 12638 'that the\n'
jpayne@69 12639 '"import" statement is not, strictly speaking, an operation '
jpayne@69 12640 'on a module\n'
jpayne@69 12641 'object; "import foo" does not require a module object named '
jpayne@69 12642 '*foo* to\n'
jpayne@69 12643 'exist, rather it requires an (external) *definition* for a '
jpayne@69 12644 'module\n'
jpayne@69 12645 'named *foo* somewhere.)\n'
jpayne@69 12646 '\n'
jpayne@69 12647 'A special attribute of every module is "__dict__". This is '
jpayne@69 12648 'the\n'
jpayne@69 12649 'dictionary containing the module’s symbol table. Modifying '
jpayne@69 12650 'this\n'
jpayne@69 12651 'dictionary will actually change the module’s symbol table, '
jpayne@69 12652 'but direct\n'
jpayne@69 12653 'assignment to the "__dict__" attribute is not possible (you '
jpayne@69 12654 'can write\n'
jpayne@69 12655 '"m.__dict__[\'a\'] = 1", which defines "m.a" to be "1", but '
jpayne@69 12656 'you can’t\n'
jpayne@69 12657 'write "m.__dict__ = {}"). Modifying "__dict__" directly is '
jpayne@69 12658 'not\n'
jpayne@69 12659 'recommended.\n'
jpayne@69 12660 '\n'
jpayne@69 12661 'Modules built into the interpreter are written like this: '
jpayne@69 12662 '"<module\n'
jpayne@69 12663 '\'sys\' (built-in)>". If loaded from a file, they are '
jpayne@69 12664 'written as\n'
jpayne@69 12665 '"<module \'os\' from '
jpayne@69 12666 '\'/usr/local/lib/pythonX.Y/os.pyc\'>".\n',
jpayne@69 12667 'typesseq': 'Sequence Types — "list", "tuple", "range"\n'
jpayne@69 12668 '*****************************************\n'
jpayne@69 12669 '\n'
jpayne@69 12670 'There are three basic sequence types: lists, tuples, and range\n'
jpayne@69 12671 'objects. Additional sequence types tailored for processing of '
jpayne@69 12672 'binary\n'
jpayne@69 12673 'data and text strings are described in dedicated sections.\n'
jpayne@69 12674 '\n'
jpayne@69 12675 '\n'
jpayne@69 12676 'Common Sequence Operations\n'
jpayne@69 12677 '==========================\n'
jpayne@69 12678 '\n'
jpayne@69 12679 'The operations in the following table are supported by most '
jpayne@69 12680 'sequence\n'
jpayne@69 12681 'types, both mutable and immutable. The '
jpayne@69 12682 '"collections.abc.Sequence" ABC\n'
jpayne@69 12683 'is provided to make it easier to correctly implement these '
jpayne@69 12684 'operations\n'
jpayne@69 12685 'on custom sequence types.\n'
jpayne@69 12686 '\n'
jpayne@69 12687 'This table lists the sequence operations sorted in ascending '
jpayne@69 12688 'priority.\n'
jpayne@69 12689 'In the table, *s* and *t* are sequences of the same type, *n*, '
jpayne@69 12690 '*i*,\n'
jpayne@69 12691 '*j* and *k* are integers and *x* is an arbitrary object that '
jpayne@69 12692 'meets any\n'
jpayne@69 12693 'type and value restrictions imposed by *s*.\n'
jpayne@69 12694 '\n'
jpayne@69 12695 'The "in" and "not in" operations have the same priorities as '
jpayne@69 12696 'the\n'
jpayne@69 12697 'comparison operations. The "+" (concatenation) and "*" '
jpayne@69 12698 '(repetition)\n'
jpayne@69 12699 'operations have the same priority as the corresponding numeric\n'
jpayne@69 12700 'operations. [3]\n'
jpayne@69 12701 '\n'
jpayne@69 12702 '+----------------------------+----------------------------------+------------+\n'
jpayne@69 12703 '| Operation | Result '
jpayne@69 12704 '| Notes |\n'
jpayne@69 12705 '|============================|==================================|============|\n'
jpayne@69 12706 '| "x in s" | "True" if an item of *s* is '
jpayne@69 12707 '| (1) |\n'
jpayne@69 12708 '| | equal to *x*, else "False" '
jpayne@69 12709 '| |\n'
jpayne@69 12710 '+----------------------------+----------------------------------+------------+\n'
jpayne@69 12711 '| "x not in s" | "False" if an item of *s* is '
jpayne@69 12712 '| (1) |\n'
jpayne@69 12713 '| | equal to *x*, else "True" '
jpayne@69 12714 '| |\n'
jpayne@69 12715 '+----------------------------+----------------------------------+------------+\n'
jpayne@69 12716 '| "s + t" | the concatenation of *s* and *t* '
jpayne@69 12717 '| (6)(7) |\n'
jpayne@69 12718 '+----------------------------+----------------------------------+------------+\n'
jpayne@69 12719 '| "s * n" or "n * s" | equivalent to adding *s* to '
jpayne@69 12720 '| (2)(7) |\n'
jpayne@69 12721 '| | itself *n* times '
jpayne@69 12722 '| |\n'
jpayne@69 12723 '+----------------------------+----------------------------------+------------+\n'
jpayne@69 12724 '| "s[i]" | *i*th item of *s*, origin 0 '
jpayne@69 12725 '| (3) |\n'
jpayne@69 12726 '+----------------------------+----------------------------------+------------+\n'
jpayne@69 12727 '| "s[i:j]" | slice of *s* from *i* to *j* '
jpayne@69 12728 '| (3)(4) |\n'
jpayne@69 12729 '+----------------------------+----------------------------------+------------+\n'
jpayne@69 12730 '| "s[i:j:k]" | slice of *s* from *i* to *j* '
jpayne@69 12731 '| (3)(5) |\n'
jpayne@69 12732 '| | with step *k* '
jpayne@69 12733 '| |\n'
jpayne@69 12734 '+----------------------------+----------------------------------+------------+\n'
jpayne@69 12735 '| "len(s)" | length of *s* '
jpayne@69 12736 '| |\n'
jpayne@69 12737 '+----------------------------+----------------------------------+------------+\n'
jpayne@69 12738 '| "min(s)" | smallest item of *s* '
jpayne@69 12739 '| |\n'
jpayne@69 12740 '+----------------------------+----------------------------------+------------+\n'
jpayne@69 12741 '| "max(s)" | largest item of *s* '
jpayne@69 12742 '| |\n'
jpayne@69 12743 '+----------------------------+----------------------------------+------------+\n'
jpayne@69 12744 '| "s.index(x[, i[, j]])" | index of the first occurrence of '
jpayne@69 12745 '| (8) |\n'
jpayne@69 12746 '| | *x* in *s* (at or after index '
jpayne@69 12747 '| |\n'
jpayne@69 12748 '| | *i* and before index *j*) '
jpayne@69 12749 '| |\n'
jpayne@69 12750 '+----------------------------+----------------------------------+------------+\n'
jpayne@69 12751 '| "s.count(x)" | total number of occurrences of '
jpayne@69 12752 '| |\n'
jpayne@69 12753 '| | *x* in *s* '
jpayne@69 12754 '| |\n'
jpayne@69 12755 '+----------------------------+----------------------------------+------------+\n'
jpayne@69 12756 '\n'
jpayne@69 12757 'Sequences of the same type also support comparisons. In '
jpayne@69 12758 'particular,\n'
jpayne@69 12759 'tuples and lists are compared lexicographically by comparing\n'
jpayne@69 12760 'corresponding elements. This means that to compare equal, every\n'
jpayne@69 12761 'element must compare equal and the two sequences must be of the '
jpayne@69 12762 'same\n'
jpayne@69 12763 'type and have the same length. (For full details see '
jpayne@69 12764 'Comparisons in\n'
jpayne@69 12765 'the language reference.)\n'
jpayne@69 12766 '\n'
jpayne@69 12767 'Notes:\n'
jpayne@69 12768 '\n'
jpayne@69 12769 '1. While the "in" and "not in" operations are used only for '
jpayne@69 12770 'simple\n'
jpayne@69 12771 ' containment testing in the general case, some specialised '
jpayne@69 12772 'sequences\n'
jpayne@69 12773 ' (such as "str", "bytes" and "bytearray") also use them for\n'
jpayne@69 12774 ' subsequence testing:\n'
jpayne@69 12775 '\n'
jpayne@69 12776 ' >>> "gg" in "eggs"\n'
jpayne@69 12777 ' True\n'
jpayne@69 12778 '\n'
jpayne@69 12779 '2. Values of *n* less than "0" are treated as "0" (which yields '
jpayne@69 12780 'an\n'
jpayne@69 12781 ' empty sequence of the same type as *s*). Note that items in '
jpayne@69 12782 'the\n'
jpayne@69 12783 ' sequence *s* are not copied; they are referenced multiple '
jpayne@69 12784 'times.\n'
jpayne@69 12785 ' This often haunts new Python programmers; consider:\n'
jpayne@69 12786 '\n'
jpayne@69 12787 ' >>> lists = [[]] * 3\n'
jpayne@69 12788 ' >>> lists\n'
jpayne@69 12789 ' [[], [], []]\n'
jpayne@69 12790 ' >>> lists[0].append(3)\n'
jpayne@69 12791 ' >>> lists\n'
jpayne@69 12792 ' [[3], [3], [3]]\n'
jpayne@69 12793 '\n'
jpayne@69 12794 ' What has happened is that "[[]]" is a one-element list '
jpayne@69 12795 'containing\n'
jpayne@69 12796 ' an empty list, so all three elements of "[[]] * 3" are '
jpayne@69 12797 'references\n'
jpayne@69 12798 ' to this single empty list. Modifying any of the elements of\n'
jpayne@69 12799 ' "lists" modifies this single list. You can create a list of\n'
jpayne@69 12800 ' different lists this way:\n'
jpayne@69 12801 '\n'
jpayne@69 12802 ' >>> lists = [[] for i in range(3)]\n'
jpayne@69 12803 ' >>> lists[0].append(3)\n'
jpayne@69 12804 ' >>> lists[1].append(5)\n'
jpayne@69 12805 ' >>> lists[2].append(7)\n'
jpayne@69 12806 ' >>> lists\n'
jpayne@69 12807 ' [[3], [5], [7]]\n'
jpayne@69 12808 '\n'
jpayne@69 12809 ' Further explanation is available in the FAQ entry How do I '
jpayne@69 12810 'create a\n'
jpayne@69 12811 ' multidimensional list?.\n'
jpayne@69 12812 '\n'
jpayne@69 12813 '3. If *i* or *j* is negative, the index is relative to the end '
jpayne@69 12814 'of\n'
jpayne@69 12815 ' sequence *s*: "len(s) + i" or "len(s) + j" is substituted. '
jpayne@69 12816 'But\n'
jpayne@69 12817 ' note that "-0" is still "0".\n'
jpayne@69 12818 '\n'
jpayne@69 12819 '4. The slice of *s* from *i* to *j* is defined as the sequence '
jpayne@69 12820 'of\n'
jpayne@69 12821 ' items with index *k* such that "i <= k < j". If *i* or *j* '
jpayne@69 12822 'is\n'
jpayne@69 12823 ' greater than "len(s)", use "len(s)". If *i* is omitted or '
jpayne@69 12824 '"None",\n'
jpayne@69 12825 ' use "0". If *j* is omitted or "None", use "len(s)". If *i* '
jpayne@69 12826 'is\n'
jpayne@69 12827 ' greater than or equal to *j*, the slice is empty.\n'
jpayne@69 12828 '\n'
jpayne@69 12829 '5. The slice of *s* from *i* to *j* with step *k* is defined as '
jpayne@69 12830 'the\n'
jpayne@69 12831 ' sequence of items with index "x = i + n*k" such that "0 <= n '
jpayne@69 12832 '<\n'
jpayne@69 12833 ' (j-i)/k". In other words, the indices are "i", "i+k", '
jpayne@69 12834 '"i+2*k",\n'
jpayne@69 12835 ' "i+3*k" and so on, stopping when *j* is reached (but never\n'
jpayne@69 12836 ' including *j*). When *k* is positive, *i* and *j* are '
jpayne@69 12837 'reduced to\n'
jpayne@69 12838 ' "len(s)" if they are greater. When *k* is negative, *i* and '
jpayne@69 12839 '*j* are\n'
jpayne@69 12840 ' reduced to "len(s) - 1" if they are greater. If *i* or *j* '
jpayne@69 12841 'are\n'
jpayne@69 12842 ' omitted or "None", they become “end” values (which end '
jpayne@69 12843 'depends on\n'
jpayne@69 12844 ' the sign of *k*). Note, *k* cannot be zero. If *k* is '
jpayne@69 12845 '"None", it\n'
jpayne@69 12846 ' is treated like "1".\n'
jpayne@69 12847 '\n'
jpayne@69 12848 '6. Concatenating immutable sequences always results in a new\n'
jpayne@69 12849 ' object. This means that building up a sequence by repeated\n'
jpayne@69 12850 ' concatenation will have a quadratic runtime cost in the '
jpayne@69 12851 'total\n'
jpayne@69 12852 ' sequence length. To get a linear runtime cost, you must '
jpayne@69 12853 'switch to\n'
jpayne@69 12854 ' one of the alternatives below:\n'
jpayne@69 12855 '\n'
jpayne@69 12856 ' * if concatenating "str" objects, you can build a list and '
jpayne@69 12857 'use\n'
jpayne@69 12858 ' "str.join()" at the end or else write to an "io.StringIO"\n'
jpayne@69 12859 ' instance and retrieve its value when complete\n'
jpayne@69 12860 '\n'
jpayne@69 12861 ' * if concatenating "bytes" objects, you can similarly use\n'
jpayne@69 12862 ' "bytes.join()" or "io.BytesIO", or you can do in-place\n'
jpayne@69 12863 ' concatenation with a "bytearray" object. "bytearray" '
jpayne@69 12864 'objects are\n'
jpayne@69 12865 ' mutable and have an efficient overallocation mechanism\n'
jpayne@69 12866 '\n'
jpayne@69 12867 ' * if concatenating "tuple" objects, extend a "list" instead\n'
jpayne@69 12868 '\n'
jpayne@69 12869 ' * for other types, investigate the relevant class '
jpayne@69 12870 'documentation\n'
jpayne@69 12871 '\n'
jpayne@69 12872 '7. Some sequence types (such as "range") only support item\n'
jpayne@69 12873 ' sequences that follow specific patterns, and hence don’t '
jpayne@69 12874 'support\n'
jpayne@69 12875 ' sequence concatenation or repetition.\n'
jpayne@69 12876 '\n'
jpayne@69 12877 '8. "index" raises "ValueError" when *x* is not found in *s*. '
jpayne@69 12878 'Not\n'
jpayne@69 12879 ' all implementations support passing the additional arguments '
jpayne@69 12880 '*i*\n'
jpayne@69 12881 ' and *j*. These arguments allow efficient searching of '
jpayne@69 12882 'subsections\n'
jpayne@69 12883 ' of the sequence. Passing the extra arguments is roughly '
jpayne@69 12884 'equivalent\n'
jpayne@69 12885 ' to using "s[i:j].index(x)", only without copying any data and '
jpayne@69 12886 'with\n'
jpayne@69 12887 ' the returned index being relative to the start of the '
jpayne@69 12888 'sequence\n'
jpayne@69 12889 ' rather than the start of the slice.\n'
jpayne@69 12890 '\n'
jpayne@69 12891 '\n'
jpayne@69 12892 'Immutable Sequence Types\n'
jpayne@69 12893 '========================\n'
jpayne@69 12894 '\n'
jpayne@69 12895 'The only operation that immutable sequence types generally '
jpayne@69 12896 'implement\n'
jpayne@69 12897 'that is not also implemented by mutable sequence types is '
jpayne@69 12898 'support for\n'
jpayne@69 12899 'the "hash()" built-in.\n'
jpayne@69 12900 '\n'
jpayne@69 12901 'This support allows immutable sequences, such as "tuple" '
jpayne@69 12902 'instances, to\n'
jpayne@69 12903 'be used as "dict" keys and stored in "set" and "frozenset" '
jpayne@69 12904 'instances.\n'
jpayne@69 12905 '\n'
jpayne@69 12906 'Attempting to hash an immutable sequence that contains '
jpayne@69 12907 'unhashable\n'
jpayne@69 12908 'values will result in "TypeError".\n'
jpayne@69 12909 '\n'
jpayne@69 12910 '\n'
jpayne@69 12911 'Mutable Sequence Types\n'
jpayne@69 12912 '======================\n'
jpayne@69 12913 '\n'
jpayne@69 12914 'The operations in the following table are defined on mutable '
jpayne@69 12915 'sequence\n'
jpayne@69 12916 'types. The "collections.abc.MutableSequence" ABC is provided to '
jpayne@69 12917 'make\n'
jpayne@69 12918 'it easier to correctly implement these operations on custom '
jpayne@69 12919 'sequence\n'
jpayne@69 12920 'types.\n'
jpayne@69 12921 '\n'
jpayne@69 12922 'In the table *s* is an instance of a mutable sequence type, *t* '
jpayne@69 12923 'is any\n'
jpayne@69 12924 'iterable object and *x* is an arbitrary object that meets any '
jpayne@69 12925 'type and\n'
jpayne@69 12926 'value restrictions imposed by *s* (for example, "bytearray" '
jpayne@69 12927 'only\n'
jpayne@69 12928 'accepts integers that meet the value restriction "0 <= x <= '
jpayne@69 12929 '255").\n'
jpayne@69 12930 '\n'
jpayne@69 12931 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 12932 '| Operation | '
jpayne@69 12933 'Result | Notes |\n'
jpayne@69 12934 '|================================|==================================|=======================|\n'
jpayne@69 12935 '| "s[i] = x" | item *i* of *s* is replaced '
jpayne@69 12936 'by | |\n'
jpayne@69 12937 '| | '
jpayne@69 12938 '*x* | |\n'
jpayne@69 12939 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 12940 '| "s[i:j] = t" | slice of *s* from *i* to *j* '
jpayne@69 12941 'is | |\n'
jpayne@69 12942 '| | replaced by the contents of '
jpayne@69 12943 'the | |\n'
jpayne@69 12944 '| | iterable '
jpayne@69 12945 '*t* | |\n'
jpayne@69 12946 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 12947 '| "del s[i:j]" | same as "s[i:j] = '
jpayne@69 12948 '[]" | |\n'
jpayne@69 12949 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 12950 '| "s[i:j:k] = t" | the elements of "s[i:j:k]" '
jpayne@69 12951 'are | (1) |\n'
jpayne@69 12952 '| | replaced by those of '
jpayne@69 12953 '*t* | |\n'
jpayne@69 12954 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 12955 '| "del s[i:j:k]" | removes the elements '
jpayne@69 12956 'of | |\n'
jpayne@69 12957 '| | "s[i:j:k]" from the '
jpayne@69 12958 'list | |\n'
jpayne@69 12959 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 12960 '| "s.append(x)" | appends *x* to the end of '
jpayne@69 12961 'the | |\n'
jpayne@69 12962 '| | sequence (same '
jpayne@69 12963 'as | |\n'
jpayne@69 12964 '| | "s[len(s):len(s)] = '
jpayne@69 12965 '[x]") | |\n'
jpayne@69 12966 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 12967 '| "s.clear()" | removes all items from *s* '
jpayne@69 12968 '(same | (5) |\n'
jpayne@69 12969 '| | as "del '
jpayne@69 12970 's[:]") | |\n'
jpayne@69 12971 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 12972 '| "s.copy()" | creates a shallow copy of '
jpayne@69 12973 '*s* | (5) |\n'
jpayne@69 12974 '| | (same as '
jpayne@69 12975 '"s[:]") | |\n'
jpayne@69 12976 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 12977 '| "s.extend(t)" or "s += t" | extends *s* with the contents '
jpayne@69 12978 'of | |\n'
jpayne@69 12979 '| | *t* (for the most part the '
jpayne@69 12980 'same | |\n'
jpayne@69 12981 '| | as "s[len(s):len(s)] = '
jpayne@69 12982 't") | |\n'
jpayne@69 12983 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 12984 '| "s *= n" | updates *s* with its '
jpayne@69 12985 'contents | (6) |\n'
jpayne@69 12986 '| | repeated *n* '
jpayne@69 12987 'times | |\n'
jpayne@69 12988 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 12989 '| "s.insert(i, x)" | inserts *x* into *s* at '
jpayne@69 12990 'the | |\n'
jpayne@69 12991 '| | index given by *i* (same '
jpayne@69 12992 'as | |\n'
jpayne@69 12993 '| | "s[i:i] = '
jpayne@69 12994 '[x]") | |\n'
jpayne@69 12995 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 12996 '| "s.pop([i])" | retrieves the item at *i* '
jpayne@69 12997 'and | (2) |\n'
jpayne@69 12998 '| | also removes it from '
jpayne@69 12999 '*s* | |\n'
jpayne@69 13000 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13001 '| "s.remove(x)" | remove the first item from '
jpayne@69 13002 '*s* | (3) |\n'
jpayne@69 13003 '| | where "s[i]" is equal to '
jpayne@69 13004 '*x* | |\n'
jpayne@69 13005 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13006 '| "s.reverse()" | reverses the items of *s* '
jpayne@69 13007 'in | (4) |\n'
jpayne@69 13008 '| | '
jpayne@69 13009 'place | |\n'
jpayne@69 13010 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13011 '\n'
jpayne@69 13012 'Notes:\n'
jpayne@69 13013 '\n'
jpayne@69 13014 '1. *t* must have the same length as the slice it is replacing.\n'
jpayne@69 13015 '\n'
jpayne@69 13016 '2. The optional argument *i* defaults to "-1", so that by '
jpayne@69 13017 'default\n'
jpayne@69 13018 ' the last item is removed and returned.\n'
jpayne@69 13019 '\n'
jpayne@69 13020 '3. "remove()" raises "ValueError" when *x* is not found in *s*.\n'
jpayne@69 13021 '\n'
jpayne@69 13022 '4. The "reverse()" method modifies the sequence in place for\n'
jpayne@69 13023 ' economy of space when reversing a large sequence. To remind '
jpayne@69 13024 'users\n'
jpayne@69 13025 ' that it operates by side effect, it does not return the '
jpayne@69 13026 'reversed\n'
jpayne@69 13027 ' sequence.\n'
jpayne@69 13028 '\n'
jpayne@69 13029 '5. "clear()" and "copy()" are included for consistency with the\n'
jpayne@69 13030 ' interfaces of mutable containers that don’t support slicing\n'
jpayne@69 13031 ' operations (such as "dict" and "set"). "copy()" is not part '
jpayne@69 13032 'of the\n'
jpayne@69 13033 ' "collections.abc.MutableSequence" ABC, but most concrete '
jpayne@69 13034 'mutable\n'
jpayne@69 13035 ' sequence classes provide it.\n'
jpayne@69 13036 '\n'
jpayne@69 13037 ' New in version 3.3: "clear()" and "copy()" methods.\n'
jpayne@69 13038 '\n'
jpayne@69 13039 '6. The value *n* is an integer, or an object implementing\n'
jpayne@69 13040 ' "__index__()". Zero and negative values of *n* clear the '
jpayne@69 13041 'sequence.\n'
jpayne@69 13042 ' Items in the sequence are not copied; they are referenced '
jpayne@69 13043 'multiple\n'
jpayne@69 13044 ' times, as explained for "s * n" under Common Sequence '
jpayne@69 13045 'Operations.\n'
jpayne@69 13046 '\n'
jpayne@69 13047 '\n'
jpayne@69 13048 'Lists\n'
jpayne@69 13049 '=====\n'
jpayne@69 13050 '\n'
jpayne@69 13051 'Lists are mutable sequences, typically used to store collections '
jpayne@69 13052 'of\n'
jpayne@69 13053 'homogeneous items (where the precise degree of similarity will '
jpayne@69 13054 'vary by\n'
jpayne@69 13055 'application).\n'
jpayne@69 13056 '\n'
jpayne@69 13057 'class list([iterable])\n'
jpayne@69 13058 '\n'
jpayne@69 13059 ' Lists may be constructed in several ways:\n'
jpayne@69 13060 '\n'
jpayne@69 13061 ' * Using a pair of square brackets to denote the empty list: '
jpayne@69 13062 '"[]"\n'
jpayne@69 13063 '\n'
jpayne@69 13064 ' * Using square brackets, separating items with commas: '
jpayne@69 13065 '"[a]",\n'
jpayne@69 13066 ' "[a, b, c]"\n'
jpayne@69 13067 '\n'
jpayne@69 13068 ' * Using a list comprehension: "[x for x in iterable]"\n'
jpayne@69 13069 '\n'
jpayne@69 13070 ' * Using the type constructor: "list()" or "list(iterable)"\n'
jpayne@69 13071 '\n'
jpayne@69 13072 ' The constructor builds a list whose items are the same and in '
jpayne@69 13073 'the\n'
jpayne@69 13074 ' same order as *iterable*’s items. *iterable* may be either '
jpayne@69 13075 'a\n'
jpayne@69 13076 ' sequence, a container that supports iteration, or an '
jpayne@69 13077 'iterator\n'
jpayne@69 13078 ' object. If *iterable* is already a list, a copy is made and\n'
jpayne@69 13079 ' returned, similar to "iterable[:]". For example, '
jpayne@69 13080 '"list(\'abc\')"\n'
jpayne@69 13081 ' returns "[\'a\', \'b\', \'c\']" and "list( (1, 2, 3) )" '
jpayne@69 13082 'returns "[1, 2,\n'
jpayne@69 13083 ' 3]". If no argument is given, the constructor creates a new '
jpayne@69 13084 'empty\n'
jpayne@69 13085 ' list, "[]".\n'
jpayne@69 13086 '\n'
jpayne@69 13087 ' Many other operations also produce lists, including the '
jpayne@69 13088 '"sorted()"\n'
jpayne@69 13089 ' built-in.\n'
jpayne@69 13090 '\n'
jpayne@69 13091 ' Lists implement all of the common and mutable sequence '
jpayne@69 13092 'operations.\n'
jpayne@69 13093 ' Lists also provide the following additional method:\n'
jpayne@69 13094 '\n'
jpayne@69 13095 ' sort(*, key=None, reverse=False)\n'
jpayne@69 13096 '\n'
jpayne@69 13097 ' This method sorts the list in place, using only "<" '
jpayne@69 13098 'comparisons\n'
jpayne@69 13099 ' between items. Exceptions are not suppressed - if any '
jpayne@69 13100 'comparison\n'
jpayne@69 13101 ' operations fail, the entire sort operation will fail (and '
jpayne@69 13102 'the\n'
jpayne@69 13103 ' list will likely be left in a partially modified state).\n'
jpayne@69 13104 '\n'
jpayne@69 13105 ' "sort()" accepts two arguments that can only be passed by\n'
jpayne@69 13106 ' keyword (keyword-only arguments):\n'
jpayne@69 13107 '\n'
jpayne@69 13108 ' *key* specifies a function of one argument that is used '
jpayne@69 13109 'to\n'
jpayne@69 13110 ' extract a comparison key from each list element (for '
jpayne@69 13111 'example,\n'
jpayne@69 13112 ' "key=str.lower"). The key corresponding to each item in '
jpayne@69 13113 'the list\n'
jpayne@69 13114 ' is calculated once and then used for the entire sorting '
jpayne@69 13115 'process.\n'
jpayne@69 13116 ' The default value of "None" means that list items are '
jpayne@69 13117 'sorted\n'
jpayne@69 13118 ' directly without calculating a separate key value.\n'
jpayne@69 13119 '\n'
jpayne@69 13120 ' The "functools.cmp_to_key()" utility is available to '
jpayne@69 13121 'convert a\n'
jpayne@69 13122 ' 2.x style *cmp* function to a *key* function.\n'
jpayne@69 13123 '\n'
jpayne@69 13124 ' *reverse* is a boolean value. If set to "True", then the '
jpayne@69 13125 'list\n'
jpayne@69 13126 ' elements are sorted as if each comparison were reversed.\n'
jpayne@69 13127 '\n'
jpayne@69 13128 ' This method modifies the sequence in place for economy of '
jpayne@69 13129 'space\n'
jpayne@69 13130 ' when sorting a large sequence. To remind users that it '
jpayne@69 13131 'operates\n'
jpayne@69 13132 ' by side effect, it does not return the sorted sequence '
jpayne@69 13133 '(use\n'
jpayne@69 13134 ' "sorted()" to explicitly request a new sorted list '
jpayne@69 13135 'instance).\n'
jpayne@69 13136 '\n'
jpayne@69 13137 ' The "sort()" method is guaranteed to be stable. A sort '
jpayne@69 13138 'is\n'
jpayne@69 13139 ' stable if it guarantees not to change the relative order '
jpayne@69 13140 'of\n'
jpayne@69 13141 ' elements that compare equal — this is helpful for sorting '
jpayne@69 13142 'in\n'
jpayne@69 13143 ' multiple passes (for example, sort by department, then by '
jpayne@69 13144 'salary\n'
jpayne@69 13145 ' grade).\n'
jpayne@69 13146 '\n'
jpayne@69 13147 ' For sorting examples and a brief sorting tutorial, see '
jpayne@69 13148 'Sorting\n'
jpayne@69 13149 ' HOW TO.\n'
jpayne@69 13150 '\n'
jpayne@69 13151 ' **CPython implementation detail:** While a list is being '
jpayne@69 13152 'sorted,\n'
jpayne@69 13153 ' the effect of attempting to mutate, or even inspect, the '
jpayne@69 13154 'list is\n'
jpayne@69 13155 ' undefined. The C implementation of Python makes the list '
jpayne@69 13156 'appear\n'
jpayne@69 13157 ' empty for the duration, and raises "ValueError" if it can '
jpayne@69 13158 'detect\n'
jpayne@69 13159 ' that the list has been mutated during a sort.\n'
jpayne@69 13160 '\n'
jpayne@69 13161 '\n'
jpayne@69 13162 'Tuples\n'
jpayne@69 13163 '======\n'
jpayne@69 13164 '\n'
jpayne@69 13165 'Tuples are immutable sequences, typically used to store '
jpayne@69 13166 'collections of\n'
jpayne@69 13167 'heterogeneous data (such as the 2-tuples produced by the '
jpayne@69 13168 '"enumerate()"\n'
jpayne@69 13169 'built-in). Tuples are also used for cases where an immutable '
jpayne@69 13170 'sequence\n'
jpayne@69 13171 'of homogeneous data is needed (such as allowing storage in a '
jpayne@69 13172 '"set" or\n'
jpayne@69 13173 '"dict" instance).\n'
jpayne@69 13174 '\n'
jpayne@69 13175 'class tuple([iterable])\n'
jpayne@69 13176 '\n'
jpayne@69 13177 ' Tuples may be constructed in a number of ways:\n'
jpayne@69 13178 '\n'
jpayne@69 13179 ' * Using a pair of parentheses to denote the empty tuple: '
jpayne@69 13180 '"()"\n'
jpayne@69 13181 '\n'
jpayne@69 13182 ' * Using a trailing comma for a singleton tuple: "a," or '
jpayne@69 13183 '"(a,)"\n'
jpayne@69 13184 '\n'
jpayne@69 13185 ' * Separating items with commas: "a, b, c" or "(a, b, c)"\n'
jpayne@69 13186 '\n'
jpayne@69 13187 ' * Using the "tuple()" built-in: "tuple()" or '
jpayne@69 13188 '"tuple(iterable)"\n'
jpayne@69 13189 '\n'
jpayne@69 13190 ' The constructor builds a tuple whose items are the same and '
jpayne@69 13191 'in the\n'
jpayne@69 13192 ' same order as *iterable*’s items. *iterable* may be either '
jpayne@69 13193 'a\n'
jpayne@69 13194 ' sequence, a container that supports iteration, or an '
jpayne@69 13195 'iterator\n'
jpayne@69 13196 ' object. If *iterable* is already a tuple, it is returned\n'
jpayne@69 13197 ' unchanged. For example, "tuple(\'abc\')" returns "(\'a\', '
jpayne@69 13198 '\'b\', \'c\')"\n'
jpayne@69 13199 ' and "tuple( [1, 2, 3] )" returns "(1, 2, 3)". If no argument '
jpayne@69 13200 'is\n'
jpayne@69 13201 ' given, the constructor creates a new empty tuple, "()".\n'
jpayne@69 13202 '\n'
jpayne@69 13203 ' Note that it is actually the comma which makes a tuple, not '
jpayne@69 13204 'the\n'
jpayne@69 13205 ' parentheses. The parentheses are optional, except in the '
jpayne@69 13206 'empty\n'
jpayne@69 13207 ' tuple case, or when they are needed to avoid syntactic '
jpayne@69 13208 'ambiguity.\n'
jpayne@69 13209 ' For example, "f(a, b, c)" is a function call with three '
jpayne@69 13210 'arguments,\n'
jpayne@69 13211 ' while "f((a, b, c))" is a function call with a 3-tuple as the '
jpayne@69 13212 'sole\n'
jpayne@69 13213 ' argument.\n'
jpayne@69 13214 '\n'
jpayne@69 13215 ' Tuples implement all of the common sequence operations.\n'
jpayne@69 13216 '\n'
jpayne@69 13217 'For heterogeneous collections of data where access by name is '
jpayne@69 13218 'clearer\n'
jpayne@69 13219 'than access by index, "collections.namedtuple()" may be a more\n'
jpayne@69 13220 'appropriate choice than a simple tuple object.\n'
jpayne@69 13221 '\n'
jpayne@69 13222 '\n'
jpayne@69 13223 'Ranges\n'
jpayne@69 13224 '======\n'
jpayne@69 13225 '\n'
jpayne@69 13226 'The "range" type represents an immutable sequence of numbers and '
jpayne@69 13227 'is\n'
jpayne@69 13228 'commonly used for looping a specific number of times in "for" '
jpayne@69 13229 'loops.\n'
jpayne@69 13230 '\n'
jpayne@69 13231 'class range(stop)\n'
jpayne@69 13232 'class range(start, stop[, step])\n'
jpayne@69 13233 '\n'
jpayne@69 13234 ' The arguments to the range constructor must be integers '
jpayne@69 13235 '(either\n'
jpayne@69 13236 ' built-in "int" or any object that implements the "__index__"\n'
jpayne@69 13237 ' special method). If the *step* argument is omitted, it '
jpayne@69 13238 'defaults to\n'
jpayne@69 13239 ' "1". If the *start* argument is omitted, it defaults to "0". '
jpayne@69 13240 'If\n'
jpayne@69 13241 ' *step* is zero, "ValueError" is raised.\n'
jpayne@69 13242 '\n'
jpayne@69 13243 ' For a positive *step*, the contents of a range "r" are '
jpayne@69 13244 'determined\n'
jpayne@69 13245 ' by the formula "r[i] = start + step*i" where "i >= 0" and '
jpayne@69 13246 '"r[i] <\n'
jpayne@69 13247 ' stop".\n'
jpayne@69 13248 '\n'
jpayne@69 13249 ' For a negative *step*, the contents of the range are still\n'
jpayne@69 13250 ' determined by the formula "r[i] = start + step*i", but the\n'
jpayne@69 13251 ' constraints are "i >= 0" and "r[i] > stop".\n'
jpayne@69 13252 '\n'
jpayne@69 13253 ' A range object will be empty if "r[0]" does not meet the '
jpayne@69 13254 'value\n'
jpayne@69 13255 ' constraint. Ranges do support negative indices, but these '
jpayne@69 13256 'are\n'
jpayne@69 13257 ' interpreted as indexing from the end of the sequence '
jpayne@69 13258 'determined by\n'
jpayne@69 13259 ' the positive indices.\n'
jpayne@69 13260 '\n'
jpayne@69 13261 ' Ranges containing absolute values larger than "sys.maxsize" '
jpayne@69 13262 'are\n'
jpayne@69 13263 ' permitted but some features (such as "len()") may raise\n'
jpayne@69 13264 ' "OverflowError".\n'
jpayne@69 13265 '\n'
jpayne@69 13266 ' Range examples:\n'
jpayne@69 13267 '\n'
jpayne@69 13268 ' >>> list(range(10))\n'
jpayne@69 13269 ' [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n'
jpayne@69 13270 ' >>> list(range(1, 11))\n'
jpayne@69 13271 ' [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n'
jpayne@69 13272 ' >>> list(range(0, 30, 5))\n'
jpayne@69 13273 ' [0, 5, 10, 15, 20, 25]\n'
jpayne@69 13274 ' >>> list(range(0, 10, 3))\n'
jpayne@69 13275 ' [0, 3, 6, 9]\n'
jpayne@69 13276 ' >>> list(range(0, -10, -1))\n'
jpayne@69 13277 ' [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]\n'
jpayne@69 13278 ' >>> list(range(0))\n'
jpayne@69 13279 ' []\n'
jpayne@69 13280 ' >>> list(range(1, 0))\n'
jpayne@69 13281 ' []\n'
jpayne@69 13282 '\n'
jpayne@69 13283 ' Ranges implement all of the common sequence operations '
jpayne@69 13284 'except\n'
jpayne@69 13285 ' concatenation and repetition (due to the fact that range '
jpayne@69 13286 'objects\n'
jpayne@69 13287 ' can only represent sequences that follow a strict pattern '
jpayne@69 13288 'and\n'
jpayne@69 13289 ' repetition and concatenation will usually violate that '
jpayne@69 13290 'pattern).\n'
jpayne@69 13291 '\n'
jpayne@69 13292 ' start\n'
jpayne@69 13293 '\n'
jpayne@69 13294 ' The value of the *start* parameter (or "0" if the '
jpayne@69 13295 'parameter was\n'
jpayne@69 13296 ' not supplied)\n'
jpayne@69 13297 '\n'
jpayne@69 13298 ' stop\n'
jpayne@69 13299 '\n'
jpayne@69 13300 ' The value of the *stop* parameter\n'
jpayne@69 13301 '\n'
jpayne@69 13302 ' step\n'
jpayne@69 13303 '\n'
jpayne@69 13304 ' The value of the *step* parameter (or "1" if the parameter '
jpayne@69 13305 'was\n'
jpayne@69 13306 ' not supplied)\n'
jpayne@69 13307 '\n'
jpayne@69 13308 'The advantage of the "range" type over a regular "list" or '
jpayne@69 13309 '"tuple" is\n'
jpayne@69 13310 'that a "range" object will always take the same (small) amount '
jpayne@69 13311 'of\n'
jpayne@69 13312 'memory, no matter the size of the range it represents (as it '
jpayne@69 13313 'only\n'
jpayne@69 13314 'stores the "start", "stop" and "step" values, calculating '
jpayne@69 13315 'individual\n'
jpayne@69 13316 'items and subranges as needed).\n'
jpayne@69 13317 '\n'
jpayne@69 13318 'Range objects implement the "collections.abc.Sequence" ABC, and\n'
jpayne@69 13319 'provide features such as containment tests, element index '
jpayne@69 13320 'lookup,\n'
jpayne@69 13321 'slicing and support for negative indices (see Sequence Types — '
jpayne@69 13322 'list,\n'
jpayne@69 13323 'tuple, range):\n'
jpayne@69 13324 '\n'
jpayne@69 13325 '>>> r = range(0, 20, 2)\n'
jpayne@69 13326 '>>> r\n'
jpayne@69 13327 'range(0, 20, 2)\n'
jpayne@69 13328 '>>> 11 in r\n'
jpayne@69 13329 'False\n'
jpayne@69 13330 '>>> 10 in r\n'
jpayne@69 13331 'True\n'
jpayne@69 13332 '>>> r.index(10)\n'
jpayne@69 13333 '5\n'
jpayne@69 13334 '>>> r[5]\n'
jpayne@69 13335 '10\n'
jpayne@69 13336 '>>> r[:5]\n'
jpayne@69 13337 'range(0, 10, 2)\n'
jpayne@69 13338 '>>> r[-1]\n'
jpayne@69 13339 '18\n'
jpayne@69 13340 '\n'
jpayne@69 13341 'Testing range objects for equality with "==" and "!=" compares '
jpayne@69 13342 'them as\n'
jpayne@69 13343 'sequences. That is, two range objects are considered equal if '
jpayne@69 13344 'they\n'
jpayne@69 13345 'represent the same sequence of values. (Note that two range '
jpayne@69 13346 'objects\n'
jpayne@69 13347 'that compare equal might have different "start", "stop" and '
jpayne@69 13348 '"step"\n'
jpayne@69 13349 'attributes, for example "range(0) == range(2, 1, 3)" or '
jpayne@69 13350 '"range(0, 3,\n'
jpayne@69 13351 '2) == range(0, 4, 2)".)\n'
jpayne@69 13352 '\n'
jpayne@69 13353 'Changed in version 3.2: Implement the Sequence ABC. Support '
jpayne@69 13354 'slicing\n'
jpayne@69 13355 'and negative indices. Test "int" objects for membership in '
jpayne@69 13356 'constant\n'
jpayne@69 13357 'time instead of iterating through all items.\n'
jpayne@69 13358 '\n'
jpayne@69 13359 'Changed in version 3.3: Define ‘==’ and ‘!=’ to compare range '
jpayne@69 13360 'objects\n'
jpayne@69 13361 'based on the sequence of values they define (instead of '
jpayne@69 13362 'comparing\n'
jpayne@69 13363 'based on object identity).\n'
jpayne@69 13364 '\n'
jpayne@69 13365 'New in version 3.3: The "start", "stop" and "step" attributes.\n'
jpayne@69 13366 '\n'
jpayne@69 13367 'See also:\n'
jpayne@69 13368 '\n'
jpayne@69 13369 ' * The linspace recipe shows how to implement a lazy version '
jpayne@69 13370 'of\n'
jpayne@69 13371 ' range suitable for floating point applications.\n',
jpayne@69 13372 'typesseq-mutable': 'Mutable Sequence Types\n'
jpayne@69 13373 '**********************\n'
jpayne@69 13374 '\n'
jpayne@69 13375 'The operations in the following table are defined on '
jpayne@69 13376 'mutable sequence\n'
jpayne@69 13377 'types. The "collections.abc.MutableSequence" ABC is '
jpayne@69 13378 'provided to make\n'
jpayne@69 13379 'it easier to correctly implement these operations on '
jpayne@69 13380 'custom sequence\n'
jpayne@69 13381 'types.\n'
jpayne@69 13382 '\n'
jpayne@69 13383 'In the table *s* is an instance of a mutable sequence '
jpayne@69 13384 'type, *t* is any\n'
jpayne@69 13385 'iterable object and *x* is an arbitrary object that '
jpayne@69 13386 'meets any type and\n'
jpayne@69 13387 'value restrictions imposed by *s* (for example, '
jpayne@69 13388 '"bytearray" only\n'
jpayne@69 13389 'accepts integers that meet the value restriction "0 <= x '
jpayne@69 13390 '<= 255").\n'
jpayne@69 13391 '\n'
jpayne@69 13392 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13393 '| Operation | '
jpayne@69 13394 'Result | Notes '
jpayne@69 13395 '|\n'
jpayne@69 13396 '|================================|==================================|=======================|\n'
jpayne@69 13397 '| "s[i] = x" | item *i* of *s* is '
jpayne@69 13398 'replaced by | |\n'
jpayne@69 13399 '| | '
jpayne@69 13400 '*x* | '
jpayne@69 13401 '|\n'
jpayne@69 13402 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13403 '| "s[i:j] = t" | slice of *s* from *i* '
jpayne@69 13404 'to *j* is | |\n'
jpayne@69 13405 '| | replaced by the '
jpayne@69 13406 'contents of the | |\n'
jpayne@69 13407 '| | iterable '
jpayne@69 13408 '*t* | |\n'
jpayne@69 13409 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13410 '| "del s[i:j]" | same as "s[i:j] = '
jpayne@69 13411 '[]" | |\n'
jpayne@69 13412 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13413 '| "s[i:j:k] = t" | the elements of '
jpayne@69 13414 '"s[i:j:k]" are | (1) |\n'
jpayne@69 13415 '| | replaced by those of '
jpayne@69 13416 '*t* | |\n'
jpayne@69 13417 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13418 '| "del s[i:j:k]" | removes the elements '
jpayne@69 13419 'of | |\n'
jpayne@69 13420 '| | "s[i:j:k]" from the '
jpayne@69 13421 'list | |\n'
jpayne@69 13422 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13423 '| "s.append(x)" | appends *x* to the '
jpayne@69 13424 'end of the | |\n'
jpayne@69 13425 '| | sequence (same '
jpayne@69 13426 'as | |\n'
jpayne@69 13427 '| | "s[len(s):len(s)] = '
jpayne@69 13428 '[x]") | |\n'
jpayne@69 13429 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13430 '| "s.clear()" | removes all items '
jpayne@69 13431 'from *s* (same | (5) |\n'
jpayne@69 13432 '| | as "del '
jpayne@69 13433 's[:]") | |\n'
jpayne@69 13434 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13435 '| "s.copy()" | creates a shallow '
jpayne@69 13436 'copy of *s* | (5) |\n'
jpayne@69 13437 '| | (same as '
jpayne@69 13438 '"s[:]") | |\n'
jpayne@69 13439 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13440 '| "s.extend(t)" or "s += t" | extends *s* with the '
jpayne@69 13441 'contents of | |\n'
jpayne@69 13442 '| | *t* (for the most '
jpayne@69 13443 'part the same | |\n'
jpayne@69 13444 '| | as "s[len(s):len(s)] '
jpayne@69 13445 '= t") | |\n'
jpayne@69 13446 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13447 '| "s *= n" | updates *s* with its '
jpayne@69 13448 'contents | (6) |\n'
jpayne@69 13449 '| | repeated *n* '
jpayne@69 13450 'times | |\n'
jpayne@69 13451 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13452 '| "s.insert(i, x)" | inserts *x* into *s* '
jpayne@69 13453 'at the | |\n'
jpayne@69 13454 '| | index given by *i* '
jpayne@69 13455 '(same as | |\n'
jpayne@69 13456 '| | "s[i:i] = '
jpayne@69 13457 '[x]") | |\n'
jpayne@69 13458 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13459 '| "s.pop([i])" | retrieves the item at '
jpayne@69 13460 '*i* and | (2) |\n'
jpayne@69 13461 '| | also removes it from '
jpayne@69 13462 '*s* | |\n'
jpayne@69 13463 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13464 '| "s.remove(x)" | remove the first item '
jpayne@69 13465 'from *s* | (3) |\n'
jpayne@69 13466 '| | where "s[i]" is equal '
jpayne@69 13467 'to *x* | |\n'
jpayne@69 13468 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13469 '| "s.reverse()" | reverses the items of '
jpayne@69 13470 '*s* in | (4) |\n'
jpayne@69 13471 '| | '
jpayne@69 13472 'place | '
jpayne@69 13473 '|\n'
jpayne@69 13474 '+--------------------------------+----------------------------------+-----------------------+\n'
jpayne@69 13475 '\n'
jpayne@69 13476 'Notes:\n'
jpayne@69 13477 '\n'
jpayne@69 13478 '1. *t* must have the same length as the slice it is '
jpayne@69 13479 'replacing.\n'
jpayne@69 13480 '\n'
jpayne@69 13481 '2. The optional argument *i* defaults to "-1", so that '
jpayne@69 13482 'by default\n'
jpayne@69 13483 ' the last item is removed and returned.\n'
jpayne@69 13484 '\n'
jpayne@69 13485 '3. "remove()" raises "ValueError" when *x* is not found '
jpayne@69 13486 'in *s*.\n'
jpayne@69 13487 '\n'
jpayne@69 13488 '4. The "reverse()" method modifies the sequence in place '
jpayne@69 13489 'for\n'
jpayne@69 13490 ' economy of space when reversing a large sequence. To '
jpayne@69 13491 'remind users\n'
jpayne@69 13492 ' that it operates by side effect, it does not return '
jpayne@69 13493 'the reversed\n'
jpayne@69 13494 ' sequence.\n'
jpayne@69 13495 '\n'
jpayne@69 13496 '5. "clear()" and "copy()" are included for consistency '
jpayne@69 13497 'with the\n'
jpayne@69 13498 ' interfaces of mutable containers that don’t support '
jpayne@69 13499 'slicing\n'
jpayne@69 13500 ' operations (such as "dict" and "set"). "copy()" is '
jpayne@69 13501 'not part of the\n'
jpayne@69 13502 ' "collections.abc.MutableSequence" ABC, but most '
jpayne@69 13503 'concrete mutable\n'
jpayne@69 13504 ' sequence classes provide it.\n'
jpayne@69 13505 '\n'
jpayne@69 13506 ' New in version 3.3: "clear()" and "copy()" methods.\n'
jpayne@69 13507 '\n'
jpayne@69 13508 '6. The value *n* is an integer, or an object '
jpayne@69 13509 'implementing\n'
jpayne@69 13510 ' "__index__()". Zero and negative values of *n* clear '
jpayne@69 13511 'the sequence.\n'
jpayne@69 13512 ' Items in the sequence are not copied; they are '
jpayne@69 13513 'referenced multiple\n'
jpayne@69 13514 ' times, as explained for "s * n" under Common Sequence '
jpayne@69 13515 'Operations.\n',
jpayne@69 13516 'unary': 'Unary arithmetic and bitwise operations\n'
jpayne@69 13517 '***************************************\n'
jpayne@69 13518 '\n'
jpayne@69 13519 'All unary arithmetic and bitwise operations have the same '
jpayne@69 13520 'priority:\n'
jpayne@69 13521 '\n'
jpayne@69 13522 ' u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr\n'
jpayne@69 13523 '\n'
jpayne@69 13524 'The unary "-" (minus) operator yields the negation of its numeric\n'
jpayne@69 13525 'argument.\n'
jpayne@69 13526 '\n'
jpayne@69 13527 'The unary "+" (plus) operator yields its numeric argument '
jpayne@69 13528 'unchanged.\n'
jpayne@69 13529 '\n'
jpayne@69 13530 'The unary "~" (invert) operator yields the bitwise inversion of '
jpayne@69 13531 'its\n'
jpayne@69 13532 'integer argument. The bitwise inversion of "x" is defined as\n'
jpayne@69 13533 '"-(x+1)". It only applies to integral numbers.\n'
jpayne@69 13534 '\n'
jpayne@69 13535 'In all three cases, if the argument does not have the proper type, '
jpayne@69 13536 'a\n'
jpayne@69 13537 '"TypeError" exception is raised.\n',
jpayne@69 13538 'while': 'The "while" statement\n'
jpayne@69 13539 '*********************\n'
jpayne@69 13540 '\n'
jpayne@69 13541 'The "while" statement is used for repeated execution as long as an\n'
jpayne@69 13542 'expression is true:\n'
jpayne@69 13543 '\n'
jpayne@69 13544 ' while_stmt ::= "while" expression ":" suite\n'
jpayne@69 13545 ' ["else" ":" suite]\n'
jpayne@69 13546 '\n'
jpayne@69 13547 'This repeatedly tests the expression and, if it is true, executes '
jpayne@69 13548 'the\n'
jpayne@69 13549 'first suite; if the expression is false (which may be the first '
jpayne@69 13550 'time\n'
jpayne@69 13551 'it is tested) the suite of the "else" clause, if present, is '
jpayne@69 13552 'executed\n'
jpayne@69 13553 'and the loop terminates.\n'
jpayne@69 13554 '\n'
jpayne@69 13555 'A "break" statement executed in the first suite terminates the '
jpayne@69 13556 'loop\n'
jpayne@69 13557 'without executing the "else" clause’s suite. A "continue" '
jpayne@69 13558 'statement\n'
jpayne@69 13559 'executed in the first suite skips the rest of the suite and goes '
jpayne@69 13560 'back\n'
jpayne@69 13561 'to testing the expression.\n',
jpayne@69 13562 'with': 'The "with" statement\n'
jpayne@69 13563 '********************\n'
jpayne@69 13564 '\n'
jpayne@69 13565 'The "with" statement is used to wrap the execution of a block with\n'
jpayne@69 13566 'methods defined by a context manager (see section With Statement\n'
jpayne@69 13567 'Context Managers). This allows common "try"…"except"…"finally" '
jpayne@69 13568 'usage\n'
jpayne@69 13569 'patterns to be encapsulated for convenient reuse.\n'
jpayne@69 13570 '\n'
jpayne@69 13571 ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n'
jpayne@69 13572 ' with_item ::= expression ["as" target]\n'
jpayne@69 13573 '\n'
jpayne@69 13574 'The execution of the "with" statement with one “item” proceeds as\n'
jpayne@69 13575 'follows:\n'
jpayne@69 13576 '\n'
jpayne@69 13577 '1. The context expression (the expression given in the "with_item")\n'
jpayne@69 13578 ' is evaluated to obtain a context manager.\n'
jpayne@69 13579 '\n'
jpayne@69 13580 '2. The context manager’s "__exit__()" is loaded for later use.\n'
jpayne@69 13581 '\n'
jpayne@69 13582 '3. The context manager’s "__enter__()" method is invoked.\n'
jpayne@69 13583 '\n'
jpayne@69 13584 '4. If a target was included in the "with" statement, the return\n'
jpayne@69 13585 ' value from "__enter__()" is assigned to it.\n'
jpayne@69 13586 '\n'
jpayne@69 13587 ' Note: The "with" statement guarantees that if the "__enter__()"\n'
jpayne@69 13588 ' method returns without an error, then "__exit__()" will always '
jpayne@69 13589 'be\n'
jpayne@69 13590 ' called. Thus, if an error occurs during the assignment to the\n'
jpayne@69 13591 ' target list, it will be treated the same as an error occurring\n'
jpayne@69 13592 ' within the suite would be. See step 6 below.\n'
jpayne@69 13593 '\n'
jpayne@69 13594 '5. The suite is executed.\n'
jpayne@69 13595 '\n'
jpayne@69 13596 '6. The context manager’s "__exit__()" method is invoked. If an\n'
jpayne@69 13597 ' exception caused the suite to be exited, its type, value, and\n'
jpayne@69 13598 ' traceback are passed as arguments to "__exit__()". Otherwise, '
jpayne@69 13599 'three\n'
jpayne@69 13600 ' "None" arguments are supplied.\n'
jpayne@69 13601 '\n'
jpayne@69 13602 ' If the suite was exited due to an exception, and the return '
jpayne@69 13603 'value\n'
jpayne@69 13604 ' from the "__exit__()" method was false, the exception is '
jpayne@69 13605 'reraised.\n'
jpayne@69 13606 ' If the return value was true, the exception is suppressed, and\n'
jpayne@69 13607 ' execution continues with the statement following the "with"\n'
jpayne@69 13608 ' statement.\n'
jpayne@69 13609 '\n'
jpayne@69 13610 ' If the suite was exited for any reason other than an exception, '
jpayne@69 13611 'the\n'
jpayne@69 13612 ' return value from "__exit__()" is ignored, and execution '
jpayne@69 13613 'proceeds\n'
jpayne@69 13614 ' at the normal location for the kind of exit that was taken.\n'
jpayne@69 13615 '\n'
jpayne@69 13616 'With more than one item, the context managers are processed as if\n'
jpayne@69 13617 'multiple "with" statements were nested:\n'
jpayne@69 13618 '\n'
jpayne@69 13619 ' with A() as a, B() as b:\n'
jpayne@69 13620 ' suite\n'
jpayne@69 13621 '\n'
jpayne@69 13622 'is equivalent to\n'
jpayne@69 13623 '\n'
jpayne@69 13624 ' with A() as a:\n'
jpayne@69 13625 ' with B() as b:\n'
jpayne@69 13626 ' suite\n'
jpayne@69 13627 '\n'
jpayne@69 13628 'Changed in version 3.1: Support for multiple context expressions.\n'
jpayne@69 13629 '\n'
jpayne@69 13630 'See also:\n'
jpayne@69 13631 '\n'
jpayne@69 13632 ' **PEP 343** - The “with” statement\n'
jpayne@69 13633 ' The specification, background, and examples for the Python '
jpayne@69 13634 '"with"\n'
jpayne@69 13635 ' statement.\n',
jpayne@69 13636 'yield': 'The "yield" statement\n'
jpayne@69 13637 '*********************\n'
jpayne@69 13638 '\n'
jpayne@69 13639 ' yield_stmt ::= yield_expression\n'
jpayne@69 13640 '\n'
jpayne@69 13641 'A "yield" statement is semantically equivalent to a yield '
jpayne@69 13642 'expression.\n'
jpayne@69 13643 'The yield statement can be used to omit the parentheses that would\n'
jpayne@69 13644 'otherwise be required in the equivalent yield expression '
jpayne@69 13645 'statement.\n'
jpayne@69 13646 'For example, the yield statements\n'
jpayne@69 13647 '\n'
jpayne@69 13648 ' yield <expr>\n'
jpayne@69 13649 ' yield from <expr>\n'
jpayne@69 13650 '\n'
jpayne@69 13651 'are equivalent to the yield expression statements\n'
jpayne@69 13652 '\n'
jpayne@69 13653 ' (yield <expr>)\n'
jpayne@69 13654 ' (yield from <expr>)\n'
jpayne@69 13655 '\n'
jpayne@69 13656 'Yield expressions and statements are only used when defining a\n'
jpayne@69 13657 '*generator* function, and are only used in the body of the '
jpayne@69 13658 'generator\n'
jpayne@69 13659 'function. Using yield in a function definition is sufficient to '
jpayne@69 13660 'cause\n'
jpayne@69 13661 'that definition to create a generator function instead of a normal\n'
jpayne@69 13662 'function.\n'
jpayne@69 13663 '\n'
jpayne@69 13664 'For full details of "yield" semantics, refer to the Yield '
jpayne@69 13665 'expressions\n'
jpayne@69 13666 'section.\n'}