Georg Brandl | 0f25cea | 2012-03-04 16:12:09 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Larry Hastings | fcdd34d | 2015-09-12 17:24:02 +0100 | [diff] [blame] | 2 | # Autogenerated by Sphinx on Sat Sep 12 17:22:24 2015 |
| 3 | topics = {'assert': '\n' |
| 4 | 'The "assert" statement\n' |
| 5 | '**********************\n' |
| 6 | '\n' |
| 7 | 'Assert statements are a convenient way to insert debugging ' |
| 8 | 'assertions\n' |
| 9 | 'into a program:\n' |
| 10 | '\n' |
| 11 | ' assert_stmt ::= "assert" expression ["," expression]\n' |
| 12 | '\n' |
| 13 | 'The simple form, "assert expression", is equivalent to\n' |
| 14 | '\n' |
| 15 | ' if __debug__:\n' |
| 16 | ' if not expression: raise AssertionError\n' |
| 17 | '\n' |
| 18 | 'The extended form, "assert expression1, expression2", is ' |
| 19 | 'equivalent to\n' |
| 20 | '\n' |
| 21 | ' if __debug__:\n' |
| 22 | ' if not expression1: raise AssertionError(expression2)\n' |
| 23 | '\n' |
| 24 | 'These equivalences assume that "__debug__" and "AssertionError" ' |
| 25 | 'refer\n' |
| 26 | 'to the built-in variables with those names. In the current\n' |
| 27 | 'implementation, the built-in variable "__debug__" is "True" ' |
| 28 | 'under\n' |
| 29 | 'normal circumstances, "False" when optimization is requested ' |
| 30 | '(command\n' |
| 31 | 'line option -O). The current code generator emits no code for ' |
| 32 | 'an\n' |
| 33 | 'assert statement when optimization is requested at compile ' |
| 34 | 'time. Note\n' |
| 35 | 'that it is unnecessary to include the source code for the ' |
| 36 | 'expression\n' |
| 37 | 'that failed in the error message; it will be displayed as part ' |
| 38 | 'of the\n' |
| 39 | 'stack trace.\n' |
| 40 | '\n' |
| 41 | 'Assignments to "__debug__" are illegal. The value for the ' |
| 42 | 'built-in\n' |
| 43 | 'variable is determined when the interpreter starts.\n', |
| 44 | 'assignment': '\n' |
| 45 | 'Assignment statements\n' |
| 46 | '*********************\n' |
| 47 | '\n' |
| 48 | 'Assignment statements are used to (re)bind names to values ' |
| 49 | 'and to\n' |
| 50 | 'modify attributes or items of mutable objects:\n' |
| 51 | '\n' |
| 52 | ' assignment_stmt ::= (target_list "=")+ (expression_list | ' |
| 53 | 'yield_expression)\n' |
| 54 | ' target_list ::= target ("," target)* [","]\n' |
| 55 | ' target ::= identifier\n' |
| 56 | ' | "(" target_list ")"\n' |
| 57 | ' | "[" target_list "]"\n' |
| 58 | ' | attributeref\n' |
| 59 | ' | subscription\n' |
| 60 | ' | slicing\n' |
| 61 | ' | "*" target\n' |
| 62 | '\n' |
| 63 | '(See section *Primaries* for the syntax definitions for\n' |
| 64 | '*attributeref*, *subscription*, and *slicing*.)\n' |
| 65 | '\n' |
| 66 | 'An assignment statement evaluates the expression list ' |
| 67 | '(remember that\n' |
| 68 | 'this can be a single expression or a comma-separated list, ' |
| 69 | 'the latter\n' |
| 70 | 'yielding a tuple) and assigns the single resulting object to ' |
| 71 | 'each of\n' |
| 72 | 'the target lists, from left to right.\n' |
| 73 | '\n' |
| 74 | 'Assignment is defined recursively depending on the form of ' |
| 75 | 'the target\n' |
| 76 | '(list). When a target is part of a mutable object (an ' |
| 77 | 'attribute\n' |
| 78 | 'reference, subscription or slicing), the mutable object ' |
| 79 | 'must\n' |
| 80 | 'ultimately perform the assignment and decide about its ' |
| 81 | 'validity, and\n' |
| 82 | 'may raise an exception if the assignment is unacceptable. ' |
| 83 | 'The rules\n' |
| 84 | 'observed by various types and the exceptions raised are ' |
| 85 | 'given with the\n' |
| 86 | 'definition of the object types (see section *The standard ' |
| 87 | 'type\n' |
| 88 | 'hierarchy*).\n' |
| 89 | '\n' |
| 90 | 'Assignment of an object to a target list, optionally ' |
| 91 | 'enclosed in\n' |
| 92 | 'parentheses or square brackets, is recursively defined as ' |
| 93 | 'follows.\n' |
| 94 | '\n' |
| 95 | '* If the target list is a single target: The object is ' |
| 96 | 'assigned to\n' |
| 97 | ' that target.\n' |
| 98 | '\n' |
| 99 | '* If the target list is a comma-separated list of targets: ' |
| 100 | 'The\n' |
| 101 | ' object must be an iterable with the same number of items ' |
| 102 | 'as there\n' |
| 103 | ' are targets in the target list, and the items are ' |
| 104 | 'assigned, from\n' |
| 105 | ' left to right, to the corresponding targets.\n' |
| 106 | '\n' |
| 107 | ' * If the target list contains one target prefixed with an\n' |
| 108 | ' asterisk, called a "starred" target: The object must be ' |
| 109 | 'a sequence\n' |
| 110 | ' with at least as many items as there are targets in the ' |
| 111 | 'target\n' |
| 112 | ' list, minus one. The first items of the sequence are ' |
| 113 | 'assigned,\n' |
| 114 | ' from left to right, to the targets before the starred ' |
| 115 | 'target. The\n' |
| 116 | ' final items of the sequence are assigned to the targets ' |
| 117 | 'after the\n' |
| 118 | ' starred target. A list of the remaining items in the ' |
| 119 | 'sequence is\n' |
| 120 | ' then assigned to the starred target (the list can be ' |
| 121 | 'empty).\n' |
| 122 | '\n' |
| 123 | ' * Else: The object must be a sequence with the same number ' |
| 124 | 'of\n' |
| 125 | ' items as there are targets in the target list, and the ' |
| 126 | 'items are\n' |
| 127 | ' assigned, from left to right, to the corresponding ' |
| 128 | 'targets.\n' |
| 129 | '\n' |
| 130 | 'Assignment of an object to a single target is recursively ' |
| 131 | 'defined as\n' |
| 132 | 'follows.\n' |
| 133 | '\n' |
| 134 | '* If the target is an identifier (name):\n' |
| 135 | '\n' |
| 136 | ' * If the name does not occur in a "global" or "nonlocal" ' |
| 137 | 'statement\n' |
| 138 | ' in the current code block: the name is bound to the ' |
| 139 | 'object in the\n' |
| 140 | ' current local namespace.\n' |
| 141 | '\n' |
| 142 | ' * Otherwise: the name is bound to the object in the ' |
| 143 | 'global\n' |
| 144 | ' namespace or the outer namespace determined by ' |
| 145 | '"nonlocal",\n' |
| 146 | ' respectively.\n' |
| 147 | '\n' |
| 148 | ' The name is rebound if it was already bound. This may ' |
| 149 | 'cause the\n' |
| 150 | ' reference count for the object previously bound to the ' |
| 151 | 'name to reach\n' |
| 152 | ' zero, causing the object to be deallocated and its ' |
| 153 | 'destructor (if it\n' |
| 154 | ' has one) to be called.\n' |
| 155 | '\n' |
| 156 | '* If the target is a target list enclosed in parentheses or ' |
| 157 | 'in\n' |
| 158 | ' square brackets: The object must be an iterable with the ' |
| 159 | 'same number\n' |
| 160 | ' of items as there are targets in the target list, and its ' |
| 161 | 'items are\n' |
| 162 | ' assigned, from left to right, to the corresponding ' |
| 163 | 'targets.\n' |
| 164 | '\n' |
| 165 | '* If the target is an attribute reference: The primary ' |
| 166 | 'expression in\n' |
| 167 | ' the reference is evaluated. It should yield an object ' |
| 168 | 'with\n' |
| 169 | ' assignable attributes; if this is not the case, ' |
| 170 | '"TypeError" is\n' |
| 171 | ' raised. That object is then asked to assign the assigned ' |
| 172 | 'object to\n' |
| 173 | ' the given attribute; if it cannot perform the assignment, ' |
| 174 | 'it raises\n' |
| 175 | ' an exception (usually but not necessarily ' |
| 176 | '"AttributeError").\n' |
| 177 | '\n' |
| 178 | ' Note: If the object is a class instance and the attribute ' |
| 179 | 'reference\n' |
| 180 | ' occurs on both sides of the assignment operator, the RHS ' |
| 181 | 'expression,\n' |
| 182 | ' "a.x" can access either an instance attribute or (if no ' |
| 183 | 'instance\n' |
| 184 | ' attribute exists) a class attribute. The LHS target "a.x" ' |
| 185 | 'is always\n' |
| 186 | ' set as an instance attribute, creating it if necessary. ' |
| 187 | 'Thus, the\n' |
| 188 | ' two occurrences of "a.x" do not necessarily refer to the ' |
| 189 | 'same\n' |
| 190 | ' attribute: if the RHS expression refers to a class ' |
| 191 | 'attribute, the\n' |
| 192 | ' LHS creates a new instance attribute as the target of the\n' |
| 193 | ' assignment:\n' |
| 194 | '\n' |
| 195 | ' class Cls:\n' |
| 196 | ' x = 3 # class variable\n' |
| 197 | ' inst = Cls()\n' |
| 198 | ' inst.x = inst.x + 1 # writes inst.x as 4 leaving ' |
| 199 | 'Cls.x as 3\n' |
| 200 | '\n' |
| 201 | ' This description does not necessarily apply to descriptor\n' |
| 202 | ' attributes, such as properties created with "property()".\n' |
| 203 | '\n' |
| 204 | '* If the target is a subscription: The primary expression in ' |
| 205 | 'the\n' |
| 206 | ' reference is evaluated. It should yield either a mutable ' |
| 207 | 'sequence\n' |
| 208 | ' object (such as a list) or a mapping object (such as a ' |
| 209 | 'dictionary).\n' |
| 210 | ' Next, the subscript expression is evaluated.\n' |
| 211 | '\n' |
| 212 | ' If the primary is a mutable sequence object (such as a ' |
| 213 | 'list), the\n' |
| 214 | ' subscript must yield an integer. If it is negative, the ' |
| 215 | "sequence's\n" |
| 216 | ' length is added to it. The resulting value must be a ' |
| 217 | 'nonnegative\n' |
| 218 | " integer less than the sequence's length, and the sequence " |
| 219 | 'is asked\n' |
| 220 | ' to assign the assigned object to its item with that ' |
| 221 | 'index. If the\n' |
| 222 | ' index is out of range, "IndexError" is raised (assignment ' |
| 223 | 'to a\n' |
| 224 | ' subscripted sequence cannot add new items to a list).\n' |
| 225 | '\n' |
| 226 | ' If the primary is a mapping object (such as a dictionary), ' |
| 227 | 'the\n' |
| 228 | " subscript must have a type compatible with the mapping's " |
| 229 | 'key type,\n' |
| 230 | ' and the mapping is then asked to create a key/datum pair ' |
| 231 | 'which maps\n' |
| 232 | ' the subscript to the assigned object. This can either ' |
| 233 | 'replace an\n' |
| 234 | ' existing key/value pair with the same key value, or insert ' |
| 235 | 'a new\n' |
| 236 | ' key/value pair (if no key with the same value existed).\n' |
| 237 | '\n' |
| 238 | ' For user-defined objects, the "__setitem__()" method is ' |
| 239 | 'called with\n' |
| 240 | ' appropriate arguments.\n' |
| 241 | '\n' |
| 242 | '* If the target is a slicing: The primary expression in the\n' |
| 243 | ' reference is evaluated. It should yield a mutable ' |
| 244 | 'sequence object\n' |
| 245 | ' (such as a list). The assigned object should be a ' |
| 246 | 'sequence object\n' |
| 247 | ' of the same type. Next, the lower and upper bound ' |
| 248 | 'expressions are\n' |
| 249 | ' evaluated, insofar they are present; defaults are zero and ' |
| 250 | 'the\n' |
| 251 | " sequence's length. The bounds should evaluate to " |
| 252 | 'integers. If\n' |
| 253 | " either bound is negative, the sequence's length is added " |
| 254 | 'to it. The\n' |
| 255 | ' resulting bounds are clipped to lie between zero and the ' |
| 256 | "sequence's\n" |
| 257 | ' length, inclusive. Finally, the sequence object is asked ' |
| 258 | 'to replace\n' |
| 259 | ' the slice with the items of the assigned sequence. The ' |
| 260 | 'length of\n' |
| 261 | ' the slice may be different from the length of the assigned ' |
| 262 | 'sequence,\n' |
| 263 | ' thus changing the length of the target sequence, if the ' |
| 264 | 'target\n' |
| 265 | ' sequence allows it.\n' |
| 266 | '\n' |
| 267 | '**CPython implementation detail:** In the current ' |
| 268 | 'implementation, the\n' |
| 269 | 'syntax for targets is taken to be the same as for ' |
| 270 | 'expressions, and\n' |
| 271 | 'invalid syntax is rejected during the code generation phase, ' |
| 272 | 'causing\n' |
| 273 | 'less detailed error messages.\n' |
| 274 | '\n' |
| 275 | 'Although the definition of assignment implies that overlaps ' |
| 276 | 'between\n' |
| 277 | 'the left-hand side and the right-hand side are ' |
| 278 | "'simultanenous' (for\n" |
| 279 | 'example "a, b = b, a" swaps two variables), overlaps ' |
| 280 | '*within* the\n' |
| 281 | 'collection of assigned-to variables occur left-to-right, ' |
| 282 | 'sometimes\n' |
| 283 | 'resulting in confusion. For instance, the following program ' |
| 284 | 'prints\n' |
| 285 | '"[0, 2]":\n' |
| 286 | '\n' |
| 287 | ' x = [0, 1]\n' |
| 288 | ' i = 0\n' |
| 289 | ' i, x[i] = 1, 2 # i is updated, then x[i] is ' |
| 290 | 'updated\n' |
| 291 | ' print(x)\n' |
| 292 | '\n' |
| 293 | 'See also: **PEP 3132** - Extended Iterable Unpacking\n' |
| 294 | '\n' |
| 295 | ' The specification for the "*target" feature.\n' |
| 296 | '\n' |
| 297 | '\n' |
| 298 | 'Augmented assignment statements\n' |
| 299 | '===============================\n' |
| 300 | '\n' |
| 301 | 'Augmented assignment is the combination, in a single ' |
| 302 | 'statement, of a\n' |
| 303 | 'binary operation and an assignment statement:\n' |
| 304 | '\n' |
| 305 | ' augmented_assignment_stmt ::= augtarget augop ' |
| 306 | '(expression_list | yield_expression)\n' |
| 307 | ' augtarget ::= identifier | attributeref | ' |
| 308 | 'subscription | slicing\n' |
| 309 | ' augop ::= "+=" | "-=" | "*=" | "@=" | ' |
| 310 | '"/=" | "//=" | "%=" | "**="\n' |
| 311 | ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n' |
| 312 | '\n' |
| 313 | '(See section *Primaries* for the syntax definitions of the ' |
| 314 | 'last three\n' |
| 315 | 'symbols.)\n' |
| 316 | '\n' |
| 317 | 'An augmented assignment evaluates the target (which, unlike ' |
| 318 | 'normal\n' |
| 319 | 'assignment statements, cannot be an unpacking) and the ' |
| 320 | 'expression\n' |
| 321 | 'list, performs the binary operation specific to the type of ' |
| 322 | 'assignment\n' |
| 323 | 'on the two operands, and assigns the result to the original ' |
| 324 | 'target.\n' |
| 325 | 'The target is only evaluated once.\n' |
| 326 | '\n' |
| 327 | 'An augmented assignment expression like "x += 1" can be ' |
| 328 | 'rewritten as\n' |
| 329 | '"x = x + 1" to achieve a similar, but not exactly equal ' |
| 330 | 'effect. In the\n' |
| 331 | 'augmented version, "x" is only evaluated once. Also, when ' |
| 332 | 'possible,\n' |
| 333 | 'the actual operation is performed *in-place*, meaning that ' |
| 334 | 'rather than\n' |
| 335 | 'creating a new object and assigning that to the target, the ' |
| 336 | 'old object\n' |
| 337 | 'is modified instead.\n' |
| 338 | '\n' |
| 339 | 'Unlike normal assignments, augmented assignments evaluate ' |
| 340 | 'the left-\n' |
| 341 | 'hand side *before* evaluating the right-hand side. For ' |
| 342 | 'example, "a[i]\n' |
| 343 | '+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and ' |
| 344 | 'performs\n' |
| 345 | 'the addition, and lastly, it writes the result back to ' |
| 346 | '"a[i]".\n' |
| 347 | '\n' |
| 348 | 'With the exception of assigning to tuples and multiple ' |
| 349 | 'targets in a\n' |
| 350 | 'single statement, the assignment done by augmented ' |
| 351 | 'assignment\n' |
| 352 | 'statements is handled the same way as normal assignments. ' |
| 353 | 'Similarly,\n' |
| 354 | 'with the exception of the possible *in-place* behavior, the ' |
| 355 | 'binary\n' |
| 356 | 'operation performed by augmented assignment is the same as ' |
| 357 | 'the normal\n' |
| 358 | 'binary operations.\n' |
| 359 | '\n' |
| 360 | 'For targets which are attribute references, the same *caveat ' |
| 361 | 'about\n' |
| 362 | 'class and instance attributes* applies as for regular ' |
| 363 | 'assignments.\n', |
| 364 | 'atom-identifiers': '\n' |
| 365 | 'Identifiers (Names)\n' |
| 366 | '*******************\n' |
| 367 | '\n' |
| 368 | 'An identifier occurring as an atom is a name. See ' |
| 369 | 'section\n' |
| 370 | '*Identifiers and keywords* for lexical definition and ' |
| 371 | 'section *Naming\n' |
| 372 | 'and binding* for documentation of naming and binding.\n' |
| 373 | '\n' |
| 374 | 'When the name is bound to an object, evaluation of the ' |
| 375 | 'atom yields\n' |
| 376 | 'that object. When a name is not bound, an attempt to ' |
| 377 | 'evaluate it\n' |
| 378 | 'raises a "NameError" exception.\n' |
| 379 | '\n' |
| 380 | '**Private name mangling:** When an identifier that ' |
| 381 | 'textually occurs in\n' |
| 382 | 'a class definition begins with two or more underscore ' |
| 383 | 'characters and\n' |
| 384 | 'does not end in two or more underscores, it is ' |
| 385 | 'considered a *private\n' |
| 386 | 'name* of that class. Private names are transformed to ' |
| 387 | 'a longer form\n' |
| 388 | 'before code is generated for them. The transformation ' |
| 389 | 'inserts the\n' |
| 390 | 'class name, with leading underscores removed and a ' |
| 391 | 'single underscore\n' |
| 392 | 'inserted, in front of the name. For example, the ' |
| 393 | 'identifier "__spam"\n' |
| 394 | 'occurring in a class named "Ham" will be transformed ' |
| 395 | 'to "_Ham__spam".\n' |
| 396 | 'This transformation is independent of the syntactical ' |
| 397 | 'context in which\n' |
| 398 | 'the identifier is used. If the transformed name is ' |
| 399 | 'extremely long\n' |
| 400 | '(longer than 255 characters), implementation defined ' |
| 401 | 'truncation may\n' |
| 402 | 'happen. If the class name consists only of ' |
| 403 | 'underscores, no\n' |
| 404 | 'transformation is done.\n', |
| 405 | 'atom-literals': '\n' |
| 406 | 'Literals\n' |
| 407 | '********\n' |
| 408 | '\n' |
| 409 | 'Python supports string and bytes literals and various ' |
| 410 | 'numeric\n' |
| 411 | 'literals:\n' |
| 412 | '\n' |
| 413 | ' literal ::= stringliteral | bytesliteral\n' |
| 414 | ' | integer | floatnumber | imagnumber\n' |
| 415 | '\n' |
| 416 | 'Evaluation of a literal yields an object of the given ' |
| 417 | 'type (string,\n' |
| 418 | 'bytes, integer, floating point number, complex number) ' |
| 419 | 'with the given\n' |
| 420 | 'value. The value may be approximated in the case of ' |
| 421 | 'floating point\n' |
| 422 | 'and imaginary (complex) literals. See section *Literals* ' |
| 423 | 'for details.\n' |
| 424 | '\n' |
| 425 | 'All literals correspond to immutable data types, and ' |
| 426 | 'hence the\n' |
| 427 | "object's identity is less important than its value. " |
| 428 | 'Multiple\n' |
| 429 | 'evaluations of literals with the same value (either the ' |
| 430 | 'same\n' |
| 431 | 'occurrence in the program text or a different occurrence) ' |
| 432 | 'may obtain\n' |
| 433 | 'the same object or a different object with the same ' |
| 434 | 'value.\n', |
| 435 | 'attribute-access': '\n' |
| 436 | 'Customizing attribute access\n' |
| 437 | '****************************\n' |
| 438 | '\n' |
| 439 | 'The following methods can be defined to customize the ' |
| 440 | 'meaning of\n' |
| 441 | 'attribute access (use of, assignment to, or deletion ' |
| 442 | 'of "x.name") for\n' |
| 443 | 'class instances.\n' |
| 444 | '\n' |
| 445 | 'object.__getattr__(self, name)\n' |
| 446 | '\n' |
| 447 | ' Called when an attribute lookup has not found the ' |
| 448 | 'attribute in the\n' |
| 449 | ' usual places (i.e. it is not an instance attribute ' |
| 450 | 'nor is it found\n' |
| 451 | ' in the class tree for "self"). "name" is the ' |
| 452 | 'attribute name. This\n' |
| 453 | ' method should return the (computed) attribute value ' |
| 454 | 'or raise an\n' |
| 455 | ' "AttributeError" exception.\n' |
| 456 | '\n' |
| 457 | ' Note that if the attribute is found through the ' |
| 458 | 'normal mechanism,\n' |
| 459 | ' "__getattr__()" is not called. (This is an ' |
| 460 | 'intentional asymmetry\n' |
| 461 | ' between "__getattr__()" and "__setattr__()".) This ' |
| 462 | 'is done both for\n' |
| 463 | ' efficiency reasons and because otherwise ' |
| 464 | '"__getattr__()" would have\n' |
| 465 | ' no way to access other attributes of the instance. ' |
| 466 | 'Note that at\n' |
| 467 | ' least for instance variables, you can fake total ' |
| 468 | 'control by not\n' |
| 469 | ' inserting any values in the instance attribute ' |
| 470 | 'dictionary (but\n' |
| 471 | ' instead inserting them in another object). See ' |
| 472 | 'the\n' |
| 473 | ' "__getattribute__()" method below for a way to ' |
| 474 | 'actually get total\n' |
| 475 | ' control over attribute access.\n' |
| 476 | '\n' |
| 477 | 'object.__getattribute__(self, name)\n' |
| 478 | '\n' |
| 479 | ' Called unconditionally to implement attribute ' |
| 480 | 'accesses for\n' |
| 481 | ' instances of the class. If the class also defines ' |
| 482 | '"__getattr__()",\n' |
| 483 | ' the latter will not be called unless ' |
| 484 | '"__getattribute__()" either\n' |
| 485 | ' calls it explicitly or raises an "AttributeError". ' |
| 486 | 'This method\n' |
| 487 | ' should return the (computed) attribute value or ' |
| 488 | 'raise an\n' |
| 489 | ' "AttributeError" exception. In order to avoid ' |
| 490 | 'infinite recursion in\n' |
| 491 | ' this method, its implementation should always call ' |
| 492 | 'the base class\n' |
| 493 | ' method with the same name to access any attributes ' |
| 494 | 'it needs, for\n' |
| 495 | ' example, "object.__getattribute__(self, name)".\n' |
| 496 | '\n' |
| 497 | ' Note: This method may still be bypassed when ' |
| 498 | 'looking up special\n' |
| 499 | ' methods as the result of implicit invocation via ' |
| 500 | 'language syntax\n' |
| 501 | ' or built-in functions. See *Special method ' |
| 502 | 'lookup*.\n' |
| 503 | '\n' |
| 504 | 'object.__setattr__(self, name, value)\n' |
| 505 | '\n' |
| 506 | ' Called when an attribute assignment is attempted. ' |
| 507 | 'This is called\n' |
| 508 | ' instead of the normal mechanism (i.e. store the ' |
| 509 | 'value in the\n' |
| 510 | ' instance dictionary). *name* is the attribute name, ' |
| 511 | '*value* is the\n' |
| 512 | ' value to be assigned to it.\n' |
| 513 | '\n' |
| 514 | ' If "__setattr__()" wants to assign to an instance ' |
| 515 | 'attribute, it\n' |
| 516 | ' should call the base class method with the same ' |
| 517 | 'name, for example,\n' |
| 518 | ' "object.__setattr__(self, name, value)".\n' |
| 519 | '\n' |
| 520 | 'object.__delattr__(self, name)\n' |
| 521 | '\n' |
| 522 | ' Like "__setattr__()" but for attribute deletion ' |
| 523 | 'instead of\n' |
| 524 | ' assignment. This should only be implemented if ' |
| 525 | '"del obj.name" is\n' |
| 526 | ' meaningful for the object.\n' |
| 527 | '\n' |
| 528 | 'object.__dir__(self)\n' |
| 529 | '\n' |
| 530 | ' Called when "dir()" is called on the object. A ' |
| 531 | 'sequence must be\n' |
| 532 | ' returned. "dir()" converts the returned sequence to ' |
| 533 | 'a list and\n' |
| 534 | ' sorts it.\n' |
| 535 | '\n' |
| 536 | '\n' |
| 537 | 'Implementing Descriptors\n' |
| 538 | '========================\n' |
| 539 | '\n' |
| 540 | 'The following methods only apply when an instance of ' |
| 541 | 'the class\n' |
| 542 | 'containing the method (a so-called *descriptor* class) ' |
| 543 | 'appears in an\n' |
| 544 | '*owner* class (the descriptor must be in either the ' |
| 545 | "owner's class\n" |
| 546 | 'dictionary or in the class dictionary for one of its ' |
| 547 | 'parents). In the\n' |
| 548 | 'examples below, "the attribute" refers to the ' |
| 549 | 'attribute whose name is\n' |
| 550 | "the key of the property in the owner class' " |
| 551 | '"__dict__".\n' |
| 552 | '\n' |
| 553 | 'object.__get__(self, instance, owner)\n' |
| 554 | '\n' |
| 555 | ' Called to get the attribute of the owner class ' |
| 556 | '(class attribute\n' |
| 557 | ' access) or of an instance of that class (instance ' |
| 558 | 'attribute\n' |
| 559 | ' access). *owner* is always the owner class, while ' |
| 560 | '*instance* is the\n' |
| 561 | ' instance that the attribute was accessed through, ' |
| 562 | 'or "None" when\n' |
| 563 | ' the attribute is accessed through the *owner*. ' |
| 564 | 'This method should\n' |
| 565 | ' return the (computed) attribute value or raise an ' |
| 566 | '"AttributeError"\n' |
| 567 | ' exception.\n' |
| 568 | '\n' |
| 569 | 'object.__set__(self, instance, value)\n' |
| 570 | '\n' |
| 571 | ' Called to set the attribute on an instance ' |
| 572 | '*instance* of the owner\n' |
| 573 | ' class to a new value, *value*.\n' |
| 574 | '\n' |
| 575 | 'object.__delete__(self, instance)\n' |
| 576 | '\n' |
| 577 | ' Called to delete the attribute on an instance ' |
| 578 | '*instance* of the\n' |
| 579 | ' owner class.\n' |
| 580 | '\n' |
| 581 | 'The attribute "__objclass__" is interpreted by the ' |
| 582 | '"inspect" module as\n' |
| 583 | 'specifying the class where this object was defined ' |
| 584 | '(setting this\n' |
| 585 | 'appropriately can assist in runtime introspection of ' |
| 586 | 'dynamic class\n' |
| 587 | 'attributes). For callables, it may indicate that an ' |
| 588 | 'instance of the\n' |
| 589 | 'given type (or a subclass) is expected or required as ' |
| 590 | 'the first\n' |
| 591 | 'positional argument (for example, CPython sets this ' |
| 592 | 'attribute for\n' |
| 593 | 'unbound methods that are implemented in C).\n' |
| 594 | '\n' |
| 595 | '\n' |
| 596 | 'Invoking Descriptors\n' |
| 597 | '====================\n' |
| 598 | '\n' |
| 599 | 'In general, a descriptor is an object attribute with ' |
| 600 | '"binding\n' |
| 601 | 'behavior", one whose attribute access has been ' |
| 602 | 'overridden by methods\n' |
| 603 | 'in the descriptor protocol: "__get__()", "__set__()", ' |
| 604 | 'and\n' |
| 605 | '"__delete__()". If any of those methods are defined ' |
| 606 | 'for an object, it\n' |
| 607 | 'is said to be a descriptor.\n' |
| 608 | '\n' |
| 609 | 'The default behavior for attribute access is to get, ' |
| 610 | 'set, or delete\n' |
| 611 | "the attribute from an object's dictionary. For " |
| 612 | 'instance, "a.x" has a\n' |
| 613 | 'lookup chain starting with "a.__dict__[\'x\']", then\n' |
| 614 | '"type(a).__dict__[\'x\']", and continuing through the ' |
| 615 | 'base classes of\n' |
| 616 | '"type(a)" excluding metaclasses.\n' |
| 617 | '\n' |
| 618 | 'However, if the looked-up value is an object defining ' |
| 619 | 'one of the\n' |
| 620 | 'descriptor methods, then Python may override the ' |
| 621 | 'default behavior and\n' |
| 622 | 'invoke the descriptor method instead. Where this ' |
| 623 | 'occurs in the\n' |
| 624 | 'precedence chain depends on which descriptor methods ' |
| 625 | 'were defined and\n' |
| 626 | 'how they were called.\n' |
| 627 | '\n' |
| 628 | 'The starting point for descriptor invocation is a ' |
| 629 | 'binding, "a.x". How\n' |
| 630 | 'the arguments are assembled depends on "a":\n' |
| 631 | '\n' |
| 632 | 'Direct Call\n' |
| 633 | ' The simplest and least common call is when user ' |
| 634 | 'code directly\n' |
| 635 | ' invokes a descriptor method: "x.__get__(a)".\n' |
| 636 | '\n' |
| 637 | 'Instance Binding\n' |
| 638 | ' If binding to an object instance, "a.x" is ' |
| 639 | 'transformed into the\n' |
| 640 | ' call: "type(a).__dict__[\'x\'].__get__(a, ' |
| 641 | 'type(a))".\n' |
| 642 | '\n' |
| 643 | 'Class Binding\n' |
| 644 | ' If binding to a class, "A.x" is transformed into ' |
| 645 | 'the call:\n' |
| 646 | ' "A.__dict__[\'x\'].__get__(None, A)".\n' |
| 647 | '\n' |
| 648 | 'Super Binding\n' |
| 649 | ' If "a" is an instance of "super", then the binding ' |
| 650 | '"super(B,\n' |
| 651 | ' obj).m()" searches "obj.__class__.__mro__" for the ' |
| 652 | 'base class "A"\n' |
| 653 | ' immediately preceding "B" and then invokes the ' |
| 654 | 'descriptor with the\n' |
| 655 | ' call: "A.__dict__[\'m\'].__get__(obj, ' |
| 656 | 'obj.__class__)".\n' |
| 657 | '\n' |
| 658 | 'For instance bindings, the precedence of descriptor ' |
| 659 | 'invocation depends\n' |
| 660 | 'on the which descriptor methods are defined. A ' |
| 661 | 'descriptor can define\n' |
| 662 | 'any combination of "__get__()", "__set__()" and ' |
| 663 | '"__delete__()". If it\n' |
| 664 | 'does not define "__get__()", then accessing the ' |
| 665 | 'attribute will return\n' |
| 666 | 'the descriptor object itself unless there is a value ' |
| 667 | "in the object's\n" |
| 668 | 'instance dictionary. If the descriptor defines ' |
| 669 | '"__set__()" and/or\n' |
| 670 | '"__delete__()", it is a data descriptor; if it defines ' |
| 671 | 'neither, it is\n' |
| 672 | 'a non-data descriptor. Normally, data descriptors ' |
| 673 | 'define both\n' |
| 674 | '"__get__()" and "__set__()", while non-data ' |
| 675 | 'descriptors have just the\n' |
| 676 | '"__get__()" method. Data descriptors with "__set__()" ' |
| 677 | 'and "__get__()"\n' |
| 678 | 'defined always override a redefinition in an instance ' |
| 679 | 'dictionary. In\n' |
| 680 | 'contrast, non-data descriptors can be overridden by ' |
| 681 | 'instances.\n' |
| 682 | '\n' |
| 683 | 'Python methods (including "staticmethod()" and ' |
| 684 | '"classmethod()") are\n' |
| 685 | 'implemented as non-data descriptors. Accordingly, ' |
| 686 | 'instances can\n' |
| 687 | 'redefine and override methods. This allows individual ' |
| 688 | 'instances to\n' |
| 689 | 'acquire behaviors that differ from other instances of ' |
| 690 | 'the same class.\n' |
| 691 | '\n' |
| 692 | 'The "property()" function is implemented as a data ' |
| 693 | 'descriptor.\n' |
| 694 | 'Accordingly, instances cannot override the behavior of ' |
| 695 | 'a property.\n' |
| 696 | '\n' |
| 697 | '\n' |
| 698 | '__slots__\n' |
| 699 | '=========\n' |
| 700 | '\n' |
| 701 | 'By default, instances of classes have a dictionary for ' |
| 702 | 'attribute\n' |
| 703 | 'storage. This wastes space for objects having very ' |
| 704 | 'few instance\n' |
| 705 | 'variables. The space consumption can become acute ' |
| 706 | 'when creating large\n' |
| 707 | 'numbers of instances.\n' |
| 708 | '\n' |
| 709 | 'The default can be overridden by defining *__slots__* ' |
| 710 | 'in a class\n' |
| 711 | 'definition. The *__slots__* declaration takes a ' |
| 712 | 'sequence of instance\n' |
| 713 | 'variables and reserves just enough space in each ' |
| 714 | 'instance to hold a\n' |
| 715 | 'value for each variable. Space is saved because ' |
| 716 | '*__dict__* is not\n' |
| 717 | 'created for each instance.\n' |
| 718 | '\n' |
| 719 | 'object.__slots__\n' |
| 720 | '\n' |
| 721 | ' This class variable can be assigned a string, ' |
| 722 | 'iterable, or sequence\n' |
| 723 | ' of strings with variable names used by instances. ' |
| 724 | '*__slots__*\n' |
| 725 | ' reserves space for the declared variables and ' |
| 726 | 'prevents the\n' |
| 727 | ' automatic creation of *__dict__* and *__weakref__* ' |
| 728 | 'for each\n' |
| 729 | ' instance.\n' |
| 730 | '\n' |
| 731 | '\n' |
| 732 | 'Notes on using *__slots__*\n' |
| 733 | '--------------------------\n' |
| 734 | '\n' |
| 735 | '* When inheriting from a class without *__slots__*, ' |
| 736 | 'the *__dict__*\n' |
| 737 | ' attribute of that class will always be accessible, ' |
| 738 | 'so a *__slots__*\n' |
| 739 | ' definition in the subclass is meaningless.\n' |
| 740 | '\n' |
| 741 | '* Without a *__dict__* variable, instances cannot be ' |
| 742 | 'assigned new\n' |
| 743 | ' variables not listed in the *__slots__* definition. ' |
| 744 | 'Attempts to\n' |
| 745 | ' assign to an unlisted variable name raises ' |
| 746 | '"AttributeError". If\n' |
| 747 | ' dynamic assignment of new variables is desired, then ' |
| 748 | 'add\n' |
| 749 | ' "\'__dict__\'" to the sequence of strings in the ' |
| 750 | '*__slots__*\n' |
| 751 | ' declaration.\n' |
| 752 | '\n' |
| 753 | '* Without a *__weakref__* variable for each instance, ' |
| 754 | 'classes\n' |
| 755 | ' defining *__slots__* do not support weak references ' |
| 756 | 'to its\n' |
| 757 | ' instances. If weak reference support is needed, then ' |
| 758 | 'add\n' |
| 759 | ' "\'__weakref__\'" to the sequence of strings in the ' |
| 760 | '*__slots__*\n' |
| 761 | ' declaration.\n' |
| 762 | '\n' |
| 763 | '* *__slots__* are implemented at the class level by ' |
| 764 | 'creating\n' |
| 765 | ' descriptors (*Implementing Descriptors*) for each ' |
| 766 | 'variable name. As\n' |
| 767 | ' a result, class attributes cannot be used to set ' |
| 768 | 'default values for\n' |
| 769 | ' instance variables defined by *__slots__*; ' |
| 770 | 'otherwise, the class\n' |
| 771 | ' attribute would overwrite the descriptor ' |
| 772 | 'assignment.\n' |
| 773 | '\n' |
| 774 | '* The action of a *__slots__* declaration is limited ' |
| 775 | 'to the class\n' |
| 776 | ' where it is defined. As a result, subclasses will ' |
| 777 | 'have a *__dict__*\n' |
| 778 | ' unless they also define *__slots__* (which must only ' |
| 779 | 'contain names\n' |
| 780 | ' of any *additional* slots).\n' |
| 781 | '\n' |
| 782 | '* If a class defines a slot also defined in a base ' |
| 783 | 'class, the\n' |
| 784 | ' instance variable defined by the base class slot is ' |
| 785 | 'inaccessible\n' |
| 786 | ' (except by retrieving its descriptor directly from ' |
| 787 | 'the base class).\n' |
| 788 | ' This renders the meaning of the program undefined. ' |
| 789 | 'In the future, a\n' |
| 790 | ' check may be added to prevent this.\n' |
| 791 | '\n' |
| 792 | '* Nonempty *__slots__* does not work for classes ' |
| 793 | 'derived from\n' |
| 794 | ' "variable-length" built-in types such as "int", ' |
| 795 | '"bytes" and "tuple".\n' |
| 796 | '\n' |
| 797 | '* Any non-string iterable may be assigned to ' |
| 798 | '*__slots__*. Mappings\n' |
| 799 | ' may also be used; however, in the future, special ' |
| 800 | 'meaning may be\n' |
| 801 | ' assigned to the values corresponding to each key.\n' |
| 802 | '\n' |
| 803 | '* *__class__* assignment works only if both classes ' |
| 804 | 'have the same\n' |
| 805 | ' *__slots__*.\n', |
| 806 | 'attribute-references': '\n' |
| 807 | 'Attribute references\n' |
| 808 | '********************\n' |
| 809 | '\n' |
| 810 | 'An attribute reference is a primary followed by a ' |
| 811 | 'period and a name:\n' |
| 812 | '\n' |
| 813 | ' attributeref ::= primary "." identifier\n' |
| 814 | '\n' |
| 815 | 'The primary must evaluate to an object of a type ' |
| 816 | 'that supports\n' |
| 817 | 'attribute references, which most objects do. This ' |
| 818 | 'object is then\n' |
| 819 | 'asked to produce the attribute whose name is the ' |
| 820 | 'identifier. This\n' |
| 821 | 'production can be customized by overriding the ' |
| 822 | '"__getattr__()" method.\n' |
| 823 | 'If this attribute is not available, the exception ' |
| 824 | '"AttributeError" is\n' |
| 825 | 'raised. Otherwise, the type and value of the ' |
| 826 | 'object produced is\n' |
| 827 | 'determined by the object. Multiple evaluations of ' |
| 828 | 'the same attribute\n' |
| 829 | 'reference may yield different objects.\n', |
| 830 | 'augassign': '\n' |
| 831 | 'Augmented assignment statements\n' |
| 832 | '*******************************\n' |
| 833 | '\n' |
| 834 | 'Augmented assignment is the combination, in a single ' |
| 835 | 'statement, of a\n' |
| 836 | 'binary operation and an assignment statement:\n' |
| 837 | '\n' |
| 838 | ' augmented_assignment_stmt ::= augtarget augop ' |
| 839 | '(expression_list | yield_expression)\n' |
| 840 | ' augtarget ::= identifier | attributeref | ' |
| 841 | 'subscription | slicing\n' |
| 842 | ' augop ::= "+=" | "-=" | "*=" | "@=" | ' |
| 843 | '"/=" | "//=" | "%=" | "**="\n' |
| 844 | ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n' |
| 845 | '\n' |
| 846 | '(See section *Primaries* for the syntax definitions of the ' |
| 847 | 'last three\n' |
| 848 | 'symbols.)\n' |
| 849 | '\n' |
| 850 | 'An augmented assignment evaluates the target (which, unlike ' |
| 851 | 'normal\n' |
| 852 | 'assignment statements, cannot be an unpacking) and the ' |
| 853 | 'expression\n' |
| 854 | 'list, performs the binary operation specific to the type of ' |
| 855 | 'assignment\n' |
| 856 | 'on the two operands, and assigns the result to the original ' |
| 857 | 'target.\n' |
| 858 | 'The target is only evaluated once.\n' |
| 859 | '\n' |
| 860 | 'An augmented assignment expression like "x += 1" can be ' |
| 861 | 'rewritten as\n' |
| 862 | '"x = x + 1" to achieve a similar, but not exactly equal ' |
| 863 | 'effect. In the\n' |
| 864 | 'augmented version, "x" is only evaluated once. Also, when ' |
| 865 | 'possible,\n' |
| 866 | 'the actual operation is performed *in-place*, meaning that ' |
| 867 | 'rather than\n' |
| 868 | 'creating a new object and assigning that to the target, the ' |
| 869 | 'old object\n' |
| 870 | 'is modified instead.\n' |
| 871 | '\n' |
| 872 | 'Unlike normal assignments, augmented assignments evaluate the ' |
| 873 | 'left-\n' |
| 874 | 'hand side *before* evaluating the right-hand side. For ' |
| 875 | 'example, "a[i]\n' |
| 876 | '+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and ' |
| 877 | 'performs\n' |
| 878 | 'the addition, and lastly, it writes the result back to ' |
| 879 | '"a[i]".\n' |
| 880 | '\n' |
| 881 | 'With the exception of assigning to tuples and multiple ' |
| 882 | 'targets in a\n' |
| 883 | 'single statement, the assignment done by augmented ' |
| 884 | 'assignment\n' |
| 885 | 'statements is handled the same way as normal assignments. ' |
| 886 | 'Similarly,\n' |
| 887 | 'with the exception of the possible *in-place* behavior, the ' |
| 888 | 'binary\n' |
| 889 | 'operation performed by augmented assignment is the same as ' |
| 890 | 'the normal\n' |
| 891 | 'binary operations.\n' |
| 892 | '\n' |
| 893 | 'For targets which are attribute references, the same *caveat ' |
| 894 | 'about\n' |
| 895 | 'class and instance attributes* applies as for regular ' |
| 896 | 'assignments.\n', |
| 897 | 'binary': '\n' |
| 898 | 'Binary arithmetic operations\n' |
| 899 | '****************************\n' |
| 900 | '\n' |
| 901 | 'The binary arithmetic operations have the conventional priority\n' |
| 902 | 'levels. Note that some of these operations also apply to ' |
| 903 | 'certain non-\n' |
| 904 | 'numeric types. Apart from the power operator, there are only ' |
| 905 | 'two\n' |
| 906 | 'levels, one for multiplicative operators and one for additive\n' |
| 907 | 'operators:\n' |
| 908 | '\n' |
| 909 | ' m_expr ::= u_expr | m_expr "*" u_expr | m_expr "@" m_expr |\n' |
| 910 | ' m_expr "//" u_expr| m_expr "/" u_expr |\n' |
| 911 | ' m_expr "%" u_expr\n' |
| 912 | ' a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr\n' |
| 913 | '\n' |
| 914 | 'The "*" (multiplication) operator yields the product of its ' |
| 915 | 'arguments.\n' |
| 916 | 'The arguments must either both be numbers, or one argument must ' |
| 917 | 'be an\n' |
| 918 | 'integer and the other must be a sequence. In the former case, ' |
| 919 | 'the\n' |
| 920 | 'numbers are converted to a common type and then multiplied ' |
| 921 | 'together.\n' |
| 922 | 'In the latter case, sequence repetition is performed; a ' |
| 923 | 'negative\n' |
| 924 | 'repetition factor yields an empty sequence.\n' |
| 925 | '\n' |
| 926 | 'The "@" (at) operator is intended to be used for matrix\n' |
| 927 | 'multiplication. No builtin Python types implement this ' |
| 928 | 'operator.\n' |
| 929 | '\n' |
| 930 | 'New in version 3.5.\n' |
| 931 | '\n' |
| 932 | 'The "/" (division) and "//" (floor division) operators yield ' |
| 933 | 'the\n' |
| 934 | 'quotient of their arguments. The numeric arguments are first\n' |
| 935 | 'converted to a common type. Division of integers yields a float, ' |
| 936 | 'while\n' |
| 937 | 'floor division of integers results in an integer; the result is ' |
| 938 | 'that\n' |
| 939 | "of mathematical division with the 'floor' function applied to " |
| 940 | 'the\n' |
| 941 | 'result. Division by zero raises the "ZeroDivisionError" ' |
| 942 | 'exception.\n' |
| 943 | '\n' |
| 944 | 'The "%" (modulo) operator yields the remainder from the division ' |
| 945 | 'of\n' |
| 946 | 'the first argument by the second. The numeric arguments are ' |
| 947 | 'first\n' |
| 948 | 'converted to a common type. A zero right argument raises the\n' |
| 949 | '"ZeroDivisionError" exception. The arguments may be floating ' |
| 950 | 'point\n' |
| 951 | 'numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals ' |
| 952 | '"4*0.7 +\n' |
| 953 | '0.34".) The modulo operator always yields a result with the ' |
| 954 | 'same sign\n' |
| 955 | 'as its second operand (or zero); the absolute value of the ' |
| 956 | 'result is\n' |
| 957 | 'strictly smaller than the absolute value of the second operand ' |
| 958 | '[1].\n' |
| 959 | '\n' |
| 960 | 'The floor division and modulo operators are connected by the ' |
| 961 | 'following\n' |
| 962 | 'identity: "x == (x//y)*y + (x%y)". Floor division and modulo ' |
| 963 | 'are also\n' |
| 964 | 'connected with the built-in function "divmod()": "divmod(x, y) ' |
| 965 | '==\n' |
| 966 | '(x//y, x%y)". [2].\n' |
| 967 | '\n' |
| 968 | 'In addition to performing the modulo operation on numbers, the ' |
| 969 | '"%"\n' |
| 970 | 'operator is also overloaded by string objects to perform ' |
| 971 | 'old-style\n' |
| 972 | 'string formatting (also known as interpolation). The syntax ' |
| 973 | 'for\n' |
| 974 | 'string formatting is described in the Python Library Reference,\n' |
| 975 | 'section *printf-style String Formatting*.\n' |
| 976 | '\n' |
| 977 | 'The floor division operator, the modulo operator, and the ' |
| 978 | '"divmod()"\n' |
| 979 | 'function are not defined for complex numbers. Instead, convert ' |
| 980 | 'to a\n' |
| 981 | 'floating point number using the "abs()" function if ' |
| 982 | 'appropriate.\n' |
| 983 | '\n' |
| 984 | 'The "+" (addition) operator yields the sum of its arguments. ' |
| 985 | 'The\n' |
| 986 | 'arguments must either both be numbers or both be sequences of ' |
| 987 | 'the same\n' |
| 988 | 'type. In the former case, the numbers are converted to a common ' |
| 989 | 'type\n' |
| 990 | 'and then added together. In the latter case, the sequences are\n' |
| 991 | 'concatenated.\n' |
| 992 | '\n' |
| 993 | 'The "-" (subtraction) operator yields the difference of its ' |
| 994 | 'arguments.\n' |
| 995 | 'The numeric arguments are first converted to a common type.\n', |
| 996 | 'bitwise': '\n' |
| 997 | 'Binary bitwise operations\n' |
| 998 | '*************************\n' |
| 999 | '\n' |
| 1000 | 'Each of the three bitwise operations has a different priority ' |
| 1001 | 'level:\n' |
| 1002 | '\n' |
| 1003 | ' and_expr ::= shift_expr | and_expr "&" shift_expr\n' |
| 1004 | ' xor_expr ::= and_expr | xor_expr "^" and_expr\n' |
| 1005 | ' or_expr ::= xor_expr | or_expr "|" xor_expr\n' |
| 1006 | '\n' |
| 1007 | 'The "&" operator yields the bitwise AND of its arguments, which ' |
| 1008 | 'must\n' |
| 1009 | 'be integers.\n' |
| 1010 | '\n' |
| 1011 | 'The "^" operator yields the bitwise XOR (exclusive OR) of its\n' |
| 1012 | 'arguments, which must be integers.\n' |
| 1013 | '\n' |
| 1014 | 'The "|" operator yields the bitwise (inclusive) OR of its ' |
| 1015 | 'arguments,\n' |
| 1016 | 'which must be integers.\n', |
| 1017 | 'bltin-code-objects': '\n' |
| 1018 | 'Code Objects\n' |
| 1019 | '************\n' |
| 1020 | '\n' |
| 1021 | 'Code objects are used by the implementation to ' |
| 1022 | 'represent "pseudo-\n' |
| 1023 | 'compiled" executable Python code such as a function ' |
| 1024 | 'body. They differ\n' |
| 1025 | "from function objects because they don't contain a " |
| 1026 | 'reference to their\n' |
| 1027 | 'global execution environment. Code objects are ' |
| 1028 | 'returned by the built-\n' |
| 1029 | 'in "compile()" function and can be extracted from ' |
| 1030 | 'function objects\n' |
| 1031 | 'through their "__code__" attribute. See also the ' |
| 1032 | '"code" module.\n' |
| 1033 | '\n' |
| 1034 | 'A code object can be executed or evaluated by ' |
| 1035 | 'passing it (instead of a\n' |
| 1036 | 'source string) to the "exec()" or "eval()" built-in ' |
| 1037 | 'functions.\n' |
| 1038 | '\n' |
| 1039 | 'See *The standard type hierarchy* for more ' |
| 1040 | 'information.\n', |
| 1041 | 'bltin-ellipsis-object': '\n' |
| 1042 | 'The Ellipsis Object\n' |
| 1043 | '*******************\n' |
| 1044 | '\n' |
| 1045 | 'This object is commonly used by slicing (see ' |
| 1046 | '*Slicings*). It supports\n' |
| 1047 | 'no special operations. There is exactly one ' |
| 1048 | 'ellipsis object, named\n' |
| 1049 | '"Ellipsis" (a built-in name). "type(Ellipsis)()" ' |
| 1050 | 'produces the\n' |
| 1051 | '"Ellipsis" singleton.\n' |
| 1052 | '\n' |
| 1053 | 'It is written as "Ellipsis" or "...".\n', |
| 1054 | 'bltin-null-object': '\n' |
| 1055 | 'The Null Object\n' |
| 1056 | '***************\n' |
| 1057 | '\n' |
| 1058 | "This object is returned by functions that don't " |
| 1059 | 'explicitly return a\n' |
| 1060 | 'value. It supports no special operations. There is ' |
| 1061 | 'exactly one null\n' |
| 1062 | 'object, named "None" (a built-in name). ' |
| 1063 | '"type(None)()" produces the\n' |
| 1064 | 'same singleton.\n' |
| 1065 | '\n' |
| 1066 | 'It is written as "None".\n', |
| 1067 | 'bltin-type-objects': '\n' |
| 1068 | 'Type Objects\n' |
| 1069 | '************\n' |
| 1070 | '\n' |
| 1071 | 'Type objects represent the various object types. An ' |
| 1072 | "object's type is\n" |
| 1073 | 'accessed by the built-in function "type()". There ' |
| 1074 | 'are no special\n' |
| 1075 | 'operations on types. The standard module "types" ' |
| 1076 | 'defines names for\n' |
| 1077 | 'all standard built-in types.\n' |
| 1078 | '\n' |
| 1079 | 'Types are written like this: "<class \'int\'>".\n', |
| 1080 | 'booleans': '\n' |
| 1081 | 'Boolean operations\n' |
| 1082 | '******************\n' |
| 1083 | '\n' |
| 1084 | ' or_test ::= and_test | or_test "or" and_test\n' |
| 1085 | ' and_test ::= not_test | and_test "and" not_test\n' |
| 1086 | ' not_test ::= comparison | "not" not_test\n' |
| 1087 | '\n' |
| 1088 | 'In the context of Boolean operations, and also when ' |
| 1089 | 'expressions are\n' |
| 1090 | 'used by control flow statements, the following values are ' |
| 1091 | 'interpreted\n' |
| 1092 | 'as false: "False", "None", numeric zero of all types, and ' |
| 1093 | 'empty\n' |
| 1094 | 'strings and containers (including strings, tuples, lists,\n' |
| 1095 | 'dictionaries, sets and frozensets). All other values are ' |
| 1096 | 'interpreted\n' |
| 1097 | 'as true. User-defined objects can customize their truth value ' |
| 1098 | 'by\n' |
| 1099 | 'providing a "__bool__()" method.\n' |
| 1100 | '\n' |
| 1101 | 'The operator "not" yields "True" if its argument is false, ' |
| 1102 | '"False"\n' |
| 1103 | 'otherwise.\n' |
| 1104 | '\n' |
| 1105 | 'The expression "x and y" first evaluates *x*; if *x* is false, ' |
| 1106 | 'its\n' |
| 1107 | 'value is returned; otherwise, *y* is evaluated and the ' |
| 1108 | 'resulting value\n' |
| 1109 | 'is returned.\n' |
| 1110 | '\n' |
| 1111 | 'The expression "x or y" first evaluates *x*; if *x* is true, ' |
| 1112 | 'its value\n' |
| 1113 | 'is returned; otherwise, *y* is evaluated and the resulting ' |
| 1114 | 'value is\n' |
| 1115 | 'returned.\n' |
| 1116 | '\n' |
| 1117 | '(Note that neither "and" nor "or" restrict the value and type ' |
| 1118 | 'they\n' |
| 1119 | 'return to "False" and "True", but rather return the last ' |
| 1120 | 'evaluated\n' |
| 1121 | 'argument. This is sometimes useful, e.g., if "s" is a string ' |
| 1122 | 'that\n' |
| 1123 | 'should be replaced by a default value if it is empty, the ' |
| 1124 | 'expression\n' |
| 1125 | '"s or \'foo\'" yields the desired value. Because "not" has to ' |
| 1126 | 'create a\n' |
| 1127 | 'new value, it returns a boolean value regardless of the type ' |
| 1128 | 'of its\n' |
| 1129 | 'argument (for example, "not \'foo\'" produces "False" rather ' |
| 1130 | 'than "\'\'".)\n', |
| 1131 | 'break': '\n' |
| 1132 | 'The "break" statement\n' |
| 1133 | '*********************\n' |
| 1134 | '\n' |
| 1135 | ' break_stmt ::= "break"\n' |
| 1136 | '\n' |
| 1137 | '"break" may only occur syntactically nested in a "for" or ' |
| 1138 | '"while"\n' |
| 1139 | 'loop, but not nested in a function or class definition within ' |
| 1140 | 'that\n' |
| 1141 | 'loop.\n' |
| 1142 | '\n' |
| 1143 | 'It terminates the nearest enclosing loop, skipping the optional ' |
| 1144 | '"else"\n' |
| 1145 | 'clause if the loop has one.\n' |
| 1146 | '\n' |
| 1147 | 'If a "for" loop is terminated by "break", the loop control ' |
| 1148 | 'target\n' |
| 1149 | 'keeps its current value.\n' |
| 1150 | '\n' |
| 1151 | 'When "break" passes control out of a "try" statement with a ' |
| 1152 | '"finally"\n' |
| 1153 | 'clause, that "finally" clause is executed before really leaving ' |
| 1154 | 'the\n' |
| 1155 | 'loop.\n', |
| 1156 | 'callable-types': '\n' |
| 1157 | 'Emulating callable objects\n' |
| 1158 | '**************************\n' |
| 1159 | '\n' |
| 1160 | 'object.__call__(self[, args...])\n' |
| 1161 | '\n' |
| 1162 | ' Called when the instance is "called" as a function; ' |
| 1163 | 'if this method\n' |
| 1164 | ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n' |
| 1165 | ' "x.__call__(arg1, arg2, ...)".\n', |
| 1166 | 'calls': '\n' |
| 1167 | 'Calls\n' |
| 1168 | '*****\n' |
| 1169 | '\n' |
| 1170 | 'A call calls a callable object (e.g., a *function*) with a ' |
| 1171 | 'possibly\n' |
| 1172 | 'empty series of *arguments*:\n' |
| 1173 | '\n' |
| 1174 | ' call ::= primary "(" [argument_list [","] | ' |
| 1175 | 'comprehension] ")"\n' |
| 1176 | ' argument_list ::= positional_arguments ["," ' |
| 1177 | 'keyword_arguments]\n' |
| 1178 | ' ["," "*" expression] ["," ' |
| 1179 | 'keyword_arguments]\n' |
| 1180 | ' ["," "**" expression]\n' |
| 1181 | ' | keyword_arguments ["," "*" expression]\n' |
| 1182 | ' ["," keyword_arguments] ["," "**" ' |
| 1183 | 'expression]\n' |
| 1184 | ' | "*" expression ["," keyword_arguments] ' |
| 1185 | '["," "**" expression]\n' |
| 1186 | ' | "**" expression\n' |
| 1187 | ' positional_arguments ::= expression ("," expression)*\n' |
| 1188 | ' keyword_arguments ::= keyword_item ("," keyword_item)*\n' |
| 1189 | ' keyword_item ::= identifier "=" expression\n' |
| 1190 | '\n' |
| 1191 | 'An optional trailing comma may be present after the positional ' |
| 1192 | 'and\n' |
| 1193 | 'keyword arguments but does not affect the semantics.\n' |
| 1194 | '\n' |
| 1195 | 'The primary must evaluate to a callable object (user-defined\n' |
| 1196 | 'functions, built-in functions, methods of built-in objects, ' |
| 1197 | 'class\n' |
| 1198 | 'objects, methods of class instances, and all objects having a\n' |
| 1199 | '"__call__()" method are callable). All argument expressions are\n' |
| 1200 | 'evaluated before the call is attempted. Please refer to section\n' |
| 1201 | '*Function definitions* for the syntax of formal *parameter* ' |
| 1202 | 'lists.\n' |
| 1203 | '\n' |
| 1204 | 'If keyword arguments are present, they are first converted to\n' |
| 1205 | 'positional arguments, as follows. First, a list of unfilled ' |
| 1206 | 'slots is\n' |
| 1207 | 'created for the formal parameters. If there are N positional\n' |
| 1208 | 'arguments, they are placed in the first N slots. Next, for each\n' |
| 1209 | 'keyword argument, the identifier is used to determine the\n' |
| 1210 | 'corresponding slot (if the identifier is the same as the first ' |
| 1211 | 'formal\n' |
| 1212 | 'parameter name, the first slot is used, and so on). If the slot ' |
| 1213 | 'is\n' |
| 1214 | 'already filled, a "TypeError" exception is raised. Otherwise, ' |
| 1215 | 'the\n' |
| 1216 | 'value of the argument is placed in the slot, filling it (even if ' |
| 1217 | 'the\n' |
| 1218 | 'expression is "None", it fills the slot). When all arguments ' |
| 1219 | 'have\n' |
| 1220 | 'been processed, the slots that are still unfilled are filled with ' |
| 1221 | 'the\n' |
| 1222 | 'corresponding default value from the function definition. ' |
| 1223 | '(Default\n' |
| 1224 | 'values are calculated, once, when the function is defined; thus, ' |
| 1225 | 'a\n' |
| 1226 | 'mutable object such as a list or dictionary used as default value ' |
| 1227 | 'will\n' |
| 1228 | "be shared by all calls that don't specify an argument value for " |
| 1229 | 'the\n' |
| 1230 | 'corresponding slot; this should usually be avoided.) If there ' |
| 1231 | 'are any\n' |
| 1232 | 'unfilled slots for which no default value is specified, a ' |
| 1233 | '"TypeError"\n' |
| 1234 | 'exception is raised. Otherwise, the list of filled slots is used ' |
| 1235 | 'as\n' |
| 1236 | 'the argument list for the call.\n' |
| 1237 | '\n' |
| 1238 | '**CPython implementation detail:** An implementation may provide\n' |
| 1239 | 'built-in functions whose positional parameters do not have names, ' |
| 1240 | 'even\n' |
| 1241 | "if they are 'named' for the purpose of documentation, and which\n" |
| 1242 | 'therefore cannot be supplied by keyword. In CPython, this is the ' |
| 1243 | 'case\n' |
| 1244 | 'for functions implemented in C that use "PyArg_ParseTuple()" to ' |
| 1245 | 'parse\n' |
| 1246 | 'their arguments.\n' |
| 1247 | '\n' |
| 1248 | 'If there are more positional arguments than there are formal ' |
| 1249 | 'parameter\n' |
| 1250 | 'slots, a "TypeError" exception is raised, unless a formal ' |
| 1251 | 'parameter\n' |
| 1252 | 'using the syntax "*identifier" is present; in this case, that ' |
| 1253 | 'formal\n' |
| 1254 | 'parameter receives a tuple containing the excess positional ' |
| 1255 | 'arguments\n' |
| 1256 | '(or an empty tuple if there were no excess positional ' |
| 1257 | 'arguments).\n' |
| 1258 | '\n' |
| 1259 | 'If any keyword argument does not correspond to a formal ' |
| 1260 | 'parameter\n' |
| 1261 | 'name, a "TypeError" exception is raised, unless a formal ' |
| 1262 | 'parameter\n' |
| 1263 | 'using the syntax "**identifier" is present; in this case, that ' |
| 1264 | 'formal\n' |
| 1265 | 'parameter receives a dictionary containing the excess keyword\n' |
| 1266 | 'arguments (using the keywords as keys and the argument values as\n' |
| 1267 | 'corresponding values), or a (new) empty dictionary if there were ' |
| 1268 | 'no\n' |
| 1269 | 'excess keyword arguments.\n' |
| 1270 | '\n' |
| 1271 | 'If the syntax "*expression" appears in the function call, ' |
| 1272 | '"expression"\n' |
| 1273 | 'must evaluate to an iterable. Elements from this iterable are ' |
| 1274 | 'treated\n' |
| 1275 | 'as if they were additional positional arguments; if there are\n' |
| 1276 | 'positional arguments *x1*, ..., *xN*, and "expression" evaluates ' |
| 1277 | 'to a\n' |
| 1278 | 'sequence *y1*, ..., *yM*, this is equivalent to a call with M+N\n' |
| 1279 | 'positional arguments *x1*, ..., *xN*, *y1*, ..., *yM*.\n' |
| 1280 | '\n' |
| 1281 | 'A consequence of this is that although the "*expression" syntax ' |
| 1282 | 'may\n' |
| 1283 | 'appear *after* some keyword arguments, it is processed *before* ' |
| 1284 | 'the\n' |
| 1285 | 'keyword arguments (and the "**expression" argument, if any -- ' |
| 1286 | 'see\n' |
| 1287 | 'below). So:\n' |
| 1288 | '\n' |
| 1289 | ' >>> def f(a, b):\n' |
| 1290 | ' ... print(a, b)\n' |
| 1291 | ' ...\n' |
| 1292 | ' >>> f(b=1, *(2,))\n' |
| 1293 | ' 2 1\n' |
| 1294 | ' >>> f(a=1, *(2,))\n' |
| 1295 | ' Traceback (most recent call last):\n' |
| 1296 | ' File "<stdin>", line 1, in ?\n' |
| 1297 | " TypeError: f() got multiple values for keyword argument 'a'\n" |
| 1298 | ' >>> f(1, *(2,))\n' |
| 1299 | ' 1 2\n' |
| 1300 | '\n' |
| 1301 | 'It is unusual for both keyword arguments and the "*expression" ' |
| 1302 | 'syntax\n' |
| 1303 | 'to be used in the same call, so in practice this confusion does ' |
| 1304 | 'not\n' |
| 1305 | 'arise.\n' |
| 1306 | '\n' |
| 1307 | 'If the syntax "**expression" appears in the function call,\n' |
| 1308 | '"expression" must evaluate to a mapping, the contents of which ' |
| 1309 | 'are\n' |
| 1310 | 'treated as additional keyword arguments. In the case of a ' |
| 1311 | 'keyword\n' |
| 1312 | 'appearing in both "expression" and as an explicit keyword ' |
| 1313 | 'argument, a\n' |
| 1314 | '"TypeError" exception is raised.\n' |
| 1315 | '\n' |
| 1316 | 'Formal parameters using the syntax "*identifier" or ' |
| 1317 | '"**identifier"\n' |
| 1318 | 'cannot be used as positional argument slots or as keyword ' |
| 1319 | 'argument\n' |
| 1320 | 'names.\n' |
| 1321 | '\n' |
| 1322 | 'A call always returns some value, possibly "None", unless it ' |
| 1323 | 'raises an\n' |
| 1324 | 'exception. How this value is computed depends on the type of ' |
| 1325 | 'the\n' |
| 1326 | 'callable object.\n' |
| 1327 | '\n' |
| 1328 | 'If it is---\n' |
| 1329 | '\n' |
| 1330 | 'a user-defined function:\n' |
| 1331 | ' The code block for the function is executed, passing it the\n' |
| 1332 | ' argument list. The first thing the code block will do is bind ' |
| 1333 | 'the\n' |
| 1334 | ' formal parameters to the arguments; this is described in ' |
| 1335 | 'section\n' |
| 1336 | ' *Function definitions*. When the code block executes a ' |
| 1337 | '"return"\n' |
| 1338 | ' statement, this specifies the return value of the function ' |
| 1339 | 'call.\n' |
| 1340 | '\n' |
| 1341 | 'a built-in function or method:\n' |
| 1342 | ' The result is up to the interpreter; see *Built-in Functions* ' |
| 1343 | 'for\n' |
| 1344 | ' the descriptions of built-in functions and methods.\n' |
| 1345 | '\n' |
| 1346 | 'a class object:\n' |
| 1347 | ' A new instance of that class is returned.\n' |
| 1348 | '\n' |
| 1349 | 'a class instance method:\n' |
| 1350 | ' The corresponding user-defined function is called, with an ' |
| 1351 | 'argument\n' |
| 1352 | ' list that is one longer than the argument list of the call: ' |
| 1353 | 'the\n' |
| 1354 | ' instance becomes the first argument.\n' |
| 1355 | '\n' |
| 1356 | 'a class instance:\n' |
| 1357 | ' The class must define a "__call__()" method; the effect is ' |
| 1358 | 'then the\n' |
| 1359 | ' same as if that method was called.\n', |
| 1360 | 'class': '\n' |
| 1361 | 'Class definitions\n' |
| 1362 | '*****************\n' |
| 1363 | '\n' |
| 1364 | 'A class definition defines a class object (see section *The ' |
| 1365 | 'standard\n' |
| 1366 | 'type hierarchy*):\n' |
| 1367 | '\n' |
| 1368 | ' classdef ::= [decorators] "class" classname [inheritance] ' |
| 1369 | '":" suite\n' |
| 1370 | ' inheritance ::= "(" [parameter_list] ")"\n' |
| 1371 | ' classname ::= identifier\n' |
| 1372 | '\n' |
| 1373 | 'A class definition is an executable statement. The inheritance ' |
| 1374 | 'list\n' |
| 1375 | 'usually gives a list of base classes (see *Customizing class ' |
| 1376 | 'creation*\n' |
| 1377 | 'for more advanced uses), so each item in the list should evaluate ' |
| 1378 | 'to a\n' |
| 1379 | 'class object which allows subclassing. Classes without an ' |
| 1380 | 'inheritance\n' |
| 1381 | 'list inherit, by default, from the base class "object"; hence,\n' |
| 1382 | '\n' |
| 1383 | ' class Foo:\n' |
| 1384 | ' pass\n' |
| 1385 | '\n' |
| 1386 | 'is equivalent to\n' |
| 1387 | '\n' |
| 1388 | ' class Foo(object):\n' |
| 1389 | ' pass\n' |
| 1390 | '\n' |
| 1391 | "The class's suite is then executed in a new execution frame (see\n" |
| 1392 | '*Naming and binding*), using a newly created local namespace and ' |
| 1393 | 'the\n' |
| 1394 | 'original global namespace. (Usually, the suite contains mostly\n' |
| 1395 | "function definitions.) When the class's suite finishes " |
| 1396 | 'execution, its\n' |
| 1397 | 'execution frame is discarded but its local namespace is saved. ' |
| 1398 | '[4] A\n' |
| 1399 | 'class object is then created using the inheritance list for the ' |
| 1400 | 'base\n' |
| 1401 | 'classes and the saved local namespace for the attribute ' |
| 1402 | 'dictionary.\n' |
| 1403 | 'The class name is bound to this class object in the original ' |
| 1404 | 'local\n' |
| 1405 | 'namespace.\n' |
| 1406 | '\n' |
| 1407 | 'Class creation can be customized heavily using *metaclasses*.\n' |
| 1408 | '\n' |
| 1409 | 'Classes can also be decorated: just like when decorating ' |
| 1410 | 'functions,\n' |
| 1411 | '\n' |
| 1412 | ' @f1(arg)\n' |
| 1413 | ' @f2\n' |
| 1414 | ' class Foo: pass\n' |
| 1415 | '\n' |
| 1416 | 'is equivalent to\n' |
| 1417 | '\n' |
| 1418 | ' class Foo: pass\n' |
| 1419 | ' Foo = f1(arg)(f2(Foo))\n' |
| 1420 | '\n' |
| 1421 | 'The evaluation rules for the decorator expressions are the same ' |
| 1422 | 'as for\n' |
| 1423 | 'function decorators. The result must be a class object, which is ' |
| 1424 | 'then\n' |
| 1425 | 'bound to the class name.\n' |
| 1426 | '\n' |
| 1427 | "**Programmer's note:** Variables defined in the class definition " |
| 1428 | 'are\n' |
| 1429 | 'class attributes; they are shared by instances. Instance ' |
| 1430 | 'attributes\n' |
| 1431 | 'can be set in a method with "self.name = value". Both class and\n' |
| 1432 | 'instance attributes are accessible through the notation ' |
| 1433 | '""self.name"",\n' |
| 1434 | 'and an instance attribute hides a class attribute with the same ' |
| 1435 | 'name\n' |
| 1436 | 'when accessed in this way. Class attributes can be used as ' |
| 1437 | 'defaults\n' |
| 1438 | 'for instance attributes, but using mutable values there can lead ' |
| 1439 | 'to\n' |
| 1440 | 'unexpected results. *Descriptors* can be used to create ' |
| 1441 | 'instance\n' |
| 1442 | 'variables with different implementation details.\n' |
| 1443 | '\n' |
| 1444 | 'See also: **PEP 3115** - Metaclasses in Python 3 **PEP 3129** -\n' |
| 1445 | ' Class Decorators\n', |
| 1446 | 'comparisons': '\n' |
| 1447 | 'Comparisons\n' |
| 1448 | '***********\n' |
| 1449 | '\n' |
| 1450 | 'Unlike C, all comparison operations in Python have the same ' |
| 1451 | 'priority,\n' |
| 1452 | 'which is lower than that of any arithmetic, shifting or ' |
| 1453 | 'bitwise\n' |
| 1454 | 'operation. Also unlike C, expressions like "a < b < c" ' |
| 1455 | 'have the\n' |
| 1456 | 'interpretation that is conventional in mathematics:\n' |
| 1457 | '\n' |
| 1458 | ' comparison ::= or_expr ( comp_operator or_expr )*\n' |
| 1459 | ' comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!="\n' |
| 1460 | ' | "is" ["not"] | ["not"] "in"\n' |
| 1461 | '\n' |
| 1462 | 'Comparisons yield boolean values: "True" or "False".\n' |
| 1463 | '\n' |
| 1464 | 'Comparisons can be chained arbitrarily, e.g., "x < y <= z" ' |
| 1465 | 'is\n' |
| 1466 | 'equivalent to "x < y and y <= z", except that "y" is ' |
| 1467 | 'evaluated only\n' |
| 1468 | 'once (but in both cases "z" is not evaluated at all when "x ' |
| 1469 | '< y" is\n' |
| 1470 | 'found to be false).\n' |
| 1471 | '\n' |
| 1472 | 'Formally, if *a*, *b*, *c*, ..., *y*, *z* are expressions ' |
| 1473 | 'and *op1*,\n' |
| 1474 | '*op2*, ..., *opN* are comparison operators, then "a op1 b ' |
| 1475 | 'op2 c ... y\n' |
| 1476 | 'opN z" is equivalent to "a op1 b and b op2 c and ... y opN ' |
| 1477 | 'z", except\n' |
| 1478 | 'that each expression is evaluated at most once.\n' |
| 1479 | '\n' |
| 1480 | 'Note that "a op1 b op2 c" doesn\'t imply any kind of ' |
| 1481 | 'comparison between\n' |
| 1482 | '*a* and *c*, so that, e.g., "x < y > z" is perfectly legal ' |
| 1483 | '(though\n' |
| 1484 | 'perhaps not pretty).\n' |
| 1485 | '\n' |
| 1486 | 'The operators "<", ">", "==", ">=", "<=", and "!=" compare ' |
| 1487 | 'the values\n' |
| 1488 | 'of two objects. The objects need not have the same type. ' |
| 1489 | 'If both are\n' |
| 1490 | 'numbers, they are converted to a common type. Otherwise, ' |
| 1491 | 'the "==" and\n' |
| 1492 | '"!=" operators *always* consider objects of different types ' |
| 1493 | 'to be\n' |
| 1494 | 'unequal, while the "<", ">", ">=" and "<=" operators raise ' |
| 1495 | 'a\n' |
| 1496 | '"TypeError" when comparing objects of different types that ' |
| 1497 | 'do not\n' |
| 1498 | 'implement these operators for the given pair of types. You ' |
| 1499 | 'can\n' |
| 1500 | 'control comparison behavior of objects of non-built-in ' |
| 1501 | 'types by\n' |
| 1502 | 'defining rich comparison methods like "__gt__()", described ' |
| 1503 | 'in section\n' |
| 1504 | '*Basic customization*.\n' |
| 1505 | '\n' |
| 1506 | 'Comparison of objects of the same type depends on the ' |
| 1507 | 'type:\n' |
| 1508 | '\n' |
| 1509 | '* Numbers are compared arithmetically.\n' |
| 1510 | '\n' |
| 1511 | '* The values "float(\'NaN\')" and "Decimal(\'NaN\')" are ' |
| 1512 | 'special. They\n' |
| 1513 | ' are identical to themselves, "x is x" but are not equal ' |
| 1514 | 'to\n' |
| 1515 | ' themselves, "x != x". Additionally, comparing any value ' |
| 1516 | 'to a\n' |
| 1517 | ' not-a-number value will return "False". For example, ' |
| 1518 | 'both "3 <\n' |
| 1519 | ' float(\'NaN\')" and "float(\'NaN\') < 3" will return ' |
| 1520 | '"False".\n' |
| 1521 | '\n' |
| 1522 | '* Bytes objects are compared lexicographically using the ' |
| 1523 | 'numeric\n' |
| 1524 | ' values of their elements.\n' |
| 1525 | '\n' |
| 1526 | '* Strings are compared lexicographically using the numeric\n' |
| 1527 | ' equivalents (the result of the built-in function "ord()") ' |
| 1528 | 'of their\n' |
| 1529 | " characters. [3] String and bytes object can't be " |
| 1530 | 'compared!\n' |
| 1531 | '\n' |
| 1532 | '* Tuples and lists are compared lexicographically using ' |
| 1533 | 'comparison\n' |
| 1534 | ' of corresponding elements. This means that to compare ' |
| 1535 | 'equal, each\n' |
| 1536 | ' element must compare equal and the two sequences must be ' |
| 1537 | 'of the same\n' |
| 1538 | ' type and have the same length.\n' |
| 1539 | '\n' |
| 1540 | ' If not equal, the sequences are ordered the same as their ' |
| 1541 | 'first\n' |
| 1542 | ' differing elements. For example, "[1,2,x] <= [1,2,y]" ' |
| 1543 | 'has the same\n' |
| 1544 | ' value as "x <= y". If the corresponding element does not ' |
| 1545 | 'exist, the\n' |
| 1546 | ' shorter sequence is ordered first (for example, "[1,2] < ' |
| 1547 | '[1,2,3]").\n' |
| 1548 | '\n' |
| 1549 | '* Mappings (dictionaries) compare equal if and only if they ' |
| 1550 | 'have the\n' |
| 1551 | ' same "(key, value)" pairs. Order comparisons "(\'<\', ' |
| 1552 | "'<=', '>=',\n" |
| 1553 | ' \'>\')" raise "TypeError".\n' |
| 1554 | '\n' |
| 1555 | '* Sets and frozensets define comparison operators to mean ' |
| 1556 | 'subset and\n' |
| 1557 | ' superset tests. Those relations do not define total ' |
| 1558 | 'orderings (the\n' |
| 1559 | ' two sets "{1,2}" and "{2,3}" are not equal, nor subsets ' |
| 1560 | 'of one\n' |
| 1561 | ' another, nor supersets of one another). Accordingly, ' |
| 1562 | 'sets are not\n' |
| 1563 | ' appropriate arguments for functions which depend on total ' |
| 1564 | 'ordering.\n' |
| 1565 | ' For example, "min()", "max()", and "sorted()" produce ' |
| 1566 | 'undefined\n' |
| 1567 | ' results given a list of sets as inputs.\n' |
| 1568 | '\n' |
| 1569 | '* Most other objects of built-in types compare unequal ' |
| 1570 | 'unless they\n' |
| 1571 | ' are the same object; the choice whether one object is ' |
| 1572 | 'considered\n' |
| 1573 | ' smaller or larger than another one is made arbitrarily ' |
| 1574 | 'but\n' |
| 1575 | ' consistently within one execution of a program.\n' |
| 1576 | '\n' |
| 1577 | 'Comparison of objects of differing types depends on whether ' |
| 1578 | 'either of\n' |
| 1579 | 'the types provide explicit support for the comparison. ' |
| 1580 | 'Most numeric\n' |
| 1581 | 'types can be compared with one another. When cross-type ' |
| 1582 | 'comparison is\n' |
| 1583 | 'not supported, the comparison method returns ' |
| 1584 | '"NotImplemented".\n' |
| 1585 | '\n' |
| 1586 | 'The operators "in" and "not in" test for membership. "x in ' |
| 1587 | 's"\n' |
| 1588 | 'evaluates to true if *x* is a member of *s*, and false ' |
| 1589 | 'otherwise. "x\n' |
| 1590 | 'not in s" returns the negation of "x in s". All built-in ' |
| 1591 | 'sequences\n' |
| 1592 | 'and set types support this as well as dictionary, for which ' |
| 1593 | '"in" tests\n' |
| 1594 | 'whether the dictionary has a given key. For container types ' |
| 1595 | 'such as\n' |
| 1596 | 'list, tuple, set, frozenset, dict, or collections.deque, ' |
| 1597 | 'the\n' |
| 1598 | 'expression "x in y" is equivalent to "any(x is e or x == e ' |
| 1599 | 'for e in\n' |
| 1600 | 'y)".\n' |
| 1601 | '\n' |
| 1602 | 'For the string and bytes types, "x in y" is true if and ' |
| 1603 | 'only if *x* is\n' |
| 1604 | 'a substring of *y*. An equivalent test is "y.find(x) != ' |
| 1605 | '-1". Empty\n' |
| 1606 | 'strings are always considered to be a substring of any ' |
| 1607 | 'other string,\n' |
| 1608 | 'so """ in "abc"" will return "True".\n' |
| 1609 | '\n' |
| 1610 | 'For user-defined classes which define the "__contains__()" ' |
| 1611 | 'method, "x\n' |
| 1612 | 'in y" is true if and only if "y.__contains__(x)" is true.\n' |
| 1613 | '\n' |
| 1614 | 'For user-defined classes which do not define ' |
| 1615 | '"__contains__()" but do\n' |
| 1616 | 'define "__iter__()", "x in y" is true if some value "z" ' |
| 1617 | 'with "x == z"\n' |
| 1618 | 'is produced while iterating over "y". If an exception is ' |
| 1619 | 'raised\n' |
| 1620 | 'during the iteration, it is as if "in" raised that ' |
| 1621 | 'exception.\n' |
| 1622 | '\n' |
| 1623 | 'Lastly, the old-style iteration protocol is tried: if a ' |
| 1624 | 'class defines\n' |
| 1625 | '"__getitem__()", "x in y" is true if and only if there is a ' |
| 1626 | 'non-\n' |
| 1627 | 'negative integer index *i* such that "x == y[i]", and all ' |
| 1628 | 'lower\n' |
| 1629 | 'integer indices do not raise "IndexError" exception. (If ' |
| 1630 | 'any other\n' |
| 1631 | 'exception is raised, it is as if "in" raised that ' |
| 1632 | 'exception).\n' |
| 1633 | '\n' |
| 1634 | 'The operator "not in" is defined to have the inverse true ' |
| 1635 | 'value of\n' |
| 1636 | '"in".\n' |
| 1637 | '\n' |
| 1638 | 'The operators "is" and "is not" test for object identity: ' |
| 1639 | '"x is y" is\n' |
| 1640 | 'true if and only if *x* and *y* are the same object. "x is ' |
| 1641 | 'not y"\n' |
| 1642 | 'yields the inverse truth value. [4]\n', |
| 1643 | 'compound': '\n' |
| 1644 | 'Compound statements\n' |
| 1645 | '*******************\n' |
| 1646 | '\n' |
| 1647 | 'Compound statements contain (groups of) other statements; they ' |
| 1648 | 'affect\n' |
| 1649 | 'or control the execution of those other statements in some ' |
| 1650 | 'way. In\n' |
| 1651 | 'general, compound statements span multiple lines, although in ' |
| 1652 | 'simple\n' |
| 1653 | 'incarnations a whole compound statement may be contained in ' |
| 1654 | 'one line.\n' |
| 1655 | '\n' |
| 1656 | 'The "if", "while" and "for" statements implement traditional ' |
| 1657 | 'control\n' |
| 1658 | 'flow constructs. "try" specifies exception handlers and/or ' |
| 1659 | 'cleanup\n' |
| 1660 | 'code for a group of statements, while the "with" statement ' |
| 1661 | 'allows the\n' |
| 1662 | 'execution of initialization and finalization code around a ' |
| 1663 | 'block of\n' |
| 1664 | 'code. Function and class definitions are also syntactically ' |
| 1665 | 'compound\n' |
| 1666 | 'statements.\n' |
| 1667 | '\n' |
| 1668 | "A compound statement consists of one or more 'clauses.' A " |
| 1669 | 'clause\n' |
| 1670 | "consists of a header and a 'suite.' The clause headers of a\n" |
| 1671 | 'particular compound statement are all at the same indentation ' |
| 1672 | 'level.\n' |
| 1673 | 'Each clause header begins with a uniquely identifying keyword ' |
| 1674 | 'and ends\n' |
| 1675 | 'with a colon. A suite is a group of statements controlled by ' |
| 1676 | 'a\n' |
| 1677 | 'clause. A suite can be one or more semicolon-separated ' |
| 1678 | 'simple\n' |
| 1679 | 'statements on the same line as the header, following the ' |
| 1680 | "header's\n" |
| 1681 | 'colon, or it can be one or more indented statements on ' |
| 1682 | 'subsequent\n' |
| 1683 | 'lines. Only the latter form of a suite can contain nested ' |
| 1684 | 'compound\n' |
| 1685 | 'statements; the following is illegal, mostly because it ' |
| 1686 | "wouldn't be\n" |
| 1687 | 'clear to which "if" clause a following "else" clause would ' |
| 1688 | 'belong:\n' |
| 1689 | '\n' |
| 1690 | ' if test1: if test2: print(x)\n' |
| 1691 | '\n' |
| 1692 | 'Also note that the semicolon binds tighter than the colon in ' |
| 1693 | 'this\n' |
| 1694 | 'context, so that in the following example, either all or none ' |
| 1695 | 'of the\n' |
| 1696 | '"print()" calls are executed:\n' |
| 1697 | '\n' |
| 1698 | ' if x < y < z: print(x); print(y); print(z)\n' |
| 1699 | '\n' |
| 1700 | 'Summarizing:\n' |
| 1701 | '\n' |
| 1702 | ' compound_stmt ::= if_stmt\n' |
| 1703 | ' | while_stmt\n' |
| 1704 | ' | for_stmt\n' |
| 1705 | ' | try_stmt\n' |
| 1706 | ' | with_stmt\n' |
| 1707 | ' | funcdef\n' |
| 1708 | ' | classdef\n' |
| 1709 | ' | async_with_stmt\n' |
| 1710 | ' | async_for_stmt\n' |
| 1711 | ' | async_funcdef\n' |
| 1712 | ' suite ::= stmt_list NEWLINE | NEWLINE INDENT ' |
| 1713 | 'statement+ DEDENT\n' |
| 1714 | ' statement ::= stmt_list NEWLINE | compound_stmt\n' |
| 1715 | ' stmt_list ::= simple_stmt (";" simple_stmt)* [";"]\n' |
| 1716 | '\n' |
| 1717 | 'Note that statements always end in a "NEWLINE" possibly ' |
| 1718 | 'followed by a\n' |
| 1719 | '"DEDENT". Also note that optional continuation clauses always ' |
| 1720 | 'begin\n' |
| 1721 | 'with a keyword that cannot start a statement, thus there are ' |
| 1722 | 'no\n' |
| 1723 | 'ambiguities (the \'dangling "else"\' problem is solved in ' |
| 1724 | 'Python by\n' |
| 1725 | 'requiring nested "if" statements to be indented).\n' |
| 1726 | '\n' |
| 1727 | 'The formatting of the grammar rules in the following sections ' |
| 1728 | 'places\n' |
| 1729 | 'each clause on a separate line for clarity.\n' |
| 1730 | '\n' |
| 1731 | '\n' |
| 1732 | 'The "if" statement\n' |
| 1733 | '==================\n' |
| 1734 | '\n' |
| 1735 | 'The "if" statement is used for conditional execution:\n' |
| 1736 | '\n' |
| 1737 | ' if_stmt ::= "if" expression ":" suite\n' |
| 1738 | ' ( "elif" expression ":" suite )*\n' |
| 1739 | ' ["else" ":" suite]\n' |
| 1740 | '\n' |
| 1741 | 'It selects exactly one of the suites by evaluating the ' |
| 1742 | 'expressions one\n' |
| 1743 | 'by one until one is found to be true (see section *Boolean ' |
| 1744 | 'operations*\n' |
| 1745 | 'for the definition of true and false); then that suite is ' |
| 1746 | 'executed\n' |
| 1747 | '(and no other part of the "if" statement is executed or ' |
| 1748 | 'evaluated).\n' |
| 1749 | 'If all expressions are false, the suite of the "else" clause, ' |
| 1750 | 'if\n' |
| 1751 | 'present, is executed.\n' |
| 1752 | '\n' |
| 1753 | '\n' |
| 1754 | 'The "while" statement\n' |
| 1755 | '=====================\n' |
| 1756 | '\n' |
| 1757 | 'The "while" statement is used for repeated execution as long ' |
| 1758 | 'as an\n' |
| 1759 | 'expression is true:\n' |
| 1760 | '\n' |
| 1761 | ' while_stmt ::= "while" expression ":" suite\n' |
| 1762 | ' ["else" ":" suite]\n' |
| 1763 | '\n' |
| 1764 | 'This repeatedly tests the expression and, if it is true, ' |
| 1765 | 'executes the\n' |
| 1766 | 'first suite; if the expression is false (which may be the ' |
| 1767 | 'first time\n' |
| 1768 | 'it is tested) the suite of the "else" clause, if present, is ' |
| 1769 | 'executed\n' |
| 1770 | 'and the loop terminates.\n' |
| 1771 | '\n' |
| 1772 | 'A "break" statement executed in the first suite terminates the ' |
| 1773 | 'loop\n' |
| 1774 | 'without executing the "else" clause\'s suite. A "continue" ' |
| 1775 | 'statement\n' |
| 1776 | 'executed in the first suite skips the rest of the suite and ' |
| 1777 | 'goes back\n' |
| 1778 | 'to testing the expression.\n' |
| 1779 | '\n' |
| 1780 | '\n' |
| 1781 | 'The "for" statement\n' |
| 1782 | '===================\n' |
| 1783 | '\n' |
| 1784 | 'The "for" statement is used to iterate over the elements of a ' |
| 1785 | 'sequence\n' |
| 1786 | '(such as a string, tuple or list) or other iterable object:\n' |
| 1787 | '\n' |
| 1788 | ' for_stmt ::= "for" target_list "in" expression_list ":" ' |
| 1789 | 'suite\n' |
| 1790 | ' ["else" ":" suite]\n' |
| 1791 | '\n' |
| 1792 | 'The expression list is evaluated once; it should yield an ' |
| 1793 | 'iterable\n' |
| 1794 | 'object. An iterator is created for the result of the\n' |
| 1795 | '"expression_list". The suite is then executed once for each ' |
| 1796 | 'item\n' |
| 1797 | 'provided by the iterator, in the order returned by the ' |
| 1798 | 'iterator. Each\n' |
| 1799 | 'item in turn is assigned to the target list using the standard ' |
| 1800 | 'rules\n' |
| 1801 | 'for assignments (see *Assignment statements*), and then the ' |
| 1802 | 'suite is\n' |
| 1803 | 'executed. When the items are exhausted (which is immediately ' |
| 1804 | 'when the\n' |
| 1805 | 'sequence is empty or an iterator raises a "StopIteration" ' |
| 1806 | 'exception),\n' |
| 1807 | 'the suite in the "else" clause, if present, is executed, and ' |
| 1808 | 'the loop\n' |
| 1809 | 'terminates.\n' |
| 1810 | '\n' |
| 1811 | 'A "break" statement executed in the first suite terminates the ' |
| 1812 | 'loop\n' |
| 1813 | 'without executing the "else" clause\'s suite. A "continue" ' |
| 1814 | 'statement\n' |
| 1815 | 'executed in the first suite skips the rest of the suite and ' |
| 1816 | 'continues\n' |
| 1817 | 'with the next item, or with the "else" clause if there is no ' |
| 1818 | 'next\n' |
| 1819 | 'item.\n' |
| 1820 | '\n' |
| 1821 | 'The for-loop makes assignments to the variables(s) in the ' |
| 1822 | 'target list.\n' |
| 1823 | 'This overwrites all previous assignments to those variables ' |
| 1824 | 'including\n' |
| 1825 | 'those made in the suite of the for-loop:\n' |
| 1826 | '\n' |
| 1827 | ' for i in range(10):\n' |
| 1828 | ' print(i)\n' |
| 1829 | ' i = 5 # this will not affect the for-loop\n' |
| 1830 | ' # because i will be overwritten with ' |
| 1831 | 'the next\n' |
| 1832 | ' # index in the range\n' |
| 1833 | '\n' |
| 1834 | 'Names in the target list are not deleted when the loop is ' |
| 1835 | 'finished,\n' |
| 1836 | 'but if the sequence is empty, they will not have been assigned ' |
| 1837 | 'to at\n' |
| 1838 | 'all by the loop. Hint: the built-in function "range()" ' |
| 1839 | 'returns an\n' |
| 1840 | 'iterator of integers suitable to emulate the effect of ' |
| 1841 | 'Pascal\'s "for i\n' |
| 1842 | ':= a to b do"; e.g., "list(range(3))" returns the list "[0, 1, ' |
| 1843 | '2]".\n' |
| 1844 | '\n' |
| 1845 | 'Note: There is a subtlety when the sequence is being modified ' |
| 1846 | 'by the\n' |
| 1847 | ' loop (this can only occur for mutable sequences, i.e. ' |
| 1848 | 'lists). An\n' |
| 1849 | ' internal counter is used to keep track of which item is used ' |
| 1850 | 'next,\n' |
| 1851 | ' and this is incremented on each iteration. When this ' |
| 1852 | 'counter has\n' |
| 1853 | ' reached the length of the sequence the loop terminates. ' |
| 1854 | 'This means\n' |
| 1855 | ' that if the suite deletes the current (or a previous) item ' |
| 1856 | 'from the\n' |
| 1857 | ' sequence, the next item will be skipped (since it gets the ' |
| 1858 | 'index of\n' |
| 1859 | ' the current item which has already been treated). Likewise, ' |
| 1860 | 'if the\n' |
| 1861 | ' suite inserts an item in the sequence before the current ' |
| 1862 | 'item, the\n' |
| 1863 | ' current item will be treated again the next time through the ' |
| 1864 | 'loop.\n' |
| 1865 | ' This can lead to nasty bugs that can be avoided by making a\n' |
| 1866 | ' temporary copy using a slice of the whole sequence, e.g.,\n' |
| 1867 | '\n' |
| 1868 | ' for x in a[:]:\n' |
| 1869 | ' if x < 0: a.remove(x)\n' |
| 1870 | '\n' |
| 1871 | '\n' |
| 1872 | 'The "try" statement\n' |
| 1873 | '===================\n' |
| 1874 | '\n' |
| 1875 | 'The "try" statement specifies exception handlers and/or ' |
| 1876 | 'cleanup code\n' |
| 1877 | 'for a group of statements:\n' |
| 1878 | '\n' |
| 1879 | ' try_stmt ::= try1_stmt | try2_stmt\n' |
| 1880 | ' try1_stmt ::= "try" ":" suite\n' |
| 1881 | ' ("except" [expression ["as" identifier]] ":" ' |
| 1882 | 'suite)+\n' |
| 1883 | ' ["else" ":" suite]\n' |
| 1884 | ' ["finally" ":" suite]\n' |
| 1885 | ' try2_stmt ::= "try" ":" suite\n' |
| 1886 | ' "finally" ":" suite\n' |
| 1887 | '\n' |
| 1888 | 'The "except" clause(s) specify one or more exception handlers. ' |
| 1889 | 'When no\n' |
| 1890 | 'exception occurs in the "try" clause, no exception handler is\n' |
| 1891 | 'executed. When an exception occurs in the "try" suite, a ' |
| 1892 | 'search for an\n' |
| 1893 | 'exception handler is started. This search inspects the except ' |
| 1894 | 'clauses\n' |
| 1895 | 'in turn until one is found that matches the exception. An ' |
| 1896 | 'expression-\n' |
| 1897 | 'less except clause, if present, must be last; it matches any\n' |
| 1898 | 'exception. For an except clause with an expression, that ' |
| 1899 | 'expression\n' |
| 1900 | 'is evaluated, and the clause matches the exception if the ' |
| 1901 | 'resulting\n' |
| 1902 | 'object is "compatible" with the exception. An object is ' |
| 1903 | 'compatible\n' |
| 1904 | 'with an exception if it is the class or a base class of the ' |
| 1905 | 'exception\n' |
| 1906 | 'object or a tuple containing an item compatible with the ' |
| 1907 | 'exception.\n' |
| 1908 | '\n' |
| 1909 | 'If no except clause matches the exception, the search for an ' |
| 1910 | 'exception\n' |
| 1911 | 'handler continues in the surrounding code and on the ' |
| 1912 | 'invocation stack.\n' |
| 1913 | '[1]\n' |
| 1914 | '\n' |
| 1915 | 'If the evaluation of an expression in the header of an except ' |
| 1916 | 'clause\n' |
| 1917 | 'raises an exception, the original search for a handler is ' |
| 1918 | 'canceled and\n' |
| 1919 | 'a search starts for the new exception in the surrounding code ' |
| 1920 | 'and on\n' |
| 1921 | 'the call stack (it is treated as if the entire "try" statement ' |
| 1922 | 'raised\n' |
| 1923 | 'the exception).\n' |
| 1924 | '\n' |
| 1925 | 'When a matching except clause is found, the exception is ' |
| 1926 | 'assigned to\n' |
| 1927 | 'the target specified after the "as" keyword in that except ' |
| 1928 | 'clause, if\n' |
| 1929 | "present, and the except clause's suite is executed. All " |
| 1930 | 'except\n' |
| 1931 | 'clauses must have an executable block. When the end of this ' |
| 1932 | 'block is\n' |
| 1933 | 'reached, execution continues normally after the entire try ' |
| 1934 | 'statement.\n' |
| 1935 | '(This means that if two nested handlers exist for the same ' |
| 1936 | 'exception,\n' |
| 1937 | 'and the exception occurs in the try clause of the inner ' |
| 1938 | 'handler, the\n' |
| 1939 | 'outer handler will not handle the exception.)\n' |
| 1940 | '\n' |
| 1941 | 'When an exception has been assigned using "as target", it is ' |
| 1942 | 'cleared\n' |
| 1943 | 'at the end of the except clause. This is as if\n' |
| 1944 | '\n' |
| 1945 | ' except E as N:\n' |
| 1946 | ' foo\n' |
| 1947 | '\n' |
| 1948 | 'was translated to\n' |
| 1949 | '\n' |
| 1950 | ' except E as N:\n' |
| 1951 | ' try:\n' |
| 1952 | ' foo\n' |
| 1953 | ' finally:\n' |
| 1954 | ' del N\n' |
| 1955 | '\n' |
| 1956 | 'This means the exception must be assigned to a different name ' |
| 1957 | 'to be\n' |
| 1958 | 'able to refer to it after the except clause. Exceptions are ' |
| 1959 | 'cleared\n' |
| 1960 | 'because with the traceback attached to them, they form a ' |
| 1961 | 'reference\n' |
| 1962 | 'cycle with the stack frame, keeping all locals in that frame ' |
| 1963 | 'alive\n' |
| 1964 | 'until the next garbage collection occurs.\n' |
| 1965 | '\n' |
| 1966 | "Before an except clause's suite is executed, details about " |
| 1967 | 'the\n' |
| 1968 | 'exception are stored in the "sys" module and can be accessed ' |
| 1969 | 'via\n' |
| 1970 | '"sys.exc_info()". "sys.exc_info()" returns a 3-tuple ' |
| 1971 | 'consisting of the\n' |
| 1972 | 'exception class, the exception instance and a traceback object ' |
| 1973 | '(see\n' |
| 1974 | 'section *The standard type hierarchy*) identifying the point ' |
| 1975 | 'in the\n' |
| 1976 | 'program where the exception occurred. "sys.exc_info()" values ' |
| 1977 | 'are\n' |
| 1978 | 'restored to their previous values (before the call) when ' |
| 1979 | 'returning\n' |
| 1980 | 'from a function that handled an exception.\n' |
| 1981 | '\n' |
| 1982 | 'The optional "else" clause is executed if and when control ' |
| 1983 | 'flows off\n' |
| 1984 | 'the end of the "try" clause. [2] Exceptions in the "else" ' |
| 1985 | 'clause are\n' |
| 1986 | 'not handled by the preceding "except" clauses.\n' |
| 1987 | '\n' |
| 1988 | 'If "finally" is present, it specifies a \'cleanup\' handler. ' |
| 1989 | 'The "try"\n' |
| 1990 | 'clause is executed, including any "except" and "else" ' |
| 1991 | 'clauses. If an\n' |
| 1992 | 'exception occurs in any of the clauses and is not handled, ' |
| 1993 | 'the\n' |
| 1994 | 'exception is temporarily saved. The "finally" clause is ' |
| 1995 | 'executed. If\n' |
| 1996 | 'there is a saved exception it is re-raised at the end of the ' |
| 1997 | '"finally"\n' |
| 1998 | 'clause. If the "finally" clause raises another exception, the ' |
| 1999 | 'saved\n' |
| 2000 | 'exception is set as the context of the new exception. If the ' |
| 2001 | '"finally"\n' |
| 2002 | 'clause executes a "return" or "break" statement, the saved ' |
| 2003 | 'exception\n' |
| 2004 | 'is discarded:\n' |
| 2005 | '\n' |
| 2006 | ' >>> def f():\n' |
| 2007 | ' ... try:\n' |
| 2008 | ' ... 1/0\n' |
| 2009 | ' ... finally:\n' |
| 2010 | ' ... return 42\n' |
| 2011 | ' ...\n' |
| 2012 | ' >>> f()\n' |
| 2013 | ' 42\n' |
| 2014 | '\n' |
| 2015 | 'The exception information is not available to the program ' |
| 2016 | 'during\n' |
| 2017 | 'execution of the "finally" clause.\n' |
| 2018 | '\n' |
| 2019 | 'When a "return", "break" or "continue" statement is executed ' |
| 2020 | 'in the\n' |
| 2021 | '"try" suite of a "try"..."finally" statement, the "finally" ' |
| 2022 | 'clause is\n' |
| 2023 | 'also executed \'on the way out.\' A "continue" statement is ' |
| 2024 | 'illegal in\n' |
| 2025 | 'the "finally" clause. (The reason is a problem with the ' |
| 2026 | 'current\n' |
| 2027 | 'implementation --- this restriction may be lifted in the ' |
| 2028 | 'future).\n' |
| 2029 | '\n' |
| 2030 | 'The return value of a function is determined by the last ' |
| 2031 | '"return"\n' |
| 2032 | 'statement executed. Since the "finally" clause always ' |
| 2033 | 'executes, a\n' |
| 2034 | '"return" statement executed in the "finally" clause will ' |
| 2035 | 'always be the\n' |
| 2036 | 'last one executed:\n' |
| 2037 | '\n' |
| 2038 | ' >>> def foo():\n' |
| 2039 | ' ... try:\n' |
| 2040 | " ... return 'try'\n" |
| 2041 | ' ... finally:\n' |
| 2042 | " ... return 'finally'\n" |
| 2043 | ' ...\n' |
| 2044 | ' >>> foo()\n' |
| 2045 | " 'finally'\n" |
| 2046 | '\n' |
| 2047 | 'Additional information on exceptions can be found in section\n' |
| 2048 | '*Exceptions*, and information on using the "raise" statement ' |
| 2049 | 'to\n' |
| 2050 | 'generate exceptions may be found in section *The raise ' |
| 2051 | 'statement*.\n' |
| 2052 | '\n' |
| 2053 | '\n' |
| 2054 | 'The "with" statement\n' |
| 2055 | '====================\n' |
| 2056 | '\n' |
| 2057 | 'The "with" statement is used to wrap the execution of a block ' |
| 2058 | 'with\n' |
| 2059 | 'methods defined by a context manager (see section *With ' |
| 2060 | 'Statement\n' |
| 2061 | 'Context Managers*). This allows common ' |
| 2062 | '"try"..."except"..."finally"\n' |
| 2063 | 'usage patterns to be encapsulated for convenient reuse.\n' |
| 2064 | '\n' |
| 2065 | ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n' |
| 2066 | ' with_item ::= expression ["as" target]\n' |
| 2067 | '\n' |
| 2068 | 'The execution of the "with" statement with one "item" proceeds ' |
| 2069 | 'as\n' |
| 2070 | 'follows:\n' |
| 2071 | '\n' |
| 2072 | '1. The context expression (the expression given in the ' |
| 2073 | '"with_item")\n' |
| 2074 | ' is evaluated to obtain a context manager.\n' |
| 2075 | '\n' |
| 2076 | '2. The context manager\'s "__exit__()" is loaded for later ' |
| 2077 | 'use.\n' |
| 2078 | '\n' |
| 2079 | '3. The context manager\'s "__enter__()" method is invoked.\n' |
| 2080 | '\n' |
| 2081 | '4. If a target was included in the "with" statement, the ' |
| 2082 | 'return\n' |
| 2083 | ' value from "__enter__()" is assigned to it.\n' |
| 2084 | '\n' |
| 2085 | ' Note: The "with" statement guarantees that if the ' |
| 2086 | '"__enter__()"\n' |
| 2087 | ' method returns without an error, then "__exit__()" will ' |
| 2088 | 'always be\n' |
| 2089 | ' called. Thus, if an error occurs during the assignment to ' |
| 2090 | 'the\n' |
| 2091 | ' target list, it will be treated the same as an error ' |
| 2092 | 'occurring\n' |
| 2093 | ' within the suite would be. See step 6 below.\n' |
| 2094 | '\n' |
| 2095 | '5. The suite is executed.\n' |
| 2096 | '\n' |
| 2097 | '6. The context manager\'s "__exit__()" method is invoked. If ' |
| 2098 | 'an\n' |
| 2099 | ' exception caused the suite to be exited, its type, value, ' |
| 2100 | 'and\n' |
| 2101 | ' traceback are passed as arguments to "__exit__()". ' |
| 2102 | 'Otherwise, three\n' |
| 2103 | ' "None" arguments are supplied.\n' |
| 2104 | '\n' |
| 2105 | ' If the suite was exited due to an exception, and the return ' |
| 2106 | 'value\n' |
| 2107 | ' from the "__exit__()" method was false, the exception is ' |
| 2108 | 'reraised.\n' |
| 2109 | ' If the return value was true, the exception is suppressed, ' |
| 2110 | 'and\n' |
| 2111 | ' execution continues with the statement following the ' |
| 2112 | '"with"\n' |
| 2113 | ' statement.\n' |
| 2114 | '\n' |
| 2115 | ' If the suite was exited for any reason other than an ' |
| 2116 | 'exception, the\n' |
| 2117 | ' return value from "__exit__()" is ignored, and execution ' |
| 2118 | 'proceeds\n' |
| 2119 | ' at the normal location for the kind of exit that was ' |
| 2120 | 'taken.\n' |
| 2121 | '\n' |
| 2122 | 'With more than one item, the context managers are processed as ' |
| 2123 | 'if\n' |
| 2124 | 'multiple "with" statements were nested:\n' |
| 2125 | '\n' |
| 2126 | ' with A() as a, B() as b:\n' |
| 2127 | ' suite\n' |
| 2128 | '\n' |
| 2129 | 'is equivalent to\n' |
| 2130 | '\n' |
| 2131 | ' with A() as a:\n' |
| 2132 | ' with B() as b:\n' |
| 2133 | ' suite\n' |
| 2134 | '\n' |
| 2135 | 'Changed in version 3.1: Support for multiple context ' |
| 2136 | 'expressions.\n' |
| 2137 | '\n' |
| 2138 | 'See also: **PEP 0343** - The "with" statement\n' |
| 2139 | '\n' |
| 2140 | ' The specification, background, and examples for the ' |
| 2141 | 'Python "with"\n' |
| 2142 | ' statement.\n' |
| 2143 | '\n' |
| 2144 | '\n' |
| 2145 | 'Function definitions\n' |
| 2146 | '====================\n' |
| 2147 | '\n' |
| 2148 | 'A function definition defines a user-defined function object ' |
| 2149 | '(see\n' |
| 2150 | 'section *The standard type hierarchy*):\n' |
| 2151 | '\n' |
| 2152 | ' funcdef ::= [decorators] "def" funcname "(" ' |
| 2153 | '[parameter_list] ")" ["->" expression] ":" suite\n' |
| 2154 | ' decorators ::= decorator+\n' |
| 2155 | ' decorator ::= "@" dotted_name ["(" [parameter_list ' |
| 2156 | '[","]] ")"] NEWLINE\n' |
| 2157 | ' dotted_name ::= identifier ("." identifier)*\n' |
| 2158 | ' parameter_list ::= (defparameter ",")*\n' |
| 2159 | ' | "*" [parameter] ("," defparameter)* ' |
| 2160 | '["," "**" parameter]\n' |
| 2161 | ' | "**" parameter\n' |
| 2162 | ' | defparameter [","] )\n' |
| 2163 | ' parameter ::= identifier [":" expression]\n' |
| 2164 | ' defparameter ::= parameter ["=" expression]\n' |
| 2165 | ' funcname ::= identifier\n' |
| 2166 | '\n' |
| 2167 | 'A function definition is an executable statement. Its ' |
| 2168 | 'execution binds\n' |
| 2169 | 'the function name in the current local namespace to a function ' |
| 2170 | 'object\n' |
| 2171 | '(a wrapper around the executable code for the function). ' |
| 2172 | 'This\n' |
| 2173 | 'function object contains a reference to the current global ' |
| 2174 | 'namespace\n' |
| 2175 | 'as the global namespace to be used when the function is ' |
| 2176 | 'called.\n' |
| 2177 | '\n' |
| 2178 | 'The function definition does not execute the function body; ' |
| 2179 | 'this gets\n' |
| 2180 | 'executed only when the function is called. [3]\n' |
| 2181 | '\n' |
| 2182 | 'A function definition may be wrapped by one or more ' |
| 2183 | '*decorator*\n' |
| 2184 | 'expressions. Decorator expressions are evaluated when the ' |
| 2185 | 'function is\n' |
| 2186 | 'defined, in the scope that contains the function definition. ' |
| 2187 | 'The\n' |
| 2188 | 'result must be a callable, which is invoked with the function ' |
| 2189 | 'object\n' |
| 2190 | 'as the only argument. The returned value is bound to the ' |
| 2191 | 'function name\n' |
| 2192 | 'instead of the function object. Multiple decorators are ' |
| 2193 | 'applied in\n' |
| 2194 | 'nested fashion. For example, the following code\n' |
| 2195 | '\n' |
| 2196 | ' @f1(arg)\n' |
| 2197 | ' @f2\n' |
| 2198 | ' def func(): pass\n' |
| 2199 | '\n' |
| 2200 | 'is equivalent to\n' |
| 2201 | '\n' |
| 2202 | ' def func(): pass\n' |
| 2203 | ' func = f1(arg)(f2(func))\n' |
| 2204 | '\n' |
| 2205 | 'When one or more *parameters* have the form *parameter* "="\n' |
| 2206 | '*expression*, the function is said to have "default parameter ' |
| 2207 | 'values."\n' |
| 2208 | 'For a parameter with a default value, the corresponding ' |
| 2209 | '*argument* may\n' |
| 2210 | "be omitted from a call, in which case the parameter's default " |
| 2211 | 'value is\n' |
| 2212 | 'substituted. If a parameter has a default value, all ' |
| 2213 | 'following\n' |
| 2214 | 'parameters up until the ""*"" must also have a default value ' |
| 2215 | '--- this\n' |
| 2216 | 'is a syntactic restriction that is not expressed by the ' |
| 2217 | 'grammar.\n' |
| 2218 | '\n' |
| 2219 | '**Default parameter values are evaluated from left to right ' |
| 2220 | 'when the\n' |
| 2221 | 'function definition is executed.** This means that the ' |
| 2222 | 'expression is\n' |
| 2223 | 'evaluated once, when the function is defined, and that the ' |
| 2224 | 'same "pre-\n' |
| 2225 | 'computed" value is used for each call. This is especially ' |
| 2226 | 'important\n' |
| 2227 | 'to understand when a default parameter is a mutable object, ' |
| 2228 | 'such as a\n' |
| 2229 | 'list or a dictionary: if the function modifies the object ' |
| 2230 | '(e.g. by\n' |
| 2231 | 'appending an item to a list), the default value is in effect ' |
| 2232 | 'modified.\n' |
| 2233 | 'This is generally not what was intended. A way around this is ' |
| 2234 | 'to use\n' |
| 2235 | '"None" as the default, and explicitly test for it in the body ' |
| 2236 | 'of the\n' |
| 2237 | 'function, e.g.:\n' |
| 2238 | '\n' |
| 2239 | ' def whats_on_the_telly(penguin=None):\n' |
| 2240 | ' if penguin is None:\n' |
| 2241 | ' penguin = []\n' |
| 2242 | ' penguin.append("property of the zoo")\n' |
| 2243 | ' return penguin\n' |
| 2244 | '\n' |
| 2245 | 'Function call semantics are described in more detail in ' |
| 2246 | 'section\n' |
| 2247 | '*Calls*. A function call always assigns values to all ' |
| 2248 | 'parameters\n' |
| 2249 | 'mentioned in the parameter list, either from position ' |
| 2250 | 'arguments, from\n' |
| 2251 | 'keyword arguments, or from default values. If the form\n' |
| 2252 | '""*identifier"" is present, it is initialized to a tuple ' |
| 2253 | 'receiving any\n' |
| 2254 | 'excess positional parameters, defaulting to the empty tuple. ' |
| 2255 | 'If the\n' |
| 2256 | 'form ""**identifier"" is present, it is initialized to a new\n' |
| 2257 | 'dictionary receiving any excess keyword arguments, defaulting ' |
| 2258 | 'to a new\n' |
| 2259 | 'empty dictionary. Parameters after ""*"" or ""*identifier"" ' |
| 2260 | 'are\n' |
| 2261 | 'keyword-only parameters and may only be passed used keyword ' |
| 2262 | 'arguments.\n' |
| 2263 | '\n' |
| 2264 | 'Parameters may have annotations of the form "": expression"" ' |
| 2265 | 'following\n' |
| 2266 | 'the parameter name. Any parameter may have an annotation even ' |
| 2267 | 'those\n' |
| 2268 | 'of the form "*identifier" or "**identifier". Functions may ' |
| 2269 | 'have\n' |
| 2270 | '"return" annotation of the form ""-> expression"" after the ' |
| 2271 | 'parameter\n' |
| 2272 | 'list. These annotations can be any valid Python expression ' |
| 2273 | 'and are\n' |
| 2274 | 'evaluated when the function definition is executed. ' |
| 2275 | 'Annotations may\n' |
| 2276 | 'be evaluated in a different order than they appear in the ' |
| 2277 | 'source code.\n' |
| 2278 | 'The presence of annotations does not change the semantics of ' |
| 2279 | 'a\n' |
| 2280 | 'function. The annotation values are available as values of a\n' |
| 2281 | "dictionary keyed by the parameters' names in the " |
| 2282 | '"__annotations__"\n' |
| 2283 | 'attribute of the function object.\n' |
| 2284 | '\n' |
| 2285 | 'It is also possible to create anonymous functions (functions ' |
| 2286 | 'not bound\n' |
| 2287 | 'to a name), for immediate use in expressions. This uses ' |
| 2288 | 'lambda\n' |
| 2289 | 'expressions, described in section *Lambdas*. Note that the ' |
| 2290 | 'lambda\n' |
| 2291 | 'expression is merely a shorthand for a simplified function ' |
| 2292 | 'definition;\n' |
| 2293 | 'a function defined in a ""def"" statement can be passed around ' |
| 2294 | 'or\n' |
| 2295 | 'assigned to another name just like a function defined by a ' |
| 2296 | 'lambda\n' |
| 2297 | 'expression. The ""def"" form is actually more powerful since ' |
| 2298 | 'it\n' |
| 2299 | 'allows the execution of multiple statements and annotations.\n' |
| 2300 | '\n' |
| 2301 | "**Programmer's note:** Functions are first-class objects. A " |
| 2302 | '""def""\n' |
| 2303 | 'statement executed inside a function definition defines a ' |
| 2304 | 'local\n' |
| 2305 | 'function that can be returned or passed around. Free ' |
| 2306 | 'variables used\n' |
| 2307 | 'in the nested function can access the local variables of the ' |
| 2308 | 'function\n' |
| 2309 | 'containing the def. See section *Naming and binding* for ' |
| 2310 | 'details.\n' |
| 2311 | '\n' |
| 2312 | 'See also: **PEP 3107** - Function Annotations\n' |
| 2313 | '\n' |
| 2314 | ' The original specification for function annotations.\n' |
| 2315 | '\n' |
| 2316 | '\n' |
| 2317 | 'Class definitions\n' |
| 2318 | '=================\n' |
| 2319 | '\n' |
| 2320 | 'A class definition defines a class object (see section *The ' |
| 2321 | 'standard\n' |
| 2322 | 'type hierarchy*):\n' |
| 2323 | '\n' |
| 2324 | ' classdef ::= [decorators] "class" classname ' |
| 2325 | '[inheritance] ":" suite\n' |
| 2326 | ' inheritance ::= "(" [parameter_list] ")"\n' |
| 2327 | ' classname ::= identifier\n' |
| 2328 | '\n' |
| 2329 | 'A class definition is an executable statement. The ' |
| 2330 | 'inheritance list\n' |
| 2331 | 'usually gives a list of base classes (see *Customizing class ' |
| 2332 | 'creation*\n' |
| 2333 | 'for more advanced uses), so each item in the list should ' |
| 2334 | 'evaluate to a\n' |
| 2335 | 'class object which allows subclassing. Classes without an ' |
| 2336 | 'inheritance\n' |
| 2337 | 'list inherit, by default, from the base class "object"; ' |
| 2338 | 'hence,\n' |
| 2339 | '\n' |
| 2340 | ' class Foo:\n' |
| 2341 | ' pass\n' |
| 2342 | '\n' |
| 2343 | 'is equivalent to\n' |
| 2344 | '\n' |
| 2345 | ' class Foo(object):\n' |
| 2346 | ' pass\n' |
| 2347 | '\n' |
| 2348 | "The class's suite is then executed in a new execution frame " |
| 2349 | '(see\n' |
| 2350 | '*Naming and binding*), using a newly created local namespace ' |
| 2351 | 'and the\n' |
| 2352 | 'original global namespace. (Usually, the suite contains ' |
| 2353 | 'mostly\n' |
| 2354 | "function definitions.) When the class's suite finishes " |
| 2355 | 'execution, its\n' |
| 2356 | 'execution frame is discarded but its local namespace is saved. ' |
| 2357 | '[4] A\n' |
| 2358 | 'class object is then created using the inheritance list for ' |
| 2359 | 'the base\n' |
| 2360 | 'classes and the saved local namespace for the attribute ' |
| 2361 | 'dictionary.\n' |
| 2362 | 'The class name is bound to this class object in the original ' |
| 2363 | 'local\n' |
| 2364 | 'namespace.\n' |
| 2365 | '\n' |
| 2366 | 'Class creation can be customized heavily using *metaclasses*.\n' |
| 2367 | '\n' |
| 2368 | 'Classes can also be decorated: just like when decorating ' |
| 2369 | 'functions,\n' |
| 2370 | '\n' |
| 2371 | ' @f1(arg)\n' |
| 2372 | ' @f2\n' |
| 2373 | ' class Foo: pass\n' |
| 2374 | '\n' |
| 2375 | 'is equivalent to\n' |
| 2376 | '\n' |
| 2377 | ' class Foo: pass\n' |
| 2378 | ' Foo = f1(arg)(f2(Foo))\n' |
| 2379 | '\n' |
| 2380 | 'The evaluation rules for the decorator expressions are the ' |
| 2381 | 'same as for\n' |
| 2382 | 'function decorators. The result must be a class object, which ' |
| 2383 | 'is then\n' |
| 2384 | 'bound to the class name.\n' |
| 2385 | '\n' |
| 2386 | "**Programmer's note:** Variables defined in the class " |
| 2387 | 'definition are\n' |
| 2388 | 'class attributes; they are shared by instances. Instance ' |
| 2389 | 'attributes\n' |
| 2390 | 'can be set in a method with "self.name = value". Both class ' |
| 2391 | 'and\n' |
| 2392 | 'instance attributes are accessible through the notation ' |
| 2393 | '""self.name"",\n' |
| 2394 | 'and an instance attribute hides a class attribute with the ' |
| 2395 | 'same name\n' |
| 2396 | 'when accessed in this way. Class attributes can be used as ' |
| 2397 | 'defaults\n' |
| 2398 | 'for instance attributes, but using mutable values there can ' |
| 2399 | 'lead to\n' |
| 2400 | 'unexpected results. *Descriptors* can be used to create ' |
| 2401 | 'instance\n' |
| 2402 | 'variables with different implementation details.\n' |
| 2403 | '\n' |
| 2404 | 'See also: **PEP 3115** - Metaclasses in Python 3 **PEP 3129** ' |
| 2405 | '-\n' |
| 2406 | ' Class Decorators\n' |
| 2407 | '\n' |
| 2408 | '\n' |
| 2409 | 'Coroutines\n' |
| 2410 | '==========\n' |
| 2411 | '\n' |
| 2412 | 'New in version 3.5.\n' |
| 2413 | '\n' |
| 2414 | '\n' |
| 2415 | 'Coroutine function definition\n' |
| 2416 | '-----------------------------\n' |
| 2417 | '\n' |
| 2418 | ' async_funcdef ::= [decorators] "async" "def" funcname "(" ' |
| 2419 | '[parameter_list] ")" ["->" expression] ":" suite\n' |
| 2420 | '\n' |
| 2421 | 'Execution of Python coroutines can be suspended and resumed at ' |
| 2422 | 'many\n' |
| 2423 | 'points (see *coroutine*). In the body of a coroutine, any ' |
| 2424 | '"await" and\n' |
| 2425 | '"async" identifiers become reserved keywords; "await" ' |
| 2426 | 'expressions,\n' |
| 2427 | '"async for" and "async with" can only be used in coroutine ' |
| 2428 | 'bodies.\n' |
| 2429 | '\n' |
| 2430 | 'Functions defined with "async def" syntax are always ' |
| 2431 | 'coroutine\n' |
| 2432 | 'functions, even if they do not contain "await" or "async" ' |
| 2433 | 'keywords.\n' |
| 2434 | '\n' |
| 2435 | 'It is a "SyntaxError" to use "yield" expressions in "async ' |
| 2436 | 'def"\n' |
| 2437 | 'coroutines.\n' |
| 2438 | '\n' |
| 2439 | 'An example of a coroutine function:\n' |
| 2440 | '\n' |
| 2441 | ' async def func(param1, param2):\n' |
| 2442 | ' do_stuff()\n' |
| 2443 | ' await some_coroutine()\n' |
| 2444 | '\n' |
| 2445 | '\n' |
| 2446 | 'The "async for" statement\n' |
| 2447 | '-------------------------\n' |
| 2448 | '\n' |
| 2449 | ' async_for_stmt ::= "async" for_stmt\n' |
| 2450 | '\n' |
| 2451 | 'An *asynchronous iterable* is able to call asynchronous code ' |
| 2452 | 'in its\n' |
| 2453 | '*iter* implementation, and *asynchronous iterator* can call\n' |
| 2454 | 'asynchronous code in its *next* method.\n' |
| 2455 | '\n' |
| 2456 | 'The "async for" statement allows convenient iteration over\n' |
| 2457 | 'asynchronous iterators.\n' |
| 2458 | '\n' |
| 2459 | 'The following code:\n' |
| 2460 | '\n' |
| 2461 | ' async for TARGET in ITER:\n' |
| 2462 | ' BLOCK\n' |
| 2463 | ' else:\n' |
| 2464 | ' BLOCK2\n' |
| 2465 | '\n' |
| 2466 | 'Is semantically equivalent to:\n' |
| 2467 | '\n' |
| 2468 | ' iter = (ITER)\n' |
| 2469 | ' iter = await type(iter).__aiter__(iter)\n' |
| 2470 | ' running = True\n' |
| 2471 | ' while running:\n' |
| 2472 | ' try:\n' |
| 2473 | ' TARGET = await type(iter).__anext__(iter)\n' |
| 2474 | ' except StopAsyncIteration:\n' |
| 2475 | ' running = False\n' |
| 2476 | ' else:\n' |
| 2477 | ' BLOCK\n' |
| 2478 | ' else:\n' |
| 2479 | ' BLOCK2\n' |
| 2480 | '\n' |
| 2481 | 'See also "__aiter__()" and "__anext__()" for details.\n' |
| 2482 | '\n' |
| 2483 | 'It is a "SyntaxError" to use "async for" statement outside of ' |
| 2484 | 'an\n' |
| 2485 | '"async def" function.\n' |
| 2486 | '\n' |
| 2487 | '\n' |
| 2488 | 'The "async with" statement\n' |
| 2489 | '--------------------------\n' |
| 2490 | '\n' |
| 2491 | ' async_with_stmt ::= "async" with_stmt\n' |
| 2492 | '\n' |
| 2493 | 'An *asynchronous context manager* is a *context manager* that ' |
| 2494 | 'is able\n' |
| 2495 | 'to suspend execution in its *enter* and *exit* methods.\n' |
| 2496 | '\n' |
| 2497 | 'The following code:\n' |
| 2498 | '\n' |
| 2499 | ' async with EXPR as VAR:\n' |
| 2500 | ' BLOCK\n' |
| 2501 | '\n' |
| 2502 | 'Is semantically equivalent to:\n' |
| 2503 | '\n' |
| 2504 | ' mgr = (EXPR)\n' |
| 2505 | ' aexit = type(mgr).__aexit__\n' |
| 2506 | ' aenter = type(mgr).__aenter__(mgr)\n' |
| 2507 | ' exc = True\n' |
| 2508 | '\n' |
| 2509 | ' VAR = await aenter\n' |
| 2510 | ' try:\n' |
| 2511 | ' BLOCK\n' |
| 2512 | ' except:\n' |
| 2513 | ' if not await aexit(mgr, *sys.exc_info()):\n' |
| 2514 | ' raise\n' |
| 2515 | ' else:\n' |
| 2516 | ' await aexit(mgr, None, None, None)\n' |
| 2517 | '\n' |
| 2518 | 'See also "__aenter__()" and "__aexit__()" for details.\n' |
| 2519 | '\n' |
| 2520 | 'It is a "SyntaxError" to use "async with" statement outside of ' |
| 2521 | 'an\n' |
| 2522 | '"async def" function.\n' |
| 2523 | '\n' |
| 2524 | 'See also: **PEP 492** - Coroutines with async and await ' |
| 2525 | 'syntax\n' |
| 2526 | '\n' |
| 2527 | '-[ Footnotes ]-\n' |
| 2528 | '\n' |
| 2529 | '[1] The exception is propagated to the invocation stack ' |
| 2530 | 'unless\n' |
| 2531 | ' there is a "finally" clause which happens to raise ' |
| 2532 | 'another\n' |
| 2533 | ' exception. That new exception causes the old one to be ' |
| 2534 | 'lost.\n' |
| 2535 | '\n' |
| 2536 | '[2] Currently, control "flows off the end" except in the case ' |
| 2537 | 'of\n' |
| 2538 | ' an exception or the execution of a "return", "continue", ' |
| 2539 | 'or\n' |
| 2540 | ' "break" statement.\n' |
| 2541 | '\n' |
| 2542 | '[3] A string literal appearing as the first statement in the\n' |
| 2543 | " function body is transformed into the function's " |
| 2544 | '"__doc__"\n' |
| 2545 | " attribute and therefore the function's *docstring*.\n" |
| 2546 | '\n' |
| 2547 | '[4] A string literal appearing as the first statement in the ' |
| 2548 | 'class\n' |
| 2549 | ' body is transformed into the namespace\'s "__doc__" item ' |
| 2550 | 'and\n' |
| 2551 | " therefore the class's *docstring*.\n", |
| 2552 | 'context-managers': '\n' |
| 2553 | 'With Statement Context Managers\n' |
| 2554 | '*******************************\n' |
| 2555 | '\n' |
| 2556 | 'A *context manager* is an object that defines the ' |
| 2557 | 'runtime context to\n' |
| 2558 | 'be established when executing a "with" statement. The ' |
| 2559 | 'context manager\n' |
| 2560 | 'handles the entry into, and the exit from, the desired ' |
| 2561 | 'runtime context\n' |
| 2562 | 'for the execution of the block of code. Context ' |
| 2563 | 'managers are normally\n' |
| 2564 | 'invoked using the "with" statement (described in ' |
| 2565 | 'section *The with\n' |
| 2566 | 'statement*), but can also be used by directly invoking ' |
| 2567 | 'their methods.\n' |
| 2568 | '\n' |
| 2569 | 'Typical uses of context managers include saving and ' |
| 2570 | 'restoring various\n' |
| 2571 | 'kinds of global state, locking and unlocking ' |
| 2572 | 'resources, closing opened\n' |
| 2573 | 'files, etc.\n' |
| 2574 | '\n' |
| 2575 | 'For more information on context managers, see *Context ' |
| 2576 | 'Manager Types*.\n' |
| 2577 | '\n' |
| 2578 | 'object.__enter__(self)\n' |
| 2579 | '\n' |
| 2580 | ' Enter the runtime context related to this object. ' |
| 2581 | 'The "with"\n' |
| 2582 | " statement will bind this method's return value to " |
| 2583 | 'the target(s)\n' |
| 2584 | ' specified in the "as" clause of the statement, if ' |
| 2585 | 'any.\n' |
| 2586 | '\n' |
| 2587 | 'object.__exit__(self, exc_type, exc_value, traceback)\n' |
| 2588 | '\n' |
| 2589 | ' Exit the runtime context related to this object. ' |
| 2590 | 'The parameters\n' |
| 2591 | ' describe the exception that caused the context to ' |
| 2592 | 'be exited. If the\n' |
| 2593 | ' context was exited without an exception, all three ' |
| 2594 | 'arguments will\n' |
| 2595 | ' be "None".\n' |
| 2596 | '\n' |
| 2597 | ' If an exception is supplied, and the method wishes ' |
| 2598 | 'to suppress the\n' |
| 2599 | ' exception (i.e., prevent it from being propagated), ' |
| 2600 | 'it should\n' |
| 2601 | ' return a true value. Otherwise, the exception will ' |
| 2602 | 'be processed\n' |
| 2603 | ' normally upon exit from this method.\n' |
| 2604 | '\n' |
| 2605 | ' Note that "__exit__()" methods should not reraise ' |
| 2606 | 'the passed-in\n' |
| 2607 | " exception; this is the caller's responsibility.\n" |
| 2608 | '\n' |
| 2609 | 'See also: **PEP 0343** - The "with" statement\n' |
| 2610 | '\n' |
| 2611 | ' The specification, background, and examples for ' |
| 2612 | 'the Python "with"\n' |
| 2613 | ' statement.\n', |
| 2614 | 'continue': '\n' |
| 2615 | 'The "continue" statement\n' |
| 2616 | '************************\n' |
| 2617 | '\n' |
| 2618 | ' continue_stmt ::= "continue"\n' |
| 2619 | '\n' |
| 2620 | '"continue" may only occur syntactically nested in a "for" or ' |
| 2621 | '"while"\n' |
| 2622 | 'loop, but not nested in a function or class definition or ' |
| 2623 | '"finally"\n' |
| 2624 | 'clause within that loop. It continues with the next cycle of ' |
| 2625 | 'the\n' |
| 2626 | 'nearest enclosing loop.\n' |
| 2627 | '\n' |
| 2628 | 'When "continue" passes control out of a "try" statement with ' |
| 2629 | 'a\n' |
| 2630 | '"finally" clause, that "finally" clause is executed before ' |
| 2631 | 'really\n' |
| 2632 | 'starting the next loop cycle.\n', |
| 2633 | 'conversions': '\n' |
| 2634 | 'Arithmetic conversions\n' |
| 2635 | '**********************\n' |
| 2636 | '\n' |
| 2637 | 'When a description of an arithmetic operator below uses the ' |
| 2638 | 'phrase\n' |
| 2639 | '"the numeric arguments are converted to a common type," ' |
| 2640 | 'this means\n' |
| 2641 | 'that the operator implementation for built-in types works ' |
| 2642 | 'as follows:\n' |
| 2643 | '\n' |
| 2644 | '* If either argument is a complex number, the other is ' |
| 2645 | 'converted to\n' |
| 2646 | ' complex;\n' |
| 2647 | '\n' |
| 2648 | '* otherwise, if either argument is a floating point number, ' |
| 2649 | 'the\n' |
| 2650 | ' other is converted to floating point;\n' |
| 2651 | '\n' |
| 2652 | '* otherwise, both must be integers and no conversion is ' |
| 2653 | 'necessary.\n' |
| 2654 | '\n' |
| 2655 | 'Some additional rules apply for certain operators (e.g., a ' |
| 2656 | 'string as a\n' |
| 2657 | "left argument to the '%' operator). Extensions must define " |
| 2658 | 'their own\n' |
| 2659 | 'conversion behavior.\n', |
| 2660 | 'customization': '\n' |
| 2661 | 'Basic customization\n' |
| 2662 | '*******************\n' |
| 2663 | '\n' |
| 2664 | 'object.__new__(cls[, ...])\n' |
| 2665 | '\n' |
| 2666 | ' Called to create a new instance of class *cls*. ' |
| 2667 | '"__new__()" is a\n' |
| 2668 | ' static method (special-cased so you need not declare ' |
| 2669 | 'it as such)\n' |
| 2670 | ' that takes the class of which an instance was ' |
| 2671 | 'requested as its\n' |
| 2672 | ' first argument. The remaining arguments are those ' |
| 2673 | 'passed to the\n' |
| 2674 | ' object constructor expression (the call to the ' |
| 2675 | 'class). The return\n' |
| 2676 | ' value of "__new__()" should be the new object instance ' |
| 2677 | '(usually an\n' |
| 2678 | ' instance of *cls*).\n' |
| 2679 | '\n' |
| 2680 | ' Typical implementations create a new instance of the ' |
| 2681 | 'class by\n' |
| 2682 | ' invoking the superclass\'s "__new__()" method using\n' |
| 2683 | ' "super(currentclass, cls).__new__(cls[, ...])" with ' |
| 2684 | 'appropriate\n' |
| 2685 | ' arguments and then modifying the newly-created ' |
| 2686 | 'instance as\n' |
| 2687 | ' necessary before returning it.\n' |
| 2688 | '\n' |
| 2689 | ' If "__new__()" returns an instance of *cls*, then the ' |
| 2690 | 'new\n' |
| 2691 | ' instance\'s "__init__()" method will be invoked like\n' |
| 2692 | ' "__init__(self[, ...])", where *self* is the new ' |
| 2693 | 'instance and the\n' |
| 2694 | ' remaining arguments are the same as were passed to ' |
| 2695 | '"__new__()".\n' |
| 2696 | '\n' |
| 2697 | ' If "__new__()" does not return an instance of *cls*, ' |
| 2698 | 'then the new\n' |
| 2699 | ' instance\'s "__init__()" method will not be invoked.\n' |
| 2700 | '\n' |
| 2701 | ' "__new__()" is intended mainly to allow subclasses of ' |
| 2702 | 'immutable\n' |
| 2703 | ' types (like int, str, or tuple) to customize instance ' |
| 2704 | 'creation. It\n' |
| 2705 | ' is also commonly overridden in custom metaclasses in ' |
| 2706 | 'order to\n' |
| 2707 | ' customize class creation.\n' |
| 2708 | '\n' |
| 2709 | 'object.__init__(self[, ...])\n' |
| 2710 | '\n' |
| 2711 | ' Called after the instance has been created (by ' |
| 2712 | '"__new__()"), but\n' |
| 2713 | ' before it is returned to the caller. The arguments ' |
| 2714 | 'are those\n' |
| 2715 | ' passed to the class constructor expression. If a base ' |
| 2716 | 'class has an\n' |
| 2717 | ' "__init__()" method, the derived class\'s "__init__()" ' |
| 2718 | 'method, if\n' |
| 2719 | ' any, must explicitly call it to ensure proper ' |
| 2720 | 'initialization of the\n' |
| 2721 | ' base class part of the instance; for example:\n' |
| 2722 | ' "BaseClass.__init__(self, [args...])".\n' |
| 2723 | '\n' |
| 2724 | ' Because "__new__()" and "__init__()" work together in ' |
| 2725 | 'constructing\n' |
| 2726 | ' objects ("__new__()" to create it, and "__init__()" to ' |
| 2727 | 'customise\n' |
| 2728 | ' it), no non-"None" value may be returned by ' |
| 2729 | '"__init__()"; doing so\n' |
| 2730 | ' will cause a "TypeError" to be raised at runtime.\n' |
| 2731 | '\n' |
| 2732 | 'object.__del__(self)\n' |
| 2733 | '\n' |
| 2734 | ' Called when the instance is about to be destroyed. ' |
| 2735 | 'This is also\n' |
| 2736 | ' called a destructor. If a base class has a ' |
| 2737 | '"__del__()" method, the\n' |
| 2738 | ' derived class\'s "__del__()" method, if any, must ' |
| 2739 | 'explicitly call it\n' |
| 2740 | ' to ensure proper deletion of the base class part of ' |
| 2741 | 'the instance.\n' |
| 2742 | ' Note that it is possible (though not recommended!) for ' |
| 2743 | 'the\n' |
| 2744 | ' "__del__()" method to postpone destruction of the ' |
| 2745 | 'instance by\n' |
| 2746 | ' creating a new reference to it. It may then be called ' |
| 2747 | 'at a later\n' |
| 2748 | ' time when this new reference is deleted. It is not ' |
| 2749 | 'guaranteed that\n' |
| 2750 | ' "__del__()" methods are called for objects that still ' |
| 2751 | 'exist when\n' |
| 2752 | ' the interpreter exits.\n' |
| 2753 | '\n' |
| 2754 | ' Note: "del x" doesn\'t directly call "x.__del__()" --- ' |
| 2755 | 'the former\n' |
| 2756 | ' decrements the reference count for "x" by one, and ' |
| 2757 | 'the latter is\n' |
| 2758 | ' only called when "x"\'s reference count reaches ' |
| 2759 | 'zero. Some common\n' |
| 2760 | ' situations that may prevent the reference count of ' |
| 2761 | 'an object from\n' |
| 2762 | ' going to zero include: circular references between ' |
| 2763 | 'objects (e.g.,\n' |
| 2764 | ' a doubly-linked list or a tree data structure with ' |
| 2765 | 'parent and\n' |
| 2766 | ' child pointers); a reference to the object on the ' |
| 2767 | 'stack frame of\n' |
| 2768 | ' a function that caught an exception (the traceback ' |
| 2769 | 'stored in\n' |
| 2770 | ' "sys.exc_info()[2]" keeps the stack frame alive); or ' |
| 2771 | 'a reference\n' |
| 2772 | ' to the object on the stack frame that raised an ' |
| 2773 | 'unhandled\n' |
| 2774 | ' exception in interactive mode (the traceback stored ' |
| 2775 | 'in\n' |
| 2776 | ' "sys.last_traceback" keeps the stack frame alive). ' |
| 2777 | 'The first\n' |
| 2778 | ' situation can only be remedied by explicitly ' |
| 2779 | 'breaking the cycles;\n' |
| 2780 | ' the second can be resolved by freeing the reference ' |
| 2781 | 'to the\n' |
| 2782 | ' traceback object when it is no longer useful, and ' |
| 2783 | 'the third can\n' |
| 2784 | ' be resolved by storing "None" in ' |
| 2785 | '"sys.last_traceback". Circular\n' |
| 2786 | ' references which are garbage are detected and ' |
| 2787 | 'cleaned up when the\n' |
| 2788 | " cyclic garbage collector is enabled (it's on by " |
| 2789 | 'default). Refer\n' |
| 2790 | ' to the documentation for the "gc" module for more ' |
| 2791 | 'information\n' |
| 2792 | ' about this topic.\n' |
| 2793 | '\n' |
| 2794 | ' Warning: Due to the precarious circumstances under ' |
| 2795 | 'which\n' |
| 2796 | ' "__del__()" methods are invoked, exceptions that ' |
| 2797 | 'occur during\n' |
| 2798 | ' their execution are ignored, and a warning is ' |
| 2799 | 'printed to\n' |
| 2800 | ' "sys.stderr" instead. Also, when "__del__()" is ' |
| 2801 | 'invoked in\n' |
| 2802 | ' response to a module being deleted (e.g., when ' |
| 2803 | 'execution of the\n' |
| 2804 | ' program is done), other globals referenced by the ' |
| 2805 | '"__del__()"\n' |
| 2806 | ' method may already have been deleted or in the ' |
| 2807 | 'process of being\n' |
| 2808 | ' torn down (e.g. the import machinery shutting ' |
| 2809 | 'down). For this\n' |
| 2810 | ' reason, "__del__()" methods should do the absolute ' |
| 2811 | 'minimum needed\n' |
| 2812 | ' to maintain external invariants. Starting with ' |
| 2813 | 'version 1.5,\n' |
| 2814 | ' Python guarantees that globals whose name begins ' |
| 2815 | 'with a single\n' |
| 2816 | ' underscore are deleted from their module before ' |
| 2817 | 'other globals are\n' |
| 2818 | ' deleted; if no other references to such globals ' |
| 2819 | 'exist, this may\n' |
| 2820 | ' help in assuring that imported modules are still ' |
| 2821 | 'available at the\n' |
| 2822 | ' time when the "__del__()" method is called.\n' |
| 2823 | '\n' |
| 2824 | 'object.__repr__(self)\n' |
| 2825 | '\n' |
| 2826 | ' Called by the "repr()" built-in function to compute ' |
| 2827 | 'the "official"\n' |
| 2828 | ' string representation of an object. If at all ' |
| 2829 | 'possible, this\n' |
| 2830 | ' should look like a valid Python expression that could ' |
| 2831 | 'be used to\n' |
| 2832 | ' recreate an object with the same value (given an ' |
| 2833 | 'appropriate\n' |
| 2834 | ' environment). If this is not possible, a string of ' |
| 2835 | 'the form\n' |
| 2836 | ' "<...some useful description...>" should be returned. ' |
| 2837 | 'The return\n' |
| 2838 | ' value must be a string object. If a class defines ' |
| 2839 | '"__repr__()" but\n' |
| 2840 | ' not "__str__()", then "__repr__()" is also used when ' |
| 2841 | 'an "informal"\n' |
| 2842 | ' string representation of instances of that class is ' |
| 2843 | 'required.\n' |
| 2844 | '\n' |
| 2845 | ' This is typically used for debugging, so it is ' |
| 2846 | 'important that the\n' |
| 2847 | ' representation is information-rich and unambiguous.\n' |
| 2848 | '\n' |
| 2849 | 'object.__str__(self)\n' |
| 2850 | '\n' |
| 2851 | ' Called by "str(object)" and the built-in functions ' |
| 2852 | '"format()" and\n' |
| 2853 | ' "print()" to compute the "informal" or nicely ' |
| 2854 | 'printable string\n' |
| 2855 | ' representation of an object. The return value must be ' |
| 2856 | 'a *string*\n' |
| 2857 | ' object.\n' |
| 2858 | '\n' |
| 2859 | ' This method differs from "object.__repr__()" in that ' |
| 2860 | 'there is no\n' |
| 2861 | ' expectation that "__str__()" return a valid Python ' |
| 2862 | 'expression: a\n' |
| 2863 | ' more convenient or concise representation can be ' |
| 2864 | 'used.\n' |
| 2865 | '\n' |
| 2866 | ' The default implementation defined by the built-in ' |
| 2867 | 'type "object"\n' |
| 2868 | ' calls "object.__repr__()".\n' |
| 2869 | '\n' |
| 2870 | 'object.__bytes__(self)\n' |
| 2871 | '\n' |
| 2872 | ' Called by "bytes()" to compute a byte-string ' |
| 2873 | 'representation of an\n' |
| 2874 | ' object. This should return a "bytes" object.\n' |
| 2875 | '\n' |
| 2876 | 'object.__format__(self, format_spec)\n' |
| 2877 | '\n' |
| 2878 | ' Called by the "format()" built-in function (and by ' |
| 2879 | 'extension, the\n' |
| 2880 | ' "str.format()" method of class "str") to produce a ' |
| 2881 | '"formatted"\n' |
| 2882 | ' string representation of an object. The "format_spec" ' |
| 2883 | 'argument is a\n' |
| 2884 | ' string that contains a description of the formatting ' |
| 2885 | 'options\n' |
| 2886 | ' desired. The interpretation of the "format_spec" ' |
| 2887 | 'argument is up to\n' |
| 2888 | ' the type implementing "__format__()", however most ' |
| 2889 | 'classes will\n' |
| 2890 | ' either delegate formatting to one of the built-in ' |
| 2891 | 'types, or use a\n' |
| 2892 | ' similar formatting option syntax.\n' |
| 2893 | '\n' |
| 2894 | ' See *Format Specification Mini-Language* for a ' |
| 2895 | 'description of the\n' |
| 2896 | ' standard formatting syntax.\n' |
| 2897 | '\n' |
| 2898 | ' The return value must be a string object.\n' |
| 2899 | '\n' |
| 2900 | ' Changed in version 3.4: The __format__ method of ' |
| 2901 | '"object" itself\n' |
| 2902 | ' raises a "TypeError" if passed any non-empty string.\n' |
| 2903 | '\n' |
| 2904 | 'object.__lt__(self, other)\n' |
| 2905 | 'object.__le__(self, other)\n' |
| 2906 | 'object.__eq__(self, other)\n' |
| 2907 | 'object.__ne__(self, other)\n' |
| 2908 | 'object.__gt__(self, other)\n' |
| 2909 | 'object.__ge__(self, other)\n' |
| 2910 | '\n' |
| 2911 | ' These are the so-called "rich comparison" methods. ' |
| 2912 | 'The\n' |
| 2913 | ' correspondence between operator symbols and method ' |
| 2914 | 'names is as\n' |
| 2915 | ' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls ' |
| 2916 | '"x.__le__(y)",\n' |
| 2917 | ' "x==y" calls "x.__eq__(y)", "x!=y" calls ' |
| 2918 | '"x.__ne__(y)", "x>y" calls\n' |
| 2919 | ' "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".\n' |
| 2920 | '\n' |
| 2921 | ' A rich comparison method may return the singleton ' |
| 2922 | '"NotImplemented"\n' |
| 2923 | ' if it does not implement the operation for a given ' |
| 2924 | 'pair of\n' |
| 2925 | ' arguments. By convention, "False" and "True" are ' |
| 2926 | 'returned for a\n' |
| 2927 | ' successful comparison. However, these methods can ' |
| 2928 | 'return any value,\n' |
| 2929 | ' so if the comparison operator is used in a Boolean ' |
| 2930 | 'context (e.g.,\n' |
| 2931 | ' in the condition of an "if" statement), Python will ' |
| 2932 | 'call "bool()"\n' |
| 2933 | ' on the value to determine if the result is true or ' |
| 2934 | 'false.\n' |
| 2935 | '\n' |
| 2936 | ' By default, "__ne__()" delegates to "__eq__()" and ' |
| 2937 | 'inverts the\n' |
| 2938 | ' result unless it is "NotImplemented". There are no ' |
| 2939 | 'other implied\n' |
| 2940 | ' relationships among the comparison operators, for ' |
| 2941 | 'example, the\n' |
| 2942 | ' truth of "(x<y or x==y)" does not imply "x<=y". To ' |
| 2943 | 'automatically\n' |
| 2944 | ' generate ordering operations from a single root ' |
| 2945 | 'operation, see\n' |
| 2946 | ' "functools.total_ordering()".\n' |
| 2947 | '\n' |
| 2948 | ' See the paragraph on "__hash__()" for some important ' |
| 2949 | 'notes on\n' |
| 2950 | ' creating *hashable* objects which support custom ' |
| 2951 | 'comparison\n' |
| 2952 | ' operations and are usable as dictionary keys.\n' |
| 2953 | '\n' |
| 2954 | ' There are no swapped-argument versions of these ' |
| 2955 | 'methods (to be used\n' |
| 2956 | ' when the left argument does not support the operation ' |
| 2957 | 'but the right\n' |
| 2958 | ' argument does); rather, "__lt__()" and "__gt__()" are ' |
| 2959 | "each other's\n" |
| 2960 | ' reflection, "__le__()" and "__ge__()" are each ' |
| 2961 | "other's reflection,\n" |
| 2962 | ' and "__eq__()" and "__ne__()" are their own ' |
| 2963 | 'reflection. If the\n' |
| 2964 | " operands are of different types, and right operand's " |
| 2965 | 'type is a\n' |
| 2966 | " direct or indirect subclass of the left operand's " |
| 2967 | 'type, the\n' |
| 2968 | ' reflected method of the right operand has priority, ' |
| 2969 | 'otherwise the\n' |
| 2970 | " left operand's method has priority. Virtual " |
| 2971 | 'subclassing is not\n' |
| 2972 | ' considered.\n' |
| 2973 | '\n' |
| 2974 | 'object.__hash__(self)\n' |
| 2975 | '\n' |
| 2976 | ' Called by built-in function "hash()" and for ' |
| 2977 | 'operations on members\n' |
| 2978 | ' of hashed collections including "set", "frozenset", ' |
| 2979 | 'and "dict".\n' |
| 2980 | ' "__hash__()" should return an integer. The only ' |
| 2981 | 'required property\n' |
| 2982 | ' is that objects which compare equal have the same hash ' |
| 2983 | 'value; it is\n' |
| 2984 | ' advised to somehow mix together (e.g. using exclusive ' |
| 2985 | 'or) the hash\n' |
| 2986 | ' values for the components of the object that also play ' |
| 2987 | 'a part in\n' |
| 2988 | ' comparison of objects.\n' |
| 2989 | '\n' |
| 2990 | ' Note: "hash()" truncates the value returned from an ' |
| 2991 | "object's\n" |
| 2992 | ' custom "__hash__()" method to the size of a ' |
| 2993 | '"Py_ssize_t". This\n' |
| 2994 | ' is typically 8 bytes on 64-bit builds and 4 bytes on ' |
| 2995 | '32-bit\n' |
| 2996 | ' builds. If an object\'s "__hash__()" must ' |
| 2997 | 'interoperate on builds\n' |
| 2998 | ' of different bit sizes, be sure to check the width ' |
| 2999 | 'on all\n' |
| 3000 | ' supported builds. An easy way to do this is with ' |
| 3001 | '"python -c\n' |
| 3002 | ' "import sys; print(sys.hash_info.width)""\n' |
| 3003 | '\n' |
| 3004 | ' If a class does not define an "__eq__()" method it ' |
| 3005 | 'should not\n' |
| 3006 | ' define a "__hash__()" operation either; if it defines ' |
| 3007 | '"__eq__()"\n' |
| 3008 | ' but not "__hash__()", its instances will not be usable ' |
| 3009 | 'as items in\n' |
| 3010 | ' hashable collections. If a class defines mutable ' |
| 3011 | 'objects and\n' |
| 3012 | ' implements an "__eq__()" method, it should not ' |
| 3013 | 'implement\n' |
| 3014 | ' "__hash__()", since the implementation of hashable ' |
| 3015 | 'collections\n' |
| 3016 | " requires that a key's hash value is immutable (if the " |
| 3017 | "object's hash\n" |
| 3018 | ' value changes, it will be in the wrong hash bucket).\n' |
| 3019 | '\n' |
| 3020 | ' User-defined classes have "__eq__()" and "__hash__()" ' |
| 3021 | 'methods by\n' |
| 3022 | ' default; with them, all objects compare unequal ' |
| 3023 | '(except with\n' |
| 3024 | ' themselves) and "x.__hash__()" returns an appropriate ' |
| 3025 | 'value such\n' |
| 3026 | ' that "x == y" implies both that "x is y" and "hash(x) ' |
| 3027 | '== hash(y)".\n' |
| 3028 | '\n' |
| 3029 | ' A class that overrides "__eq__()" and does not define ' |
| 3030 | '"__hash__()"\n' |
| 3031 | ' will have its "__hash__()" implicitly set to "None". ' |
| 3032 | 'When the\n' |
| 3033 | ' "__hash__()" method of a class is "None", instances of ' |
| 3034 | 'the class\n' |
| 3035 | ' will raise an appropriate "TypeError" when a program ' |
| 3036 | 'attempts to\n' |
| 3037 | ' retrieve their hash value, and will also be correctly ' |
| 3038 | 'identified as\n' |
| 3039 | ' unhashable when checking "isinstance(obj, ' |
| 3040 | 'collections.Hashable").\n' |
| 3041 | '\n' |
| 3042 | ' If a class that overrides "__eq__()" needs to retain ' |
| 3043 | 'the\n' |
| 3044 | ' implementation of "__hash__()" from a parent class, ' |
| 3045 | 'the interpreter\n' |
| 3046 | ' must be told this explicitly by setting "__hash__ =\n' |
| 3047 | ' <ParentClass>.__hash__".\n' |
| 3048 | '\n' |
| 3049 | ' If a class that does not override "__eq__()" wishes to ' |
| 3050 | 'suppress\n' |
| 3051 | ' hash support, it should include "__hash__ = None" in ' |
| 3052 | 'the class\n' |
| 3053 | ' definition. A class which defines its own "__hash__()" ' |
| 3054 | 'that\n' |
| 3055 | ' explicitly raises a "TypeError" would be incorrectly ' |
| 3056 | 'identified as\n' |
| 3057 | ' hashable by an "isinstance(obj, collections.Hashable)" ' |
| 3058 | 'call.\n' |
| 3059 | '\n' |
| 3060 | ' Note: By default, the "__hash__()" values of str, ' |
| 3061 | 'bytes and\n' |
| 3062 | ' datetime objects are "salted" with an unpredictable ' |
| 3063 | 'random value.\n' |
| 3064 | ' Although they remain constant within an individual ' |
| 3065 | 'Python\n' |
| 3066 | ' process, they are not predictable between repeated ' |
| 3067 | 'invocations of\n' |
| 3068 | ' Python.This is intended to provide protection ' |
| 3069 | 'against a denial-\n' |
| 3070 | ' of-service caused by carefully-chosen inputs that ' |
| 3071 | 'exploit the\n' |
| 3072 | ' worst case performance of a dict insertion, O(n^2) ' |
| 3073 | 'complexity.\n' |
| 3074 | ' See ' |
| 3075 | 'http://www.ocert.org/advisories/ocert-2011-003.html for\n' |
| 3076 | ' details.Changing hash values affects the iteration ' |
| 3077 | 'order of\n' |
| 3078 | ' dicts, sets and other mappings. Python has never ' |
| 3079 | 'made guarantees\n' |
| 3080 | ' about this ordering (and it typically varies between ' |
| 3081 | '32-bit and\n' |
| 3082 | ' 64-bit builds).See also "PYTHONHASHSEED".\n' |
| 3083 | '\n' |
| 3084 | ' Changed in version 3.3: Hash randomization is enabled ' |
| 3085 | 'by default.\n' |
| 3086 | '\n' |
| 3087 | 'object.__bool__(self)\n' |
| 3088 | '\n' |
| 3089 | ' Called to implement truth value testing and the ' |
| 3090 | 'built-in operation\n' |
| 3091 | ' "bool()"; should return "False" or "True". When this ' |
| 3092 | 'method is not\n' |
| 3093 | ' defined, "__len__()" is called, if it is defined, and ' |
| 3094 | 'the object is\n' |
| 3095 | ' considered true if its result is nonzero. If a class ' |
| 3096 | 'defines\n' |
| 3097 | ' neither "__len__()" nor "__bool__()", all its ' |
| 3098 | 'instances are\n' |
| 3099 | ' considered true.\n', |
| 3100 | 'debugger': '\n' |
| 3101 | '"pdb" --- The Python Debugger\n' |
| 3102 | '*****************************\n' |
| 3103 | '\n' |
| 3104 | '**Source code:** Lib/pdb.py\n' |
| 3105 | '\n' |
| 3106 | '======================================================================\n' |
| 3107 | '\n' |
| 3108 | 'The module "pdb" defines an interactive source code debugger ' |
| 3109 | 'for\n' |
| 3110 | 'Python programs. It supports setting (conditional) ' |
| 3111 | 'breakpoints and\n' |
| 3112 | 'single stepping at the source line level, inspection of stack ' |
| 3113 | 'frames,\n' |
| 3114 | 'source code listing, and evaluation of arbitrary Python code ' |
| 3115 | 'in the\n' |
| 3116 | 'context of any stack frame. It also supports post-mortem ' |
| 3117 | 'debugging\n' |
| 3118 | 'and can be called under program control.\n' |
| 3119 | '\n' |
| 3120 | 'The debugger is extensible -- it is actually defined as the ' |
| 3121 | 'class\n' |
| 3122 | '"Pdb". This is currently undocumented but easily understood by ' |
| 3123 | 'reading\n' |
| 3124 | 'the source. The extension interface uses the modules "bdb" ' |
| 3125 | 'and "cmd".\n' |
| 3126 | '\n' |
| 3127 | 'The debugger\'s prompt is "(Pdb)". Typical usage to run a ' |
| 3128 | 'program under\n' |
| 3129 | 'control of the debugger is:\n' |
| 3130 | '\n' |
| 3131 | ' >>> import pdb\n' |
| 3132 | ' >>> import mymodule\n' |
| 3133 | " >>> pdb.run('mymodule.test()')\n" |
| 3134 | ' > <string>(0)?()\n' |
| 3135 | ' (Pdb) continue\n' |
| 3136 | ' > <string>(1)?()\n' |
| 3137 | ' (Pdb) continue\n' |
| 3138 | " NameError: 'spam'\n" |
| 3139 | ' > <string>(1)?()\n' |
| 3140 | ' (Pdb)\n' |
| 3141 | '\n' |
| 3142 | 'Changed in version 3.3: Tab-completion via the "readline" ' |
| 3143 | 'module is\n' |
| 3144 | 'available for commands and command arguments, e.g. the current ' |
| 3145 | 'global\n' |
| 3146 | 'and local names are offered as arguments of the "p" command.\n' |
| 3147 | '\n' |
| 3148 | '"pdb.py" can also be invoked as a script to debug other ' |
| 3149 | 'scripts. For\n' |
| 3150 | 'example:\n' |
| 3151 | '\n' |
| 3152 | ' python3 -m pdb myscript.py\n' |
| 3153 | '\n' |
| 3154 | 'When invoked as a script, pdb will automatically enter ' |
| 3155 | 'post-mortem\n' |
| 3156 | 'debugging if the program being debugged exits abnormally. ' |
| 3157 | 'After post-\n' |
| 3158 | 'mortem debugging (or after normal exit of the program), pdb ' |
| 3159 | 'will\n' |
| 3160 | "restart the program. Automatic restarting preserves pdb's " |
| 3161 | 'state (such\n' |
| 3162 | 'as breakpoints) and in most cases is more useful than quitting ' |
| 3163 | 'the\n' |
| 3164 | "debugger upon program's exit.\n" |
| 3165 | '\n' |
| 3166 | 'New in version 3.2: "pdb.py" now accepts a "-c" option that ' |
| 3167 | 'executes\n' |
| 3168 | 'commands as if given in a ".pdbrc" file, see *Debugger ' |
| 3169 | 'Commands*.\n' |
| 3170 | '\n' |
| 3171 | 'The typical usage to break into the debugger from a running ' |
| 3172 | 'program is\n' |
| 3173 | 'to insert\n' |
| 3174 | '\n' |
| 3175 | ' import pdb; pdb.set_trace()\n' |
| 3176 | '\n' |
| 3177 | 'at the location you want to break into the debugger. You can ' |
| 3178 | 'then\n' |
| 3179 | 'step through the code following this statement, and continue ' |
| 3180 | 'running\n' |
| 3181 | 'without the debugger using the "continue" command.\n' |
| 3182 | '\n' |
| 3183 | 'The typical usage to inspect a crashed program is:\n' |
| 3184 | '\n' |
| 3185 | ' >>> import pdb\n' |
| 3186 | ' >>> import mymodule\n' |
| 3187 | ' >>> mymodule.test()\n' |
| 3188 | ' Traceback (most recent call last):\n' |
| 3189 | ' File "<stdin>", line 1, in ?\n' |
| 3190 | ' File "./mymodule.py", line 4, in test\n' |
| 3191 | ' test2()\n' |
| 3192 | ' File "./mymodule.py", line 3, in test2\n' |
| 3193 | ' print(spam)\n' |
| 3194 | ' NameError: spam\n' |
| 3195 | ' >>> pdb.pm()\n' |
| 3196 | ' > ./mymodule.py(3)test2()\n' |
| 3197 | ' -> print(spam)\n' |
| 3198 | ' (Pdb)\n' |
| 3199 | '\n' |
| 3200 | 'The module defines the following functions; each enters the ' |
| 3201 | 'debugger\n' |
| 3202 | 'in a slightly different way:\n' |
| 3203 | '\n' |
| 3204 | 'pdb.run(statement, globals=None, locals=None)\n' |
| 3205 | '\n' |
| 3206 | ' Execute the *statement* (given as a string or a code ' |
| 3207 | 'object) under\n' |
| 3208 | ' debugger control. The debugger prompt appears before any ' |
| 3209 | 'code is\n' |
| 3210 | ' executed; you can set breakpoints and type "continue", or ' |
| 3211 | 'you can\n' |
| 3212 | ' step through the statement using "step" or "next" (all ' |
| 3213 | 'these\n' |
| 3214 | ' commands are explained below). The optional *globals* and ' |
| 3215 | '*locals*\n' |
| 3216 | ' arguments specify the environment in which the code is ' |
| 3217 | 'executed; by\n' |
| 3218 | ' default the dictionary of the module "__main__" is used. ' |
| 3219 | '(See the\n' |
| 3220 | ' explanation of the built-in "exec()" or "eval()" ' |
| 3221 | 'functions.)\n' |
| 3222 | '\n' |
| 3223 | 'pdb.runeval(expression, globals=None, locals=None)\n' |
| 3224 | '\n' |
| 3225 | ' Evaluate the *expression* (given as a string or a code ' |
| 3226 | 'object)\n' |
| 3227 | ' under debugger control. When "runeval()" returns, it ' |
| 3228 | 'returns the\n' |
| 3229 | ' value of the expression. Otherwise this function is ' |
| 3230 | 'similar to\n' |
| 3231 | ' "run()".\n' |
| 3232 | '\n' |
| 3233 | 'pdb.runcall(function, *args, **kwds)\n' |
| 3234 | '\n' |
| 3235 | ' Call the *function* (a function or method object, not a ' |
| 3236 | 'string)\n' |
| 3237 | ' with the given arguments. When "runcall()" returns, it ' |
| 3238 | 'returns\n' |
| 3239 | ' whatever the function call returned. The debugger prompt ' |
| 3240 | 'appears\n' |
| 3241 | ' as soon as the function is entered.\n' |
| 3242 | '\n' |
| 3243 | 'pdb.set_trace()\n' |
| 3244 | '\n' |
| 3245 | ' Enter the debugger at the calling stack frame. This is ' |
| 3246 | 'useful to\n' |
| 3247 | ' hard-code a breakpoint at a given point in a program, even ' |
| 3248 | 'if the\n' |
| 3249 | ' code is not otherwise being debugged (e.g. when an ' |
| 3250 | 'assertion\n' |
| 3251 | ' fails).\n' |
| 3252 | '\n' |
| 3253 | 'pdb.post_mortem(traceback=None)\n' |
| 3254 | '\n' |
| 3255 | ' Enter post-mortem debugging of the given *traceback* ' |
| 3256 | 'object. If no\n' |
| 3257 | ' *traceback* is given, it uses the one of the exception that ' |
| 3258 | 'is\n' |
| 3259 | ' currently being handled (an exception must be being handled ' |
| 3260 | 'if the\n' |
| 3261 | ' default is to be used).\n' |
| 3262 | '\n' |
| 3263 | 'pdb.pm()\n' |
| 3264 | '\n' |
| 3265 | ' Enter post-mortem debugging of the traceback found in\n' |
| 3266 | ' "sys.last_traceback".\n' |
| 3267 | '\n' |
| 3268 | 'The "run*" functions and "set_trace()" are aliases for ' |
| 3269 | 'instantiating\n' |
| 3270 | 'the "Pdb" class and calling the method of the same name. If ' |
| 3271 | 'you want\n' |
| 3272 | 'to access further features, you have to do this yourself:\n' |
| 3273 | '\n' |
| 3274 | "class class pdb.Pdb(completekey='tab', stdin=None, " |
| 3275 | 'stdout=None, skip=None, nosigint=False)\n' |
| 3276 | '\n' |
| 3277 | ' "Pdb" is the debugger class.\n' |
| 3278 | '\n' |
| 3279 | ' The *completekey*, *stdin* and *stdout* arguments are ' |
| 3280 | 'passed to the\n' |
| 3281 | ' underlying "cmd.Cmd" class; see the description there.\n' |
| 3282 | '\n' |
| 3283 | ' The *skip* argument, if given, must be an iterable of ' |
| 3284 | 'glob-style\n' |
| 3285 | ' module name patterns. The debugger will not step into ' |
| 3286 | 'frames that\n' |
| 3287 | ' originate in a module that matches one of these patterns. ' |
| 3288 | '[1]\n' |
| 3289 | '\n' |
| 3290 | ' By default, Pdb sets a handler for the SIGINT signal (which ' |
| 3291 | 'is sent\n' |
| 3292 | ' when the user presses Ctrl-C on the console) when you give ' |
| 3293 | 'a\n' |
| 3294 | ' "continue" command. This allows you to break into the ' |
| 3295 | 'debugger\n' |
| 3296 | ' again by pressing Ctrl-C. If you want Pdb not to touch the ' |
| 3297 | 'SIGINT\n' |
| 3298 | ' handler, set *nosigint* tot true.\n' |
| 3299 | '\n' |
| 3300 | ' Example call to enable tracing with *skip*:\n' |
| 3301 | '\n' |
| 3302 | " import pdb; pdb.Pdb(skip=['django.*']).set_trace()\n" |
| 3303 | '\n' |
| 3304 | ' New in version 3.1: The *skip* argument.\n' |
| 3305 | '\n' |
| 3306 | ' New in version 3.2: The *nosigint* argument. Previously, a ' |
| 3307 | 'SIGINT\n' |
| 3308 | ' handler was never set by Pdb.\n' |
| 3309 | '\n' |
| 3310 | ' run(statement, globals=None, locals=None)\n' |
| 3311 | ' runeval(expression, globals=None, locals=None)\n' |
| 3312 | ' runcall(function, *args, **kwds)\n' |
| 3313 | ' set_trace()\n' |
| 3314 | '\n' |
| 3315 | ' See the documentation for the functions explained ' |
| 3316 | 'above.\n' |
| 3317 | '\n' |
| 3318 | '\n' |
| 3319 | 'Debugger Commands\n' |
| 3320 | '=================\n' |
| 3321 | '\n' |
| 3322 | 'The commands recognized by the debugger are listed below. ' |
| 3323 | 'Most\n' |
| 3324 | 'commands can be abbreviated to one or two letters as ' |
| 3325 | 'indicated; e.g.\n' |
| 3326 | '"h(elp)" means that either "h" or "help" can be used to enter ' |
| 3327 | 'the help\n' |
| 3328 | 'command (but not "he" or "hel", nor "H" or "Help" or "HELP").\n' |
| 3329 | 'Arguments to commands must be separated by whitespace (spaces ' |
| 3330 | 'or\n' |
| 3331 | 'tabs). Optional arguments are enclosed in square brackets ' |
| 3332 | '("[]") in\n' |
| 3333 | 'the command syntax; the square brackets must not be typed.\n' |
| 3334 | 'Alternatives in the command syntax are separated by a vertical ' |
| 3335 | 'bar\n' |
| 3336 | '("|").\n' |
| 3337 | '\n' |
| 3338 | 'Entering a blank line repeats the last command entered. ' |
| 3339 | 'Exception: if\n' |
| 3340 | 'the last command was a "list" command, the next 11 lines are ' |
| 3341 | 'listed.\n' |
| 3342 | '\n' |
| 3343 | "Commands that the debugger doesn't recognize are assumed to be " |
| 3344 | 'Python\n' |
| 3345 | 'statements and are executed in the context of the program ' |
| 3346 | 'being\n' |
| 3347 | 'debugged. Python statements can also be prefixed with an ' |
| 3348 | 'exclamation\n' |
| 3349 | 'point ("!"). This is a powerful way to inspect the program ' |
| 3350 | 'being\n' |
| 3351 | 'debugged; it is even possible to change a variable or call a ' |
| 3352 | 'function.\n' |
| 3353 | 'When an exception occurs in such a statement, the exception ' |
| 3354 | 'name is\n' |
| 3355 | "printed but the debugger's state is not changed.\n" |
| 3356 | '\n' |
| 3357 | 'The debugger supports *aliases*. Aliases can have parameters ' |
| 3358 | 'which\n' |
| 3359 | 'allows one a certain level of adaptability to the context ' |
| 3360 | 'under\n' |
| 3361 | 'examination.\n' |
| 3362 | '\n' |
| 3363 | 'Multiple commands may be entered on a single line, separated ' |
| 3364 | 'by ";;".\n' |
| 3365 | '(A single ";" is not used as it is the separator for multiple ' |
| 3366 | 'commands\n' |
| 3367 | 'in a line that is passed to the Python parser.) No ' |
| 3368 | 'intelligence is\n' |
| 3369 | 'applied to separating the commands; the input is split at the ' |
| 3370 | 'first\n' |
| 3371 | '";;" pair, even if it is in the middle of a quoted string.\n' |
| 3372 | '\n' |
| 3373 | 'If a file ".pdbrc" exists in the user\'s home directory or in ' |
| 3374 | 'the\n' |
| 3375 | 'current directory, it is read in and executed as if it had ' |
| 3376 | 'been typed\n' |
| 3377 | 'at the debugger prompt. This is particularly useful for ' |
| 3378 | 'aliases. If\n' |
| 3379 | 'both files exist, the one in the home directory is read first ' |
| 3380 | 'and\n' |
| 3381 | 'aliases defined there can be overridden by the local file.\n' |
| 3382 | '\n' |
| 3383 | 'Changed in version 3.2: ".pdbrc" can now contain commands ' |
| 3384 | 'that\n' |
| 3385 | 'continue debugging, such as "continue" or "next". Previously, ' |
| 3386 | 'these\n' |
| 3387 | 'commands had no effect.\n' |
| 3388 | '\n' |
| 3389 | 'h(elp) [command]\n' |
| 3390 | '\n' |
| 3391 | ' Without argument, print the list of available commands. ' |
| 3392 | 'With a\n' |
| 3393 | ' *command* as argument, print help about that command. ' |
| 3394 | '"help pdb"\n' |
| 3395 | ' displays the full documentation (the docstring of the ' |
| 3396 | '"pdb"\n' |
| 3397 | ' module). Since the *command* argument must be an ' |
| 3398 | 'identifier, "help\n' |
| 3399 | ' exec" must be entered to get help on the "!" command.\n' |
| 3400 | '\n' |
| 3401 | 'w(here)\n' |
| 3402 | '\n' |
| 3403 | ' Print a stack trace, with the most recent frame at the ' |
| 3404 | 'bottom. An\n' |
| 3405 | ' arrow indicates the current frame, which determines the ' |
| 3406 | 'context of\n' |
| 3407 | ' most commands.\n' |
| 3408 | '\n' |
| 3409 | 'd(own) [count]\n' |
| 3410 | '\n' |
| 3411 | ' Move the current frame *count* (default one) levels down in ' |
| 3412 | 'the\n' |
| 3413 | ' stack trace (to a newer frame).\n' |
| 3414 | '\n' |
| 3415 | 'u(p) [count]\n' |
| 3416 | '\n' |
| 3417 | ' Move the current frame *count* (default one) levels up in ' |
| 3418 | 'the stack\n' |
| 3419 | ' trace (to an older frame).\n' |
| 3420 | '\n' |
| 3421 | 'b(reak) [([filename:]lineno | function) [, condition]]\n' |
| 3422 | '\n' |
| 3423 | ' With a *lineno* argument, set a break there in the current ' |
| 3424 | 'file.\n' |
| 3425 | ' With a *function* argument, set a break at the first ' |
| 3426 | 'executable\n' |
| 3427 | ' statement within that function. The line number may be ' |
| 3428 | 'prefixed\n' |
| 3429 | ' with a filename and a colon, to specify a breakpoint in ' |
| 3430 | 'another\n' |
| 3431 | " file (probably one that hasn't been loaded yet). The file " |
| 3432 | 'is\n' |
| 3433 | ' searched on "sys.path". Note that each breakpoint is ' |
| 3434 | 'assigned a\n' |
| 3435 | ' number to which all the other breakpoint commands refer.\n' |
| 3436 | '\n' |
| 3437 | ' If a second argument is present, it is an expression which ' |
| 3438 | 'must\n' |
| 3439 | ' evaluate to true before the breakpoint is honored.\n' |
| 3440 | '\n' |
| 3441 | ' Without argument, list all breaks, including for each ' |
| 3442 | 'breakpoint,\n' |
| 3443 | ' the number of times that breakpoint has been hit, the ' |
| 3444 | 'current\n' |
| 3445 | ' ignore count, and the associated condition if any.\n' |
| 3446 | '\n' |
| 3447 | 'tbreak [([filename:]lineno | function) [, condition]]\n' |
| 3448 | '\n' |
| 3449 | ' Temporary breakpoint, which is removed automatically when ' |
| 3450 | 'it is\n' |
| 3451 | ' first hit. The arguments are the same as for "break".\n' |
| 3452 | '\n' |
| 3453 | 'cl(ear) [filename:lineno | bpnumber [bpnumber ...]]\n' |
| 3454 | '\n' |
| 3455 | ' With a *filename:lineno* argument, clear all the ' |
| 3456 | 'breakpoints at\n' |
| 3457 | ' this line. With a space separated list of breakpoint ' |
| 3458 | 'numbers, clear\n' |
| 3459 | ' those breakpoints. Without argument, clear all breaks (but ' |
| 3460 | 'first\n' |
| 3461 | ' ask confirmation).\n' |
| 3462 | '\n' |
| 3463 | 'disable [bpnumber [bpnumber ...]]\n' |
| 3464 | '\n' |
| 3465 | ' Disable the breakpoints given as a space separated list of\n' |
| 3466 | ' breakpoint numbers. Disabling a breakpoint means it cannot ' |
| 3467 | 'cause\n' |
| 3468 | ' the program to stop execution, but unlike clearing a ' |
| 3469 | 'breakpoint, it\n' |
| 3470 | ' remains in the list of breakpoints and can be ' |
| 3471 | '(re-)enabled.\n' |
| 3472 | '\n' |
| 3473 | 'enable [bpnumber [bpnumber ...]]\n' |
| 3474 | '\n' |
| 3475 | ' Enable the breakpoints specified.\n' |
| 3476 | '\n' |
| 3477 | 'ignore bpnumber [count]\n' |
| 3478 | '\n' |
| 3479 | ' Set the ignore count for the given breakpoint number. If ' |
| 3480 | 'count is\n' |
| 3481 | ' omitted, the ignore count is set to 0. A breakpoint ' |
| 3482 | 'becomes active\n' |
| 3483 | ' when the ignore count is zero. When non-zero, the count ' |
| 3484 | 'is\n' |
| 3485 | ' decremented each time the breakpoint is reached and the ' |
| 3486 | 'breakpoint\n' |
| 3487 | ' is not disabled and any associated condition evaluates to ' |
| 3488 | 'true.\n' |
| 3489 | '\n' |
| 3490 | 'condition bpnumber [condition]\n' |
| 3491 | '\n' |
| 3492 | ' Set a new *condition* for the breakpoint, an expression ' |
| 3493 | 'which must\n' |
| 3494 | ' evaluate to true before the breakpoint is honored. If ' |
| 3495 | '*condition*\n' |
| 3496 | ' is absent, any existing condition is removed; i.e., the ' |
| 3497 | 'breakpoint\n' |
| 3498 | ' is made unconditional.\n' |
| 3499 | '\n' |
| 3500 | 'commands [bpnumber]\n' |
| 3501 | '\n' |
| 3502 | ' Specify a list of commands for breakpoint number ' |
| 3503 | '*bpnumber*. The\n' |
| 3504 | ' commands themselves appear on the following lines. Type a ' |
| 3505 | 'line\n' |
| 3506 | ' containing just "end" to terminate the commands. An ' |
| 3507 | 'example:\n' |
| 3508 | '\n' |
| 3509 | ' (Pdb) commands 1\n' |
| 3510 | ' (com) p some_variable\n' |
| 3511 | ' (com) end\n' |
| 3512 | ' (Pdb)\n' |
| 3513 | '\n' |
| 3514 | ' To remove all commands from a breakpoint, type commands and ' |
| 3515 | 'follow\n' |
| 3516 | ' it immediately with "end"; that is, give no commands.\n' |
| 3517 | '\n' |
| 3518 | ' With no *bpnumber* argument, commands refers to the last ' |
| 3519 | 'breakpoint\n' |
| 3520 | ' set.\n' |
| 3521 | '\n' |
| 3522 | ' You can use breakpoint commands to start your program up ' |
| 3523 | 'again.\n' |
| 3524 | ' Simply use the continue command, or step, or any other ' |
| 3525 | 'command that\n' |
| 3526 | ' resumes execution.\n' |
| 3527 | '\n' |
| 3528 | ' Specifying any command resuming execution (currently ' |
| 3529 | 'continue,\n' |
| 3530 | ' step, next, return, jump, quit and their abbreviations) ' |
| 3531 | 'terminates\n' |
| 3532 | ' the command list (as if that command was immediately ' |
| 3533 | 'followed by\n' |
| 3534 | ' end). This is because any time you resume execution (even ' |
| 3535 | 'with a\n' |
| 3536 | ' simple next or step), you may encounter another ' |
| 3537 | 'breakpoint--which\n' |
| 3538 | ' could have its own command list, leading to ambiguities ' |
| 3539 | 'about which\n' |
| 3540 | ' list to execute.\n' |
| 3541 | '\n' |
| 3542 | " If you use the 'silent' command in the command list, the " |
| 3543 | 'usual\n' |
| 3544 | ' message about stopping at a breakpoint is not printed. ' |
| 3545 | 'This may be\n' |
| 3546 | ' desirable for breakpoints that are to print a specific ' |
| 3547 | 'message and\n' |
| 3548 | ' then continue. If none of the other commands print ' |
| 3549 | 'anything, you\n' |
| 3550 | ' see no sign that the breakpoint was reached.\n' |
| 3551 | '\n' |
| 3552 | 's(tep)\n' |
| 3553 | '\n' |
| 3554 | ' Execute the current line, stop at the first possible ' |
| 3555 | 'occasion\n' |
| 3556 | ' (either in a function that is called or on the next line in ' |
| 3557 | 'the\n' |
| 3558 | ' current function).\n' |
| 3559 | '\n' |
| 3560 | 'n(ext)\n' |
| 3561 | '\n' |
| 3562 | ' Continue execution until the next line in the current ' |
| 3563 | 'function is\n' |
| 3564 | ' reached or it returns. (The difference between "next" and ' |
| 3565 | '"step"\n' |
| 3566 | ' is that "step" stops inside a called function, while ' |
| 3567 | '"next"\n' |
| 3568 | ' executes called functions at (nearly) full speed, only ' |
| 3569 | 'stopping at\n' |
| 3570 | ' the next line in the current function.)\n' |
| 3571 | '\n' |
| 3572 | 'unt(il) [lineno]\n' |
| 3573 | '\n' |
| 3574 | ' Without argument, continue execution until the line with a ' |
| 3575 | 'number\n' |
| 3576 | ' greater than the current one is reached.\n' |
| 3577 | '\n' |
| 3578 | ' With a line number, continue execution until a line with a ' |
| 3579 | 'number\n' |
| 3580 | ' greater or equal to that is reached. In both cases, also ' |
| 3581 | 'stop when\n' |
| 3582 | ' the current frame returns.\n' |
| 3583 | '\n' |
| 3584 | ' Changed in version 3.2: Allow giving an explicit line ' |
| 3585 | 'number.\n' |
| 3586 | '\n' |
| 3587 | 'r(eturn)\n' |
| 3588 | '\n' |
| 3589 | ' Continue execution until the current function returns.\n' |
| 3590 | '\n' |
| 3591 | 'c(ont(inue))\n' |
| 3592 | '\n' |
| 3593 | ' Continue execution, only stop when a breakpoint is ' |
| 3594 | 'encountered.\n' |
| 3595 | '\n' |
| 3596 | 'j(ump) lineno\n' |
| 3597 | '\n' |
| 3598 | ' Set the next line that will be executed. Only available in ' |
| 3599 | 'the\n' |
| 3600 | ' bottom-most frame. This lets you jump back and execute ' |
| 3601 | 'code again,\n' |
| 3602 | " or jump forward to skip code that you don't want to run.\n" |
| 3603 | '\n' |
| 3604 | ' It should be noted that not all jumps are allowed -- for ' |
| 3605 | 'instance\n' |
| 3606 | ' it is not possible to jump into the middle of a "for" loop ' |
| 3607 | 'or out\n' |
| 3608 | ' of a "finally" clause.\n' |
| 3609 | '\n' |
| 3610 | 'l(ist) [first[, last]]\n' |
| 3611 | '\n' |
| 3612 | ' List source code for the current file. Without arguments, ' |
| 3613 | 'list 11\n' |
| 3614 | ' lines around the current line or continue the previous ' |
| 3615 | 'listing.\n' |
| 3616 | ' With "." as argument, list 11 lines around the current ' |
| 3617 | 'line. With\n' |
| 3618 | ' one argument, list 11 lines around at that line. With two\n' |
| 3619 | ' arguments, list the given range; if the second argument is ' |
| 3620 | 'less\n' |
| 3621 | ' than the first, it is interpreted as a count.\n' |
| 3622 | '\n' |
| 3623 | ' The current line in the current frame is indicated by ' |
| 3624 | '"->". If an\n' |
| 3625 | ' exception is being debugged, the line where the exception ' |
| 3626 | 'was\n' |
| 3627 | ' originally raised or propagated is indicated by ">>", if it ' |
| 3628 | 'differs\n' |
| 3629 | ' from the current line.\n' |
| 3630 | '\n' |
| 3631 | ' New in version 3.2: The ">>" marker.\n' |
| 3632 | '\n' |
| 3633 | 'll | longlist\n' |
| 3634 | '\n' |
| 3635 | ' List all source code for the current function or frame.\n' |
| 3636 | ' Interesting lines are marked as for "list".\n' |
| 3637 | '\n' |
| 3638 | ' New in version 3.2.\n' |
| 3639 | '\n' |
| 3640 | 'a(rgs)\n' |
| 3641 | '\n' |
| 3642 | ' Print the argument list of the current function.\n' |
| 3643 | '\n' |
| 3644 | 'p expression\n' |
| 3645 | '\n' |
| 3646 | ' Evaluate the *expression* in the current context and print ' |
| 3647 | 'its\n' |
| 3648 | ' value.\n' |
| 3649 | '\n' |
| 3650 | ' Note: "print()" can also be used, but is not a debugger ' |
| 3651 | 'command\n' |
| 3652 | ' --- this executes the Python "print()" function.\n' |
| 3653 | '\n' |
| 3654 | 'pp expression\n' |
| 3655 | '\n' |
| 3656 | ' Like the "p" command, except the value of the expression is ' |
| 3657 | 'pretty-\n' |
| 3658 | ' printed using the "pprint" module.\n' |
| 3659 | '\n' |
| 3660 | 'whatis expression\n' |
| 3661 | '\n' |
| 3662 | ' Print the type of the *expression*.\n' |
| 3663 | '\n' |
| 3664 | 'source expression\n' |
| 3665 | '\n' |
| 3666 | ' Try to get source code for the given object and display ' |
| 3667 | 'it.\n' |
| 3668 | '\n' |
| 3669 | ' New in version 3.2.\n' |
| 3670 | '\n' |
| 3671 | 'display [expression]\n' |
| 3672 | '\n' |
| 3673 | ' Display the value of the expression if it changed, each ' |
| 3674 | 'time\n' |
| 3675 | ' execution stops in the current frame.\n' |
| 3676 | '\n' |
| 3677 | ' Without expression, list all display expressions for the ' |
| 3678 | 'current\n' |
| 3679 | ' frame.\n' |
| 3680 | '\n' |
| 3681 | ' New in version 3.2.\n' |
| 3682 | '\n' |
| 3683 | 'undisplay [expression]\n' |
| 3684 | '\n' |
| 3685 | ' Do not display the expression any more in the current ' |
| 3686 | 'frame.\n' |
| 3687 | ' Without expression, clear all display expressions for the ' |
| 3688 | 'current\n' |
| 3689 | ' frame.\n' |
| 3690 | '\n' |
| 3691 | ' New in version 3.2.\n' |
| 3692 | '\n' |
| 3693 | 'interact\n' |
| 3694 | '\n' |
| 3695 | ' Start an interative interpreter (using the "code" module) ' |
| 3696 | 'whose\n' |
| 3697 | ' global namespace contains all the (global and local) names ' |
| 3698 | 'found in\n' |
| 3699 | ' the current scope.\n' |
| 3700 | '\n' |
| 3701 | ' New in version 3.2.\n' |
| 3702 | '\n' |
| 3703 | 'alias [name [command]]\n' |
| 3704 | '\n' |
| 3705 | ' Create an alias called *name* that executes *command*. The ' |
| 3706 | 'command\n' |
| 3707 | ' must *not* be enclosed in quotes. Replaceable parameters ' |
| 3708 | 'can be\n' |
| 3709 | ' indicated by "%1", "%2", and so on, while "%*" is replaced ' |
| 3710 | 'by all\n' |
| 3711 | ' the parameters. If no command is given, the current alias ' |
| 3712 | 'for\n' |
| 3713 | ' *name* is shown. If no arguments are given, all aliases are ' |
| 3714 | 'listed.\n' |
| 3715 | '\n' |
| 3716 | ' Aliases may be nested and can contain anything that can be ' |
| 3717 | 'legally\n' |
| 3718 | ' typed at the pdb prompt. Note that internal pdb commands ' |
| 3719 | '*can* be\n' |
| 3720 | ' overridden by aliases. Such a command is then hidden until ' |
| 3721 | 'the\n' |
| 3722 | ' alias is removed. Aliasing is recursively applied to the ' |
| 3723 | 'first\n' |
| 3724 | ' word of the command line; all other words in the line are ' |
| 3725 | 'left\n' |
| 3726 | ' alone.\n' |
| 3727 | '\n' |
| 3728 | ' As an example, here are two useful aliases (especially when ' |
| 3729 | 'placed\n' |
| 3730 | ' in the ".pdbrc" file):\n' |
| 3731 | '\n' |
| 3732 | ' # Print instance variables (usage "pi classInst")\n' |
| 3733 | ' alias pi for k in %1.__dict__.keys(): ' |
| 3734 | 'print("%1.",k,"=",%1.__dict__[k])\n' |
| 3735 | ' # Print instance variables in self\n' |
| 3736 | ' alias ps pi self\n' |
| 3737 | '\n' |
| 3738 | 'unalias name\n' |
| 3739 | '\n' |
| 3740 | ' Delete the specified alias.\n' |
| 3741 | '\n' |
| 3742 | '! statement\n' |
| 3743 | '\n' |
| 3744 | ' Execute the (one-line) *statement* in the context of the ' |
| 3745 | 'current\n' |
| 3746 | ' stack frame. The exclamation point can be omitted unless ' |
| 3747 | 'the first\n' |
| 3748 | ' word of the statement resembles a debugger command. To set ' |
| 3749 | 'a\n' |
| 3750 | ' global variable, you can prefix the assignment command with ' |
| 3751 | 'a\n' |
| 3752 | ' "global" statement on the same line, e.g.:\n' |
| 3753 | '\n' |
| 3754 | " (Pdb) global list_options; list_options = ['-l']\n" |
| 3755 | ' (Pdb)\n' |
| 3756 | '\n' |
| 3757 | 'run [args ...]\n' |
| 3758 | 'restart [args ...]\n' |
| 3759 | '\n' |
| 3760 | ' Restart the debugged Python program. If an argument is ' |
| 3761 | 'supplied,\n' |
| 3762 | ' it is split with "shlex" and the result is used as the new\n' |
| 3763 | ' "sys.argv". History, breakpoints, actions and debugger ' |
| 3764 | 'options are\n' |
| 3765 | ' preserved. "restart" is an alias for "run".\n' |
| 3766 | '\n' |
| 3767 | 'q(uit)\n' |
| 3768 | '\n' |
| 3769 | ' Quit from the debugger. The program being executed is ' |
| 3770 | 'aborted.\n' |
| 3771 | '\n' |
| 3772 | '-[ Footnotes ]-\n' |
| 3773 | '\n' |
| 3774 | '[1] Whether a frame is considered to originate in a certain ' |
| 3775 | 'module\n' |
| 3776 | ' is determined by the "__name__" in the frame globals.\n', |
| 3777 | 'del': '\n' |
| 3778 | 'The "del" statement\n' |
| 3779 | '*******************\n' |
| 3780 | '\n' |
| 3781 | ' del_stmt ::= "del" target_list\n' |
| 3782 | '\n' |
| 3783 | 'Deletion is recursively defined very similar to the way assignment ' |
| 3784 | 'is\n' |
| 3785 | 'defined. Rather than spelling it out in full details, here are ' |
| 3786 | 'some\n' |
| 3787 | 'hints.\n' |
| 3788 | '\n' |
| 3789 | 'Deletion of a target list recursively deletes each target, from ' |
| 3790 | 'left\n' |
| 3791 | 'to right.\n' |
| 3792 | '\n' |
| 3793 | 'Deletion of a name removes the binding of that name from the local ' |
| 3794 | 'or\n' |
| 3795 | 'global namespace, depending on whether the name occurs in a ' |
| 3796 | '"global"\n' |
| 3797 | 'statement in the same code block. If the name is unbound, a\n' |
| 3798 | '"NameError" exception will be raised.\n' |
| 3799 | '\n' |
| 3800 | 'Deletion of attribute references, subscriptions and slicings is ' |
| 3801 | 'passed\n' |
| 3802 | 'to the primary object involved; deletion of a slicing is in ' |
| 3803 | 'general\n' |
| 3804 | 'equivalent to assignment of an empty slice of the right type (but ' |
| 3805 | 'even\n' |
| 3806 | 'this is determined by the sliced object).\n' |
| 3807 | '\n' |
| 3808 | 'Changed in version 3.2: Previously it was illegal to delete a name\n' |
| 3809 | 'from the local namespace if it occurs as a free variable in a ' |
| 3810 | 'nested\n' |
| 3811 | 'block.\n', |
| 3812 | 'dict': '\n' |
| 3813 | 'Dictionary displays\n' |
| 3814 | '*******************\n' |
| 3815 | '\n' |
| 3816 | 'A dictionary display is a possibly empty series of key/datum ' |
| 3817 | 'pairs\n' |
| 3818 | 'enclosed in curly braces:\n' |
| 3819 | '\n' |
| 3820 | ' dict_display ::= "{" [key_datum_list | ' |
| 3821 | 'dict_comprehension] "}"\n' |
| 3822 | ' key_datum_list ::= key_datum ("," key_datum)* [","]\n' |
| 3823 | ' key_datum ::= expression ":" expression\n' |
| 3824 | ' dict_comprehension ::= expression ":" expression comp_for\n' |
| 3825 | '\n' |
| 3826 | 'A dictionary display yields a new dictionary object.\n' |
| 3827 | '\n' |
| 3828 | 'If a comma-separated sequence of key/datum pairs is given, they ' |
| 3829 | 'are\n' |
| 3830 | 'evaluated from left to right to define the entries of the ' |
| 3831 | 'dictionary:\n' |
| 3832 | 'each key object is used as a key into the dictionary to store the\n' |
| 3833 | 'corresponding datum. This means that you can specify the same ' |
| 3834 | 'key\n' |
| 3835 | "multiple times in the key/datum list, and the final dictionary's " |
| 3836 | 'value\n' |
| 3837 | 'for that key will be the last one given.\n' |
| 3838 | '\n' |
| 3839 | 'A dict comprehension, in contrast to list and set comprehensions,\n' |
| 3840 | 'needs two expressions separated with a colon followed by the ' |
| 3841 | 'usual\n' |
| 3842 | '"for" and "if" clauses. When the comprehension is run, the ' |
| 3843 | 'resulting\n' |
| 3844 | 'key and value elements are inserted in the new dictionary in the ' |
| 3845 | 'order\n' |
| 3846 | 'they are produced.\n' |
| 3847 | '\n' |
| 3848 | 'Restrictions on the types of the key values are listed earlier in\n' |
| 3849 | 'section *The standard type hierarchy*. (To summarize, the key ' |
| 3850 | 'type\n' |
| 3851 | 'should be *hashable*, which excludes all mutable objects.) ' |
| 3852 | 'Clashes\n' |
| 3853 | 'between duplicate keys are not detected; the last datum ' |
| 3854 | '(textually\n' |
| 3855 | 'rightmost in the display) stored for a given key value prevails.\n', |
| 3856 | 'dynamic-features': '\n' |
| 3857 | 'Interaction with dynamic features\n' |
| 3858 | '*********************************\n' |
| 3859 | '\n' |
| 3860 | 'Name resolution of free variables occurs at runtime, ' |
| 3861 | 'not at compile\n' |
| 3862 | 'time. This means that the following code will print ' |
| 3863 | '42:\n' |
| 3864 | '\n' |
| 3865 | ' i = 10\n' |
| 3866 | ' def f():\n' |
| 3867 | ' print(i)\n' |
| 3868 | ' i = 42\n' |
| 3869 | ' f()\n' |
| 3870 | '\n' |
| 3871 | 'There are several cases where Python statements are ' |
| 3872 | 'illegal when used\n' |
| 3873 | 'in conjunction with nested scopes that contain free ' |
| 3874 | 'variables.\n' |
| 3875 | '\n' |
| 3876 | 'If a variable is referenced in an enclosing scope, it ' |
| 3877 | 'is illegal to\n' |
| 3878 | 'delete the name. An error will be reported at compile ' |
| 3879 | 'time.\n' |
| 3880 | '\n' |
| 3881 | 'The "eval()" and "exec()" functions do not have access ' |
| 3882 | 'to the full\n' |
| 3883 | 'environment for resolving names. Names may be ' |
| 3884 | 'resolved in the local\n' |
| 3885 | 'and global namespaces of the caller. Free variables ' |
| 3886 | 'are not resolved\n' |
| 3887 | 'in the nearest enclosing namespace, but in the global ' |
| 3888 | 'namespace. [1]\n' |
| 3889 | 'The "exec()" and "eval()" functions have optional ' |
| 3890 | 'arguments to\n' |
| 3891 | 'override the global and local namespace. If only one ' |
| 3892 | 'namespace is\n' |
| 3893 | 'specified, it is used for both.\n', |
| 3894 | 'else': '\n' |
| 3895 | 'The "if" statement\n' |
| 3896 | '******************\n' |
| 3897 | '\n' |
| 3898 | 'The "if" statement is used for conditional execution:\n' |
| 3899 | '\n' |
| 3900 | ' if_stmt ::= "if" expression ":" suite\n' |
| 3901 | ' ( "elif" expression ":" suite )*\n' |
| 3902 | ' ["else" ":" suite]\n' |
| 3903 | '\n' |
| 3904 | 'It selects exactly one of the suites by evaluating the expressions ' |
| 3905 | 'one\n' |
| 3906 | 'by one until one is found to be true (see section *Boolean ' |
| 3907 | 'operations*\n' |
| 3908 | 'for the definition of true and false); then that suite is ' |
| 3909 | 'executed\n' |
| 3910 | '(and no other part of the "if" statement is executed or ' |
| 3911 | 'evaluated).\n' |
| 3912 | 'If all expressions are false, the suite of the "else" clause, if\n' |
| 3913 | 'present, is executed.\n', |
| 3914 | 'exceptions': '\n' |
| 3915 | 'Exceptions\n' |
| 3916 | '**********\n' |
| 3917 | '\n' |
| 3918 | 'Exceptions are a means of breaking out of the normal flow of ' |
| 3919 | 'control\n' |
| 3920 | 'of a code block in order to handle errors or other ' |
| 3921 | 'exceptional\n' |
| 3922 | 'conditions. An exception is *raised* at the point where the ' |
| 3923 | 'error is\n' |
| 3924 | 'detected; it may be *handled* by the surrounding code block ' |
| 3925 | 'or by any\n' |
| 3926 | 'code block that directly or indirectly invoked the code ' |
| 3927 | 'block where\n' |
| 3928 | 'the error occurred.\n' |
| 3929 | '\n' |
| 3930 | 'The Python interpreter raises an exception when it detects a ' |
| 3931 | 'run-time\n' |
| 3932 | 'error (such as division by zero). A Python program can ' |
| 3933 | 'also\n' |
| 3934 | 'explicitly raise an exception with the "raise" statement. ' |
| 3935 | 'Exception\n' |
| 3936 | 'handlers are specified with the "try" ... "except" ' |
| 3937 | 'statement. The\n' |
| 3938 | '"finally" clause of such a statement can be used to specify ' |
| 3939 | 'cleanup\n' |
| 3940 | 'code which does not handle the exception, but is executed ' |
| 3941 | 'whether an\n' |
| 3942 | 'exception occurred or not in the preceding code.\n' |
| 3943 | '\n' |
| 3944 | 'Python uses the "termination" model of error handling: an ' |
| 3945 | 'exception\n' |
| 3946 | 'handler can find out what happened and continue execution at ' |
| 3947 | 'an outer\n' |
| 3948 | 'level, but it cannot repair the cause of the error and retry ' |
| 3949 | 'the\n' |
| 3950 | 'failing operation (except by re-entering the offending piece ' |
| 3951 | 'of code\n' |
| 3952 | 'from the top).\n' |
| 3953 | '\n' |
| 3954 | 'When an exception is not handled at all, the interpreter ' |
| 3955 | 'terminates\n' |
| 3956 | 'execution of the program, or returns to its interactive main ' |
| 3957 | 'loop. In\n' |
| 3958 | 'either case, it prints a stack backtrace, except when the ' |
| 3959 | 'exception is\n' |
| 3960 | '"SystemExit".\n' |
| 3961 | '\n' |
| 3962 | 'Exceptions are identified by class instances. The "except" ' |
| 3963 | 'clause is\n' |
| 3964 | 'selected depending on the class of the instance: it must ' |
| 3965 | 'reference the\n' |
| 3966 | 'class of the instance or a base class thereof. The instance ' |
| 3967 | 'can be\n' |
| 3968 | 'received by the handler and can carry additional information ' |
| 3969 | 'about the\n' |
| 3970 | 'exceptional condition.\n' |
| 3971 | '\n' |
| 3972 | 'Note: Exception messages are not part of the Python API. ' |
| 3973 | 'Their\n' |
| 3974 | ' contents may change from one version of Python to the next ' |
| 3975 | 'without\n' |
| 3976 | ' warning and should not be relied on by code which will run ' |
| 3977 | 'under\n' |
| 3978 | ' multiple versions of the interpreter.\n' |
| 3979 | '\n' |
| 3980 | 'See also the description of the "try" statement in section ' |
| 3981 | '*The try\n' |
| 3982 | 'statement* and "raise" statement in section *The raise ' |
| 3983 | 'statement*.\n' |
| 3984 | '\n' |
| 3985 | '-[ Footnotes ]-\n' |
| 3986 | '\n' |
| 3987 | '[1] This limitation occurs because the code that is executed ' |
| 3988 | 'by\n' |
| 3989 | ' these operations is not available at the time the module ' |
| 3990 | 'is\n' |
| 3991 | ' compiled.\n', |
| 3992 | 'execmodel': '\n' |
| 3993 | 'Execution model\n' |
| 3994 | '***************\n' |
| 3995 | '\n' |
| 3996 | '\n' |
| 3997 | 'Structure of a programm\n' |
| 3998 | '=======================\n' |
| 3999 | '\n' |
| 4000 | 'A Python program is constructed from code blocks. A *block* ' |
| 4001 | 'is a piece\n' |
| 4002 | 'of Python program text that is executed as a unit. The ' |
| 4003 | 'following are\n' |
| 4004 | 'blocks: a module, a function body, and a class definition. ' |
| 4005 | 'Each\n' |
| 4006 | 'command typed interactively is a block. A script file (a ' |
| 4007 | 'file given\n' |
| 4008 | 'as standard input to the interpreter or specified as a ' |
| 4009 | 'command line\n' |
| 4010 | 'argument to the interpreter) is a code block. A script ' |
| 4011 | 'command (a\n' |
| 4012 | 'command specified on the interpreter command line with the ' |
| 4013 | "'**-c**'\n" |
| 4014 | 'option) is a code block. The string argument passed to the ' |
| 4015 | 'built-in\n' |
| 4016 | 'functions "eval()" and "exec()" is a code block.\n' |
| 4017 | '\n' |
| 4018 | 'A code block is executed in an *execution frame*. A frame ' |
| 4019 | 'contains\n' |
| 4020 | 'some administrative information (used for debugging) and ' |
| 4021 | 'determines\n' |
| 4022 | "where and how execution continues after the code block's " |
| 4023 | 'execution has\n' |
| 4024 | 'completed.\n' |
| 4025 | '\n' |
| 4026 | '\n' |
| 4027 | 'Naming and binding\n' |
| 4028 | '==================\n' |
| 4029 | '\n' |
| 4030 | '\n' |
| 4031 | 'Binding of names\n' |
| 4032 | '----------------\n' |
| 4033 | '\n' |
| 4034 | '*Names* refer to objects. Names are introduced by name ' |
| 4035 | 'binding\n' |
| 4036 | 'operations.\n' |
| 4037 | '\n' |
| 4038 | 'The following constructs bind names: formal parameters to ' |
| 4039 | 'functions,\n' |
| 4040 | '"import" statements, class and function definitions (these ' |
| 4041 | 'bind the\n' |
| 4042 | 'class or function name in the defining block), and targets ' |
| 4043 | 'that are\n' |
| 4044 | 'identifiers if occurring in an assignment, "for" loop header, ' |
| 4045 | 'or after\n' |
| 4046 | '"as" in a "with" statement or "except" clause. The "import" ' |
| 4047 | 'statement\n' |
| 4048 | 'of the form "from ... import *" binds all names defined in ' |
| 4049 | 'the\n' |
| 4050 | 'imported module, except those beginning with an underscore. ' |
| 4051 | 'This form\n' |
| 4052 | 'may only be used at the module level.\n' |
| 4053 | '\n' |
| 4054 | 'A target occurring in a "del" statement is also considered ' |
| 4055 | 'bound for\n' |
| 4056 | 'this purpose (though the actual semantics are to unbind the ' |
| 4057 | 'name).\n' |
| 4058 | '\n' |
| 4059 | 'Each assignment or import statement occurs within a block ' |
| 4060 | 'defined by a\n' |
| 4061 | 'class or function definition or at the module level (the ' |
| 4062 | 'top-level\n' |
| 4063 | 'code block).\n' |
| 4064 | '\n' |
| 4065 | 'If a name is bound in a block, it is a local variable of that ' |
| 4066 | 'block,\n' |
| 4067 | 'unless declared as "nonlocal" or "global". If a name is ' |
| 4068 | 'bound at the\n' |
| 4069 | 'module level, it is a global variable. (The variables of the ' |
| 4070 | 'module\n' |
| 4071 | 'code block are local and global.) If a variable is used in a ' |
| 4072 | 'code\n' |
| 4073 | 'block but not defined there, it is a *free variable*.\n' |
| 4074 | '\n' |
| 4075 | 'Each occurrence of a name in the program text refers to the ' |
| 4076 | '*binding*\n' |
| 4077 | 'of that name established by the following name resolution ' |
| 4078 | 'rules.\n' |
| 4079 | '\n' |
| 4080 | '\n' |
| 4081 | 'Resolution of names\n' |
| 4082 | '-------------------\n' |
| 4083 | '\n' |
| 4084 | 'A *scope* defines the visibility of a name within a block. ' |
| 4085 | 'If a local\n' |
| 4086 | 'variable is defined in a block, its scope includes that ' |
| 4087 | 'block. If the\n' |
| 4088 | 'definition occurs in a function block, the scope extends to ' |
| 4089 | 'any blocks\n' |
| 4090 | 'contained within the defining one, unless a contained block ' |
| 4091 | 'introduces\n' |
| 4092 | 'a different binding for the name.\n' |
| 4093 | '\n' |
| 4094 | 'When a name is used in a code block, it is resolved using the ' |
| 4095 | 'nearest\n' |
| 4096 | 'enclosing scope. The set of all such scopes visible to a ' |
| 4097 | 'code block\n' |
| 4098 | "is called the block's *environment*.\n" |
| 4099 | '\n' |
| 4100 | 'When a name is not found at all, a "NameError" exception is ' |
| 4101 | 'raised. If\n' |
| 4102 | 'the current scope is a function scope, and the name refers to ' |
| 4103 | 'a local\n' |
| 4104 | 'variable that has not yet been bound to a value at the point ' |
| 4105 | 'where the\n' |
| 4106 | 'name is used, an "UnboundLocalError" exception is raised.\n' |
| 4107 | '"UnboundLocalError" is a subclass of "NameError".\n' |
| 4108 | '\n' |
| 4109 | 'If a name binding operation occurs anywhere within a code ' |
| 4110 | 'block, all\n' |
| 4111 | 'uses of the name within the block are treated as references ' |
| 4112 | 'to the\n' |
| 4113 | 'current block. This can lead to errors when a name is used ' |
| 4114 | 'within a\n' |
| 4115 | 'block before it is bound. This rule is subtle. Python ' |
| 4116 | 'lacks\n' |
| 4117 | 'declarations and allows name binding operations to occur ' |
| 4118 | 'anywhere\n' |
| 4119 | 'within a code block. The local variables of a code block can ' |
| 4120 | 'be\n' |
| 4121 | 'determined by scanning the entire text of the block for name ' |
| 4122 | 'binding\n' |
| 4123 | 'operations.\n' |
| 4124 | '\n' |
| 4125 | 'If the "global" statement occurs within a block, all uses of ' |
| 4126 | 'the name\n' |
| 4127 | 'specified in the statement refer to the binding of that name ' |
| 4128 | 'in the\n' |
| 4129 | 'top-level namespace. Names are resolved in the top-level ' |
| 4130 | 'namespace by\n' |
| 4131 | 'searching the global namespace, i.e. the namespace of the ' |
| 4132 | 'module\n' |
| 4133 | 'containing the code block, and the builtins namespace, the ' |
| 4134 | 'namespace\n' |
| 4135 | 'of the module "builtins". The global namespace is searched ' |
| 4136 | 'first. If\n' |
| 4137 | 'the name is not found there, the builtins namespace is ' |
| 4138 | 'searched. The\n' |
| 4139 | '"global" statement must precede all uses of the name.\n' |
| 4140 | '\n' |
| 4141 | 'The "global" statement has the same scope as a name binding ' |
| 4142 | 'operation\n' |
| 4143 | 'in the same block. If the nearest enclosing scope for a free ' |
| 4144 | 'variable\n' |
| 4145 | 'contains a global statement, the free variable is treated as ' |
| 4146 | 'a global.\n' |
| 4147 | '\n' |
| 4148 | 'The "nonlocal" statement causes corresponding names to refer ' |
| 4149 | 'to\n' |
| 4150 | 'previously bound variables in the nearest enclosing function ' |
| 4151 | 'scope.\n' |
| 4152 | '"SyntaxError" is raised at compile time if the given name ' |
| 4153 | 'does not\n' |
| 4154 | 'exist in any enclosing function scope.\n' |
| 4155 | '\n' |
| 4156 | 'The namespace for a module is automatically created the first ' |
| 4157 | 'time a\n' |
| 4158 | 'module is imported. The main module for a script is always ' |
| 4159 | 'called\n' |
| 4160 | '"__main__".\n' |
| 4161 | '\n' |
| 4162 | 'Class definition blocks and arguments to "exec()" and ' |
| 4163 | '"eval()" are\n' |
| 4164 | 'special in the context of name resolution. A class definition ' |
| 4165 | 'is an\n' |
| 4166 | 'executable statement that may use and define names. These ' |
| 4167 | 'references\n' |
| 4168 | 'follow the normal rules for name resolution with an exception ' |
| 4169 | 'that\n' |
| 4170 | 'unbound local variables are looked up in the global ' |
| 4171 | 'namespace. The\n' |
| 4172 | 'namespace of the class definition becomes the attribute ' |
| 4173 | 'dictionary of\n' |
| 4174 | 'the class. The scope of names defined in a class block is ' |
| 4175 | 'limited to\n' |
| 4176 | 'the class block; it does not extend to the code blocks of ' |
| 4177 | 'methods --\n' |
| 4178 | 'this includes comprehensions and generator expressions since ' |
| 4179 | 'they are\n' |
| 4180 | 'implemented using a function scope. This means that the ' |
| 4181 | 'following\n' |
| 4182 | 'will fail:\n' |
| 4183 | '\n' |
| 4184 | ' class A:\n' |
| 4185 | ' a = 42\n' |
| 4186 | ' b = list(a + i for i in range(10))\n' |
| 4187 | '\n' |
| 4188 | '\n' |
| 4189 | 'Builtins and restricted execution\n' |
| 4190 | '---------------------------------\n' |
| 4191 | '\n' |
| 4192 | 'The builtins namespace associated with the execution of a ' |
| 4193 | 'code block\n' |
| 4194 | 'is actually found by looking up the name "__builtins__" in ' |
| 4195 | 'its global\n' |
| 4196 | 'namespace; this should be a dictionary or a module (in the ' |
| 4197 | 'latter case\n' |
| 4198 | "the module's dictionary is used). By default, when in the " |
| 4199 | '"__main__"\n' |
| 4200 | 'module, "__builtins__" is the built-in module "builtins"; ' |
| 4201 | 'when in any\n' |
| 4202 | 'other module, "__builtins__" is an alias for the dictionary ' |
| 4203 | 'of the\n' |
| 4204 | '"builtins" module itself. "__builtins__" can be set to a ' |
| 4205 | 'user-created\n' |
| 4206 | 'dictionary to create a weak form of restricted execution.\n' |
| 4207 | '\n' |
| 4208 | '**CPython implementation detail:** Users should not touch\n' |
| 4209 | '"__builtins__"; it is strictly an implementation detail. ' |
| 4210 | 'Users\n' |
| 4211 | 'wanting to override values in the builtins namespace should ' |
| 4212 | '"import"\n' |
| 4213 | 'the "builtins" module and modify its attributes ' |
| 4214 | 'appropriately.\n' |
| 4215 | '\n' |
| 4216 | '\n' |
| 4217 | 'Interaction with dynamic features\n' |
| 4218 | '---------------------------------\n' |
| 4219 | '\n' |
| 4220 | 'Name resolution of free variables occurs at runtime, not at ' |
| 4221 | 'compile\n' |
| 4222 | 'time. This means that the following code will print 42:\n' |
| 4223 | '\n' |
| 4224 | ' i = 10\n' |
| 4225 | ' def f():\n' |
| 4226 | ' print(i)\n' |
| 4227 | ' i = 42\n' |
| 4228 | ' f()\n' |
| 4229 | '\n' |
| 4230 | 'There are several cases where Python statements are illegal ' |
| 4231 | 'when used\n' |
| 4232 | 'in conjunction with nested scopes that contain free ' |
| 4233 | 'variables.\n' |
| 4234 | '\n' |
| 4235 | 'If a variable is referenced in an enclosing scope, it is ' |
| 4236 | 'illegal to\n' |
| 4237 | 'delete the name. An error will be reported at compile time.\n' |
| 4238 | '\n' |
| 4239 | 'The "eval()" and "exec()" functions do not have access to the ' |
| 4240 | 'full\n' |
| 4241 | 'environment for resolving names. Names may be resolved in ' |
| 4242 | 'the local\n' |
| 4243 | 'and global namespaces of the caller. Free variables are not ' |
| 4244 | 'resolved\n' |
| 4245 | 'in the nearest enclosing namespace, but in the global ' |
| 4246 | 'namespace. [1]\n' |
| 4247 | 'The "exec()" and "eval()" functions have optional arguments ' |
| 4248 | 'to\n' |
| 4249 | 'override the global and local namespace. If only one ' |
| 4250 | 'namespace is\n' |
| 4251 | 'specified, it is used for both.\n' |
| 4252 | '\n' |
| 4253 | '\n' |
| 4254 | 'Exceptions\n' |
| 4255 | '==========\n' |
| 4256 | '\n' |
| 4257 | 'Exceptions are a means of breaking out of the normal flow of ' |
| 4258 | 'control\n' |
| 4259 | 'of a code block in order to handle errors or other ' |
| 4260 | 'exceptional\n' |
| 4261 | 'conditions. An exception is *raised* at the point where the ' |
| 4262 | 'error is\n' |
| 4263 | 'detected; it may be *handled* by the surrounding code block ' |
| 4264 | 'or by any\n' |
| 4265 | 'code block that directly or indirectly invoked the code block ' |
| 4266 | 'where\n' |
| 4267 | 'the error occurred.\n' |
| 4268 | '\n' |
| 4269 | 'The Python interpreter raises an exception when it detects a ' |
| 4270 | 'run-time\n' |
| 4271 | 'error (such as division by zero). A Python program can also\n' |
| 4272 | 'explicitly raise an exception with the "raise" statement. ' |
| 4273 | 'Exception\n' |
| 4274 | 'handlers are specified with the "try" ... "except" ' |
| 4275 | 'statement. The\n' |
| 4276 | '"finally" clause of such a statement can be used to specify ' |
| 4277 | 'cleanup\n' |
| 4278 | 'code which does not handle the exception, but is executed ' |
| 4279 | 'whether an\n' |
| 4280 | 'exception occurred or not in the preceding code.\n' |
| 4281 | '\n' |
| 4282 | 'Python uses the "termination" model of error handling: an ' |
| 4283 | 'exception\n' |
| 4284 | 'handler can find out what happened and continue execution at ' |
| 4285 | 'an outer\n' |
| 4286 | 'level, but it cannot repair the cause of the error and retry ' |
| 4287 | 'the\n' |
| 4288 | 'failing operation (except by re-entering the offending piece ' |
| 4289 | 'of code\n' |
| 4290 | 'from the top).\n' |
| 4291 | '\n' |
| 4292 | 'When an exception is not handled at all, the interpreter ' |
| 4293 | 'terminates\n' |
| 4294 | 'execution of the program, or returns to its interactive main ' |
| 4295 | 'loop. In\n' |
| 4296 | 'either case, it prints a stack backtrace, except when the ' |
| 4297 | 'exception is\n' |
| 4298 | '"SystemExit".\n' |
| 4299 | '\n' |
| 4300 | 'Exceptions are identified by class instances. The "except" ' |
| 4301 | 'clause is\n' |
| 4302 | 'selected depending on the class of the instance: it must ' |
| 4303 | 'reference the\n' |
| 4304 | 'class of the instance or a base class thereof. The instance ' |
| 4305 | 'can be\n' |
| 4306 | 'received by the handler and can carry additional information ' |
| 4307 | 'about the\n' |
| 4308 | 'exceptional condition.\n' |
| 4309 | '\n' |
| 4310 | 'Note: Exception messages are not part of the Python API. ' |
| 4311 | 'Their\n' |
| 4312 | ' contents may change from one version of Python to the next ' |
| 4313 | 'without\n' |
| 4314 | ' warning and should not be relied on by code which will run ' |
| 4315 | 'under\n' |
| 4316 | ' multiple versions of the interpreter.\n' |
| 4317 | '\n' |
| 4318 | 'See also the description of the "try" statement in section ' |
| 4319 | '*The try\n' |
| 4320 | 'statement* and "raise" statement in section *The raise ' |
| 4321 | 'statement*.\n' |
| 4322 | '\n' |
| 4323 | '-[ Footnotes ]-\n' |
| 4324 | '\n' |
| 4325 | '[1] This limitation occurs because the code that is executed ' |
| 4326 | 'by\n' |
| 4327 | ' these operations is not available at the time the module ' |
| 4328 | 'is\n' |
| 4329 | ' compiled.\n', |
| 4330 | 'exprlists': '\n' |
| 4331 | 'Expression lists\n' |
| 4332 | '****************\n' |
| 4333 | '\n' |
| 4334 | ' expression_list ::= expression ( "," expression )* [","]\n' |
| 4335 | '\n' |
| 4336 | 'An expression list containing at least one comma yields a ' |
| 4337 | 'tuple. The\n' |
| 4338 | 'length of the tuple is the number of expressions in the ' |
| 4339 | 'list. The\n' |
| 4340 | 'expressions are evaluated from left to right.\n' |
| 4341 | '\n' |
| 4342 | 'The trailing comma is required only to create a single tuple ' |
| 4343 | '(a.k.a. a\n' |
| 4344 | '*singleton*); it is optional in all other cases. A single ' |
| 4345 | 'expression\n' |
| 4346 | "without a trailing comma doesn't create a tuple, but rather " |
| 4347 | 'yields the\n' |
| 4348 | 'value of that expression. (To create an empty tuple, use an ' |
| 4349 | 'empty pair\n' |
| 4350 | 'of parentheses: "()".)\n', |
| 4351 | 'floating': '\n' |
| 4352 | 'Floating point literals\n' |
| 4353 | '***********************\n' |
| 4354 | '\n' |
| 4355 | 'Floating point literals are described by the following ' |
| 4356 | 'lexical\n' |
| 4357 | 'definitions:\n' |
| 4358 | '\n' |
| 4359 | ' floatnumber ::= pointfloat | exponentfloat\n' |
| 4360 | ' pointfloat ::= [intpart] fraction | intpart "."\n' |
| 4361 | ' exponentfloat ::= (intpart | pointfloat) exponent\n' |
| 4362 | ' intpart ::= digit+\n' |
| 4363 | ' fraction ::= "." digit+\n' |
| 4364 | ' exponent ::= ("e" | "E") ["+" | "-"] digit+\n' |
| 4365 | '\n' |
| 4366 | 'Note that the integer and exponent parts are always ' |
| 4367 | 'interpreted using\n' |
| 4368 | 'radix 10. For example, "077e010" is legal, and denotes the ' |
| 4369 | 'same number\n' |
| 4370 | 'as "77e10". The allowed range of floating point literals is\n' |
| 4371 | 'implementation-dependent. Some examples of floating point ' |
| 4372 | 'literals:\n' |
| 4373 | '\n' |
| 4374 | ' 3.14 10. .001 1e100 3.14e-10 0e0\n' |
| 4375 | '\n' |
| 4376 | 'Note that numeric literals do not include a sign; a phrase ' |
| 4377 | 'like "-1"\n' |
| 4378 | 'is actually an expression composed of the unary operator "-" ' |
| 4379 | 'and the\n' |
| 4380 | 'literal "1".\n', |
| 4381 | 'for': '\n' |
| 4382 | 'The "for" statement\n' |
| 4383 | '*******************\n' |
| 4384 | '\n' |
| 4385 | 'The "for" statement is used to iterate over the elements of a ' |
| 4386 | 'sequence\n' |
| 4387 | '(such as a string, tuple or list) or other iterable object:\n' |
| 4388 | '\n' |
| 4389 | ' for_stmt ::= "for" target_list "in" expression_list ":" suite\n' |
| 4390 | ' ["else" ":" suite]\n' |
| 4391 | '\n' |
| 4392 | 'The expression list is evaluated once; it should yield an iterable\n' |
| 4393 | 'object. An iterator is created for the result of the\n' |
| 4394 | '"expression_list". The suite is then executed once for each item\n' |
| 4395 | 'provided by the iterator, in the order returned by the iterator. ' |
| 4396 | 'Each\n' |
| 4397 | 'item in turn is assigned to the target list using the standard ' |
| 4398 | 'rules\n' |
| 4399 | 'for assignments (see *Assignment statements*), and then the suite ' |
| 4400 | 'is\n' |
| 4401 | 'executed. When the items are exhausted (which is immediately when ' |
| 4402 | 'the\n' |
| 4403 | 'sequence is empty or an iterator raises a "StopIteration" ' |
| 4404 | 'exception),\n' |
| 4405 | 'the suite in the "else" clause, if present, is executed, and the ' |
| 4406 | 'loop\n' |
| 4407 | 'terminates.\n' |
| 4408 | '\n' |
| 4409 | 'A "break" statement executed in the first suite terminates the ' |
| 4410 | 'loop\n' |
| 4411 | 'without executing the "else" clause\'s suite. A "continue" ' |
| 4412 | 'statement\n' |
| 4413 | 'executed in the first suite skips the rest of the suite and ' |
| 4414 | 'continues\n' |
| 4415 | 'with the next item, or with the "else" clause if there is no next\n' |
| 4416 | 'item.\n' |
| 4417 | '\n' |
| 4418 | 'The for-loop makes assignments to the variables(s) in the target ' |
| 4419 | 'list.\n' |
| 4420 | 'This overwrites all previous assignments to those variables ' |
| 4421 | 'including\n' |
| 4422 | 'those made in the suite of the for-loop:\n' |
| 4423 | '\n' |
| 4424 | ' for i in range(10):\n' |
| 4425 | ' print(i)\n' |
| 4426 | ' i = 5 # this will not affect the for-loop\n' |
| 4427 | ' # because i will be overwritten with the ' |
| 4428 | 'next\n' |
| 4429 | ' # index in the range\n' |
| 4430 | '\n' |
| 4431 | 'Names in the target list are not deleted when the loop is ' |
| 4432 | 'finished,\n' |
| 4433 | 'but if the sequence is empty, they will not have been assigned to ' |
| 4434 | 'at\n' |
| 4435 | 'all by the loop. Hint: the built-in function "range()" returns an\n' |
| 4436 | "iterator of integers suitable to emulate the effect of Pascal's " |
| 4437 | '"for i\n' |
| 4438 | ':= a to b do"; e.g., "list(range(3))" returns the list "[0, 1, ' |
| 4439 | '2]".\n' |
| 4440 | '\n' |
| 4441 | 'Note: There is a subtlety when the sequence is being modified by ' |
| 4442 | 'the\n' |
| 4443 | ' loop (this can only occur for mutable sequences, i.e. lists). ' |
| 4444 | 'An\n' |
| 4445 | ' internal counter is used to keep track of which item is used ' |
| 4446 | 'next,\n' |
| 4447 | ' and this is incremented on each iteration. When this counter ' |
| 4448 | 'has\n' |
| 4449 | ' reached the length of the sequence the loop terminates. This ' |
| 4450 | 'means\n' |
| 4451 | ' that if the suite deletes the current (or a previous) item from ' |
| 4452 | 'the\n' |
| 4453 | ' sequence, the next item will be skipped (since it gets the index ' |
| 4454 | 'of\n' |
| 4455 | ' the current item which has already been treated). Likewise, if ' |
| 4456 | 'the\n' |
| 4457 | ' suite inserts an item in the sequence before the current item, ' |
| 4458 | 'the\n' |
| 4459 | ' current item will be treated again the next time through the ' |
| 4460 | 'loop.\n' |
| 4461 | ' This can lead to nasty bugs that can be avoided by making a\n' |
| 4462 | ' temporary copy using a slice of the whole sequence, e.g.,\n' |
| 4463 | '\n' |
| 4464 | ' for x in a[:]:\n' |
| 4465 | ' if x < 0: a.remove(x)\n', |
| 4466 | 'formatstrings': '\n' |
| 4467 | 'Format String Syntax\n' |
| 4468 | '********************\n' |
| 4469 | '\n' |
| 4470 | 'The "str.format()" method and the "Formatter" class share ' |
| 4471 | 'the same\n' |
| 4472 | 'syntax for format strings (although in the case of ' |
| 4473 | '"Formatter",\n' |
| 4474 | 'subclasses can define their own format string syntax).\n' |
| 4475 | '\n' |
| 4476 | 'Format strings contain "replacement fields" surrounded by ' |
| 4477 | 'curly braces\n' |
| 4478 | '"{}". Anything that is not contained in braces is ' |
| 4479 | 'considered literal\n' |
| 4480 | 'text, which is copied unchanged to the output. If you ' |
| 4481 | 'need to include\n' |
| 4482 | 'a brace character in the literal text, it can be escaped ' |
| 4483 | 'by doubling:\n' |
| 4484 | '"{{" and "}}".\n' |
| 4485 | '\n' |
| 4486 | 'The grammar for a replacement field is as follows:\n' |
| 4487 | '\n' |
| 4488 | ' replacement_field ::= "{" [field_name] ["!" ' |
| 4489 | 'conversion] [":" format_spec] "}"\n' |
| 4490 | ' field_name ::= arg_name ("." attribute_name ' |
| 4491 | '| "[" element_index "]")*\n' |
| 4492 | ' arg_name ::= [identifier | integer]\n' |
| 4493 | ' attribute_name ::= identifier\n' |
| 4494 | ' element_index ::= integer | index_string\n' |
| 4495 | ' index_string ::= <any source character except ' |
| 4496 | '"]"> +\n' |
| 4497 | ' conversion ::= "r" | "s" | "a"\n' |
| 4498 | ' format_spec ::= <described in the next ' |
| 4499 | 'section>\n' |
| 4500 | '\n' |
| 4501 | 'In less formal terms, the replacement field can start ' |
| 4502 | 'with a\n' |
| 4503 | '*field_name* that specifies the object whose value is to ' |
| 4504 | 'be formatted\n' |
| 4505 | 'and inserted into the output instead of the replacement ' |
| 4506 | 'field. The\n' |
| 4507 | '*field_name* is optionally followed by a *conversion* ' |
| 4508 | 'field, which is\n' |
| 4509 | 'preceded by an exclamation point "\'!\'", and a ' |
| 4510 | '*format_spec*, which is\n' |
| 4511 | 'preceded by a colon "\':\'". These specify a non-default ' |
| 4512 | 'format for the\n' |
| 4513 | 'replacement value.\n' |
| 4514 | '\n' |
| 4515 | 'See also the *Format Specification Mini-Language* ' |
| 4516 | 'section.\n' |
| 4517 | '\n' |
| 4518 | 'The *field_name* itself begins with an *arg_name* that is ' |
| 4519 | 'either a\n' |
| 4520 | "number or a keyword. If it's a number, it refers to a " |
| 4521 | 'positional\n' |
| 4522 | "argument, and if it's a keyword, it refers to a named " |
| 4523 | 'keyword\n' |
| 4524 | 'argument. If the numerical arg_names in a format string ' |
| 4525 | 'are 0, 1, 2,\n' |
| 4526 | '... in sequence, they can all be omitted (not just some) ' |
| 4527 | 'and the\n' |
| 4528 | 'numbers 0, 1, 2, ... will be automatically inserted in ' |
| 4529 | 'that order.\n' |
| 4530 | 'Because *arg_name* is not quote-delimited, it is not ' |
| 4531 | 'possible to\n' |
| 4532 | 'specify arbitrary dictionary keys (e.g., the strings ' |
| 4533 | '"\'10\'" or\n' |
| 4534 | '"\':-]\'") within a format string. The *arg_name* can be ' |
| 4535 | 'followed by any\n' |
| 4536 | 'number of index or attribute expressions. An expression ' |
| 4537 | 'of the form\n' |
| 4538 | '"\'.name\'" selects the named attribute using ' |
| 4539 | '"getattr()", while an\n' |
| 4540 | 'expression of the form "\'[index]\'" does an index lookup ' |
| 4541 | 'using\n' |
| 4542 | '"__getitem__()".\n' |
| 4543 | '\n' |
| 4544 | 'Changed in version 3.1: The positional argument ' |
| 4545 | 'specifiers can be\n' |
| 4546 | 'omitted, so "\'{} {}\'" is equivalent to "\'{0} {1}\'".\n' |
| 4547 | '\n' |
| 4548 | 'Some simple format string examples:\n' |
| 4549 | '\n' |
| 4550 | ' "First, thou shalt count to {0}" # References first ' |
| 4551 | 'positional argument\n' |
| 4552 | ' "Bring me a {}" # Implicitly ' |
| 4553 | 'references the first positional argument\n' |
| 4554 | ' "From {} to {}" # Same as "From {0} ' |
| 4555 | 'to {1}"\n' |
| 4556 | ' "My quest is {name}" # References keyword ' |
| 4557 | "argument 'name'\n" |
| 4558 | ' "Weight in tons {0.weight}" # \'weight\' ' |
| 4559 | 'attribute of first positional arg\n' |
| 4560 | ' "Units destroyed: {players[0]}" # First element of ' |
| 4561 | "keyword argument 'players'.\n" |
| 4562 | '\n' |
| 4563 | 'The *conversion* field causes a type coercion before ' |
| 4564 | 'formatting.\n' |
| 4565 | 'Normally, the job of formatting a value is done by the ' |
| 4566 | '"__format__()"\n' |
| 4567 | 'method of the value itself. However, in some cases it is ' |
| 4568 | 'desirable to\n' |
| 4569 | 'force a type to be formatted as a string, overriding its ' |
| 4570 | 'own\n' |
| 4571 | 'definition of formatting. By converting the value to a ' |
| 4572 | 'string before\n' |
| 4573 | 'calling "__format__()", the normal formatting logic is ' |
| 4574 | 'bypassed.\n' |
| 4575 | '\n' |
| 4576 | 'Three conversion flags are currently supported: "\'!s\'" ' |
| 4577 | 'which calls\n' |
| 4578 | '"str()" on the value, "\'!r\'" which calls "repr()" and ' |
| 4579 | '"\'!a\'" which\n' |
| 4580 | 'calls "ascii()".\n' |
| 4581 | '\n' |
| 4582 | 'Some examples:\n' |
| 4583 | '\n' |
| 4584 | ' "Harold\'s a clever {0!s}" # Calls str() on the ' |
| 4585 | 'argument first\n' |
| 4586 | ' "Bring out the holy {name!r}" # Calls repr() on the ' |
| 4587 | 'argument first\n' |
| 4588 | ' "More {!a}" # Calls ascii() on ' |
| 4589 | 'the argument first\n' |
| 4590 | '\n' |
| 4591 | 'The *format_spec* field contains a specification of how ' |
| 4592 | 'the value\n' |
| 4593 | 'should be presented, including such details as field ' |
| 4594 | 'width, alignment,\n' |
| 4595 | 'padding, decimal precision and so on. Each value type ' |
| 4596 | 'can define its\n' |
| 4597 | 'own "formatting mini-language" or interpretation of the ' |
| 4598 | '*format_spec*.\n' |
| 4599 | '\n' |
| 4600 | 'Most built-in types support a common formatting ' |
| 4601 | 'mini-language, which\n' |
| 4602 | 'is described in the next section.\n' |
| 4603 | '\n' |
| 4604 | 'A *format_spec* field can also include nested replacement ' |
| 4605 | 'fields\n' |
| 4606 | 'within it. These nested replacement fields can contain ' |
| 4607 | 'only a field\n' |
| 4608 | 'name; conversion flags and format specifications are not ' |
| 4609 | 'allowed. The\n' |
| 4610 | 'replacement fields within the format_spec are substituted ' |
| 4611 | 'before the\n' |
| 4612 | '*format_spec* string is interpreted. This allows the ' |
| 4613 | 'formatting of a\n' |
| 4614 | 'value to be dynamically specified.\n' |
| 4615 | '\n' |
| 4616 | 'See the *Format examples* section for some examples.\n' |
| 4617 | '\n' |
| 4618 | '\n' |
| 4619 | 'Format Specification Mini-Language\n' |
| 4620 | '==================================\n' |
| 4621 | '\n' |
| 4622 | '"Format specifications" are used within replacement ' |
| 4623 | 'fields contained\n' |
| 4624 | 'within a format string to define how individual values ' |
| 4625 | 'are presented\n' |
| 4626 | '(see *Format String Syntax*). They can also be passed ' |
| 4627 | 'directly to the\n' |
| 4628 | 'built-in "format()" function. Each formattable type may ' |
| 4629 | 'define how\n' |
| 4630 | 'the format specification is to be interpreted.\n' |
| 4631 | '\n' |
| 4632 | 'Most built-in types implement the following options for ' |
| 4633 | 'format\n' |
| 4634 | 'specifications, although some of the formatting options ' |
| 4635 | 'are only\n' |
| 4636 | 'supported by the numeric types.\n' |
| 4637 | '\n' |
| 4638 | 'A general convention is that an empty format string ' |
| 4639 | '("""") produces\n' |
| 4640 | 'the same result as if you had called "str()" on the ' |
| 4641 | 'value. A non-empty\n' |
| 4642 | 'format string typically modifies the result.\n' |
| 4643 | '\n' |
| 4644 | 'The general form of a *standard format specifier* is:\n' |
| 4645 | '\n' |
| 4646 | ' format_spec ::= ' |
| 4647 | '[[fill]align][sign][#][0][width][,][.precision][type]\n' |
| 4648 | ' fill ::= <any character>\n' |
| 4649 | ' align ::= "<" | ">" | "=" | "^"\n' |
| 4650 | ' sign ::= "+" | "-" | " "\n' |
| 4651 | ' width ::= integer\n' |
| 4652 | ' precision ::= integer\n' |
| 4653 | ' type ::= "b" | "c" | "d" | "e" | "E" | "f" | ' |
| 4654 | '"F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"\n' |
| 4655 | '\n' |
| 4656 | 'If a valid *align* value is specified, it can be preceded ' |
| 4657 | 'by a *fill*\n' |
| 4658 | 'character that can be any character and defaults to a ' |
| 4659 | 'space if\n' |
| 4660 | 'omitted. Note that it is not possible to use "{" and "}" ' |
| 4661 | 'as *fill*\n' |
| 4662 | 'char while using the "str.format()" method; this ' |
| 4663 | 'limitation however\n' |
| 4664 | 'doesn\'t affect the "format()" function.\n' |
| 4665 | '\n' |
| 4666 | 'The meaning of the various alignment options is as ' |
| 4667 | 'follows:\n' |
| 4668 | '\n' |
| 4669 | ' ' |
| 4670 | '+-----------+------------------------------------------------------------+\n' |
| 4671 | ' | Option | ' |
| 4672 | 'Meaning ' |
| 4673 | '|\n' |
| 4674 | ' ' |
| 4675 | '+===========+============================================================+\n' |
| 4676 | ' | "\'<\'" | Forces the field to be left-aligned ' |
| 4677 | 'within the available |\n' |
| 4678 | ' | | space (this is the default for most ' |
| 4679 | 'objects). |\n' |
| 4680 | ' ' |
| 4681 | '+-----------+------------------------------------------------------------+\n' |
| 4682 | ' | "\'>\'" | Forces the field to be right-aligned ' |
| 4683 | 'within the available |\n' |
| 4684 | ' | | space (this is the default for ' |
| 4685 | 'numbers). |\n' |
| 4686 | ' ' |
| 4687 | '+-----------+------------------------------------------------------------+\n' |
| 4688 | ' | "\'=\'" | Forces the padding to be placed after ' |
| 4689 | 'the sign (if any) |\n' |
| 4690 | ' | | but before the digits. This is used for ' |
| 4691 | 'printing fields |\n' |
| 4692 | " | | in the form '+000000120'. This alignment " |
| 4693 | 'option is only |\n' |
| 4694 | ' | | valid for numeric ' |
| 4695 | 'types. |\n' |
| 4696 | ' ' |
| 4697 | '+-----------+------------------------------------------------------------+\n' |
| 4698 | ' | "\'^\'" | Forces the field to be centered within ' |
| 4699 | 'the available |\n' |
| 4700 | ' | | ' |
| 4701 | 'space. ' |
| 4702 | '|\n' |
| 4703 | ' ' |
| 4704 | '+-----------+------------------------------------------------------------+\n' |
| 4705 | '\n' |
| 4706 | 'Note that unless a minimum field width is defined, the ' |
| 4707 | 'field width\n' |
| 4708 | 'will always be the same size as the data to fill it, so ' |
| 4709 | 'that the\n' |
| 4710 | 'alignment option has no meaning in this case.\n' |
| 4711 | '\n' |
| 4712 | 'The *sign* option is only valid for number types, and can ' |
| 4713 | 'be one of\n' |
| 4714 | 'the following:\n' |
| 4715 | '\n' |
| 4716 | ' ' |
| 4717 | '+-----------+------------------------------------------------------------+\n' |
| 4718 | ' | Option | ' |
| 4719 | 'Meaning ' |
| 4720 | '|\n' |
| 4721 | ' ' |
| 4722 | '+===========+============================================================+\n' |
| 4723 | ' | "\'+\'" | indicates that a sign should be used ' |
| 4724 | 'for both positive as |\n' |
| 4725 | ' | | well as negative ' |
| 4726 | 'numbers. |\n' |
| 4727 | ' ' |
| 4728 | '+-----------+------------------------------------------------------------+\n' |
| 4729 | ' | "\'-\'" | indicates that a sign should be used ' |
| 4730 | 'only for negative |\n' |
| 4731 | ' | | numbers (this is the default ' |
| 4732 | 'behavior). |\n' |
| 4733 | ' ' |
| 4734 | '+-----------+------------------------------------------------------------+\n' |
| 4735 | ' | space | indicates that a leading space should be ' |
| 4736 | 'used on positive |\n' |
| 4737 | ' | | numbers, and a minus sign on negative ' |
| 4738 | 'numbers. |\n' |
| 4739 | ' ' |
| 4740 | '+-----------+------------------------------------------------------------+\n' |
| 4741 | '\n' |
| 4742 | 'The "\'#\'" option causes the "alternate form" to be used ' |
| 4743 | 'for the\n' |
| 4744 | 'conversion. The alternate form is defined differently ' |
| 4745 | 'for different\n' |
| 4746 | 'types. This option is only valid for integer, float, ' |
| 4747 | 'complex and\n' |
| 4748 | 'Decimal types. For integers, when binary, octal, or ' |
| 4749 | 'hexadecimal output\n' |
| 4750 | 'is used, this option adds the prefix respective "\'0b\'", ' |
| 4751 | '"\'0o\'", or\n' |
| 4752 | '"\'0x\'" to the output value. For floats, complex and ' |
| 4753 | 'Decimal the\n' |
| 4754 | 'alternate form causes the result of the conversion to ' |
| 4755 | 'always contain a\n' |
| 4756 | 'decimal-point character, even if no digits follow it. ' |
| 4757 | 'Normally, a\n' |
| 4758 | 'decimal-point character appears in the result of these ' |
| 4759 | 'conversions\n' |
| 4760 | 'only if a digit follows it. In addition, for "\'g\'" and ' |
| 4761 | '"\'G\'"\n' |
| 4762 | 'conversions, trailing zeros are not removed from the ' |
| 4763 | 'result.\n' |
| 4764 | '\n' |
| 4765 | 'The "\',\'" option signals the use of a comma for a ' |
| 4766 | 'thousands separator.\n' |
| 4767 | 'For a locale aware separator, use the "\'n\'" integer ' |
| 4768 | 'presentation type\n' |
| 4769 | 'instead.\n' |
| 4770 | '\n' |
| 4771 | 'Changed in version 3.1: Added the "\',\'" option (see ' |
| 4772 | 'also **PEP 378**).\n' |
| 4773 | '\n' |
| 4774 | '*width* is a decimal integer defining the minimum field ' |
| 4775 | 'width. If not\n' |
| 4776 | 'specified, then the field width will be determined by the ' |
| 4777 | 'content.\n' |
| 4778 | '\n' |
| 4779 | 'Preceding the *width* field by a zero ("\'0\'") character ' |
| 4780 | 'enables sign-\n' |
| 4781 | 'aware zero-padding for numeric types. This is equivalent ' |
| 4782 | 'to a *fill*\n' |
| 4783 | 'character of "\'0\'" with an *alignment* type of ' |
| 4784 | '"\'=\'".\n' |
| 4785 | '\n' |
| 4786 | 'The *precision* is a decimal number indicating how many ' |
| 4787 | 'digits should\n' |
| 4788 | 'be displayed after the decimal point for a floating point ' |
| 4789 | 'value\n' |
| 4790 | 'formatted with "\'f\'" and "\'F\'", or before and after ' |
| 4791 | 'the decimal point\n' |
| 4792 | 'for a floating point value formatted with "\'g\'" or ' |
| 4793 | '"\'G\'". For non-\n' |
| 4794 | 'number types the field indicates the maximum field size - ' |
| 4795 | 'in other\n' |
| 4796 | 'words, how many characters will be used from the field ' |
| 4797 | 'content. The\n' |
| 4798 | '*precision* is not allowed for integer values.\n' |
| 4799 | '\n' |
| 4800 | 'Finally, the *type* determines how the data should be ' |
| 4801 | 'presented.\n' |
| 4802 | '\n' |
| 4803 | 'The available string presentation types are:\n' |
| 4804 | '\n' |
| 4805 | ' ' |
| 4806 | '+-----------+------------------------------------------------------------+\n' |
| 4807 | ' | Type | ' |
| 4808 | 'Meaning ' |
| 4809 | '|\n' |
| 4810 | ' ' |
| 4811 | '+===========+============================================================+\n' |
| 4812 | ' | "\'s\'" | String format. This is the default ' |
| 4813 | 'type for strings and |\n' |
| 4814 | ' | | may be ' |
| 4815 | 'omitted. |\n' |
| 4816 | ' ' |
| 4817 | '+-----------+------------------------------------------------------------+\n' |
| 4818 | ' | None | The same as ' |
| 4819 | '"\'s\'". |\n' |
| 4820 | ' ' |
| 4821 | '+-----------+------------------------------------------------------------+\n' |
| 4822 | '\n' |
| 4823 | 'The available integer presentation types are:\n' |
| 4824 | '\n' |
| 4825 | ' ' |
| 4826 | '+-----------+------------------------------------------------------------+\n' |
| 4827 | ' | Type | ' |
| 4828 | 'Meaning ' |
| 4829 | '|\n' |
| 4830 | ' ' |
| 4831 | '+===========+============================================================+\n' |
| 4832 | ' | "\'b\'" | Binary format. Outputs the number in ' |
| 4833 | 'base 2. |\n' |
| 4834 | ' ' |
| 4835 | '+-----------+------------------------------------------------------------+\n' |
| 4836 | ' | "\'c\'" | Character. Converts the integer to the ' |
| 4837 | 'corresponding |\n' |
| 4838 | ' | | unicode character before ' |
| 4839 | 'printing. |\n' |
| 4840 | ' ' |
| 4841 | '+-----------+------------------------------------------------------------+\n' |
| 4842 | ' | "\'d\'" | Decimal Integer. Outputs the number in ' |
| 4843 | 'base 10. |\n' |
| 4844 | ' ' |
| 4845 | '+-----------+------------------------------------------------------------+\n' |
| 4846 | ' | "\'o\'" | Octal format. Outputs the number in ' |
| 4847 | 'base 8. |\n' |
| 4848 | ' ' |
| 4849 | '+-----------+------------------------------------------------------------+\n' |
| 4850 | ' | "\'x\'" | Hex format. Outputs the number in base ' |
| 4851 | '16, using lower- |\n' |
| 4852 | ' | | case letters for the digits above ' |
| 4853 | '9. |\n' |
| 4854 | ' ' |
| 4855 | '+-----------+------------------------------------------------------------+\n' |
| 4856 | ' | "\'X\'" | Hex format. Outputs the number in base ' |
| 4857 | '16, using upper- |\n' |
| 4858 | ' | | case letters for the digits above ' |
| 4859 | '9. |\n' |
| 4860 | ' ' |
| 4861 | '+-----------+------------------------------------------------------------+\n' |
| 4862 | ' | "\'n\'" | Number. This is the same as "\'d\'", ' |
| 4863 | 'except that it uses the |\n' |
| 4864 | ' | | current locale setting to insert the ' |
| 4865 | 'appropriate number |\n' |
| 4866 | ' | | separator ' |
| 4867 | 'characters. |\n' |
| 4868 | ' ' |
| 4869 | '+-----------+------------------------------------------------------------+\n' |
| 4870 | ' | None | The same as ' |
| 4871 | '"\'d\'". |\n' |
| 4872 | ' ' |
| 4873 | '+-----------+------------------------------------------------------------+\n' |
| 4874 | '\n' |
| 4875 | 'In addition to the above presentation types, integers can ' |
| 4876 | 'be formatted\n' |
| 4877 | 'with the floating point presentation types listed below ' |
| 4878 | '(except "\'n\'"\n' |
| 4879 | 'and None). When doing so, "float()" is used to convert ' |
| 4880 | 'the integer to\n' |
| 4881 | 'a floating point number before formatting.\n' |
| 4882 | '\n' |
| 4883 | 'The available presentation types for floating point and ' |
| 4884 | 'decimal values\n' |
| 4885 | 'are:\n' |
| 4886 | '\n' |
| 4887 | ' ' |
| 4888 | '+-----------+------------------------------------------------------------+\n' |
| 4889 | ' | Type | ' |
| 4890 | 'Meaning ' |
| 4891 | '|\n' |
| 4892 | ' ' |
| 4893 | '+===========+============================================================+\n' |
| 4894 | ' | "\'e\'" | Exponent notation. Prints the number ' |
| 4895 | 'in scientific |\n' |
| 4896 | " | | notation using the letter 'e' to " |
| 4897 | 'indicate the exponent. |\n' |
| 4898 | ' | | The default precision is ' |
| 4899 | '"6". |\n' |
| 4900 | ' ' |
| 4901 | '+-----------+------------------------------------------------------------+\n' |
| 4902 | ' | "\'E\'" | Exponent notation. Same as "\'e\'" ' |
| 4903 | 'except it uses an upper |\n' |
| 4904 | " | | case 'E' as the separator " |
| 4905 | 'character. |\n' |
| 4906 | ' ' |
| 4907 | '+-----------+------------------------------------------------------------+\n' |
| 4908 | ' | "\'f\'" | Fixed point. Displays the number as a ' |
| 4909 | 'fixed-point number. |\n' |
| 4910 | ' | | The default precision is ' |
| 4911 | '"6". |\n' |
| 4912 | ' ' |
| 4913 | '+-----------+------------------------------------------------------------+\n' |
| 4914 | ' | "\'F\'" | Fixed point. Same as "\'f\'", but ' |
| 4915 | 'converts "nan" to "NAN" |\n' |
| 4916 | ' | | and "inf" to ' |
| 4917 | '"INF". |\n' |
| 4918 | ' ' |
| 4919 | '+-----------+------------------------------------------------------------+\n' |
| 4920 | ' | "\'g\'" | General format. For a given precision ' |
| 4921 | '"p >= 1", this |\n' |
| 4922 | ' | | rounds the number to "p" significant ' |
| 4923 | 'digits and then |\n' |
| 4924 | ' | | formats the result in either fixed-point ' |
| 4925 | 'format or in |\n' |
| 4926 | ' | | scientific notation, depending on its ' |
| 4927 | 'magnitude. The |\n' |
| 4928 | ' | | precise rules are as follows: suppose ' |
| 4929 | 'that the result |\n' |
| 4930 | ' | | formatted with presentation type "\'e\'" ' |
| 4931 | 'and precision "p-1" |\n' |
| 4932 | ' | | would have exponent "exp". Then if "-4 ' |
| 4933 | '<= exp < p", the |\n' |
| 4934 | ' | | number is formatted with presentation ' |
| 4935 | 'type "\'f\'" and |\n' |
| 4936 | ' | | precision "p-1-exp". Otherwise, the ' |
| 4937 | 'number is formatted |\n' |
| 4938 | ' | | with presentation type "\'e\'" and ' |
| 4939 | 'precision "p-1". In both |\n' |
| 4940 | ' | | cases insignificant trailing zeros are ' |
| 4941 | 'removed from the |\n' |
| 4942 | ' | | significand, and the decimal point is ' |
| 4943 | 'also removed if |\n' |
| 4944 | ' | | there are no remaining digits following ' |
| 4945 | 'it. Positive and |\n' |
| 4946 | ' | | negative infinity, positive and negative ' |
| 4947 | 'zero, and nans, |\n' |
| 4948 | ' | | are formatted as "inf", "-inf", "0", ' |
| 4949 | '"-0" and "nan" |\n' |
| 4950 | ' | | respectively, regardless of the ' |
| 4951 | 'precision. A precision of |\n' |
| 4952 | ' | | "0" is treated as equivalent to a ' |
| 4953 | 'precision of "1". The |\n' |
| 4954 | ' | | default precision is ' |
| 4955 | '"6". |\n' |
| 4956 | ' ' |
| 4957 | '+-----------+------------------------------------------------------------+\n' |
| 4958 | ' | "\'G\'" | General format. Same as "\'g\'" except ' |
| 4959 | 'switches to "\'E\'" if |\n' |
| 4960 | ' | | the number gets too large. The ' |
| 4961 | 'representations of infinity |\n' |
| 4962 | ' | | and NaN are uppercased, ' |
| 4963 | 'too. |\n' |
| 4964 | ' ' |
| 4965 | '+-----------+------------------------------------------------------------+\n' |
| 4966 | ' | "\'n\'" | Number. This is the same as "\'g\'", ' |
| 4967 | 'except that it uses the |\n' |
| 4968 | ' | | current locale setting to insert the ' |
| 4969 | 'appropriate number |\n' |
| 4970 | ' | | separator ' |
| 4971 | 'characters. |\n' |
| 4972 | ' ' |
| 4973 | '+-----------+------------------------------------------------------------+\n' |
| 4974 | ' | "\'%\'" | Percentage. Multiplies the number by ' |
| 4975 | '100 and displays in |\n' |
| 4976 | ' | | fixed ("\'f\'") format, followed by a ' |
| 4977 | 'percent sign. |\n' |
| 4978 | ' ' |
| 4979 | '+-----------+------------------------------------------------------------+\n' |
| 4980 | ' | None | Similar to "\'g\'", except that ' |
| 4981 | 'fixed-point notation, when |\n' |
| 4982 | ' | | used, has at least one digit past the ' |
| 4983 | 'decimal point. The |\n' |
| 4984 | ' | | default precision is as high as needed ' |
| 4985 | 'to represent the |\n' |
| 4986 | ' | | particular value. The overall effect is ' |
| 4987 | 'to match the |\n' |
| 4988 | ' | | output of "str()" as altered by the ' |
| 4989 | 'other format |\n' |
| 4990 | ' | | ' |
| 4991 | 'modifiers. ' |
| 4992 | '|\n' |
| 4993 | ' ' |
| 4994 | '+-----------+------------------------------------------------------------+\n' |
| 4995 | '\n' |
| 4996 | '\n' |
| 4997 | 'Format examples\n' |
| 4998 | '===============\n' |
| 4999 | '\n' |
| 5000 | 'This section contains examples of the new format syntax ' |
| 5001 | 'and comparison\n' |
| 5002 | 'with the old "%"-formatting.\n' |
| 5003 | '\n' |
| 5004 | 'In most of the cases the syntax is similar to the old ' |
| 5005 | '"%"-formatting,\n' |
| 5006 | 'with the addition of the "{}" and with ":" used instead ' |
| 5007 | 'of "%". For\n' |
| 5008 | 'example, "\'%03.2f\'" can be translated to ' |
| 5009 | '"\'{:03.2f}\'".\n' |
| 5010 | '\n' |
| 5011 | 'The new format syntax also supports new and different ' |
| 5012 | 'options, shown\n' |
| 5013 | 'in the follow examples.\n' |
| 5014 | '\n' |
| 5015 | 'Accessing arguments by position:\n' |
| 5016 | '\n' |
| 5017 | " >>> '{0}, {1}, {2}'.format('a', 'b', 'c')\n" |
| 5018 | " 'a, b, c'\n" |
| 5019 | " >>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only\n" |
| 5020 | " 'a, b, c'\n" |
| 5021 | " >>> '{2}, {1}, {0}'.format('a', 'b', 'c')\n" |
| 5022 | " 'c, b, a'\n" |
| 5023 | " >>> '{2}, {1}, {0}'.format(*'abc') # unpacking " |
| 5024 | 'argument sequence\n' |
| 5025 | " 'c, b, a'\n" |
| 5026 | " >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' " |
| 5027 | 'indices can be repeated\n' |
| 5028 | " 'abracadabra'\n" |
| 5029 | '\n' |
| 5030 | 'Accessing arguments by name:\n' |
| 5031 | '\n' |
| 5032 | " >>> 'Coordinates: {latitude}, " |
| 5033 | "{longitude}'.format(latitude='37.24N', " |
| 5034 | "longitude='-115.81W')\n" |
| 5035 | " 'Coordinates: 37.24N, -115.81W'\n" |
| 5036 | " >>> coord = {'latitude': '37.24N', 'longitude': " |
| 5037 | "'-115.81W'}\n" |
| 5038 | " >>> 'Coordinates: {latitude}, " |
| 5039 | "{longitude}'.format(**coord)\n" |
| 5040 | " 'Coordinates: 37.24N, -115.81W'\n" |
| 5041 | '\n' |
| 5042 | "Accessing arguments' attributes:\n" |
| 5043 | '\n' |
| 5044 | ' >>> c = 3-5j\n' |
| 5045 | " >>> ('The complex number {0} is formed from the real " |
| 5046 | "part {0.real} '\n" |
| 5047 | " ... 'and the imaginary part {0.imag}.').format(c)\n" |
| 5048 | " 'The complex number (3-5j) is formed from the real " |
| 5049 | "part 3.0 and the imaginary part -5.0.'\n" |
| 5050 | ' >>> class Point:\n' |
| 5051 | ' ... def __init__(self, x, y):\n' |
| 5052 | ' ... self.x, self.y = x, y\n' |
| 5053 | ' ... def __str__(self):\n' |
| 5054 | " ... return 'Point({self.x}, " |
| 5055 | "{self.y})'.format(self=self)\n" |
| 5056 | ' ...\n' |
| 5057 | ' >>> str(Point(4, 2))\n' |
| 5058 | " 'Point(4, 2)'\n" |
| 5059 | '\n' |
| 5060 | "Accessing arguments' items:\n" |
| 5061 | '\n' |
| 5062 | ' >>> coord = (3, 5)\n' |
| 5063 | " >>> 'X: {0[0]}; Y: {0[1]}'.format(coord)\n" |
| 5064 | " 'X: 3; Y: 5'\n" |
| 5065 | '\n' |
| 5066 | 'Replacing "%s" and "%r":\n' |
| 5067 | '\n' |
| 5068 | ' >>> "repr() shows quotes: {!r}; str() doesn\'t: ' |
| 5069 | '{!s}".format(\'test1\', \'test2\')\n' |
| 5070 | ' "repr() shows quotes: \'test1\'; str() doesn\'t: ' |
| 5071 | 'test2"\n' |
| 5072 | '\n' |
| 5073 | 'Aligning the text and specifying a width:\n' |
| 5074 | '\n' |
| 5075 | " >>> '{:<30}'.format('left aligned')\n" |
| 5076 | " 'left aligned '\n" |
| 5077 | " >>> '{:>30}'.format('right aligned')\n" |
| 5078 | " ' right aligned'\n" |
| 5079 | " >>> '{:^30}'.format('centered')\n" |
| 5080 | " ' centered '\n" |
| 5081 | " >>> '{:*^30}'.format('centered') # use '*' as a fill " |
| 5082 | 'char\n' |
| 5083 | " '***********centered***********'\n" |
| 5084 | '\n' |
| 5085 | 'Replacing "%+f", "%-f", and "% f" and specifying a sign:\n' |
| 5086 | '\n' |
| 5087 | " >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it " |
| 5088 | 'always\n' |
| 5089 | " '+3.140000; -3.140000'\n" |
| 5090 | " >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space " |
| 5091 | 'for positive numbers\n' |
| 5092 | " ' 3.140000; -3.140000'\n" |
| 5093 | " >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only " |
| 5094 | "the minus -- same as '{:f}; {:f}'\n" |
| 5095 | " '3.140000; -3.140000'\n" |
| 5096 | '\n' |
| 5097 | 'Replacing "%x" and "%o" and converting the value to ' |
| 5098 | 'different bases:\n' |
| 5099 | '\n' |
| 5100 | ' >>> # format also supports binary numbers\n' |
| 5101 | ' >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: ' |
| 5102 | '{0:b}".format(42)\n' |
| 5103 | " 'int: 42; hex: 2a; oct: 52; bin: 101010'\n" |
| 5104 | ' >>> # with 0x, 0o, or 0b as prefix:\n' |
| 5105 | ' >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: ' |
| 5106 | '{0:#b}".format(42)\n' |
| 5107 | " 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'\n" |
| 5108 | '\n' |
| 5109 | 'Using the comma as a thousands separator:\n' |
| 5110 | '\n' |
| 5111 | " >>> '{:,}'.format(1234567890)\n" |
| 5112 | " '1,234,567,890'\n" |
| 5113 | '\n' |
| 5114 | 'Expressing a percentage:\n' |
| 5115 | '\n' |
| 5116 | ' >>> points = 19\n' |
| 5117 | ' >>> total = 22\n' |
| 5118 | " >>> 'Correct answers: {:.2%}'.format(points/total)\n" |
| 5119 | " 'Correct answers: 86.36%'\n" |
| 5120 | '\n' |
| 5121 | 'Using type-specific formatting:\n' |
| 5122 | '\n' |
| 5123 | ' >>> import datetime\n' |
| 5124 | ' >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)\n' |
| 5125 | " >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)\n" |
| 5126 | " '2010-07-04 12:15:58'\n" |
| 5127 | '\n' |
| 5128 | 'Nesting arguments and more complex examples:\n' |
| 5129 | '\n' |
| 5130 | " >>> for align, text in zip('<^>', ['left', 'center', " |
| 5131 | "'right']):\n" |
| 5132 | " ... '{0:{fill}{align}16}'.format(text, fill=align, " |
| 5133 | 'align=align)\n' |
| 5134 | ' ...\n' |
| 5135 | " 'left<<<<<<<<<<<<'\n" |
| 5136 | " '^^^^^center^^^^^'\n" |
| 5137 | " '>>>>>>>>>>>right'\n" |
| 5138 | ' >>>\n' |
| 5139 | ' >>> octets = [192, 168, 0, 1]\n' |
| 5140 | " >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)\n" |
| 5141 | " 'C0A80001'\n" |
| 5142 | ' >>> int(_, 16)\n' |
| 5143 | ' 3232235521\n' |
| 5144 | ' >>>\n' |
| 5145 | ' >>> width = 5\n' |
| 5146 | ' >>> for num in range(5,12): #doctest: ' |
| 5147 | '+NORMALIZE_WHITESPACE\n' |
| 5148 | " ... for base in 'dXob':\n" |
| 5149 | " ... print('{0:{width}{base}}'.format(num, " |
| 5150 | "base=base, width=width), end=' ')\n" |
| 5151 | ' ... print()\n' |
| 5152 | ' ...\n' |
| 5153 | ' 5 5 5 101\n' |
| 5154 | ' 6 6 6 110\n' |
| 5155 | ' 7 7 7 111\n' |
| 5156 | ' 8 8 10 1000\n' |
| 5157 | ' 9 9 11 1001\n' |
| 5158 | ' 10 A 12 1010\n' |
| 5159 | ' 11 B 13 1011\n', |
| 5160 | 'function': '\n' |
| 5161 | 'Function definitions\n' |
| 5162 | '********************\n' |
| 5163 | '\n' |
| 5164 | 'A function definition defines a user-defined function object ' |
| 5165 | '(see\n' |
| 5166 | 'section *The standard type hierarchy*):\n' |
| 5167 | '\n' |
| 5168 | ' funcdef ::= [decorators] "def" funcname "(" ' |
| 5169 | '[parameter_list] ")" ["->" expression] ":" suite\n' |
| 5170 | ' decorators ::= decorator+\n' |
| 5171 | ' decorator ::= "@" dotted_name ["(" [parameter_list ' |
| 5172 | '[","]] ")"] NEWLINE\n' |
| 5173 | ' dotted_name ::= identifier ("." identifier)*\n' |
| 5174 | ' parameter_list ::= (defparameter ",")*\n' |
| 5175 | ' | "*" [parameter] ("," defparameter)* ' |
| 5176 | '["," "**" parameter]\n' |
| 5177 | ' | "**" parameter\n' |
| 5178 | ' | defparameter [","] )\n' |
| 5179 | ' parameter ::= identifier [":" expression]\n' |
| 5180 | ' defparameter ::= parameter ["=" expression]\n' |
| 5181 | ' funcname ::= identifier\n' |
| 5182 | '\n' |
| 5183 | 'A function definition is an executable statement. Its ' |
| 5184 | 'execution binds\n' |
| 5185 | 'the function name in the current local namespace to a function ' |
| 5186 | 'object\n' |
| 5187 | '(a wrapper around the executable code for the function). ' |
| 5188 | 'This\n' |
| 5189 | 'function object contains a reference to the current global ' |
| 5190 | 'namespace\n' |
| 5191 | 'as the global namespace to be used when the function is ' |
| 5192 | 'called.\n' |
| 5193 | '\n' |
| 5194 | 'The function definition does not execute the function body; ' |
| 5195 | 'this gets\n' |
| 5196 | 'executed only when the function is called. [3]\n' |
| 5197 | '\n' |
| 5198 | 'A function definition may be wrapped by one or more ' |
| 5199 | '*decorator*\n' |
| 5200 | 'expressions. Decorator expressions are evaluated when the ' |
| 5201 | 'function is\n' |
| 5202 | 'defined, in the scope that contains the function definition. ' |
| 5203 | 'The\n' |
| 5204 | 'result must be a callable, which is invoked with the function ' |
| 5205 | 'object\n' |
| 5206 | 'as the only argument. The returned value is bound to the ' |
| 5207 | 'function name\n' |
| 5208 | 'instead of the function object. Multiple decorators are ' |
| 5209 | 'applied in\n' |
| 5210 | 'nested fashion. For example, the following code\n' |
| 5211 | '\n' |
| 5212 | ' @f1(arg)\n' |
| 5213 | ' @f2\n' |
| 5214 | ' def func(): pass\n' |
| 5215 | '\n' |
| 5216 | 'is equivalent to\n' |
| 5217 | '\n' |
| 5218 | ' def func(): pass\n' |
| 5219 | ' func = f1(arg)(f2(func))\n' |
| 5220 | '\n' |
| 5221 | 'When one or more *parameters* have the form *parameter* "="\n' |
| 5222 | '*expression*, the function is said to have "default parameter ' |
| 5223 | 'values."\n' |
| 5224 | 'For a parameter with a default value, the corresponding ' |
| 5225 | '*argument* may\n' |
| 5226 | "be omitted from a call, in which case the parameter's default " |
| 5227 | 'value is\n' |
| 5228 | 'substituted. If a parameter has a default value, all ' |
| 5229 | 'following\n' |
| 5230 | 'parameters up until the ""*"" must also have a default value ' |
| 5231 | '--- this\n' |
| 5232 | 'is a syntactic restriction that is not expressed by the ' |
| 5233 | 'grammar.\n' |
| 5234 | '\n' |
| 5235 | '**Default parameter values are evaluated from left to right ' |
| 5236 | 'when the\n' |
| 5237 | 'function definition is executed.** This means that the ' |
| 5238 | 'expression is\n' |
| 5239 | 'evaluated once, when the function is defined, and that the ' |
| 5240 | 'same "pre-\n' |
| 5241 | 'computed" value is used for each call. This is especially ' |
| 5242 | 'important\n' |
| 5243 | 'to understand when a default parameter is a mutable object, ' |
| 5244 | 'such as a\n' |
| 5245 | 'list or a dictionary: if the function modifies the object ' |
| 5246 | '(e.g. by\n' |
| 5247 | 'appending an item to a list), the default value is in effect ' |
| 5248 | 'modified.\n' |
| 5249 | 'This is generally not what was intended. A way around this is ' |
| 5250 | 'to use\n' |
| 5251 | '"None" as the default, and explicitly test for it in the body ' |
| 5252 | 'of the\n' |
| 5253 | 'function, e.g.:\n' |
| 5254 | '\n' |
| 5255 | ' def whats_on_the_telly(penguin=None):\n' |
| 5256 | ' if penguin is None:\n' |
| 5257 | ' penguin = []\n' |
| 5258 | ' penguin.append("property of the zoo")\n' |
| 5259 | ' return penguin\n' |
| 5260 | '\n' |
| 5261 | 'Function call semantics are described in more detail in ' |
| 5262 | 'section\n' |
| 5263 | '*Calls*. A function call always assigns values to all ' |
| 5264 | 'parameters\n' |
| 5265 | 'mentioned in the parameter list, either from position ' |
| 5266 | 'arguments, from\n' |
| 5267 | 'keyword arguments, or from default values. If the form\n' |
| 5268 | '""*identifier"" is present, it is initialized to a tuple ' |
| 5269 | 'receiving any\n' |
| 5270 | 'excess positional parameters, defaulting to the empty tuple. ' |
| 5271 | 'If the\n' |
| 5272 | 'form ""**identifier"" is present, it is initialized to a new\n' |
| 5273 | 'dictionary receiving any excess keyword arguments, defaulting ' |
| 5274 | 'to a new\n' |
| 5275 | 'empty dictionary. Parameters after ""*"" or ""*identifier"" ' |
| 5276 | 'are\n' |
| 5277 | 'keyword-only parameters and may only be passed used keyword ' |
| 5278 | 'arguments.\n' |
| 5279 | '\n' |
| 5280 | 'Parameters may have annotations of the form "": expression"" ' |
| 5281 | 'following\n' |
| 5282 | 'the parameter name. Any parameter may have an annotation even ' |
| 5283 | 'those\n' |
| 5284 | 'of the form "*identifier" or "**identifier". Functions may ' |
| 5285 | 'have\n' |
| 5286 | '"return" annotation of the form ""-> expression"" after the ' |
| 5287 | 'parameter\n' |
| 5288 | 'list. These annotations can be any valid Python expression ' |
| 5289 | 'and are\n' |
| 5290 | 'evaluated when the function definition is executed. ' |
| 5291 | 'Annotations may\n' |
| 5292 | 'be evaluated in a different order than they appear in the ' |
| 5293 | 'source code.\n' |
| 5294 | 'The presence of annotations does not change the semantics of ' |
| 5295 | 'a\n' |
| 5296 | 'function. The annotation values are available as values of a\n' |
| 5297 | "dictionary keyed by the parameters' names in the " |
| 5298 | '"__annotations__"\n' |
| 5299 | 'attribute of the function object.\n' |
| 5300 | '\n' |
| 5301 | 'It is also possible to create anonymous functions (functions ' |
| 5302 | 'not bound\n' |
| 5303 | 'to a name), for immediate use in expressions. This uses ' |
| 5304 | 'lambda\n' |
| 5305 | 'expressions, described in section *Lambdas*. Note that the ' |
| 5306 | 'lambda\n' |
| 5307 | 'expression is merely a shorthand for a simplified function ' |
| 5308 | 'definition;\n' |
| 5309 | 'a function defined in a ""def"" statement can be passed around ' |
| 5310 | 'or\n' |
| 5311 | 'assigned to another name just like a function defined by a ' |
| 5312 | 'lambda\n' |
| 5313 | 'expression. The ""def"" form is actually more powerful since ' |
| 5314 | 'it\n' |
| 5315 | 'allows the execution of multiple statements and annotations.\n' |
| 5316 | '\n' |
| 5317 | "**Programmer's note:** Functions are first-class objects. A " |
| 5318 | '""def""\n' |
| 5319 | 'statement executed inside a function definition defines a ' |
| 5320 | 'local\n' |
| 5321 | 'function that can be returned or passed around. Free ' |
| 5322 | 'variables used\n' |
| 5323 | 'in the nested function can access the local variables of the ' |
| 5324 | 'function\n' |
| 5325 | 'containing the def. See section *Naming and binding* for ' |
| 5326 | 'details.\n' |
| 5327 | '\n' |
| 5328 | 'See also: **PEP 3107** - Function Annotations\n' |
| 5329 | '\n' |
| 5330 | ' The original specification for function annotations.\n', |
| 5331 | 'global': '\n' |
| 5332 | 'The "global" statement\n' |
| 5333 | '**********************\n' |
| 5334 | '\n' |
| 5335 | ' global_stmt ::= "global" identifier ("," identifier)*\n' |
| 5336 | '\n' |
| 5337 | 'The "global" statement is a declaration which holds for the ' |
| 5338 | 'entire\n' |
| 5339 | 'current code block. It means that the listed identifiers are to ' |
| 5340 | 'be\n' |
| 5341 | 'interpreted as globals. It would be impossible to assign to a ' |
| 5342 | 'global\n' |
| 5343 | 'variable without "global", although free variables may refer to\n' |
| 5344 | 'globals without being declared global.\n' |
| 5345 | '\n' |
| 5346 | 'Names listed in a "global" statement must not be used in the ' |
| 5347 | 'same code\n' |
| 5348 | 'block textually preceding that "global" statement.\n' |
| 5349 | '\n' |
| 5350 | 'Names listed in a "global" statement must not be defined as ' |
| 5351 | 'formal\n' |
| 5352 | 'parameters or in a "for" loop control target, "class" ' |
| 5353 | 'definition,\n' |
| 5354 | 'function definition, or "import" statement.\n' |
| 5355 | '\n' |
| 5356 | '**CPython implementation detail:** The current implementation ' |
| 5357 | 'does not\n' |
| 5358 | 'enforce the two restrictions, but programs should not abuse ' |
| 5359 | 'this\n' |
| 5360 | 'freedom, as future implementations may enforce them or silently ' |
| 5361 | 'change\n' |
| 5362 | 'the meaning of the program.\n' |
| 5363 | '\n' |
| 5364 | '**Programmer\'s note:** the "global" is a directive to the ' |
| 5365 | 'parser. It\n' |
| 5366 | 'applies only to code parsed at the same time as the "global"\n' |
| 5367 | 'statement. In particular, a "global" statement contained in a ' |
| 5368 | 'string\n' |
| 5369 | 'or code object supplied to the built-in "exec()" function does ' |
| 5370 | 'not\n' |
| 5371 | 'affect the code block *containing* the function call, and code\n' |
| 5372 | 'contained in such a string is unaffected by "global" statements ' |
| 5373 | 'in the\n' |
| 5374 | 'code containing the function call. The same applies to the ' |
| 5375 | '"eval()"\n' |
| 5376 | 'and "compile()" functions.\n', |
| 5377 | 'id-classes': '\n' |
| 5378 | 'Reserved classes of identifiers\n' |
| 5379 | '*******************************\n' |
| 5380 | '\n' |
| 5381 | 'Certain classes of identifiers (besides keywords) have ' |
| 5382 | 'special\n' |
| 5383 | 'meanings. These classes are identified by the patterns of ' |
| 5384 | 'leading and\n' |
| 5385 | 'trailing underscore characters:\n' |
| 5386 | '\n' |
| 5387 | '"_*"\n' |
| 5388 | ' Not imported by "from module import *". The special ' |
| 5389 | 'identifier "_"\n' |
| 5390 | ' is used in the interactive interpreter to store the ' |
| 5391 | 'result of the\n' |
| 5392 | ' last evaluation; it is stored in the "builtins" module. ' |
| 5393 | 'When not\n' |
| 5394 | ' in interactive mode, "_" has no special meaning and is ' |
| 5395 | 'not defined.\n' |
| 5396 | ' See section *The import statement*.\n' |
| 5397 | '\n' |
| 5398 | ' Note: The name "_" is often used in conjunction with\n' |
| 5399 | ' internationalization; refer to the documentation for ' |
| 5400 | 'the\n' |
| 5401 | ' "gettext" module for more information on this ' |
| 5402 | 'convention.\n' |
| 5403 | '\n' |
| 5404 | '"__*__"\n' |
| 5405 | ' System-defined names. These names are defined by the ' |
| 5406 | 'interpreter\n' |
| 5407 | ' and its implementation (including the standard library). ' |
| 5408 | 'Current\n' |
| 5409 | ' system names are discussed in the *Special method names* ' |
| 5410 | 'section\n' |
| 5411 | ' and elsewhere. More will likely be defined in future ' |
| 5412 | 'versions of\n' |
| 5413 | ' Python. *Any* use of "__*__" names, in any context, that ' |
| 5414 | 'does not\n' |
| 5415 | ' follow explicitly documented use, is subject to breakage ' |
| 5416 | 'without\n' |
| 5417 | ' warning.\n' |
| 5418 | '\n' |
| 5419 | '"__*"\n' |
| 5420 | ' Class-private names. Names in this category, when used ' |
| 5421 | 'within the\n' |
| 5422 | ' context of a class definition, are re-written to use a ' |
| 5423 | 'mangled form\n' |
| 5424 | ' to help avoid name clashes between "private" attributes ' |
| 5425 | 'of base and\n' |
| 5426 | ' derived classes. See section *Identifiers (Names)*.\n', |
| 5427 | 'identifiers': '\n' |
| 5428 | 'Identifiers and keywords\n' |
| 5429 | '************************\n' |
| 5430 | '\n' |
| 5431 | 'Identifiers (also referred to as *names*) are described by ' |
| 5432 | 'the\n' |
| 5433 | 'following lexical definitions.\n' |
| 5434 | '\n' |
| 5435 | 'The syntax of identifiers in Python is based on the Unicode ' |
| 5436 | 'standard\n' |
| 5437 | 'annex UAX-31, with elaboration and changes as defined ' |
| 5438 | 'below; see also\n' |
| 5439 | '**PEP 3131** for further details.\n' |
| 5440 | '\n' |
| 5441 | 'Within the ASCII range (U+0001..U+007F), the valid ' |
| 5442 | 'characters for\n' |
| 5443 | 'identifiers are the same as in Python 2.x: the uppercase ' |
| 5444 | 'and lowercase\n' |
| 5445 | 'letters "A" through "Z", the underscore "_" and, except for ' |
| 5446 | 'the first\n' |
| 5447 | 'character, the digits "0" through "9".\n' |
| 5448 | '\n' |
| 5449 | 'Python 3.0 introduces additional characters from outside ' |
| 5450 | 'the ASCII\n' |
| 5451 | 'range (see **PEP 3131**). For these characters, the ' |
| 5452 | 'classification\n' |
| 5453 | 'uses the version of the Unicode Character Database as ' |
| 5454 | 'included in the\n' |
| 5455 | '"unicodedata" module.\n' |
| 5456 | '\n' |
| 5457 | 'Identifiers are unlimited in length. Case is significant.\n' |
| 5458 | '\n' |
| 5459 | ' identifier ::= xid_start xid_continue*\n' |
| 5460 | ' id_start ::= <all characters in general categories ' |
| 5461 | 'Lu, Ll, Lt, Lm, Lo, Nl, the underscore, and characters with ' |
| 5462 | 'the Other_ID_Start property>\n' |
| 5463 | ' id_continue ::= <all characters in id_start, plus ' |
| 5464 | 'characters in the categories Mn, Mc, Nd, Pc and others with ' |
| 5465 | 'the Other_ID_Continue property>\n' |
| 5466 | ' xid_start ::= <all characters in id_start whose NFKC ' |
| 5467 | 'normalization is in "id_start xid_continue*">\n' |
| 5468 | ' xid_continue ::= <all characters in id_continue whose ' |
| 5469 | 'NFKC normalization is in "id_continue*">\n' |
| 5470 | '\n' |
| 5471 | 'The Unicode category codes mentioned above stand for:\n' |
| 5472 | '\n' |
| 5473 | '* *Lu* - uppercase letters\n' |
| 5474 | '\n' |
| 5475 | '* *Ll* - lowercase letters\n' |
| 5476 | '\n' |
| 5477 | '* *Lt* - titlecase letters\n' |
| 5478 | '\n' |
| 5479 | '* *Lm* - modifier letters\n' |
| 5480 | '\n' |
| 5481 | '* *Lo* - other letters\n' |
| 5482 | '\n' |
| 5483 | '* *Nl* - letter numbers\n' |
| 5484 | '\n' |
| 5485 | '* *Mn* - nonspacing marks\n' |
| 5486 | '\n' |
| 5487 | '* *Mc* - spacing combining marks\n' |
| 5488 | '\n' |
| 5489 | '* *Nd* - decimal numbers\n' |
| 5490 | '\n' |
| 5491 | '* *Pc* - connector punctuations\n' |
| 5492 | '\n' |
| 5493 | '* *Other_ID_Start* - explicit list of characters in ' |
| 5494 | 'PropList.txt to\n' |
| 5495 | ' support backwards compatibility\n' |
| 5496 | '\n' |
| 5497 | '* *Other_ID_Continue* - likewise\n' |
| 5498 | '\n' |
| 5499 | 'All identifiers are converted into the normal form NFKC ' |
| 5500 | 'while parsing;\n' |
| 5501 | 'comparison of identifiers is based on NFKC.\n' |
| 5502 | '\n' |
| 5503 | 'A non-normative HTML file listing all valid identifier ' |
| 5504 | 'characters for\n' |
| 5505 | 'Unicode 4.1 can be found at http://www.dcl.hpi.uni-\n' |
| 5506 | 'potsdam.de/home/loewis/table-3131.html.\n' |
| 5507 | '\n' |
| 5508 | '\n' |
| 5509 | 'Keywords\n' |
| 5510 | '========\n' |
| 5511 | '\n' |
| 5512 | 'The following identifiers are used as reserved words, or ' |
| 5513 | '*keywords* of\n' |
| 5514 | 'the language, and cannot be used as ordinary identifiers. ' |
| 5515 | 'They must\n' |
| 5516 | 'be spelled exactly as written here:\n' |
| 5517 | '\n' |
| 5518 | ' False class finally is return\n' |
| 5519 | ' None continue for lambda try\n' |
| 5520 | ' True def from nonlocal while\n' |
| 5521 | ' and del global not with\n' |
| 5522 | ' as elif if or yield\n' |
| 5523 | ' assert else import pass\n' |
| 5524 | ' break except in raise\n' |
| 5525 | '\n' |
| 5526 | '\n' |
| 5527 | 'Reserved classes of identifiers\n' |
| 5528 | '===============================\n' |
| 5529 | '\n' |
| 5530 | 'Certain classes of identifiers (besides keywords) have ' |
| 5531 | 'special\n' |
| 5532 | 'meanings. These classes are identified by the patterns of ' |
| 5533 | 'leading and\n' |
| 5534 | 'trailing underscore characters:\n' |
| 5535 | '\n' |
| 5536 | '"_*"\n' |
| 5537 | ' Not imported by "from module import *". The special ' |
| 5538 | 'identifier "_"\n' |
| 5539 | ' is used in the interactive interpreter to store the ' |
| 5540 | 'result of the\n' |
| 5541 | ' last evaluation; it is stored in the "builtins" module. ' |
| 5542 | 'When not\n' |
| 5543 | ' in interactive mode, "_" has no special meaning and is ' |
| 5544 | 'not defined.\n' |
| 5545 | ' See section *The import statement*.\n' |
| 5546 | '\n' |
| 5547 | ' Note: The name "_" is often used in conjunction with\n' |
| 5548 | ' internationalization; refer to the documentation for ' |
| 5549 | 'the\n' |
| 5550 | ' "gettext" module for more information on this ' |
| 5551 | 'convention.\n' |
| 5552 | '\n' |
| 5553 | '"__*__"\n' |
| 5554 | ' System-defined names. These names are defined by the ' |
| 5555 | 'interpreter\n' |
| 5556 | ' and its implementation (including the standard ' |
| 5557 | 'library). Current\n' |
| 5558 | ' system names are discussed in the *Special method names* ' |
| 5559 | 'section\n' |
| 5560 | ' and elsewhere. More will likely be defined in future ' |
| 5561 | 'versions of\n' |
| 5562 | ' Python. *Any* use of "__*__" names, in any context, ' |
| 5563 | 'that does not\n' |
| 5564 | ' follow explicitly documented use, is subject to breakage ' |
| 5565 | 'without\n' |
| 5566 | ' warning.\n' |
| 5567 | '\n' |
| 5568 | '"__*"\n' |
| 5569 | ' Class-private names. Names in this category, when used ' |
| 5570 | 'within the\n' |
| 5571 | ' context of a class definition, are re-written to use a ' |
| 5572 | 'mangled form\n' |
| 5573 | ' to help avoid name clashes between "private" attributes ' |
| 5574 | 'of base and\n' |
| 5575 | ' derived classes. See section *Identifiers (Names)*.\n', |
| 5576 | 'if': '\n' |
| 5577 | 'The "if" statement\n' |
| 5578 | '******************\n' |
| 5579 | '\n' |
| 5580 | 'The "if" statement is used for conditional execution:\n' |
| 5581 | '\n' |
| 5582 | ' if_stmt ::= "if" expression ":" suite\n' |
| 5583 | ' ( "elif" expression ":" suite )*\n' |
| 5584 | ' ["else" ":" suite]\n' |
| 5585 | '\n' |
| 5586 | 'It selects exactly one of the suites by evaluating the expressions ' |
| 5587 | 'one\n' |
| 5588 | 'by one until one is found to be true (see section *Boolean ' |
| 5589 | 'operations*\n' |
| 5590 | 'for the definition of true and false); then that suite is executed\n' |
| 5591 | '(and no other part of the "if" statement is executed or evaluated).\n' |
| 5592 | 'If all expressions are false, the suite of the "else" clause, if\n' |
| 5593 | 'present, is executed.\n', |
| 5594 | 'imaginary': '\n' |
| 5595 | 'Imaginary literals\n' |
| 5596 | '******************\n' |
| 5597 | '\n' |
| 5598 | 'Imaginary literals are described by the following lexical ' |
| 5599 | 'definitions:\n' |
| 5600 | '\n' |
| 5601 | ' imagnumber ::= (floatnumber | intpart) ("j" | "J")\n' |
| 5602 | '\n' |
| 5603 | 'An imaginary literal yields a complex number with a real part ' |
| 5604 | 'of 0.0.\n' |
| 5605 | 'Complex numbers are represented as a pair of floating point ' |
| 5606 | 'numbers\n' |
| 5607 | 'and have the same restrictions on their range. To create a ' |
| 5608 | 'complex\n' |
| 5609 | 'number with a nonzero real part, add a floating point number ' |
| 5610 | 'to it,\n' |
| 5611 | 'e.g., "(3+4j)". Some examples of imaginary literals:\n' |
| 5612 | '\n' |
| 5613 | ' 3.14j 10.j 10j .001j 1e100j 3.14e-10j\n', |
| 5614 | 'import': '\n' |
| 5615 | 'The "import" statement\n' |
| 5616 | '**********************\n' |
| 5617 | '\n' |
| 5618 | ' import_stmt ::= "import" module ["as" name] ( "," module ' |
| 5619 | '["as" name] )*\n' |
| 5620 | ' | "from" relative_module "import" identifier ' |
| 5621 | '["as" name]\n' |
| 5622 | ' ( "," identifier ["as" name] )*\n' |
| 5623 | ' | "from" relative_module "import" "(" ' |
| 5624 | 'identifier ["as" name]\n' |
| 5625 | ' ( "," identifier ["as" name] )* [","] ")"\n' |
| 5626 | ' | "from" module "import" "*"\n' |
| 5627 | ' module ::= (identifier ".")* identifier\n' |
| 5628 | ' relative_module ::= "."* module | "."+\n' |
| 5629 | ' name ::= identifier\n' |
| 5630 | '\n' |
| 5631 | 'The basic import statement (no "from" clause) is executed in ' |
| 5632 | 'two\n' |
| 5633 | 'steps:\n' |
| 5634 | '\n' |
| 5635 | '1. find a module, loading and initializing it if necessary\n' |
| 5636 | '\n' |
| 5637 | '2. define a name or names in the local namespace for the scope\n' |
| 5638 | ' where the "import" statement occurs.\n' |
| 5639 | '\n' |
| 5640 | 'When the statement contains multiple clauses (separated by ' |
| 5641 | 'commas) the\n' |
| 5642 | 'two steps are carried out separately for each clause, just as ' |
| 5643 | 'though\n' |
| 5644 | 'the clauses had been separated out into individiual import ' |
| 5645 | 'statements.\n' |
| 5646 | '\n' |
| 5647 | 'The details of the first step, finding and loading modules are\n' |
| 5648 | 'described in greater detail in the section on the *import ' |
| 5649 | 'system*,\n' |
| 5650 | 'which also describes the various types of packages and modules ' |
| 5651 | 'that\n' |
| 5652 | 'can be imported, as well as all the hooks that can be used to\n' |
| 5653 | 'customize the import system. Note that failures in this step ' |
| 5654 | 'may\n' |
| 5655 | 'indicate either that the module could not be located, *or* that ' |
| 5656 | 'an\n' |
| 5657 | 'error occurred while initializing the module, which includes ' |
| 5658 | 'execution\n' |
| 5659 | "of the module's code.\n" |
| 5660 | '\n' |
| 5661 | 'If the requested module is retrieved successfully, it will be ' |
| 5662 | 'made\n' |
| 5663 | 'available in the local namespace in one of three ways:\n' |
| 5664 | '\n' |
| 5665 | '* If the module name is followed by "as", then the name ' |
| 5666 | 'following\n' |
| 5667 | ' "as" is bound directly to the imported module.\n' |
| 5668 | '\n' |
| 5669 | '* If no other name is specified, and the module being imported ' |
| 5670 | 'is a\n' |
| 5671 | " top level module, the module's name is bound in the local " |
| 5672 | 'namespace\n' |
| 5673 | ' as a reference to the imported module\n' |
| 5674 | '\n' |
| 5675 | '* If the module being imported is *not* a top level module, then ' |
| 5676 | 'the\n' |
| 5677 | ' name of the top level package that contains the module is ' |
| 5678 | 'bound in\n' |
| 5679 | ' the local namespace as a reference to the top level package. ' |
| 5680 | 'The\n' |
| 5681 | ' imported module must be accessed using its full qualified ' |
| 5682 | 'name\n' |
| 5683 | ' rather than directly\n' |
| 5684 | '\n' |
| 5685 | 'The "from" form uses a slightly more complex process:\n' |
| 5686 | '\n' |
| 5687 | '1. find the module specified in the "from" clause, loading and\n' |
| 5688 | ' initializing it if necessary;\n' |
| 5689 | '\n' |
| 5690 | '2. for each of the identifiers specified in the "import" ' |
| 5691 | 'clauses:\n' |
| 5692 | '\n' |
| 5693 | ' 1. check if the imported module has an attribute by that ' |
| 5694 | 'name\n' |
| 5695 | '\n' |
| 5696 | ' 2. if not, attempt to import a submodule with that name and ' |
| 5697 | 'then\n' |
| 5698 | ' check the imported module again for that attribute\n' |
| 5699 | '\n' |
| 5700 | ' 3. if the attribute is not found, "ImportError" is raised.\n' |
| 5701 | '\n' |
| 5702 | ' 4. otherwise, a reference to that value is stored in the ' |
| 5703 | 'local\n' |
| 5704 | ' namespace, using the name in the "as" clause if it is ' |
| 5705 | 'present,\n' |
| 5706 | ' otherwise using the attribute name\n' |
| 5707 | '\n' |
| 5708 | 'Examples:\n' |
| 5709 | '\n' |
| 5710 | ' import foo # foo imported and bound locally\n' |
| 5711 | ' import foo.bar.baz # foo.bar.baz imported, foo bound ' |
| 5712 | 'locally\n' |
| 5713 | ' import foo.bar.baz as fbb # foo.bar.baz imported and bound ' |
| 5714 | 'as fbb\n' |
| 5715 | ' from foo.bar import baz # foo.bar.baz imported and bound ' |
| 5716 | 'as baz\n' |
| 5717 | ' from foo import attr # foo imported and foo.attr bound ' |
| 5718 | 'as attr\n' |
| 5719 | '\n' |
| 5720 | 'If the list of identifiers is replaced by a star ("\'*\'"), all ' |
| 5721 | 'public\n' |
| 5722 | 'names defined in the module are bound in the local namespace for ' |
| 5723 | 'the\n' |
| 5724 | 'scope where the "import" statement occurs.\n' |
| 5725 | '\n' |
| 5726 | 'The *public names* defined by a module are determined by ' |
| 5727 | 'checking the\n' |
| 5728 | 'module\'s namespace for a variable named "__all__"; if defined, ' |
| 5729 | 'it must\n' |
| 5730 | 'be a sequence of strings which are names defined or imported by ' |
| 5731 | 'that\n' |
| 5732 | 'module. The names given in "__all__" are all considered public ' |
| 5733 | 'and\n' |
| 5734 | 'are required to exist. If "__all__" is not defined, the set of ' |
| 5735 | 'public\n' |
| 5736 | "names includes all names found in the module's namespace which " |
| 5737 | 'do not\n' |
| 5738 | 'begin with an underscore character ("\'_\'"). "__all__" should ' |
| 5739 | 'contain\n' |
| 5740 | 'the entire public API. It is intended to avoid accidentally ' |
| 5741 | 'exporting\n' |
| 5742 | 'items that are not part of the API (such as library modules ' |
| 5743 | 'which were\n' |
| 5744 | 'imported and used within the module).\n' |
| 5745 | '\n' |
| 5746 | 'The wild card form of import --- "from module import *" --- is ' |
| 5747 | 'only\n' |
| 5748 | 'allowed at the module level. Attempting to use it in class or\n' |
| 5749 | 'function definitions will raise a "SyntaxError".\n' |
| 5750 | '\n' |
| 5751 | 'When specifying what module to import you do not have to specify ' |
| 5752 | 'the\n' |
| 5753 | 'absolute name of the module. When a module or package is ' |
| 5754 | 'contained\n' |
| 5755 | 'within another package it is possible to make a relative import ' |
| 5756 | 'within\n' |
| 5757 | 'the same top package without having to mention the package name. ' |
| 5758 | 'By\n' |
| 5759 | 'using leading dots in the specified module or package after ' |
| 5760 | '"from" you\n' |
| 5761 | 'can specify how high to traverse up the current package ' |
| 5762 | 'hierarchy\n' |
| 5763 | 'without specifying exact names. One leading dot means the ' |
| 5764 | 'current\n' |
| 5765 | 'package where the module making the import exists. Two dots ' |
| 5766 | 'means up\n' |
| 5767 | 'one package level. Three dots is up two levels, etc. So if you ' |
| 5768 | 'execute\n' |
| 5769 | '"from . import mod" from a module in the "pkg" package then you ' |
| 5770 | 'will\n' |
| 5771 | 'end up importing "pkg.mod". If you execute "from ..subpkg2 ' |
| 5772 | 'import mod"\n' |
| 5773 | 'from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". ' |
| 5774 | 'The\n' |
| 5775 | 'specification for relative imports is contained within **PEP ' |
| 5776 | '328**.\n' |
| 5777 | '\n' |
| 5778 | '"importlib.import_module()" is provided to support applications ' |
| 5779 | 'that\n' |
| 5780 | 'determine dynamically the modules to be loaded.\n' |
| 5781 | '\n' |
| 5782 | '\n' |
| 5783 | 'Future statements\n' |
| 5784 | '=================\n' |
| 5785 | '\n' |
| 5786 | 'A *future statement* is a directive to the compiler that a ' |
| 5787 | 'particular\n' |
| 5788 | 'module should be compiled using syntax or semantics that will ' |
| 5789 | 'be\n' |
| 5790 | 'available in a specified future release of Python where the ' |
| 5791 | 'feature\n' |
| 5792 | 'becomes standard.\n' |
| 5793 | '\n' |
| 5794 | 'The future statement is intended to ease migration to future ' |
| 5795 | 'versions\n' |
| 5796 | 'of Python that introduce incompatible changes to the language. ' |
| 5797 | 'It\n' |
| 5798 | 'allows use of the new features on a per-module basis before the\n' |
| 5799 | 'release in which the feature becomes standard.\n' |
| 5800 | '\n' |
| 5801 | ' future_statement ::= "from" "__future__" "import" feature ' |
| 5802 | '["as" name]\n' |
| 5803 | ' ("," feature ["as" name])*\n' |
| 5804 | ' | "from" "__future__" "import" "(" ' |
| 5805 | 'feature ["as" name]\n' |
| 5806 | ' ("," feature ["as" name])* [","] ")"\n' |
| 5807 | ' feature ::= identifier\n' |
| 5808 | ' name ::= identifier\n' |
| 5809 | '\n' |
| 5810 | 'A future statement must appear near the top of the module. The ' |
| 5811 | 'only\n' |
| 5812 | 'lines that can appear before a future statement are:\n' |
| 5813 | '\n' |
| 5814 | '* the module docstring (if any),\n' |
| 5815 | '\n' |
| 5816 | '* comments,\n' |
| 5817 | '\n' |
| 5818 | '* blank lines, and\n' |
| 5819 | '\n' |
| 5820 | '* other future statements.\n' |
| 5821 | '\n' |
| 5822 | 'The features recognized by Python 3.0 are "absolute_import",\n' |
| 5823 | '"division", "generators", "unicode_literals", "print_function",\n' |
| 5824 | '"nested_scopes" and "with_statement". They are all redundant ' |
| 5825 | 'because\n' |
| 5826 | 'they are always enabled, and only kept for backwards ' |
| 5827 | 'compatibility.\n' |
| 5828 | '\n' |
| 5829 | 'A future statement is recognized and treated specially at ' |
| 5830 | 'compile\n' |
| 5831 | 'time: Changes to the semantics of core constructs are often\n' |
| 5832 | 'implemented by generating different code. It may even be the ' |
| 5833 | 'case\n' |
| 5834 | 'that a new feature introduces new incompatible syntax (such as a ' |
| 5835 | 'new\n' |
| 5836 | 'reserved word), in which case the compiler may need to parse ' |
| 5837 | 'the\n' |
| 5838 | 'module differently. Such decisions cannot be pushed off until\n' |
| 5839 | 'runtime.\n' |
| 5840 | '\n' |
| 5841 | 'For any given release, the compiler knows which feature names ' |
| 5842 | 'have\n' |
| 5843 | 'been defined, and raises a compile-time error if a future ' |
| 5844 | 'statement\n' |
| 5845 | 'contains a feature not known to it.\n' |
| 5846 | '\n' |
| 5847 | 'The direct runtime semantics are the same as for any import ' |
| 5848 | 'statement:\n' |
| 5849 | 'there is a standard module "__future__", described later, and it ' |
| 5850 | 'will\n' |
| 5851 | 'be imported in the usual way at the time the future statement ' |
| 5852 | 'is\n' |
| 5853 | 'executed.\n' |
| 5854 | '\n' |
| 5855 | 'The interesting runtime semantics depend on the specific ' |
| 5856 | 'feature\n' |
| 5857 | 'enabled by the future statement.\n' |
| 5858 | '\n' |
| 5859 | 'Note that there is nothing special about the statement:\n' |
| 5860 | '\n' |
| 5861 | ' import __future__ [as name]\n' |
| 5862 | '\n' |
| 5863 | "That is not a future statement; it's an ordinary import " |
| 5864 | 'statement with\n' |
| 5865 | 'no special semantics or syntax restrictions.\n' |
| 5866 | '\n' |
| 5867 | 'Code compiled by calls to the built-in functions "exec()" and\n' |
| 5868 | '"compile()" that occur in a module "M" containing a future ' |
| 5869 | 'statement\n' |
| 5870 | 'will, by default, use the new syntax or semantics associated ' |
| 5871 | 'with the\n' |
| 5872 | 'future statement. This can be controlled by optional arguments ' |
| 5873 | 'to\n' |
| 5874 | '"compile()" --- see the documentation of that function for ' |
| 5875 | 'details.\n' |
| 5876 | '\n' |
| 5877 | 'A future statement typed at an interactive interpreter prompt ' |
| 5878 | 'will\n' |
| 5879 | 'take effect for the rest of the interpreter session. If an\n' |
| 5880 | 'interpreter is started with the *-i* option, is passed a script ' |
| 5881 | 'name\n' |
| 5882 | 'to execute, and the script includes a future statement, it will ' |
| 5883 | 'be in\n' |
| 5884 | 'effect in the interactive session started after the script is\n' |
| 5885 | 'executed.\n' |
| 5886 | '\n' |
| 5887 | 'See also: **PEP 236** - Back to the __future__\n' |
| 5888 | '\n' |
| 5889 | ' The original proposal for the __future__ mechanism.\n', |
| 5890 | 'in': '\n' |
| 5891 | 'Comparisons\n' |
| 5892 | '***********\n' |
| 5893 | '\n' |
| 5894 | 'Unlike C, all comparison operations in Python have the same ' |
| 5895 | 'priority,\n' |
| 5896 | 'which is lower than that of any arithmetic, shifting or bitwise\n' |
| 5897 | 'operation. Also unlike C, expressions like "a < b < c" have the\n' |
| 5898 | 'interpretation that is conventional in mathematics:\n' |
| 5899 | '\n' |
| 5900 | ' comparison ::= or_expr ( comp_operator or_expr )*\n' |
| 5901 | ' comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!="\n' |
| 5902 | ' | "is" ["not"] | ["not"] "in"\n' |
| 5903 | '\n' |
| 5904 | 'Comparisons yield boolean values: "True" or "False".\n' |
| 5905 | '\n' |
| 5906 | 'Comparisons can be chained arbitrarily, e.g., "x < y <= z" is\n' |
| 5907 | 'equivalent to "x < y and y <= z", except that "y" is evaluated only\n' |
| 5908 | 'once (but in both cases "z" is not evaluated at all when "x < y" is\n' |
| 5909 | 'found to be false).\n' |
| 5910 | '\n' |
| 5911 | 'Formally, if *a*, *b*, *c*, ..., *y*, *z* are expressions and ' |
| 5912 | '*op1*,\n' |
| 5913 | '*op2*, ..., *opN* are comparison operators, then "a op1 b op2 c ... ' |
| 5914 | 'y\n' |
| 5915 | 'opN z" is equivalent to "a op1 b and b op2 c and ... y opN z", ' |
| 5916 | 'except\n' |
| 5917 | 'that each expression is evaluated at most once.\n' |
| 5918 | '\n' |
| 5919 | 'Note that "a op1 b op2 c" doesn\'t imply any kind of comparison ' |
| 5920 | 'between\n' |
| 5921 | '*a* and *c*, so that, e.g., "x < y > z" is perfectly legal (though\n' |
| 5922 | 'perhaps not pretty).\n' |
| 5923 | '\n' |
| 5924 | 'The operators "<", ">", "==", ">=", "<=", and "!=" compare the ' |
| 5925 | 'values\n' |
| 5926 | 'of two objects. The objects need not have the same type. If both ' |
| 5927 | 'are\n' |
| 5928 | 'numbers, they are converted to a common type. Otherwise, the "==" ' |
| 5929 | 'and\n' |
| 5930 | '"!=" operators *always* consider objects of different types to be\n' |
| 5931 | 'unequal, while the "<", ">", ">=" and "<=" operators raise a\n' |
| 5932 | '"TypeError" when comparing objects of different types that do not\n' |
| 5933 | 'implement these operators for the given pair of types. You can\n' |
| 5934 | 'control comparison behavior of objects of non-built-in types by\n' |
| 5935 | 'defining rich comparison methods like "__gt__()", described in ' |
| 5936 | 'section\n' |
| 5937 | '*Basic customization*.\n' |
| 5938 | '\n' |
| 5939 | 'Comparison of objects of the same type depends on the type:\n' |
| 5940 | '\n' |
| 5941 | '* Numbers are compared arithmetically.\n' |
| 5942 | '\n' |
| 5943 | '* The values "float(\'NaN\')" and "Decimal(\'NaN\')" are special. ' |
| 5944 | 'They\n' |
| 5945 | ' are identical to themselves, "x is x" but are not equal to\n' |
| 5946 | ' themselves, "x != x". Additionally, comparing any value to a\n' |
| 5947 | ' not-a-number value will return "False". For example, both "3 <\n' |
| 5948 | ' float(\'NaN\')" and "float(\'NaN\') < 3" will return "False".\n' |
| 5949 | '\n' |
| 5950 | '* Bytes objects are compared lexicographically using the numeric\n' |
| 5951 | ' values of their elements.\n' |
| 5952 | '\n' |
| 5953 | '* Strings are compared lexicographically using the numeric\n' |
| 5954 | ' equivalents (the result of the built-in function "ord()") of ' |
| 5955 | 'their\n' |
| 5956 | " characters. [3] String and bytes object can't be compared!\n" |
| 5957 | '\n' |
| 5958 | '* Tuples and lists are compared lexicographically using comparison\n' |
| 5959 | ' of corresponding elements. This means that to compare equal, ' |
| 5960 | 'each\n' |
| 5961 | ' element must compare equal and the two sequences must be of the ' |
| 5962 | 'same\n' |
| 5963 | ' type and have the same length.\n' |
| 5964 | '\n' |
| 5965 | ' If not equal, the sequences are ordered the same as their first\n' |
| 5966 | ' differing elements. For example, "[1,2,x] <= [1,2,y]" has the ' |
| 5967 | 'same\n' |
| 5968 | ' value as "x <= y". If the corresponding element does not exist, ' |
| 5969 | 'the\n' |
| 5970 | ' shorter sequence is ordered first (for example, "[1,2] < ' |
| 5971 | '[1,2,3]").\n' |
| 5972 | '\n' |
| 5973 | '* Mappings (dictionaries) compare equal if and only if they have ' |
| 5974 | 'the\n' |
| 5975 | ' same "(key, value)" pairs. Order comparisons "(\'<\', \'<=\', ' |
| 5976 | "'>=',\n" |
| 5977 | ' \'>\')" raise "TypeError".\n' |
| 5978 | '\n' |
| 5979 | '* Sets and frozensets define comparison operators to mean subset ' |
| 5980 | 'and\n' |
| 5981 | ' superset tests. Those relations do not define total orderings ' |
| 5982 | '(the\n' |
| 5983 | ' two sets "{1,2}" and "{2,3}" are not equal, nor subsets of one\n' |
| 5984 | ' another, nor supersets of one another). Accordingly, sets are ' |
| 5985 | 'not\n' |
| 5986 | ' appropriate arguments for functions which depend on total ' |
| 5987 | 'ordering.\n' |
| 5988 | ' For example, "min()", "max()", and "sorted()" produce undefined\n' |
| 5989 | ' results given a list of sets as inputs.\n' |
| 5990 | '\n' |
| 5991 | '* Most other objects of built-in types compare unequal unless they\n' |
| 5992 | ' are the same object; the choice whether one object is considered\n' |
| 5993 | ' smaller or larger than another one is made arbitrarily but\n' |
| 5994 | ' consistently within one execution of a program.\n' |
| 5995 | '\n' |
| 5996 | 'Comparison of objects of differing types depends on whether either ' |
| 5997 | 'of\n' |
| 5998 | 'the types provide explicit support for the comparison. Most ' |
| 5999 | 'numeric\n' |
| 6000 | 'types can be compared with one another. When cross-type comparison ' |
| 6001 | 'is\n' |
| 6002 | 'not supported, the comparison method returns "NotImplemented".\n' |
| 6003 | '\n' |
| 6004 | 'The operators "in" and "not in" test for membership. "x in s"\n' |
| 6005 | 'evaluates to true if *x* is a member of *s*, and false otherwise. ' |
| 6006 | '"x\n' |
| 6007 | 'not in s" returns the negation of "x in s". All built-in sequences\n' |
| 6008 | 'and set types support this as well as dictionary, for which "in" ' |
| 6009 | 'tests\n' |
| 6010 | 'whether the dictionary has a given key. For container types such as\n' |
| 6011 | 'list, tuple, set, frozenset, dict, or collections.deque, the\n' |
| 6012 | 'expression "x in y" is equivalent to "any(x is e or x == e for e in\n' |
| 6013 | 'y)".\n' |
| 6014 | '\n' |
| 6015 | 'For the string and bytes types, "x in y" is true if and only if *x* ' |
| 6016 | 'is\n' |
| 6017 | 'a substring of *y*. An equivalent test is "y.find(x) != -1". ' |
| 6018 | 'Empty\n' |
| 6019 | 'strings are always considered to be a substring of any other ' |
| 6020 | 'string,\n' |
| 6021 | 'so """ in "abc"" will return "True".\n' |
| 6022 | '\n' |
| 6023 | 'For user-defined classes which define the "__contains__()" method, ' |
| 6024 | '"x\n' |
| 6025 | 'in y" is true if and only if "y.__contains__(x)" is true.\n' |
| 6026 | '\n' |
| 6027 | 'For user-defined classes which do not define "__contains__()" but ' |
| 6028 | 'do\n' |
| 6029 | 'define "__iter__()", "x in y" is true if some value "z" with "x == ' |
| 6030 | 'z"\n' |
| 6031 | 'is produced while iterating over "y". If an exception is raised\n' |
| 6032 | 'during the iteration, it is as if "in" raised that exception.\n' |
| 6033 | '\n' |
| 6034 | 'Lastly, the old-style iteration protocol is tried: if a class ' |
| 6035 | 'defines\n' |
| 6036 | '"__getitem__()", "x in y" is true if and only if there is a non-\n' |
| 6037 | 'negative integer index *i* such that "x == y[i]", and all lower\n' |
| 6038 | 'integer indices do not raise "IndexError" exception. (If any other\n' |
| 6039 | 'exception is raised, it is as if "in" raised that exception).\n' |
| 6040 | '\n' |
| 6041 | 'The operator "not in" is defined to have the inverse true value of\n' |
| 6042 | '"in".\n' |
| 6043 | '\n' |
| 6044 | 'The operators "is" and "is not" test for object identity: "x is y" ' |
| 6045 | 'is\n' |
| 6046 | 'true if and only if *x* and *y* are the same object. "x is not y"\n' |
| 6047 | 'yields the inverse truth value. [4]\n', |
| 6048 | 'integers': '\n' |
| 6049 | 'Integer literals\n' |
| 6050 | '****************\n' |
| 6051 | '\n' |
| 6052 | 'Integer literals are described by the following lexical ' |
| 6053 | 'definitions:\n' |
| 6054 | '\n' |
| 6055 | ' integer ::= decimalinteger | octinteger | hexinteger ' |
| 6056 | '| bininteger\n' |
| 6057 | ' decimalinteger ::= nonzerodigit digit* | "0"+\n' |
| 6058 | ' nonzerodigit ::= "1"..."9"\n' |
| 6059 | ' digit ::= "0"..."9"\n' |
| 6060 | ' octinteger ::= "0" ("o" | "O") octdigit+\n' |
| 6061 | ' hexinteger ::= "0" ("x" | "X") hexdigit+\n' |
| 6062 | ' bininteger ::= "0" ("b" | "B") bindigit+\n' |
| 6063 | ' octdigit ::= "0"..."7"\n' |
| 6064 | ' hexdigit ::= digit | "a"..."f" | "A"..."F"\n' |
| 6065 | ' bindigit ::= "0" | "1"\n' |
| 6066 | '\n' |
| 6067 | 'There is no limit for the length of integer literals apart ' |
| 6068 | 'from what\n' |
| 6069 | 'can be stored in available memory.\n' |
| 6070 | '\n' |
| 6071 | 'Note that leading zeros in a non-zero decimal number are not ' |
| 6072 | 'allowed.\n' |
| 6073 | 'This is for disambiguation with C-style octal literals, which ' |
| 6074 | 'Python\n' |
| 6075 | 'used before version 3.0.\n' |
| 6076 | '\n' |
| 6077 | 'Some examples of integer literals:\n' |
| 6078 | '\n' |
| 6079 | ' 7 2147483647 0o177 ' |
| 6080 | '0b100110111\n' |
| 6081 | ' 3 79228162514264337593543950336 0o377 ' |
| 6082 | '0xdeadbeef\n', |
| 6083 | 'lambda': '\n' |
| 6084 | 'Lambdas\n' |
| 6085 | '*******\n' |
| 6086 | '\n' |
| 6087 | ' lambda_expr ::= "lambda" [parameter_list]: expression\n' |
| 6088 | ' lambda_expr_nocond ::= "lambda" [parameter_list]: ' |
| 6089 | 'expression_nocond\n' |
| 6090 | '\n' |
| 6091 | 'Lambda expressions (sometimes called lambda forms) are used to ' |
| 6092 | 'create\n' |
| 6093 | 'anonymous functions. The expression "lambda arguments: ' |
| 6094 | 'expression"\n' |
| 6095 | 'yields a function object. The unnamed object behaves like a ' |
| 6096 | 'function\n' |
| 6097 | 'object defined with\n' |
| 6098 | '\n' |
| 6099 | ' def <lambda>(arguments):\n' |
| 6100 | ' return expression\n' |
| 6101 | '\n' |
| 6102 | 'See section *Function definitions* for the syntax of parameter ' |
| 6103 | 'lists.\n' |
| 6104 | 'Note that functions created with lambda expressions cannot ' |
| 6105 | 'contain\n' |
| 6106 | 'statements or annotations.\n', |
| 6107 | 'lists': '\n' |
| 6108 | 'List displays\n' |
| 6109 | '*************\n' |
| 6110 | '\n' |
| 6111 | 'A list display is a possibly empty series of expressions enclosed ' |
| 6112 | 'in\n' |
| 6113 | 'square brackets:\n' |
| 6114 | '\n' |
| 6115 | ' list_display ::= "[" [expression_list | comprehension] "]"\n' |
| 6116 | '\n' |
| 6117 | 'A list display yields a new list object, the contents being ' |
| 6118 | 'specified\n' |
| 6119 | 'by either a list of expressions or a comprehension. When a ' |
| 6120 | 'comma-\n' |
| 6121 | 'separated list of expressions is supplied, its elements are ' |
| 6122 | 'evaluated\n' |
| 6123 | 'from left to right and placed into the list object in that ' |
| 6124 | 'order.\n' |
| 6125 | 'When a comprehension is supplied, the list is constructed from ' |
| 6126 | 'the\n' |
| 6127 | 'elements resulting from the comprehension.\n', |
| 6128 | 'naming': '\n' |
| 6129 | 'Naming and binding\n' |
| 6130 | '******************\n' |
| 6131 | '\n' |
| 6132 | '\n' |
| 6133 | 'Binding of names\n' |
| 6134 | '================\n' |
| 6135 | '\n' |
| 6136 | '*Names* refer to objects. Names are introduced by name binding\n' |
| 6137 | 'operations.\n' |
| 6138 | '\n' |
| 6139 | 'The following constructs bind names: formal parameters to ' |
| 6140 | 'functions,\n' |
| 6141 | '"import" statements, class and function definitions (these bind ' |
| 6142 | 'the\n' |
| 6143 | 'class or function name in the defining block), and targets that ' |
| 6144 | 'are\n' |
| 6145 | 'identifiers if occurring in an assignment, "for" loop header, or ' |
| 6146 | 'after\n' |
| 6147 | '"as" in a "with" statement or "except" clause. The "import" ' |
| 6148 | 'statement\n' |
| 6149 | 'of the form "from ... import *" binds all names defined in the\n' |
| 6150 | 'imported module, except those beginning with an underscore. ' |
| 6151 | 'This form\n' |
| 6152 | 'may only be used at the module level.\n' |
| 6153 | '\n' |
| 6154 | 'A target occurring in a "del" statement is also considered bound ' |
| 6155 | 'for\n' |
| 6156 | 'this purpose (though the actual semantics are to unbind the ' |
| 6157 | 'name).\n' |
| 6158 | '\n' |
| 6159 | 'Each assignment or import statement occurs within a block ' |
| 6160 | 'defined by a\n' |
| 6161 | 'class or function definition or at the module level (the ' |
| 6162 | 'top-level\n' |
| 6163 | 'code block).\n' |
| 6164 | '\n' |
| 6165 | 'If a name is bound in a block, it is a local variable of that ' |
| 6166 | 'block,\n' |
| 6167 | 'unless declared as "nonlocal" or "global". If a name is bound ' |
| 6168 | 'at the\n' |
| 6169 | 'module level, it is a global variable. (The variables of the ' |
| 6170 | 'module\n' |
| 6171 | 'code block are local and global.) If a variable is used in a ' |
| 6172 | 'code\n' |
| 6173 | 'block but not defined there, it is a *free variable*.\n' |
| 6174 | '\n' |
| 6175 | 'Each occurrence of a name in the program text refers to the ' |
| 6176 | '*binding*\n' |
| 6177 | 'of that name established by the following name resolution ' |
| 6178 | 'rules.\n' |
| 6179 | '\n' |
| 6180 | '\n' |
| 6181 | 'Resolution of names\n' |
| 6182 | '===================\n' |
| 6183 | '\n' |
| 6184 | 'A *scope* defines the visibility of a name within a block. If a ' |
| 6185 | 'local\n' |
| 6186 | 'variable is defined in a block, its scope includes that block. ' |
| 6187 | 'If the\n' |
| 6188 | 'definition occurs in a function block, the scope extends to any ' |
| 6189 | 'blocks\n' |
| 6190 | 'contained within the defining one, unless a contained block ' |
| 6191 | 'introduces\n' |
| 6192 | 'a different binding for the name.\n' |
| 6193 | '\n' |
| 6194 | 'When a name is used in a code block, it is resolved using the ' |
| 6195 | 'nearest\n' |
| 6196 | 'enclosing scope. The set of all such scopes visible to a code ' |
| 6197 | 'block\n' |
| 6198 | "is called the block's *environment*.\n" |
| 6199 | '\n' |
| 6200 | 'When a name is not found at all, a "NameError" exception is ' |
| 6201 | 'raised. If\n' |
| 6202 | 'the current scope is a function scope, and the name refers to a ' |
| 6203 | 'local\n' |
| 6204 | 'variable that has not yet been bound to a value at the point ' |
| 6205 | 'where the\n' |
| 6206 | 'name is used, an "UnboundLocalError" exception is raised.\n' |
| 6207 | '"UnboundLocalError" is a subclass of "NameError".\n' |
| 6208 | '\n' |
| 6209 | 'If a name binding operation occurs anywhere within a code block, ' |
| 6210 | 'all\n' |
| 6211 | 'uses of the name within the block are treated as references to ' |
| 6212 | 'the\n' |
| 6213 | 'current block. This can lead to errors when a name is used ' |
| 6214 | 'within a\n' |
| 6215 | 'block before it is bound. This rule is subtle. Python lacks\n' |
| 6216 | 'declarations and allows name binding operations to occur ' |
| 6217 | 'anywhere\n' |
| 6218 | 'within a code block. The local variables of a code block can ' |
| 6219 | 'be\n' |
| 6220 | 'determined by scanning the entire text of the block for name ' |
| 6221 | 'binding\n' |
| 6222 | 'operations.\n' |
| 6223 | '\n' |
| 6224 | 'If the "global" statement occurs within a block, all uses of the ' |
| 6225 | 'name\n' |
| 6226 | 'specified in the statement refer to the binding of that name in ' |
| 6227 | 'the\n' |
| 6228 | 'top-level namespace. Names are resolved in the top-level ' |
| 6229 | 'namespace by\n' |
| 6230 | 'searching the global namespace, i.e. the namespace of the ' |
| 6231 | 'module\n' |
| 6232 | 'containing the code block, and the builtins namespace, the ' |
| 6233 | 'namespace\n' |
| 6234 | 'of the module "builtins". The global namespace is searched ' |
| 6235 | 'first. If\n' |
| 6236 | 'the name is not found there, the builtins namespace is ' |
| 6237 | 'searched. The\n' |
| 6238 | '"global" statement must precede all uses of the name.\n' |
| 6239 | '\n' |
| 6240 | 'The "global" statement has the same scope as a name binding ' |
| 6241 | 'operation\n' |
| 6242 | 'in the same block. If the nearest enclosing scope for a free ' |
| 6243 | 'variable\n' |
| 6244 | 'contains a global statement, the free variable is treated as a ' |
| 6245 | 'global.\n' |
| 6246 | '\n' |
| 6247 | 'The "nonlocal" statement causes corresponding names to refer to\n' |
| 6248 | 'previously bound variables in the nearest enclosing function ' |
| 6249 | 'scope.\n' |
| 6250 | '"SyntaxError" is raised at compile time if the given name does ' |
| 6251 | 'not\n' |
| 6252 | 'exist in any enclosing function scope.\n' |
| 6253 | '\n' |
| 6254 | 'The namespace for a module is automatically created the first ' |
| 6255 | 'time a\n' |
| 6256 | 'module is imported. The main module for a script is always ' |
| 6257 | 'called\n' |
| 6258 | '"__main__".\n' |
| 6259 | '\n' |
| 6260 | 'Class definition blocks and arguments to "exec()" and "eval()" ' |
| 6261 | 'are\n' |
| 6262 | 'special in the context of name resolution. A class definition is ' |
| 6263 | 'an\n' |
| 6264 | 'executable statement that may use and define names. These ' |
| 6265 | 'references\n' |
| 6266 | 'follow the normal rules for name resolution with an exception ' |
| 6267 | 'that\n' |
| 6268 | 'unbound local variables are looked up in the global namespace. ' |
| 6269 | 'The\n' |
| 6270 | 'namespace of the class definition becomes the attribute ' |
| 6271 | 'dictionary of\n' |
| 6272 | 'the class. The scope of names defined in a class block is ' |
| 6273 | 'limited to\n' |
| 6274 | 'the class block; it does not extend to the code blocks of ' |
| 6275 | 'methods --\n' |
| 6276 | 'this includes comprehensions and generator expressions since ' |
| 6277 | 'they are\n' |
| 6278 | 'implemented using a function scope. This means that the ' |
| 6279 | 'following\n' |
| 6280 | 'will fail:\n' |
| 6281 | '\n' |
| 6282 | ' class A:\n' |
| 6283 | ' a = 42\n' |
| 6284 | ' b = list(a + i for i in range(10))\n' |
| 6285 | '\n' |
| 6286 | '\n' |
| 6287 | 'Builtins and restricted execution\n' |
| 6288 | '=================================\n' |
| 6289 | '\n' |
| 6290 | 'The builtins namespace associated with the execution of a code ' |
| 6291 | 'block\n' |
| 6292 | 'is actually found by looking up the name "__builtins__" in its ' |
| 6293 | 'global\n' |
| 6294 | 'namespace; this should be a dictionary or a module (in the ' |
| 6295 | 'latter case\n' |
| 6296 | "the module's dictionary is used). By default, when in the " |
| 6297 | '"__main__"\n' |
| 6298 | 'module, "__builtins__" is the built-in module "builtins"; when ' |
| 6299 | 'in any\n' |
| 6300 | 'other module, "__builtins__" is an alias for the dictionary of ' |
| 6301 | 'the\n' |
| 6302 | '"builtins" module itself. "__builtins__" can be set to a ' |
| 6303 | 'user-created\n' |
| 6304 | 'dictionary to create a weak form of restricted execution.\n' |
| 6305 | '\n' |
| 6306 | '**CPython implementation detail:** Users should not touch\n' |
| 6307 | '"__builtins__"; it is strictly an implementation detail. Users\n' |
| 6308 | 'wanting to override values in the builtins namespace should ' |
| 6309 | '"import"\n' |
| 6310 | 'the "builtins" module and modify its attributes appropriately.\n' |
| 6311 | '\n' |
| 6312 | '\n' |
| 6313 | 'Interaction with dynamic features\n' |
| 6314 | '=================================\n' |
| 6315 | '\n' |
| 6316 | 'Name resolution of free variables occurs at runtime, not at ' |
| 6317 | 'compile\n' |
| 6318 | 'time. This means that the following code will print 42:\n' |
| 6319 | '\n' |
| 6320 | ' i = 10\n' |
| 6321 | ' def f():\n' |
| 6322 | ' print(i)\n' |
| 6323 | ' i = 42\n' |
| 6324 | ' f()\n' |
| 6325 | '\n' |
| 6326 | 'There are several cases where Python statements are illegal when ' |
| 6327 | 'used\n' |
| 6328 | 'in conjunction with nested scopes that contain free variables.\n' |
| 6329 | '\n' |
| 6330 | 'If a variable is referenced in an enclosing scope, it is illegal ' |
| 6331 | 'to\n' |
| 6332 | 'delete the name. An error will be reported at compile time.\n' |
| 6333 | '\n' |
| 6334 | 'The "eval()" and "exec()" functions do not have access to the ' |
| 6335 | 'full\n' |
| 6336 | 'environment for resolving names. Names may be resolved in the ' |
| 6337 | 'local\n' |
| 6338 | 'and global namespaces of the caller. Free variables are not ' |
| 6339 | 'resolved\n' |
| 6340 | 'in the nearest enclosing namespace, but in the global ' |
| 6341 | 'namespace. [1]\n' |
| 6342 | 'The "exec()" and "eval()" functions have optional arguments to\n' |
| 6343 | 'override the global and local namespace. If only one namespace ' |
| 6344 | 'is\n' |
| 6345 | 'specified, it is used for both.\n', |
| 6346 | 'nonlocal': '\n' |
| 6347 | 'The "nonlocal" statement\n' |
| 6348 | '************************\n' |
| 6349 | '\n' |
| 6350 | ' nonlocal_stmt ::= "nonlocal" identifier ("," identifier)*\n' |
| 6351 | '\n' |
| 6352 | 'The "nonlocal" statement causes the listed identifiers to ' |
| 6353 | 'refer to\n' |
| 6354 | 'previously bound variables in the nearest enclosing scope ' |
| 6355 | 'excluding\n' |
| 6356 | 'globals. This is important because the default behavior for ' |
| 6357 | 'binding is\n' |
| 6358 | 'to search the local namespace first. The statement allows\n' |
| 6359 | 'encapsulated code to rebind variables outside of the local ' |
| 6360 | 'scope\n' |
| 6361 | 'besides the global (module) scope.\n' |
| 6362 | '\n' |
| 6363 | 'Names listed in a "nonlocal" statement, unlike those listed in ' |
| 6364 | 'a\n' |
| 6365 | '"global" statement, must refer to pre-existing bindings in an\n' |
| 6366 | 'enclosing scope (the scope in which a new binding should be ' |
| 6367 | 'created\n' |
| 6368 | 'cannot be determined unambiguously).\n' |
| 6369 | '\n' |
| 6370 | 'Names listed in a "nonlocal" statement must not collide with ' |
| 6371 | 'pre-\n' |
| 6372 | 'existing bindings in the local scope.\n' |
| 6373 | '\n' |
| 6374 | 'See also: **PEP 3104** - Access to Names in Outer Scopes\n' |
| 6375 | '\n' |
| 6376 | ' The specification for the "nonlocal" statement.\n', |
| 6377 | 'numbers': '\n' |
| 6378 | 'Numeric literals\n' |
| 6379 | '****************\n' |
| 6380 | '\n' |
| 6381 | 'There are three types of numeric literals: integers, floating ' |
| 6382 | 'point\n' |
| 6383 | 'numbers, and imaginary numbers. There are no complex literals\n' |
| 6384 | '(complex numbers can be formed by adding a real number and an\n' |
| 6385 | 'imaginary number).\n' |
| 6386 | '\n' |
| 6387 | 'Note that numeric literals do not include a sign; a phrase like ' |
| 6388 | '"-1"\n' |
| 6389 | 'is actually an expression composed of the unary operator ' |
| 6390 | '\'"-"\' and the\n' |
| 6391 | 'literal "1".\n', |
| 6392 | 'numeric-types': '\n' |
| 6393 | 'Emulating numeric types\n' |
| 6394 | '***********************\n' |
| 6395 | '\n' |
| 6396 | 'The following methods can be defined to emulate numeric ' |
| 6397 | 'objects.\n' |
| 6398 | 'Methods corresponding to operations that are not ' |
| 6399 | 'supported by the\n' |
| 6400 | 'particular kind of number implemented (e.g., bitwise ' |
| 6401 | 'operations for\n' |
| 6402 | 'non-integral numbers) should be left undefined.\n' |
| 6403 | '\n' |
| 6404 | 'object.__add__(self, other)\n' |
| 6405 | 'object.__sub__(self, other)\n' |
| 6406 | 'object.__mul__(self, other)\n' |
| 6407 | 'object.__matmul__(self, other)\n' |
| 6408 | 'object.__truediv__(self, other)\n' |
| 6409 | 'object.__floordiv__(self, other)\n' |
| 6410 | 'object.__mod__(self, other)\n' |
| 6411 | 'object.__divmod__(self, other)\n' |
| 6412 | 'object.__pow__(self, other[, modulo])\n' |
| 6413 | 'object.__lshift__(self, other)\n' |
| 6414 | 'object.__rshift__(self, other)\n' |
| 6415 | 'object.__and__(self, other)\n' |
| 6416 | 'object.__xor__(self, other)\n' |
| 6417 | 'object.__or__(self, other)\n' |
| 6418 | '\n' |
| 6419 | ' These methods are called to implement the binary ' |
| 6420 | 'arithmetic\n' |
| 6421 | ' operations ("+", "-", "*", "@", "/", "//", "%", ' |
| 6422 | '"divmod()",\n' |
| 6423 | ' "pow()", "**", "<<", ">>", "&", "^", "|"). For ' |
| 6424 | 'instance, to\n' |
| 6425 | ' evaluate the expression "x + y", where *x* is an ' |
| 6426 | 'instance of a\n' |
| 6427 | ' class that has an "__add__()" method, "x.__add__(y)" ' |
| 6428 | 'is called.\n' |
| 6429 | ' The "__divmod__()" method should be the equivalent to ' |
| 6430 | 'using\n' |
| 6431 | ' "__floordiv__()" and "__mod__()"; it should not be ' |
| 6432 | 'related to\n' |
| 6433 | ' "__truediv__()". Note that "__pow__()" should be ' |
| 6434 | 'defined to accept\n' |
| 6435 | ' an optional third argument if the ternary version of ' |
| 6436 | 'the built-in\n' |
| 6437 | ' "pow()" function is to be supported.\n' |
| 6438 | '\n' |
| 6439 | ' If one of those methods does not support the operation ' |
| 6440 | 'with the\n' |
| 6441 | ' supplied arguments, it should return ' |
| 6442 | '"NotImplemented".\n' |
| 6443 | '\n' |
| 6444 | 'object.__radd__(self, other)\n' |
| 6445 | 'object.__rsub__(self, other)\n' |
| 6446 | 'object.__rmul__(self, other)\n' |
| 6447 | 'object.__rmatmul__(self, other)\n' |
| 6448 | 'object.__rtruediv__(self, other)\n' |
| 6449 | 'object.__rfloordiv__(self, other)\n' |
| 6450 | 'object.__rmod__(self, other)\n' |
| 6451 | 'object.__rdivmod__(self, other)\n' |
| 6452 | 'object.__rpow__(self, other)\n' |
| 6453 | 'object.__rlshift__(self, other)\n' |
| 6454 | 'object.__rrshift__(self, other)\n' |
| 6455 | 'object.__rand__(self, other)\n' |
| 6456 | 'object.__rxor__(self, other)\n' |
| 6457 | 'object.__ror__(self, other)\n' |
| 6458 | '\n' |
| 6459 | ' These methods are called to implement the binary ' |
| 6460 | 'arithmetic\n' |
| 6461 | ' operations ("+", "-", "*", "@", "/", "//", "%", ' |
| 6462 | '"divmod()",\n' |
| 6463 | ' "pow()", "**", "<<", ">>", "&", "^", "|") with ' |
| 6464 | 'reflected (swapped)\n' |
| 6465 | ' operands. These functions are only called if the left ' |
| 6466 | 'operand does\n' |
| 6467 | ' not support the corresponding operation and the ' |
| 6468 | 'operands are of\n' |
| 6469 | ' different types. [2] For instance, to evaluate the ' |
| 6470 | 'expression "x -\n' |
| 6471 | ' y", where *y* is an instance of a class that has an ' |
| 6472 | '"__rsub__()"\n' |
| 6473 | ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" ' |
| 6474 | 'returns\n' |
| 6475 | ' *NotImplemented*.\n' |
| 6476 | '\n' |
| 6477 | ' Note that ternary "pow()" will not try calling ' |
| 6478 | '"__rpow__()" (the\n' |
| 6479 | ' coercion rules would become too complicated).\n' |
| 6480 | '\n' |
| 6481 | " Note: If the right operand's type is a subclass of the " |
| 6482 | 'left\n' |
| 6483 | " operand's type and that subclass provides the " |
| 6484 | 'reflected method\n' |
| 6485 | ' for the operation, this method will be called before ' |
| 6486 | 'the left\n' |
| 6487 | " operand's non-reflected method. This behavior " |
| 6488 | 'allows subclasses\n' |
| 6489 | " to override their ancestors' operations.\n" |
| 6490 | '\n' |
| 6491 | 'object.__iadd__(self, other)\n' |
| 6492 | 'object.__isub__(self, other)\n' |
| 6493 | 'object.__imul__(self, other)\n' |
| 6494 | 'object.__imatmul__(self, other)\n' |
| 6495 | 'object.__itruediv__(self, other)\n' |
| 6496 | 'object.__ifloordiv__(self, other)\n' |
| 6497 | 'object.__imod__(self, other)\n' |
| 6498 | 'object.__ipow__(self, other[, modulo])\n' |
| 6499 | 'object.__ilshift__(self, other)\n' |
| 6500 | 'object.__irshift__(self, other)\n' |
| 6501 | 'object.__iand__(self, other)\n' |
| 6502 | 'object.__ixor__(self, other)\n' |
| 6503 | 'object.__ior__(self, other)\n' |
| 6504 | '\n' |
| 6505 | ' These methods are called to implement the augmented ' |
| 6506 | 'arithmetic\n' |
| 6507 | ' assignments ("+=", "-=", "*=", "@=", "/=", "//=", ' |
| 6508 | '"%=", "**=",\n' |
| 6509 | ' "<<=", ">>=", "&=", "^=", "|="). These methods should ' |
| 6510 | 'attempt to\n' |
| 6511 | ' do the operation in-place (modifying *self*) and ' |
| 6512 | 'return the result\n' |
| 6513 | ' (which could be, but does not have to be, *self*). If ' |
| 6514 | 'a specific\n' |
| 6515 | ' method is not defined, the augmented assignment falls ' |
| 6516 | 'back to the\n' |
| 6517 | ' normal methods. For instance, if *x* is an instance ' |
| 6518 | 'of a class\n' |
| 6519 | ' with an "__iadd__()" method, "x += y" is equivalent to ' |
| 6520 | '"x =\n' |
| 6521 | ' x.__iadd__(y)" . Otherwise, "x.__add__(y)" and ' |
| 6522 | '"y.__radd__(x)" are\n' |
| 6523 | ' considered, as with the evaluation of "x + y". In ' |
| 6524 | 'certain\n' |
| 6525 | ' situations, augmented assignment can result in ' |
| 6526 | 'unexpected errors\n' |
| 6527 | " (see *Why does a_tuple[i] += ['item'] raise an " |
| 6528 | 'exception when the\n' |
| 6529 | ' addition works?*), but this behavior is in fact part ' |
| 6530 | 'of the data\n' |
| 6531 | ' model.\n' |
| 6532 | '\n' |
| 6533 | 'object.__neg__(self)\n' |
| 6534 | 'object.__pos__(self)\n' |
| 6535 | 'object.__abs__(self)\n' |
| 6536 | 'object.__invert__(self)\n' |
| 6537 | '\n' |
| 6538 | ' Called to implement the unary arithmetic operations ' |
| 6539 | '("-", "+",\n' |
| 6540 | ' "abs()" and "~").\n' |
| 6541 | '\n' |
| 6542 | 'object.__complex__(self)\n' |
| 6543 | 'object.__int__(self)\n' |
| 6544 | 'object.__float__(self)\n' |
| 6545 | 'object.__round__(self[, n])\n' |
| 6546 | '\n' |
| 6547 | ' Called to implement the built-in functions ' |
| 6548 | '"complex()", "int()",\n' |
| 6549 | ' "float()" and "round()". Should return a value of the ' |
| 6550 | 'appropriate\n' |
| 6551 | ' type.\n' |
| 6552 | '\n' |
| 6553 | 'object.__index__(self)\n' |
| 6554 | '\n' |
| 6555 | ' Called to implement "operator.index()", and whenever ' |
| 6556 | 'Python needs\n' |
| 6557 | ' to losslessly convert the numeric object to an integer ' |
| 6558 | 'object (such\n' |
| 6559 | ' as in slicing, or in the built-in "bin()", "hex()" and ' |
| 6560 | '"oct()"\n' |
| 6561 | ' functions). Presence of this method indicates that the ' |
| 6562 | 'numeric\n' |
| 6563 | ' object is an integer type. Must return an integer.\n' |
| 6564 | '\n' |
| 6565 | ' Note: In order to have a coherent integer type class, ' |
| 6566 | 'when\n' |
| 6567 | ' "__index__()" is defined "__int__()" should also be ' |
| 6568 | 'defined, and\n' |
| 6569 | ' both should return the same value.\n', |
| 6570 | 'objects': '\n' |
| 6571 | 'Objects, values and types\n' |
| 6572 | '*************************\n' |
| 6573 | '\n' |
| 6574 | "*Objects* are Python's abstraction for data. All data in a " |
| 6575 | 'Python\n' |
| 6576 | 'program is represented by objects or by relations between ' |
| 6577 | 'objects. (In\n' |
| 6578 | "a sense, and in conformance to Von Neumann's model of a " |
| 6579 | '"stored\n' |
| 6580 | 'program computer," code is also represented by objects.)\n' |
| 6581 | '\n' |
| 6582 | "Every object has an identity, a type and a value. An object's\n" |
| 6583 | '*identity* never changes once it has been created; you may ' |
| 6584 | 'think of it\n' |
| 6585 | 'as the object\'s address in memory. The \'"is"\' operator ' |
| 6586 | 'compares the\n' |
| 6587 | 'identity of two objects; the "id()" function returns an ' |
| 6588 | 'integer\n' |
| 6589 | 'representing its identity.\n' |
| 6590 | '\n' |
| 6591 | '**CPython implementation detail:** For CPython, "id(x)" is the ' |
| 6592 | 'memory\n' |
| 6593 | 'address where "x" is stored.\n' |
| 6594 | '\n' |
| 6595 | "An object's type determines the operations that the object " |
| 6596 | 'supports\n' |
| 6597 | '(e.g., "does it have a length?") and also defines the possible ' |
| 6598 | 'values\n' |
| 6599 | 'for objects of that type. The "type()" function returns an ' |
| 6600 | "object's\n" |
| 6601 | 'type (which is an object itself). Like its identity, an ' |
| 6602 | "object's\n" |
| 6603 | '*type* is also unchangeable. [1]\n' |
| 6604 | '\n' |
| 6605 | 'The *value* of some objects can change. Objects whose value ' |
| 6606 | 'can\n' |
| 6607 | 'change are said to be *mutable*; objects whose value is ' |
| 6608 | 'unchangeable\n' |
| 6609 | 'once they are created are called *immutable*. (The value of an\n' |
| 6610 | 'immutable container object that contains a reference to a ' |
| 6611 | 'mutable\n' |
| 6612 | "object can change when the latter's value is changed; however " |
| 6613 | 'the\n' |
| 6614 | 'container is still considered immutable, because the collection ' |
| 6615 | 'of\n' |
| 6616 | 'objects it contains cannot be changed. So, immutability is ' |
| 6617 | 'not\n' |
| 6618 | 'strictly the same as having an unchangeable value, it is more ' |
| 6619 | 'subtle.)\n' |
| 6620 | "An object's mutability is determined by its type; for " |
| 6621 | 'instance,\n' |
| 6622 | 'numbers, strings and tuples are immutable, while dictionaries ' |
| 6623 | 'and\n' |
| 6624 | 'lists are mutable.\n' |
| 6625 | '\n' |
| 6626 | 'Objects are never explicitly destroyed; however, when they ' |
| 6627 | 'become\n' |
| 6628 | 'unreachable they may be garbage-collected. An implementation ' |
| 6629 | 'is\n' |
| 6630 | 'allowed to postpone garbage collection or omit it altogether ' |
| 6631 | '--- it is\n' |
| 6632 | 'a matter of implementation quality how garbage collection is\n' |
| 6633 | 'implemented, as long as no objects are collected that are ' |
| 6634 | 'still\n' |
| 6635 | 'reachable.\n' |
| 6636 | '\n' |
| 6637 | '**CPython implementation detail:** CPython currently uses a ' |
| 6638 | 'reference-\n' |
| 6639 | 'counting scheme with (optional) delayed detection of cyclically ' |
| 6640 | 'linked\n' |
| 6641 | 'garbage, which collects most objects as soon as they become\n' |
| 6642 | 'unreachable, but is not guaranteed to collect garbage ' |
| 6643 | 'containing\n' |
| 6644 | 'circular references. See the documentation of the "gc" module ' |
| 6645 | 'for\n' |
| 6646 | 'information on controlling the collection of cyclic garbage. ' |
| 6647 | 'Other\n' |
| 6648 | 'implementations act differently and CPython may change. Do not ' |
| 6649 | 'depend\n' |
| 6650 | 'on immediate finalization of objects when they become ' |
| 6651 | 'unreachable (so\n' |
| 6652 | 'you should always close files explicitly).\n' |
| 6653 | '\n' |
| 6654 | "Note that the use of the implementation's tracing or debugging\n" |
| 6655 | 'facilities may keep objects alive that would normally be ' |
| 6656 | 'collectable.\n' |
| 6657 | 'Also note that catching an exception with a ' |
| 6658 | '\'"try"..."except"\'\n' |
| 6659 | 'statement may keep objects alive.\n' |
| 6660 | '\n' |
| 6661 | 'Some objects contain references to "external" resources such as ' |
| 6662 | 'open\n' |
| 6663 | 'files or windows. It is understood that these resources are ' |
| 6664 | 'freed\n' |
| 6665 | 'when the object is garbage-collected, but since garbage ' |
| 6666 | 'collection is\n' |
| 6667 | 'not guaranteed to happen, such objects also provide an explicit ' |
| 6668 | 'way to\n' |
| 6669 | 'release the external resource, usually a "close()" method. ' |
| 6670 | 'Programs\n' |
| 6671 | 'are strongly recommended to explicitly close such objects. ' |
| 6672 | 'The\n' |
| 6673 | '\'"try"..."finally"\' statement and the \'"with"\' statement ' |
| 6674 | 'provide\n' |
| 6675 | 'convenient ways to do this.\n' |
| 6676 | '\n' |
| 6677 | 'Some objects contain references to other objects; these are ' |
| 6678 | 'called\n' |
| 6679 | '*containers*. Examples of containers are tuples, lists and\n' |
| 6680 | "dictionaries. The references are part of a container's value. " |
| 6681 | 'In\n' |
| 6682 | 'most cases, when we talk about the value of a container, we ' |
| 6683 | 'imply the\n' |
| 6684 | 'values, not the identities of the contained objects; however, ' |
| 6685 | 'when we\n' |
| 6686 | 'talk about the mutability of a container, only the identities ' |
| 6687 | 'of the\n' |
| 6688 | 'immediately contained objects are implied. So, if an ' |
| 6689 | 'immutable\n' |
| 6690 | 'container (like a tuple) contains a reference to a mutable ' |
| 6691 | 'object, its\n' |
| 6692 | 'value changes if that mutable object is changed.\n' |
| 6693 | '\n' |
| 6694 | 'Types affect almost all aspects of object behavior. Even the\n' |
| 6695 | 'importance of object identity is affected in some sense: for ' |
| 6696 | 'immutable\n' |
| 6697 | 'types, operations that compute new values may actually return ' |
| 6698 | 'a\n' |
| 6699 | 'reference to any existing object with the same type and value, ' |
| 6700 | 'while\n' |
| 6701 | 'for mutable objects this is not allowed. E.g., after "a = 1; b ' |
| 6702 | '= 1",\n' |
| 6703 | '"a" and "b" may or may not refer to the same object with the ' |
| 6704 | 'value\n' |
| 6705 | 'one, depending on the implementation, but after "c = []; d = ' |
| 6706 | '[]", "c"\n' |
| 6707 | 'and "d" are guaranteed to refer to two different, unique, ' |
| 6708 | 'newly\n' |
| 6709 | 'created empty lists. (Note that "c = d = []" assigns the same ' |
| 6710 | 'object\n' |
| 6711 | 'to both "c" and "d".)\n', |
| 6712 | 'operator-summary': '\n' |
| 6713 | 'Operator precedence\n' |
| 6714 | '*******************\n' |
| 6715 | '\n' |
| 6716 | 'The following table summarizes the operator precedence ' |
| 6717 | 'in Python, from\n' |
| 6718 | 'lowest precedence (least binding) to highest ' |
| 6719 | 'precedence (most\n' |
| 6720 | 'binding). Operators in the same box have the same ' |
| 6721 | 'precedence. Unless\n' |
| 6722 | 'the syntax is explicitly given, operators are binary. ' |
| 6723 | 'Operators in\n' |
| 6724 | 'the same box group left to right (except for ' |
| 6725 | 'exponentiation, which\n' |
| 6726 | 'groups from right to left).\n' |
| 6727 | '\n' |
| 6728 | 'Note that comparisons, membership tests, and identity ' |
| 6729 | 'tests, all have\n' |
| 6730 | 'the same precedence and have a left-to-right chaining ' |
| 6731 | 'feature as\n' |
| 6732 | 'described in the *Comparisons* section.\n' |
| 6733 | '\n' |
| 6734 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6735 | '| Operator | ' |
| 6736 | 'Description |\n' |
| 6737 | '+=================================================+=======================================+\n' |
| 6738 | '| "lambda" | ' |
| 6739 | 'Lambda expression |\n' |
| 6740 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6741 | '| "if" -- "else" | ' |
| 6742 | 'Conditional expression |\n' |
| 6743 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6744 | '| "or" | ' |
| 6745 | 'Boolean OR |\n' |
| 6746 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6747 | '| "and" | ' |
| 6748 | 'Boolean AND |\n' |
| 6749 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6750 | '| "not" "x" | ' |
| 6751 | 'Boolean NOT |\n' |
| 6752 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6753 | '| "in", "not in", "is", "is not", "<", "<=", ">", | ' |
| 6754 | 'Comparisons, including membership |\n' |
| 6755 | '| ">=", "!=", "==" | ' |
| 6756 | 'tests and identity tests |\n' |
| 6757 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6758 | '| "|" | ' |
| 6759 | 'Bitwise OR |\n' |
| 6760 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6761 | '| "^" | ' |
| 6762 | 'Bitwise XOR |\n' |
| 6763 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6764 | '| "&" | ' |
| 6765 | 'Bitwise AND |\n' |
| 6766 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6767 | '| "<<", ">>" | ' |
| 6768 | 'Shifts |\n' |
| 6769 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6770 | '| "+", "-" | ' |
| 6771 | 'Addition and subtraction |\n' |
| 6772 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6773 | '| "*", "@", "/", "//", "%" | ' |
| 6774 | 'Multiplication, matrix multiplication |\n' |
| 6775 | '| | ' |
| 6776 | 'division, remainder [5] |\n' |
| 6777 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6778 | '| "+x", "-x", "~x" | ' |
| 6779 | 'Positive, negative, bitwise NOT |\n' |
| 6780 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6781 | '| "**" | ' |
| 6782 | 'Exponentiation [6] |\n' |
| 6783 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6784 | '| "await" "x" | ' |
| 6785 | 'Await expression |\n' |
| 6786 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6787 | '| "x[index]", "x[index:index]", | ' |
| 6788 | 'Subscription, slicing, call, |\n' |
| 6789 | '| "x(arguments...)", "x.attribute" | ' |
| 6790 | 'attribute reference |\n' |
| 6791 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6792 | '| "(expressions...)", "[expressions...]", "{key: | ' |
| 6793 | 'Binding or tuple display, list |\n' |
| 6794 | '| value...}", "{expressions...}" | ' |
| 6795 | 'display, dictionary display, set |\n' |
| 6796 | '| | ' |
| 6797 | 'display |\n' |
| 6798 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6799 | '\n' |
| 6800 | '-[ Footnotes ]-\n' |
| 6801 | '\n' |
| 6802 | '[1] While "abs(x%y) < abs(y)" is true mathematically, ' |
| 6803 | 'for floats\n' |
| 6804 | ' it may not be true numerically due to roundoff. ' |
| 6805 | 'For example, and\n' |
| 6806 | ' assuming a platform on which a Python float is an ' |
| 6807 | 'IEEE 754 double-\n' |
| 6808 | ' precision number, in order that "-1e-100 % 1e100" ' |
| 6809 | 'have the same\n' |
| 6810 | ' sign as "1e100", the computed result is "-1e-100 + ' |
| 6811 | '1e100", which\n' |
| 6812 | ' is numerically exactly equal to "1e100". The ' |
| 6813 | 'function\n' |
| 6814 | ' "math.fmod()" returns a result whose sign matches ' |
| 6815 | 'the sign of the\n' |
| 6816 | ' first argument instead, and so returns "-1e-100" ' |
| 6817 | 'in this case.\n' |
| 6818 | ' Which approach is more appropriate depends on the ' |
| 6819 | 'application.\n' |
| 6820 | '\n' |
| 6821 | '[2] If x is very close to an exact integer multiple of ' |
| 6822 | "y, it's\n" |
| 6823 | ' possible for "x//y" to be one larger than ' |
| 6824 | '"(x-x%y)//y" due to\n' |
| 6825 | ' rounding. In such cases, Python returns the ' |
| 6826 | 'latter result, in\n' |
| 6827 | ' order to preserve that "divmod(x,y)[0] * y + x % ' |
| 6828 | 'y" be very close\n' |
| 6829 | ' to "x".\n' |
| 6830 | '\n' |
| 6831 | '[3] While comparisons between strings make sense at ' |
| 6832 | 'the byte\n' |
| 6833 | ' level, they may be counter-intuitive to users. ' |
| 6834 | 'For example, the\n' |
| 6835 | ' strings ""\\u00C7"" and ""\\u0043\\u0327"" compare ' |
| 6836 | 'differently, even\n' |
| 6837 | ' though they both represent the same unicode ' |
| 6838 | 'character (LATIN\n' |
| 6839 | ' CAPITAL LETTER C WITH CEDILLA). To compare ' |
| 6840 | 'strings in a human\n' |
| 6841 | ' recognizable way, compare using ' |
| 6842 | '"unicodedata.normalize()".\n' |
| 6843 | '\n' |
| 6844 | '[4] Due to automatic garbage-collection, free lists, ' |
| 6845 | 'and the\n' |
| 6846 | ' dynamic nature of descriptors, you may notice ' |
| 6847 | 'seemingly unusual\n' |
| 6848 | ' behaviour in certain uses of the "is" operator, ' |
| 6849 | 'like those\n' |
| 6850 | ' involving comparisons between instance methods, or ' |
| 6851 | 'constants.\n' |
| 6852 | ' Check their documentation for more info.\n' |
| 6853 | '\n' |
| 6854 | '[5] The "%" operator is also used for string ' |
| 6855 | 'formatting; the same\n' |
| 6856 | ' precedence applies.\n' |
| 6857 | '\n' |
| 6858 | '[6] The power operator "**" binds less tightly than an ' |
| 6859 | 'arithmetic\n' |
| 6860 | ' or bitwise unary operator on its right, that is, ' |
| 6861 | '"2**-1" is "0.5".\n', |
| 6862 | 'pass': '\n' |
| 6863 | 'The "pass" statement\n' |
| 6864 | '********************\n' |
| 6865 | '\n' |
| 6866 | ' pass_stmt ::= "pass"\n' |
| 6867 | '\n' |
| 6868 | '"pass" is a null operation --- when it is executed, nothing ' |
| 6869 | 'happens.\n' |
| 6870 | 'It is useful as a placeholder when a statement is required\n' |
| 6871 | 'syntactically, but no code needs to be executed, for example:\n' |
| 6872 | '\n' |
| 6873 | ' def f(arg): pass # a function that does nothing (yet)\n' |
| 6874 | '\n' |
| 6875 | ' class C: pass # a class with no methods (yet)\n', |
| 6876 | 'power': '\n' |
| 6877 | 'The power operator\n' |
| 6878 | '******************\n' |
| 6879 | '\n' |
| 6880 | 'The power operator binds more tightly than unary operators on ' |
| 6881 | 'its\n' |
| 6882 | 'left; it binds less tightly than unary operators on its right. ' |
| 6883 | 'The\n' |
| 6884 | 'syntax is:\n' |
| 6885 | '\n' |
| 6886 | ' power ::= await ["**" u_expr]\n' |
| 6887 | '\n' |
| 6888 | 'Thus, in an unparenthesized sequence of power and unary ' |
| 6889 | 'operators, the\n' |
| 6890 | 'operators are evaluated from right to left (this does not ' |
| 6891 | 'constrain\n' |
| 6892 | 'the evaluation order for the operands): "-1**2" results in "-1".\n' |
| 6893 | '\n' |
| 6894 | 'The power operator has the same semantics as the built-in ' |
| 6895 | '"pow()"\n' |
| 6896 | 'function, when called with two arguments: it yields its left ' |
| 6897 | 'argument\n' |
| 6898 | 'raised to the power of its right argument. The numeric arguments ' |
| 6899 | 'are\n' |
| 6900 | 'first converted to a common type, and the result is of that ' |
| 6901 | 'type.\n' |
| 6902 | '\n' |
| 6903 | 'For int operands, the result has the same type as the operands ' |
| 6904 | 'unless\n' |
| 6905 | 'the second argument is negative; in that case, all arguments are\n' |
| 6906 | 'converted to float and a float result is delivered. For example,\n' |
| 6907 | '"10**2" returns "100", but "10**-2" returns "0.01".\n' |
| 6908 | '\n' |
| 6909 | 'Raising "0.0" to a negative power results in a ' |
| 6910 | '"ZeroDivisionError".\n' |
| 6911 | 'Raising a negative number to a fractional power results in a ' |
| 6912 | '"complex"\n' |
| 6913 | 'number. (In earlier versions it raised a "ValueError".)\n', |
| 6914 | 'raise': '\n' |
| 6915 | 'The "raise" statement\n' |
| 6916 | '*********************\n' |
| 6917 | '\n' |
| 6918 | ' raise_stmt ::= "raise" [expression ["from" expression]]\n' |
| 6919 | '\n' |
| 6920 | 'If no expressions are present, "raise" re-raises the last ' |
| 6921 | 'exception\n' |
| 6922 | 'that was active in the current scope. If no exception is active ' |
| 6923 | 'in\n' |
| 6924 | 'the current scope, a "RuntimeError" exception is raised ' |
| 6925 | 'indicating\n' |
| 6926 | 'that this is an error.\n' |
| 6927 | '\n' |
| 6928 | 'Otherwise, "raise" evaluates the first expression as the ' |
| 6929 | 'exception\n' |
| 6930 | 'object. It must be either a subclass or an instance of\n' |
| 6931 | '"BaseException". If it is a class, the exception instance will ' |
| 6932 | 'be\n' |
| 6933 | 'obtained when needed by instantiating the class with no ' |
| 6934 | 'arguments.\n' |
| 6935 | '\n' |
| 6936 | "The *type* of the exception is the exception instance's class, " |
| 6937 | 'the\n' |
| 6938 | '*value* is the instance itself.\n' |
| 6939 | '\n' |
| 6940 | 'A traceback object is normally created automatically when an ' |
| 6941 | 'exception\n' |
| 6942 | 'is raised and attached to it as the "__traceback__" attribute, ' |
| 6943 | 'which\n' |
| 6944 | 'is writable. You can create an exception and set your own ' |
| 6945 | 'traceback in\n' |
| 6946 | 'one step using the "with_traceback()" exception method (which ' |
| 6947 | 'returns\n' |
| 6948 | 'the same exception instance, with its traceback set to its ' |
| 6949 | 'argument),\n' |
| 6950 | 'like so:\n' |
| 6951 | '\n' |
| 6952 | ' raise Exception("foo occurred").with_traceback(tracebackobj)\n' |
| 6953 | '\n' |
| 6954 | 'The "from" clause is used for exception chaining: if given, the ' |
| 6955 | 'second\n' |
| 6956 | '*expression* must be another exception class or instance, which ' |
| 6957 | 'will\n' |
| 6958 | 'then be attached to the raised exception as the "__cause__" ' |
| 6959 | 'attribute\n' |
| 6960 | '(which is writable). If the raised exception is not handled, ' |
| 6961 | 'both\n' |
| 6962 | 'exceptions will be printed:\n' |
| 6963 | '\n' |
| 6964 | ' >>> try:\n' |
| 6965 | ' ... print(1 / 0)\n' |
| 6966 | ' ... except Exception as exc:\n' |
| 6967 | ' ... raise RuntimeError("Something bad happened") from exc\n' |
| 6968 | ' ...\n' |
| 6969 | ' Traceback (most recent call last):\n' |
| 6970 | ' File "<stdin>", line 2, in <module>\n' |
| 6971 | ' ZeroDivisionError: int division or modulo by zero\n' |
| 6972 | '\n' |
| 6973 | ' The above exception was the direct cause of the following ' |
| 6974 | 'exception:\n' |
| 6975 | '\n' |
| 6976 | ' Traceback (most recent call last):\n' |
| 6977 | ' File "<stdin>", line 4, in <module>\n' |
| 6978 | ' RuntimeError: Something bad happened\n' |
| 6979 | '\n' |
| 6980 | 'A similar mechanism works implicitly if an exception is raised ' |
| 6981 | 'inside\n' |
| 6982 | 'an exception handler or a "finally" clause: the previous ' |
| 6983 | 'exception is\n' |
| 6984 | 'then attached as the new exception\'s "__context__" attribute:\n' |
| 6985 | '\n' |
| 6986 | ' >>> try:\n' |
| 6987 | ' ... print(1 / 0)\n' |
| 6988 | ' ... except:\n' |
| 6989 | ' ... raise RuntimeError("Something bad happened")\n' |
| 6990 | ' ...\n' |
| 6991 | ' Traceback (most recent call last):\n' |
| 6992 | ' File "<stdin>", line 2, in <module>\n' |
| 6993 | ' ZeroDivisionError: int division or modulo by zero\n' |
| 6994 | '\n' |
| 6995 | ' During handling of the above exception, another exception ' |
| 6996 | 'occurred:\n' |
| 6997 | '\n' |
| 6998 | ' Traceback (most recent call last):\n' |
| 6999 | ' File "<stdin>", line 4, in <module>\n' |
| 7000 | ' RuntimeError: Something bad happened\n' |
| 7001 | '\n' |
| 7002 | 'Additional information on exceptions can be found in section\n' |
| 7003 | '*Exceptions*, and information about handling exceptions is in ' |
| 7004 | 'section\n' |
| 7005 | '*The try statement*.\n', |
| 7006 | 'return': '\n' |
| 7007 | 'The "return" statement\n' |
| 7008 | '**********************\n' |
| 7009 | '\n' |
| 7010 | ' return_stmt ::= "return" [expression_list]\n' |
| 7011 | '\n' |
| 7012 | '"return" may only occur syntactically nested in a function ' |
| 7013 | 'definition,\n' |
| 7014 | 'not within a nested class definition.\n' |
| 7015 | '\n' |
| 7016 | 'If an expression list is present, it is evaluated, else "None" ' |
| 7017 | 'is\n' |
| 7018 | 'substituted.\n' |
| 7019 | '\n' |
| 7020 | '"return" leaves the current function call with the expression ' |
| 7021 | 'list (or\n' |
| 7022 | '"None") as return value.\n' |
| 7023 | '\n' |
| 7024 | 'When "return" passes control out of a "try" statement with a ' |
| 7025 | '"finally"\n' |
| 7026 | 'clause, that "finally" clause is executed before really leaving ' |
| 7027 | 'the\n' |
| 7028 | 'function.\n' |
| 7029 | '\n' |
| 7030 | 'In a generator function, the "return" statement indicates that ' |
| 7031 | 'the\n' |
| 7032 | 'generator is done and will cause "StopIteration" to be raised. ' |
| 7033 | 'The\n' |
| 7034 | 'returned value (if any) is used as an argument to construct\n' |
| 7035 | '"StopIteration" and becomes the "StopIteration.value" ' |
| 7036 | 'attribute.\n', |
| 7037 | 'sequence-types': '\n' |
| 7038 | 'Emulating container types\n' |
| 7039 | '*************************\n' |
| 7040 | '\n' |
| 7041 | 'The following methods can be defined to implement ' |
| 7042 | 'container objects.\n' |
| 7043 | 'Containers usually are sequences (such as lists or ' |
| 7044 | 'tuples) or mappings\n' |
| 7045 | '(like dictionaries), but can represent other containers ' |
| 7046 | 'as well. The\n' |
| 7047 | 'first set of methods is used either to emulate a ' |
| 7048 | 'sequence or to\n' |
| 7049 | 'emulate a mapping; the difference is that for a ' |
| 7050 | 'sequence, the\n' |
| 7051 | 'allowable keys should be the integers *k* for which "0 ' |
| 7052 | '<= k < N" where\n' |
| 7053 | '*N* is the length of the sequence, or slice objects, ' |
| 7054 | 'which define a\n' |
| 7055 | 'range of items. It is also recommended that mappings ' |
| 7056 | 'provide the\n' |
| 7057 | 'methods "keys()", "values()", "items()", "get()", ' |
| 7058 | '"clear()",\n' |
| 7059 | '"setdefault()", "pop()", "popitem()", "copy()", and ' |
| 7060 | '"update()"\n' |
| 7061 | "behaving similar to those for Python's standard " |
| 7062 | 'dictionary objects.\n' |
| 7063 | 'The "collections" module provides a "MutableMapping" ' |
| 7064 | 'abstract base\n' |
| 7065 | 'class to help create those methods from a base set of ' |
| 7066 | '"__getitem__()",\n' |
| 7067 | '"__setitem__()", "__delitem__()", and "keys()". Mutable ' |
| 7068 | 'sequences\n' |
| 7069 | 'should provide methods "append()", "count()", "index()", ' |
| 7070 | '"extend()",\n' |
| 7071 | '"insert()", "pop()", "remove()", "reverse()" and ' |
| 7072 | '"sort()", like Python\n' |
| 7073 | 'standard list objects. Finally, sequence types should ' |
| 7074 | 'implement\n' |
| 7075 | 'addition (meaning concatenation) and multiplication ' |
| 7076 | '(meaning\n' |
| 7077 | 'repetition) by defining the methods "__add__()", ' |
| 7078 | '"__radd__()",\n' |
| 7079 | '"__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" ' |
| 7080 | 'described\n' |
| 7081 | 'below; they should not define other numerical ' |
| 7082 | 'operators. It is\n' |
| 7083 | 'recommended that both mappings and sequences implement ' |
| 7084 | 'the\n' |
| 7085 | '"__contains__()" method to allow efficient use of the ' |
| 7086 | '"in" operator;\n' |
| 7087 | 'for mappings, "in" should search the mapping\'s keys; ' |
| 7088 | 'for sequences, it\n' |
| 7089 | 'should search through the values. It is further ' |
| 7090 | 'recommended that both\n' |
| 7091 | 'mappings and sequences implement the "__iter__()" method ' |
| 7092 | 'to allow\n' |
| 7093 | 'efficient iteration through the container; for mappings, ' |
| 7094 | '"__iter__()"\n' |
| 7095 | 'should be the same as "keys()"; for sequences, it should ' |
| 7096 | 'iterate\n' |
| 7097 | 'through the values.\n' |
| 7098 | '\n' |
| 7099 | 'object.__len__(self)\n' |
| 7100 | '\n' |
| 7101 | ' Called to implement the built-in function "len()". ' |
| 7102 | 'Should return\n' |
| 7103 | ' the length of the object, an integer ">=" 0. Also, ' |
| 7104 | 'an object that\n' |
| 7105 | ' doesn\'t define a "__bool__()" method and whose ' |
| 7106 | '"__len__()" method\n' |
| 7107 | ' returns zero is considered to be false in a Boolean ' |
| 7108 | 'context.\n' |
| 7109 | '\n' |
| 7110 | 'object.__length_hint__(self)\n' |
| 7111 | '\n' |
| 7112 | ' Called to implement "operator.length_hint()". Should ' |
| 7113 | 'return an\n' |
| 7114 | ' estimated length for the object (which may be greater ' |
| 7115 | 'or less than\n' |
| 7116 | ' the actual length). The length must be an integer ' |
| 7117 | '">=" 0. This\n' |
| 7118 | ' method is purely an optimization and is never ' |
| 7119 | 'required for\n' |
| 7120 | ' correctness.\n' |
| 7121 | '\n' |
| 7122 | ' New in version 3.4.\n' |
| 7123 | '\n' |
| 7124 | 'Note: Slicing is done exclusively with the following ' |
| 7125 | 'three methods.\n' |
| 7126 | ' A call like\n' |
| 7127 | '\n' |
| 7128 | ' a[1:2] = b\n' |
| 7129 | '\n' |
| 7130 | ' is translated to\n' |
| 7131 | '\n' |
| 7132 | ' a[slice(1, 2, None)] = b\n' |
| 7133 | '\n' |
| 7134 | ' and so forth. Missing slice items are always filled ' |
| 7135 | 'in with "None".\n' |
| 7136 | '\n' |
| 7137 | 'object.__getitem__(self, key)\n' |
| 7138 | '\n' |
| 7139 | ' Called to implement evaluation of "self[key]". For ' |
| 7140 | 'sequence types,\n' |
| 7141 | ' the accepted keys should be integers and slice ' |
| 7142 | 'objects. Note that\n' |
| 7143 | ' the special interpretation of negative indexes (if ' |
| 7144 | 'the class wishes\n' |
| 7145 | ' to emulate a sequence type) is up to the ' |
| 7146 | '"__getitem__()" method. If\n' |
| 7147 | ' *key* is of an inappropriate type, "TypeError" may be ' |
| 7148 | 'raised; if of\n' |
| 7149 | ' a value outside the set of indexes for the sequence ' |
| 7150 | '(after any\n' |
| 7151 | ' special interpretation of negative values), ' |
| 7152 | '"IndexError" should be\n' |
| 7153 | ' raised. For mapping types, if *key* is missing (not ' |
| 7154 | 'in the\n' |
| 7155 | ' container), "KeyError" should be raised.\n' |
| 7156 | '\n' |
| 7157 | ' Note: "for" loops expect that an "IndexError" will be ' |
| 7158 | 'raised for\n' |
| 7159 | ' illegal indexes to allow proper detection of the ' |
| 7160 | 'end of the\n' |
| 7161 | ' sequence.\n' |
| 7162 | '\n' |
| 7163 | 'object.__missing__(self, key)\n' |
| 7164 | '\n' |
| 7165 | ' Called by "dict"."__getitem__()" to implement ' |
| 7166 | '"self[key]" for dict\n' |
| 7167 | ' subclasses when key is not in the dictionary.\n' |
| 7168 | '\n' |
| 7169 | 'object.__setitem__(self, key, value)\n' |
| 7170 | '\n' |
| 7171 | ' Called to implement assignment to "self[key]". Same ' |
| 7172 | 'note as for\n' |
| 7173 | ' "__getitem__()". This should only be implemented for ' |
| 7174 | 'mappings if\n' |
| 7175 | ' the objects support changes to the values for keys, ' |
| 7176 | 'or if new keys\n' |
| 7177 | ' can be added, or for sequences if elements can be ' |
| 7178 | 'replaced. The\n' |
| 7179 | ' same exceptions should be raised for improper *key* ' |
| 7180 | 'values as for\n' |
| 7181 | ' the "__getitem__()" method.\n' |
| 7182 | '\n' |
| 7183 | 'object.__delitem__(self, key)\n' |
| 7184 | '\n' |
| 7185 | ' Called to implement deletion of "self[key]". Same ' |
| 7186 | 'note as for\n' |
| 7187 | ' "__getitem__()". This should only be implemented for ' |
| 7188 | 'mappings if\n' |
| 7189 | ' the objects support removal of keys, or for sequences ' |
| 7190 | 'if elements\n' |
| 7191 | ' can be removed from the sequence. The same ' |
| 7192 | 'exceptions should be\n' |
| 7193 | ' raised for improper *key* values as for the ' |
| 7194 | '"__getitem__()" method.\n' |
| 7195 | '\n' |
| 7196 | 'object.__iter__(self)\n' |
| 7197 | '\n' |
| 7198 | ' This method is called when an iterator is required ' |
| 7199 | 'for a container.\n' |
| 7200 | ' This method should return a new iterator object that ' |
| 7201 | 'can iterate\n' |
| 7202 | ' over all the objects in the container. For mappings, ' |
| 7203 | 'it should\n' |
| 7204 | ' iterate over the keys of the container.\n' |
| 7205 | '\n' |
| 7206 | ' Iterator objects also need to implement this method; ' |
| 7207 | 'they are\n' |
| 7208 | ' required to return themselves. For more information ' |
| 7209 | 'on iterator\n' |
| 7210 | ' objects, see *Iterator Types*.\n' |
| 7211 | '\n' |
| 7212 | 'object.__reversed__(self)\n' |
| 7213 | '\n' |
| 7214 | ' Called (if present) by the "reversed()" built-in to ' |
| 7215 | 'implement\n' |
| 7216 | ' reverse iteration. It should return a new iterator ' |
| 7217 | 'object that\n' |
| 7218 | ' iterates over all the objects in the container in ' |
| 7219 | 'reverse order.\n' |
| 7220 | '\n' |
| 7221 | ' If the "__reversed__()" method is not provided, the ' |
| 7222 | '"reversed()"\n' |
| 7223 | ' built-in will fall back to using the sequence ' |
| 7224 | 'protocol ("__len__()"\n' |
| 7225 | ' and "__getitem__()"). Objects that support the ' |
| 7226 | 'sequence protocol\n' |
| 7227 | ' should only provide "__reversed__()" if they can ' |
| 7228 | 'provide an\n' |
| 7229 | ' implementation that is more efficient than the one ' |
| 7230 | 'provided by\n' |
| 7231 | ' "reversed()".\n' |
| 7232 | '\n' |
| 7233 | 'The membership test operators ("in" and "not in") are ' |
| 7234 | 'normally\n' |
| 7235 | 'implemented as an iteration through a sequence. ' |
| 7236 | 'However, container\n' |
| 7237 | 'objects can supply the following special method with a ' |
| 7238 | 'more efficient\n' |
| 7239 | 'implementation, which also does not require the object ' |
| 7240 | 'be a sequence.\n' |
| 7241 | '\n' |
| 7242 | 'object.__contains__(self, item)\n' |
| 7243 | '\n' |
| 7244 | ' Called to implement membership test operators. ' |
| 7245 | 'Should return true\n' |
| 7246 | ' if *item* is in *self*, false otherwise. For mapping ' |
| 7247 | 'objects, this\n' |
| 7248 | ' should consider the keys of the mapping rather than ' |
| 7249 | 'the values or\n' |
| 7250 | ' the key-item pairs.\n' |
| 7251 | '\n' |
| 7252 | ' For objects that don\'t define "__contains__()", the ' |
| 7253 | 'membership test\n' |
| 7254 | ' first tries iteration via "__iter__()", then the old ' |
| 7255 | 'sequence\n' |
| 7256 | ' iteration protocol via "__getitem__()", see *this ' |
| 7257 | 'section in the\n' |
| 7258 | ' language reference*.\n', |
| 7259 | 'shifting': '\n' |
| 7260 | 'Shifting operations\n' |
| 7261 | '*******************\n' |
| 7262 | '\n' |
| 7263 | 'The shifting operations have lower priority than the ' |
| 7264 | 'arithmetic\n' |
| 7265 | 'operations:\n' |
| 7266 | '\n' |
| 7267 | ' shift_expr ::= a_expr | shift_expr ( "<<" | ">>" ) a_expr\n' |
| 7268 | '\n' |
| 7269 | 'These operators accept integers as arguments. They shift the ' |
| 7270 | 'first\n' |
| 7271 | 'argument to the left or right by the number of bits given by ' |
| 7272 | 'the\n' |
| 7273 | 'second argument.\n' |
| 7274 | '\n' |
| 7275 | 'A right shift by *n* bits is defined as floor division by ' |
| 7276 | '"pow(2,n)".\n' |
| 7277 | 'A left shift by *n* bits is defined as multiplication with ' |
| 7278 | '"pow(2,n)".\n' |
| 7279 | '\n' |
| 7280 | 'Note: In the current implementation, the right-hand operand ' |
| 7281 | 'is\n' |
| 7282 | ' required to be at most "sys.maxsize". If the right-hand ' |
| 7283 | 'operand is\n' |
| 7284 | ' larger than "sys.maxsize" an "OverflowError" exception is ' |
| 7285 | 'raised.\n', |
| 7286 | 'slicings': '\n' |
| 7287 | 'Slicings\n' |
| 7288 | '********\n' |
| 7289 | '\n' |
| 7290 | 'A slicing selects a range of items in a sequence object (e.g., ' |
| 7291 | 'a\n' |
| 7292 | 'string, tuple or list). Slicings may be used as expressions ' |
| 7293 | 'or as\n' |
| 7294 | 'targets in assignment or "del" statements. The syntax for a ' |
| 7295 | 'slicing:\n' |
| 7296 | '\n' |
| 7297 | ' slicing ::= primary "[" slice_list "]"\n' |
| 7298 | ' slice_list ::= slice_item ("," slice_item)* [","]\n' |
| 7299 | ' slice_item ::= expression | proper_slice\n' |
| 7300 | ' proper_slice ::= [lower_bound] ":" [upper_bound] [ ":" ' |
| 7301 | '[stride] ]\n' |
| 7302 | ' lower_bound ::= expression\n' |
| 7303 | ' upper_bound ::= expression\n' |
| 7304 | ' stride ::= expression\n' |
| 7305 | '\n' |
| 7306 | 'There is ambiguity in the formal syntax here: anything that ' |
| 7307 | 'looks like\n' |
| 7308 | 'an expression list also looks like a slice list, so any ' |
| 7309 | 'subscription\n' |
| 7310 | 'can be interpreted as a slicing. Rather than further ' |
| 7311 | 'complicating the\n' |
| 7312 | 'syntax, this is disambiguated by defining that in this case ' |
| 7313 | 'the\n' |
| 7314 | 'interpretation as a subscription takes priority over the\n' |
| 7315 | 'interpretation as a slicing (this is the case if the slice ' |
| 7316 | 'list\n' |
| 7317 | 'contains no proper slice).\n' |
| 7318 | '\n' |
| 7319 | 'The semantics for a slicing are as follows. The primary is ' |
| 7320 | 'indexed\n' |
| 7321 | '(using the same "__getitem__()" method as normal subscription) ' |
| 7322 | 'with a\n' |
| 7323 | 'key that is constructed from the slice list, as follows. If ' |
| 7324 | 'the slice\n' |
| 7325 | 'list contains at least one comma, the key is a tuple ' |
| 7326 | 'containing the\n' |
| 7327 | 'conversion of the slice items; otherwise, the conversion of ' |
| 7328 | 'the lone\n' |
| 7329 | 'slice item is the key. The conversion of a slice item that is ' |
| 7330 | 'an\n' |
| 7331 | 'expression is that expression. The conversion of a proper ' |
| 7332 | 'slice is a\n' |
| 7333 | 'slice object (see section *The standard type hierarchy*) ' |
| 7334 | 'whose\n' |
| 7335 | '"start", "stop" and "step" attributes are the values of the\n' |
| 7336 | 'expressions given as lower bound, upper bound and stride,\n' |
| 7337 | 'respectively, substituting "None" for missing expressions.\n', |
| 7338 | 'specialattrs': '\n' |
| 7339 | 'Special Attributes\n' |
| 7340 | '******************\n' |
| 7341 | '\n' |
| 7342 | 'The implementation adds a few special read-only attributes ' |
| 7343 | 'to several\n' |
| 7344 | 'object types, where they are relevant. Some of these are ' |
| 7345 | 'not reported\n' |
| 7346 | 'by the "dir()" built-in function.\n' |
| 7347 | '\n' |
| 7348 | 'object.__dict__\n' |
| 7349 | '\n' |
| 7350 | ' A dictionary or other mapping object used to store an ' |
| 7351 | "object's\n" |
| 7352 | ' (writable) attributes.\n' |
| 7353 | '\n' |
| 7354 | 'instance.__class__\n' |
| 7355 | '\n' |
| 7356 | ' The class to which a class instance belongs.\n' |
| 7357 | '\n' |
| 7358 | 'class.__bases__\n' |
| 7359 | '\n' |
| 7360 | ' The tuple of base classes of a class object.\n' |
| 7361 | '\n' |
| 7362 | 'class.__name__\n' |
| 7363 | '\n' |
| 7364 | ' The name of the class or type.\n' |
| 7365 | '\n' |
| 7366 | 'class.__qualname__\n' |
| 7367 | '\n' |
| 7368 | ' The *qualified name* of the class or type.\n' |
| 7369 | '\n' |
| 7370 | ' New in version 3.3.\n' |
| 7371 | '\n' |
| 7372 | 'class.__mro__\n' |
| 7373 | '\n' |
| 7374 | ' This attribute is a tuple of classes that are ' |
| 7375 | 'considered when\n' |
| 7376 | ' looking for base classes during method resolution.\n' |
| 7377 | '\n' |
| 7378 | 'class.mro()\n' |
| 7379 | '\n' |
| 7380 | ' This method can be overridden by a metaclass to ' |
| 7381 | 'customize the\n' |
| 7382 | ' method resolution order for its instances. It is ' |
| 7383 | 'called at class\n' |
| 7384 | ' instantiation, and its result is stored in "__mro__".\n' |
| 7385 | '\n' |
| 7386 | 'class.__subclasses__()\n' |
| 7387 | '\n' |
| 7388 | ' Each class keeps a list of weak references to its ' |
| 7389 | 'immediate\n' |
| 7390 | ' subclasses. This method returns a list of all those ' |
| 7391 | 'references\n' |
| 7392 | ' still alive. Example:\n' |
| 7393 | '\n' |
| 7394 | ' >>> int.__subclasses__()\n' |
| 7395 | " [<class 'bool'>]\n" |
| 7396 | '\n' |
| 7397 | '-[ Footnotes ]-\n' |
| 7398 | '\n' |
| 7399 | '[1] Additional information on these special methods may be ' |
| 7400 | 'found\n' |
| 7401 | ' in the Python Reference Manual (*Basic ' |
| 7402 | 'customization*).\n' |
| 7403 | '\n' |
| 7404 | '[2] As a consequence, the list "[1, 2]" is considered ' |
| 7405 | 'equal to\n' |
| 7406 | ' "[1.0, 2.0]", and similarly for tuples.\n' |
| 7407 | '\n' |
| 7408 | "[3] They must have since the parser can't tell the type of " |
| 7409 | 'the\n' |
| 7410 | ' operands.\n' |
| 7411 | '\n' |
| 7412 | '[4] Cased characters are those with general category ' |
| 7413 | 'property\n' |
| 7414 | ' being one of "Lu" (Letter, uppercase), "Ll" (Letter, ' |
| 7415 | 'lowercase),\n' |
| 7416 | ' or "Lt" (Letter, titlecase).\n' |
| 7417 | '\n' |
| 7418 | '[5] To format only a tuple you should therefore provide a\n' |
| 7419 | ' singleton tuple whose only element is the tuple to be ' |
| 7420 | 'formatted.\n', |
| 7421 | 'specialnames': '\n' |
| 7422 | 'Special method names\n' |
| 7423 | '********************\n' |
| 7424 | '\n' |
| 7425 | 'A class can implement certain operations that are invoked ' |
| 7426 | 'by special\n' |
| 7427 | 'syntax (such as arithmetic operations or subscripting and ' |
| 7428 | 'slicing) by\n' |
| 7429 | "defining methods with special names. This is Python's " |
| 7430 | 'approach to\n' |
| 7431 | '*operator overloading*, allowing classes to define their ' |
| 7432 | 'own behavior\n' |
| 7433 | 'with respect to language operators. For instance, if a ' |
| 7434 | 'class defines\n' |
| 7435 | 'a method named "__getitem__()", and "x" is an instance of ' |
| 7436 | 'this class,\n' |
| 7437 | 'then "x[i]" is roughly equivalent to ' |
| 7438 | '"type(x).__getitem__(x, i)".\n' |
| 7439 | 'Except where mentioned, attempts to execute an operation ' |
| 7440 | 'raise an\n' |
| 7441 | 'exception when no appropriate method is defined ' |
| 7442 | '(typically\n' |
| 7443 | '"AttributeError" or "TypeError").\n' |
| 7444 | '\n' |
| 7445 | 'When implementing a class that emulates any built-in type, ' |
| 7446 | 'it is\n' |
| 7447 | 'important that the emulation only be implemented to the ' |
| 7448 | 'degree that it\n' |
| 7449 | 'makes sense for the object being modelled. For example, ' |
| 7450 | 'some\n' |
| 7451 | 'sequences may work well with retrieval of individual ' |
| 7452 | 'elements, but\n' |
| 7453 | 'extracting a slice may not make sense. (One example of ' |
| 7454 | 'this is the\n' |
| 7455 | '"NodeList" interface in the W3C\'s Document Object ' |
| 7456 | 'Model.)\n' |
| 7457 | '\n' |
| 7458 | '\n' |
| 7459 | 'Basic customization\n' |
| 7460 | '===================\n' |
| 7461 | '\n' |
| 7462 | 'object.__new__(cls[, ...])\n' |
| 7463 | '\n' |
| 7464 | ' Called to create a new instance of class *cls*. ' |
| 7465 | '"__new__()" is a\n' |
| 7466 | ' static method (special-cased so you need not declare it ' |
| 7467 | 'as such)\n' |
| 7468 | ' that takes the class of which an instance was requested ' |
| 7469 | 'as its\n' |
| 7470 | ' first argument. The remaining arguments are those ' |
| 7471 | 'passed to the\n' |
| 7472 | ' object constructor expression (the call to the class). ' |
| 7473 | 'The return\n' |
| 7474 | ' value of "__new__()" should be the new object instance ' |
| 7475 | '(usually an\n' |
| 7476 | ' instance of *cls*).\n' |
| 7477 | '\n' |
| 7478 | ' Typical implementations create a new instance of the ' |
| 7479 | 'class by\n' |
| 7480 | ' invoking the superclass\'s "__new__()" method using\n' |
| 7481 | ' "super(currentclass, cls).__new__(cls[, ...])" with ' |
| 7482 | 'appropriate\n' |
| 7483 | ' arguments and then modifying the newly-created instance ' |
| 7484 | 'as\n' |
| 7485 | ' necessary before returning it.\n' |
| 7486 | '\n' |
| 7487 | ' If "__new__()" returns an instance of *cls*, then the ' |
| 7488 | 'new\n' |
| 7489 | ' instance\'s "__init__()" method will be invoked like\n' |
| 7490 | ' "__init__(self[, ...])", where *self* is the new ' |
| 7491 | 'instance and the\n' |
| 7492 | ' remaining arguments are the same as were passed to ' |
| 7493 | '"__new__()".\n' |
| 7494 | '\n' |
| 7495 | ' If "__new__()" does not return an instance of *cls*, ' |
| 7496 | 'then the new\n' |
| 7497 | ' instance\'s "__init__()" method will not be invoked.\n' |
| 7498 | '\n' |
| 7499 | ' "__new__()" is intended mainly to allow subclasses of ' |
| 7500 | 'immutable\n' |
| 7501 | ' types (like int, str, or tuple) to customize instance ' |
| 7502 | 'creation. It\n' |
| 7503 | ' is also commonly overridden in custom metaclasses in ' |
| 7504 | 'order to\n' |
| 7505 | ' customize class creation.\n' |
| 7506 | '\n' |
| 7507 | 'object.__init__(self[, ...])\n' |
| 7508 | '\n' |
| 7509 | ' Called after the instance has been created (by ' |
| 7510 | '"__new__()"), but\n' |
| 7511 | ' before it is returned to the caller. The arguments are ' |
| 7512 | 'those\n' |
| 7513 | ' passed to the class constructor expression. If a base ' |
| 7514 | 'class has an\n' |
| 7515 | ' "__init__()" method, the derived class\'s "__init__()" ' |
| 7516 | 'method, if\n' |
| 7517 | ' any, must explicitly call it to ensure proper ' |
| 7518 | 'initialization of the\n' |
| 7519 | ' base class part of the instance; for example:\n' |
| 7520 | ' "BaseClass.__init__(self, [args...])".\n' |
| 7521 | '\n' |
| 7522 | ' Because "__new__()" and "__init__()" work together in ' |
| 7523 | 'constructing\n' |
| 7524 | ' objects ("__new__()" to create it, and "__init__()" to ' |
| 7525 | 'customise\n' |
| 7526 | ' it), no non-"None" value may be returned by ' |
| 7527 | '"__init__()"; doing so\n' |
| 7528 | ' will cause a "TypeError" to be raised at runtime.\n' |
| 7529 | '\n' |
| 7530 | 'object.__del__(self)\n' |
| 7531 | '\n' |
| 7532 | ' Called when the instance is about to be destroyed. ' |
| 7533 | 'This is also\n' |
| 7534 | ' called a destructor. If a base class has a "__del__()" ' |
| 7535 | 'method, the\n' |
| 7536 | ' derived class\'s "__del__()" method, if any, must ' |
| 7537 | 'explicitly call it\n' |
| 7538 | ' to ensure proper deletion of the base class part of the ' |
| 7539 | 'instance.\n' |
| 7540 | ' Note that it is possible (though not recommended!) for ' |
| 7541 | 'the\n' |
| 7542 | ' "__del__()" method to postpone destruction of the ' |
| 7543 | 'instance by\n' |
| 7544 | ' creating a new reference to it. It may then be called ' |
| 7545 | 'at a later\n' |
| 7546 | ' time when this new reference is deleted. It is not ' |
| 7547 | 'guaranteed that\n' |
| 7548 | ' "__del__()" methods are called for objects that still ' |
| 7549 | 'exist when\n' |
| 7550 | ' the interpreter exits.\n' |
| 7551 | '\n' |
| 7552 | ' Note: "del x" doesn\'t directly call "x.__del__()" --- ' |
| 7553 | 'the former\n' |
| 7554 | ' decrements the reference count for "x" by one, and ' |
| 7555 | 'the latter is\n' |
| 7556 | ' only called when "x"\'s reference count reaches ' |
| 7557 | 'zero. Some common\n' |
| 7558 | ' situations that may prevent the reference count of an ' |
| 7559 | 'object from\n' |
| 7560 | ' going to zero include: circular references between ' |
| 7561 | 'objects (e.g.,\n' |
| 7562 | ' a doubly-linked list or a tree data structure with ' |
| 7563 | 'parent and\n' |
| 7564 | ' child pointers); a reference to the object on the ' |
| 7565 | 'stack frame of\n' |
| 7566 | ' a function that caught an exception (the traceback ' |
| 7567 | 'stored in\n' |
| 7568 | ' "sys.exc_info()[2]" keeps the stack frame alive); or ' |
| 7569 | 'a reference\n' |
| 7570 | ' to the object on the stack frame that raised an ' |
| 7571 | 'unhandled\n' |
| 7572 | ' exception in interactive mode (the traceback stored ' |
| 7573 | 'in\n' |
| 7574 | ' "sys.last_traceback" keeps the stack frame alive). ' |
| 7575 | 'The first\n' |
| 7576 | ' situation can only be remedied by explicitly breaking ' |
| 7577 | 'the cycles;\n' |
| 7578 | ' the second can be resolved by freeing the reference ' |
| 7579 | 'to the\n' |
| 7580 | ' traceback object when it is no longer useful, and the ' |
| 7581 | 'third can\n' |
| 7582 | ' be resolved by storing "None" in ' |
| 7583 | '"sys.last_traceback". Circular\n' |
| 7584 | ' references which are garbage are detected and cleaned ' |
| 7585 | 'up when the\n' |
| 7586 | " cyclic garbage collector is enabled (it's on by " |
| 7587 | 'default). Refer\n' |
| 7588 | ' to the documentation for the "gc" module for more ' |
| 7589 | 'information\n' |
| 7590 | ' about this topic.\n' |
| 7591 | '\n' |
| 7592 | ' Warning: Due to the precarious circumstances under ' |
| 7593 | 'which\n' |
| 7594 | ' "__del__()" methods are invoked, exceptions that ' |
| 7595 | 'occur during\n' |
| 7596 | ' their execution are ignored, and a warning is printed ' |
| 7597 | 'to\n' |
| 7598 | ' "sys.stderr" instead. Also, when "__del__()" is ' |
| 7599 | 'invoked in\n' |
| 7600 | ' response to a module being deleted (e.g., when ' |
| 7601 | 'execution of the\n' |
| 7602 | ' program is done), other globals referenced by the ' |
| 7603 | '"__del__()"\n' |
| 7604 | ' method may already have been deleted or in the ' |
| 7605 | 'process of being\n' |
| 7606 | ' torn down (e.g. the import machinery shutting down). ' |
| 7607 | 'For this\n' |
| 7608 | ' reason, "__del__()" methods should do the absolute ' |
| 7609 | 'minimum needed\n' |
| 7610 | ' to maintain external invariants. Starting with ' |
| 7611 | 'version 1.5,\n' |
| 7612 | ' Python guarantees that globals whose name begins with ' |
| 7613 | 'a single\n' |
| 7614 | ' underscore are deleted from their module before other ' |
| 7615 | 'globals are\n' |
| 7616 | ' deleted; if no other references to such globals ' |
| 7617 | 'exist, this may\n' |
| 7618 | ' help in assuring that imported modules are still ' |
| 7619 | 'available at the\n' |
| 7620 | ' time when the "__del__()" method is called.\n' |
| 7621 | '\n' |
| 7622 | 'object.__repr__(self)\n' |
| 7623 | '\n' |
| 7624 | ' Called by the "repr()" built-in function to compute the ' |
| 7625 | '"official"\n' |
| 7626 | ' string representation of an object. If at all ' |
| 7627 | 'possible, this\n' |
| 7628 | ' should look like a valid Python expression that could ' |
| 7629 | 'be used to\n' |
| 7630 | ' recreate an object with the same value (given an ' |
| 7631 | 'appropriate\n' |
| 7632 | ' environment). If this is not possible, a string of the ' |
| 7633 | 'form\n' |
| 7634 | ' "<...some useful description...>" should be returned. ' |
| 7635 | 'The return\n' |
| 7636 | ' value must be a string object. If a class defines ' |
| 7637 | '"__repr__()" but\n' |
| 7638 | ' not "__str__()", then "__repr__()" is also used when an ' |
| 7639 | '"informal"\n' |
| 7640 | ' string representation of instances of that class is ' |
| 7641 | 'required.\n' |
| 7642 | '\n' |
| 7643 | ' This is typically used for debugging, so it is ' |
| 7644 | 'important that the\n' |
| 7645 | ' representation is information-rich and unambiguous.\n' |
| 7646 | '\n' |
| 7647 | 'object.__str__(self)\n' |
| 7648 | '\n' |
| 7649 | ' Called by "str(object)" and the built-in functions ' |
| 7650 | '"format()" and\n' |
| 7651 | ' "print()" to compute the "informal" or nicely printable ' |
| 7652 | 'string\n' |
| 7653 | ' representation of an object. The return value must be ' |
| 7654 | 'a *string*\n' |
| 7655 | ' object.\n' |
| 7656 | '\n' |
| 7657 | ' This method differs from "object.__repr__()" in that ' |
| 7658 | 'there is no\n' |
| 7659 | ' expectation that "__str__()" return a valid Python ' |
| 7660 | 'expression: a\n' |
| 7661 | ' more convenient or concise representation can be used.\n' |
| 7662 | '\n' |
| 7663 | ' The default implementation defined by the built-in type ' |
| 7664 | '"object"\n' |
| 7665 | ' calls "object.__repr__()".\n' |
| 7666 | '\n' |
| 7667 | 'object.__bytes__(self)\n' |
| 7668 | '\n' |
| 7669 | ' Called by "bytes()" to compute a byte-string ' |
| 7670 | 'representation of an\n' |
| 7671 | ' object. This should return a "bytes" object.\n' |
| 7672 | '\n' |
| 7673 | 'object.__format__(self, format_spec)\n' |
| 7674 | '\n' |
| 7675 | ' Called by the "format()" built-in function (and by ' |
| 7676 | 'extension, the\n' |
| 7677 | ' "str.format()" method of class "str") to produce a ' |
| 7678 | '"formatted"\n' |
| 7679 | ' string representation of an object. The "format_spec" ' |
| 7680 | 'argument is a\n' |
| 7681 | ' string that contains a description of the formatting ' |
| 7682 | 'options\n' |
| 7683 | ' desired. The interpretation of the "format_spec" ' |
| 7684 | 'argument is up to\n' |
| 7685 | ' the type implementing "__format__()", however most ' |
| 7686 | 'classes will\n' |
| 7687 | ' either delegate formatting to one of the built-in ' |
| 7688 | 'types, or use a\n' |
| 7689 | ' similar formatting option syntax.\n' |
| 7690 | '\n' |
| 7691 | ' See *Format Specification Mini-Language* for a ' |
| 7692 | 'description of the\n' |
| 7693 | ' standard formatting syntax.\n' |
| 7694 | '\n' |
| 7695 | ' The return value must be a string object.\n' |
| 7696 | '\n' |
| 7697 | ' Changed in version 3.4: The __format__ method of ' |
| 7698 | '"object" itself\n' |
| 7699 | ' raises a "TypeError" if passed any non-empty string.\n' |
| 7700 | '\n' |
| 7701 | 'object.__lt__(self, other)\n' |
| 7702 | 'object.__le__(self, other)\n' |
| 7703 | 'object.__eq__(self, other)\n' |
| 7704 | 'object.__ne__(self, other)\n' |
| 7705 | 'object.__gt__(self, other)\n' |
| 7706 | 'object.__ge__(self, other)\n' |
| 7707 | '\n' |
| 7708 | ' These are the so-called "rich comparison" methods. The\n' |
| 7709 | ' correspondence between operator symbols and method ' |
| 7710 | 'names is as\n' |
| 7711 | ' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls ' |
| 7712 | '"x.__le__(y)",\n' |
| 7713 | ' "x==y" calls "x.__eq__(y)", "x!=y" calls "x.__ne__(y)", ' |
| 7714 | '"x>y" calls\n' |
| 7715 | ' "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".\n' |
| 7716 | '\n' |
| 7717 | ' A rich comparison method may return the singleton ' |
| 7718 | '"NotImplemented"\n' |
| 7719 | ' if it does not implement the operation for a given pair ' |
| 7720 | 'of\n' |
| 7721 | ' arguments. By convention, "False" and "True" are ' |
| 7722 | 'returned for a\n' |
| 7723 | ' successful comparison. However, these methods can ' |
| 7724 | 'return any value,\n' |
| 7725 | ' so if the comparison operator is used in a Boolean ' |
| 7726 | 'context (e.g.,\n' |
| 7727 | ' in the condition of an "if" statement), Python will ' |
| 7728 | 'call "bool()"\n' |
| 7729 | ' on the value to determine if the result is true or ' |
| 7730 | 'false.\n' |
| 7731 | '\n' |
| 7732 | ' By default, "__ne__()" delegates to "__eq__()" and ' |
| 7733 | 'inverts the\n' |
| 7734 | ' result unless it is "NotImplemented". There are no ' |
| 7735 | 'other implied\n' |
| 7736 | ' relationships among the comparison operators, for ' |
| 7737 | 'example, the\n' |
| 7738 | ' truth of "(x<y or x==y)" does not imply "x<=y". To ' |
| 7739 | 'automatically\n' |
| 7740 | ' generate ordering operations from a single root ' |
| 7741 | 'operation, see\n' |
| 7742 | ' "functools.total_ordering()".\n' |
| 7743 | '\n' |
| 7744 | ' See the paragraph on "__hash__()" for some important ' |
| 7745 | 'notes on\n' |
| 7746 | ' creating *hashable* objects which support custom ' |
| 7747 | 'comparison\n' |
| 7748 | ' operations and are usable as dictionary keys.\n' |
| 7749 | '\n' |
| 7750 | ' There are no swapped-argument versions of these methods ' |
| 7751 | '(to be used\n' |
| 7752 | ' when the left argument does not support the operation ' |
| 7753 | 'but the right\n' |
| 7754 | ' argument does); rather, "__lt__()" and "__gt__()" are ' |
| 7755 | "each other's\n" |
| 7756 | ' reflection, "__le__()" and "__ge__()" are each other\'s ' |
| 7757 | 'reflection,\n' |
| 7758 | ' and "__eq__()" and "__ne__()" are their own reflection. ' |
| 7759 | 'If the\n' |
| 7760 | " operands are of different types, and right operand's " |
| 7761 | 'type is a\n' |
| 7762 | " direct or indirect subclass of the left operand's type, " |
| 7763 | 'the\n' |
| 7764 | ' reflected method of the right operand has priority, ' |
| 7765 | 'otherwise the\n' |
| 7766 | " left operand's method has priority. Virtual " |
| 7767 | 'subclassing is not\n' |
| 7768 | ' considered.\n' |
| 7769 | '\n' |
| 7770 | 'object.__hash__(self)\n' |
| 7771 | '\n' |
| 7772 | ' Called by built-in function "hash()" and for operations ' |
| 7773 | 'on members\n' |
| 7774 | ' of hashed collections including "set", "frozenset", and ' |
| 7775 | '"dict".\n' |
| 7776 | ' "__hash__()" should return an integer. The only ' |
| 7777 | 'required property\n' |
| 7778 | ' is that objects which compare equal have the same hash ' |
| 7779 | 'value; it is\n' |
| 7780 | ' advised to somehow mix together (e.g. using exclusive ' |
| 7781 | 'or) the hash\n' |
| 7782 | ' values for the components of the object that also play ' |
| 7783 | 'a part in\n' |
| 7784 | ' comparison of objects.\n' |
| 7785 | '\n' |
| 7786 | ' Note: "hash()" truncates the value returned from an ' |
| 7787 | "object's\n" |
| 7788 | ' custom "__hash__()" method to the size of a ' |
| 7789 | '"Py_ssize_t". This\n' |
| 7790 | ' is typically 8 bytes on 64-bit builds and 4 bytes on ' |
| 7791 | '32-bit\n' |
| 7792 | ' builds. If an object\'s "__hash__()" must ' |
| 7793 | 'interoperate on builds\n' |
| 7794 | ' of different bit sizes, be sure to check the width on ' |
| 7795 | 'all\n' |
| 7796 | ' supported builds. An easy way to do this is with ' |
| 7797 | '"python -c\n' |
| 7798 | ' "import sys; print(sys.hash_info.width)""\n' |
| 7799 | '\n' |
| 7800 | ' If a class does not define an "__eq__()" method it ' |
| 7801 | 'should not\n' |
| 7802 | ' define a "__hash__()" operation either; if it defines ' |
| 7803 | '"__eq__()"\n' |
| 7804 | ' but not "__hash__()", its instances will not be usable ' |
| 7805 | 'as items in\n' |
| 7806 | ' hashable collections. If a class defines mutable ' |
| 7807 | 'objects and\n' |
| 7808 | ' implements an "__eq__()" method, it should not ' |
| 7809 | 'implement\n' |
| 7810 | ' "__hash__()", since the implementation of hashable ' |
| 7811 | 'collections\n' |
| 7812 | " requires that a key's hash value is immutable (if the " |
| 7813 | "object's hash\n" |
| 7814 | ' value changes, it will be in the wrong hash bucket).\n' |
| 7815 | '\n' |
| 7816 | ' User-defined classes have "__eq__()" and "__hash__()" ' |
| 7817 | 'methods by\n' |
| 7818 | ' default; with them, all objects compare unequal (except ' |
| 7819 | 'with\n' |
| 7820 | ' themselves) and "x.__hash__()" returns an appropriate ' |
| 7821 | 'value such\n' |
| 7822 | ' that "x == y" implies both that "x is y" and "hash(x) ' |
| 7823 | '== hash(y)".\n' |
| 7824 | '\n' |
| 7825 | ' A class that overrides "__eq__()" and does not define ' |
| 7826 | '"__hash__()"\n' |
| 7827 | ' will have its "__hash__()" implicitly set to "None". ' |
| 7828 | 'When the\n' |
| 7829 | ' "__hash__()" method of a class is "None", instances of ' |
| 7830 | 'the class\n' |
| 7831 | ' will raise an appropriate "TypeError" when a program ' |
| 7832 | 'attempts to\n' |
| 7833 | ' retrieve their hash value, and will also be correctly ' |
| 7834 | 'identified as\n' |
| 7835 | ' unhashable when checking "isinstance(obj, ' |
| 7836 | 'collections.Hashable").\n' |
| 7837 | '\n' |
| 7838 | ' If a class that overrides "__eq__()" needs to retain ' |
| 7839 | 'the\n' |
| 7840 | ' implementation of "__hash__()" from a parent class, the ' |
| 7841 | 'interpreter\n' |
| 7842 | ' must be told this explicitly by setting "__hash__ =\n' |
| 7843 | ' <ParentClass>.__hash__".\n' |
| 7844 | '\n' |
| 7845 | ' If a class that does not override "__eq__()" wishes to ' |
| 7846 | 'suppress\n' |
| 7847 | ' hash support, it should include "__hash__ = None" in ' |
| 7848 | 'the class\n' |
| 7849 | ' definition. A class which defines its own "__hash__()" ' |
| 7850 | 'that\n' |
| 7851 | ' explicitly raises a "TypeError" would be incorrectly ' |
| 7852 | 'identified as\n' |
| 7853 | ' hashable by an "isinstance(obj, collections.Hashable)" ' |
| 7854 | 'call.\n' |
| 7855 | '\n' |
| 7856 | ' Note: By default, the "__hash__()" values of str, bytes ' |
| 7857 | 'and\n' |
| 7858 | ' datetime objects are "salted" with an unpredictable ' |
| 7859 | 'random value.\n' |
| 7860 | ' Although they remain constant within an individual ' |
| 7861 | 'Python\n' |
| 7862 | ' process, they are not predictable between repeated ' |
| 7863 | 'invocations of\n' |
| 7864 | ' Python.This is intended to provide protection against ' |
| 7865 | 'a denial-\n' |
| 7866 | ' of-service caused by carefully-chosen inputs that ' |
| 7867 | 'exploit the\n' |
| 7868 | ' worst case performance of a dict insertion, O(n^2) ' |
| 7869 | 'complexity.\n' |
| 7870 | ' See ' |
| 7871 | 'http://www.ocert.org/advisories/ocert-2011-003.html for\n' |
| 7872 | ' details.Changing hash values affects the iteration ' |
| 7873 | 'order of\n' |
| 7874 | ' dicts, sets and other mappings. Python has never ' |
| 7875 | 'made guarantees\n' |
| 7876 | ' about this ordering (and it typically varies between ' |
| 7877 | '32-bit and\n' |
| 7878 | ' 64-bit builds).See also "PYTHONHASHSEED".\n' |
| 7879 | '\n' |
| 7880 | ' Changed in version 3.3: Hash randomization is enabled ' |
| 7881 | 'by default.\n' |
| 7882 | '\n' |
| 7883 | 'object.__bool__(self)\n' |
| 7884 | '\n' |
| 7885 | ' Called to implement truth value testing and the ' |
| 7886 | 'built-in operation\n' |
| 7887 | ' "bool()"; should return "False" or "True". When this ' |
| 7888 | 'method is not\n' |
| 7889 | ' defined, "__len__()" is called, if it is defined, and ' |
| 7890 | 'the object is\n' |
| 7891 | ' considered true if its result is nonzero. If a class ' |
| 7892 | 'defines\n' |
| 7893 | ' neither "__len__()" nor "__bool__()", all its instances ' |
| 7894 | 'are\n' |
| 7895 | ' considered true.\n' |
| 7896 | '\n' |
| 7897 | '\n' |
| 7898 | 'Customizing attribute access\n' |
| 7899 | '============================\n' |
| 7900 | '\n' |
| 7901 | 'The following methods can be defined to customize the ' |
| 7902 | 'meaning of\n' |
| 7903 | 'attribute access (use of, assignment to, or deletion of ' |
| 7904 | '"x.name") for\n' |
| 7905 | 'class instances.\n' |
| 7906 | '\n' |
| 7907 | 'object.__getattr__(self, name)\n' |
| 7908 | '\n' |
| 7909 | ' Called when an attribute lookup has not found the ' |
| 7910 | 'attribute in the\n' |
| 7911 | ' usual places (i.e. it is not an instance attribute nor ' |
| 7912 | 'is it found\n' |
| 7913 | ' in the class tree for "self"). "name" is the attribute ' |
| 7914 | 'name. This\n' |
| 7915 | ' method should return the (computed) attribute value or ' |
| 7916 | 'raise an\n' |
| 7917 | ' "AttributeError" exception.\n' |
| 7918 | '\n' |
| 7919 | ' Note that if the attribute is found through the normal ' |
| 7920 | 'mechanism,\n' |
| 7921 | ' "__getattr__()" is not called. (This is an intentional ' |
| 7922 | 'asymmetry\n' |
| 7923 | ' between "__getattr__()" and "__setattr__()".) This is ' |
| 7924 | 'done both for\n' |
| 7925 | ' efficiency reasons and because otherwise ' |
| 7926 | '"__getattr__()" would have\n' |
| 7927 | ' no way to access other attributes of the instance. ' |
| 7928 | 'Note that at\n' |
| 7929 | ' least for instance variables, you can fake total ' |
| 7930 | 'control by not\n' |
| 7931 | ' inserting any values in the instance attribute ' |
| 7932 | 'dictionary (but\n' |
| 7933 | ' instead inserting them in another object). See the\n' |
| 7934 | ' "__getattribute__()" method below for a way to actually ' |
| 7935 | 'get total\n' |
| 7936 | ' control over attribute access.\n' |
| 7937 | '\n' |
| 7938 | 'object.__getattribute__(self, name)\n' |
| 7939 | '\n' |
| 7940 | ' Called unconditionally to implement attribute accesses ' |
| 7941 | 'for\n' |
| 7942 | ' instances of the class. If the class also defines ' |
| 7943 | '"__getattr__()",\n' |
| 7944 | ' the latter will not be called unless ' |
| 7945 | '"__getattribute__()" either\n' |
| 7946 | ' calls it explicitly or raises an "AttributeError". This ' |
| 7947 | 'method\n' |
| 7948 | ' should return the (computed) attribute value or raise ' |
| 7949 | 'an\n' |
| 7950 | ' "AttributeError" exception. In order to avoid infinite ' |
| 7951 | 'recursion in\n' |
| 7952 | ' this method, its implementation should always call the ' |
| 7953 | 'base class\n' |
| 7954 | ' method with the same name to access any attributes it ' |
| 7955 | 'needs, for\n' |
| 7956 | ' example, "object.__getattribute__(self, name)".\n' |
| 7957 | '\n' |
| 7958 | ' Note: This method may still be bypassed when looking up ' |
| 7959 | 'special\n' |
| 7960 | ' methods as the result of implicit invocation via ' |
| 7961 | 'language syntax\n' |
| 7962 | ' or built-in functions. See *Special method lookup*.\n' |
| 7963 | '\n' |
| 7964 | 'object.__setattr__(self, name, value)\n' |
| 7965 | '\n' |
| 7966 | ' Called when an attribute assignment is attempted. This ' |
| 7967 | 'is called\n' |
| 7968 | ' instead of the normal mechanism (i.e. store the value ' |
| 7969 | 'in the\n' |
| 7970 | ' instance dictionary). *name* is the attribute name, ' |
| 7971 | '*value* is the\n' |
| 7972 | ' value to be assigned to it.\n' |
| 7973 | '\n' |
| 7974 | ' If "__setattr__()" wants to assign to an instance ' |
| 7975 | 'attribute, it\n' |
| 7976 | ' should call the base class method with the same name, ' |
| 7977 | 'for example,\n' |
| 7978 | ' "object.__setattr__(self, name, value)".\n' |
| 7979 | '\n' |
| 7980 | 'object.__delattr__(self, name)\n' |
| 7981 | '\n' |
| 7982 | ' Like "__setattr__()" but for attribute deletion instead ' |
| 7983 | 'of\n' |
| 7984 | ' assignment. This should only be implemented if "del ' |
| 7985 | 'obj.name" is\n' |
| 7986 | ' meaningful for the object.\n' |
| 7987 | '\n' |
| 7988 | 'object.__dir__(self)\n' |
| 7989 | '\n' |
| 7990 | ' Called when "dir()" is called on the object. A sequence ' |
| 7991 | 'must be\n' |
| 7992 | ' returned. "dir()" converts the returned sequence to a ' |
| 7993 | 'list and\n' |
| 7994 | ' sorts it.\n' |
| 7995 | '\n' |
| 7996 | '\n' |
| 7997 | 'Implementing Descriptors\n' |
| 7998 | '------------------------\n' |
| 7999 | '\n' |
| 8000 | 'The following methods only apply when an instance of the ' |
| 8001 | 'class\n' |
| 8002 | 'containing the method (a so-called *descriptor* class) ' |
| 8003 | 'appears in an\n' |
| 8004 | '*owner* class (the descriptor must be in either the ' |
| 8005 | "owner's class\n" |
| 8006 | 'dictionary or in the class dictionary for one of its ' |
| 8007 | 'parents). In the\n' |
| 8008 | 'examples below, "the attribute" refers to the attribute ' |
| 8009 | 'whose name is\n' |
| 8010 | 'the key of the property in the owner class\' "__dict__".\n' |
| 8011 | '\n' |
| 8012 | 'object.__get__(self, instance, owner)\n' |
| 8013 | '\n' |
| 8014 | ' Called to get the attribute of the owner class (class ' |
| 8015 | 'attribute\n' |
| 8016 | ' access) or of an instance of that class (instance ' |
| 8017 | 'attribute\n' |
| 8018 | ' access). *owner* is always the owner class, while ' |
| 8019 | '*instance* is the\n' |
| 8020 | ' instance that the attribute was accessed through, or ' |
| 8021 | '"None" when\n' |
| 8022 | ' the attribute is accessed through the *owner*. This ' |
| 8023 | 'method should\n' |
| 8024 | ' return the (computed) attribute value or raise an ' |
| 8025 | '"AttributeError"\n' |
| 8026 | ' exception.\n' |
| 8027 | '\n' |
| 8028 | 'object.__set__(self, instance, value)\n' |
| 8029 | '\n' |
| 8030 | ' Called to set the attribute on an instance *instance* ' |
| 8031 | 'of the owner\n' |
| 8032 | ' class to a new value, *value*.\n' |
| 8033 | '\n' |
| 8034 | 'object.__delete__(self, instance)\n' |
| 8035 | '\n' |
| 8036 | ' Called to delete the attribute on an instance ' |
| 8037 | '*instance* of the\n' |
| 8038 | ' owner class.\n' |
| 8039 | '\n' |
| 8040 | 'The attribute "__objclass__" is interpreted by the ' |
| 8041 | '"inspect" module as\n' |
| 8042 | 'specifying the class where this object was defined ' |
| 8043 | '(setting this\n' |
| 8044 | 'appropriately can assist in runtime introspection of ' |
| 8045 | 'dynamic class\n' |
| 8046 | 'attributes). For callables, it may indicate that an ' |
| 8047 | 'instance of the\n' |
| 8048 | 'given type (or a subclass) is expected or required as the ' |
| 8049 | 'first\n' |
| 8050 | 'positional argument (for example, CPython sets this ' |
| 8051 | 'attribute for\n' |
| 8052 | 'unbound methods that are implemented in C).\n' |
| 8053 | '\n' |
| 8054 | '\n' |
| 8055 | 'Invoking Descriptors\n' |
| 8056 | '--------------------\n' |
| 8057 | '\n' |
| 8058 | 'In general, a descriptor is an object attribute with ' |
| 8059 | '"binding\n' |
| 8060 | 'behavior", one whose attribute access has been overridden ' |
| 8061 | 'by methods\n' |
| 8062 | 'in the descriptor protocol: "__get__()", "__set__()", ' |
| 8063 | 'and\n' |
| 8064 | '"__delete__()". If any of those methods are defined for an ' |
| 8065 | 'object, it\n' |
| 8066 | 'is said to be a descriptor.\n' |
| 8067 | '\n' |
| 8068 | 'The default behavior for attribute access is to get, set, ' |
| 8069 | 'or delete\n' |
| 8070 | "the attribute from an object's dictionary. For instance, " |
| 8071 | '"a.x" has a\n' |
| 8072 | 'lookup chain starting with "a.__dict__[\'x\']", then\n' |
| 8073 | '"type(a).__dict__[\'x\']", and continuing through the base ' |
| 8074 | 'classes of\n' |
| 8075 | '"type(a)" excluding metaclasses.\n' |
| 8076 | '\n' |
| 8077 | 'However, if the looked-up value is an object defining one ' |
| 8078 | 'of the\n' |
| 8079 | 'descriptor methods, then Python may override the default ' |
| 8080 | 'behavior and\n' |
| 8081 | 'invoke the descriptor method instead. Where this occurs ' |
| 8082 | 'in the\n' |
| 8083 | 'precedence chain depends on which descriptor methods were ' |
| 8084 | 'defined and\n' |
| 8085 | 'how they were called.\n' |
| 8086 | '\n' |
| 8087 | 'The starting point for descriptor invocation is a binding, ' |
| 8088 | '"a.x". How\n' |
| 8089 | 'the arguments are assembled depends on "a":\n' |
| 8090 | '\n' |
| 8091 | 'Direct Call\n' |
| 8092 | ' The simplest and least common call is when user code ' |
| 8093 | 'directly\n' |
| 8094 | ' invokes a descriptor method: "x.__get__(a)".\n' |
| 8095 | '\n' |
| 8096 | 'Instance Binding\n' |
| 8097 | ' If binding to an object instance, "a.x" is transformed ' |
| 8098 | 'into the\n' |
| 8099 | ' call: "type(a).__dict__[\'x\'].__get__(a, type(a))".\n' |
| 8100 | '\n' |
| 8101 | 'Class Binding\n' |
| 8102 | ' If binding to a class, "A.x" is transformed into the ' |
| 8103 | 'call:\n' |
| 8104 | ' "A.__dict__[\'x\'].__get__(None, A)".\n' |
| 8105 | '\n' |
| 8106 | 'Super Binding\n' |
| 8107 | ' If "a" is an instance of "super", then the binding ' |
| 8108 | '"super(B,\n' |
| 8109 | ' obj).m()" searches "obj.__class__.__mro__" for the base ' |
| 8110 | 'class "A"\n' |
| 8111 | ' immediately preceding "B" and then invokes the ' |
| 8112 | 'descriptor with the\n' |
| 8113 | ' call: "A.__dict__[\'m\'].__get__(obj, obj.__class__)".\n' |
| 8114 | '\n' |
| 8115 | 'For instance bindings, the precedence of descriptor ' |
| 8116 | 'invocation depends\n' |
| 8117 | 'on the which descriptor methods are defined. A descriptor ' |
| 8118 | 'can define\n' |
| 8119 | 'any combination of "__get__()", "__set__()" and ' |
| 8120 | '"__delete__()". If it\n' |
| 8121 | 'does not define "__get__()", then accessing the attribute ' |
| 8122 | 'will return\n' |
| 8123 | 'the descriptor object itself unless there is a value in ' |
| 8124 | "the object's\n" |
| 8125 | 'instance dictionary. If the descriptor defines ' |
| 8126 | '"__set__()" and/or\n' |
| 8127 | '"__delete__()", it is a data descriptor; if it defines ' |
| 8128 | 'neither, it is\n' |
| 8129 | 'a non-data descriptor. Normally, data descriptors define ' |
| 8130 | 'both\n' |
| 8131 | '"__get__()" and "__set__()", while non-data descriptors ' |
| 8132 | 'have just the\n' |
| 8133 | '"__get__()" method. Data descriptors with "__set__()" and ' |
| 8134 | '"__get__()"\n' |
| 8135 | 'defined always override a redefinition in an instance ' |
| 8136 | 'dictionary. In\n' |
| 8137 | 'contrast, non-data descriptors can be overridden by ' |
| 8138 | 'instances.\n' |
| 8139 | '\n' |
| 8140 | 'Python methods (including "staticmethod()" and ' |
| 8141 | '"classmethod()") are\n' |
| 8142 | 'implemented as non-data descriptors. Accordingly, ' |
| 8143 | 'instances can\n' |
| 8144 | 'redefine and override methods. This allows individual ' |
| 8145 | 'instances to\n' |
| 8146 | 'acquire behaviors that differ from other instances of the ' |
| 8147 | 'same class.\n' |
| 8148 | '\n' |
| 8149 | 'The "property()" function is implemented as a data ' |
| 8150 | 'descriptor.\n' |
| 8151 | 'Accordingly, instances cannot override the behavior of a ' |
| 8152 | 'property.\n' |
| 8153 | '\n' |
| 8154 | '\n' |
| 8155 | '__slots__\n' |
| 8156 | '---------\n' |
| 8157 | '\n' |
| 8158 | 'By default, instances of classes have a dictionary for ' |
| 8159 | 'attribute\n' |
| 8160 | 'storage. This wastes space for objects having very few ' |
| 8161 | 'instance\n' |
| 8162 | 'variables. The space consumption can become acute when ' |
| 8163 | 'creating large\n' |
| 8164 | 'numbers of instances.\n' |
| 8165 | '\n' |
| 8166 | 'The default can be overridden by defining *__slots__* in a ' |
| 8167 | 'class\n' |
| 8168 | 'definition. The *__slots__* declaration takes a sequence ' |
| 8169 | 'of instance\n' |
| 8170 | 'variables and reserves just enough space in each instance ' |
| 8171 | 'to hold a\n' |
| 8172 | 'value for each variable. Space is saved because ' |
| 8173 | '*__dict__* is not\n' |
| 8174 | 'created for each instance.\n' |
| 8175 | '\n' |
| 8176 | 'object.__slots__\n' |
| 8177 | '\n' |
| 8178 | ' This class variable can be assigned a string, iterable, ' |
| 8179 | 'or sequence\n' |
| 8180 | ' of strings with variable names used by instances. ' |
| 8181 | '*__slots__*\n' |
| 8182 | ' reserves space for the declared variables and prevents ' |
| 8183 | 'the\n' |
| 8184 | ' automatic creation of *__dict__* and *__weakref__* for ' |
| 8185 | 'each\n' |
| 8186 | ' instance.\n' |
| 8187 | '\n' |
| 8188 | '\n' |
| 8189 | 'Notes on using *__slots__*\n' |
| 8190 | '~~~~~~~~~~~~~~~~~~~~~~~~~~\n' |
| 8191 | '\n' |
| 8192 | '* When inheriting from a class without *__slots__*, the ' |
| 8193 | '*__dict__*\n' |
| 8194 | ' attribute of that class will always be accessible, so a ' |
| 8195 | '*__slots__*\n' |
| 8196 | ' definition in the subclass is meaningless.\n' |
| 8197 | '\n' |
| 8198 | '* Without a *__dict__* variable, instances cannot be ' |
| 8199 | 'assigned new\n' |
| 8200 | ' variables not listed in the *__slots__* definition. ' |
| 8201 | 'Attempts to\n' |
| 8202 | ' assign to an unlisted variable name raises ' |
| 8203 | '"AttributeError". If\n' |
| 8204 | ' dynamic assignment of new variables is desired, then ' |
| 8205 | 'add\n' |
| 8206 | ' "\'__dict__\'" to the sequence of strings in the ' |
| 8207 | '*__slots__*\n' |
| 8208 | ' declaration.\n' |
| 8209 | '\n' |
| 8210 | '* Without a *__weakref__* variable for each instance, ' |
| 8211 | 'classes\n' |
| 8212 | ' defining *__slots__* do not support weak references to ' |
| 8213 | 'its\n' |
| 8214 | ' instances. If weak reference support is needed, then ' |
| 8215 | 'add\n' |
| 8216 | ' "\'__weakref__\'" to the sequence of strings in the ' |
| 8217 | '*__slots__*\n' |
| 8218 | ' declaration.\n' |
| 8219 | '\n' |
| 8220 | '* *__slots__* are implemented at the class level by ' |
| 8221 | 'creating\n' |
| 8222 | ' descriptors (*Implementing Descriptors*) for each ' |
| 8223 | 'variable name. As\n' |
| 8224 | ' a result, class attributes cannot be used to set default ' |
| 8225 | 'values for\n' |
| 8226 | ' instance variables defined by *__slots__*; otherwise, ' |
| 8227 | 'the class\n' |
| 8228 | ' attribute would overwrite the descriptor assignment.\n' |
| 8229 | '\n' |
| 8230 | '* The action of a *__slots__* declaration is limited to ' |
| 8231 | 'the class\n' |
| 8232 | ' where it is defined. As a result, subclasses will have ' |
| 8233 | 'a *__dict__*\n' |
| 8234 | ' unless they also define *__slots__* (which must only ' |
| 8235 | 'contain names\n' |
| 8236 | ' of any *additional* slots).\n' |
| 8237 | '\n' |
| 8238 | '* If a class defines a slot also defined in a base class, ' |
| 8239 | 'the\n' |
| 8240 | ' instance variable defined by the base class slot is ' |
| 8241 | 'inaccessible\n' |
| 8242 | ' (except by retrieving its descriptor directly from the ' |
| 8243 | 'base class).\n' |
| 8244 | ' This renders the meaning of the program undefined. In ' |
| 8245 | 'the future, a\n' |
| 8246 | ' check may be added to prevent this.\n' |
| 8247 | '\n' |
| 8248 | '* Nonempty *__slots__* does not work for classes derived ' |
| 8249 | 'from\n' |
| 8250 | ' "variable-length" built-in types such as "int", "bytes" ' |
| 8251 | 'and "tuple".\n' |
| 8252 | '\n' |
| 8253 | '* Any non-string iterable may be assigned to *__slots__*. ' |
| 8254 | 'Mappings\n' |
| 8255 | ' may also be used; however, in the future, special ' |
| 8256 | 'meaning may be\n' |
| 8257 | ' assigned to the values corresponding to each key.\n' |
| 8258 | '\n' |
| 8259 | '* *__class__* assignment works only if both classes have ' |
| 8260 | 'the same\n' |
| 8261 | ' *__slots__*.\n' |
| 8262 | '\n' |
| 8263 | '\n' |
| 8264 | 'Customizing class creation\n' |
| 8265 | '==========================\n' |
| 8266 | '\n' |
| 8267 | 'By default, classes are constructed using "type()". The ' |
| 8268 | 'class body is\n' |
| 8269 | 'executed in a new namespace and the class name is bound ' |
| 8270 | 'locally to the\n' |
| 8271 | 'result of "type(name, bases, namespace)".\n' |
| 8272 | '\n' |
| 8273 | 'The class creation process can be customised by passing ' |
| 8274 | 'the\n' |
| 8275 | '"metaclass" keyword argument in the class definition line, ' |
| 8276 | 'or by\n' |
| 8277 | 'inheriting from an existing class that included such an ' |
| 8278 | 'argument. In\n' |
| 8279 | 'the following example, both "MyClass" and "MySubclass" are ' |
| 8280 | 'instances\n' |
| 8281 | 'of "Meta":\n' |
| 8282 | '\n' |
| 8283 | ' class Meta(type):\n' |
| 8284 | ' pass\n' |
| 8285 | '\n' |
| 8286 | ' class MyClass(metaclass=Meta):\n' |
| 8287 | ' pass\n' |
| 8288 | '\n' |
| 8289 | ' class MySubclass(MyClass):\n' |
| 8290 | ' pass\n' |
| 8291 | '\n' |
| 8292 | 'Any other keyword arguments that are specified in the ' |
| 8293 | 'class definition\n' |
| 8294 | 'are passed through to all metaclass operations described ' |
| 8295 | 'below.\n' |
| 8296 | '\n' |
| 8297 | 'When a class definition is executed, the following steps ' |
| 8298 | 'occur:\n' |
| 8299 | '\n' |
| 8300 | '* the appropriate metaclass is determined\n' |
| 8301 | '\n' |
| 8302 | '* the class namespace is prepared\n' |
| 8303 | '\n' |
| 8304 | '* the class body is executed\n' |
| 8305 | '\n' |
| 8306 | '* the class object is created\n' |
| 8307 | '\n' |
| 8308 | '\n' |
| 8309 | 'Determining the appropriate metaclass\n' |
| 8310 | '-------------------------------------\n' |
| 8311 | '\n' |
| 8312 | 'The appropriate metaclass for a class definition is ' |
| 8313 | 'determined as\n' |
| 8314 | 'follows:\n' |
| 8315 | '\n' |
| 8316 | '* if no bases and no explicit metaclass are given, then ' |
| 8317 | '"type()" is\n' |
| 8318 | ' used\n' |
| 8319 | '\n' |
| 8320 | '* if an explicit metaclass is given and it is *not* an ' |
| 8321 | 'instance of\n' |
| 8322 | ' "type()", then it is used directly as the metaclass\n' |
| 8323 | '\n' |
| 8324 | '* if an instance of "type()" is given as the explicit ' |
| 8325 | 'metaclass, or\n' |
| 8326 | ' bases are defined, then the most derived metaclass is ' |
| 8327 | 'used\n' |
| 8328 | '\n' |
| 8329 | 'The most derived metaclass is selected from the explicitly ' |
| 8330 | 'specified\n' |
| 8331 | 'metaclass (if any) and the metaclasses (i.e. "type(cls)") ' |
| 8332 | 'of all\n' |
| 8333 | 'specified base classes. The most derived metaclass is one ' |
| 8334 | 'which is a\n' |
| 8335 | 'subtype of *all* of these candidate metaclasses. If none ' |
| 8336 | 'of the\n' |
| 8337 | 'candidate metaclasses meets that criterion, then the class ' |
| 8338 | 'definition\n' |
| 8339 | 'will fail with "TypeError".\n' |
| 8340 | '\n' |
| 8341 | '\n' |
| 8342 | 'Preparing the class namespace\n' |
| 8343 | '-----------------------------\n' |
| 8344 | '\n' |
| 8345 | 'Once the appropriate metaclass has been identified, then ' |
| 8346 | 'the class\n' |
| 8347 | 'namespace is prepared. If the metaclass has a ' |
| 8348 | '"__prepare__" attribute,\n' |
| 8349 | 'it is called as "namespace = metaclass.__prepare__(name, ' |
| 8350 | 'bases,\n' |
| 8351 | '**kwds)" (where the additional keyword arguments, if any, ' |
| 8352 | 'come from\n' |
| 8353 | 'the class definition).\n' |
| 8354 | '\n' |
| 8355 | 'If the metaclass has no "__prepare__" attribute, then the ' |
| 8356 | 'class\n' |
| 8357 | 'namespace is initialised as an empty "dict()" instance.\n' |
| 8358 | '\n' |
| 8359 | 'See also: **PEP 3115** - Metaclasses in Python 3000\n' |
| 8360 | '\n' |
| 8361 | ' Introduced the "__prepare__" namespace hook\n' |
| 8362 | '\n' |
| 8363 | '\n' |
| 8364 | 'Executing the class body\n' |
| 8365 | '------------------------\n' |
| 8366 | '\n' |
| 8367 | 'The class body is executed (approximately) as "exec(body, ' |
| 8368 | 'globals(),\n' |
| 8369 | 'namespace)". The key difference from a normal call to ' |
| 8370 | '"exec()" is that\n' |
| 8371 | 'lexical scoping allows the class body (including any ' |
| 8372 | 'methods) to\n' |
| 8373 | 'reference names from the current and outer scopes when the ' |
| 8374 | 'class\n' |
| 8375 | 'definition occurs inside a function.\n' |
| 8376 | '\n' |
| 8377 | 'However, even when the class definition occurs inside the ' |
| 8378 | 'function,\n' |
| 8379 | 'methods defined inside the class still cannot see names ' |
| 8380 | 'defined at the\n' |
| 8381 | 'class scope. Class variables must be accessed through the ' |
| 8382 | 'first\n' |
| 8383 | 'parameter of instance or class methods, and cannot be ' |
| 8384 | 'accessed at all\n' |
| 8385 | 'from static methods.\n' |
| 8386 | '\n' |
| 8387 | '\n' |
| 8388 | 'Creating the class object\n' |
| 8389 | '-------------------------\n' |
| 8390 | '\n' |
| 8391 | 'Once the class namespace has been populated by executing ' |
| 8392 | 'the class\n' |
| 8393 | 'body, the class object is created by calling ' |
| 8394 | '"metaclass(name, bases,\n' |
| 8395 | 'namespace, **kwds)" (the additional keywords passed here ' |
| 8396 | 'are the same\n' |
| 8397 | 'as those passed to "__prepare__").\n' |
| 8398 | '\n' |
| 8399 | 'This class object is the one that will be referenced by ' |
| 8400 | 'the zero-\n' |
| 8401 | 'argument form of "super()". "__class__" is an implicit ' |
| 8402 | 'closure\n' |
| 8403 | 'reference created by the compiler if any methods in a ' |
| 8404 | 'class body refer\n' |
| 8405 | 'to either "__class__" or "super". This allows the zero ' |
| 8406 | 'argument form\n' |
| 8407 | 'of "super()" to correctly identify the class being defined ' |
| 8408 | 'based on\n' |
| 8409 | 'lexical scoping, while the class or instance that was used ' |
| 8410 | 'to make the\n' |
| 8411 | 'current call is identified based on the first argument ' |
| 8412 | 'passed to the\n' |
| 8413 | 'method.\n' |
| 8414 | '\n' |
| 8415 | 'After the class object is created, it is passed to the ' |
| 8416 | 'class\n' |
| 8417 | 'decorators included in the class definition (if any) and ' |
| 8418 | 'the resulting\n' |
| 8419 | 'object is bound in the local namespace as the defined ' |
| 8420 | 'class.\n' |
| 8421 | '\n' |
| 8422 | 'See also: **PEP 3135** - New super\n' |
| 8423 | '\n' |
| 8424 | ' Describes the implicit "__class__" closure reference\n' |
| 8425 | '\n' |
| 8426 | '\n' |
| 8427 | 'Metaclass example\n' |
| 8428 | '-----------------\n' |
| 8429 | '\n' |
| 8430 | 'The potential uses for metaclasses are boundless. Some ' |
| 8431 | 'ideas that have\n' |
| 8432 | 'been explored include logging, interface checking, ' |
| 8433 | 'automatic\n' |
| 8434 | 'delegation, automatic property creation, proxies, ' |
| 8435 | 'frameworks, and\n' |
| 8436 | 'automatic resource locking/synchronization.\n' |
| 8437 | '\n' |
| 8438 | 'Here is an example of a metaclass that uses an\n' |
| 8439 | '"collections.OrderedDict" to remember the order that class ' |
| 8440 | 'variables\n' |
| 8441 | 'are defined:\n' |
| 8442 | '\n' |
| 8443 | ' class OrderedClass(type):\n' |
| 8444 | '\n' |
| 8445 | ' @classmethod\n' |
| 8446 | ' def __prepare__(metacls, name, bases, **kwds):\n' |
| 8447 | ' return collections.OrderedDict()\n' |
| 8448 | '\n' |
| 8449 | ' def __new__(cls, name, bases, namespace, **kwds):\n' |
| 8450 | ' result = type.__new__(cls, name, bases, ' |
| 8451 | 'dict(namespace))\n' |
| 8452 | ' result.members = tuple(namespace)\n' |
| 8453 | ' return result\n' |
| 8454 | '\n' |
| 8455 | ' class A(metaclass=OrderedClass):\n' |
| 8456 | ' def one(self): pass\n' |
| 8457 | ' def two(self): pass\n' |
| 8458 | ' def three(self): pass\n' |
| 8459 | ' def four(self): pass\n' |
| 8460 | '\n' |
| 8461 | ' >>> A.members\n' |
| 8462 | " ('__module__', 'one', 'two', 'three', 'four')\n" |
| 8463 | '\n' |
| 8464 | 'When the class definition for *A* gets executed, the ' |
| 8465 | 'process begins\n' |
| 8466 | 'with calling the metaclass\'s "__prepare__()" method which ' |
| 8467 | 'returns an\n' |
| 8468 | 'empty "collections.OrderedDict". That mapping records the ' |
| 8469 | 'methods and\n' |
| 8470 | 'attributes of *A* as they are defined within the body of ' |
| 8471 | 'the class\n' |
| 8472 | 'statement. Once those definitions are executed, the ' |
| 8473 | 'ordered dictionary\n' |
| 8474 | 'is fully populated and the metaclass\'s "__new__()" method ' |
| 8475 | 'gets\n' |
| 8476 | 'invoked. That method builds the new type and it saves the ' |
| 8477 | 'ordered\n' |
| 8478 | 'dictionary keys in an attribute called "members".\n' |
| 8479 | '\n' |
| 8480 | '\n' |
| 8481 | 'Customizing instance and subclass checks\n' |
| 8482 | '========================================\n' |
| 8483 | '\n' |
| 8484 | 'The following methods are used to override the default ' |
| 8485 | 'behavior of the\n' |
| 8486 | '"isinstance()" and "issubclass()" built-in functions.\n' |
| 8487 | '\n' |
| 8488 | 'In particular, the metaclass "abc.ABCMeta" implements ' |
| 8489 | 'these methods in\n' |
| 8490 | 'order to allow the addition of Abstract Base Classes ' |
| 8491 | '(ABCs) as\n' |
| 8492 | '"virtual base classes" to any class or type (including ' |
| 8493 | 'built-in\n' |
| 8494 | 'types), including other ABCs.\n' |
| 8495 | '\n' |
| 8496 | 'class.__instancecheck__(self, instance)\n' |
| 8497 | '\n' |
| 8498 | ' Return true if *instance* should be considered a ' |
| 8499 | '(direct or\n' |
| 8500 | ' indirect) instance of *class*. If defined, called to ' |
| 8501 | 'implement\n' |
| 8502 | ' "isinstance(instance, class)".\n' |
| 8503 | '\n' |
| 8504 | 'class.__subclasscheck__(self, subclass)\n' |
| 8505 | '\n' |
| 8506 | ' Return true if *subclass* should be considered a ' |
| 8507 | '(direct or\n' |
| 8508 | ' indirect) subclass of *class*. If defined, called to ' |
| 8509 | 'implement\n' |
| 8510 | ' "issubclass(subclass, class)".\n' |
| 8511 | '\n' |
| 8512 | 'Note that these methods are looked up on the type ' |
| 8513 | '(metaclass) of a\n' |
| 8514 | 'class. They cannot be defined as class methods in the ' |
| 8515 | 'actual class.\n' |
| 8516 | 'This is consistent with the lookup of special methods that ' |
| 8517 | 'are called\n' |
| 8518 | 'on instances, only in this case the instance is itself a ' |
| 8519 | 'class.\n' |
| 8520 | '\n' |
| 8521 | 'See also: **PEP 3119** - Introducing Abstract Base ' |
| 8522 | 'Classes\n' |
| 8523 | '\n' |
| 8524 | ' Includes the specification for customizing ' |
| 8525 | '"isinstance()" and\n' |
| 8526 | ' "issubclass()" behavior through "__instancecheck__()" ' |
| 8527 | 'and\n' |
| 8528 | ' "__subclasscheck__()", with motivation for this ' |
| 8529 | 'functionality in\n' |
| 8530 | ' the context of adding Abstract Base Classes (see the ' |
| 8531 | '"abc"\n' |
| 8532 | ' module) to the language.\n' |
| 8533 | '\n' |
| 8534 | '\n' |
| 8535 | 'Emulating callable objects\n' |
| 8536 | '==========================\n' |
| 8537 | '\n' |
| 8538 | 'object.__call__(self[, args...])\n' |
| 8539 | '\n' |
| 8540 | ' Called when the instance is "called" as a function; if ' |
| 8541 | 'this method\n' |
| 8542 | ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n' |
| 8543 | ' "x.__call__(arg1, arg2, ...)".\n' |
| 8544 | '\n' |
| 8545 | '\n' |
| 8546 | 'Emulating container types\n' |
| 8547 | '=========================\n' |
| 8548 | '\n' |
| 8549 | 'The following methods can be defined to implement ' |
| 8550 | 'container objects.\n' |
| 8551 | 'Containers usually are sequences (such as lists or tuples) ' |
| 8552 | 'or mappings\n' |
| 8553 | '(like dictionaries), but can represent other containers as ' |
| 8554 | 'well. The\n' |
| 8555 | 'first set of methods is used either to emulate a sequence ' |
| 8556 | 'or to\n' |
| 8557 | 'emulate a mapping; the difference is that for a sequence, ' |
| 8558 | 'the\n' |
| 8559 | 'allowable keys should be the integers *k* for which "0 <= ' |
| 8560 | 'k < N" where\n' |
| 8561 | '*N* is the length of the sequence, or slice objects, which ' |
| 8562 | 'define a\n' |
| 8563 | 'range of items. It is also recommended that mappings ' |
| 8564 | 'provide the\n' |
| 8565 | 'methods "keys()", "values()", "items()", "get()", ' |
| 8566 | '"clear()",\n' |
| 8567 | '"setdefault()", "pop()", "popitem()", "copy()", and ' |
| 8568 | '"update()"\n' |
| 8569 | "behaving similar to those for Python's standard dictionary " |
| 8570 | 'objects.\n' |
| 8571 | 'The "collections" module provides a "MutableMapping" ' |
| 8572 | 'abstract base\n' |
| 8573 | 'class to help create those methods from a base set of ' |
| 8574 | '"__getitem__()",\n' |
| 8575 | '"__setitem__()", "__delitem__()", and "keys()". Mutable ' |
| 8576 | 'sequences\n' |
| 8577 | 'should provide methods "append()", "count()", "index()", ' |
| 8578 | '"extend()",\n' |
| 8579 | '"insert()", "pop()", "remove()", "reverse()" and "sort()", ' |
| 8580 | 'like Python\n' |
| 8581 | 'standard list objects. Finally, sequence types should ' |
| 8582 | 'implement\n' |
| 8583 | 'addition (meaning concatenation) and multiplication ' |
| 8584 | '(meaning\n' |
| 8585 | 'repetition) by defining the methods "__add__()", ' |
| 8586 | '"__radd__()",\n' |
| 8587 | '"__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" ' |
| 8588 | 'described\n' |
| 8589 | 'below; they should not define other numerical operators. ' |
| 8590 | 'It is\n' |
| 8591 | 'recommended that both mappings and sequences implement ' |
| 8592 | 'the\n' |
| 8593 | '"__contains__()" method to allow efficient use of the "in" ' |
| 8594 | 'operator;\n' |
| 8595 | 'for mappings, "in" should search the mapping\'s keys; for ' |
| 8596 | 'sequences, it\n' |
| 8597 | 'should search through the values. It is further ' |
| 8598 | 'recommended that both\n' |
| 8599 | 'mappings and sequences implement the "__iter__()" method ' |
| 8600 | 'to allow\n' |
| 8601 | 'efficient iteration through the container; for mappings, ' |
| 8602 | '"__iter__()"\n' |
| 8603 | 'should be the same as "keys()"; for sequences, it should ' |
| 8604 | 'iterate\n' |
| 8605 | 'through the values.\n' |
| 8606 | '\n' |
| 8607 | 'object.__len__(self)\n' |
| 8608 | '\n' |
| 8609 | ' Called to implement the built-in function "len()". ' |
| 8610 | 'Should return\n' |
| 8611 | ' the length of the object, an integer ">=" 0. Also, an ' |
| 8612 | 'object that\n' |
| 8613 | ' doesn\'t define a "__bool__()" method and whose ' |
| 8614 | '"__len__()" method\n' |
| 8615 | ' returns zero is considered to be false in a Boolean ' |
| 8616 | 'context.\n' |
| 8617 | '\n' |
| 8618 | 'object.__length_hint__(self)\n' |
| 8619 | '\n' |
| 8620 | ' Called to implement "operator.length_hint()". Should ' |
| 8621 | 'return an\n' |
| 8622 | ' estimated length for the object (which may be greater ' |
| 8623 | 'or less than\n' |
| 8624 | ' the actual length). The length must be an integer ">=" ' |
| 8625 | '0. This\n' |
| 8626 | ' method is purely an optimization and is never required ' |
| 8627 | 'for\n' |
| 8628 | ' correctness.\n' |
| 8629 | '\n' |
| 8630 | ' New in version 3.4.\n' |
| 8631 | '\n' |
| 8632 | 'Note: Slicing is done exclusively with the following three ' |
| 8633 | 'methods.\n' |
| 8634 | ' A call like\n' |
| 8635 | '\n' |
| 8636 | ' a[1:2] = b\n' |
| 8637 | '\n' |
| 8638 | ' is translated to\n' |
| 8639 | '\n' |
| 8640 | ' a[slice(1, 2, None)] = b\n' |
| 8641 | '\n' |
| 8642 | ' and so forth. Missing slice items are always filled in ' |
| 8643 | 'with "None".\n' |
| 8644 | '\n' |
| 8645 | 'object.__getitem__(self, key)\n' |
| 8646 | '\n' |
| 8647 | ' Called to implement evaluation of "self[key]". For ' |
| 8648 | 'sequence types,\n' |
| 8649 | ' the accepted keys should be integers and slice ' |
| 8650 | 'objects. Note that\n' |
| 8651 | ' the special interpretation of negative indexes (if the ' |
| 8652 | 'class wishes\n' |
| 8653 | ' to emulate a sequence type) is up to the ' |
| 8654 | '"__getitem__()" method. If\n' |
| 8655 | ' *key* is of an inappropriate type, "TypeError" may be ' |
| 8656 | 'raised; if of\n' |
| 8657 | ' a value outside the set of indexes for the sequence ' |
| 8658 | '(after any\n' |
| 8659 | ' special interpretation of negative values), ' |
| 8660 | '"IndexError" should be\n' |
| 8661 | ' raised. For mapping types, if *key* is missing (not in ' |
| 8662 | 'the\n' |
| 8663 | ' container), "KeyError" should be raised.\n' |
| 8664 | '\n' |
| 8665 | ' Note: "for" loops expect that an "IndexError" will be ' |
| 8666 | 'raised for\n' |
| 8667 | ' illegal indexes to allow proper detection of the end ' |
| 8668 | 'of the\n' |
| 8669 | ' sequence.\n' |
| 8670 | '\n' |
| 8671 | 'object.__missing__(self, key)\n' |
| 8672 | '\n' |
| 8673 | ' Called by "dict"."__getitem__()" to implement ' |
| 8674 | '"self[key]" for dict\n' |
| 8675 | ' subclasses when key is not in the dictionary.\n' |
| 8676 | '\n' |
| 8677 | 'object.__setitem__(self, key, value)\n' |
| 8678 | '\n' |
| 8679 | ' Called to implement assignment to "self[key]". Same ' |
| 8680 | 'note as for\n' |
| 8681 | ' "__getitem__()". This should only be implemented for ' |
| 8682 | 'mappings if\n' |
| 8683 | ' the objects support changes to the values for keys, or ' |
| 8684 | 'if new keys\n' |
| 8685 | ' can be added, or for sequences if elements can be ' |
| 8686 | 'replaced. The\n' |
| 8687 | ' same exceptions should be raised for improper *key* ' |
| 8688 | 'values as for\n' |
| 8689 | ' the "__getitem__()" method.\n' |
| 8690 | '\n' |
| 8691 | 'object.__delitem__(self, key)\n' |
| 8692 | '\n' |
| 8693 | ' Called to implement deletion of "self[key]". Same note ' |
| 8694 | 'as for\n' |
| 8695 | ' "__getitem__()". This should only be implemented for ' |
| 8696 | 'mappings if\n' |
| 8697 | ' the objects support removal of keys, or for sequences ' |
| 8698 | 'if elements\n' |
| 8699 | ' can be removed from the sequence. The same exceptions ' |
| 8700 | 'should be\n' |
| 8701 | ' raised for improper *key* values as for the ' |
| 8702 | '"__getitem__()" method.\n' |
| 8703 | '\n' |
| 8704 | 'object.__iter__(self)\n' |
| 8705 | '\n' |
| 8706 | ' This method is called when an iterator is required for ' |
| 8707 | 'a container.\n' |
| 8708 | ' This method should return a new iterator object that ' |
| 8709 | 'can iterate\n' |
| 8710 | ' over all the objects in the container. For mappings, ' |
| 8711 | 'it should\n' |
| 8712 | ' iterate over the keys of the container.\n' |
| 8713 | '\n' |
| 8714 | ' Iterator objects also need to implement this method; ' |
| 8715 | 'they are\n' |
| 8716 | ' required to return themselves. For more information on ' |
| 8717 | 'iterator\n' |
| 8718 | ' objects, see *Iterator Types*.\n' |
| 8719 | '\n' |
| 8720 | 'object.__reversed__(self)\n' |
| 8721 | '\n' |
| 8722 | ' Called (if present) by the "reversed()" built-in to ' |
| 8723 | 'implement\n' |
| 8724 | ' reverse iteration. It should return a new iterator ' |
| 8725 | 'object that\n' |
| 8726 | ' iterates over all the objects in the container in ' |
| 8727 | 'reverse order.\n' |
| 8728 | '\n' |
| 8729 | ' If the "__reversed__()" method is not provided, the ' |
| 8730 | '"reversed()"\n' |
| 8731 | ' built-in will fall back to using the sequence protocol ' |
| 8732 | '("__len__()"\n' |
| 8733 | ' and "__getitem__()"). Objects that support the ' |
| 8734 | 'sequence protocol\n' |
| 8735 | ' should only provide "__reversed__()" if they can ' |
| 8736 | 'provide an\n' |
| 8737 | ' implementation that is more efficient than the one ' |
| 8738 | 'provided by\n' |
| 8739 | ' "reversed()".\n' |
| 8740 | '\n' |
| 8741 | 'The membership test operators ("in" and "not in") are ' |
| 8742 | 'normally\n' |
| 8743 | 'implemented as an iteration through a sequence. However, ' |
| 8744 | 'container\n' |
| 8745 | 'objects can supply the following special method with a ' |
| 8746 | 'more efficient\n' |
| 8747 | 'implementation, which also does not require the object be ' |
| 8748 | 'a sequence.\n' |
| 8749 | '\n' |
| 8750 | 'object.__contains__(self, item)\n' |
| 8751 | '\n' |
| 8752 | ' Called to implement membership test operators. Should ' |
| 8753 | 'return true\n' |
| 8754 | ' if *item* is in *self*, false otherwise. For mapping ' |
| 8755 | 'objects, this\n' |
| 8756 | ' should consider the keys of the mapping rather than the ' |
| 8757 | 'values or\n' |
| 8758 | ' the key-item pairs.\n' |
| 8759 | '\n' |
| 8760 | ' For objects that don\'t define "__contains__()", the ' |
| 8761 | 'membership test\n' |
| 8762 | ' first tries iteration via "__iter__()", then the old ' |
| 8763 | 'sequence\n' |
| 8764 | ' iteration protocol via "__getitem__()", see *this ' |
| 8765 | 'section in the\n' |
| 8766 | ' language reference*.\n' |
| 8767 | '\n' |
| 8768 | '\n' |
| 8769 | 'Emulating numeric types\n' |
| 8770 | '=======================\n' |
| 8771 | '\n' |
| 8772 | 'The following methods can be defined to emulate numeric ' |
| 8773 | 'objects.\n' |
| 8774 | 'Methods corresponding to operations that are not supported ' |
| 8775 | 'by the\n' |
| 8776 | 'particular kind of number implemented (e.g., bitwise ' |
| 8777 | 'operations for\n' |
| 8778 | 'non-integral numbers) should be left undefined.\n' |
| 8779 | '\n' |
| 8780 | 'object.__add__(self, other)\n' |
| 8781 | 'object.__sub__(self, other)\n' |
| 8782 | 'object.__mul__(self, other)\n' |
| 8783 | 'object.__matmul__(self, other)\n' |
| 8784 | 'object.__truediv__(self, other)\n' |
| 8785 | 'object.__floordiv__(self, other)\n' |
| 8786 | 'object.__mod__(self, other)\n' |
| 8787 | 'object.__divmod__(self, other)\n' |
| 8788 | 'object.__pow__(self, other[, modulo])\n' |
| 8789 | 'object.__lshift__(self, other)\n' |
| 8790 | 'object.__rshift__(self, other)\n' |
| 8791 | 'object.__and__(self, other)\n' |
| 8792 | 'object.__xor__(self, other)\n' |
| 8793 | 'object.__or__(self, other)\n' |
| 8794 | '\n' |
| 8795 | ' These methods are called to implement the binary ' |
| 8796 | 'arithmetic\n' |
| 8797 | ' operations ("+", "-", "*", "@", "/", "//", "%", ' |
| 8798 | '"divmod()",\n' |
| 8799 | ' "pow()", "**", "<<", ">>", "&", "^", "|"). For ' |
| 8800 | 'instance, to\n' |
| 8801 | ' evaluate the expression "x + y", where *x* is an ' |
| 8802 | 'instance of a\n' |
| 8803 | ' class that has an "__add__()" method, "x.__add__(y)" is ' |
| 8804 | 'called.\n' |
| 8805 | ' The "__divmod__()" method should be the equivalent to ' |
| 8806 | 'using\n' |
| 8807 | ' "__floordiv__()" and "__mod__()"; it should not be ' |
| 8808 | 'related to\n' |
| 8809 | ' "__truediv__()". Note that "__pow__()" should be ' |
| 8810 | 'defined to accept\n' |
| 8811 | ' an optional third argument if the ternary version of ' |
| 8812 | 'the built-in\n' |
| 8813 | ' "pow()" function is to be supported.\n' |
| 8814 | '\n' |
| 8815 | ' If one of those methods does not support the operation ' |
| 8816 | 'with the\n' |
| 8817 | ' supplied arguments, it should return "NotImplemented".\n' |
| 8818 | '\n' |
| 8819 | 'object.__radd__(self, other)\n' |
| 8820 | 'object.__rsub__(self, other)\n' |
| 8821 | 'object.__rmul__(self, other)\n' |
| 8822 | 'object.__rmatmul__(self, other)\n' |
| 8823 | 'object.__rtruediv__(self, other)\n' |
| 8824 | 'object.__rfloordiv__(self, other)\n' |
| 8825 | 'object.__rmod__(self, other)\n' |
| 8826 | 'object.__rdivmod__(self, other)\n' |
| 8827 | 'object.__rpow__(self, other)\n' |
| 8828 | 'object.__rlshift__(self, other)\n' |
| 8829 | 'object.__rrshift__(self, other)\n' |
| 8830 | 'object.__rand__(self, other)\n' |
| 8831 | 'object.__rxor__(self, other)\n' |
| 8832 | 'object.__ror__(self, other)\n' |
| 8833 | '\n' |
| 8834 | ' These methods are called to implement the binary ' |
| 8835 | 'arithmetic\n' |
| 8836 | ' operations ("+", "-", "*", "@", "/", "//", "%", ' |
| 8837 | '"divmod()",\n' |
| 8838 | ' "pow()", "**", "<<", ">>", "&", "^", "|") with ' |
| 8839 | 'reflected (swapped)\n' |
| 8840 | ' operands. These functions are only called if the left ' |
| 8841 | 'operand does\n' |
| 8842 | ' not support the corresponding operation and the ' |
| 8843 | 'operands are of\n' |
| 8844 | ' different types. [2] For instance, to evaluate the ' |
| 8845 | 'expression "x -\n' |
| 8846 | ' y", where *y* is an instance of a class that has an ' |
| 8847 | '"__rsub__()"\n' |
| 8848 | ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" ' |
| 8849 | 'returns\n' |
| 8850 | ' *NotImplemented*.\n' |
| 8851 | '\n' |
| 8852 | ' Note that ternary "pow()" will not try calling ' |
| 8853 | '"__rpow__()" (the\n' |
| 8854 | ' coercion rules would become too complicated).\n' |
| 8855 | '\n' |
| 8856 | " Note: If the right operand's type is a subclass of the " |
| 8857 | 'left\n' |
| 8858 | " operand's type and that subclass provides the " |
| 8859 | 'reflected method\n' |
| 8860 | ' for the operation, this method will be called before ' |
| 8861 | 'the left\n' |
| 8862 | " operand's non-reflected method. This behavior allows " |
| 8863 | 'subclasses\n' |
| 8864 | " to override their ancestors' operations.\n" |
| 8865 | '\n' |
| 8866 | 'object.__iadd__(self, other)\n' |
| 8867 | 'object.__isub__(self, other)\n' |
| 8868 | 'object.__imul__(self, other)\n' |
| 8869 | 'object.__imatmul__(self, other)\n' |
| 8870 | 'object.__itruediv__(self, other)\n' |
| 8871 | 'object.__ifloordiv__(self, other)\n' |
| 8872 | 'object.__imod__(self, other)\n' |
| 8873 | 'object.__ipow__(self, other[, modulo])\n' |
| 8874 | 'object.__ilshift__(self, other)\n' |
| 8875 | 'object.__irshift__(self, other)\n' |
| 8876 | 'object.__iand__(self, other)\n' |
| 8877 | 'object.__ixor__(self, other)\n' |
| 8878 | 'object.__ior__(self, other)\n' |
| 8879 | '\n' |
| 8880 | ' These methods are called to implement the augmented ' |
| 8881 | 'arithmetic\n' |
| 8882 | ' assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", ' |
| 8883 | '"**=",\n' |
| 8884 | ' "<<=", ">>=", "&=", "^=", "|="). These methods should ' |
| 8885 | 'attempt to\n' |
| 8886 | ' do the operation in-place (modifying *self*) and return ' |
| 8887 | 'the result\n' |
| 8888 | ' (which could be, but does not have to be, *self*). If ' |
| 8889 | 'a specific\n' |
| 8890 | ' method is not defined, the augmented assignment falls ' |
| 8891 | 'back to the\n' |
| 8892 | ' normal methods. For instance, if *x* is an instance of ' |
| 8893 | 'a class\n' |
| 8894 | ' with an "__iadd__()" method, "x += y" is equivalent to ' |
| 8895 | '"x =\n' |
| 8896 | ' x.__iadd__(y)" . Otherwise, "x.__add__(y)" and ' |
| 8897 | '"y.__radd__(x)" are\n' |
| 8898 | ' considered, as with the evaluation of "x + y". In ' |
| 8899 | 'certain\n' |
| 8900 | ' situations, augmented assignment can result in ' |
| 8901 | 'unexpected errors\n' |
| 8902 | " (see *Why does a_tuple[i] += ['item'] raise an " |
| 8903 | 'exception when the\n' |
| 8904 | ' addition works?*), but this behavior is in fact part of ' |
| 8905 | 'the data\n' |
| 8906 | ' model.\n' |
| 8907 | '\n' |
| 8908 | 'object.__neg__(self)\n' |
| 8909 | 'object.__pos__(self)\n' |
| 8910 | 'object.__abs__(self)\n' |
| 8911 | 'object.__invert__(self)\n' |
| 8912 | '\n' |
| 8913 | ' Called to implement the unary arithmetic operations ' |
| 8914 | '("-", "+",\n' |
| 8915 | ' "abs()" and "~").\n' |
| 8916 | '\n' |
| 8917 | 'object.__complex__(self)\n' |
| 8918 | 'object.__int__(self)\n' |
| 8919 | 'object.__float__(self)\n' |
| 8920 | 'object.__round__(self[, n])\n' |
| 8921 | '\n' |
| 8922 | ' Called to implement the built-in functions "complex()", ' |
| 8923 | '"int()",\n' |
| 8924 | ' "float()" and "round()". Should return a value of the ' |
| 8925 | 'appropriate\n' |
| 8926 | ' type.\n' |
| 8927 | '\n' |
| 8928 | 'object.__index__(self)\n' |
| 8929 | '\n' |
| 8930 | ' Called to implement "operator.index()", and whenever ' |
| 8931 | 'Python needs\n' |
| 8932 | ' to losslessly convert the numeric object to an integer ' |
| 8933 | 'object (such\n' |
| 8934 | ' as in slicing, or in the built-in "bin()", "hex()" and ' |
| 8935 | '"oct()"\n' |
| 8936 | ' functions). Presence of this method indicates that the ' |
| 8937 | 'numeric\n' |
| 8938 | ' object is an integer type. Must return an integer.\n' |
| 8939 | '\n' |
| 8940 | ' Note: In order to have a coherent integer type class, ' |
| 8941 | 'when\n' |
| 8942 | ' "__index__()" is defined "__int__()" should also be ' |
| 8943 | 'defined, and\n' |
| 8944 | ' both should return the same value.\n' |
| 8945 | '\n' |
| 8946 | '\n' |
| 8947 | 'With Statement Context Managers\n' |
| 8948 | '===============================\n' |
| 8949 | '\n' |
| 8950 | 'A *context manager* is an object that defines the runtime ' |
| 8951 | 'context to\n' |
| 8952 | 'be established when executing a "with" statement. The ' |
| 8953 | 'context manager\n' |
| 8954 | 'handles the entry into, and the exit from, the desired ' |
| 8955 | 'runtime context\n' |
| 8956 | 'for the execution of the block of code. Context managers ' |
| 8957 | 'are normally\n' |
| 8958 | 'invoked using the "with" statement (described in section ' |
| 8959 | '*The with\n' |
| 8960 | 'statement*), but can also be used by directly invoking ' |
| 8961 | 'their methods.\n' |
| 8962 | '\n' |
| 8963 | 'Typical uses of context managers include saving and ' |
| 8964 | 'restoring various\n' |
| 8965 | 'kinds of global state, locking and unlocking resources, ' |
| 8966 | 'closing opened\n' |
| 8967 | 'files, etc.\n' |
| 8968 | '\n' |
| 8969 | 'For more information on context managers, see *Context ' |
| 8970 | 'Manager Types*.\n' |
| 8971 | '\n' |
| 8972 | 'object.__enter__(self)\n' |
| 8973 | '\n' |
| 8974 | ' Enter the runtime context related to this object. The ' |
| 8975 | '"with"\n' |
| 8976 | " statement will bind this method's return value to the " |
| 8977 | 'target(s)\n' |
| 8978 | ' specified in the "as" clause of the statement, if any.\n' |
| 8979 | '\n' |
| 8980 | 'object.__exit__(self, exc_type, exc_value, traceback)\n' |
| 8981 | '\n' |
| 8982 | ' Exit the runtime context related to this object. The ' |
| 8983 | 'parameters\n' |
| 8984 | ' describe the exception that caused the context to be ' |
| 8985 | 'exited. If the\n' |
| 8986 | ' context was exited without an exception, all three ' |
| 8987 | 'arguments will\n' |
| 8988 | ' be "None".\n' |
| 8989 | '\n' |
| 8990 | ' If an exception is supplied, and the method wishes to ' |
| 8991 | 'suppress the\n' |
| 8992 | ' exception (i.e., prevent it from being propagated), it ' |
| 8993 | 'should\n' |
| 8994 | ' return a true value. Otherwise, the exception will be ' |
| 8995 | 'processed\n' |
| 8996 | ' normally upon exit from this method.\n' |
| 8997 | '\n' |
| 8998 | ' Note that "__exit__()" methods should not reraise the ' |
| 8999 | 'passed-in\n' |
| 9000 | " exception; this is the caller's responsibility.\n" |
| 9001 | '\n' |
| 9002 | 'See also: **PEP 0343** - The "with" statement\n' |
| 9003 | '\n' |
| 9004 | ' The specification, background, and examples for the ' |
| 9005 | 'Python "with"\n' |
| 9006 | ' statement.\n' |
| 9007 | '\n' |
| 9008 | '\n' |
| 9009 | 'Special method lookup\n' |
| 9010 | '=====================\n' |
| 9011 | '\n' |
| 9012 | 'For custom classes, implicit invocations of special ' |
| 9013 | 'methods are only\n' |
| 9014 | "guaranteed to work correctly if defined on an object's " |
| 9015 | 'type, not in\n' |
| 9016 | "the object's instance dictionary. That behaviour is the " |
| 9017 | 'reason why\n' |
| 9018 | 'the following code raises an exception:\n' |
| 9019 | '\n' |
| 9020 | ' >>> class C:\n' |
| 9021 | ' ... pass\n' |
| 9022 | ' ...\n' |
| 9023 | ' >>> c = C()\n' |
| 9024 | ' >>> c.__len__ = lambda: 5\n' |
| 9025 | ' >>> len(c)\n' |
| 9026 | ' Traceback (most recent call last):\n' |
| 9027 | ' File "<stdin>", line 1, in <module>\n' |
| 9028 | " TypeError: object of type 'C' has no len()\n" |
| 9029 | '\n' |
| 9030 | 'The rationale behind this behaviour lies with a number of ' |
| 9031 | 'special\n' |
| 9032 | 'methods such as "__hash__()" and "__repr__()" that are ' |
| 9033 | 'implemented by\n' |
| 9034 | 'all objects, including type objects. If the implicit ' |
| 9035 | 'lookup of these\n' |
| 9036 | 'methods used the conventional lookup process, they would ' |
| 9037 | 'fail when\n' |
| 9038 | 'invoked on the type object itself:\n' |
| 9039 | '\n' |
| 9040 | ' >>> 1 .__hash__() == hash(1)\n' |
| 9041 | ' True\n' |
| 9042 | ' >>> int.__hash__() == hash(int)\n' |
| 9043 | ' Traceback (most recent call last):\n' |
| 9044 | ' File "<stdin>", line 1, in <module>\n' |
| 9045 | " TypeError: descriptor '__hash__' of 'int' object needs " |
| 9046 | 'an argument\n' |
| 9047 | '\n' |
| 9048 | 'Incorrectly attempting to invoke an unbound method of a ' |
| 9049 | 'class in this\n' |
| 9050 | "way is sometimes referred to as 'metaclass confusion', and " |
| 9051 | 'is avoided\n' |
| 9052 | 'by bypassing the instance when looking up special ' |
| 9053 | 'methods:\n' |
| 9054 | '\n' |
| 9055 | ' >>> type(1).__hash__(1) == hash(1)\n' |
| 9056 | ' True\n' |
| 9057 | ' >>> type(int).__hash__(int) == hash(int)\n' |
| 9058 | ' True\n' |
| 9059 | '\n' |
| 9060 | 'In addition to bypassing any instance attributes in the ' |
| 9061 | 'interest of\n' |
| 9062 | 'correctness, implicit special method lookup generally also ' |
| 9063 | 'bypasses\n' |
| 9064 | 'the "__getattribute__()" method even of the object\'s ' |
| 9065 | 'metaclass:\n' |
| 9066 | '\n' |
| 9067 | ' >>> class Meta(type):\n' |
| 9068 | ' ... def __getattribute__(*args):\n' |
| 9069 | ' ... print("Metaclass getattribute invoked")\n' |
| 9070 | ' ... return type.__getattribute__(*args)\n' |
| 9071 | ' ...\n' |
| 9072 | ' >>> class C(object, metaclass=Meta):\n' |
| 9073 | ' ... def __len__(self):\n' |
| 9074 | ' ... return 10\n' |
| 9075 | ' ... def __getattribute__(*args):\n' |
| 9076 | ' ... print("Class getattribute invoked")\n' |
| 9077 | ' ... return object.__getattribute__(*args)\n' |
| 9078 | ' ...\n' |
| 9079 | ' >>> c = C()\n' |
| 9080 | ' >>> c.__len__() # Explicit lookup via ' |
| 9081 | 'instance\n' |
| 9082 | ' Class getattribute invoked\n' |
| 9083 | ' 10\n' |
| 9084 | ' >>> type(c).__len__(c) # Explicit lookup via ' |
| 9085 | 'type\n' |
| 9086 | ' Metaclass getattribute invoked\n' |
| 9087 | ' 10\n' |
| 9088 | ' >>> len(c) # Implicit lookup\n' |
| 9089 | ' 10\n' |
| 9090 | '\n' |
| 9091 | 'Bypassing the "__getattribute__()" machinery in this ' |
| 9092 | 'fashion provides\n' |
| 9093 | 'significant scope for speed optimisations within the ' |
| 9094 | 'interpreter, at\n' |
| 9095 | 'the cost of some flexibility in the handling of special ' |
| 9096 | 'methods (the\n' |
| 9097 | 'special method *must* be set on the class object itself in ' |
| 9098 | 'order to be\n' |
| 9099 | 'consistently invoked by the interpreter).\n', |
| 9100 | 'string-methods': '\n' |
| 9101 | 'String Methods\n' |
| 9102 | '**************\n' |
| 9103 | '\n' |
| 9104 | 'Strings implement all of the *common* sequence ' |
| 9105 | 'operations, along with\n' |
| 9106 | 'the additional methods described below.\n' |
| 9107 | '\n' |
| 9108 | 'Strings also support two styles of string formatting, ' |
| 9109 | 'one providing a\n' |
| 9110 | 'large degree of flexibility and customization (see ' |
| 9111 | '"str.format()",\n' |
| 9112 | '*Format String Syntax* and *String Formatting*) and the ' |
| 9113 | 'other based on\n' |
| 9114 | 'C "printf" style formatting that handles a narrower ' |
| 9115 | 'range of types and\n' |
| 9116 | 'is slightly harder to use correctly, but is often faster ' |
| 9117 | 'for the cases\n' |
| 9118 | 'it can handle (*printf-style String Formatting*).\n' |
| 9119 | '\n' |
| 9120 | 'The *Text Processing Services* section of the standard ' |
| 9121 | 'library covers\n' |
| 9122 | 'a number of other modules that provide various text ' |
| 9123 | 'related utilities\n' |
| 9124 | '(including regular expression support in the "re" ' |
| 9125 | 'module).\n' |
| 9126 | '\n' |
| 9127 | 'str.capitalize()\n' |
| 9128 | '\n' |
| 9129 | ' Return a copy of the string with its first character ' |
| 9130 | 'capitalized\n' |
| 9131 | ' and the rest lowercased.\n' |
| 9132 | '\n' |
| 9133 | 'str.casefold()\n' |
| 9134 | '\n' |
| 9135 | ' Return a casefolded copy of the string. Casefolded ' |
| 9136 | 'strings may be\n' |
| 9137 | ' used for caseless matching.\n' |
| 9138 | '\n' |
| 9139 | ' Casefolding is similar to lowercasing but more ' |
| 9140 | 'aggressive because\n' |
| 9141 | ' it is intended to remove all case distinctions in a ' |
| 9142 | 'string. For\n' |
| 9143 | ' example, the German lowercase letter "\'ß\'" is ' |
| 9144 | 'equivalent to ""ss"".\n' |
| 9145 | ' Since it is already lowercase, "lower()" would do ' |
| 9146 | 'nothing to "\'ß\'";\n' |
| 9147 | ' "casefold()" converts it to ""ss"".\n' |
| 9148 | '\n' |
| 9149 | ' The casefolding algorithm is described in section ' |
| 9150 | '3.13 of the\n' |
| 9151 | ' Unicode Standard.\n' |
| 9152 | '\n' |
| 9153 | ' New in version 3.3.\n' |
| 9154 | '\n' |
| 9155 | 'str.center(width[, fillchar])\n' |
| 9156 | '\n' |
| 9157 | ' Return centered in a string of length *width*. ' |
| 9158 | 'Padding is done\n' |
| 9159 | ' using the specified *fillchar* (default is an ASCII ' |
| 9160 | 'space). The\n' |
| 9161 | ' original string is returned if *width* is less than ' |
| 9162 | 'or equal to\n' |
| 9163 | ' "len(s)".\n' |
| 9164 | '\n' |
| 9165 | 'str.count(sub[, start[, end]])\n' |
| 9166 | '\n' |
| 9167 | ' Return the number of non-overlapping occurrences of ' |
| 9168 | 'substring *sub*\n' |
| 9169 | ' in the range [*start*, *end*]. Optional arguments ' |
| 9170 | '*start* and\n' |
| 9171 | ' *end* are interpreted as in slice notation.\n' |
| 9172 | '\n' |
| 9173 | 'str.encode(encoding="utf-8", errors="strict")\n' |
| 9174 | '\n' |
| 9175 | ' Return an encoded version of the string as a bytes ' |
| 9176 | 'object. Default\n' |
| 9177 | ' encoding is "\'utf-8\'". *errors* may be given to set ' |
| 9178 | 'a different\n' |
| 9179 | ' error handling scheme. The default for *errors* is ' |
| 9180 | '"\'strict\'",\n' |
| 9181 | ' meaning that encoding errors raise a "UnicodeError". ' |
| 9182 | 'Other possible\n' |
| 9183 | ' values are "\'ignore\'", "\'replace\'", ' |
| 9184 | '"\'xmlcharrefreplace\'",\n' |
| 9185 | ' "\'backslashreplace\'" and any other name registered ' |
| 9186 | 'via\n' |
| 9187 | ' "codecs.register_error()", see section *Error ' |
| 9188 | 'Handlers*. For a list\n' |
| 9189 | ' of possible encodings, see section *Standard ' |
| 9190 | 'Encodings*.\n' |
| 9191 | '\n' |
| 9192 | ' Changed in version 3.1: Support for keyword arguments ' |
| 9193 | 'added.\n' |
| 9194 | '\n' |
| 9195 | 'str.endswith(suffix[, start[, end]])\n' |
| 9196 | '\n' |
| 9197 | ' Return "True" if the string ends with the specified ' |
| 9198 | '*suffix*,\n' |
| 9199 | ' otherwise return "False". *suffix* can also be a ' |
| 9200 | 'tuple of suffixes\n' |
| 9201 | ' to look for. With optional *start*, test beginning ' |
| 9202 | 'at that\n' |
| 9203 | ' position. With optional *end*, stop comparing at ' |
| 9204 | 'that position.\n' |
| 9205 | '\n' |
| 9206 | 'str.expandtabs(tabsize=8)\n' |
| 9207 | '\n' |
| 9208 | ' Return a copy of the string where all tab characters ' |
| 9209 | 'are replaced\n' |
| 9210 | ' by one or more spaces, depending on the current ' |
| 9211 | 'column and the\n' |
| 9212 | ' given tab size. Tab positions occur every *tabsize* ' |
| 9213 | 'characters\n' |
| 9214 | ' (default is 8, giving tab positions at columns 0, 8, ' |
| 9215 | '16 and so on).\n' |
| 9216 | ' To expand the string, the current column is set to ' |
| 9217 | 'zero and the\n' |
| 9218 | ' string is examined character by character. If the ' |
| 9219 | 'character is a\n' |
| 9220 | ' tab ("\\t"), one or more space characters are ' |
| 9221 | 'inserted in the result\n' |
| 9222 | ' until the current column is equal to the next tab ' |
| 9223 | 'position. (The\n' |
| 9224 | ' tab character itself is not copied.) If the ' |
| 9225 | 'character is a newline\n' |
| 9226 | ' ("\\n") or return ("\\r"), it is copied and the ' |
| 9227 | 'current column is\n' |
| 9228 | ' reset to zero. Any other character is copied ' |
| 9229 | 'unchanged and the\n' |
| 9230 | ' current column is incremented by one regardless of ' |
| 9231 | 'how the\n' |
| 9232 | ' character is represented when printed.\n' |
| 9233 | '\n' |
| 9234 | " >>> '01\\t012\\t0123\\t01234'.expandtabs()\n" |
| 9235 | " '01 012 0123 01234'\n" |
| 9236 | " >>> '01\\t012\\t0123\\t01234'.expandtabs(4)\n" |
| 9237 | " '01 012 0123 01234'\n" |
| 9238 | '\n' |
| 9239 | 'str.find(sub[, start[, end]])\n' |
| 9240 | '\n' |
| 9241 | ' Return the lowest index in the string where substring ' |
| 9242 | '*sub* is\n' |
| 9243 | ' found, such that *sub* is contained in the slice ' |
| 9244 | '"s[start:end]".\n' |
| 9245 | ' Optional arguments *start* and *end* are interpreted ' |
| 9246 | 'as in slice\n' |
| 9247 | ' notation. Return "-1" if *sub* is not found.\n' |
| 9248 | '\n' |
| 9249 | ' Note: The "find()" method should be used only if you ' |
| 9250 | 'need to know\n' |
| 9251 | ' the position of *sub*. To check if *sub* is a ' |
| 9252 | 'substring or not,\n' |
| 9253 | ' use the "in" operator:\n' |
| 9254 | '\n' |
| 9255 | " >>> 'Py' in 'Python'\n" |
| 9256 | ' True\n' |
| 9257 | '\n' |
| 9258 | 'str.format(*args, **kwargs)\n' |
| 9259 | '\n' |
| 9260 | ' Perform a string formatting operation. The string on ' |
| 9261 | 'which this\n' |
| 9262 | ' method is called can contain literal text or ' |
| 9263 | 'replacement fields\n' |
| 9264 | ' delimited by braces "{}". Each replacement field ' |
| 9265 | 'contains either\n' |
| 9266 | ' the numeric index of a positional argument, or the ' |
| 9267 | 'name of a\n' |
| 9268 | ' keyword argument. Returns a copy of the string where ' |
| 9269 | 'each\n' |
| 9270 | ' replacement field is replaced with the string value ' |
| 9271 | 'of the\n' |
| 9272 | ' corresponding argument.\n' |
| 9273 | '\n' |
| 9274 | ' >>> "The sum of 1 + 2 is {0}".format(1+2)\n' |
| 9275 | " 'The sum of 1 + 2 is 3'\n" |
| 9276 | '\n' |
| 9277 | ' See *Format String Syntax* for a description of the ' |
| 9278 | 'various\n' |
| 9279 | ' formatting options that can be specified in format ' |
| 9280 | 'strings.\n' |
| 9281 | '\n' |
| 9282 | 'str.format_map(mapping)\n' |
| 9283 | '\n' |
| 9284 | ' Similar to "str.format(**mapping)", except that ' |
| 9285 | '"mapping" is used\n' |
| 9286 | ' directly and not copied to a "dict". This is useful ' |
| 9287 | 'if for example\n' |
| 9288 | ' "mapping" is a dict subclass:\n' |
| 9289 | '\n' |
| 9290 | ' >>> class Default(dict):\n' |
| 9291 | ' ... def __missing__(self, key):\n' |
| 9292 | ' ... return key\n' |
| 9293 | ' ...\n' |
| 9294 | " >>> '{name} was born in " |
| 9295 | "{country}'.format_map(Default(name='Guido'))\n" |
| 9296 | " 'Guido was born in country'\n" |
| 9297 | '\n' |
| 9298 | ' New in version 3.2.\n' |
| 9299 | '\n' |
| 9300 | 'str.index(sub[, start[, end]])\n' |
| 9301 | '\n' |
| 9302 | ' Like "find()", but raise "ValueError" when the ' |
| 9303 | 'substring is not\n' |
| 9304 | ' found.\n' |
| 9305 | '\n' |
| 9306 | 'str.isalnum()\n' |
| 9307 | '\n' |
| 9308 | ' Return true if all characters in the string are ' |
| 9309 | 'alphanumeric and\n' |
| 9310 | ' there is at least one character, false otherwise. A ' |
| 9311 | 'character "c"\n' |
| 9312 | ' is alphanumeric if one of the following returns ' |
| 9313 | '"True":\n' |
| 9314 | ' "c.isalpha()", "c.isdecimal()", "c.isdigit()", or ' |
| 9315 | '"c.isnumeric()".\n' |
| 9316 | '\n' |
| 9317 | 'str.isalpha()\n' |
| 9318 | '\n' |
| 9319 | ' Return true if all characters in the string are ' |
| 9320 | 'alphabetic and\n' |
| 9321 | ' there is at least one character, false otherwise. ' |
| 9322 | 'Alphabetic\n' |
| 9323 | ' characters are those characters defined in the ' |
| 9324 | 'Unicode character\n' |
| 9325 | ' database as "Letter", i.e., those with general ' |
| 9326 | 'category property\n' |
| 9327 | ' being one of "Lm", "Lt", "Lu", "Ll", or "Lo". Note ' |
| 9328 | 'that this is\n' |
| 9329 | ' different from the "Alphabetic" property defined in ' |
| 9330 | 'the Unicode\n' |
| 9331 | ' Standard.\n' |
| 9332 | '\n' |
| 9333 | 'str.isdecimal()\n' |
| 9334 | '\n' |
| 9335 | ' Return true if all characters in the string are ' |
| 9336 | 'decimal characters\n' |
| 9337 | ' and there is at least one character, false otherwise. ' |
| 9338 | 'Decimal\n' |
| 9339 | ' characters are those from general category "Nd". This ' |
| 9340 | 'category\n' |
| 9341 | ' includes digit characters, and all characters that ' |
| 9342 | 'can be used to\n' |
| 9343 | ' form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC ' |
| 9344 | 'DIGIT ZERO.\n' |
| 9345 | '\n' |
| 9346 | 'str.isdigit()\n' |
| 9347 | '\n' |
| 9348 | ' Return true if all characters in the string are ' |
| 9349 | 'digits and there is\n' |
| 9350 | ' at least one character, false otherwise. Digits ' |
| 9351 | 'include decimal\n' |
| 9352 | ' characters and digits that need special handling, ' |
| 9353 | 'such as the\n' |
| 9354 | ' compatibility superscript digits. Formally, a digit ' |
| 9355 | 'is a character\n' |
| 9356 | ' that has the property value Numeric_Type=Digit or\n' |
| 9357 | ' Numeric_Type=Decimal.\n' |
| 9358 | '\n' |
| 9359 | 'str.isidentifier()\n' |
| 9360 | '\n' |
| 9361 | ' Return true if the string is a valid identifier ' |
| 9362 | 'according to the\n' |
| 9363 | ' language definition, section *Identifiers and ' |
| 9364 | 'keywords*.\n' |
| 9365 | '\n' |
| 9366 | ' Use "keyword.iskeyword()" to test for reserved ' |
| 9367 | 'identifiers such as\n' |
| 9368 | ' "def" and "class".\n' |
| 9369 | '\n' |
| 9370 | 'str.islower()\n' |
| 9371 | '\n' |
| 9372 | ' Return true if all cased characters [4] in the string ' |
| 9373 | 'are lowercase\n' |
| 9374 | ' and there is at least one cased character, false ' |
| 9375 | 'otherwise.\n' |
| 9376 | '\n' |
| 9377 | 'str.isnumeric()\n' |
| 9378 | '\n' |
| 9379 | ' Return true if all characters in the string are ' |
| 9380 | 'numeric characters,\n' |
| 9381 | ' and there is at least one character, false otherwise. ' |
| 9382 | 'Numeric\n' |
| 9383 | ' characters include digit characters, and all ' |
| 9384 | 'characters that have\n' |
| 9385 | ' the Unicode numeric value property, e.g. U+2155, ' |
| 9386 | 'VULGAR FRACTION\n' |
| 9387 | ' ONE FIFTH. Formally, numeric characters are those ' |
| 9388 | 'with the\n' |
| 9389 | ' property value Numeric_Type=Digit, ' |
| 9390 | 'Numeric_Type=Decimal or\n' |
| 9391 | ' Numeric_Type=Numeric.\n' |
| 9392 | '\n' |
| 9393 | 'str.isprintable()\n' |
| 9394 | '\n' |
| 9395 | ' Return true if all characters in the string are ' |
| 9396 | 'printable or the\n' |
| 9397 | ' string is empty, false otherwise. Nonprintable ' |
| 9398 | 'characters are\n' |
| 9399 | ' those characters defined in the Unicode character ' |
| 9400 | 'database as\n' |
| 9401 | ' "Other" or "Separator", excepting the ASCII space ' |
| 9402 | '(0x20) which is\n' |
| 9403 | ' considered printable. (Note that printable ' |
| 9404 | 'characters in this\n' |
| 9405 | ' context are those which should not be escaped when ' |
| 9406 | '"repr()" is\n' |
| 9407 | ' invoked on a string. It has no bearing on the ' |
| 9408 | 'handling of strings\n' |
| 9409 | ' written to "sys.stdout" or "sys.stderr".)\n' |
| 9410 | '\n' |
| 9411 | 'str.isspace()\n' |
| 9412 | '\n' |
| 9413 | ' Return true if there are only whitespace characters ' |
| 9414 | 'in the string\n' |
| 9415 | ' and there is at least one character, false ' |
| 9416 | 'otherwise. Whitespace\n' |
| 9417 | ' characters are those characters defined in the ' |
| 9418 | 'Unicode character\n' |
| 9419 | ' database as "Other" or "Separator" and those with ' |
| 9420 | 'bidirectional\n' |
| 9421 | ' property being one of "WS", "B", or "S".\n' |
| 9422 | '\n' |
| 9423 | 'str.istitle()\n' |
| 9424 | '\n' |
| 9425 | ' Return true if the string is a titlecased string and ' |
| 9426 | 'there is at\n' |
| 9427 | ' least one character, for example uppercase characters ' |
| 9428 | 'may only\n' |
| 9429 | ' follow uncased characters and lowercase characters ' |
| 9430 | 'only cased ones.\n' |
| 9431 | ' Return false otherwise.\n' |
| 9432 | '\n' |
| 9433 | 'str.isupper()\n' |
| 9434 | '\n' |
| 9435 | ' Return true if all cased characters [4] in the string ' |
| 9436 | 'are uppercase\n' |
| 9437 | ' and there is at least one cased character, false ' |
| 9438 | 'otherwise.\n' |
| 9439 | '\n' |
| 9440 | 'str.join(iterable)\n' |
| 9441 | '\n' |
| 9442 | ' Return a string which is the concatenation of the ' |
| 9443 | 'strings in the\n' |
| 9444 | ' *iterable* *iterable*. A "TypeError" will be raised ' |
| 9445 | 'if there are\n' |
| 9446 | ' any non-string values in *iterable*, including ' |
| 9447 | '"bytes" objects.\n' |
| 9448 | ' The separator between elements is the string ' |
| 9449 | 'providing this method.\n' |
| 9450 | '\n' |
| 9451 | 'str.ljust(width[, fillchar])\n' |
| 9452 | '\n' |
| 9453 | ' Return the string left justified in a string of ' |
| 9454 | 'length *width*.\n' |
| 9455 | ' Padding is done using the specified *fillchar* ' |
| 9456 | '(default is an ASCII\n' |
| 9457 | ' space). The original string is returned if *width* is ' |
| 9458 | 'less than or\n' |
| 9459 | ' equal to "len(s)".\n' |
| 9460 | '\n' |
| 9461 | 'str.lower()\n' |
| 9462 | '\n' |
| 9463 | ' Return a copy of the string with all the cased ' |
| 9464 | 'characters [4]\n' |
| 9465 | ' converted to lowercase.\n' |
| 9466 | '\n' |
| 9467 | ' The lowercasing algorithm used is described in ' |
| 9468 | 'section 3.13 of the\n' |
| 9469 | ' Unicode Standard.\n' |
| 9470 | '\n' |
| 9471 | 'str.lstrip([chars])\n' |
| 9472 | '\n' |
| 9473 | ' Return a copy of the string with leading characters ' |
| 9474 | 'removed. The\n' |
| 9475 | ' *chars* argument is a string specifying the set of ' |
| 9476 | 'characters to be\n' |
| 9477 | ' removed. If omitted or "None", the *chars* argument ' |
| 9478 | 'defaults to\n' |
| 9479 | ' removing whitespace. The *chars* argument is not a ' |
| 9480 | 'prefix; rather,\n' |
| 9481 | ' all combinations of its values are stripped:\n' |
| 9482 | '\n' |
| 9483 | " >>> ' spacious '.lstrip()\n" |
| 9484 | " 'spacious '\n" |
| 9485 | " >>> 'www.example.com'.lstrip('cmowz.')\n" |
| 9486 | " 'example.com'\n" |
| 9487 | '\n' |
| 9488 | 'static str.maketrans(x[, y[, z]])\n' |
| 9489 | '\n' |
| 9490 | ' This static method returns a translation table usable ' |
| 9491 | 'for\n' |
| 9492 | ' "str.translate()".\n' |
| 9493 | '\n' |
| 9494 | ' If there is only one argument, it must be a ' |
| 9495 | 'dictionary mapping\n' |
| 9496 | ' Unicode ordinals (integers) or characters (strings of ' |
| 9497 | 'length 1) to\n' |
| 9498 | ' Unicode ordinals, strings (of arbitrary lengths) or ' |
| 9499 | 'None.\n' |
| 9500 | ' Character keys will then be converted to ordinals.\n' |
| 9501 | '\n' |
| 9502 | ' If there are two arguments, they must be strings of ' |
| 9503 | 'equal length,\n' |
| 9504 | ' and in the resulting dictionary, each character in x ' |
| 9505 | 'will be mapped\n' |
| 9506 | ' to the character at the same position in y. If there ' |
| 9507 | 'is a third\n' |
| 9508 | ' argument, it must be a string, whose characters will ' |
| 9509 | 'be mapped to\n' |
| 9510 | ' None in the result.\n' |
| 9511 | '\n' |
| 9512 | 'str.partition(sep)\n' |
| 9513 | '\n' |
| 9514 | ' Split the string at the first occurrence of *sep*, ' |
| 9515 | 'and return a\n' |
| 9516 | ' 3-tuple containing the part before the separator, the ' |
| 9517 | 'separator\n' |
| 9518 | ' itself, and the part after the separator. If the ' |
| 9519 | 'separator is not\n' |
| 9520 | ' found, return a 3-tuple containing the string itself, ' |
| 9521 | 'followed by\n' |
| 9522 | ' two empty strings.\n' |
| 9523 | '\n' |
| 9524 | 'str.replace(old, new[, count])\n' |
| 9525 | '\n' |
| 9526 | ' Return a copy of the string with all occurrences of ' |
| 9527 | 'substring *old*\n' |
| 9528 | ' replaced by *new*. If the optional argument *count* ' |
| 9529 | 'is given, only\n' |
| 9530 | ' the first *count* occurrences are replaced.\n' |
| 9531 | '\n' |
| 9532 | 'str.rfind(sub[, start[, end]])\n' |
| 9533 | '\n' |
| 9534 | ' Return the highest index in the string where ' |
| 9535 | 'substring *sub* is\n' |
| 9536 | ' found, such that *sub* is contained within ' |
| 9537 | '"s[start:end]".\n' |
| 9538 | ' Optional arguments *start* and *end* are interpreted ' |
| 9539 | 'as in slice\n' |
| 9540 | ' notation. Return "-1" on failure.\n' |
| 9541 | '\n' |
| 9542 | 'str.rindex(sub[, start[, end]])\n' |
| 9543 | '\n' |
| 9544 | ' Like "rfind()" but raises "ValueError" when the ' |
| 9545 | 'substring *sub* is\n' |
| 9546 | ' not found.\n' |
| 9547 | '\n' |
| 9548 | 'str.rjust(width[, fillchar])\n' |
| 9549 | '\n' |
| 9550 | ' Return the string right justified in a string of ' |
| 9551 | 'length *width*.\n' |
| 9552 | ' Padding is done using the specified *fillchar* ' |
| 9553 | '(default is an ASCII\n' |
| 9554 | ' space). The original string is returned if *width* is ' |
| 9555 | 'less than or\n' |
| 9556 | ' equal to "len(s)".\n' |
| 9557 | '\n' |
| 9558 | 'str.rpartition(sep)\n' |
| 9559 | '\n' |
| 9560 | ' Split the string at the last occurrence of *sep*, and ' |
| 9561 | 'return a\n' |
| 9562 | ' 3-tuple containing the part before the separator, the ' |
| 9563 | 'separator\n' |
| 9564 | ' itself, and the part after the separator. If the ' |
| 9565 | 'separator is not\n' |
| 9566 | ' found, return a 3-tuple containing two empty strings, ' |
| 9567 | 'followed by\n' |
| 9568 | ' the string itself.\n' |
| 9569 | '\n' |
| 9570 | 'str.rsplit(sep=None, maxsplit=-1)\n' |
| 9571 | '\n' |
| 9572 | ' Return a list of the words in the string, using *sep* ' |
| 9573 | 'as the\n' |
| 9574 | ' delimiter string. If *maxsplit* is given, at most ' |
| 9575 | '*maxsplit* splits\n' |
| 9576 | ' are done, the *rightmost* ones. If *sep* is not ' |
| 9577 | 'specified or\n' |
| 9578 | ' "None", any whitespace string is a separator. Except ' |
| 9579 | 'for splitting\n' |
| 9580 | ' from the right, "rsplit()" behaves like "split()" ' |
| 9581 | 'which is\n' |
| 9582 | ' described in detail below.\n' |
| 9583 | '\n' |
| 9584 | 'str.rstrip([chars])\n' |
| 9585 | '\n' |
| 9586 | ' Return a copy of the string with trailing characters ' |
| 9587 | 'removed. The\n' |
| 9588 | ' *chars* argument is a string specifying the set of ' |
| 9589 | 'characters to be\n' |
| 9590 | ' removed. If omitted or "None", the *chars* argument ' |
| 9591 | 'defaults to\n' |
| 9592 | ' removing whitespace. The *chars* argument is not a ' |
| 9593 | 'suffix; rather,\n' |
| 9594 | ' all combinations of its values are stripped:\n' |
| 9595 | '\n' |
| 9596 | " >>> ' spacious '.rstrip()\n" |
| 9597 | " ' spacious'\n" |
| 9598 | " >>> 'mississippi'.rstrip('ipz')\n" |
| 9599 | " 'mississ'\n" |
| 9600 | '\n' |
| 9601 | 'str.split(sep=None, maxsplit=-1)\n' |
| 9602 | '\n' |
| 9603 | ' Return a list of the words in the string, using *sep* ' |
| 9604 | 'as the\n' |
| 9605 | ' delimiter string. If *maxsplit* is given, at most ' |
| 9606 | '*maxsplit*\n' |
| 9607 | ' splits are done (thus, the list will have at most ' |
| 9608 | '"maxsplit+1"\n' |
| 9609 | ' elements). If *maxsplit* is not specified or "-1", ' |
| 9610 | 'then there is\n' |
| 9611 | ' no limit on the number of splits (all possible splits ' |
| 9612 | 'are made).\n' |
| 9613 | '\n' |
| 9614 | ' If *sep* is given, consecutive delimiters are not ' |
| 9615 | 'grouped together\n' |
| 9616 | ' and are deemed to delimit empty strings (for ' |
| 9617 | 'example,\n' |
| 9618 | ' "\'1,,2\'.split(\',\')" returns "[\'1\', \'\', ' |
| 9619 | '\'2\']"). The *sep* argument\n' |
| 9620 | ' may consist of multiple characters (for example,\n' |
| 9621 | ' "\'1<>2<>3\'.split(\'<>\')" returns "[\'1\', \'2\', ' |
| 9622 | '\'3\']"). Splitting an\n' |
| 9623 | ' empty string with a specified separator returns ' |
| 9624 | '"[\'\']".\n' |
| 9625 | '\n' |
| 9626 | ' For example:\n' |
| 9627 | '\n' |
| 9628 | " >>> '1,2,3'.split(',')\n" |
| 9629 | " ['1', '2', '3']\n" |
| 9630 | " >>> '1,2,3'.split(',', maxsplit=1)\n" |
| 9631 | " ['1', '2,3']\n" |
| 9632 | " >>> '1,2,,3,'.split(',')\n" |
| 9633 | " ['1', '2', '', '3', '']\n" |
| 9634 | '\n' |
| 9635 | ' If *sep* is not specified or is "None", a different ' |
| 9636 | 'splitting\n' |
| 9637 | ' algorithm is applied: runs of consecutive whitespace ' |
| 9638 | 'are regarded\n' |
| 9639 | ' as a single separator, and the result will contain no ' |
| 9640 | 'empty strings\n' |
| 9641 | ' at the start or end if the string has leading or ' |
| 9642 | 'trailing\n' |
| 9643 | ' whitespace. Consequently, splitting an empty string ' |
| 9644 | 'or a string\n' |
| 9645 | ' consisting of just whitespace with a "None" separator ' |
| 9646 | 'returns "[]".\n' |
| 9647 | '\n' |
| 9648 | ' For example:\n' |
| 9649 | '\n' |
| 9650 | " >>> '1 2 3'.split()\n" |
| 9651 | " ['1', '2', '3']\n" |
| 9652 | " >>> '1 2 3'.split(maxsplit=1)\n" |
| 9653 | " ['1', '2 3']\n" |
| 9654 | " >>> ' 1 2 3 '.split()\n" |
| 9655 | " ['1', '2', '3']\n" |
| 9656 | '\n' |
| 9657 | 'str.splitlines([keepends])\n' |
| 9658 | '\n' |
| 9659 | ' Return a list of the lines in the string, breaking at ' |
| 9660 | 'line\n' |
| 9661 | ' boundaries. Line breaks are not included in the ' |
| 9662 | 'resulting list\n' |
| 9663 | ' unless *keepends* is given and true.\n' |
| 9664 | '\n' |
| 9665 | ' This method splits on the following line boundaries. ' |
| 9666 | 'In\n' |
| 9667 | ' particular, the boundaries are a superset of ' |
| 9668 | '*universal newlines*.\n' |
| 9669 | '\n' |
| 9670 | ' ' |
| 9671 | '+-------------------------+-------------------------------+\n' |
| 9672 | ' | Representation | ' |
| 9673 | 'Description |\n' |
| 9674 | ' ' |
| 9675 | '+=========================+===============================+\n' |
| 9676 | ' | "\\n" | Line ' |
| 9677 | 'Feed |\n' |
| 9678 | ' ' |
| 9679 | '+-------------------------+-------------------------------+\n' |
| 9680 | ' | "\\r" | Carriage ' |
| 9681 | 'Return |\n' |
| 9682 | ' ' |
| 9683 | '+-------------------------+-------------------------------+\n' |
| 9684 | ' | "\\r\\n" | Carriage Return + Line ' |
| 9685 | 'Feed |\n' |
| 9686 | ' ' |
| 9687 | '+-------------------------+-------------------------------+\n' |
| 9688 | ' | "\\v" or "\\x0b" | Line ' |
| 9689 | 'Tabulation |\n' |
| 9690 | ' ' |
| 9691 | '+-------------------------+-------------------------------+\n' |
| 9692 | ' | "\\f" or "\\x0c" | Form ' |
| 9693 | 'Feed |\n' |
| 9694 | ' ' |
| 9695 | '+-------------------------+-------------------------------+\n' |
| 9696 | ' | "\\x1c" | File ' |
| 9697 | 'Separator |\n' |
| 9698 | ' ' |
| 9699 | '+-------------------------+-------------------------------+\n' |
| 9700 | ' | "\\x1d" | Group ' |
| 9701 | 'Separator |\n' |
| 9702 | ' ' |
| 9703 | '+-------------------------+-------------------------------+\n' |
| 9704 | ' | "\\x1e" | Record ' |
| 9705 | 'Separator |\n' |
| 9706 | ' ' |
| 9707 | '+-------------------------+-------------------------------+\n' |
| 9708 | ' | "\\x85" | Next Line (C1 Control ' |
| 9709 | 'Code) |\n' |
| 9710 | ' ' |
| 9711 | '+-------------------------+-------------------------------+\n' |
| 9712 | ' | "\\u2028" | Line ' |
| 9713 | 'Separator |\n' |
| 9714 | ' ' |
| 9715 | '+-------------------------+-------------------------------+\n' |
| 9716 | ' | "\\u2029" | Paragraph ' |
| 9717 | 'Separator |\n' |
| 9718 | ' ' |
| 9719 | '+-------------------------+-------------------------------+\n' |
| 9720 | '\n' |
| 9721 | ' Changed in version 3.2: "\\v" and "\\f" added to list ' |
| 9722 | 'of line\n' |
| 9723 | ' boundaries.\n' |
| 9724 | '\n' |
| 9725 | ' For example:\n' |
| 9726 | '\n' |
| 9727 | " >>> 'ab c\\n\\nde fg\\rkl\\r\\n'.splitlines()\n" |
| 9728 | " ['ab c', '', 'de fg', 'kl']\n" |
| 9729 | " >>> 'ab c\\n\\nde " |
| 9730 | "fg\\rkl\\r\\n'.splitlines(keepends=True)\n" |
| 9731 | " ['ab c\\n', '\\n', 'de fg\\r', 'kl\\r\\n']\n" |
| 9732 | '\n' |
| 9733 | ' Unlike "split()" when a delimiter string *sep* is ' |
| 9734 | 'given, this\n' |
| 9735 | ' method returns an empty list for the empty string, ' |
| 9736 | 'and a terminal\n' |
| 9737 | ' line break does not result in an extra line:\n' |
| 9738 | '\n' |
| 9739 | ' >>> "".splitlines()\n' |
| 9740 | ' []\n' |
| 9741 | ' >>> "One line\\n".splitlines()\n' |
| 9742 | " ['One line']\n" |
| 9743 | '\n' |
| 9744 | ' For comparison, "split(\'\\n\')" gives:\n' |
| 9745 | '\n' |
| 9746 | " >>> ''.split('\\n')\n" |
| 9747 | " ['']\n" |
| 9748 | " >>> 'Two lines\\n'.split('\\n')\n" |
| 9749 | " ['Two lines', '']\n" |
| 9750 | '\n' |
| 9751 | 'str.startswith(prefix[, start[, end]])\n' |
| 9752 | '\n' |
| 9753 | ' Return "True" if string starts with the *prefix*, ' |
| 9754 | 'otherwise return\n' |
| 9755 | ' "False". *prefix* can also be a tuple of prefixes to ' |
| 9756 | 'look for.\n' |
| 9757 | ' With optional *start*, test string beginning at that ' |
| 9758 | 'position.\n' |
| 9759 | ' With optional *end*, stop comparing string at that ' |
| 9760 | 'position.\n' |
| 9761 | '\n' |
| 9762 | 'str.strip([chars])\n' |
| 9763 | '\n' |
| 9764 | ' Return a copy of the string with the leading and ' |
| 9765 | 'trailing\n' |
| 9766 | ' characters removed. The *chars* argument is a string ' |
| 9767 | 'specifying the\n' |
| 9768 | ' set of characters to be removed. If omitted or ' |
| 9769 | '"None", the *chars*\n' |
| 9770 | ' argument defaults to removing whitespace. The *chars* ' |
| 9771 | 'argument is\n' |
| 9772 | ' not a prefix or suffix; rather, all combinations of ' |
| 9773 | 'its values are\n' |
| 9774 | ' stripped:\n' |
| 9775 | '\n' |
| 9776 | " >>> ' spacious '.strip()\n" |
| 9777 | " 'spacious'\n" |
| 9778 | " >>> 'www.example.com'.strip('cmowz.')\n" |
| 9779 | " 'example'\n" |
| 9780 | '\n' |
| 9781 | ' The outermost leading and trailing *chars* argument ' |
| 9782 | 'values are\n' |
| 9783 | ' stripped from the string. Characters are removed from ' |
| 9784 | 'the leading\n' |
| 9785 | ' end until reaching a string character that is not ' |
| 9786 | 'contained in the\n' |
| 9787 | ' set of characters in *chars*. A similar action takes ' |
| 9788 | 'place on the\n' |
| 9789 | ' trailing end. For example:\n' |
| 9790 | '\n' |
| 9791 | " >>> comment_string = '#....... Section 3.2.1 Issue " |
| 9792 | "#32 .......'\n" |
| 9793 | " >>> comment_string.strip('.#! ')\n" |
| 9794 | " 'Section 3.2.1 Issue #32'\n" |
| 9795 | '\n' |
| 9796 | 'str.swapcase()\n' |
| 9797 | '\n' |
| 9798 | ' Return a copy of the string with uppercase characters ' |
| 9799 | 'converted to\n' |
| 9800 | ' lowercase and vice versa. Note that it is not ' |
| 9801 | 'necessarily true that\n' |
| 9802 | ' "s.swapcase().swapcase() == s".\n' |
| 9803 | '\n' |
| 9804 | 'str.title()\n' |
| 9805 | '\n' |
| 9806 | ' Return a titlecased version of the string where words ' |
| 9807 | 'start with an\n' |
| 9808 | ' uppercase character and the remaining characters are ' |
| 9809 | 'lowercase.\n' |
| 9810 | '\n' |
| 9811 | ' For example:\n' |
| 9812 | '\n' |
| 9813 | " >>> 'Hello world'.title()\n" |
| 9814 | " 'Hello World'\n" |
| 9815 | '\n' |
| 9816 | ' The algorithm uses a simple language-independent ' |
| 9817 | 'definition of a\n' |
| 9818 | ' word as groups of consecutive letters. The ' |
| 9819 | 'definition works in\n' |
| 9820 | ' many contexts but it means that apostrophes in ' |
| 9821 | 'contractions and\n' |
| 9822 | ' possessives form word boundaries, which may not be ' |
| 9823 | 'the desired\n' |
| 9824 | ' result:\n' |
| 9825 | '\n' |
| 9826 | ' >>> "they\'re bill\'s friends from the ' |
| 9827 | 'UK".title()\n' |
| 9828 | ' "They\'Re Bill\'S Friends From The Uk"\n' |
| 9829 | '\n' |
| 9830 | ' A workaround for apostrophes can be constructed using ' |
| 9831 | 'regular\n' |
| 9832 | ' expressions:\n' |
| 9833 | '\n' |
| 9834 | ' >>> import re\n' |
| 9835 | ' >>> def titlecase(s):\n' |
| 9836 | ' ... return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n' |
| 9837 | ' ... lambda mo: ' |
| 9838 | 'mo.group(0)[0].upper() +\n' |
| 9839 | ' ... ' |
| 9840 | 'mo.group(0)[1:].lower(),\n' |
| 9841 | ' ... s)\n' |
| 9842 | ' ...\n' |
| 9843 | ' >>> titlecase("they\'re bill\'s friends.")\n' |
| 9844 | ' "They\'re Bill\'s Friends."\n' |
| 9845 | '\n' |
| 9846 | 'str.translate(table)\n' |
| 9847 | '\n' |
| 9848 | ' Return a copy of the string in which each character ' |
| 9849 | 'has been mapped\n' |
| 9850 | ' through the given translation table. The table must ' |
| 9851 | 'be an object\n' |
| 9852 | ' that implements indexing via "__getitem__()", ' |
| 9853 | 'typically a *mapping*\n' |
| 9854 | ' or *sequence*. When indexed by a Unicode ordinal (an ' |
| 9855 | 'integer), the\n' |
| 9856 | ' table object can do any of the following: return a ' |
| 9857 | 'Unicode ordinal\n' |
| 9858 | ' or a string, to map the character to one or more ' |
| 9859 | 'other characters;\n' |
| 9860 | ' return "None", to delete the character from the ' |
| 9861 | 'return string; or\n' |
| 9862 | ' raise a "LookupError" exception, to map the character ' |
| 9863 | 'to itself.\n' |
| 9864 | '\n' |
| 9865 | ' You can use "str.maketrans()" to create a translation ' |
| 9866 | 'map from\n' |
| 9867 | ' character-to-character mappings in different ' |
| 9868 | 'formats.\n' |
| 9869 | '\n' |
| 9870 | ' See also the "codecs" module for a more flexible ' |
| 9871 | 'approach to custom\n' |
| 9872 | ' character mappings.\n' |
| 9873 | '\n' |
| 9874 | 'str.upper()\n' |
| 9875 | '\n' |
| 9876 | ' Return a copy of the string with all the cased ' |
| 9877 | 'characters [4]\n' |
| 9878 | ' converted to uppercase. Note that ' |
| 9879 | '"str.upper().isupper()" might be\n' |
| 9880 | ' "False" if "s" contains uncased characters or if the ' |
| 9881 | 'Unicode\n' |
| 9882 | ' category of the resulting character(s) is not "Lu" ' |
| 9883 | '(Letter,\n' |
| 9884 | ' uppercase), but e.g. "Lt" (Letter, titlecase).\n' |
| 9885 | '\n' |
| 9886 | ' The uppercasing algorithm used is described in ' |
| 9887 | 'section 3.13 of the\n' |
| 9888 | ' Unicode Standard.\n' |
| 9889 | '\n' |
| 9890 | 'str.zfill(width)\n' |
| 9891 | '\n' |
| 9892 | ' Return a copy of the string left filled with ASCII ' |
| 9893 | '"\'0\'" digits to\n' |
| 9894 | ' make a string of length *width*. A leading sign ' |
| 9895 | 'prefix\n' |
| 9896 | ' ("\'+\'"/"\'-\'") is handled by inserting the padding ' |
| 9897 | '*after* the sign\n' |
| 9898 | ' character rather than before. The original string is ' |
| 9899 | 'returned if\n' |
| 9900 | ' *width* is less than or equal to "len(s)".\n' |
| 9901 | '\n' |
| 9902 | ' For example:\n' |
| 9903 | '\n' |
| 9904 | ' >>> "42".zfill(5)\n' |
| 9905 | " '00042'\n" |
| 9906 | ' >>> "-42".zfill(5)\n' |
| 9907 | " '-0042'\n", |
| 9908 | 'strings': '\n' |
| 9909 | 'String and Bytes literals\n' |
| 9910 | '*************************\n' |
| 9911 | '\n' |
| 9912 | 'String literals are described by the following lexical ' |
| 9913 | 'definitions:\n' |
| 9914 | '\n' |
| 9915 | ' stringliteral ::= [stringprefix](shortstring | ' |
| 9916 | 'longstring)\n' |
| 9917 | ' stringprefix ::= "r" | "u" | "R" | "U"\n' |
| 9918 | ' shortstring ::= "\'" shortstringitem* "\'" | \'"\' ' |
| 9919 | 'shortstringitem* \'"\'\n' |
| 9920 | ' longstring ::= "\'\'\'" longstringitem* "\'\'\'" | ' |
| 9921 | '\'"""\' longstringitem* \'"""\'\n' |
| 9922 | ' shortstringitem ::= shortstringchar | stringescapeseq\n' |
| 9923 | ' longstringitem ::= longstringchar | stringescapeseq\n' |
| 9924 | ' shortstringchar ::= <any source character except "\\" or ' |
| 9925 | 'newline or the quote>\n' |
| 9926 | ' longstringchar ::= <any source character except "\\">\n' |
| 9927 | ' stringescapeseq ::= "\\" <any source character>\n' |
| 9928 | '\n' |
| 9929 | ' bytesliteral ::= bytesprefix(shortbytes | longbytes)\n' |
| 9930 | ' bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" | ' |
| 9931 | '"rb" | "rB" | "Rb" | "RB"\n' |
| 9932 | ' shortbytes ::= "\'" shortbytesitem* "\'" | \'"\' ' |
| 9933 | 'shortbytesitem* \'"\'\n' |
| 9934 | ' longbytes ::= "\'\'\'" longbytesitem* "\'\'\'" | ' |
| 9935 | '\'"""\' longbytesitem* \'"""\'\n' |
| 9936 | ' shortbytesitem ::= shortbyteschar | bytesescapeseq\n' |
| 9937 | ' longbytesitem ::= longbyteschar | bytesescapeseq\n' |
| 9938 | ' shortbyteschar ::= <any ASCII character except "\\" or ' |
| 9939 | 'newline or the quote>\n' |
| 9940 | ' longbyteschar ::= <any ASCII character except "\\">\n' |
| 9941 | ' bytesescapeseq ::= "\\" <any ASCII character>\n' |
| 9942 | '\n' |
| 9943 | 'One syntactic restriction not indicated by these productions is ' |
| 9944 | 'that\n' |
| 9945 | 'whitespace is not allowed between the "stringprefix" or ' |
| 9946 | '"bytesprefix"\n' |
| 9947 | 'and the rest of the literal. The source character set is ' |
| 9948 | 'defined by\n' |
| 9949 | 'the encoding declaration; it is UTF-8 if no encoding ' |
| 9950 | 'declaration is\n' |
| 9951 | 'given in the source file; see section *Encoding declarations*.\n' |
| 9952 | '\n' |
| 9953 | 'In plain English: Both types of literals can be enclosed in ' |
| 9954 | 'matching\n' |
| 9955 | 'single quotes ("\'") or double quotes ("""). They can also be ' |
| 9956 | 'enclosed\n' |
| 9957 | 'in matching groups of three single or double quotes (these are\n' |
| 9958 | 'generally referred to as *triple-quoted strings*). The ' |
| 9959 | 'backslash\n' |
| 9960 | '("\\") character is used to escape characters that otherwise ' |
| 9961 | 'have a\n' |
| 9962 | 'special meaning, such as newline, backslash itself, or the ' |
| 9963 | 'quote\n' |
| 9964 | 'character.\n' |
| 9965 | '\n' |
| 9966 | 'Bytes literals are always prefixed with "\'b\'" or "\'B\'"; ' |
| 9967 | 'they produce\n' |
| 9968 | 'an instance of the "bytes" type instead of the "str" type. ' |
| 9969 | 'They may\n' |
| 9970 | 'only contain ASCII characters; bytes with a numeric value of ' |
| 9971 | '128 or\n' |
| 9972 | 'greater must be expressed with escapes.\n' |
| 9973 | '\n' |
| 9974 | 'As of Python 3.3 it is possible again to prefix string literals ' |
| 9975 | 'with a\n' |
| 9976 | '"u" prefix to simplify maintenance of dual 2.x and 3.x ' |
| 9977 | 'codebases.\n' |
| 9978 | '\n' |
| 9979 | 'Both string and bytes literals may optionally be prefixed with ' |
| 9980 | 'a\n' |
| 9981 | 'letter "\'r\'" or "\'R\'"; such strings are called *raw ' |
| 9982 | 'strings* and treat\n' |
| 9983 | 'backslashes as literal characters. As a result, in string ' |
| 9984 | 'literals,\n' |
| 9985 | '"\'\\U\'" and "\'\\u\'" escapes in raw strings are not treated ' |
| 9986 | 'specially.\n' |
| 9987 | "Given that Python 2.x's raw unicode literals behave differently " |
| 9988 | 'than\n' |
| 9989 | 'Python 3.x\'s the "\'ur\'" syntax is not supported.\n' |
| 9990 | '\n' |
| 9991 | 'New in version 3.3: The "\'rb\'" prefix of raw bytes literals ' |
| 9992 | 'has been\n' |
| 9993 | 'added as a synonym of "\'br\'".\n' |
| 9994 | '\n' |
| 9995 | 'New in version 3.3: Support for the unicode legacy literal\n' |
| 9996 | '("u\'value\'") was reintroduced to simplify the maintenance of ' |
| 9997 | 'dual\n' |
| 9998 | 'Python 2.x and 3.x codebases. See **PEP 414** for more ' |
| 9999 | 'information.\n' |
| 10000 | '\n' |
| 10001 | 'In triple-quoted literals, unescaped newlines and quotes are ' |
| 10002 | 'allowed\n' |
| 10003 | '(and are retained), except that three unescaped quotes in a ' |
| 10004 | 'row\n' |
| 10005 | 'terminate the literal. (A "quote" is the character used to ' |
| 10006 | 'open the\n' |
| 10007 | 'literal, i.e. either "\'" or """.)\n' |
| 10008 | '\n' |
| 10009 | 'Unless an "\'r\'" or "\'R\'" prefix is present, escape ' |
| 10010 | 'sequences in string\n' |
| 10011 | 'and bytes literals are interpreted according to rules similar ' |
| 10012 | 'to those\n' |
| 10013 | 'used by Standard C. The recognized escape sequences are:\n' |
| 10014 | '\n' |
| 10015 | '+-------------------+-----------------------------------+---------+\n' |
| 10016 | '| Escape Sequence | Meaning | ' |
| 10017 | 'Notes |\n' |
| 10018 | '+===================+===================================+=========+\n' |
| 10019 | '| "\\newline" | Backslash and newline ignored ' |
| 10020 | '| |\n' |
| 10021 | '+-------------------+-----------------------------------+---------+\n' |
| 10022 | '| "\\\\" | Backslash ("\\") ' |
| 10023 | '| |\n' |
| 10024 | '+-------------------+-----------------------------------+---------+\n' |
| 10025 | '| "\\\'" | Single quote ("\'") ' |
| 10026 | '| |\n' |
| 10027 | '+-------------------+-----------------------------------+---------+\n' |
| 10028 | '| "\\"" | Double quote (""") ' |
| 10029 | '| |\n' |
| 10030 | '+-------------------+-----------------------------------+---------+\n' |
| 10031 | '| "\\a" | ASCII Bell (BEL) ' |
| 10032 | '| |\n' |
| 10033 | '+-------------------+-----------------------------------+---------+\n' |
| 10034 | '| "\\b" | ASCII Backspace (BS) ' |
| 10035 | '| |\n' |
| 10036 | '+-------------------+-----------------------------------+---------+\n' |
| 10037 | '| "\\f" | ASCII Formfeed (FF) ' |
| 10038 | '| |\n' |
| 10039 | '+-------------------+-----------------------------------+---------+\n' |
| 10040 | '| "\\n" | ASCII Linefeed (LF) ' |
| 10041 | '| |\n' |
| 10042 | '+-------------------+-----------------------------------+---------+\n' |
| 10043 | '| "\\r" | ASCII Carriage Return (CR) ' |
| 10044 | '| |\n' |
| 10045 | '+-------------------+-----------------------------------+---------+\n' |
| 10046 | '| "\\t" | ASCII Horizontal Tab (TAB) ' |
| 10047 | '| |\n' |
| 10048 | '+-------------------+-----------------------------------+---------+\n' |
| 10049 | '| "\\v" | ASCII Vertical Tab (VT) ' |
| 10050 | '| |\n' |
| 10051 | '+-------------------+-----------------------------------+---------+\n' |
| 10052 | '| "\\ooo" | Character with octal value *ooo* | ' |
| 10053 | '(1,3) |\n' |
| 10054 | '+-------------------+-----------------------------------+---------+\n' |
| 10055 | '| "\\xhh" | Character with hex value *hh* | ' |
| 10056 | '(2,3) |\n' |
| 10057 | '+-------------------+-----------------------------------+---------+\n' |
| 10058 | '\n' |
| 10059 | 'Escape sequences only recognized in string literals are:\n' |
| 10060 | '\n' |
| 10061 | '+-------------------+-----------------------------------+---------+\n' |
| 10062 | '| Escape Sequence | Meaning | ' |
| 10063 | 'Notes |\n' |
| 10064 | '+===================+===================================+=========+\n' |
| 10065 | '| "\\N{name}" | Character named *name* in the | ' |
| 10066 | '(4) |\n' |
| 10067 | '| | Unicode database ' |
| 10068 | '| |\n' |
| 10069 | '+-------------------+-----------------------------------+---------+\n' |
| 10070 | '| "\\uxxxx" | Character with 16-bit hex value | ' |
| 10071 | '(5) |\n' |
| 10072 | '| | *xxxx* ' |
| 10073 | '| |\n' |
| 10074 | '+-------------------+-----------------------------------+---------+\n' |
| 10075 | '| "\\Uxxxxxxxx" | Character with 32-bit hex value | ' |
| 10076 | '(6) |\n' |
| 10077 | '| | *xxxxxxxx* ' |
| 10078 | '| |\n' |
| 10079 | '+-------------------+-----------------------------------+---------+\n' |
| 10080 | '\n' |
| 10081 | 'Notes:\n' |
| 10082 | '\n' |
| 10083 | '1. As in Standard C, up to three octal digits are accepted.\n' |
| 10084 | '\n' |
| 10085 | '2. Unlike in Standard C, exactly two hex digits are required.\n' |
| 10086 | '\n' |
| 10087 | '3. In a bytes literal, hexadecimal and octal escapes denote ' |
| 10088 | 'the\n' |
| 10089 | ' byte with the given value. In a string literal, these ' |
| 10090 | 'escapes\n' |
| 10091 | ' denote a Unicode character with the given value.\n' |
| 10092 | '\n' |
| 10093 | '4. Changed in version 3.3: Support for name aliases [1] has ' |
| 10094 | 'been\n' |
| 10095 | ' added.\n' |
| 10096 | '\n' |
| 10097 | '5. Individual code units which form parts of a surrogate pair ' |
| 10098 | 'can\n' |
| 10099 | ' be encoded using this escape sequence. Exactly four hex ' |
| 10100 | 'digits are\n' |
| 10101 | ' required.\n' |
| 10102 | '\n' |
| 10103 | '6. Any Unicode character can be encoded this way. Exactly ' |
| 10104 | 'eight\n' |
| 10105 | ' hex digits are required.\n' |
| 10106 | '\n' |
| 10107 | 'Unlike Standard C, all unrecognized escape sequences are left ' |
| 10108 | 'in the\n' |
| 10109 | 'string unchanged, i.e., *the backslash is left in the result*. ' |
| 10110 | '(This\n' |
| 10111 | 'behavior is useful when debugging: if an escape sequence is ' |
| 10112 | 'mistyped,\n' |
| 10113 | 'the resulting output is more easily recognized as broken.) It ' |
| 10114 | 'is also\n' |
| 10115 | 'important to note that the escape sequences only recognized in ' |
| 10116 | 'string\n' |
| 10117 | 'literals fall into the category of unrecognized escapes for ' |
| 10118 | 'bytes\n' |
| 10119 | 'literals.\n' |
| 10120 | '\n' |
| 10121 | 'Even in a raw literal, quotes can be escaped with a backslash, ' |
| 10122 | 'but the\n' |
| 10123 | 'backslash remains in the result; for example, "r"\\""" is a ' |
| 10124 | 'valid\n' |
| 10125 | 'string literal consisting of two characters: a backslash and a ' |
| 10126 | 'double\n' |
| 10127 | 'quote; "r"\\"" is not a valid string literal (even a raw string ' |
| 10128 | 'cannot\n' |
| 10129 | 'end in an odd number of backslashes). Specifically, *a raw ' |
| 10130 | 'literal\n' |
| 10131 | 'cannot end in a single backslash* (since the backslash would ' |
| 10132 | 'escape\n' |
| 10133 | 'the following quote character). Note also that a single ' |
| 10134 | 'backslash\n' |
| 10135 | 'followed by a newline is interpreted as those two characters as ' |
| 10136 | 'part\n' |
| 10137 | 'of the literal, *not* as a line continuation.\n', |
| 10138 | 'subscriptions': '\n' |
| 10139 | 'Subscriptions\n' |
| 10140 | '*************\n' |
| 10141 | '\n' |
| 10142 | 'A subscription selects an item of a sequence (string, ' |
| 10143 | 'tuple or list)\n' |
| 10144 | 'or mapping (dictionary) object:\n' |
| 10145 | '\n' |
| 10146 | ' subscription ::= primary "[" expression_list "]"\n' |
| 10147 | '\n' |
| 10148 | 'The primary must evaluate to an object that supports ' |
| 10149 | 'subscription\n' |
| 10150 | '(lists or dictionaries for example). User-defined ' |
| 10151 | 'objects can support\n' |
| 10152 | 'subscription by defining a "__getitem__()" method.\n' |
| 10153 | '\n' |
| 10154 | 'For built-in objects, there are two types of objects that ' |
| 10155 | 'support\n' |
| 10156 | 'subscription:\n' |
| 10157 | '\n' |
| 10158 | 'If the primary is a mapping, the expression list must ' |
| 10159 | 'evaluate to an\n' |
| 10160 | 'object whose value is one of the keys of the mapping, and ' |
| 10161 | 'the\n' |
| 10162 | 'subscription selects the value in the mapping that ' |
| 10163 | 'corresponds to that\n' |
| 10164 | 'key. (The expression list is a tuple except if it has ' |
| 10165 | 'exactly one\n' |
| 10166 | 'item.)\n' |
| 10167 | '\n' |
| 10168 | 'If the primary is a sequence, the expression (list) must ' |
| 10169 | 'evaluate to\n' |
| 10170 | 'an integer or a slice (as discussed in the following ' |
| 10171 | 'section).\n' |
| 10172 | '\n' |
| 10173 | 'The formal syntax makes no special provision for negative ' |
| 10174 | 'indices in\n' |
| 10175 | 'sequences; however, built-in sequences all provide a ' |
| 10176 | '"__getitem__()"\n' |
| 10177 | 'method that interprets negative indices by adding the ' |
| 10178 | 'length of the\n' |
| 10179 | 'sequence to the index (so that "x[-1]" selects the last ' |
| 10180 | 'item of "x").\n' |
| 10181 | 'The resulting value must be a nonnegative integer less ' |
| 10182 | 'than the number\n' |
| 10183 | 'of items in the sequence, and the subscription selects ' |
| 10184 | 'the item whose\n' |
| 10185 | 'index is that value (counting from zero). Since the ' |
| 10186 | 'support for\n' |
| 10187 | "negative indices and slicing occurs in the object's " |
| 10188 | '"__getitem__()"\n' |
| 10189 | 'method, subclasses overriding this method will need to ' |
| 10190 | 'explicitly add\n' |
| 10191 | 'that support.\n' |
| 10192 | '\n' |
| 10193 | "A string's items are characters. A character is not a " |
| 10194 | 'separate data\n' |
| 10195 | 'type but a string of exactly one character.\n', |
| 10196 | 'truth': '\n' |
| 10197 | 'Truth Value Testing\n' |
| 10198 | '*******************\n' |
| 10199 | '\n' |
| 10200 | 'Any object can be tested for truth value, for use in an "if" or\n' |
| 10201 | '"while" condition or as operand of the Boolean operations below. ' |
| 10202 | 'The\n' |
| 10203 | 'following values are considered false:\n' |
| 10204 | '\n' |
| 10205 | '* "None"\n' |
| 10206 | '\n' |
| 10207 | '* "False"\n' |
| 10208 | '\n' |
| 10209 | '* zero of any numeric type, for example, "0", "0.0", "0j".\n' |
| 10210 | '\n' |
| 10211 | '* any empty sequence, for example, "\'\'", "()", "[]".\n' |
| 10212 | '\n' |
| 10213 | '* any empty mapping, for example, "{}".\n' |
| 10214 | '\n' |
| 10215 | '* instances of user-defined classes, if the class defines a\n' |
| 10216 | ' "__bool__()" or "__len__()" method, when that method returns ' |
| 10217 | 'the\n' |
| 10218 | ' integer zero or "bool" value "False". [1]\n' |
| 10219 | '\n' |
| 10220 | 'All other values are considered true --- so objects of many types ' |
| 10221 | 'are\n' |
| 10222 | 'always true.\n' |
| 10223 | '\n' |
| 10224 | 'Operations and built-in functions that have a Boolean result ' |
| 10225 | 'always\n' |
| 10226 | 'return "0" or "False" for false and "1" or "True" for true, ' |
| 10227 | 'unless\n' |
| 10228 | 'otherwise stated. (Important exception: the Boolean operations ' |
| 10229 | '"or"\n' |
| 10230 | 'and "and" always return one of their operands.)\n', |
| 10231 | 'try': '\n' |
| 10232 | 'The "try" statement\n' |
| 10233 | '*******************\n' |
| 10234 | '\n' |
| 10235 | 'The "try" statement specifies exception handlers and/or cleanup ' |
| 10236 | 'code\n' |
| 10237 | 'for a group of statements:\n' |
| 10238 | '\n' |
| 10239 | ' try_stmt ::= try1_stmt | try2_stmt\n' |
| 10240 | ' try1_stmt ::= "try" ":" suite\n' |
| 10241 | ' ("except" [expression ["as" identifier]] ":" ' |
| 10242 | 'suite)+\n' |
| 10243 | ' ["else" ":" suite]\n' |
| 10244 | ' ["finally" ":" suite]\n' |
| 10245 | ' try2_stmt ::= "try" ":" suite\n' |
| 10246 | ' "finally" ":" suite\n' |
| 10247 | '\n' |
| 10248 | 'The "except" clause(s) specify one or more exception handlers. When ' |
| 10249 | 'no\n' |
| 10250 | 'exception occurs in the "try" clause, no exception handler is\n' |
| 10251 | 'executed. When an exception occurs in the "try" suite, a search for ' |
| 10252 | 'an\n' |
| 10253 | 'exception handler is started. This search inspects the except ' |
| 10254 | 'clauses\n' |
| 10255 | 'in turn until one is found that matches the exception. An ' |
| 10256 | 'expression-\n' |
| 10257 | 'less except clause, if present, must be last; it matches any\n' |
| 10258 | 'exception. For an except clause with an expression, that ' |
| 10259 | 'expression\n' |
| 10260 | 'is evaluated, and the clause matches the exception if the ' |
| 10261 | 'resulting\n' |
| 10262 | 'object is "compatible" with the exception. An object is ' |
| 10263 | 'compatible\n' |
| 10264 | 'with an exception if it is the class or a base class of the ' |
| 10265 | 'exception\n' |
| 10266 | 'object or a tuple containing an item compatible with the ' |
| 10267 | 'exception.\n' |
| 10268 | '\n' |
| 10269 | 'If no except clause matches the exception, the search for an ' |
| 10270 | 'exception\n' |
| 10271 | 'handler continues in the surrounding code and on the invocation ' |
| 10272 | 'stack.\n' |
| 10273 | '[1]\n' |
| 10274 | '\n' |
| 10275 | 'If the evaluation of an expression in the header of an except ' |
| 10276 | 'clause\n' |
| 10277 | 'raises an exception, the original search for a handler is canceled ' |
| 10278 | 'and\n' |
| 10279 | 'a search starts for the new exception in the surrounding code and ' |
| 10280 | 'on\n' |
| 10281 | 'the call stack (it is treated as if the entire "try" statement ' |
| 10282 | 'raised\n' |
| 10283 | 'the exception).\n' |
| 10284 | '\n' |
| 10285 | 'When a matching except clause is found, the exception is assigned ' |
| 10286 | 'to\n' |
| 10287 | 'the target specified after the "as" keyword in that except clause, ' |
| 10288 | 'if\n' |
| 10289 | "present, and the except clause's suite is executed. All except\n" |
| 10290 | 'clauses must have an executable block. When the end of this block ' |
| 10291 | 'is\n' |
| 10292 | 'reached, execution continues normally after the entire try ' |
| 10293 | 'statement.\n' |
| 10294 | '(This means that if two nested handlers exist for the same ' |
| 10295 | 'exception,\n' |
| 10296 | 'and the exception occurs in the try clause of the inner handler, ' |
| 10297 | 'the\n' |
| 10298 | 'outer handler will not handle the exception.)\n' |
| 10299 | '\n' |
| 10300 | 'When an exception has been assigned using "as target", it is ' |
| 10301 | 'cleared\n' |
| 10302 | 'at the end of the except clause. This is as if\n' |
| 10303 | '\n' |
| 10304 | ' except E as N:\n' |
| 10305 | ' foo\n' |
| 10306 | '\n' |
| 10307 | 'was translated to\n' |
| 10308 | '\n' |
| 10309 | ' except E as N:\n' |
| 10310 | ' try:\n' |
| 10311 | ' foo\n' |
| 10312 | ' finally:\n' |
| 10313 | ' del N\n' |
| 10314 | '\n' |
| 10315 | 'This means the exception must be assigned to a different name to ' |
| 10316 | 'be\n' |
| 10317 | 'able to refer to it after the except clause. Exceptions are ' |
| 10318 | 'cleared\n' |
| 10319 | 'because with the traceback attached to them, they form a reference\n' |
| 10320 | 'cycle with the stack frame, keeping all locals in that frame alive\n' |
| 10321 | 'until the next garbage collection occurs.\n' |
| 10322 | '\n' |
| 10323 | "Before an except clause's suite is executed, details about the\n" |
| 10324 | 'exception are stored in the "sys" module and can be accessed via\n' |
| 10325 | '"sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting of ' |
| 10326 | 'the\n' |
| 10327 | 'exception class, the exception instance and a traceback object ' |
| 10328 | '(see\n' |
| 10329 | 'section *The standard type hierarchy*) identifying the point in ' |
| 10330 | 'the\n' |
| 10331 | 'program where the exception occurred. "sys.exc_info()" values are\n' |
| 10332 | 'restored to their previous values (before the call) when returning\n' |
| 10333 | 'from a function that handled an exception.\n' |
| 10334 | '\n' |
| 10335 | 'The optional "else" clause is executed if and when control flows ' |
| 10336 | 'off\n' |
| 10337 | 'the end of the "try" clause. [2] Exceptions in the "else" clause ' |
| 10338 | 'are\n' |
| 10339 | 'not handled by the preceding "except" clauses.\n' |
| 10340 | '\n' |
| 10341 | 'If "finally" is present, it specifies a \'cleanup\' handler. The ' |
| 10342 | '"try"\n' |
| 10343 | 'clause is executed, including any "except" and "else" clauses. If ' |
| 10344 | 'an\n' |
| 10345 | 'exception occurs in any of the clauses and is not handled, the\n' |
| 10346 | 'exception is temporarily saved. The "finally" clause is executed. ' |
| 10347 | 'If\n' |
| 10348 | 'there is a saved exception it is re-raised at the end of the ' |
| 10349 | '"finally"\n' |
| 10350 | 'clause. If the "finally" clause raises another exception, the ' |
| 10351 | 'saved\n' |
| 10352 | 'exception is set as the context of the new exception. If the ' |
| 10353 | '"finally"\n' |
| 10354 | 'clause executes a "return" or "break" statement, the saved ' |
| 10355 | 'exception\n' |
| 10356 | 'is discarded:\n' |
| 10357 | '\n' |
| 10358 | ' >>> def f():\n' |
| 10359 | ' ... try:\n' |
| 10360 | ' ... 1/0\n' |
| 10361 | ' ... finally:\n' |
| 10362 | ' ... return 42\n' |
| 10363 | ' ...\n' |
| 10364 | ' >>> f()\n' |
| 10365 | ' 42\n' |
| 10366 | '\n' |
| 10367 | 'The exception information is not available to the program during\n' |
| 10368 | 'execution of the "finally" clause.\n' |
| 10369 | '\n' |
| 10370 | 'When a "return", "break" or "continue" statement is executed in ' |
| 10371 | 'the\n' |
| 10372 | '"try" suite of a "try"..."finally" statement, the "finally" clause ' |
| 10373 | 'is\n' |
| 10374 | 'also executed \'on the way out.\' A "continue" statement is illegal ' |
| 10375 | 'in\n' |
| 10376 | 'the "finally" clause. (The reason is a problem with the current\n' |
| 10377 | 'implementation --- this restriction may be lifted in the future).\n' |
| 10378 | '\n' |
| 10379 | 'The return value of a function is determined by the last "return"\n' |
| 10380 | 'statement executed. Since the "finally" clause always executes, a\n' |
| 10381 | '"return" statement executed in the "finally" clause will always be ' |
| 10382 | 'the\n' |
| 10383 | 'last one executed:\n' |
| 10384 | '\n' |
| 10385 | ' >>> def foo():\n' |
| 10386 | ' ... try:\n' |
| 10387 | " ... return 'try'\n" |
| 10388 | ' ... finally:\n' |
| 10389 | " ... return 'finally'\n" |
| 10390 | ' ...\n' |
| 10391 | ' >>> foo()\n' |
| 10392 | " 'finally'\n" |
| 10393 | '\n' |
| 10394 | 'Additional information on exceptions can be found in section\n' |
| 10395 | '*Exceptions*, and information on using the "raise" statement to\n' |
| 10396 | 'generate exceptions may be found in section *The raise statement*.\n', |
| 10397 | 'types': '\n' |
| 10398 | 'The standard type hierarchy\n' |
| 10399 | '***************************\n' |
| 10400 | '\n' |
| 10401 | 'Below is a list of the types that are built into Python. ' |
| 10402 | 'Extension\n' |
| 10403 | 'modules (written in C, Java, or other languages, depending on ' |
| 10404 | 'the\n' |
| 10405 | 'implementation) can define additional types. Future versions of\n' |
| 10406 | 'Python may add types to the type hierarchy (e.g., rational ' |
| 10407 | 'numbers,\n' |
| 10408 | 'efficiently stored arrays of integers, etc.), although such ' |
| 10409 | 'additions\n' |
| 10410 | 'will often be provided via the standard library instead.\n' |
| 10411 | '\n' |
| 10412 | 'Some of the type descriptions below contain a paragraph listing\n' |
| 10413 | "'special attributes.' These are attributes that provide access " |
| 10414 | 'to the\n' |
| 10415 | 'implementation and are not intended for general use. Their ' |
| 10416 | 'definition\n' |
| 10417 | 'may change in the future.\n' |
| 10418 | '\n' |
| 10419 | 'None\n' |
| 10420 | ' This type has a single value. There is a single object with ' |
| 10421 | 'this\n' |
| 10422 | ' value. This object is accessed through the built-in name ' |
| 10423 | '"None". It\n' |
| 10424 | ' is used to signify the absence of a value in many situations, ' |
| 10425 | 'e.g.,\n' |
| 10426 | " it is returned from functions that don't explicitly return\n" |
| 10427 | ' anything. Its truth value is false.\n' |
| 10428 | '\n' |
| 10429 | 'NotImplemented\n' |
| 10430 | ' This type has a single value. There is a single object with ' |
| 10431 | 'this\n' |
| 10432 | ' value. This object is accessed through the built-in name\n' |
| 10433 | ' "NotImplemented". Numeric methods and rich comparison methods\n' |
| 10434 | ' should return this value if they do not implement the ' |
| 10435 | 'operation for\n' |
| 10436 | ' the operands provided. (The interpreter will then try the\n' |
| 10437 | ' reflected operation, or some other fallback, depending on the\n' |
| 10438 | ' operator.) Its truth value is true.\n' |
| 10439 | '\n' |
| 10440 | ' See *Implementing the arithmetic operations* for more ' |
| 10441 | 'details.\n' |
| 10442 | '\n' |
| 10443 | 'Ellipsis\n' |
| 10444 | ' This type has a single value. There is a single object with ' |
| 10445 | 'this\n' |
| 10446 | ' value. This object is accessed through the literal "..." or ' |
| 10447 | 'the\n' |
| 10448 | ' built-in name "Ellipsis". Its truth value is true.\n' |
| 10449 | '\n' |
| 10450 | '"numbers.Number"\n' |
| 10451 | ' These are created by numeric literals and returned as results ' |
| 10452 | 'by\n' |
| 10453 | ' arithmetic operators and arithmetic built-in functions. ' |
| 10454 | 'Numeric\n' |
| 10455 | ' objects are immutable; once created their value never ' |
| 10456 | 'changes.\n' |
| 10457 | ' Python numbers are of course strongly related to mathematical\n' |
| 10458 | ' numbers, but subject to the limitations of numerical ' |
| 10459 | 'representation\n' |
| 10460 | ' in computers.\n' |
| 10461 | '\n' |
| 10462 | ' Python distinguishes between integers, floating point numbers, ' |
| 10463 | 'and\n' |
| 10464 | ' complex numbers:\n' |
| 10465 | '\n' |
| 10466 | ' "numbers.Integral"\n' |
| 10467 | ' These represent elements from the mathematical set of ' |
| 10468 | 'integers\n' |
| 10469 | ' (positive and negative).\n' |
| 10470 | '\n' |
| 10471 | ' There are two types of integers:\n' |
| 10472 | '\n' |
| 10473 | ' Integers ("int")\n' |
| 10474 | '\n' |
| 10475 | ' These represent numbers in an unlimited range, subject ' |
| 10476 | 'to\n' |
| 10477 | ' available (virtual) memory only. For the purpose of ' |
| 10478 | 'shift\n' |
| 10479 | ' and mask operations, a binary representation is assumed, ' |
| 10480 | 'and\n' |
| 10481 | " negative numbers are represented in a variant of 2's\n" |
| 10482 | ' complement which gives the illusion of an infinite ' |
| 10483 | 'string of\n' |
| 10484 | ' sign bits extending to the left.\n' |
| 10485 | '\n' |
| 10486 | ' Booleans ("bool")\n' |
| 10487 | ' These represent the truth values False and True. The ' |
| 10488 | 'two\n' |
| 10489 | ' objects representing the values "False" and "True" are ' |
| 10490 | 'the\n' |
| 10491 | ' only Boolean objects. The Boolean type is a subtype of ' |
| 10492 | 'the\n' |
| 10493 | ' integer type, and Boolean values behave like the values ' |
| 10494 | '0 and\n' |
| 10495 | ' 1, respectively, in almost all contexts, the exception ' |
| 10496 | 'being\n' |
| 10497 | ' that when converted to a string, the strings ""False"" ' |
| 10498 | 'or\n' |
| 10499 | ' ""True"" are returned, respectively.\n' |
| 10500 | '\n' |
| 10501 | ' The rules for integer representation are intended to give ' |
| 10502 | 'the\n' |
| 10503 | ' most meaningful interpretation of shift and mask ' |
| 10504 | 'operations\n' |
| 10505 | ' involving negative integers.\n' |
| 10506 | '\n' |
| 10507 | ' "numbers.Real" ("float")\n' |
| 10508 | ' These represent machine-level double precision floating ' |
| 10509 | 'point\n' |
| 10510 | ' numbers. You are at the mercy of the underlying machine\n' |
| 10511 | ' architecture (and C or Java implementation) for the ' |
| 10512 | 'accepted\n' |
| 10513 | ' range and handling of overflow. Python does not support ' |
| 10514 | 'single-\n' |
| 10515 | ' precision floating point numbers; the savings in processor ' |
| 10516 | 'and\n' |
| 10517 | ' memory usage that are usually the reason for using these ' |
| 10518 | 'are\n' |
| 10519 | ' dwarfed by the overhead of using objects in Python, so ' |
| 10520 | 'there is\n' |
| 10521 | ' no reason to complicate the language with two kinds of ' |
| 10522 | 'floating\n' |
| 10523 | ' point numbers.\n' |
| 10524 | '\n' |
| 10525 | ' "numbers.Complex" ("complex")\n' |
| 10526 | ' These represent complex numbers as a pair of machine-level\n' |
| 10527 | ' double precision floating point numbers. The same caveats ' |
| 10528 | 'apply\n' |
| 10529 | ' as for floating point numbers. The real and imaginary parts ' |
| 10530 | 'of a\n' |
| 10531 | ' complex number "z" can be retrieved through the read-only\n' |
| 10532 | ' attributes "z.real" and "z.imag".\n' |
| 10533 | '\n' |
| 10534 | 'Sequences\n' |
| 10535 | ' These represent finite ordered sets indexed by non-negative\n' |
| 10536 | ' numbers. The built-in function "len()" returns the number of ' |
| 10537 | 'items\n' |
| 10538 | ' of a sequence. When the length of a sequence is *n*, the index ' |
| 10539 | 'set\n' |
| 10540 | ' contains the numbers 0, 1, ..., *n*-1. Item *i* of sequence ' |
| 10541 | '*a* is\n' |
| 10542 | ' selected by "a[i]".\n' |
| 10543 | '\n' |
| 10544 | ' Sequences also support slicing: "a[i:j]" selects all items ' |
| 10545 | 'with\n' |
| 10546 | ' index *k* such that *i* "<=" *k* "<" *j*. When used as an\n' |
| 10547 | ' expression, a slice is a sequence of the same type. This ' |
| 10548 | 'implies\n' |
| 10549 | ' that the index set is renumbered so that it starts at 0.\n' |
| 10550 | '\n' |
| 10551 | ' Some sequences also support "extended slicing" with a third ' |
| 10552 | '"step"\n' |
| 10553 | ' parameter: "a[i:j:k]" selects all items of *a* with index *x* ' |
| 10554 | 'where\n' |
| 10555 | ' "x = i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*.\n' |
| 10556 | '\n' |
| 10557 | ' Sequences are distinguished according to their mutability:\n' |
| 10558 | '\n' |
| 10559 | ' Immutable sequences\n' |
| 10560 | ' An object of an immutable sequence type cannot change once ' |
| 10561 | 'it is\n' |
| 10562 | ' created. (If the object contains references to other ' |
| 10563 | 'objects,\n' |
| 10564 | ' these other objects may be mutable and may be changed; ' |
| 10565 | 'however,\n' |
| 10566 | ' the collection of objects directly referenced by an ' |
| 10567 | 'immutable\n' |
| 10568 | ' object cannot change.)\n' |
| 10569 | '\n' |
| 10570 | ' The following types are immutable sequences:\n' |
| 10571 | '\n' |
| 10572 | ' Strings\n' |
| 10573 | ' A string is a sequence of values that represent Unicode ' |
| 10574 | 'code\n' |
| 10575 | ' points. All the code points in the range "U+0000 - ' |
| 10576 | 'U+10FFFF"\n' |
| 10577 | " can be represented in a string. Python doesn't have a " |
| 10578 | '"char"\n' |
| 10579 | ' type; instead, every code point in the string is ' |
| 10580 | 'represented\n' |
| 10581 | ' as a string object with length "1". The built-in ' |
| 10582 | 'function\n' |
| 10583 | ' "ord()" converts a code point from its string form to ' |
| 10584 | 'an\n' |
| 10585 | ' integer in the range "0 - 10FFFF"; "chr()" converts an\n' |
| 10586 | ' integer in the range "0 - 10FFFF" to the corresponding ' |
| 10587 | 'length\n' |
| 10588 | ' "1" string object. "str.encode()" can be used to convert ' |
| 10589 | 'a\n' |
| 10590 | ' "str" to "bytes" using the given text encoding, and\n' |
| 10591 | ' "bytes.decode()" can be used to achieve the opposite.\n' |
| 10592 | '\n' |
| 10593 | ' Tuples\n' |
| 10594 | ' The items of a tuple are arbitrary Python objects. ' |
| 10595 | 'Tuples of\n' |
| 10596 | ' two or more items are formed by comma-separated lists ' |
| 10597 | 'of\n' |
| 10598 | " expressions. A tuple of one item (a 'singleton') can " |
| 10599 | 'be\n' |
| 10600 | ' formed by affixing a comma to an expression (an ' |
| 10601 | 'expression by\n' |
| 10602 | ' itself does not create a tuple, since parentheses must ' |
| 10603 | 'be\n' |
| 10604 | ' usable for grouping of expressions). An empty tuple can ' |
| 10605 | 'be\n' |
| 10606 | ' formed by an empty pair of parentheses.\n' |
| 10607 | '\n' |
| 10608 | ' Bytes\n' |
| 10609 | ' A bytes object is an immutable array. The items are ' |
| 10610 | '8-bit\n' |
| 10611 | ' bytes, represented by integers in the range 0 <= x < ' |
| 10612 | '256.\n' |
| 10613 | ' Bytes literals (like "b\'abc\'") and the built-in ' |
| 10614 | 'function\n' |
| 10615 | ' "bytes()" can be used to construct bytes objects. ' |
| 10616 | 'Also,\n' |
| 10617 | ' bytes objects can be decoded to strings via the ' |
| 10618 | '"decode()"\n' |
| 10619 | ' method.\n' |
| 10620 | '\n' |
| 10621 | ' Mutable sequences\n' |
| 10622 | ' Mutable sequences can be changed after they are created. ' |
| 10623 | 'The\n' |
| 10624 | ' subscription and slicing notations can be used as the ' |
| 10625 | 'target of\n' |
| 10626 | ' assignment and "del" (delete) statements.\n' |
| 10627 | '\n' |
| 10628 | ' There are currently two intrinsic mutable sequence types:\n' |
| 10629 | '\n' |
| 10630 | ' Lists\n' |
| 10631 | ' The items of a list are arbitrary Python objects. Lists ' |
| 10632 | 'are\n' |
| 10633 | ' formed by placing a comma-separated list of expressions ' |
| 10634 | 'in\n' |
| 10635 | ' square brackets. (Note that there are no special cases ' |
| 10636 | 'needed\n' |
| 10637 | ' to form lists of length 0 or 1.)\n' |
| 10638 | '\n' |
| 10639 | ' Byte Arrays\n' |
| 10640 | ' A bytearray object is a mutable array. They are created ' |
| 10641 | 'by\n' |
| 10642 | ' the built-in "bytearray()" constructor. Aside from ' |
| 10643 | 'being\n' |
| 10644 | ' mutable (and hence unhashable), byte arrays otherwise ' |
| 10645 | 'provide\n' |
| 10646 | ' the same interface and functionality as immutable bytes\n' |
| 10647 | ' objects.\n' |
| 10648 | '\n' |
| 10649 | ' The extension module "array" provides an additional example ' |
| 10650 | 'of a\n' |
| 10651 | ' mutable sequence type, as does the "collections" module.\n' |
| 10652 | '\n' |
| 10653 | 'Set types\n' |
| 10654 | ' These represent unordered, finite sets of unique, immutable\n' |
| 10655 | ' objects. As such, they cannot be indexed by any subscript. ' |
| 10656 | 'However,\n' |
| 10657 | ' they can be iterated over, and the built-in function "len()"\n' |
| 10658 | ' returns the number of items in a set. Common uses for sets are ' |
| 10659 | 'fast\n' |
| 10660 | ' membership testing, removing duplicates from a sequence, and\n' |
| 10661 | ' computing mathematical operations such as intersection, ' |
| 10662 | 'union,\n' |
| 10663 | ' difference, and symmetric difference.\n' |
| 10664 | '\n' |
| 10665 | ' For set elements, the same immutability rules apply as for\n' |
| 10666 | ' dictionary keys. Note that numeric types obey the normal rules ' |
| 10667 | 'for\n' |
| 10668 | ' numeric comparison: if two numbers compare equal (e.g., "1" ' |
| 10669 | 'and\n' |
| 10670 | ' "1.0"), only one of them can be contained in a set.\n' |
| 10671 | '\n' |
| 10672 | ' There are currently two intrinsic set types:\n' |
| 10673 | '\n' |
| 10674 | ' Sets\n' |
| 10675 | ' These represent a mutable set. They are created by the ' |
| 10676 | 'built-in\n' |
| 10677 | ' "set()" constructor and can be modified afterwards by ' |
| 10678 | 'several\n' |
| 10679 | ' methods, such as "add()".\n' |
| 10680 | '\n' |
| 10681 | ' Frozen sets\n' |
| 10682 | ' These represent an immutable set. They are created by the\n' |
| 10683 | ' built-in "frozenset()" constructor. As a frozenset is ' |
| 10684 | 'immutable\n' |
| 10685 | ' and *hashable*, it can be used again as an element of ' |
| 10686 | 'another\n' |
| 10687 | ' set, or as a dictionary key.\n' |
| 10688 | '\n' |
| 10689 | 'Mappings\n' |
| 10690 | ' These represent finite sets of objects indexed by arbitrary ' |
| 10691 | 'index\n' |
| 10692 | ' sets. The subscript notation "a[k]" selects the item indexed ' |
| 10693 | 'by "k"\n' |
| 10694 | ' from the mapping "a"; this can be used in expressions and as ' |
| 10695 | 'the\n' |
| 10696 | ' target of assignments or "del" statements. The built-in ' |
| 10697 | 'function\n' |
| 10698 | ' "len()" returns the number of items in a mapping.\n' |
| 10699 | '\n' |
| 10700 | ' There is currently a single intrinsic mapping type:\n' |
| 10701 | '\n' |
| 10702 | ' Dictionaries\n' |
| 10703 | ' These represent finite sets of objects indexed by nearly\n' |
| 10704 | ' arbitrary values. The only types of values not acceptable ' |
| 10705 | 'as\n' |
| 10706 | ' keys are values containing lists or dictionaries or other\n' |
| 10707 | ' mutable types that are compared by value rather than by ' |
| 10708 | 'object\n' |
| 10709 | ' identity, the reason being that the efficient ' |
| 10710 | 'implementation of\n' |
| 10711 | " dictionaries requires a key's hash value to remain " |
| 10712 | 'constant.\n' |
| 10713 | ' Numeric types used for keys obey the normal rules for ' |
| 10714 | 'numeric\n' |
| 10715 | ' comparison: if two numbers compare equal (e.g., "1" and ' |
| 10716 | '"1.0")\n' |
| 10717 | ' then they can be used interchangeably to index the same\n' |
| 10718 | ' dictionary entry.\n' |
| 10719 | '\n' |
| 10720 | ' Dictionaries are mutable; they can be created by the ' |
| 10721 | '"{...}"\n' |
| 10722 | ' notation (see section *Dictionary displays*).\n' |
| 10723 | '\n' |
| 10724 | ' The extension modules "dbm.ndbm" and "dbm.gnu" provide\n' |
| 10725 | ' additional examples of mapping types, as does the ' |
| 10726 | '"collections"\n' |
| 10727 | ' module.\n' |
| 10728 | '\n' |
| 10729 | 'Callable types\n' |
| 10730 | ' These are the types to which the function call operation (see\n' |
| 10731 | ' section *Calls*) can be applied:\n' |
| 10732 | '\n' |
| 10733 | ' User-defined functions\n' |
| 10734 | ' A user-defined function object is created by a function\n' |
| 10735 | ' definition (see section *Function definitions*). It should ' |
| 10736 | 'be\n' |
| 10737 | ' called with an argument list containing the same number of ' |
| 10738 | 'items\n' |
| 10739 | " as the function's formal parameter list.\n" |
| 10740 | '\n' |
| 10741 | ' Special attributes:\n' |
| 10742 | '\n' |
| 10743 | ' ' |
| 10744 | '+---------------------------+---------------------------------+-------------+\n' |
| 10745 | ' | Attribute | ' |
| 10746 | 'Meaning | |\n' |
| 10747 | ' ' |
| 10748 | '+===========================+=================================+=============+\n' |
| 10749 | ' | "__doc__" | The function\'s ' |
| 10750 | 'documentation | Writable |\n' |
| 10751 | ' | | string, or "None" ' |
| 10752 | 'if | |\n' |
| 10753 | ' | | unavailable; not inherited ' |
| 10754 | 'by | |\n' |
| 10755 | ' | | ' |
| 10756 | 'subclasses | |\n' |
| 10757 | ' ' |
| 10758 | '+---------------------------+---------------------------------+-------------+\n' |
| 10759 | ' | "__name__" | The function\'s ' |
| 10760 | 'name | Writable |\n' |
| 10761 | ' ' |
| 10762 | '+---------------------------+---------------------------------+-------------+\n' |
| 10763 | ' | "__qualname__" | The function\'s *qualified ' |
| 10764 | 'name* | Writable |\n' |
| 10765 | ' | | New in version ' |
| 10766 | '3.3. | |\n' |
| 10767 | ' ' |
| 10768 | '+---------------------------+---------------------------------+-------------+\n' |
| 10769 | ' | "__module__" | The name of the module ' |
| 10770 | 'the | Writable |\n' |
| 10771 | ' | | function was defined in, ' |
| 10772 | 'or | |\n' |
| 10773 | ' | | "None" if ' |
| 10774 | 'unavailable. | |\n' |
| 10775 | ' ' |
| 10776 | '+---------------------------+---------------------------------+-------------+\n' |
| 10777 | ' | "__defaults__" | A tuple containing ' |
| 10778 | 'default | Writable |\n' |
| 10779 | ' | | argument values for ' |
| 10780 | 'those | |\n' |
| 10781 | ' | | arguments that have ' |
| 10782 | 'defaults, | |\n' |
| 10783 | ' | | or "None" if no arguments ' |
| 10784 | 'have | |\n' |
| 10785 | ' | | a default ' |
| 10786 | 'value | |\n' |
| 10787 | ' ' |
| 10788 | '+---------------------------+---------------------------------+-------------+\n' |
| 10789 | ' | "__code__" | The code object ' |
| 10790 | 'representing | Writable |\n' |
| 10791 | ' | | the compiled function ' |
| 10792 | 'body. | |\n' |
| 10793 | ' ' |
| 10794 | '+---------------------------+---------------------------------+-------------+\n' |
| 10795 | ' | "__globals__" | A reference to the ' |
| 10796 | 'dictionary | Read-only |\n' |
| 10797 | ' | | that holds the ' |
| 10798 | "function's | |\n" |
| 10799 | ' | | global variables --- the ' |
| 10800 | 'global | |\n' |
| 10801 | ' | | namespace of the module ' |
| 10802 | 'in | |\n' |
| 10803 | ' | | which the function was ' |
| 10804 | 'defined. | |\n' |
| 10805 | ' ' |
| 10806 | '+---------------------------+---------------------------------+-------------+\n' |
| 10807 | ' | "__dict__" | The namespace ' |
| 10808 | 'supporting | Writable |\n' |
| 10809 | ' | | arbitrary function ' |
| 10810 | 'attributes. | |\n' |
| 10811 | ' ' |
| 10812 | '+---------------------------+---------------------------------+-------------+\n' |
| 10813 | ' | "__closure__" | "None" or a tuple of cells ' |
| 10814 | 'that | Read-only |\n' |
| 10815 | ' | | contain bindings for ' |
| 10816 | 'the | |\n' |
| 10817 | " | | function's free " |
| 10818 | 'variables. | |\n' |
| 10819 | ' ' |
| 10820 | '+---------------------------+---------------------------------+-------------+\n' |
| 10821 | ' | "__annotations__" | A dict containing ' |
| 10822 | 'annotations | Writable |\n' |
| 10823 | ' | | of parameters. The keys of ' |
| 10824 | 'the | |\n' |
| 10825 | ' | | dict are the parameter ' |
| 10826 | 'names, | |\n' |
| 10827 | ' | | and "\'return\'" for the ' |
| 10828 | 'return | |\n' |
| 10829 | ' | | annotation, if ' |
| 10830 | 'provided. | |\n' |
| 10831 | ' ' |
| 10832 | '+---------------------------+---------------------------------+-------------+\n' |
| 10833 | ' | "__kwdefaults__" | A dict containing defaults ' |
| 10834 | 'for | Writable |\n' |
| 10835 | ' | | keyword-only ' |
| 10836 | 'parameters. | |\n' |
| 10837 | ' ' |
| 10838 | '+---------------------------+---------------------------------+-------------+\n' |
| 10839 | '\n' |
| 10840 | ' Most of the attributes labelled "Writable" check the type ' |
| 10841 | 'of the\n' |
| 10842 | ' assigned value.\n' |
| 10843 | '\n' |
| 10844 | ' Function objects also support getting and setting ' |
| 10845 | 'arbitrary\n' |
| 10846 | ' attributes, which can be used, for example, to attach ' |
| 10847 | 'metadata\n' |
| 10848 | ' to functions. Regular attribute dot-notation is used to ' |
| 10849 | 'get and\n' |
| 10850 | ' set such attributes. *Note that the current implementation ' |
| 10851 | 'only\n' |
| 10852 | ' supports function attributes on user-defined functions. ' |
| 10853 | 'Function\n' |
| 10854 | ' attributes on built-in functions may be supported in the\n' |
| 10855 | ' future.*\n' |
| 10856 | '\n' |
| 10857 | " Additional information about a function's definition can " |
| 10858 | 'be\n' |
| 10859 | ' retrieved from its code object; see the description of ' |
| 10860 | 'internal\n' |
| 10861 | ' types below.\n' |
| 10862 | '\n' |
| 10863 | ' Instance methods\n' |
| 10864 | ' An instance method object combines a class, a class ' |
| 10865 | 'instance and\n' |
| 10866 | ' any callable object (normally a user-defined function).\n' |
| 10867 | '\n' |
| 10868 | ' Special read-only attributes: "__self__" is the class ' |
| 10869 | 'instance\n' |
| 10870 | ' object, "__func__" is the function object; "__doc__" is ' |
| 10871 | 'the\n' |
| 10872 | ' method\'s documentation (same as "__func__.__doc__"); ' |
| 10873 | '"__name__"\n' |
| 10874 | ' is the method name (same as "__func__.__name__"); ' |
| 10875 | '"__module__"\n' |
| 10876 | ' is the name of the module the method was defined in, or ' |
| 10877 | '"None"\n' |
| 10878 | ' if unavailable.\n' |
| 10879 | '\n' |
| 10880 | ' Methods also support accessing (but not setting) the ' |
| 10881 | 'arbitrary\n' |
| 10882 | ' function attributes on the underlying function object.\n' |
| 10883 | '\n' |
| 10884 | ' User-defined method objects may be created when getting an\n' |
| 10885 | ' attribute of a class (perhaps via an instance of that ' |
| 10886 | 'class), if\n' |
| 10887 | ' that attribute is a user-defined function object or a ' |
| 10888 | 'class\n' |
| 10889 | ' method object.\n' |
| 10890 | '\n' |
| 10891 | ' When an instance method object is created by retrieving a ' |
| 10892 | 'user-\n' |
| 10893 | ' defined function object from a class via one of its ' |
| 10894 | 'instances,\n' |
| 10895 | ' its "__self__" attribute is the instance, and the method ' |
| 10896 | 'object\n' |
| 10897 | ' is said to be bound. The new method\'s "__func__" ' |
| 10898 | 'attribute is\n' |
| 10899 | ' the original function object.\n' |
| 10900 | '\n' |
| 10901 | ' When a user-defined method object is created by retrieving\n' |
| 10902 | ' another method object from a class or instance, the ' |
| 10903 | 'behaviour is\n' |
| 10904 | ' the same as for a function object, except that the ' |
| 10905 | '"__func__"\n' |
| 10906 | ' attribute of the new instance is not the original method ' |
| 10907 | 'object\n' |
| 10908 | ' but its "__func__" attribute.\n' |
| 10909 | '\n' |
| 10910 | ' When an instance method object is created by retrieving a ' |
| 10911 | 'class\n' |
| 10912 | ' method object from a class or instance, its "__self__" ' |
| 10913 | 'attribute\n' |
| 10914 | ' is the class itself, and its "__func__" attribute is the\n' |
| 10915 | ' function object underlying the class method.\n' |
| 10916 | '\n' |
| 10917 | ' When an instance method object is called, the underlying\n' |
| 10918 | ' function ("__func__") is called, inserting the class ' |
| 10919 | 'instance\n' |
| 10920 | ' ("__self__") in front of the argument list. For instance, ' |
| 10921 | 'when\n' |
| 10922 | ' "C" is a class which contains a definition for a function ' |
| 10923 | '"f()",\n' |
| 10924 | ' and "x" is an instance of "C", calling "x.f(1)" is ' |
| 10925 | 'equivalent to\n' |
| 10926 | ' calling "C.f(x, 1)".\n' |
| 10927 | '\n' |
| 10928 | ' When an instance method object is derived from a class ' |
| 10929 | 'method\n' |
| 10930 | ' object, the "class instance" stored in "__self__" will ' |
| 10931 | 'actually\n' |
| 10932 | ' be the class itself, so that calling either "x.f(1)" or ' |
| 10933 | '"C.f(1)"\n' |
| 10934 | ' is equivalent to calling "f(C,1)" where "f" is the ' |
| 10935 | 'underlying\n' |
| 10936 | ' function.\n' |
| 10937 | '\n' |
| 10938 | ' Note that the transformation from function object to ' |
| 10939 | 'instance\n' |
| 10940 | ' method object happens each time the attribute is retrieved ' |
| 10941 | 'from\n' |
| 10942 | ' the instance. In some cases, a fruitful optimization is ' |
| 10943 | 'to\n' |
| 10944 | ' assign the attribute to a local variable and call that ' |
| 10945 | 'local\n' |
| 10946 | ' variable. Also notice that this transformation only happens ' |
| 10947 | 'for\n' |
| 10948 | ' user-defined functions; other callable objects (and all ' |
| 10949 | 'non-\n' |
| 10950 | ' callable objects) are retrieved without transformation. It ' |
| 10951 | 'is\n' |
| 10952 | ' also important to note that user-defined functions which ' |
| 10953 | 'are\n' |
| 10954 | ' attributes of a class instance are not converted to bound\n' |
| 10955 | ' methods; this *only* happens when the function is an ' |
| 10956 | 'attribute\n' |
| 10957 | ' of the class.\n' |
| 10958 | '\n' |
| 10959 | ' Generator functions\n' |
| 10960 | ' A function or method which uses the "yield" statement (see\n' |
| 10961 | ' section *The yield statement*) is called a *generator ' |
| 10962 | 'function*.\n' |
| 10963 | ' Such a function, when called, always returns an iterator ' |
| 10964 | 'object\n' |
| 10965 | ' which can be used to execute the body of the function: ' |
| 10966 | 'calling\n' |
| 10967 | ' the iterator\'s "iterator.__next__()" method will cause ' |
| 10968 | 'the\n' |
| 10969 | ' function to execute until it provides a value using the ' |
| 10970 | '"yield"\n' |
| 10971 | ' statement. When the function executes a "return" statement ' |
| 10972 | 'or\n' |
| 10973 | ' falls off the end, a "StopIteration" exception is raised ' |
| 10974 | 'and the\n' |
| 10975 | ' iterator will have reached the end of the set of values to ' |
| 10976 | 'be\n' |
| 10977 | ' returned.\n' |
| 10978 | '\n' |
| 10979 | ' Coroutine functions\n' |
| 10980 | ' A function or method which is defined using "async def" is\n' |
| 10981 | ' called a *coroutine function*. Such a function, when ' |
| 10982 | 'called,\n' |
| 10983 | ' returns a *coroutine* object. It may contain "await"\n' |
| 10984 | ' expressions, as well as "async with" and "async for" ' |
| 10985 | 'statements.\n' |
| 10986 | ' See also the *Coroutine Objects* section.\n' |
| 10987 | '\n' |
| 10988 | ' Built-in functions\n' |
| 10989 | ' A built-in function object is a wrapper around a C ' |
| 10990 | 'function.\n' |
| 10991 | ' Examples of built-in functions are "len()" and ' |
| 10992 | '"math.sin()"\n' |
| 10993 | ' ("math" is a standard built-in module). The number and type ' |
| 10994 | 'of\n' |
| 10995 | ' the arguments are determined by the C function. Special ' |
| 10996 | 'read-\n' |
| 10997 | ' only attributes: "__doc__" is the function\'s ' |
| 10998 | 'documentation\n' |
| 10999 | ' string, or "None" if unavailable; "__name__" is the ' |
| 11000 | "function's\n" |
| 11001 | ' name; "__self__" is set to "None" (but see the next item);\n' |
| 11002 | ' "__module__" is the name of the module the function was ' |
| 11003 | 'defined\n' |
| 11004 | ' in or "None" if unavailable.\n' |
| 11005 | '\n' |
| 11006 | ' Built-in methods\n' |
| 11007 | ' This is really a different disguise of a built-in function, ' |
| 11008 | 'this\n' |
| 11009 | ' time containing an object passed to the C function as an\n' |
| 11010 | ' implicit extra argument. An example of a built-in method ' |
| 11011 | 'is\n' |
| 11012 | ' "alist.append()", assuming *alist* is a list object. In ' |
| 11013 | 'this\n' |
| 11014 | ' case, the special read-only attribute "__self__" is set to ' |
| 11015 | 'the\n' |
| 11016 | ' object denoted by *alist*.\n' |
| 11017 | '\n' |
| 11018 | ' Classes\n' |
| 11019 | ' Classes are callable. These objects normally act as ' |
| 11020 | 'factories\n' |
| 11021 | ' for new instances of themselves, but variations are ' |
| 11022 | 'possible for\n' |
| 11023 | ' class types that override "__new__()". The arguments of ' |
| 11024 | 'the\n' |
| 11025 | ' call are passed to "__new__()" and, in the typical case, ' |
| 11026 | 'to\n' |
| 11027 | ' "__init__()" to initialize the new instance.\n' |
| 11028 | '\n' |
| 11029 | ' Class Instances\n' |
| 11030 | ' Instances of arbitrary classes can be made callable by ' |
| 11031 | 'defining\n' |
| 11032 | ' a "__call__()" method in their class.\n' |
| 11033 | '\n' |
| 11034 | 'Modules\n' |
| 11035 | ' Modules are a basic organizational unit of Python code, and ' |
| 11036 | 'are\n' |
| 11037 | ' created by the *import system* as invoked either by the ' |
| 11038 | '"import"\n' |
| 11039 | ' statement (see "import"), or by calling functions such as\n' |
| 11040 | ' "importlib.import_module()" and built-in "__import__()". A ' |
| 11041 | 'module\n' |
| 11042 | ' object has a namespace implemented by a dictionary object ' |
| 11043 | '(this is\n' |
| 11044 | ' the dictionary referenced by the "__globals__" attribute of\n' |
| 11045 | ' functions defined in the module). Attribute references are\n' |
| 11046 | ' translated to lookups in this dictionary, e.g., "m.x" is ' |
| 11047 | 'equivalent\n' |
| 11048 | ' to "m.__dict__["x"]". A module object does not contain the ' |
| 11049 | 'code\n' |
| 11050 | " object used to initialize the module (since it isn't needed " |
| 11051 | 'once\n' |
| 11052 | ' the initialization is done).\n' |
| 11053 | '\n' |
| 11054 | " Attribute assignment updates the module's namespace " |
| 11055 | 'dictionary,\n' |
| 11056 | ' e.g., "m.x = 1" is equivalent to "m.__dict__["x"] = 1".\n' |
| 11057 | '\n' |
| 11058 | ' Special read-only attribute: "__dict__" is the module\'s ' |
| 11059 | 'namespace\n' |
| 11060 | ' as a dictionary object.\n' |
| 11061 | '\n' |
| 11062 | ' **CPython implementation detail:** Because of the way CPython\n' |
| 11063 | ' clears module dictionaries, the module dictionary will be ' |
| 11064 | 'cleared\n' |
| 11065 | ' when the module falls out of scope even if the dictionary ' |
| 11066 | 'still has\n' |
| 11067 | ' live references. To avoid this, copy the dictionary or keep ' |
| 11068 | 'the\n' |
| 11069 | ' module around while using its dictionary directly.\n' |
| 11070 | '\n' |
| 11071 | ' Predefined (writable) attributes: "__name__" is the module\'s ' |
| 11072 | 'name;\n' |
| 11073 | ' "__doc__" is the module\'s documentation string, or "None" if\n' |
| 11074 | ' unavailable; "__file__" is the pathname of the file from which ' |
| 11075 | 'the\n' |
| 11076 | ' module was loaded, if it was loaded from a file. The ' |
| 11077 | '"__file__"\n' |
| 11078 | ' attribute may be missing for certain types of modules, such as ' |
| 11079 | 'C\n' |
| 11080 | ' modules that are statically linked into the interpreter; for\n' |
| 11081 | ' extension modules loaded dynamically from a shared library, it ' |
| 11082 | 'is\n' |
| 11083 | ' the pathname of the shared library file.\n' |
| 11084 | '\n' |
| 11085 | 'Custom classes\n' |
| 11086 | ' Custom class types are typically created by class definitions ' |
| 11087 | '(see\n' |
| 11088 | ' section *Class definitions*). A class has a namespace ' |
| 11089 | 'implemented\n' |
| 11090 | ' by a dictionary object. Class attribute references are ' |
| 11091 | 'translated\n' |
| 11092 | ' to lookups in this dictionary, e.g., "C.x" is translated to\n' |
| 11093 | ' "C.__dict__["x"]" (although there are a number of hooks which ' |
| 11094 | 'allow\n' |
| 11095 | ' for other means of locating attributes). When the attribute ' |
| 11096 | 'name is\n' |
| 11097 | ' not found there, the attribute search continues in the base\n' |
| 11098 | ' classes. This search of the base classes uses the C3 method\n' |
| 11099 | ' resolution order which behaves correctly even in the presence ' |
| 11100 | 'of\n' |
| 11101 | " 'diamond' inheritance structures where there are multiple\n" |
| 11102 | ' inheritance paths leading back to a common ancestor. ' |
| 11103 | 'Additional\n' |
| 11104 | ' details on the C3 MRO used by Python can be found in the\n' |
| 11105 | ' documentation accompanying the 2.3 release at\n' |
| 11106 | ' https://www.python.org/download/releases/2.3/mro/.\n' |
| 11107 | '\n' |
| 11108 | ' When a class attribute reference (for class "C", say) would ' |
| 11109 | 'yield a\n' |
| 11110 | ' class method object, it is transformed into an instance ' |
| 11111 | 'method\n' |
| 11112 | ' object whose "__self__" attributes is "C". When it would ' |
| 11113 | 'yield a\n' |
| 11114 | ' static method object, it is transformed into the object ' |
| 11115 | 'wrapped by\n' |
| 11116 | ' the static method object. See section *Implementing ' |
| 11117 | 'Descriptors*\n' |
| 11118 | ' for another way in which attributes retrieved from a class ' |
| 11119 | 'may\n' |
| 11120 | ' differ from those actually contained in its "__dict__".\n' |
| 11121 | '\n' |
| 11122 | " Class attribute assignments update the class's dictionary, " |
| 11123 | 'never\n' |
| 11124 | ' the dictionary of a base class.\n' |
| 11125 | '\n' |
| 11126 | ' A class object can be called (see above) to yield a class ' |
| 11127 | 'instance\n' |
| 11128 | ' (see below).\n' |
| 11129 | '\n' |
| 11130 | ' Special attributes: "__name__" is the class name; "__module__" ' |
| 11131 | 'is\n' |
| 11132 | ' the module name in which the class was defined; "__dict__" is ' |
| 11133 | 'the\n' |
| 11134 | ' dictionary containing the class\'s namespace; "__bases__" is a ' |
| 11135 | 'tuple\n' |
| 11136 | ' (possibly empty or a singleton) containing the base classes, ' |
| 11137 | 'in the\n' |
| 11138 | ' order of their occurrence in the base class list; "__doc__" is ' |
| 11139 | 'the\n' |
| 11140 | " class's documentation string, or None if undefined.\n" |
| 11141 | '\n' |
| 11142 | 'Class instances\n' |
| 11143 | ' A class instance is created by calling a class object (see ' |
| 11144 | 'above).\n' |
| 11145 | ' A class instance has a namespace implemented as a dictionary ' |
| 11146 | 'which\n' |
| 11147 | ' is the first place in which attribute references are ' |
| 11148 | 'searched.\n' |
| 11149 | " When an attribute is not found there, and the instance's class " |
| 11150 | 'has\n' |
| 11151 | ' an attribute by that name, the search continues with the ' |
| 11152 | 'class\n' |
| 11153 | ' attributes. If a class attribute is found that is a ' |
| 11154 | 'user-defined\n' |
| 11155 | ' function object, it is transformed into an instance method ' |
| 11156 | 'object\n' |
| 11157 | ' whose "__self__" attribute is the instance. Static method ' |
| 11158 | 'and\n' |
| 11159 | ' class method objects are also transformed; see above under\n' |
| 11160 | ' "Classes". See section *Implementing Descriptors* for another ' |
| 11161 | 'way\n' |
| 11162 | ' in which attributes of a class retrieved via its instances ' |
| 11163 | 'may\n' |
| 11164 | " differ from the objects actually stored in the class's " |
| 11165 | '"__dict__".\n' |
| 11166 | " If no class attribute is found, and the object's class has a\n" |
| 11167 | ' "__getattr__()" method, that is called to satisfy the lookup.\n' |
| 11168 | '\n' |
| 11169 | " Attribute assignments and deletions update the instance's\n" |
| 11170 | " dictionary, never a class's dictionary. If the class has a\n" |
| 11171 | ' "__setattr__()" or "__delattr__()" method, this is called ' |
| 11172 | 'instead\n' |
| 11173 | ' of updating the instance dictionary directly.\n' |
| 11174 | '\n' |
| 11175 | ' Class instances can pretend to be numbers, sequences, or ' |
| 11176 | 'mappings\n' |
| 11177 | ' if they have methods with certain special names. See section\n' |
| 11178 | ' *Special method names*.\n' |
| 11179 | '\n' |
| 11180 | ' Special attributes: "__dict__" is the attribute dictionary;\n' |
| 11181 | ' "__class__" is the instance\'s class.\n' |
| 11182 | '\n' |
| 11183 | 'I/O objects (also known as file objects)\n' |
| 11184 | ' A *file object* represents an open file. Various shortcuts ' |
| 11185 | 'are\n' |
| 11186 | ' available to create file objects: the "open()" built-in ' |
| 11187 | 'function,\n' |
| 11188 | ' and also "os.popen()", "os.fdopen()", and the "makefile()" ' |
| 11189 | 'method\n' |
| 11190 | ' of socket objects (and perhaps by other functions or methods\n' |
| 11191 | ' provided by extension modules).\n' |
| 11192 | '\n' |
| 11193 | ' The objects "sys.stdin", "sys.stdout" and "sys.stderr" are\n' |
| 11194 | ' initialized to file objects corresponding to the ' |
| 11195 | "interpreter's\n" |
| 11196 | ' standard input, output and error streams; they are all open in ' |
| 11197 | 'text\n' |
| 11198 | ' mode and therefore follow the interface defined by the\n' |
| 11199 | ' "io.TextIOBase" abstract class.\n' |
| 11200 | '\n' |
| 11201 | 'Internal types\n' |
| 11202 | ' A few types used internally by the interpreter are exposed to ' |
| 11203 | 'the\n' |
| 11204 | ' user. Their definitions may change with future versions of ' |
| 11205 | 'the\n' |
| 11206 | ' interpreter, but they are mentioned here for completeness.\n' |
| 11207 | '\n' |
| 11208 | ' Code objects\n' |
| 11209 | ' Code objects represent *byte-compiled* executable Python ' |
| 11210 | 'code,\n' |
| 11211 | ' or *bytecode*. The difference between a code object and a\n' |
| 11212 | ' function object is that the function object contains an ' |
| 11213 | 'explicit\n' |
| 11214 | " reference to the function's globals (the module in which it " |
| 11215 | 'was\n' |
| 11216 | ' defined), while a code object contains no context; also ' |
| 11217 | 'the\n' |
| 11218 | ' default argument values are stored in the function object, ' |
| 11219 | 'not\n' |
| 11220 | ' in the code object (because they represent values ' |
| 11221 | 'calculated at\n' |
| 11222 | ' run-time). Unlike function objects, code objects are ' |
| 11223 | 'immutable\n' |
| 11224 | ' and contain no references (directly or indirectly) to ' |
| 11225 | 'mutable\n' |
| 11226 | ' objects.\n' |
| 11227 | '\n' |
| 11228 | ' Special read-only attributes: "co_name" gives the function ' |
| 11229 | 'name;\n' |
| 11230 | ' "co_argcount" is the number of positional arguments ' |
| 11231 | '(including\n' |
| 11232 | ' arguments with default values); "co_nlocals" is the number ' |
| 11233 | 'of\n' |
| 11234 | ' local variables used by the function (including ' |
| 11235 | 'arguments);\n' |
| 11236 | ' "co_varnames" is a tuple containing the names of the local\n' |
| 11237 | ' variables (starting with the argument names); "co_cellvars" ' |
| 11238 | 'is a\n' |
| 11239 | ' tuple containing the names of local variables that are\n' |
| 11240 | ' referenced by nested functions; "co_freevars" is a tuple\n' |
| 11241 | ' containing the names of free variables; "co_code" is a ' |
| 11242 | 'string\n' |
| 11243 | ' representing the sequence of bytecode instructions; ' |
| 11244 | '"co_consts"\n' |
| 11245 | ' is a tuple containing the literals used by the bytecode;\n' |
| 11246 | ' "co_names" is a tuple containing the names used by the ' |
| 11247 | 'bytecode;\n' |
| 11248 | ' "co_filename" is the filename from which the code was ' |
| 11249 | 'compiled;\n' |
| 11250 | ' "co_firstlineno" is the first line number of the function;\n' |
| 11251 | ' "co_lnotab" is a string encoding the mapping from bytecode\n' |
| 11252 | ' offsets to line numbers (for details see the source code of ' |
| 11253 | 'the\n' |
| 11254 | ' interpreter); "co_stacksize" is the required stack size\n' |
| 11255 | ' (including local variables); "co_flags" is an integer ' |
| 11256 | 'encoding a\n' |
| 11257 | ' number of flags for the interpreter.\n' |
| 11258 | '\n' |
| 11259 | ' The following flag bits are defined for "co_flags": bit ' |
| 11260 | '"0x04"\n' |
| 11261 | ' is set if the function uses the "*arguments" syntax to ' |
| 11262 | 'accept an\n' |
| 11263 | ' arbitrary number of positional arguments; bit "0x08" is set ' |
| 11264 | 'if\n' |
| 11265 | ' the function uses the "**keywords" syntax to accept ' |
| 11266 | 'arbitrary\n' |
| 11267 | ' keyword arguments; bit "0x20" is set if the function is a\n' |
| 11268 | ' generator.\n' |
| 11269 | '\n' |
| 11270 | ' Future feature declarations ("from __future__ import ' |
| 11271 | 'division")\n' |
| 11272 | ' also use bits in "co_flags" to indicate whether a code ' |
| 11273 | 'object\n' |
| 11274 | ' was compiled with a particular feature enabled: bit ' |
| 11275 | '"0x2000" is\n' |
| 11276 | ' set if the function was compiled with future division ' |
| 11277 | 'enabled;\n' |
| 11278 | ' bits "0x10" and "0x1000" were used in earlier versions of\n' |
| 11279 | ' Python.\n' |
| 11280 | '\n' |
| 11281 | ' Other bits in "co_flags" are reserved for internal use.\n' |
| 11282 | '\n' |
| 11283 | ' If a code object represents a function, the first item in\n' |
| 11284 | ' "co_consts" is the documentation string of the function, ' |
| 11285 | 'or\n' |
| 11286 | ' "None" if undefined.\n' |
| 11287 | '\n' |
| 11288 | ' Frame objects\n' |
| 11289 | ' Frame objects represent execution frames. They may occur ' |
| 11290 | 'in\n' |
| 11291 | ' traceback objects (see below).\n' |
| 11292 | '\n' |
| 11293 | ' Special read-only attributes: "f_back" is to the previous ' |
| 11294 | 'stack\n' |
| 11295 | ' frame (towards the caller), or "None" if this is the ' |
| 11296 | 'bottom\n' |
| 11297 | ' stack frame; "f_code" is the code object being executed in ' |
| 11298 | 'this\n' |
| 11299 | ' frame; "f_locals" is the dictionary used to look up local\n' |
| 11300 | ' variables; "f_globals" is used for global variables;\n' |
| 11301 | ' "f_builtins" is used for built-in (intrinsic) names; ' |
| 11302 | '"f_lasti"\n' |
| 11303 | ' gives the precise instruction (this is an index into the\n' |
| 11304 | ' bytecode string of the code object).\n' |
| 11305 | '\n' |
| 11306 | ' Special writable attributes: "f_trace", if not "None", is ' |
| 11307 | 'a\n' |
| 11308 | ' function called at the start of each source code line (this ' |
| 11309 | 'is\n' |
| 11310 | ' used by the debugger); "f_lineno" is the current line ' |
| 11311 | 'number of\n' |
| 11312 | ' the frame --- writing to this from within a trace function ' |
| 11313 | 'jumps\n' |
| 11314 | ' to the given line (only for the bottom-most frame). A ' |
| 11315 | 'debugger\n' |
| 11316 | ' can implement a Jump command (aka Set Next Statement) by ' |
| 11317 | 'writing\n' |
| 11318 | ' to f_lineno.\n' |
| 11319 | '\n' |
| 11320 | ' Frame objects support one method:\n' |
| 11321 | '\n' |
| 11322 | ' frame.clear()\n' |
| 11323 | '\n' |
| 11324 | ' This method clears all references to local variables ' |
| 11325 | 'held by\n' |
| 11326 | ' the frame. Also, if the frame belonged to a generator, ' |
| 11327 | 'the\n' |
| 11328 | ' generator is finalized. This helps break reference ' |
| 11329 | 'cycles\n' |
| 11330 | ' involving frame objects (for example when catching an\n' |
| 11331 | ' exception and storing its traceback for later use).\n' |
| 11332 | '\n' |
| 11333 | ' "RuntimeError" is raised if the frame is currently ' |
| 11334 | 'executing.\n' |
| 11335 | '\n' |
| 11336 | ' New in version 3.4.\n' |
| 11337 | '\n' |
| 11338 | ' Traceback objects\n' |
| 11339 | ' Traceback objects represent a stack trace of an exception. ' |
| 11340 | 'A\n' |
| 11341 | ' traceback object is created when an exception occurs. When ' |
| 11342 | 'the\n' |
| 11343 | ' search for an exception handler unwinds the execution ' |
| 11344 | 'stack, at\n' |
| 11345 | ' each unwound level a traceback object is inserted in front ' |
| 11346 | 'of\n' |
| 11347 | ' the current traceback. When an exception handler is ' |
| 11348 | 'entered,\n' |
| 11349 | ' the stack trace is made available to the program. (See ' |
| 11350 | 'section\n' |
| 11351 | ' *The try statement*.) It is accessible as the third item of ' |
| 11352 | 'the\n' |
| 11353 | ' tuple returned by "sys.exc_info()". When the program ' |
| 11354 | 'contains no\n' |
| 11355 | ' suitable handler, the stack trace is written (nicely ' |
| 11356 | 'formatted)\n' |
| 11357 | ' to the standard error stream; if the interpreter is ' |
| 11358 | 'interactive,\n' |
| 11359 | ' it is also made available to the user as ' |
| 11360 | '"sys.last_traceback".\n' |
| 11361 | '\n' |
| 11362 | ' Special read-only attributes: "tb_next" is the next level ' |
| 11363 | 'in the\n' |
| 11364 | ' stack trace (towards the frame where the exception ' |
| 11365 | 'occurred), or\n' |
| 11366 | ' "None" if there is no next level; "tb_frame" points to the\n' |
| 11367 | ' execution frame of the current level; "tb_lineno" gives the ' |
| 11368 | 'line\n' |
| 11369 | ' number where the exception occurred; "tb_lasti" indicates ' |
| 11370 | 'the\n' |
| 11371 | ' precise instruction. The line number and last instruction ' |
| 11372 | 'in\n' |
| 11373 | ' the traceback may differ from the line number of its frame\n' |
| 11374 | ' object if the exception occurred in a "try" statement with ' |
| 11375 | 'no\n' |
| 11376 | ' matching except clause or with a finally clause.\n' |
| 11377 | '\n' |
| 11378 | ' Slice objects\n' |
| 11379 | ' Slice objects are used to represent slices for ' |
| 11380 | '"__getitem__()"\n' |
| 11381 | ' methods. They are also created by the built-in "slice()"\n' |
| 11382 | ' function.\n' |
| 11383 | '\n' |
| 11384 | ' Special read-only attributes: "start" is the lower bound; ' |
| 11385 | '"stop"\n' |
| 11386 | ' is the upper bound; "step" is the step value; each is ' |
| 11387 | '"None" if\n' |
| 11388 | ' omitted. These attributes can have any type.\n' |
| 11389 | '\n' |
| 11390 | ' Slice objects support one method:\n' |
| 11391 | '\n' |
| 11392 | ' slice.indices(self, length)\n' |
| 11393 | '\n' |
| 11394 | ' This method takes a single integer argument *length* ' |
| 11395 | 'and\n' |
| 11396 | ' computes information about the slice that the slice ' |
| 11397 | 'object\n' |
| 11398 | ' would describe if applied to a sequence of *length* ' |
| 11399 | 'items.\n' |
| 11400 | ' It returns a tuple of three integers; respectively these ' |
| 11401 | 'are\n' |
| 11402 | ' the *start* and *stop* indices and the *step* or stride\n' |
| 11403 | ' length of the slice. Missing or out-of-bounds indices ' |
| 11404 | 'are\n' |
| 11405 | ' handled in a manner consistent with regular slices.\n' |
| 11406 | '\n' |
| 11407 | ' Static method objects\n' |
| 11408 | ' Static method objects provide a way of defeating the\n' |
| 11409 | ' transformation of function objects to method objects ' |
| 11410 | 'described\n' |
| 11411 | ' above. A static method object is a wrapper around any ' |
| 11412 | 'other\n' |
| 11413 | ' object, usually a user-defined method object. When a ' |
| 11414 | 'static\n' |
| 11415 | ' method object is retrieved from a class or a class ' |
| 11416 | 'instance, the\n' |
| 11417 | ' object actually returned is the wrapped object, which is ' |
| 11418 | 'not\n' |
| 11419 | ' subject to any further transformation. Static method ' |
| 11420 | 'objects are\n' |
| 11421 | ' not themselves callable, although the objects they wrap ' |
| 11422 | 'usually\n' |
| 11423 | ' are. Static method objects are created by the built-in\n' |
| 11424 | ' "staticmethod()" constructor.\n' |
| 11425 | '\n' |
| 11426 | ' Class method objects\n' |
| 11427 | ' A class method object, like a static method object, is a ' |
| 11428 | 'wrapper\n' |
| 11429 | ' around another object that alters the way in which that ' |
| 11430 | 'object\n' |
| 11431 | ' is retrieved from classes and class instances. The ' |
| 11432 | 'behaviour of\n' |
| 11433 | ' class method objects upon such retrieval is described ' |
| 11434 | 'above,\n' |
| 11435 | ' under "User-defined methods". Class method objects are ' |
| 11436 | 'created\n' |
| 11437 | ' by the built-in "classmethod()" constructor.\n', |
| 11438 | 'typesfunctions': '\n' |
| 11439 | 'Functions\n' |
| 11440 | '*********\n' |
| 11441 | '\n' |
| 11442 | 'Function objects are created by function definitions. ' |
| 11443 | 'The only\n' |
| 11444 | 'operation on a function object is to call it: ' |
| 11445 | '"func(argument-list)".\n' |
| 11446 | '\n' |
| 11447 | 'There are really two flavors of function objects: ' |
| 11448 | 'built-in functions\n' |
| 11449 | 'and user-defined functions. Both support the same ' |
| 11450 | 'operation (to call\n' |
| 11451 | 'the function), but the implementation is different, ' |
| 11452 | 'hence the\n' |
| 11453 | 'different object types.\n' |
| 11454 | '\n' |
| 11455 | 'See *Function definitions* for more information.\n', |
| 11456 | 'typesmapping': '\n' |
| 11457 | 'Mapping Types --- "dict"\n' |
| 11458 | '************************\n' |
| 11459 | '\n' |
| 11460 | 'A *mapping* object maps *hashable* values to arbitrary ' |
| 11461 | 'objects.\n' |
| 11462 | 'Mappings are mutable objects. There is currently only one ' |
| 11463 | 'standard\n' |
| 11464 | 'mapping type, the *dictionary*. (For other containers see ' |
| 11465 | 'the built-\n' |
| 11466 | 'in "list", "set", and "tuple" classes, and the ' |
| 11467 | '"collections" module.)\n' |
| 11468 | '\n' |
| 11469 | "A dictionary's keys are *almost* arbitrary values. Values " |
| 11470 | 'that are\n' |
| 11471 | 'not *hashable*, that is, values containing lists, ' |
| 11472 | 'dictionaries or\n' |
| 11473 | 'other mutable types (that are compared by value rather ' |
| 11474 | 'than by object\n' |
| 11475 | 'identity) may not be used as keys. Numeric types used for ' |
| 11476 | 'keys obey\n' |
| 11477 | 'the normal rules for numeric comparison: if two numbers ' |
| 11478 | 'compare equal\n' |
| 11479 | '(such as "1" and "1.0") then they can be used ' |
| 11480 | 'interchangeably to index\n' |
| 11481 | 'the same dictionary entry. (Note however, that since ' |
| 11482 | 'computers store\n' |
| 11483 | 'floating-point numbers as approximations it is usually ' |
| 11484 | 'unwise to use\n' |
| 11485 | 'them as dictionary keys.)\n' |
| 11486 | '\n' |
| 11487 | 'Dictionaries can be created by placing a comma-separated ' |
| 11488 | 'list of "key:\n' |
| 11489 | 'value" pairs within braces, for example: "{\'jack\': 4098, ' |
| 11490 | "'sjoerd':\n" |
| 11491 | '4127}" or "{4098: \'jack\', 4127: \'sjoerd\'}", or by the ' |
| 11492 | '"dict"\n' |
| 11493 | 'constructor.\n' |
| 11494 | '\n' |
| 11495 | 'class class dict(**kwarg)\n' |
| 11496 | 'class class dict(mapping, **kwarg)\n' |
| 11497 | 'class class dict(iterable, **kwarg)\n' |
| 11498 | '\n' |
| 11499 | ' Return a new dictionary initialized from an optional ' |
| 11500 | 'positional\n' |
| 11501 | ' argument and a possibly empty set of keyword ' |
| 11502 | 'arguments.\n' |
| 11503 | '\n' |
| 11504 | ' If no positional argument is given, an empty dictionary ' |
| 11505 | 'is created.\n' |
| 11506 | ' If a positional argument is given and it is a mapping ' |
| 11507 | 'object, a\n' |
| 11508 | ' dictionary is created with the same key-value pairs as ' |
| 11509 | 'the mapping\n' |
| 11510 | ' object. Otherwise, the positional argument must be an ' |
| 11511 | '*iterable*\n' |
| 11512 | ' object. Each item in the iterable must itself be an ' |
| 11513 | 'iterable with\n' |
| 11514 | ' exactly two objects. The first object of each item ' |
| 11515 | 'becomes a key\n' |
| 11516 | ' in the new dictionary, and the second object the ' |
| 11517 | 'corresponding\n' |
| 11518 | ' value. If a key occurs more than once, the last value ' |
| 11519 | 'for that key\n' |
| 11520 | ' becomes the corresponding value in the new dictionary.\n' |
| 11521 | '\n' |
| 11522 | ' If keyword arguments are given, the keyword arguments ' |
| 11523 | 'and their\n' |
| 11524 | ' values are added to the dictionary created from the ' |
| 11525 | 'positional\n' |
| 11526 | ' argument. If a key being added is already present, the ' |
| 11527 | 'value from\n' |
| 11528 | ' the keyword argument replaces the value from the ' |
| 11529 | 'positional\n' |
| 11530 | ' argument.\n' |
| 11531 | '\n' |
| 11532 | ' To illustrate, the following examples all return a ' |
| 11533 | 'dictionary equal\n' |
| 11534 | ' to "{"one": 1, "two": 2, "three": 3}":\n' |
| 11535 | '\n' |
| 11536 | ' >>> a = dict(one=1, two=2, three=3)\n' |
| 11537 | " >>> b = {'one': 1, 'two': 2, 'three': 3}\n" |
| 11538 | " >>> c = dict(zip(['one', 'two', 'three'], [1, 2, " |
| 11539 | '3]))\n' |
| 11540 | " >>> d = dict([('two', 2), ('one', 1), ('three', " |
| 11541 | '3)])\n' |
| 11542 | " >>> e = dict({'three': 3, 'one': 1, 'two': 2})\n" |
| 11543 | ' >>> a == b == c == d == e\n' |
| 11544 | ' True\n' |
| 11545 | '\n' |
| 11546 | ' Providing keyword arguments as in the first example ' |
| 11547 | 'only works for\n' |
| 11548 | ' keys that are valid Python identifiers. Otherwise, any ' |
| 11549 | 'valid keys\n' |
| 11550 | ' can be used.\n' |
| 11551 | '\n' |
| 11552 | ' These are the operations that dictionaries support (and ' |
| 11553 | 'therefore,\n' |
| 11554 | ' custom mapping types should support too):\n' |
| 11555 | '\n' |
| 11556 | ' len(d)\n' |
| 11557 | '\n' |
| 11558 | ' Return the number of items in the dictionary *d*.\n' |
| 11559 | '\n' |
| 11560 | ' d[key]\n' |
| 11561 | '\n' |
| 11562 | ' Return the item of *d* with key *key*. Raises a ' |
| 11563 | '"KeyError" if\n' |
| 11564 | ' *key* is not in the map.\n' |
| 11565 | '\n' |
| 11566 | ' If a subclass of dict defines a method ' |
| 11567 | '"__missing__()" and *key*\n' |
| 11568 | ' is not present, the "d[key]" operation calls that ' |
| 11569 | 'method with\n' |
| 11570 | ' the key *key* as argument. The "d[key]" operation ' |
| 11571 | 'then returns\n' |
| 11572 | ' or raises whatever is returned or raised by the\n' |
| 11573 | ' "__missing__(key)" call. No other operations or ' |
| 11574 | 'methods invoke\n' |
| 11575 | ' "__missing__()". If "__missing__()" is not defined, ' |
| 11576 | '"KeyError"\n' |
| 11577 | ' is raised. "__missing__()" must be a method; it ' |
| 11578 | 'cannot be an\n' |
| 11579 | ' instance variable:\n' |
| 11580 | '\n' |
| 11581 | ' >>> class Counter(dict):\n' |
| 11582 | ' ... def __missing__(self, key):\n' |
| 11583 | ' ... return 0\n' |
| 11584 | ' >>> c = Counter()\n' |
| 11585 | " >>> c['red']\n" |
| 11586 | ' 0\n' |
| 11587 | " >>> c['red'] += 1\n" |
| 11588 | " >>> c['red']\n" |
| 11589 | ' 1\n' |
| 11590 | '\n' |
| 11591 | ' The example above shows part of the implementation ' |
| 11592 | 'of\n' |
| 11593 | ' "collections.Counter". A different "__missing__" ' |
| 11594 | 'method is used\n' |
| 11595 | ' by "collections.defaultdict".\n' |
| 11596 | '\n' |
| 11597 | ' d[key] = value\n' |
| 11598 | '\n' |
| 11599 | ' Set "d[key]" to *value*.\n' |
| 11600 | '\n' |
| 11601 | ' del d[key]\n' |
| 11602 | '\n' |
| 11603 | ' Remove "d[key]" from *d*. Raises a "KeyError" if ' |
| 11604 | '*key* is not\n' |
| 11605 | ' in the map.\n' |
| 11606 | '\n' |
| 11607 | ' key in d\n' |
| 11608 | '\n' |
| 11609 | ' Return "True" if *d* has a key *key*, else "False".\n' |
| 11610 | '\n' |
| 11611 | ' key not in d\n' |
| 11612 | '\n' |
| 11613 | ' Equivalent to "not key in d".\n' |
| 11614 | '\n' |
| 11615 | ' iter(d)\n' |
| 11616 | '\n' |
| 11617 | ' Return an iterator over the keys of the dictionary. ' |
| 11618 | 'This is a\n' |
| 11619 | ' shortcut for "iter(d.keys())".\n' |
| 11620 | '\n' |
| 11621 | ' clear()\n' |
| 11622 | '\n' |
| 11623 | ' Remove all items from the dictionary.\n' |
| 11624 | '\n' |
| 11625 | ' copy()\n' |
| 11626 | '\n' |
| 11627 | ' Return a shallow copy of the dictionary.\n' |
| 11628 | '\n' |
| 11629 | ' classmethod fromkeys(seq[, value])\n' |
| 11630 | '\n' |
| 11631 | ' Create a new dictionary with keys from *seq* and ' |
| 11632 | 'values set to\n' |
| 11633 | ' *value*.\n' |
| 11634 | '\n' |
| 11635 | ' "fromkeys()" is a class method that returns a new ' |
| 11636 | 'dictionary.\n' |
| 11637 | ' *value* defaults to "None".\n' |
| 11638 | '\n' |
| 11639 | ' get(key[, default])\n' |
| 11640 | '\n' |
| 11641 | ' Return the value for *key* if *key* is in the ' |
| 11642 | 'dictionary, else\n' |
| 11643 | ' *default*. If *default* is not given, it defaults to ' |
| 11644 | '"None", so\n' |
| 11645 | ' that this method never raises a "KeyError".\n' |
| 11646 | '\n' |
| 11647 | ' items()\n' |
| 11648 | '\n' |
| 11649 | ' Return a new view of the dictionary\'s items ("(key, ' |
| 11650 | 'value)"\n' |
| 11651 | ' pairs). See the *documentation of view objects*.\n' |
| 11652 | '\n' |
| 11653 | ' keys()\n' |
| 11654 | '\n' |
| 11655 | " Return a new view of the dictionary's keys. See " |
| 11656 | 'the\n' |
| 11657 | ' *documentation of view objects*.\n' |
| 11658 | '\n' |
| 11659 | ' pop(key[, default])\n' |
| 11660 | '\n' |
| 11661 | ' If *key* is in the dictionary, remove it and return ' |
| 11662 | 'its value,\n' |
| 11663 | ' else return *default*. If *default* is not given ' |
| 11664 | 'and *key* is\n' |
| 11665 | ' not in the dictionary, a "KeyError" is raised.\n' |
| 11666 | '\n' |
| 11667 | ' popitem()\n' |
| 11668 | '\n' |
| 11669 | ' Remove and return an arbitrary "(key, value)" pair ' |
| 11670 | 'from the\n' |
| 11671 | ' dictionary.\n' |
| 11672 | '\n' |
| 11673 | ' "popitem()" is useful to destructively iterate over ' |
| 11674 | 'a\n' |
| 11675 | ' dictionary, as often used in set algorithms. If the ' |
| 11676 | 'dictionary\n' |
| 11677 | ' is empty, calling "popitem()" raises a "KeyError".\n' |
| 11678 | '\n' |
| 11679 | ' setdefault(key[, default])\n' |
| 11680 | '\n' |
| 11681 | ' If *key* is in the dictionary, return its value. If ' |
| 11682 | 'not, insert\n' |
| 11683 | ' *key* with a value of *default* and return ' |
| 11684 | '*default*. *default*\n' |
| 11685 | ' defaults to "None".\n' |
| 11686 | '\n' |
| 11687 | ' update([other])\n' |
| 11688 | '\n' |
| 11689 | ' Update the dictionary with the key/value pairs from ' |
| 11690 | '*other*,\n' |
| 11691 | ' overwriting existing keys. Return "None".\n' |
| 11692 | '\n' |
| 11693 | ' "update()" accepts either another dictionary object ' |
| 11694 | 'or an\n' |
| 11695 | ' iterable of key/value pairs (as tuples or other ' |
| 11696 | 'iterables of\n' |
| 11697 | ' length two). If keyword arguments are specified, ' |
| 11698 | 'the dictionary\n' |
| 11699 | ' is then updated with those key/value pairs: ' |
| 11700 | '"d.update(red=1,\n' |
| 11701 | ' blue=2)".\n' |
| 11702 | '\n' |
| 11703 | ' values()\n' |
| 11704 | '\n' |
| 11705 | " Return a new view of the dictionary's values. See " |
| 11706 | 'the\n' |
| 11707 | ' *documentation of view objects*.\n' |
| 11708 | '\n' |
| 11709 | ' Dictionaries compare equal if and only if they have the ' |
| 11710 | 'same "(key,\n' |
| 11711 | ' value)" pairs. Order comparisons (\'<\', \'<=\', ' |
| 11712 | "'>=', '>') raise\n" |
| 11713 | ' "TypeError".\n' |
| 11714 | '\n' |
| 11715 | 'See also: "types.MappingProxyType" can be used to create a ' |
| 11716 | 'read-only\n' |
| 11717 | ' view of a "dict".\n' |
| 11718 | '\n' |
| 11719 | '\n' |
| 11720 | 'Dictionary view objects\n' |
| 11721 | '=======================\n' |
| 11722 | '\n' |
| 11723 | 'The objects returned by "dict.keys()", "dict.values()" ' |
| 11724 | 'and\n' |
| 11725 | '"dict.items()" are *view objects*. They provide a dynamic ' |
| 11726 | 'view on the\n' |
| 11727 | "dictionary's entries, which means that when the dictionary " |
| 11728 | 'changes,\n' |
| 11729 | 'the view reflects these changes.\n' |
| 11730 | '\n' |
| 11731 | 'Dictionary views can be iterated over to yield their ' |
| 11732 | 'respective data,\n' |
| 11733 | 'and support membership tests:\n' |
| 11734 | '\n' |
| 11735 | 'len(dictview)\n' |
| 11736 | '\n' |
| 11737 | ' Return the number of entries in the dictionary.\n' |
| 11738 | '\n' |
| 11739 | 'iter(dictview)\n' |
| 11740 | '\n' |
| 11741 | ' Return an iterator over the keys, values or items ' |
| 11742 | '(represented as\n' |
| 11743 | ' tuples of "(key, value)") in the dictionary.\n' |
| 11744 | '\n' |
| 11745 | ' Keys and values are iterated over in an arbitrary order ' |
| 11746 | 'which is\n' |
| 11747 | ' non-random, varies across Python implementations, and ' |
| 11748 | 'depends on\n' |
| 11749 | " the dictionary's history of insertions and deletions. " |
| 11750 | 'If keys,\n' |
| 11751 | ' values and items views are iterated over with no ' |
| 11752 | 'intervening\n' |
| 11753 | ' modifications to the dictionary, the order of items ' |
| 11754 | 'will directly\n' |
| 11755 | ' correspond. This allows the creation of "(value, key)" ' |
| 11756 | 'pairs using\n' |
| 11757 | ' "zip()": "pairs = zip(d.values(), d.keys())". Another ' |
| 11758 | 'way to\n' |
| 11759 | ' create the same list is "pairs = [(v, k) for (k, v) in ' |
| 11760 | 'd.items()]".\n' |
| 11761 | '\n' |
| 11762 | ' Iterating views while adding or deleting entries in the ' |
| 11763 | 'dictionary\n' |
| 11764 | ' may raise a "RuntimeError" or fail to iterate over all ' |
| 11765 | 'entries.\n' |
| 11766 | '\n' |
| 11767 | 'x in dictview\n' |
| 11768 | '\n' |
| 11769 | ' Return "True" if *x* is in the underlying dictionary\'s ' |
| 11770 | 'keys, values\n' |
| 11771 | ' or items (in the latter case, *x* should be a "(key, ' |
| 11772 | 'value)"\n' |
| 11773 | ' tuple).\n' |
| 11774 | '\n' |
| 11775 | 'Keys views are set-like since their entries are unique and ' |
| 11776 | 'hashable.\n' |
| 11777 | 'If all values are hashable, so that "(key, value)" pairs ' |
| 11778 | 'are unique\n' |
| 11779 | 'and hashable, then the items view is also set-like. ' |
| 11780 | '(Values views are\n' |
| 11781 | 'not treated as set-like since the entries are generally ' |
| 11782 | 'not unique.)\n' |
| 11783 | 'For set-like views, all of the operations defined for the ' |
| 11784 | 'abstract\n' |
| 11785 | 'base class "collections.abc.Set" are available (for ' |
| 11786 | 'example, "==",\n' |
| 11787 | '"<", or "^").\n' |
| 11788 | '\n' |
| 11789 | 'An example of dictionary view usage:\n' |
| 11790 | '\n' |
| 11791 | " >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, " |
| 11792 | "'spam': 500}\n" |
| 11793 | ' >>> keys = dishes.keys()\n' |
| 11794 | ' >>> values = dishes.values()\n' |
| 11795 | '\n' |
| 11796 | ' >>> # iteration\n' |
| 11797 | ' >>> n = 0\n' |
| 11798 | ' >>> for val in values:\n' |
| 11799 | ' ... n += val\n' |
| 11800 | ' >>> print(n)\n' |
| 11801 | ' 504\n' |
| 11802 | '\n' |
| 11803 | ' >>> # keys and values are iterated over in the same ' |
| 11804 | 'order\n' |
| 11805 | ' >>> list(keys)\n' |
| 11806 | " ['eggs', 'bacon', 'sausage', 'spam']\n" |
| 11807 | ' >>> list(values)\n' |
| 11808 | ' [2, 1, 1, 500]\n' |
| 11809 | '\n' |
| 11810 | ' >>> # view objects are dynamic and reflect dict ' |
| 11811 | 'changes\n' |
| 11812 | " >>> del dishes['eggs']\n" |
| 11813 | " >>> del dishes['sausage']\n" |
| 11814 | ' >>> list(keys)\n' |
| 11815 | " ['spam', 'bacon']\n" |
| 11816 | '\n' |
| 11817 | ' >>> # set operations\n' |
| 11818 | " >>> keys & {'eggs', 'bacon', 'salad'}\n" |
| 11819 | " {'bacon'}\n" |
| 11820 | " >>> keys ^ {'sausage', 'juice'}\n" |
| 11821 | " {'juice', 'sausage', 'bacon', 'spam'}\n", |
| 11822 | 'typesmethods': '\n' |
| 11823 | 'Methods\n' |
| 11824 | '*******\n' |
| 11825 | '\n' |
| 11826 | 'Methods are functions that are called using the attribute ' |
| 11827 | 'notation.\n' |
| 11828 | 'There are two flavors: built-in methods (such as ' |
| 11829 | '"append()" on lists)\n' |
| 11830 | 'and class instance methods. Built-in methods are ' |
| 11831 | 'described with the\n' |
| 11832 | 'types that support them.\n' |
| 11833 | '\n' |
| 11834 | 'If you access a method (a function defined in a class ' |
| 11835 | 'namespace)\n' |
| 11836 | 'through an instance, you get a special object: a *bound ' |
| 11837 | 'method* (also\n' |
| 11838 | 'called *instance method*) object. When called, it will add ' |
| 11839 | 'the "self"\n' |
| 11840 | 'argument to the argument list. Bound methods have two ' |
| 11841 | 'special read-\n' |
| 11842 | 'only attributes: "m.__self__" is the object on which the ' |
| 11843 | 'method\n' |
| 11844 | 'operates, and "m.__func__" is the function implementing ' |
| 11845 | 'the method.\n' |
| 11846 | 'Calling "m(arg-1, arg-2, ..., arg-n)" is completely ' |
| 11847 | 'equivalent to\n' |
| 11848 | 'calling "m.__func__(m.__self__, arg-1, arg-2, ..., ' |
| 11849 | 'arg-n)".\n' |
| 11850 | '\n' |
| 11851 | 'Like function objects, bound method objects support ' |
| 11852 | 'getting arbitrary\n' |
| 11853 | 'attributes. However, since method attributes are actually ' |
| 11854 | 'stored on\n' |
| 11855 | 'the underlying function object ("meth.__func__"), setting ' |
| 11856 | 'method\n' |
| 11857 | 'attributes on bound methods is disallowed. Attempting to ' |
| 11858 | 'set an\n' |
| 11859 | 'attribute on a method results in an "AttributeError" being ' |
| 11860 | 'raised. In\n' |
| 11861 | 'order to set a method attribute, you need to explicitly ' |
| 11862 | 'set it on the\n' |
| 11863 | 'underlying function object:\n' |
| 11864 | '\n' |
| 11865 | ' >>> class C:\n' |
| 11866 | ' ... def method(self):\n' |
| 11867 | ' ... pass\n' |
| 11868 | ' ...\n' |
| 11869 | ' >>> c = C()\n' |
| 11870 | " >>> c.method.whoami = 'my name is method' # can't set " |
| 11871 | 'on the method\n' |
| 11872 | ' Traceback (most recent call last):\n' |
| 11873 | ' File "<stdin>", line 1, in <module>\n' |
| 11874 | " AttributeError: 'method' object has no attribute " |
| 11875 | "'whoami'\n" |
| 11876 | " >>> c.method.__func__.whoami = 'my name is method'\n" |
| 11877 | ' >>> c.method.whoami\n' |
| 11878 | " 'my name is method'\n" |
| 11879 | '\n' |
| 11880 | 'See *The standard type hierarchy* for more information.\n', |
| 11881 | 'typesmodules': '\n' |
| 11882 | 'Modules\n' |
| 11883 | '*******\n' |
| 11884 | '\n' |
| 11885 | 'The only special operation on a module is attribute ' |
| 11886 | 'access: "m.name",\n' |
| 11887 | 'where *m* is a module and *name* accesses a name defined ' |
| 11888 | "in *m*'s\n" |
| 11889 | 'symbol table. Module attributes can be assigned to. (Note ' |
| 11890 | 'that the\n' |
| 11891 | '"import" statement is not, strictly speaking, an operation ' |
| 11892 | 'on a module\n' |
| 11893 | 'object; "import foo" does not require a module object ' |
| 11894 | 'named *foo* to\n' |
| 11895 | 'exist, rather it requires an (external) *definition* for a ' |
| 11896 | 'module\n' |
| 11897 | 'named *foo* somewhere.)\n' |
| 11898 | '\n' |
| 11899 | 'A special attribute of every module is "__dict__". This is ' |
| 11900 | 'the\n' |
| 11901 | "dictionary containing the module's symbol table. Modifying " |
| 11902 | 'this\n' |
| 11903 | "dictionary will actually change the module's symbol table, " |
| 11904 | 'but direct\n' |
| 11905 | 'assignment to the "__dict__" attribute is not possible ' |
| 11906 | '(you can write\n' |
| 11907 | '"m.__dict__[\'a\'] = 1", which defines "m.a" to be "1", ' |
| 11908 | "but you can't\n" |
| 11909 | 'write "m.__dict__ = {}"). Modifying "__dict__" directly ' |
| 11910 | 'is not\n' |
| 11911 | 'recommended.\n' |
| 11912 | '\n' |
| 11913 | 'Modules built into the interpreter are written like this: ' |
| 11914 | '"<module\n' |
| 11915 | '\'sys\' (built-in)>". If loaded from a file, they are ' |
| 11916 | 'written as\n' |
| 11917 | '"<module \'os\' from ' |
| 11918 | '\'/usr/local/lib/pythonX.Y/os.pyc\'>".\n', |
| 11919 | 'typesseq': '\n' |
| 11920 | 'Sequence Types --- "list", "tuple", "range"\n' |
| 11921 | '*******************************************\n' |
| 11922 | '\n' |
| 11923 | 'There are three basic sequence types: lists, tuples, and ' |
| 11924 | 'range\n' |
| 11925 | 'objects. Additional sequence types tailored for processing of ' |
| 11926 | '*binary\n' |
| 11927 | 'data* and *text strings* are described in dedicated sections.\n' |
| 11928 | '\n' |
| 11929 | '\n' |
| 11930 | 'Common Sequence Operations\n' |
| 11931 | '==========================\n' |
| 11932 | '\n' |
| 11933 | 'The operations in the following table are supported by most ' |
| 11934 | 'sequence\n' |
| 11935 | 'types, both mutable and immutable. The ' |
| 11936 | '"collections.abc.Sequence" ABC\n' |
| 11937 | 'is provided to make it easier to correctly implement these ' |
| 11938 | 'operations\n' |
| 11939 | 'on custom sequence types.\n' |
| 11940 | '\n' |
| 11941 | 'This table lists the sequence operations sorted in ascending ' |
| 11942 | 'priority.\n' |
| 11943 | 'In the table, *s* and *t* are sequences of the same type, *n*, ' |
| 11944 | '*i*,\n' |
| 11945 | '*j* and *k* are integers and *x* is an arbitrary object that ' |
| 11946 | 'meets any\n' |
| 11947 | 'type and value restrictions imposed by *s*.\n' |
| 11948 | '\n' |
| 11949 | 'The "in" and "not in" operations have the same priorities as ' |
| 11950 | 'the\n' |
| 11951 | 'comparison operations. The "+" (concatenation) and "*" ' |
| 11952 | '(repetition)\n' |
| 11953 | 'operations have the same priority as the corresponding ' |
| 11954 | 'numeric\n' |
| 11955 | 'operations.\n' |
| 11956 | '\n' |
| 11957 | '+----------------------------+----------------------------------+------------+\n' |
| 11958 | '| Operation | ' |
| 11959 | 'Result | Notes |\n' |
| 11960 | '+============================+==================================+============+\n' |
| 11961 | '| "x in s" | "True" if an item of *s* ' |
| 11962 | 'is | (1) |\n' |
| 11963 | '| | equal to *x*, else ' |
| 11964 | '"False" | |\n' |
| 11965 | '+----------------------------+----------------------------------+------------+\n' |
| 11966 | '| "x not in s" | "False" if an item of *s* ' |
| 11967 | 'is | (1) |\n' |
| 11968 | '| | equal to *x*, else ' |
| 11969 | '"True" | |\n' |
| 11970 | '+----------------------------+----------------------------------+------------+\n' |
| 11971 | '| "s + t" | the concatenation of *s* and ' |
| 11972 | '*t* | (6)(7) |\n' |
| 11973 | '+----------------------------+----------------------------------+------------+\n' |
| 11974 | '| "s * n" or "n * s" | *n* shallow copies of ' |
| 11975 | '*s* | (2)(7) |\n' |
| 11976 | '| | ' |
| 11977 | 'concatenated | |\n' |
| 11978 | '+----------------------------+----------------------------------+------------+\n' |
| 11979 | '| "s[i]" | *i*th item of *s*, origin ' |
| 11980 | '0 | (3) |\n' |
| 11981 | '+----------------------------+----------------------------------+------------+\n' |
| 11982 | '| "s[i:j]" | slice of *s* from *i* to ' |
| 11983 | '*j* | (3)(4) |\n' |
| 11984 | '+----------------------------+----------------------------------+------------+\n' |
| 11985 | '| "s[i:j:k]" | slice of *s* from *i* to ' |
| 11986 | '*j* | (3)(5) |\n' |
| 11987 | '| | with step ' |
| 11988 | '*k* | |\n' |
| 11989 | '+----------------------------+----------------------------------+------------+\n' |
| 11990 | '| "len(s)" | length of ' |
| 11991 | '*s* | |\n' |
| 11992 | '+----------------------------+----------------------------------+------------+\n' |
| 11993 | '| "min(s)" | smallest item of ' |
| 11994 | '*s* | |\n' |
| 11995 | '+----------------------------+----------------------------------+------------+\n' |
| 11996 | '| "max(s)" | largest item of ' |
| 11997 | '*s* | |\n' |
| 11998 | '+----------------------------+----------------------------------+------------+\n' |
| 11999 | '| "s.index(x[, i[, j]])" | index of the first occurrence ' |
| 12000 | 'of | (8) |\n' |
| 12001 | '| | *x* in *s* (at or after ' |
| 12002 | 'index | |\n' |
| 12003 | '| | *i* and before index ' |
| 12004 | '*j*) | |\n' |
| 12005 | '+----------------------------+----------------------------------+------------+\n' |
| 12006 | '| "s.count(x)" | total number of occurrences ' |
| 12007 | 'of | |\n' |
| 12008 | '| | *x* in ' |
| 12009 | '*s* | |\n' |
| 12010 | '+----------------------------+----------------------------------+------------+\n' |
| 12011 | '\n' |
| 12012 | 'Sequences of the same type also support comparisons. In ' |
| 12013 | 'particular,\n' |
| 12014 | 'tuples and lists are compared lexicographically by comparing\n' |
| 12015 | 'corresponding elements. This means that to compare equal, ' |
| 12016 | 'every\n' |
| 12017 | 'element must compare equal and the two sequences must be of ' |
| 12018 | 'the same\n' |
| 12019 | 'type and have the same length. (For full details see ' |
| 12020 | '*Comparisons* in\n' |
| 12021 | 'the language reference.)\n' |
| 12022 | '\n' |
| 12023 | 'Notes:\n' |
| 12024 | '\n' |
| 12025 | '1. While the "in" and "not in" operations are used only for ' |
| 12026 | 'simple\n' |
| 12027 | ' containment testing in the general case, some specialised ' |
| 12028 | 'sequences\n' |
| 12029 | ' (such as "str", "bytes" and "bytearray") also use them for\n' |
| 12030 | ' subsequence testing:\n' |
| 12031 | '\n' |
| 12032 | ' >>> "gg" in "eggs"\n' |
| 12033 | ' True\n' |
| 12034 | '\n' |
| 12035 | '2. Values of *n* less than "0" are treated as "0" (which ' |
| 12036 | 'yields an\n' |
| 12037 | ' empty sequence of the same type as *s*). Note also that ' |
| 12038 | 'the copies\n' |
| 12039 | ' are shallow; nested structures are not copied. This often ' |
| 12040 | 'haunts\n' |
| 12041 | ' new Python programmers; consider:\n' |
| 12042 | '\n' |
| 12043 | ' >>> lists = [[]] * 3\n' |
| 12044 | ' >>> lists\n' |
| 12045 | ' [[], [], []]\n' |
| 12046 | ' >>> lists[0].append(3)\n' |
| 12047 | ' >>> lists\n' |
| 12048 | ' [[3], [3], [3]]\n' |
| 12049 | '\n' |
| 12050 | ' What has happened is that "[[]]" is a one-element list ' |
| 12051 | 'containing\n' |
| 12052 | ' an empty list, so all three elements of "[[]] * 3" are ' |
| 12053 | '(pointers\n' |
| 12054 | ' to) this single empty list. Modifying any of the elements ' |
| 12055 | 'of\n' |
| 12056 | ' "lists" modifies this single list. You can create a list ' |
| 12057 | 'of\n' |
| 12058 | ' different lists this way:\n' |
| 12059 | '\n' |
| 12060 | ' >>> lists = [[] for i in range(3)]\n' |
| 12061 | ' >>> lists[0].append(3)\n' |
| 12062 | ' >>> lists[1].append(5)\n' |
| 12063 | ' >>> lists[2].append(7)\n' |
| 12064 | ' >>> lists\n' |
| 12065 | ' [[3], [5], [7]]\n' |
| 12066 | '\n' |
| 12067 | '3. If *i* or *j* is negative, the index is relative to the end ' |
| 12068 | 'of\n' |
| 12069 | ' the string: "len(s) + i" or "len(s) + j" is substituted. ' |
| 12070 | 'But note\n' |
| 12071 | ' that "-0" is still "0".\n' |
| 12072 | '\n' |
| 12073 | '4. The slice of *s* from *i* to *j* is defined as the sequence ' |
| 12074 | 'of\n' |
| 12075 | ' items with index *k* such that "i <= k < j". If *i* or *j* ' |
| 12076 | 'is\n' |
| 12077 | ' greater than "len(s)", use "len(s)". If *i* is omitted or ' |
| 12078 | '"None",\n' |
| 12079 | ' use "0". If *j* is omitted or "None", use "len(s)". If ' |
| 12080 | '*i* is\n' |
| 12081 | ' greater than or equal to *j*, the slice is empty.\n' |
| 12082 | '\n' |
| 12083 | '5. The slice of *s* from *i* to *j* with step *k* is defined ' |
| 12084 | 'as the\n' |
| 12085 | ' sequence of items with index "x = i + n*k" such that "0 <= ' |
| 12086 | 'n <\n' |
| 12087 | ' (j-i)/k". In other words, the indices are "i", "i+k", ' |
| 12088 | '"i+2*k",\n' |
| 12089 | ' "i+3*k" and so on, stopping when *j* is reached (but never\n' |
| 12090 | ' including *j*). If *i* or *j* is greater than "len(s)", ' |
| 12091 | 'use\n' |
| 12092 | ' "len(s)". If *i* or *j* are omitted or "None", they become ' |
| 12093 | '"end"\n' |
| 12094 | ' values (which end depends on the sign of *k*). Note, *k* ' |
| 12095 | 'cannot be\n' |
| 12096 | ' zero. If *k* is "None", it is treated like "1".\n' |
| 12097 | '\n' |
| 12098 | '6. Concatenating immutable sequences always results in a new\n' |
| 12099 | ' object. This means that building up a sequence by repeated\n' |
| 12100 | ' concatenation will have a quadratic runtime cost in the ' |
| 12101 | 'total\n' |
| 12102 | ' sequence length. To get a linear runtime cost, you must ' |
| 12103 | 'switch to\n' |
| 12104 | ' one of the alternatives below:\n' |
| 12105 | '\n' |
| 12106 | ' * if concatenating "str" objects, you can build a list and ' |
| 12107 | 'use\n' |
| 12108 | ' "str.join()" at the end or else write to a "io.StringIO" ' |
| 12109 | 'instance\n' |
| 12110 | ' and retrieve its value when complete\n' |
| 12111 | '\n' |
| 12112 | ' * if concatenating "bytes" objects, you can similarly use\n' |
| 12113 | ' "bytes.join()" or "io.BytesIO", or you can do in-place\n' |
| 12114 | ' concatenation with a "bytearray" object. "bytearray" ' |
| 12115 | 'objects are\n' |
| 12116 | ' mutable and have an efficient overallocation mechanism\n' |
| 12117 | '\n' |
| 12118 | ' * if concatenating "tuple" objects, extend a "list" ' |
| 12119 | 'instead\n' |
| 12120 | '\n' |
| 12121 | ' * for other types, investigate the relevant class ' |
| 12122 | 'documentation\n' |
| 12123 | '\n' |
| 12124 | '7. Some sequence types (such as "range") only support item\n' |
| 12125 | " sequences that follow specific patterns, and hence don't " |
| 12126 | 'support\n' |
| 12127 | ' sequence concatenation or repetition.\n' |
| 12128 | '\n' |
| 12129 | '8. "index" raises "ValueError" when *x* is not found in *s*. ' |
| 12130 | 'When\n' |
| 12131 | ' supported, the additional arguments to the index method ' |
| 12132 | 'allow\n' |
| 12133 | ' efficient searching of subsections of the sequence. Passing ' |
| 12134 | 'the\n' |
| 12135 | ' extra arguments is roughly equivalent to using ' |
| 12136 | '"s[i:j].index(x)",\n' |
| 12137 | ' only without copying any data and with the returned index ' |
| 12138 | 'being\n' |
| 12139 | ' relative to the start of the sequence rather than the start ' |
| 12140 | 'of the\n' |
| 12141 | ' slice.\n' |
| 12142 | '\n' |
| 12143 | '\n' |
| 12144 | 'Immutable Sequence Types\n' |
| 12145 | '========================\n' |
| 12146 | '\n' |
| 12147 | 'The only operation that immutable sequence types generally ' |
| 12148 | 'implement\n' |
| 12149 | 'that is not also implemented by mutable sequence types is ' |
| 12150 | 'support for\n' |
| 12151 | 'the "hash()" built-in.\n' |
| 12152 | '\n' |
| 12153 | 'This support allows immutable sequences, such as "tuple" ' |
| 12154 | 'instances, to\n' |
| 12155 | 'be used as "dict" keys and stored in "set" and "frozenset" ' |
| 12156 | 'instances.\n' |
| 12157 | '\n' |
| 12158 | 'Attempting to hash an immutable sequence that contains ' |
| 12159 | 'unhashable\n' |
| 12160 | 'values will result in "TypeError".\n' |
| 12161 | '\n' |
| 12162 | '\n' |
| 12163 | 'Mutable Sequence Types\n' |
| 12164 | '======================\n' |
| 12165 | '\n' |
| 12166 | 'The operations in the following table are defined on mutable ' |
| 12167 | 'sequence\n' |
| 12168 | 'types. The "collections.abc.MutableSequence" ABC is provided ' |
| 12169 | 'to make\n' |
| 12170 | 'it easier to correctly implement these operations on custom ' |
| 12171 | 'sequence\n' |
| 12172 | 'types.\n' |
| 12173 | '\n' |
| 12174 | 'In the table *s* is an instance of a mutable sequence type, ' |
| 12175 | '*t* is any\n' |
| 12176 | 'iterable object and *x* is an arbitrary object that meets any ' |
| 12177 | 'type and\n' |
| 12178 | 'value restrictions imposed by *s* (for example, "bytearray" ' |
| 12179 | 'only\n' |
| 12180 | 'accepts integers that meet the value restriction "0 <= x <= ' |
| 12181 | '255").\n' |
| 12182 | '\n' |
| 12183 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12184 | '| Operation | ' |
| 12185 | 'Result | Notes |\n' |
| 12186 | '+================================+==================================+=======================+\n' |
| 12187 | '| "s[i] = x" | item *i* of *s* is replaced ' |
| 12188 | 'by | |\n' |
| 12189 | '| | ' |
| 12190 | '*x* | |\n' |
| 12191 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12192 | '| "s[i:j] = t" | slice of *s* from *i* to ' |
| 12193 | '*j* is | |\n' |
| 12194 | '| | replaced by the contents of ' |
| 12195 | 'the | |\n' |
| 12196 | '| | iterable ' |
| 12197 | '*t* | |\n' |
| 12198 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12199 | '| "del s[i:j]" | same as "s[i:j] = ' |
| 12200 | '[]" | |\n' |
| 12201 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12202 | '| "s[i:j:k] = t" | the elements of "s[i:j:k]" ' |
| 12203 | 'are | (1) |\n' |
| 12204 | '| | replaced by those of ' |
| 12205 | '*t* | |\n' |
| 12206 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12207 | '| "del s[i:j:k]" | removes the elements ' |
| 12208 | 'of | |\n' |
| 12209 | '| | "s[i:j:k]" from the ' |
| 12210 | 'list | |\n' |
| 12211 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12212 | '| "s.append(x)" | appends *x* to the end of ' |
| 12213 | 'the | |\n' |
| 12214 | '| | sequence (same ' |
| 12215 | 'as | |\n' |
| 12216 | '| | "s[len(s):len(s)] = ' |
| 12217 | '[x]") | |\n' |
| 12218 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12219 | '| "s.clear()" | removes all items from "s" ' |
| 12220 | '(same | (5) |\n' |
| 12221 | '| | as "del ' |
| 12222 | 's[:]") | |\n' |
| 12223 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12224 | '| "s.copy()" | creates a shallow copy of ' |
| 12225 | '"s" | (5) |\n' |
| 12226 | '| | (same as ' |
| 12227 | '"s[:]") | |\n' |
| 12228 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12229 | '| "s.extend(t)" | extends *s* with the ' |
| 12230 | 'contents of | |\n' |
| 12231 | '| | *t* (same as ' |
| 12232 | '"s[len(s):len(s)] = | |\n' |
| 12233 | '| | ' |
| 12234 | 't") | |\n' |
| 12235 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12236 | '| "s.insert(i, x)" | inserts *x* into *s* at ' |
| 12237 | 'the | |\n' |
| 12238 | '| | index given by *i* (same ' |
| 12239 | 'as | |\n' |
| 12240 | '| | "s[i:i] = ' |
| 12241 | '[x]") | |\n' |
| 12242 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12243 | '| "s.pop([i])" | retrieves the item at *i* ' |
| 12244 | 'and | (2) |\n' |
| 12245 | '| | also removes it from ' |
| 12246 | '*s* | |\n' |
| 12247 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12248 | '| "s.remove(x)" | remove the first item from ' |
| 12249 | '*s* | (3) |\n' |
| 12250 | '| | where "s[i] == ' |
| 12251 | 'x" | |\n' |
| 12252 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12253 | '| "s.reverse()" | reverses the items of *s* ' |
| 12254 | 'in | (4) |\n' |
| 12255 | '| | ' |
| 12256 | 'place | |\n' |
| 12257 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12258 | '\n' |
| 12259 | 'Notes:\n' |
| 12260 | '\n' |
| 12261 | '1. *t* must have the same length as the slice it is ' |
| 12262 | 'replacing.\n' |
| 12263 | '\n' |
| 12264 | '2. The optional argument *i* defaults to "-1", so that by ' |
| 12265 | 'default\n' |
| 12266 | ' the last item is removed and returned.\n' |
| 12267 | '\n' |
| 12268 | '3. "remove" raises "ValueError" when *x* is not found in *s*.\n' |
| 12269 | '\n' |
| 12270 | '4. The "reverse()" method modifies the sequence in place for\n' |
| 12271 | ' economy of space when reversing a large sequence. To ' |
| 12272 | 'remind users\n' |
| 12273 | ' that it operates by side effect, it does not return the ' |
| 12274 | 'reversed\n' |
| 12275 | ' sequence.\n' |
| 12276 | '\n' |
| 12277 | '5. "clear()" and "copy()" are included for consistency with ' |
| 12278 | 'the\n' |
| 12279 | " interfaces of mutable containers that don't support " |
| 12280 | 'slicing\n' |
| 12281 | ' operations (such as "dict" and "set")\n' |
| 12282 | '\n' |
| 12283 | ' New in version 3.3: "clear()" and "copy()" methods.\n' |
| 12284 | '\n' |
| 12285 | '\n' |
| 12286 | 'Lists\n' |
| 12287 | '=====\n' |
| 12288 | '\n' |
| 12289 | 'Lists are mutable sequences, typically used to store ' |
| 12290 | 'collections of\n' |
| 12291 | 'homogeneous items (where the precise degree of similarity will ' |
| 12292 | 'vary by\n' |
| 12293 | 'application).\n' |
| 12294 | '\n' |
| 12295 | 'class class list([iterable])\n' |
| 12296 | '\n' |
| 12297 | ' Lists may be constructed in several ways:\n' |
| 12298 | '\n' |
| 12299 | ' * Using a pair of square brackets to denote the empty list: ' |
| 12300 | '"[]"\n' |
| 12301 | '\n' |
| 12302 | ' * Using square brackets, separating items with commas: ' |
| 12303 | '"[a]",\n' |
| 12304 | ' "[a, b, c]"\n' |
| 12305 | '\n' |
| 12306 | ' * Using a list comprehension: "[x for x in iterable]"\n' |
| 12307 | '\n' |
| 12308 | ' * Using the type constructor: "list()" or "list(iterable)"\n' |
| 12309 | '\n' |
| 12310 | ' The constructor builds a list whose items are the same and ' |
| 12311 | 'in the\n' |
| 12312 | " same order as *iterable*'s items. *iterable* may be either " |
| 12313 | 'a\n' |
| 12314 | ' sequence, a container that supports iteration, or an ' |
| 12315 | 'iterator\n' |
| 12316 | ' object. If *iterable* is already a list, a copy is made ' |
| 12317 | 'and\n' |
| 12318 | ' returned, similar to "iterable[:]". For example, ' |
| 12319 | '"list(\'abc\')"\n' |
| 12320 | ' returns "[\'a\', \'b\', \'c\']" and "list( (1, 2, 3) )" ' |
| 12321 | 'returns "[1, 2,\n' |
| 12322 | ' 3]". If no argument is given, the constructor creates a new ' |
| 12323 | 'empty\n' |
| 12324 | ' list, "[]".\n' |
| 12325 | '\n' |
| 12326 | ' Many other operations also produce lists, including the ' |
| 12327 | '"sorted()"\n' |
| 12328 | ' built-in.\n' |
| 12329 | '\n' |
| 12330 | ' Lists implement all of the *common* and *mutable* sequence\n' |
| 12331 | ' operations. Lists also provide the following additional ' |
| 12332 | 'method:\n' |
| 12333 | '\n' |
| 12334 | ' sort(*, key=None, reverse=None)\n' |
| 12335 | '\n' |
| 12336 | ' This method sorts the list in place, using only "<" ' |
| 12337 | 'comparisons\n' |
| 12338 | ' between items. Exceptions are not suppressed - if any ' |
| 12339 | 'comparison\n' |
| 12340 | ' operations fail, the entire sort operation will fail ' |
| 12341 | '(and the\n' |
| 12342 | ' list will likely be left in a partially modified ' |
| 12343 | 'state).\n' |
| 12344 | '\n' |
| 12345 | ' "sort()" accepts two arguments that can only be passed ' |
| 12346 | 'by\n' |
| 12347 | ' keyword (*keyword-only arguments*):\n' |
| 12348 | '\n' |
| 12349 | ' *key* specifies a function of one argument that is used ' |
| 12350 | 'to\n' |
| 12351 | ' extract a comparison key from each list element (for ' |
| 12352 | 'example,\n' |
| 12353 | ' "key=str.lower"). The key corresponding to each item in ' |
| 12354 | 'the list\n' |
| 12355 | ' is calculated once and then used for the entire sorting ' |
| 12356 | 'process.\n' |
| 12357 | ' The default value of "None" means that list items are ' |
| 12358 | 'sorted\n' |
| 12359 | ' directly without calculating a separate key value.\n' |
| 12360 | '\n' |
| 12361 | ' The "functools.cmp_to_key()" utility is available to ' |
| 12362 | 'convert a\n' |
| 12363 | ' 2.x style *cmp* function to a *key* function.\n' |
| 12364 | '\n' |
| 12365 | ' *reverse* is a boolean value. If set to "True", then ' |
| 12366 | 'the list\n' |
| 12367 | ' elements are sorted as if each comparison were ' |
| 12368 | 'reversed.\n' |
| 12369 | '\n' |
| 12370 | ' This method modifies the sequence in place for economy ' |
| 12371 | 'of space\n' |
| 12372 | ' when sorting a large sequence. To remind users that it ' |
| 12373 | 'operates\n' |
| 12374 | ' by side effect, it does not return the sorted sequence ' |
| 12375 | '(use\n' |
| 12376 | ' "sorted()" to explicitly request a new sorted list ' |
| 12377 | 'instance).\n' |
| 12378 | '\n' |
| 12379 | ' The "sort()" method is guaranteed to be stable. A sort ' |
| 12380 | 'is\n' |
| 12381 | ' stable if it guarantees not to change the relative order ' |
| 12382 | 'of\n' |
| 12383 | ' elements that compare equal --- this is helpful for ' |
| 12384 | 'sorting in\n' |
| 12385 | ' multiple passes (for example, sort by department, then ' |
| 12386 | 'by salary\n' |
| 12387 | ' grade).\n' |
| 12388 | '\n' |
| 12389 | ' **CPython implementation detail:** While a list is being ' |
| 12390 | 'sorted,\n' |
| 12391 | ' the effect of attempting to mutate, or even inspect, the ' |
| 12392 | 'list is\n' |
| 12393 | ' undefined. The C implementation of Python makes the ' |
| 12394 | 'list appear\n' |
| 12395 | ' empty for the duration, and raises "ValueError" if it ' |
| 12396 | 'can detect\n' |
| 12397 | ' that the list has been mutated during a sort.\n' |
| 12398 | '\n' |
| 12399 | '\n' |
| 12400 | 'Tuples\n' |
| 12401 | '======\n' |
| 12402 | '\n' |
| 12403 | 'Tuples are immutable sequences, typically used to store ' |
| 12404 | 'collections of\n' |
| 12405 | 'heterogeneous data (such as the 2-tuples produced by the ' |
| 12406 | '"enumerate()"\n' |
| 12407 | 'built-in). Tuples are also used for cases where an immutable ' |
| 12408 | 'sequence\n' |
| 12409 | 'of homogeneous data is needed (such as allowing storage in a ' |
| 12410 | '"set" or\n' |
| 12411 | '"dict" instance).\n' |
| 12412 | '\n' |
| 12413 | 'class class tuple([iterable])\n' |
| 12414 | '\n' |
| 12415 | ' Tuples may be constructed in a number of ways:\n' |
| 12416 | '\n' |
| 12417 | ' * Using a pair of parentheses to denote the empty tuple: ' |
| 12418 | '"()"\n' |
| 12419 | '\n' |
| 12420 | ' * Using a trailing comma for a singleton tuple: "a," or ' |
| 12421 | '"(a,)"\n' |
| 12422 | '\n' |
| 12423 | ' * Separating items with commas: "a, b, c" or "(a, b, c)"\n' |
| 12424 | '\n' |
| 12425 | ' * Using the "tuple()" built-in: "tuple()" or ' |
| 12426 | '"tuple(iterable)"\n' |
| 12427 | '\n' |
| 12428 | ' The constructor builds a tuple whose items are the same and ' |
| 12429 | 'in the\n' |
| 12430 | " same order as *iterable*'s items. *iterable* may be either " |
| 12431 | 'a\n' |
| 12432 | ' sequence, a container that supports iteration, or an ' |
| 12433 | 'iterator\n' |
| 12434 | ' object. If *iterable* is already a tuple, it is returned\n' |
| 12435 | ' unchanged. For example, "tuple(\'abc\')" returns "(\'a\', ' |
| 12436 | '\'b\', \'c\')"\n' |
| 12437 | ' and "tuple( [1, 2, 3] )" returns "(1, 2, 3)". If no ' |
| 12438 | 'argument is\n' |
| 12439 | ' given, the constructor creates a new empty tuple, "()".\n' |
| 12440 | '\n' |
| 12441 | ' Note that it is actually the comma which makes a tuple, not ' |
| 12442 | 'the\n' |
| 12443 | ' parentheses. The parentheses are optional, except in the ' |
| 12444 | 'empty\n' |
| 12445 | ' tuple case, or when they are needed to avoid syntactic ' |
| 12446 | 'ambiguity.\n' |
| 12447 | ' For example, "f(a, b, c)" is a function call with three ' |
| 12448 | 'arguments,\n' |
| 12449 | ' while "f((a, b, c))" is a function call with a 3-tuple as ' |
| 12450 | 'the sole\n' |
| 12451 | ' argument.\n' |
| 12452 | '\n' |
| 12453 | ' Tuples implement all of the *common* sequence operations.\n' |
| 12454 | '\n' |
| 12455 | 'For heterogeneous collections of data where access by name is ' |
| 12456 | 'clearer\n' |
| 12457 | 'than access by index, "collections.namedtuple()" may be a ' |
| 12458 | 'more\n' |
| 12459 | 'appropriate choice than a simple tuple object.\n' |
| 12460 | '\n' |
| 12461 | '\n' |
| 12462 | 'Ranges\n' |
| 12463 | '======\n' |
| 12464 | '\n' |
| 12465 | 'The "range" type represents an immutable sequence of numbers ' |
| 12466 | 'and is\n' |
| 12467 | 'commonly used for looping a specific number of times in "for" ' |
| 12468 | 'loops.\n' |
| 12469 | '\n' |
| 12470 | 'class class range(stop)\n' |
| 12471 | 'class class range(start, stop[, step])\n' |
| 12472 | '\n' |
| 12473 | ' The arguments to the range constructor must be integers ' |
| 12474 | '(either\n' |
| 12475 | ' built-in "int" or any object that implements the ' |
| 12476 | '"__index__"\n' |
| 12477 | ' special method). If the *step* argument is omitted, it ' |
| 12478 | 'defaults to\n' |
| 12479 | ' "1". If the *start* argument is omitted, it defaults to ' |
| 12480 | '"0". If\n' |
| 12481 | ' *step* is zero, "ValueError" is raised.\n' |
| 12482 | '\n' |
| 12483 | ' For a positive *step*, the contents of a range "r" are ' |
| 12484 | 'determined\n' |
| 12485 | ' by the formula "r[i] = start + step*i" where "i >= 0" and ' |
| 12486 | '"r[i] <\n' |
| 12487 | ' stop".\n' |
| 12488 | '\n' |
| 12489 | ' For a negative *step*, the contents of the range are still\n' |
| 12490 | ' determined by the formula "r[i] = start + step*i", but the\n' |
| 12491 | ' constraints are "i >= 0" and "r[i] > stop".\n' |
| 12492 | '\n' |
| 12493 | ' A range object will be empty if "r[0]" does not meet the ' |
| 12494 | 'value\n' |
| 12495 | ' constraint. Ranges do support negative indices, but these ' |
| 12496 | 'are\n' |
| 12497 | ' interpreted as indexing from the end of the sequence ' |
| 12498 | 'determined by\n' |
| 12499 | ' the positive indices.\n' |
| 12500 | '\n' |
| 12501 | ' Ranges containing absolute values larger than "sys.maxsize" ' |
| 12502 | 'are\n' |
| 12503 | ' permitted but some features (such as "len()") may raise\n' |
| 12504 | ' "OverflowError".\n' |
| 12505 | '\n' |
| 12506 | ' Range examples:\n' |
| 12507 | '\n' |
| 12508 | ' >>> list(range(10))\n' |
| 12509 | ' [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n' |
| 12510 | ' >>> list(range(1, 11))\n' |
| 12511 | ' [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n' |
| 12512 | ' >>> list(range(0, 30, 5))\n' |
| 12513 | ' [0, 5, 10, 15, 20, 25]\n' |
| 12514 | ' >>> list(range(0, 10, 3))\n' |
| 12515 | ' [0, 3, 6, 9]\n' |
| 12516 | ' >>> list(range(0, -10, -1))\n' |
| 12517 | ' [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]\n' |
| 12518 | ' >>> list(range(0))\n' |
| 12519 | ' []\n' |
| 12520 | ' >>> list(range(1, 0))\n' |
| 12521 | ' []\n' |
| 12522 | '\n' |
| 12523 | ' Ranges implement all of the *common* sequence operations ' |
| 12524 | 'except\n' |
| 12525 | ' concatenation and repetition (due to the fact that range ' |
| 12526 | 'objects\n' |
| 12527 | ' can only represent sequences that follow a strict pattern ' |
| 12528 | 'and\n' |
| 12529 | ' repetition and concatenation will usually violate that ' |
| 12530 | 'pattern).\n' |
| 12531 | '\n' |
| 12532 | 'The advantage of the "range" type over a regular "list" or ' |
| 12533 | '"tuple" is\n' |
| 12534 | 'that a "range" object will always take the same (small) amount ' |
| 12535 | 'of\n' |
| 12536 | 'memory, no matter the size of the range it represents (as it ' |
| 12537 | 'only\n' |
| 12538 | 'stores the "start", "stop" and "step" values, calculating ' |
| 12539 | 'individual\n' |
| 12540 | 'items and subranges as needed).\n' |
| 12541 | '\n' |
| 12542 | 'Range objects implement the "collections.abc.Sequence" ABC, ' |
| 12543 | 'and\n' |
| 12544 | 'provide features such as containment tests, element index ' |
| 12545 | 'lookup,\n' |
| 12546 | 'slicing and support for negative indices (see *Sequence Types ' |
| 12547 | '---\n' |
| 12548 | 'list, tuple, range*):\n' |
| 12549 | '\n' |
| 12550 | '>>> r = range(0, 20, 2)\n' |
| 12551 | '>>> r\n' |
| 12552 | 'range(0, 20, 2)\n' |
| 12553 | '>>> 11 in r\n' |
| 12554 | 'False\n' |
| 12555 | '>>> 10 in r\n' |
| 12556 | 'True\n' |
| 12557 | '>>> r.index(10)\n' |
| 12558 | '5\n' |
| 12559 | '>>> r[5]\n' |
| 12560 | '10\n' |
| 12561 | '>>> r[:5]\n' |
| 12562 | 'range(0, 10, 2)\n' |
| 12563 | '>>> r[-1]\n' |
| 12564 | '18\n' |
| 12565 | '\n' |
| 12566 | 'Testing range objects for equality with "==" and "!=" compares ' |
| 12567 | 'them as\n' |
| 12568 | 'sequences. That is, two range objects are considered equal if ' |
| 12569 | 'they\n' |
| 12570 | 'represent the same sequence of values. (Note that two range ' |
| 12571 | 'objects\n' |
| 12572 | 'that compare equal might have different "start", "stop" and ' |
| 12573 | '"step"\n' |
| 12574 | 'attributes, for example "range(0) == range(2, 1, 3)" or ' |
| 12575 | '"range(0, 3,\n' |
| 12576 | '2) == range(0, 4, 2)".)\n' |
| 12577 | '\n' |
| 12578 | 'Changed in version 3.2: Implement the Sequence ABC. Support ' |
| 12579 | 'slicing\n' |
| 12580 | 'and negative indices. Test "int" objects for membership in ' |
| 12581 | 'constant\n' |
| 12582 | 'time instead of iterating through all items.\n' |
| 12583 | '\n' |
| 12584 | "Changed in version 3.3: Define '==' and '!=' to compare range " |
| 12585 | 'objects\n' |
| 12586 | 'based on the sequence of values they define (instead of ' |
| 12587 | 'comparing\n' |
| 12588 | 'based on object identity).\n' |
| 12589 | '\n' |
| 12590 | 'New in version 3.3: The "start", "stop" and "step" ' |
| 12591 | 'attributes.\n', |
| 12592 | 'typesseq-mutable': '\n' |
| 12593 | 'Mutable Sequence Types\n' |
| 12594 | '**********************\n' |
| 12595 | '\n' |
| 12596 | 'The operations in the following table are defined on ' |
| 12597 | 'mutable sequence\n' |
| 12598 | 'types. The "collections.abc.MutableSequence" ABC is ' |
| 12599 | 'provided to make\n' |
| 12600 | 'it easier to correctly implement these operations on ' |
| 12601 | 'custom sequence\n' |
| 12602 | 'types.\n' |
| 12603 | '\n' |
| 12604 | 'In the table *s* is an instance of a mutable sequence ' |
| 12605 | 'type, *t* is any\n' |
| 12606 | 'iterable object and *x* is an arbitrary object that ' |
| 12607 | 'meets any type and\n' |
| 12608 | 'value restrictions imposed by *s* (for example, ' |
| 12609 | '"bytearray" only\n' |
| 12610 | 'accepts integers that meet the value restriction "0 <= ' |
| 12611 | 'x <= 255").\n' |
| 12612 | '\n' |
| 12613 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12614 | '| Operation | ' |
| 12615 | 'Result | ' |
| 12616 | 'Notes |\n' |
| 12617 | '+================================+==================================+=======================+\n' |
| 12618 | '| "s[i] = x" | item *i* of *s* is ' |
| 12619 | 'replaced by | |\n' |
| 12620 | '| | ' |
| 12621 | '*x* ' |
| 12622 | '| |\n' |
| 12623 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12624 | '| "s[i:j] = t" | slice of *s* from ' |
| 12625 | '*i* to *j* is | |\n' |
| 12626 | '| | replaced by the ' |
| 12627 | 'contents of the | |\n' |
| 12628 | '| | iterable ' |
| 12629 | '*t* | |\n' |
| 12630 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12631 | '| "del s[i:j]" | same as "s[i:j] = ' |
| 12632 | '[]" | |\n' |
| 12633 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12634 | '| "s[i:j:k] = t" | the elements of ' |
| 12635 | '"s[i:j:k]" are | (1) |\n' |
| 12636 | '| | replaced by those ' |
| 12637 | 'of *t* | |\n' |
| 12638 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12639 | '| "del s[i:j:k]" | removes the ' |
| 12640 | 'elements of | |\n' |
| 12641 | '| | "s[i:j:k]" from the ' |
| 12642 | 'list | |\n' |
| 12643 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12644 | '| "s.append(x)" | appends *x* to the ' |
| 12645 | 'end of the | |\n' |
| 12646 | '| | sequence (same ' |
| 12647 | 'as | |\n' |
| 12648 | '| | "s[len(s):len(s)] = ' |
| 12649 | '[x]") | |\n' |
| 12650 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12651 | '| "s.clear()" | removes all items ' |
| 12652 | 'from "s" (same | (5) |\n' |
| 12653 | '| | as "del ' |
| 12654 | 's[:]") | |\n' |
| 12655 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12656 | '| "s.copy()" | creates a shallow ' |
| 12657 | 'copy of "s" | (5) |\n' |
| 12658 | '| | (same as ' |
| 12659 | '"s[:]") | |\n' |
| 12660 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12661 | '| "s.extend(t)" | extends *s* with ' |
| 12662 | 'the contents of | |\n' |
| 12663 | '| | *t* (same as ' |
| 12664 | '"s[len(s):len(s)] = | |\n' |
| 12665 | '| | ' |
| 12666 | 't") ' |
| 12667 | '| |\n' |
| 12668 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12669 | '| "s.insert(i, x)" | inserts *x* into ' |
| 12670 | '*s* at the | |\n' |
| 12671 | '| | index given by *i* ' |
| 12672 | '(same as | |\n' |
| 12673 | '| | "s[i:i] = ' |
| 12674 | '[x]") | |\n' |
| 12675 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12676 | '| "s.pop([i])" | retrieves the item ' |
| 12677 | 'at *i* and | (2) |\n' |
| 12678 | '| | also removes it ' |
| 12679 | 'from *s* | |\n' |
| 12680 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12681 | '| "s.remove(x)" | remove the first ' |
| 12682 | 'item from *s* | (3) |\n' |
| 12683 | '| | where "s[i] == ' |
| 12684 | 'x" | |\n' |
| 12685 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12686 | '| "s.reverse()" | reverses the items ' |
| 12687 | 'of *s* in | (4) |\n' |
| 12688 | '| | ' |
| 12689 | 'place ' |
| 12690 | '| |\n' |
| 12691 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12692 | '\n' |
| 12693 | 'Notes:\n' |
| 12694 | '\n' |
| 12695 | '1. *t* must have the same length as the slice it is ' |
| 12696 | 'replacing.\n' |
| 12697 | '\n' |
| 12698 | '2. The optional argument *i* defaults to "-1", so that ' |
| 12699 | 'by default\n' |
| 12700 | ' the last item is removed and returned.\n' |
| 12701 | '\n' |
| 12702 | '3. "remove" raises "ValueError" when *x* is not found ' |
| 12703 | 'in *s*.\n' |
| 12704 | '\n' |
| 12705 | '4. The "reverse()" method modifies the sequence in ' |
| 12706 | 'place for\n' |
| 12707 | ' economy of space when reversing a large sequence. ' |
| 12708 | 'To remind users\n' |
| 12709 | ' that it operates by side effect, it does not return ' |
| 12710 | 'the reversed\n' |
| 12711 | ' sequence.\n' |
| 12712 | '\n' |
| 12713 | '5. "clear()" and "copy()" are included for consistency ' |
| 12714 | 'with the\n' |
| 12715 | " interfaces of mutable containers that don't support " |
| 12716 | 'slicing\n' |
| 12717 | ' operations (such as "dict" and "set")\n' |
| 12718 | '\n' |
| 12719 | ' New in version 3.3: "clear()" and "copy()" ' |
| 12720 | 'methods.\n', |
| 12721 | 'unary': '\n' |
| 12722 | 'Unary arithmetic and bitwise operations\n' |
| 12723 | '***************************************\n' |
| 12724 | '\n' |
| 12725 | 'All unary arithmetic and bitwise operations have the same ' |
| 12726 | 'priority:\n' |
| 12727 | '\n' |
| 12728 | ' u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr\n' |
| 12729 | '\n' |
| 12730 | 'The unary "-" (minus) operator yields the negation of its ' |
| 12731 | 'numeric\n' |
| 12732 | 'argument.\n' |
| 12733 | '\n' |
| 12734 | 'The unary "+" (plus) operator yields its numeric argument ' |
| 12735 | 'unchanged.\n' |
| 12736 | '\n' |
| 12737 | 'The unary "~" (invert) operator yields the bitwise inversion of ' |
| 12738 | 'its\n' |
| 12739 | 'integer argument. The bitwise inversion of "x" is defined as\n' |
| 12740 | '"-(x+1)". It only applies to integral numbers.\n' |
| 12741 | '\n' |
| 12742 | 'In all three cases, if the argument does not have the proper ' |
| 12743 | 'type, a\n' |
| 12744 | '"TypeError" exception is raised.\n', |
| 12745 | 'while': '\n' |
| 12746 | 'The "while" statement\n' |
| 12747 | '*********************\n' |
| 12748 | '\n' |
| 12749 | 'The "while" statement is used for repeated execution as long as ' |
| 12750 | 'an\n' |
| 12751 | 'expression is true:\n' |
| 12752 | '\n' |
| 12753 | ' while_stmt ::= "while" expression ":" suite\n' |
| 12754 | ' ["else" ":" suite]\n' |
| 12755 | '\n' |
| 12756 | 'This repeatedly tests the expression and, if it is true, executes ' |
| 12757 | 'the\n' |
| 12758 | 'first suite; if the expression is false (which may be the first ' |
| 12759 | 'time\n' |
| 12760 | 'it is tested) the suite of the "else" clause, if present, is ' |
| 12761 | 'executed\n' |
| 12762 | 'and the loop terminates.\n' |
| 12763 | '\n' |
| 12764 | 'A "break" statement executed in the first suite terminates the ' |
| 12765 | 'loop\n' |
| 12766 | 'without executing the "else" clause\'s suite. A "continue" ' |
| 12767 | 'statement\n' |
| 12768 | 'executed in the first suite skips the rest of the suite and goes ' |
| 12769 | 'back\n' |
| 12770 | 'to testing the expression.\n', |
| 12771 | 'with': '\n' |
| 12772 | 'The "with" statement\n' |
| 12773 | '********************\n' |
| 12774 | '\n' |
| 12775 | 'The "with" statement is used to wrap the execution of a block ' |
| 12776 | 'with\n' |
| 12777 | 'methods defined by a context manager (see section *With Statement\n' |
| 12778 | 'Context Managers*). This allows common ' |
| 12779 | '"try"..."except"..."finally"\n' |
| 12780 | 'usage patterns to be encapsulated for convenient reuse.\n' |
| 12781 | '\n' |
| 12782 | ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n' |
| 12783 | ' with_item ::= expression ["as" target]\n' |
| 12784 | '\n' |
| 12785 | 'The execution of the "with" statement with one "item" proceeds as\n' |
| 12786 | 'follows:\n' |
| 12787 | '\n' |
| 12788 | '1. The context expression (the expression given in the ' |
| 12789 | '"with_item")\n' |
| 12790 | ' is evaluated to obtain a context manager.\n' |
| 12791 | '\n' |
| 12792 | '2. The context manager\'s "__exit__()" is loaded for later use.\n' |
| 12793 | '\n' |
| 12794 | '3. The context manager\'s "__enter__()" method is invoked.\n' |
| 12795 | '\n' |
| 12796 | '4. If a target was included in the "with" statement, the return\n' |
| 12797 | ' value from "__enter__()" is assigned to it.\n' |
| 12798 | '\n' |
| 12799 | ' Note: The "with" statement guarantees that if the ' |
| 12800 | '"__enter__()"\n' |
| 12801 | ' method returns without an error, then "__exit__()" will ' |
| 12802 | 'always be\n' |
| 12803 | ' called. Thus, if an error occurs during the assignment to ' |
| 12804 | 'the\n' |
| 12805 | ' target list, it will be treated the same as an error ' |
| 12806 | 'occurring\n' |
| 12807 | ' within the suite would be. See step 6 below.\n' |
| 12808 | '\n' |
| 12809 | '5. The suite is executed.\n' |
| 12810 | '\n' |
| 12811 | '6. The context manager\'s "__exit__()" method is invoked. If an\n' |
| 12812 | ' exception caused the suite to be exited, its type, value, and\n' |
| 12813 | ' traceback are passed as arguments to "__exit__()". Otherwise, ' |
| 12814 | 'three\n' |
| 12815 | ' "None" arguments are supplied.\n' |
| 12816 | '\n' |
| 12817 | ' If the suite was exited due to an exception, and the return ' |
| 12818 | 'value\n' |
| 12819 | ' from the "__exit__()" method was false, the exception is ' |
| 12820 | 'reraised.\n' |
| 12821 | ' If the return value was true, the exception is suppressed, and\n' |
| 12822 | ' execution continues with the statement following the "with"\n' |
| 12823 | ' statement.\n' |
| 12824 | '\n' |
| 12825 | ' If the suite was exited for any reason other than an exception, ' |
| 12826 | 'the\n' |
| 12827 | ' return value from "__exit__()" is ignored, and execution ' |
| 12828 | 'proceeds\n' |
| 12829 | ' at the normal location for the kind of exit that was taken.\n' |
| 12830 | '\n' |
| 12831 | 'With more than one item, the context managers are processed as if\n' |
| 12832 | 'multiple "with" statements were nested:\n' |
| 12833 | '\n' |
| 12834 | ' with A() as a, B() as b:\n' |
| 12835 | ' suite\n' |
| 12836 | '\n' |
| 12837 | 'is equivalent to\n' |
| 12838 | '\n' |
| 12839 | ' with A() as a:\n' |
| 12840 | ' with B() as b:\n' |
| 12841 | ' suite\n' |
| 12842 | '\n' |
| 12843 | 'Changed in version 3.1: Support for multiple context expressions.\n' |
| 12844 | '\n' |
| 12845 | 'See also: **PEP 0343** - The "with" statement\n' |
| 12846 | '\n' |
| 12847 | ' The specification, background, and examples for the Python ' |
| 12848 | '"with"\n' |
| 12849 | ' statement.\n', |
| 12850 | 'yield': '\n' |
| 12851 | 'The "yield" statement\n' |
| 12852 | '*********************\n' |
| 12853 | '\n' |
| 12854 | ' yield_stmt ::= yield_expression\n' |
| 12855 | '\n' |
| 12856 | 'A "yield" statement is semantically equivalent to a *yield\n' |
| 12857 | 'expression*. The yield statement can be used to omit the ' |
| 12858 | 'parentheses\n' |
| 12859 | 'that would otherwise be required in the equivalent yield ' |
| 12860 | 'expression\n' |
| 12861 | 'statement. For example, the yield statements\n' |
| 12862 | '\n' |
| 12863 | ' yield <expr>\n' |
| 12864 | ' yield from <expr>\n' |
| 12865 | '\n' |
| 12866 | 'are equivalent to the yield expression statements\n' |
| 12867 | '\n' |
| 12868 | ' (yield <expr>)\n' |
| 12869 | ' (yield from <expr>)\n' |
| 12870 | '\n' |
| 12871 | 'Yield expressions and statements are only used when defining a\n' |
| 12872 | '*generator* function, and are only used in the body of the ' |
| 12873 | 'generator\n' |
| 12874 | 'function. Using yield in a function definition is sufficient to ' |
| 12875 | 'cause\n' |
| 12876 | 'that definition to create a generator function instead of a ' |
| 12877 | 'normal\n' |
| 12878 | 'function.\n' |
| 12879 | '\n' |
| 12880 | 'For full details of "yield" semantics, refer to the *Yield\n' |
| 12881 | 'expressions* section.\n'} |