| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
topics = {'assert': '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" under\n' |
| 28 |
'normal circumstances, "False" when optimization is requested ' |
| 29 |
'(command\n' |
| 30 |
'line option "-O"). The current code generator emits no code for ' |
| 31 |
'an\n' |
| 32 |
'assert statement when optimization is requested at compile time. ' |
| 33 |
'Note\n' |
| 34 |
'that it is unnecessary to include the source code for the ' |
| 35 |
'expression\n' |
| 36 |
'that failed in the error message; it will be displayed as part of ' |
| 37 |
'the\n' |
| 38 |
'stack trace.\n' |
| 39 |
'\n' |
| 40 |
'Assignments to "__debug__" are illegal. The value for the ' |
| 41 |
'built-in\n' |
| 42 |
'variable is determined when the interpreter starts.\n', |
| 43 |
'assignment': 'Assignment statements\n' |
| 44 |
'*********************\n' |
| 45 |
'\n' |
| 46 |
'Assignment statements are used to (re)bind names to values and ' |
| 47 |
'to\n' |
| 48 |
'modify attributes or items of mutable objects:\n' |
| 49 |
'\n' |
| 50 |
' assignment_stmt ::= (target_list "=")+ (starred_expression ' |
| 51 |
'| yield_expression)\n' |
| 52 |
' target_list ::= target ("," target)* [","]\n' |
| 53 |
' target ::= identifier\n' |
| 54 |
' | "(" [target_list] ")"\n' |
| 55 |
' | "[" [target_list] "]"\n' |
| 56 |
' | attributeref\n' |
| 57 |
' | subscription\n' |
| 58 |
' | slicing\n' |
| 59 |
' | "*" target\n' |
| 60 |
'\n' |
| 61 |
'(See section Primaries for the syntax definitions for ' |
| 62 |
'*attributeref*,\n' |
| 63 |
'*subscription*, and *slicing*.)\n' |
| 64 |
'\n' |
| 65 |
'An assignment statement evaluates the expression list ' |
| 66 |
'(remember that\n' |
| 67 |
'this can be a single expression or a comma-separated list, the ' |
| 68 |
'latter\n' |
| 69 |
'yielding a tuple) and assigns the single resulting object to ' |
| 70 |
'each of\n' |
| 71 |
'the target lists, from left to right.\n' |
| 72 |
'\n' |
| 73 |
'Assignment is defined recursively depending on the form of the ' |
| 74 |
'target\n' |
| 75 |
'(list). When a target is part of a mutable object (an ' |
| 76 |
'attribute\n' |
| 77 |
'reference, subscription or slicing), the mutable object must\n' |
| 78 |
'ultimately perform the assignment and decide about its ' |
| 79 |
'validity, and\n' |
| 80 |
'may raise an exception if the assignment is unacceptable. The ' |
| 81 |
'rules\n' |
| 82 |
'observed by various types and the exceptions raised are given ' |
| 83 |
'with the\n' |
| 84 |
'definition of the object types (see section The standard type\n' |
| 85 |
'hierarchy).\n' |
| 86 |
'\n' |
| 87 |
'Assignment of an object to a target list, optionally enclosed ' |
| 88 |
'in\n' |
| 89 |
'parentheses or square brackets, is recursively defined as ' |
| 90 |
'follows.\n' |
| 91 |
'\n' |
| 92 |
'* If the target list is a single target with no trailing ' |
| 93 |
'comma,\n' |
| 94 |
' optionally in parentheses, the object is assigned to that ' |
| 95 |
'target.\n' |
| 96 |
'\n' |
| 97 |
'* Else:\n' |
| 98 |
'\n' |
| 99 |
' * If the target list contains one target prefixed with an ' |
| 100 |
'asterisk,\n' |
| 101 |
' called a “starred” target: The object must be an iterable ' |
| 102 |
'with at\n' |
| 103 |
' least as many items as there are targets in the target ' |
| 104 |
'list, minus\n' |
| 105 |
' one. The first items of the iterable are assigned, from ' |
| 106 |
'left to\n' |
| 107 |
' right, to the targets before the starred target. The ' |
| 108 |
'final items\n' |
| 109 |
' of the iterable are assigned to the targets after the ' |
| 110 |
'starred\n' |
| 111 |
' target. A list of the remaining items in the iterable is ' |
| 112 |
'then\n' |
| 113 |
' assigned to the starred target (the list can be empty).\n' |
| 114 |
'\n' |
| 115 |
' * Else: The object must be an iterable with the same number ' |
| 116 |
'of items\n' |
| 117 |
' as there are targets in the target list, and the items ' |
| 118 |
'are\n' |
| 119 |
' assigned, from left to right, to the corresponding ' |
| 120 |
'targets.\n' |
| 121 |
'\n' |
| 122 |
'Assignment of an object to a single target is recursively ' |
| 123 |
'defined as\n' |
| 124 |
'follows.\n' |
| 125 |
'\n' |
| 126 |
'* If the target is an identifier (name):\n' |
| 127 |
'\n' |
| 128 |
' * If the name does not occur in a "global" or "nonlocal" ' |
| 129 |
'statement\n' |
| 130 |
' in the current code block: the name is bound to the object ' |
| 131 |
'in the\n' |
| 132 |
' current local namespace.\n' |
| 133 |
'\n' |
| 134 |
' * Otherwise: the name is bound to the object in the global ' |
| 135 |
'namespace\n' |
| 136 |
' or the outer namespace determined by "nonlocal", ' |
| 137 |
'respectively.\n' |
| 138 |
'\n' |
| 139 |
' The name is rebound if it was already bound. This may cause ' |
| 140 |
'the\n' |
| 141 |
' reference count for the object previously bound to the name ' |
| 142 |
'to reach\n' |
| 143 |
' zero, causing the object to be deallocated and its ' |
| 144 |
'destructor (if it\n' |
| 145 |
' has one) to be called.\n' |
| 146 |
'\n' |
| 147 |
'* If the target is an attribute reference: The primary ' |
| 148 |
'expression in\n' |
| 149 |
' the reference is evaluated. It should yield an object with\n' |
| 150 |
' assignable attributes; if this is not the case, "TypeError" ' |
| 151 |
'is\n' |
| 152 |
' raised. That object is then asked to assign the assigned ' |
| 153 |
'object to\n' |
| 154 |
' the given attribute; if it cannot perform the assignment, it ' |
| 155 |
'raises\n' |
| 156 |
' an exception (usually but not necessarily ' |
| 157 |
'"AttributeError").\n' |
| 158 |
'\n' |
| 159 |
' Note: If the object is a class instance and the attribute ' |
| 160 |
'reference\n' |
| 161 |
' occurs on both sides of the assignment operator, the ' |
| 162 |
'right-hand side\n' |
| 163 |
' expression, "a.x" can access either an instance attribute or ' |
| 164 |
'(if no\n' |
| 165 |
' instance attribute exists) a class attribute. The left-hand ' |
| 166 |
'side\n' |
| 167 |
' target "a.x" is always set as an instance attribute, ' |
| 168 |
'creating it if\n' |
| 169 |
' necessary. Thus, the two occurrences of "a.x" do not ' |
| 170 |
'necessarily\n' |
| 171 |
' refer to the same attribute: if the right-hand side ' |
| 172 |
'expression\n' |
| 173 |
' refers to a class attribute, the left-hand side creates a ' |
| 174 |
'new\n' |
| 175 |
' instance attribute as the target of the assignment:\n' |
| 176 |
'\n' |
| 177 |
' class Cls:\n' |
| 178 |
' x = 3 # class variable\n' |
| 179 |
' inst = Cls()\n' |
| 180 |
' inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x ' |
| 181 |
'as 3\n' |
| 182 |
'\n' |
| 183 |
' This description does not necessarily apply to descriptor\n' |
| 184 |
' attributes, such as properties created with "property()".\n' |
| 185 |
'\n' |
| 186 |
'* If the target is a subscription: The primary expression in ' |
| 187 |
'the\n' |
| 188 |
' reference is evaluated. It should yield either a mutable ' |
| 189 |
'sequence\n' |
| 190 |
' object (such as a list) or a mapping object (such as a ' |
| 191 |
'dictionary).\n' |
| 192 |
' Next, the subscript expression is evaluated.\n' |
| 193 |
'\n' |
| 194 |
' If the primary is a mutable sequence object (such as a ' |
| 195 |
'list), the\n' |
| 196 |
' subscript must yield an integer. If it is negative, the ' |
| 197 |
'sequence’s\n' |
| 198 |
' length is added to it. The resulting value must be a ' |
| 199 |
'nonnegative\n' |
| 200 |
' integer less than the sequence’s length, and the sequence is ' |
| 201 |
'asked\n' |
| 202 |
' to assign the assigned object to its item with that index. ' |
| 203 |
'If the\n' |
| 204 |
' index is out of range, "IndexError" is raised (assignment to ' |
| 205 |
'a\n' |
| 206 |
' subscripted sequence cannot add new items to a list).\n' |
| 207 |
'\n' |
| 208 |
' If the primary is a mapping object (such as a dictionary), ' |
| 209 |
'the\n' |
| 210 |
' subscript must have a type compatible with the mapping’s key ' |
| 211 |
'type,\n' |
| 212 |
' and the mapping is then asked to create a key/value pair ' |
| 213 |
'which maps\n' |
| 214 |
' the subscript to the assigned object. This can either ' |
| 215 |
'replace an\n' |
| 216 |
' existing key/value pair with the same key value, or insert a ' |
| 217 |
'new\n' |
| 218 |
' key/value pair (if no key with the same value existed).\n' |
| 219 |
'\n' |
| 220 |
' For user-defined objects, the "__setitem__()" method is ' |
| 221 |
'called with\n' |
| 222 |
' appropriate arguments.\n' |
| 223 |
'\n' |
| 224 |
'* If the target is a slicing: The primary expression in the ' |
| 225 |
'reference\n' |
| 226 |
' is evaluated. It should yield a mutable sequence object ' |
| 227 |
'(such as a\n' |
| 228 |
' list). The assigned object should be a sequence object of ' |
| 229 |
'the same\n' |
| 230 |
' type. Next, the lower and upper bound expressions are ' |
| 231 |
'evaluated,\n' |
| 232 |
' insofar they are present; defaults are zero and the ' |
| 233 |
'sequence’s\n' |
| 234 |
' length. The bounds should evaluate to integers. If either ' |
| 235 |
'bound is\n' |
| 236 |
' negative, the sequence’s length is added to it. The ' |
| 237 |
'resulting\n' |
| 238 |
' bounds are clipped to lie between zero and the sequence’s ' |
| 239 |
'length,\n' |
| 240 |
' inclusive. Finally, the sequence object is asked to replace ' |
| 241 |
'the\n' |
| 242 |
' slice with the items of the assigned sequence. The length ' |
| 243 |
'of the\n' |
| 244 |
' slice may be different from the length of the assigned ' |
| 245 |
'sequence,\n' |
| 246 |
' thus changing the length of the target sequence, if the ' |
| 247 |
'target\n' |
| 248 |
' sequence allows it.\n' |
| 249 |
'\n' |
| 250 |
'**CPython implementation detail:** In the current ' |
| 251 |
'implementation, the\n' |
| 252 |
'syntax for targets is taken to be the same as for expressions, ' |
| 253 |
'and\n' |
| 254 |
'invalid syntax is rejected during the code generation phase, ' |
| 255 |
'causing\n' |
| 256 |
'less detailed error messages.\n' |
| 257 |
'\n' |
| 258 |
'Although the definition of assignment implies that overlaps ' |
| 259 |
'between\n' |
| 260 |
'the left-hand side and the right-hand side are ‘simultaneous’ ' |
| 261 |
'(for\n' |
| 262 |
'example "a, b = b, a" swaps two variables), overlaps *within* ' |
| 263 |
'the\n' |
| 264 |
'collection of assigned-to variables occur left-to-right, ' |
| 265 |
'sometimes\n' |
| 266 |
'resulting in confusion. For instance, the following program ' |
| 267 |
'prints\n' |
| 268 |
'"[0, 2]":\n' |
| 269 |
'\n' |
| 270 |
' x = [0, 1]\n' |
| 271 |
' i = 0\n' |
| 272 |
' i, x[i] = 1, 2 # i is updated, then x[i] is ' |
| 273 |
'updated\n' |
| 274 |
' print(x)\n' |
| 275 |
'\n' |
| 276 |
'See also:\n' |
| 277 |
'\n' |
| 278 |
' **PEP 3132** - Extended Iterable Unpacking\n' |
| 279 |
' The specification for the "*target" feature.\n' |
| 280 |
'\n' |
| 281 |
'\n' |
| 282 |
'Augmented assignment statements\n' |
| 283 |
'===============================\n' |
| 284 |
'\n' |
| 285 |
'Augmented assignment is the combination, in a single ' |
| 286 |
'statement, of a\n' |
| 287 |
'binary operation and an assignment statement:\n' |
| 288 |
'\n' |
| 289 |
' augmented_assignment_stmt ::= augtarget augop ' |
| 290 |
'(expression_list | yield_expression)\n' |
| 291 |
' augtarget ::= identifier | attributeref | ' |
| 292 |
'subscription | slicing\n' |
| 293 |
' augop ::= "+=" | "-=" | "*=" | "@=" | ' |
| 294 |
'"/=" | "//=" | "%=" | "**="\n' |
| 295 |
' | ">>=" | "<<=" | "&=" | "^=" | "|="\n' |
| 296 |
'\n' |
| 297 |
'(See section Primaries for the syntax definitions of the last ' |
| 298 |
'three\n' |
| 299 |
'symbols.)\n' |
| 300 |
'\n' |
| 301 |
'An augmented assignment evaluates the target (which, unlike ' |
| 302 |
'normal\n' |
| 303 |
'assignment statements, cannot be an unpacking) and the ' |
| 304 |
'expression\n' |
| 305 |
'list, performs the binary operation specific to the type of ' |
| 306 |
'assignment\n' |
| 307 |
'on the two operands, and assigns the result to the original ' |
| 308 |
'target.\n' |
| 309 |
'The target is only evaluated once.\n' |
| 310 |
'\n' |
| 311 |
'An augmented assignment expression like "x += 1" can be ' |
| 312 |
'rewritten as\n' |
| 313 |
'"x = x + 1" to achieve a similar, but not exactly equal ' |
| 314 |
'effect. In the\n' |
| 315 |
'augmented version, "x" is only evaluated once. Also, when ' |
| 316 |
'possible,\n' |
| 317 |
'the actual operation is performed *in-place*, meaning that ' |
| 318 |
'rather than\n' |
| 319 |
'creating a new object and assigning that to the target, the ' |
| 320 |
'old object\n' |
| 321 |
'is modified instead.\n' |
| 322 |
'\n' |
| 323 |
'Unlike normal assignments, augmented assignments evaluate the ' |
| 324 |
'left-\n' |
| 325 |
'hand side *before* evaluating the right-hand side. For ' |
| 326 |
'example, "a[i]\n' |
| 327 |
'+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and ' |
| 328 |
'performs\n' |
| 329 |
'the addition, and lastly, it writes the result back to ' |
| 330 |
'"a[i]".\n' |
| 331 |
'\n' |
| 332 |
'With the exception of assigning to tuples and multiple targets ' |
| 333 |
'in a\n' |
| 334 |
'single statement, the assignment done by augmented assignment\n' |
| 335 |
'statements is handled the same way as normal assignments. ' |
| 336 |
'Similarly,\n' |
| 337 |
'with the exception of the possible *in-place* behavior, the ' |
| 338 |
'binary\n' |
| 339 |
'operation performed by augmented assignment is the same as the ' |
| 340 |
'normal\n' |
| 341 |
'binary operations.\n' |
| 342 |
'\n' |
| 343 |
'For targets which are attribute references, the same caveat ' |
| 344 |
'about\n' |
| 345 |
'class and instance attributes applies as for regular ' |
| 346 |
'assignments.\n' |
| 347 |
'\n' |
| 348 |
'\n' |
| 349 |
'Annotated assignment statements\n' |
| 350 |
'===============================\n' |
| 351 |
'\n' |
| 352 |
'*Annotation* assignment is the combination, in a single ' |
| 353 |
'statement, of\n' |
| 354 |
'a variable or attribute annotation and an optional assignment\n' |
| 355 |
'statement:\n' |
| 356 |
'\n' |
| 357 |
' annotated_assignment_stmt ::= augtarget ":" expression\n' |
| 358 |
' ["=" (starred_expression | ' |
| 359 |
'yield_expression)]\n' |
| 360 |
'\n' |
| 361 |
'The difference from normal Assignment statements is that only ' |
| 362 |
'a single\n' |
| 363 |
'target is allowed.\n' |
| 364 |
'\n' |
| 365 |
'For simple names as assignment targets, if in class or module ' |
| 366 |
'scope,\n' |
| 367 |
'the annotations are evaluated and stored in a special class or ' |
| 368 |
'module\n' |
| 369 |
'attribute "__annotations__" that is a dictionary mapping from ' |
| 370 |
'variable\n' |
| 371 |
'names (mangled if private) to evaluated annotations. This ' |
| 372 |
'attribute is\n' |
| 373 |
'writable and is automatically created at the start of class or ' |
| 374 |
'module\n' |
| 375 |
'body execution, if annotations are found statically.\n' |
| 376 |
'\n' |
| 377 |
'For expressions as assignment targets, the annotations are ' |
| 378 |
'evaluated\n' |
| 379 |
'if in class or module scope, but not stored.\n' |
| 380 |
'\n' |
| 381 |
'If a name is annotated in a function scope, then this name is ' |
| 382 |
'local\n' |
| 383 |
'for that scope. Annotations are never evaluated and stored in ' |
| 384 |
'function\n' |
| 385 |
'scopes.\n' |
| 386 |
'\n' |
| 387 |
'If the right hand side is present, an annotated assignment ' |
| 388 |
'performs\n' |
| 389 |
'the actual assignment before evaluating annotations (where\n' |
| 390 |
'applicable). If the right hand side is not present for an ' |
| 391 |
'expression\n' |
| 392 |
'target, then the interpreter evaluates the target except for ' |
| 393 |
'the last\n' |
| 394 |
'"__setitem__()" or "__setattr__()" call.\n' |
| 395 |
'\n' |
| 396 |
'See also:\n' |
| 397 |
'\n' |
| 398 |
' **PEP 526** - Syntax for Variable Annotations\n' |
| 399 |
' The proposal that added syntax for annotating the types ' |
| 400 |
'of\n' |
| 401 |
' variables (including class variables and instance ' |
| 402 |
'variables),\n' |
| 403 |
' instead of expressing them through comments.\n' |
| 404 |
'\n' |
| 405 |
' **PEP 484** - Type hints\n' |
| 406 |
' The proposal that added the "typing" module to provide a ' |
| 407 |
'standard\n' |
| 408 |
' syntax for type annotations that can be used in static ' |
| 409 |
'analysis\n' |
| 410 |
' tools and IDEs.\n' |
| 411 |
'\n' |
| 412 |
'Changed in version 3.8: Now annotated assignments allow the ' |
| 413 |
'same\n' |
| 414 |
'expressions in the right hand side as regular assignments. ' |
| 415 |
'Previously,\n' |
| 416 |
'some expressions (like un-parenthesized tuple expressions) ' |
| 417 |
'caused a\n' |
| 418 |
'syntax error.\n', |
| 419 |
'async': 'Coroutines\n' |
| 420 |
'**********\n' |
| 421 |
'\n' |
| 422 |
'New in version 3.5.\n' |
| 423 |
'\n' |
| 424 |
'\n' |
| 425 |
'Coroutine function definition\n' |
| 426 |
'=============================\n' |
| 427 |
'\n' |
| 428 |
' async_funcdef ::= [decorators] "async" "def" funcname "(" ' |
| 429 |
'[parameter_list] ")"\n' |
| 430 |
' ["->" expression] ":" suite\n' |
| 431 |
'\n' |
| 432 |
'Execution of Python coroutines can be suspended and resumed at ' |
| 433 |
'many\n' |
| 434 |
'points (see *coroutine*). "await" expressions, "async for" and ' |
| 435 |
'"async\n' |
| 436 |
'with" can only be used in the body of a coroutine function.\n' |
| 437 |
'\n' |
| 438 |
'Functions defined with "async def" syntax are always coroutine\n' |
| 439 |
'functions, even if they do not contain "await" or "async" ' |
| 440 |
'keywords.\n' |
| 441 |
'\n' |
| 442 |
'It is a "SyntaxError" to use a "yield from" expression inside the ' |
| 443 |
'body\n' |
| 444 |
'of a coroutine function.\n' |
| 445 |
'\n' |
| 446 |
'An example of a coroutine function:\n' |
| 447 |
'\n' |
| 448 |
' async def func(param1, param2):\n' |
| 449 |
' do_stuff()\n' |
| 450 |
' await some_coroutine()\n' |
| 451 |
'\n' |
| 452 |
'Changed in version 3.7: "await" and "async" are now keywords;\n' |
| 453 |
'previously they were only treated as such inside the body of a\n' |
| 454 |
'coroutine function.\n' |
| 455 |
'\n' |
| 456 |
'\n' |
| 457 |
'The "async for" statement\n' |
| 458 |
'=========================\n' |
| 459 |
'\n' |
| 460 |
' async_for_stmt ::= "async" for_stmt\n' |
| 461 |
'\n' |
| 462 |
'An *asynchronous iterable* provides an "__aiter__" method that\n' |
| 463 |
'directly returns an *asynchronous iterator*, which can call\n' |
| 464 |
'asynchronous code in its "__anext__" method.\n' |
| 465 |
'\n' |
| 466 |
'The "async for" statement allows convenient iteration over\n' |
| 467 |
'asynchronous iterables.\n' |
| 468 |
'\n' |
| 469 |
'The following code:\n' |
| 470 |
'\n' |
| 471 |
' async for TARGET in ITER:\n' |
| 472 |
' SUITE\n' |
| 473 |
' else:\n' |
| 474 |
' SUITE2\n' |
| 475 |
'\n' |
| 476 |
'Is semantically equivalent to:\n' |
| 477 |
'\n' |
| 478 |
' iter = (ITER)\n' |
| 479 |
' iter = type(iter).__aiter__(iter)\n' |
| 480 |
' running = True\n' |
| 481 |
'\n' |
| 482 |
' while running:\n' |
| 483 |
' try:\n' |
| 484 |
' TARGET = await type(iter).__anext__(iter)\n' |
| 485 |
' except StopAsyncIteration:\n' |
| 486 |
' running = False\n' |
| 487 |
' else:\n' |
| 488 |
' SUITE\n' |
| 489 |
' else:\n' |
| 490 |
' SUITE2\n' |
| 491 |
'\n' |
| 492 |
'See also "__aiter__()" and "__anext__()" for details.\n' |
| 493 |
'\n' |
| 494 |
'It is a "SyntaxError" to use an "async for" statement outside the ' |
| 495 |
'body\n' |
| 496 |
'of a coroutine function.\n' |
| 497 |
'\n' |
| 498 |
'\n' |
| 499 |
'The "async with" statement\n' |
| 500 |
'==========================\n' |
| 501 |
'\n' |
| 502 |
' async_with_stmt ::= "async" with_stmt\n' |
| 503 |
'\n' |
| 504 |
'An *asynchronous context manager* is a *context manager* that is ' |
| 505 |
'able\n' |
| 506 |
'to suspend execution in its *enter* and *exit* methods.\n' |
| 507 |
'\n' |
| 508 |
'The following code:\n' |
| 509 |
'\n' |
| 510 |
' async with EXPRESSION as TARGET:\n' |
| 511 |
' SUITE\n' |
| 512 |
'\n' |
| 513 |
'is semantically equivalent to:\n' |
| 514 |
'\n' |
| 515 |
' manager = (EXPRESSION)\n' |
| 516 |
' aenter = type(manager).__aenter__\n' |
| 517 |
' aexit = type(manager).__aexit__\n' |
| 518 |
' value = await aenter(manager)\n' |
| 519 |
' hit_except = False\n' |
| 520 |
'\n' |
| 521 |
' try:\n' |
| 522 |
' TARGET = value\n' |
| 523 |
' SUITE\n' |
| 524 |
' except:\n' |
| 525 |
' hit_except = True\n' |
| 526 |
' if not await aexit(manager, *sys.exc_info()):\n' |
| 527 |
' raise\n' |
| 528 |
' finally:\n' |
| 529 |
' if not hit_except:\n' |
| 530 |
' await aexit(manager, None, None, None)\n' |
| 531 |
'\n' |
| 532 |
'See also "__aenter__()" and "__aexit__()" for details.\n' |
| 533 |
'\n' |
| 534 |
'It is a "SyntaxError" to use an "async with" statement outside the\n' |
| 535 |
'body of a coroutine function.\n' |
| 536 |
'\n' |
| 537 |
'See also:\n' |
| 538 |
'\n' |
| 539 |
' **PEP 492** - Coroutines with async and await syntax\n' |
| 540 |
' The proposal that made coroutines a proper standalone concept ' |
| 541 |
'in\n' |
| 542 |
' Python, and added supporting syntax.\n' |
| 543 |
'\n' |
| 544 |
'-[ Footnotes ]-\n' |
| 545 |
'\n' |
| 546 |
'[1] The exception is propagated to the invocation stack unless ' |
| 547 |
'there\n' |
| 548 |
' is a "finally" clause which happens to raise another ' |
| 549 |
'exception.\n' |
| 550 |
' That new exception causes the old one to be lost.\n' |
| 551 |
'\n' |
| 552 |
'[2] In pattern matching, a sequence is defined as one of the\n' |
| 553 |
' following:\n' |
| 554 |
'\n' |
| 555 |
' * a class that inherits from "collections.abc.Sequence"\n' |
| 556 |
'\n' |
| 557 |
' * a Python class that has been registered as\n' |
| 558 |
' "collections.abc.Sequence"\n' |
| 559 |
'\n' |
| 560 |
' * a builtin class that has its (CPython) "Py_TPFLAGS_SEQUENCE" ' |
| 561 |
'bit\n' |
| 562 |
' set\n' |
| 563 |
'\n' |
| 564 |
' * a class that inherits from any of the above\n' |
| 565 |
'\n' |
| 566 |
' The following standard library classes are sequences:\n' |
| 567 |
'\n' |
| 568 |
' * "array.array"\n' |
| 569 |
'\n' |
| 570 |
' * "collections.deque"\n' |
| 571 |
'\n' |
| 572 |
' * "list"\n' |
| 573 |
'\n' |
| 574 |
' * "memoryview"\n' |
| 575 |
'\n' |
| 576 |
' * "range"\n' |
| 577 |
'\n' |
| 578 |
' * "tuple"\n' |
| 579 |
'\n' |
| 580 |
' Note:\n' |
| 581 |
'\n' |
| 582 |
' Subject values of type "str", "bytes", and "bytearray" do ' |
| 583 |
'not\n' |
| 584 |
' match sequence patterns.\n' |
| 585 |
'\n' |
| 586 |
'[3] In pattern matching, a mapping is defined as one of the ' |
| 587 |
'following:\n' |
| 588 |
'\n' |
| 589 |
' * a class that inherits from "collections.abc.Mapping"\n' |
| 590 |
'\n' |
| 591 |
' * a Python class that has been registered as\n' |
| 592 |
' "collections.abc.Mapping"\n' |
| 593 |
'\n' |
| 594 |
' * a builtin class that has its (CPython) "Py_TPFLAGS_MAPPING" ' |
| 595 |
'bit\n' |
| 596 |
' set\n' |
| 597 |
'\n' |
| 598 |
' * a class that inherits from any of the above\n' |
| 599 |
'\n' |
| 600 |
' The standard library classes "dict" and ' |
| 601 |
'"types.MappingProxyType"\n' |
| 602 |
' are mappings.\n' |
| 603 |
'\n' |
| 604 |
'[4] A string literal appearing as the first statement in the ' |
| 605 |
'function\n' |
| 606 |
' body is transformed into the function’s "__doc__" attribute ' |
| 607 |
'and\n' |
| 608 |
' therefore the function’s *docstring*.\n' |
| 609 |
'\n' |
| 610 |
'[5] A string literal appearing as the first statement in the class\n' |
| 611 |
' body is transformed into the namespace’s "__doc__" item and\n' |
| 612 |
' therefore the class’s *docstring*.\n', |
| 613 |
'atom-identifiers': 'Identifiers (Names)\n' |
| 614 |
'*******************\n' |
| 615 |
'\n' |
| 616 |
'An identifier occurring as an atom is a name. See ' |
| 617 |
'section Identifiers\n' |
| 618 |
'and keywords for lexical definition and section Naming ' |
| 619 |
'and binding for\n' |
| 620 |
'documentation of naming and binding.\n' |
| 621 |
'\n' |
| 622 |
'When the name is bound to an object, evaluation of the ' |
| 623 |
'atom yields\n' |
| 624 |
'that object. When a name is not bound, an attempt to ' |
| 625 |
'evaluate it\n' |
| 626 |
'raises a "NameError" exception.\n' |
| 627 |
'\n' |
| 628 |
'**Private name mangling:** When an identifier that ' |
| 629 |
'textually occurs in\n' |
| 630 |
'a class definition begins with two or more underscore ' |
| 631 |
'characters and\n' |
| 632 |
'does not end in two or more underscores, it is ' |
| 633 |
'considered a *private\n' |
| 634 |
'name* of that class. Private names are transformed to a ' |
| 635 |
'longer form\n' |
| 636 |
'before code is generated for them. The transformation ' |
| 637 |
'inserts the\n' |
| 638 |
'class name, with leading underscores removed and a ' |
| 639 |
'single underscore\n' |
| 640 |
'inserted, in front of the name. For example, the ' |
| 641 |
'identifier "__spam"\n' |
| 642 |
'occurring in a class named "Ham" will be transformed to ' |
| 643 |
'"_Ham__spam".\n' |
| 644 |
'This transformation is independent of the syntactical ' |
| 645 |
'context in which\n' |
| 646 |
'the identifier is used. If the transformed name is ' |
| 647 |
'extremely long\n' |
| 648 |
'(longer than 255 characters), implementation defined ' |
| 649 |
'truncation may\n' |
| 650 |
'happen. If the class name consists only of underscores, ' |
| 651 |
'no\n' |
| 652 |
'transformation is done.\n', |
| 653 |
'atom-literals': 'Literals\n' |
| 654 |
'********\n' |
| 655 |
'\n' |
| 656 |
'Python supports string and bytes literals and various ' |
| 657 |
'numeric\n' |
| 658 |
'literals:\n' |
| 659 |
'\n' |
| 660 |
' literal ::= stringliteral | bytesliteral\n' |
| 661 |
' | integer | floatnumber | imagnumber\n' |
| 662 |
'\n' |
| 663 |
'Evaluation of a literal yields an object of the given type ' |
| 664 |
'(string,\n' |
| 665 |
'bytes, integer, floating point number, complex number) with ' |
| 666 |
'the given\n' |
| 667 |
'value. The value may be approximated in the case of ' |
| 668 |
'floating point\n' |
| 669 |
'and imaginary (complex) literals. See section Literals for ' |
| 670 |
'details.\n' |
| 671 |
'\n' |
| 672 |
'All literals correspond to immutable data types, and hence ' |
| 673 |
'the\n' |
| 674 |
'object’s identity is less important than its value. ' |
| 675 |
'Multiple\n' |
| 676 |
'evaluations of literals with the same value (either the ' |
| 677 |
'same\n' |
| 678 |
'occurrence in the program text or a different occurrence) ' |
| 679 |
'may obtain\n' |
| 680 |
'the same object or a different object with the same ' |
| 681 |
'value.\n', |
| 682 |
'attribute-access': 'Customizing attribute access\n' |
| 683 |
'****************************\n' |
| 684 |
'\n' |
| 685 |
'The following methods can be defined to customize the ' |
| 686 |
'meaning of\n' |
| 687 |
'attribute access (use of, assignment to, or deletion of ' |
| 688 |
'"x.name") for\n' |
| 689 |
'class instances.\n' |
| 690 |
'\n' |
| 691 |
'object.__getattr__(self, name)\n' |
| 692 |
'\n' |
| 693 |
' Called when the default attribute access fails with ' |
| 694 |
'an\n' |
| 695 |
' "AttributeError" (either "__getattribute__()" raises ' |
| 696 |
'an\n' |
| 697 |
' "AttributeError" because *name* is not an instance ' |
| 698 |
'attribute or an\n' |
| 699 |
' attribute in the class tree for "self"; or ' |
| 700 |
'"__get__()" of a *name*\n' |
| 701 |
' property raises "AttributeError"). This method ' |
| 702 |
'should either\n' |
| 703 |
' return the (computed) attribute value or raise an ' |
| 704 |
'"AttributeError"\n' |
| 705 |
' exception.\n' |
| 706 |
'\n' |
| 707 |
' Note that if the attribute is found through the ' |
| 708 |
'normal mechanism,\n' |
| 709 |
' "__getattr__()" is not called. (This is an ' |
| 710 |
'intentional asymmetry\n' |
| 711 |
' between "__getattr__()" and "__setattr__()".) This is ' |
| 712 |
'done both for\n' |
| 713 |
' efficiency reasons and because otherwise ' |
| 714 |
'"__getattr__()" would have\n' |
| 715 |
' no way to access other attributes of the instance. ' |
| 716 |
'Note that at\n' |
| 717 |
' least for instance variables, you can fake total ' |
| 718 |
'control by not\n' |
| 719 |
' inserting any values in the instance attribute ' |
| 720 |
'dictionary (but\n' |
| 721 |
' instead inserting them in another object). See the\n' |
| 722 |
' "__getattribute__()" method below for a way to ' |
| 723 |
'actually get total\n' |
| 724 |
' control over attribute access.\n' |
| 725 |
'\n' |
| 726 |
'object.__getattribute__(self, name)\n' |
| 727 |
'\n' |
| 728 |
' Called unconditionally to implement attribute ' |
| 729 |
'accesses for\n' |
| 730 |
' instances of the class. If the class also defines ' |
| 731 |
'"__getattr__()",\n' |
| 732 |
' the latter will not be called unless ' |
| 733 |
'"__getattribute__()" either\n' |
| 734 |
' calls it explicitly or raises an "AttributeError". ' |
| 735 |
'This method\n' |
| 736 |
' should return the (computed) attribute value or raise ' |
| 737 |
'an\n' |
| 738 |
' "AttributeError" exception. In order to avoid ' |
| 739 |
'infinite recursion in\n' |
| 740 |
' this method, its implementation should always call ' |
| 741 |
'the base class\n' |
| 742 |
' method with the same name to access any attributes it ' |
| 743 |
'needs, for\n' |
| 744 |
' example, "object.__getattribute__(self, name)".\n' |
| 745 |
'\n' |
| 746 |
' Note:\n' |
| 747 |
'\n' |
| 748 |
' This method may still be bypassed when looking up ' |
| 749 |
'special methods\n' |
| 750 |
' as the result of implicit invocation via language ' |
| 751 |
'syntax or\n' |
| 752 |
' built-in functions. See Special method lookup.\n' |
| 753 |
'\n' |
| 754 |
' For certain sensitive attribute accesses, raises an ' |
| 755 |
'auditing event\n' |
| 756 |
' "object.__getattr__" with arguments "obj" and ' |
| 757 |
'"name".\n' |
| 758 |
'\n' |
| 759 |
'object.__setattr__(self, name, value)\n' |
| 760 |
'\n' |
| 761 |
' Called when an attribute assignment is attempted. ' |
| 762 |
'This is called\n' |
| 763 |
' instead of the normal mechanism (i.e. store the value ' |
| 764 |
'in the\n' |
| 765 |
' instance dictionary). *name* is the attribute name, ' |
| 766 |
'*value* is the\n' |
| 767 |
' value to be assigned to it.\n' |
| 768 |
'\n' |
| 769 |
' If "__setattr__()" wants to assign to an instance ' |
| 770 |
'attribute, it\n' |
| 771 |
' should call the base class method with the same name, ' |
| 772 |
'for example,\n' |
| 773 |
' "object.__setattr__(self, name, value)".\n' |
| 774 |
'\n' |
| 775 |
' For certain sensitive attribute assignments, raises ' |
| 776 |
'an auditing\n' |
| 777 |
' event "object.__setattr__" with arguments "obj", ' |
| 778 |
'"name", "value".\n' |
| 779 |
'\n' |
| 780 |
'object.__delattr__(self, name)\n' |
| 781 |
'\n' |
| 782 |
' Like "__setattr__()" but for attribute deletion ' |
| 783 |
'instead of\n' |
| 784 |
' assignment. This should only be implemented if "del ' |
| 785 |
'obj.name" is\n' |
| 786 |
' meaningful for the object.\n' |
| 787 |
'\n' |
| 788 |
' For certain sensitive attribute deletions, raises an ' |
| 789 |
'auditing event\n' |
| 790 |
' "object.__delattr__" with arguments "obj" and ' |
| 791 |
'"name".\n' |
| 792 |
'\n' |
| 793 |
'object.__dir__(self)\n' |
| 794 |
'\n' |
| 795 |
' Called when "dir()" is called on the object. An ' |
| 796 |
'iterable must be\n' |
| 797 |
' returned. "dir()" converts the returned iterable to a ' |
| 798 |
'list and\n' |
| 799 |
' sorts it.\n' |
| 800 |
'\n' |
| 801 |
'\n' |
| 802 |
'Customizing module attribute access\n' |
| 803 |
'===================================\n' |
| 804 |
'\n' |
| 805 |
'Special names "__getattr__" and "__dir__" can be also ' |
| 806 |
'used to\n' |
| 807 |
'customize access to module attributes. The "__getattr__" ' |
| 808 |
'function at\n' |
| 809 |
'the module level should accept one argument which is the ' |
| 810 |
'name of an\n' |
| 811 |
'attribute and return the computed value or raise an ' |
| 812 |
'"AttributeError".\n' |
| 813 |
'If an attribute is not found on a module object through ' |
| 814 |
'the normal\n' |
| 815 |
'lookup, i.e. "object.__getattribute__()", then ' |
| 816 |
'"__getattr__" is\n' |
| 817 |
'searched in the module "__dict__" before raising an ' |
| 818 |
'"AttributeError".\n' |
| 819 |
'If found, it is called with the attribute name and the ' |
| 820 |
'result is\n' |
| 821 |
'returned.\n' |
| 822 |
'\n' |
| 823 |
'The "__dir__" function should accept no arguments, and ' |
| 824 |
'return an\n' |
| 825 |
'iterable of strings that represents the names accessible ' |
| 826 |
'on module. If\n' |
| 827 |
'present, this function overrides the standard "dir()" ' |
| 828 |
'search on a\n' |
| 829 |
'module.\n' |
| 830 |
'\n' |
| 831 |
'For a more fine grained customization of the module ' |
| 832 |
'behavior (setting\n' |
| 833 |
'attributes, properties, etc.), one can set the ' |
| 834 |
'"__class__" attribute\n' |
| 835 |
'of a module object to a subclass of "types.ModuleType". ' |
| 836 |
'For example:\n' |
| 837 |
'\n' |
| 838 |
' import sys\n' |
| 839 |
' from types import ModuleType\n' |
| 840 |
'\n' |
| 841 |
' class VerboseModule(ModuleType):\n' |
| 842 |
' def __repr__(self):\n' |
| 843 |
" return f'Verbose {self.__name__}'\n" |
| 844 |
'\n' |
| 845 |
' def __setattr__(self, attr, value):\n' |
| 846 |
" print(f'Setting {attr}...')\n" |
| 847 |
' super().__setattr__(attr, value)\n' |
| 848 |
'\n' |
| 849 |
' sys.modules[__name__].__class__ = VerboseModule\n' |
| 850 |
'\n' |
| 851 |
'Note:\n' |
| 852 |
'\n' |
| 853 |
' Defining module "__getattr__" and setting module ' |
| 854 |
'"__class__" only\n' |
| 855 |
' affect lookups made using the attribute access syntax ' |
| 856 |
'– directly\n' |
| 857 |
' accessing the module globals (whether by code within ' |
| 858 |
'the module, or\n' |
| 859 |
' via a reference to the module’s globals dictionary) is ' |
| 860 |
'unaffected.\n' |
| 861 |
'\n' |
| 862 |
'Changed in version 3.5: "__class__" module attribute is ' |
| 863 |
'now writable.\n' |
| 864 |
'\n' |
| 865 |
'New in version 3.7: "__getattr__" and "__dir__" module ' |
| 866 |
'attributes.\n' |
| 867 |
'\n' |
| 868 |
'See also:\n' |
| 869 |
'\n' |
| 870 |
' **PEP 562** - Module __getattr__ and __dir__\n' |
| 871 |
' Describes the "__getattr__" and "__dir__" functions ' |
| 872 |
'on modules.\n' |
| 873 |
'\n' |
| 874 |
'\n' |
| 875 |
'Implementing Descriptors\n' |
| 876 |
'========================\n' |
| 877 |
'\n' |
| 878 |
'The following methods only apply when an instance of the ' |
| 879 |
'class\n' |
| 880 |
'containing the method (a so-called *descriptor* class) ' |
| 881 |
'appears in an\n' |
| 882 |
'*owner* class (the descriptor must be in either the ' |
| 883 |
'owner’s class\n' |
| 884 |
'dictionary or in the class dictionary for one of its ' |
| 885 |
'parents). In the\n' |
| 886 |
'examples below, “the attribute” refers to the attribute ' |
| 887 |
'whose name is\n' |
| 888 |
'the key of the property in the owner class’ "__dict__".\n' |
| 889 |
'\n' |
| 890 |
'object.__get__(self, instance, owner=None)\n' |
| 891 |
'\n' |
| 892 |
' Called to get the attribute of the owner class (class ' |
| 893 |
'attribute\n' |
| 894 |
' access) or of an instance of that class (instance ' |
| 895 |
'attribute\n' |
| 896 |
' access). The optional *owner* argument is the owner ' |
| 897 |
'class, while\n' |
| 898 |
' *instance* is the instance that the attribute was ' |
| 899 |
'accessed through,\n' |
| 900 |
' or "None" when the attribute is accessed through the ' |
| 901 |
'*owner*.\n' |
| 902 |
'\n' |
| 903 |
' This method should return the computed attribute ' |
| 904 |
'value or raise an\n' |
| 905 |
' "AttributeError" exception.\n' |
| 906 |
'\n' |
| 907 |
' **PEP 252** specifies that "__get__()" is callable ' |
| 908 |
'with one or two\n' |
| 909 |
' arguments. Python’s own built-in descriptors support ' |
| 910 |
'this\n' |
| 911 |
' specification; however, it is likely that some ' |
| 912 |
'third-party tools\n' |
| 913 |
' have descriptors that require both arguments. ' |
| 914 |
'Python’s own\n' |
| 915 |
' "__getattribute__()" implementation always passes in ' |
| 916 |
'both arguments\n' |
| 917 |
' whether they are required or not.\n' |
| 918 |
'\n' |
| 919 |
'object.__set__(self, instance, value)\n' |
| 920 |
'\n' |
| 921 |
' Called to set the attribute on an instance *instance* ' |
| 922 |
'of the owner\n' |
| 923 |
' class to a new value, *value*.\n' |
| 924 |
'\n' |
| 925 |
' Note, adding "__set__()" or "__delete__()" changes ' |
| 926 |
'the kind of\n' |
| 927 |
' descriptor to a “data descriptor”. See Invoking ' |
| 928 |
'Descriptors for\n' |
| 929 |
' more details.\n' |
| 930 |
'\n' |
| 931 |
'object.__delete__(self, instance)\n' |
| 932 |
'\n' |
| 933 |
' Called to delete the attribute on an instance ' |
| 934 |
'*instance* of the\n' |
| 935 |
' owner class.\n' |
| 936 |
'\n' |
| 937 |
'Instances of descriptors may also have the ' |
| 938 |
'"__objclass__" attribute\n' |
| 939 |
'present:\n' |
| 940 |
'\n' |
| 941 |
'object.__objclass__\n' |
| 942 |
'\n' |
| 943 |
' The attribute "__objclass__" is interpreted by the ' |
| 944 |
'"inspect" module\n' |
| 945 |
' as specifying the class where this object was defined ' |
| 946 |
'(setting this\n' |
| 947 |
' appropriately can assist in runtime introspection of ' |
| 948 |
'dynamic class\n' |
| 949 |
' attributes). For callables, it may indicate that an ' |
| 950 |
'instance of the\n' |
| 951 |
' given type (or a subclass) is expected or required as ' |
| 952 |
'the first\n' |
| 953 |
' positional argument (for example, CPython sets this ' |
| 954 |
'attribute for\n' |
| 955 |
' unbound methods that are implemented in C).\n' |
| 956 |
'\n' |
| 957 |
'\n' |
| 958 |
'Invoking Descriptors\n' |
| 959 |
'====================\n' |
| 960 |
'\n' |
| 961 |
'In general, a descriptor is an object attribute with ' |
| 962 |
'“binding\n' |
| 963 |
'behavior”, one whose attribute access has been ' |
| 964 |
'overridden by methods\n' |
| 965 |
'in the descriptor protocol: "__get__()", "__set__()", ' |
| 966 |
'and\n' |
| 967 |
'"__delete__()". If any of those methods are defined for ' |
| 968 |
'an object, it\n' |
| 969 |
'is said to be a descriptor.\n' |
| 970 |
'\n' |
| 971 |
'The default behavior for attribute access is to get, ' |
| 972 |
'set, or delete\n' |
| 973 |
'the attribute from an object’s dictionary. For instance, ' |
| 974 |
'"a.x" has a\n' |
| 975 |
'lookup chain starting with "a.__dict__[\'x\']", then\n' |
| 976 |
'"type(a).__dict__[\'x\']", and continuing through the ' |
| 977 |
'base classes of\n' |
| 978 |
'"type(a)" excluding metaclasses.\n' |
| 979 |
'\n' |
| 980 |
'However, if the looked-up value is an object defining ' |
| 981 |
'one of the\n' |
| 982 |
'descriptor methods, then Python may override the default ' |
| 983 |
'behavior and\n' |
| 984 |
'invoke the descriptor method instead. Where this occurs ' |
| 985 |
'in the\n' |
| 986 |
'precedence chain depends on which descriptor methods ' |
| 987 |
'were defined and\n' |
| 988 |
'how they were called.\n' |
| 989 |
'\n' |
| 990 |
'The starting point for descriptor invocation is a ' |
| 991 |
'binding, "a.x". How\n' |
| 992 |
'the arguments are assembled depends on "a":\n' |
| 993 |
'\n' |
| 994 |
'Direct Call\n' |
| 995 |
' The simplest and least common call is when user code ' |
| 996 |
'directly\n' |
| 997 |
' invokes a descriptor method: "x.__get__(a)".\n' |
| 998 |
'\n' |
| 999 |
'Instance Binding\n' |
| 1000 |
' If binding to an object instance, "a.x" is ' |
| 1001 |
'transformed into the\n' |
| 1002 |
' call: "type(a).__dict__[\'x\'].__get__(a, type(a))".\n' |
| 1003 |
'\n' |
| 1004 |
'Class Binding\n' |
| 1005 |
' If binding to a class, "A.x" is transformed into the ' |
| 1006 |
'call:\n' |
| 1007 |
' "A.__dict__[\'x\'].__get__(None, A)".\n' |
| 1008 |
'\n' |
| 1009 |
'Super Binding\n' |
| 1010 |
' A dotted lookup such as "super(A, a).x" searches\n' |
| 1011 |
' "a.__class__.__mro__" for a base class "B" following ' |
| 1012 |
'"A" and then\n' |
| 1013 |
' returns "B.__dict__[\'x\'].__get__(a, A)". If not a ' |
| 1014 |
'descriptor, "x"\n' |
| 1015 |
' is returned unchanged.\n' |
| 1016 |
'\n' |
| 1017 |
'For instance bindings, the precedence of descriptor ' |
| 1018 |
'invocation depends\n' |
| 1019 |
'on which descriptor methods are defined. A descriptor ' |
| 1020 |
'can define any\n' |
| 1021 |
'combination of "__get__()", "__set__()" and ' |
| 1022 |
'"__delete__()". If it\n' |
| 1023 |
'does not define "__get__()", then accessing the ' |
| 1024 |
'attribute will return\n' |
| 1025 |
'the descriptor object itself unless there is a value in ' |
| 1026 |
'the object’s\n' |
| 1027 |
'instance dictionary. If the descriptor defines ' |
| 1028 |
'"__set__()" and/or\n' |
| 1029 |
'"__delete__()", it is a data descriptor; if it defines ' |
| 1030 |
'neither, it is\n' |
| 1031 |
'a non-data descriptor. Normally, data descriptors ' |
| 1032 |
'define both\n' |
| 1033 |
'"__get__()" and "__set__()", while non-data descriptors ' |
| 1034 |
'have just the\n' |
| 1035 |
'"__get__()" method. Data descriptors with "__get__()" ' |
| 1036 |
'and "__set__()"\n' |
| 1037 |
'(and/or "__delete__()") defined always override a ' |
| 1038 |
'redefinition in an\n' |
| 1039 |
'instance dictionary. In contrast, non-data descriptors ' |
| 1040 |
'can be\n' |
| 1041 |
'overridden by instances.\n' |
| 1042 |
'\n' |
| 1043 |
'Python methods (including those decorated with ' |
| 1044 |
'"@staticmethod" and\n' |
| 1045 |
'"@classmethod") are implemented as non-data ' |
| 1046 |
'descriptors. Accordingly,\n' |
| 1047 |
'instances can redefine and override methods. This ' |
| 1048 |
'allows individual\n' |
| 1049 |
'instances to acquire behaviors that differ from other ' |
| 1050 |
'instances of the\n' |
| 1051 |
'same class.\n' |
| 1052 |
'\n' |
| 1053 |
'The "property()" function is implemented as a data ' |
| 1054 |
'descriptor.\n' |
| 1055 |
'Accordingly, instances cannot override the behavior of a ' |
| 1056 |
'property.\n' |
| 1057 |
'\n' |
| 1058 |
'\n' |
| 1059 |
'__slots__\n' |
| 1060 |
'=========\n' |
| 1061 |
'\n' |
| 1062 |
'*__slots__* allow us to explicitly declare data members ' |
| 1063 |
'(like\n' |
| 1064 |
'properties) and deny the creation of "__dict__" and ' |
| 1065 |
'*__weakref__*\n' |
| 1066 |
'(unless explicitly declared in *__slots__* or available ' |
| 1067 |
'in a parent.)\n' |
| 1068 |
'\n' |
| 1069 |
'The space saved over using "__dict__" can be ' |
| 1070 |
'significant. Attribute\n' |
| 1071 |
'lookup speed can be significantly improved as well.\n' |
| 1072 |
'\n' |
| 1073 |
'object.__slots__\n' |
| 1074 |
'\n' |
| 1075 |
' This class variable can be assigned a string, ' |
| 1076 |
'iterable, or sequence\n' |
| 1077 |
' of strings with variable names used by instances. ' |
| 1078 |
'*__slots__*\n' |
| 1079 |
' reserves space for the declared variables and ' |
| 1080 |
'prevents the\n' |
| 1081 |
' automatic creation of "__dict__" and *__weakref__* ' |
| 1082 |
'for each\n' |
| 1083 |
' instance.\n' |
| 1084 |
'\n' |
| 1085 |
'Notes on using *__slots__*:\n' |
| 1086 |
'\n' |
| 1087 |
'* When inheriting from a class without *__slots__*, the ' |
| 1088 |
'"__dict__" and\n' |
| 1089 |
' *__weakref__* attribute of the instances will always ' |
| 1090 |
'be accessible.\n' |
| 1091 |
'\n' |
| 1092 |
'* Without a "__dict__" variable, instances cannot be ' |
| 1093 |
'assigned new\n' |
| 1094 |
' variables not listed in the *__slots__* definition. ' |
| 1095 |
'Attempts to\n' |
| 1096 |
' assign to an unlisted variable name raises ' |
| 1097 |
'"AttributeError". If\n' |
| 1098 |
' dynamic assignment of new variables is desired, then ' |
| 1099 |
'add\n' |
| 1100 |
' "\'__dict__\'" to the sequence of strings in the ' |
| 1101 |
'*__slots__*\n' |
| 1102 |
' declaration.\n' |
| 1103 |
'\n' |
| 1104 |
'* Without a *__weakref__* variable for each instance, ' |
| 1105 |
'classes defining\n' |
| 1106 |
' *__slots__* do not support "weak references" to its ' |
| 1107 |
'instances. If\n' |
| 1108 |
' weak reference support is needed, then add ' |
| 1109 |
'"\'__weakref__\'" to the\n' |
| 1110 |
' sequence of strings in the *__slots__* declaration.\n' |
| 1111 |
'\n' |
| 1112 |
'* *__slots__* are implemented at the class level by ' |
| 1113 |
'creating\n' |
| 1114 |
' descriptors for each variable name. As a result, ' |
| 1115 |
'class attributes\n' |
| 1116 |
' cannot be used to set default values for instance ' |
| 1117 |
'variables defined\n' |
| 1118 |
' by *__slots__*; otherwise, the class attribute would ' |
| 1119 |
'overwrite the\n' |
| 1120 |
' descriptor assignment.\n' |
| 1121 |
'\n' |
| 1122 |
'* The action of a *__slots__* declaration is not limited ' |
| 1123 |
'to the class\n' |
| 1124 |
' where it is defined. *__slots__* declared in parents ' |
| 1125 |
'are available\n' |
| 1126 |
' in child classes. However, child subclasses will get a ' |
| 1127 |
'"__dict__"\n' |
| 1128 |
' and *__weakref__* unless they also define *__slots__* ' |
| 1129 |
'(which should\n' |
| 1130 |
' only contain names of any *additional* slots).\n' |
| 1131 |
'\n' |
| 1132 |
'* If a class defines a slot also defined in a base ' |
| 1133 |
'class, the instance\n' |
| 1134 |
' variable defined by the base class slot is ' |
| 1135 |
'inaccessible (except by\n' |
| 1136 |
' retrieving its descriptor directly from the base ' |
| 1137 |
'class). This\n' |
| 1138 |
' renders the meaning of the program undefined. In the ' |
| 1139 |
'future, a\n' |
| 1140 |
' check may be added to prevent this.\n' |
| 1141 |
'\n' |
| 1142 |
'* "TypeError" will be raised if nonempty *__slots__* are ' |
| 1143 |
'defined for a\n' |
| 1144 |
' class derived from a ""variable-length" built-in type" ' |
| 1145 |
'such as\n' |
| 1146 |
' "int", "bytes", and "tuple".\n' |
| 1147 |
'\n' |
| 1148 |
'* Any non-string *iterable* may be assigned to ' |
| 1149 |
'*__slots__*.\n' |
| 1150 |
'\n' |
| 1151 |
'* If a "dictionary" is used to assign *__slots__*, the ' |
| 1152 |
'dictionary keys\n' |
| 1153 |
' will be used as the slot names. The values of the ' |
| 1154 |
'dictionary can be\n' |
| 1155 |
' used to provide per-attribute docstrings that will be ' |
| 1156 |
'recognised by\n' |
| 1157 |
' "inspect.getdoc()" and displayed in the output of ' |
| 1158 |
'"help()".\n' |
| 1159 |
'\n' |
| 1160 |
'* "__class__" assignment works only if both classes have ' |
| 1161 |
'the same\n' |
| 1162 |
' *__slots__*.\n' |
| 1163 |
'\n' |
| 1164 |
'* Multiple inheritance with multiple slotted parent ' |
| 1165 |
'classes can be\n' |
| 1166 |
' used, but only one parent is allowed to have ' |
| 1167 |
'attributes created by\n' |
| 1168 |
' slots (the other bases must have empty slot layouts) - ' |
| 1169 |
'violations\n' |
| 1170 |
' raise "TypeError".\n' |
| 1171 |
'\n' |
| 1172 |
'* If an *iterator* is used for *__slots__* then a ' |
| 1173 |
'*descriptor* is\n' |
| 1174 |
' created for each of the iterator’s values. However, ' |
| 1175 |
'the *__slots__*\n' |
| 1176 |
' attribute will be an empty iterator.\n', |
| 1177 |
'attribute-references': 'Attribute references\n' |
| 1178 |
'********************\n' |
| 1179 |
'\n' |
| 1180 |
'An attribute reference is a primary followed by a ' |
| 1181 |
'period and a name:\n' |
| 1182 |
'\n' |
| 1183 |
' attributeref ::= primary "." identifier\n' |
| 1184 |
'\n' |
| 1185 |
'The primary must evaluate to an object of a type ' |
| 1186 |
'that supports\n' |
| 1187 |
'attribute references, which most objects do. This ' |
| 1188 |
'object is then\n' |
| 1189 |
'asked to produce the attribute whose name is the ' |
| 1190 |
'identifier. The type\n' |
| 1191 |
'and value produced is determined by the object. ' |
| 1192 |
'Multiple evaluations\n' |
| 1193 |
'of the same attribute reference may yield different ' |
| 1194 |
'objects.\n' |
| 1195 |
'\n' |
| 1196 |
'This production can be customized by overriding the\n' |
| 1197 |
'"__getattribute__()" method or the "__getattr__()" ' |
| 1198 |
'method. The\n' |
| 1199 |
'"__getattribute__()" method is called first and ' |
| 1200 |
'either returns a value\n' |
| 1201 |
'or raises "AttributeError" if the attribute is not ' |
| 1202 |
'available.\n' |
| 1203 |
'\n' |
| 1204 |
'If an "AttributeError" is raised and the object has ' |
| 1205 |
'a "__getattr__()"\n' |
| 1206 |
'method, that method is called as a fallback.\n', |
| 1207 |
'augassign': 'Augmented assignment statements\n' |
| 1208 |
'*******************************\n' |
| 1209 |
'\n' |
| 1210 |
'Augmented assignment is the combination, in a single statement, ' |
| 1211 |
'of a\n' |
| 1212 |
'binary operation and an assignment statement:\n' |
| 1213 |
'\n' |
| 1214 |
' augmented_assignment_stmt ::= augtarget augop ' |
| 1215 |
'(expression_list | yield_expression)\n' |
| 1216 |
' augtarget ::= identifier | attributeref | ' |
| 1217 |
'subscription | slicing\n' |
| 1218 |
' augop ::= "+=" | "-=" | "*=" | "@=" | ' |
| 1219 |
'"/=" | "//=" | "%=" | "**="\n' |
| 1220 |
' | ">>=" | "<<=" | "&=" | "^=" | "|="\n' |
| 1221 |
'\n' |
| 1222 |
'(See section Primaries for the syntax definitions of the last ' |
| 1223 |
'three\n' |
| 1224 |
'symbols.)\n' |
| 1225 |
'\n' |
| 1226 |
'An augmented assignment evaluates the target (which, unlike ' |
| 1227 |
'normal\n' |
| 1228 |
'assignment statements, cannot be an unpacking) and the ' |
| 1229 |
'expression\n' |
| 1230 |
'list, performs the binary operation specific to the type of ' |
| 1231 |
'assignment\n' |
| 1232 |
'on the two operands, and assigns the result to the original ' |
| 1233 |
'target.\n' |
| 1234 |
'The target is only evaluated once.\n' |
| 1235 |
'\n' |
| 1236 |
'An augmented assignment expression like "x += 1" can be ' |
| 1237 |
'rewritten as\n' |
| 1238 |
'"x = x + 1" to achieve a similar, but not exactly equal effect. ' |
| 1239 |
'In the\n' |
| 1240 |
'augmented version, "x" is only evaluated once. Also, when ' |
| 1241 |
'possible,\n' |
| 1242 |
'the actual operation is performed *in-place*, meaning that ' |
| 1243 |
'rather than\n' |
| 1244 |
'creating a new object and assigning that to the target, the old ' |
| 1245 |
'object\n' |
| 1246 |
'is modified instead.\n' |
| 1247 |
'\n' |
| 1248 |
'Unlike normal assignments, augmented assignments evaluate the ' |
| 1249 |
'left-\n' |
| 1250 |
'hand side *before* evaluating the right-hand side. For ' |
| 1251 |
'example, "a[i]\n' |
| 1252 |
'+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and ' |
| 1253 |
'performs\n' |
| 1254 |
'the addition, and lastly, it writes the result back to "a[i]".\n' |
| 1255 |
'\n' |
| 1256 |
'With the exception of assigning to tuples and multiple targets ' |
| 1257 |
'in a\n' |
| 1258 |
'single statement, the assignment done by augmented assignment\n' |
| 1259 |
'statements is handled the same way as normal assignments. ' |
| 1260 |
'Similarly,\n' |
| 1261 |
'with the exception of the possible *in-place* behavior, the ' |
| 1262 |
'binary\n' |
| 1263 |
'operation performed by augmented assignment is the same as the ' |
| 1264 |
'normal\n' |
| 1265 |
'binary operations.\n' |
| 1266 |
'\n' |
| 1267 |
'For targets which are attribute references, the same caveat ' |
| 1268 |
'about\n' |
| 1269 |
'class and instance attributes applies as for regular ' |
| 1270 |
'assignments.\n', |
| 1271 |
'await': 'Await expression\n' |
| 1272 |
'****************\n' |
| 1273 |
'\n' |
| 1274 |
'Suspend the execution of *coroutine* on an *awaitable* object. Can\n' |
| 1275 |
'only be used inside a *coroutine function*.\n' |
| 1276 |
'\n' |
| 1277 |
' await_expr ::= "await" primary\n' |
| 1278 |
'\n' |
| 1279 |
'New in version 3.5.\n', |
| 1280 |
'binary': 'Binary arithmetic operations\n' |
| 1281 |
'****************************\n' |
| 1282 |
'\n' |
| 1283 |
'The binary arithmetic operations have the conventional priority\n' |
| 1284 |
'levels. Note that some of these operations also apply to certain ' |
| 1285 |
'non-\n' |
| 1286 |
'numeric types. Apart from the power operator, there are only two\n' |
| 1287 |
'levels, one for multiplicative operators and one for additive\n' |
| 1288 |
'operators:\n' |
| 1289 |
'\n' |
| 1290 |
' m_expr ::= u_expr | m_expr "*" u_expr | m_expr "@" m_expr |\n' |
| 1291 |
' m_expr "//" u_expr | m_expr "/" u_expr |\n' |
| 1292 |
' m_expr "%" u_expr\n' |
| 1293 |
' a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr\n' |
| 1294 |
'\n' |
| 1295 |
'The "*" (multiplication) operator yields the product of its ' |
| 1296 |
'arguments.\n' |
| 1297 |
'The arguments must either both be numbers, or one argument must be ' |
| 1298 |
'an\n' |
| 1299 |
'integer and the other must be a sequence. In the former case, the\n' |
| 1300 |
'numbers are converted to a common type and then multiplied ' |
| 1301 |
'together.\n' |
| 1302 |
'In the latter case, sequence repetition is performed; a negative\n' |
| 1303 |
'repetition factor yields an empty sequence.\n' |
| 1304 |
'\n' |
| 1305 |
'This operation can be customized using the special "__mul__()" ' |
| 1306 |
'and\n' |
| 1307 |
'"__rmul__()" methods.\n' |
| 1308 |
'\n' |
| 1309 |
'The "@" (at) operator is intended to be used for matrix\n' |
| 1310 |
'multiplication. No builtin Python types implement this operator.\n' |
| 1311 |
'\n' |
| 1312 |
'New in version 3.5.\n' |
| 1313 |
'\n' |
| 1314 |
'The "/" (division) and "//" (floor division) operators yield the\n' |
| 1315 |
'quotient of their arguments. The numeric arguments are first\n' |
| 1316 |
'converted to a common type. Division of integers yields a float, ' |
| 1317 |
'while\n' |
| 1318 |
'floor division of integers results in an integer; the result is ' |
| 1319 |
'that\n' |
| 1320 |
'of mathematical division with the ‘floor’ function applied to the\n' |
| 1321 |
'result. Division by zero raises the "ZeroDivisionError" ' |
| 1322 |
'exception.\n' |
| 1323 |
'\n' |
| 1324 |
'This operation can be customized using the special "__truediv__()" ' |
| 1325 |
'and\n' |
| 1326 |
'"__floordiv__()" methods.\n' |
| 1327 |
'\n' |
| 1328 |
'The "%" (modulo) operator yields the remainder from the division ' |
| 1329 |
'of\n' |
| 1330 |
'the first argument by the second. The numeric arguments are ' |
| 1331 |
'first\n' |
| 1332 |
'converted to a common type. A zero right argument raises the\n' |
| 1333 |
'"ZeroDivisionError" exception. The arguments may be floating ' |
| 1334 |
'point\n' |
| 1335 |
'numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals ' |
| 1336 |
'"4*0.7 +\n' |
| 1337 |
'0.34".) The modulo operator always yields a result with the same ' |
| 1338 |
'sign\n' |
| 1339 |
'as its second operand (or zero); the absolute value of the result ' |
| 1340 |
'is\n' |
| 1341 |
'strictly smaller than the absolute value of the second operand ' |
| 1342 |
'[1].\n' |
| 1343 |
'\n' |
| 1344 |
'The floor division and modulo operators are connected by the ' |
| 1345 |
'following\n' |
| 1346 |
'identity: "x == (x//y)*y + (x%y)". Floor division and modulo are ' |
| 1347 |
'also\n' |
| 1348 |
'connected with the built-in function "divmod()": "divmod(x, y) ==\n' |
| 1349 |
'(x//y, x%y)". [2].\n' |
| 1350 |
'\n' |
| 1351 |
'In addition to performing the modulo operation on numbers, the ' |
| 1352 |
'"%"\n' |
| 1353 |
'operator is also overloaded by string objects to perform ' |
| 1354 |
'old-style\n' |
| 1355 |
'string formatting (also known as interpolation). The syntax for\n' |
| 1356 |
'string formatting is described in the Python Library Reference,\n' |
| 1357 |
'section printf-style String Formatting.\n' |
| 1358 |
'\n' |
| 1359 |
'The *modulo* operation can be customized using the special ' |
| 1360 |
'"__mod__()"\n' |
| 1361 |
'method.\n' |
| 1362 |
'\n' |
| 1363 |
'The floor division operator, the modulo operator, and the ' |
| 1364 |
'"divmod()"\n' |
| 1365 |
'function are not defined for complex numbers. Instead, convert to ' |
| 1366 |
'a\n' |
| 1367 |
'floating point number using the "abs()" function if appropriate.\n' |
| 1368 |
'\n' |
| 1369 |
'The "+" (addition) operator yields the sum of its arguments. The\n' |
| 1370 |
'arguments must either both be numbers or both be sequences of the ' |
| 1371 |
'same\n' |
| 1372 |
'type. In the former case, the numbers are converted to a common ' |
| 1373 |
'type\n' |
| 1374 |
'and then added together. In the latter case, the sequences are\n' |
| 1375 |
'concatenated.\n' |
| 1376 |
'\n' |
| 1377 |
'This operation can be customized using the special "__add__()" ' |
| 1378 |
'and\n' |
| 1379 |
'"__radd__()" methods.\n' |
| 1380 |
'\n' |
| 1381 |
'The "-" (subtraction) operator yields the difference of its ' |
| 1382 |
'arguments.\n' |
| 1383 |
'The numeric arguments are first converted to a common type.\n' |
| 1384 |
'\n' |
| 1385 |
'This operation can be customized using the special "__sub__()" ' |
| 1386 |
'method.\n', |
| 1387 |
'bitwise': 'Binary bitwise operations\n' |
| 1388 |
'*************************\n' |
| 1389 |
'\n' |
| 1390 |
'Each of the three bitwise operations has a different priority ' |
| 1391 |
'level:\n' |
| 1392 |
'\n' |
| 1393 |
' and_expr ::= shift_expr | and_expr "&" shift_expr\n' |
| 1394 |
' xor_expr ::= and_expr | xor_expr "^" and_expr\n' |
| 1395 |
' or_expr ::= xor_expr | or_expr "|" xor_expr\n' |
| 1396 |
'\n' |
| 1397 |
'The "&" operator yields the bitwise AND of its arguments, which ' |
| 1398 |
'must\n' |
| 1399 |
'be integers or one of them must be a custom object overriding\n' |
| 1400 |
'"__and__()" or "__rand__()" special methods.\n' |
| 1401 |
'\n' |
| 1402 |
'The "^" operator yields the bitwise XOR (exclusive OR) of its\n' |
| 1403 |
'arguments, which must be integers or one of them must be a ' |
| 1404 |
'custom\n' |
| 1405 |
'object overriding "__xor__()" or "__rxor__()" special methods.\n' |
| 1406 |
'\n' |
| 1407 |
'The "|" operator yields the bitwise (inclusive) OR of its ' |
| 1408 |
'arguments,\n' |
| 1409 |
'which must be integers or one of them must be a custom object\n' |
| 1410 |
'overriding "__or__()" or "__ror__()" special methods.\n', |
| 1411 |
'bltin-code-objects': 'Code Objects\n' |
| 1412 |
'************\n' |
| 1413 |
'\n' |
| 1414 |
'Code objects are used by the implementation to ' |
| 1415 |
'represent “pseudo-\n' |
| 1416 |
'compiled” executable Python code such as a function ' |
| 1417 |
'body. They differ\n' |
| 1418 |
'from function objects because they don’t contain a ' |
| 1419 |
'reference to their\n' |
| 1420 |
'global execution environment. Code objects are ' |
| 1421 |
'returned by the built-\n' |
| 1422 |
'in "compile()" function and can be extracted from ' |
| 1423 |
'function objects\n' |
| 1424 |
'through their "__code__" attribute. See also the ' |
| 1425 |
'"code" module.\n' |
| 1426 |
'\n' |
| 1427 |
'Accessing "__code__" raises an auditing event ' |
| 1428 |
'"object.__getattr__"\n' |
| 1429 |
'with arguments "obj" and ""__code__"".\n' |
| 1430 |
'\n' |
| 1431 |
'A code object can be executed or evaluated by passing ' |
| 1432 |
'it (instead of a\n' |
| 1433 |
'source string) to the "exec()" or "eval()" built-in ' |
| 1434 |
'functions.\n' |
| 1435 |
'\n' |
| 1436 |
'See The standard type hierarchy for more ' |
| 1437 |
'information.\n', |
| 1438 |
'bltin-ellipsis-object': 'The Ellipsis Object\n' |
| 1439 |
'*******************\n' |
| 1440 |
'\n' |
| 1441 |
'This object is commonly used by slicing (see ' |
| 1442 |
'Slicings). It supports\n' |
| 1443 |
'no special operations. There is exactly one ' |
| 1444 |
'ellipsis object, named\n' |
| 1445 |
'"Ellipsis" (a built-in name). "type(Ellipsis)()" ' |
| 1446 |
'produces the\n' |
| 1447 |
'"Ellipsis" singleton.\n' |
| 1448 |
'\n' |
| 1449 |
'It is written as "Ellipsis" or "...".\n', |
| 1450 |
'bltin-null-object': 'The Null Object\n' |
| 1451 |
'***************\n' |
| 1452 |
'\n' |
| 1453 |
'This object is returned by functions that don’t ' |
| 1454 |
'explicitly return a\n' |
| 1455 |
'value. It supports no special operations. There is ' |
| 1456 |
'exactly one null\n' |
| 1457 |
'object, named "None" (a built-in name). "type(None)()" ' |
| 1458 |
'produces the\n' |
| 1459 |
'same singleton.\n' |
| 1460 |
'\n' |
| 1461 |
'It is written as "None".\n', |
| 1462 |
'bltin-type-objects': 'Type Objects\n' |
| 1463 |
'************\n' |
| 1464 |
'\n' |
| 1465 |
'Type objects represent the various object types. An ' |
| 1466 |
'object’s type is\n' |
| 1467 |
'accessed by the built-in function "type()". There are ' |
| 1468 |
'no special\n' |
| 1469 |
'operations on types. The standard module "types" ' |
| 1470 |
'defines names for\n' |
| 1471 |
'all standard built-in types.\n' |
| 1472 |
'\n' |
| 1473 |
'Types are written like this: "<class \'int\'>".\n', |
| 1474 |
'booleans': 'Boolean operations\n' |
| 1475 |
'******************\n' |
| 1476 |
'\n' |
| 1477 |
' or_test ::= and_test | or_test "or" and_test\n' |
| 1478 |
' and_test ::= not_test | and_test "and" not_test\n' |
| 1479 |
' not_test ::= comparison | "not" not_test\n' |
| 1480 |
'\n' |
| 1481 |
'In the context of Boolean operations, and also when expressions ' |
| 1482 |
'are\n' |
| 1483 |
'used by control flow statements, the following values are ' |
| 1484 |
'interpreted\n' |
| 1485 |
'as false: "False", "None", numeric zero of all types, and empty\n' |
| 1486 |
'strings and containers (including strings, tuples, lists,\n' |
| 1487 |
'dictionaries, sets and frozensets). All other values are ' |
| 1488 |
'interpreted\n' |
| 1489 |
'as true. User-defined objects can customize their truth value ' |
| 1490 |
'by\n' |
| 1491 |
'providing a "__bool__()" method.\n' |
| 1492 |
'\n' |
| 1493 |
'The operator "not" yields "True" if its argument is false, ' |
| 1494 |
'"False"\n' |
| 1495 |
'otherwise.\n' |
| 1496 |
'\n' |
| 1497 |
'The expression "x and y" first evaluates *x*; if *x* is false, ' |
| 1498 |
'its\n' |
| 1499 |
'value is returned; otherwise, *y* is evaluated and the resulting ' |
| 1500 |
'value\n' |
| 1501 |
'is returned.\n' |
| 1502 |
'\n' |
| 1503 |
'The expression "x or y" first evaluates *x*; if *x* is true, its ' |
| 1504 |
'value\n' |
| 1505 |
'is returned; otherwise, *y* is evaluated and the resulting value ' |
| 1506 |
'is\n' |
| 1507 |
'returned.\n' |
| 1508 |
'\n' |
| 1509 |
'Note that neither "and" nor "or" restrict the value and type ' |
| 1510 |
'they\n' |
| 1511 |
'return to "False" and "True", but rather return the last ' |
| 1512 |
'evaluated\n' |
| 1513 |
'argument. This is sometimes useful, e.g., if "s" is a string ' |
| 1514 |
'that\n' |
| 1515 |
'should be replaced by a default value if it is empty, the ' |
| 1516 |
'expression\n' |
| 1517 |
'"s or \'foo\'" yields the desired value. Because "not" has to ' |
| 1518 |
'create a\n' |
| 1519 |
'new value, it returns a boolean value regardless of the type of ' |
| 1520 |
'its\n' |
| 1521 |
'argument (for example, "not \'foo\'" produces "False" rather ' |
| 1522 |
'than "\'\'".)\n', |
| 1523 |
'break': 'The "break" statement\n' |
| 1524 |
'*********************\n' |
| 1525 |
'\n' |
| 1526 |
' break_stmt ::= "break"\n' |
| 1527 |
'\n' |
| 1528 |
'"break" may only occur syntactically nested in a "for" or "while"\n' |
| 1529 |
'loop, but not nested in a function or class definition within that\n' |
| 1530 |
'loop.\n' |
| 1531 |
'\n' |
| 1532 |
'It terminates the nearest enclosing loop, skipping the optional ' |
| 1533 |
'"else"\n' |
| 1534 |
'clause if the loop has one.\n' |
| 1535 |
'\n' |
| 1536 |
'If a "for" loop is terminated by "break", the loop control target\n' |
| 1537 |
'keeps its current value.\n' |
| 1538 |
'\n' |
| 1539 |
'When "break" passes control out of a "try" statement with a ' |
| 1540 |
'"finally"\n' |
| 1541 |
'clause, that "finally" clause is executed before really leaving ' |
| 1542 |
'the\n' |
| 1543 |
'loop.\n', |
| 1544 |
'callable-types': 'Emulating callable objects\n' |
| 1545 |
'**************************\n' |
| 1546 |
'\n' |
| 1547 |
'object.__call__(self[, args...])\n' |
| 1548 |
'\n' |
| 1549 |
' Called when the instance is “called” as a function; if ' |
| 1550 |
'this method\n' |
| 1551 |
' is defined, "x(arg1, arg2, ...)" roughly translates to\n' |
| 1552 |
' "type(x).__call__(x, arg1, ...)".\n', |
| 1553 |
'calls': 'Calls\n' |
| 1554 |
'*****\n' |
| 1555 |
'\n' |
| 1556 |
'A call calls a callable object (e.g., a *function*) with a ' |
| 1557 |
'possibly\n' |
| 1558 |
'empty series of *arguments*:\n' |
| 1559 |
'\n' |
| 1560 |
' call ::= primary "(" [argument_list [","] | ' |
| 1561 |
'comprehension] ")"\n' |
| 1562 |
' argument_list ::= positional_arguments ["," ' |
| 1563 |
'starred_and_keywords]\n' |
| 1564 |
' ["," keywords_arguments]\n' |
| 1565 |
' | starred_and_keywords ["," ' |
| 1566 |
'keywords_arguments]\n' |
| 1567 |
' | keywords_arguments\n' |
| 1568 |
' positional_arguments ::= positional_item ("," positional_item)*\n' |
| 1569 |
' positional_item ::= assignment_expression | "*" expression\n' |
| 1570 |
' starred_and_keywords ::= ("*" expression | keyword_item)\n' |
| 1571 |
' ("," "*" expression | "," ' |
| 1572 |
'keyword_item)*\n' |
| 1573 |
' keywords_arguments ::= (keyword_item | "**" expression)\n' |
| 1574 |
' ("," keyword_item | "," "**" ' |
| 1575 |
'expression)*\n' |
| 1576 |
' keyword_item ::= identifier "=" expression\n' |
| 1577 |
'\n' |
| 1578 |
'An optional trailing comma may be present after the positional and\n' |
| 1579 |
'keyword arguments but does not affect the semantics.\n' |
| 1580 |
'\n' |
| 1581 |
'The primary must evaluate to a callable object (user-defined\n' |
| 1582 |
'functions, built-in functions, methods of built-in objects, class\n' |
| 1583 |
'objects, methods of class instances, and all objects having a\n' |
| 1584 |
'"__call__()" method are callable). All argument expressions are\n' |
| 1585 |
'evaluated before the call is attempted. Please refer to section\n' |
| 1586 |
'Function definitions for the syntax of formal *parameter* lists.\n' |
| 1587 |
'\n' |
| 1588 |
'If keyword arguments are present, they are first converted to\n' |
| 1589 |
'positional arguments, as follows. First, a list of unfilled slots ' |
| 1590 |
'is\n' |
| 1591 |
'created for the formal parameters. If there are N positional\n' |
| 1592 |
'arguments, they are placed in the first N slots. Next, for each\n' |
| 1593 |
'keyword argument, the identifier is used to determine the\n' |
| 1594 |
'corresponding slot (if the identifier is the same as the first ' |
| 1595 |
'formal\n' |
| 1596 |
'parameter name, the first slot is used, and so on). If the slot ' |
| 1597 |
'is\n' |
| 1598 |
'already filled, a "TypeError" exception is raised. Otherwise, the\n' |
| 1599 |
'argument is placed in the slot, filling it (even if the expression ' |
| 1600 |
'is\n' |
| 1601 |
'"None", it fills the slot). When all arguments have been ' |
| 1602 |
'processed,\n' |
| 1603 |
'the slots that are still unfilled are filled with the ' |
| 1604 |
'corresponding\n' |
| 1605 |
'default value from the function definition. (Default values are\n' |
| 1606 |
'calculated, once, when the function is defined; thus, a mutable ' |
| 1607 |
'object\n' |
| 1608 |
'such as a list or dictionary used as default value will be shared ' |
| 1609 |
'by\n' |
| 1610 |
'all calls that don’t specify an argument value for the ' |
| 1611 |
'corresponding\n' |
| 1612 |
'slot; this should usually be avoided.) If there are any unfilled\n' |
| 1613 |
'slots for which no default value is specified, a "TypeError" ' |
| 1614 |
'exception\n' |
| 1615 |
'is raised. Otherwise, the list of filled slots is used as the\n' |
| 1616 |
'argument list for the call.\n' |
| 1617 |
'\n' |
| 1618 |
'**CPython implementation detail:** An implementation may provide\n' |
| 1619 |
'built-in functions whose positional parameters do not have names, ' |
| 1620 |
'even\n' |
| 1621 |
'if they are ‘named’ for the purpose of documentation, and which\n' |
| 1622 |
'therefore cannot be supplied by keyword. In CPython, this is the ' |
| 1623 |
'case\n' |
| 1624 |
'for functions implemented in C that use "PyArg_ParseTuple()" to ' |
| 1625 |
'parse\n' |
| 1626 |
'their arguments.\n' |
| 1627 |
'\n' |
| 1628 |
'If there are more positional arguments than there are formal ' |
| 1629 |
'parameter\n' |
| 1630 |
'slots, a "TypeError" exception is raised, unless a formal ' |
| 1631 |
'parameter\n' |
| 1632 |
'using the syntax "*identifier" is present; in this case, that ' |
| 1633 |
'formal\n' |
| 1634 |
'parameter receives a tuple containing the excess positional ' |
| 1635 |
'arguments\n' |
| 1636 |
'(or an empty tuple if there were no excess positional arguments).\n' |
| 1637 |
'\n' |
| 1638 |
'If any keyword argument does not correspond to a formal parameter\n' |
| 1639 |
'name, a "TypeError" exception is raised, unless a formal parameter\n' |
| 1640 |
'using the syntax "**identifier" is present; in this case, that ' |
| 1641 |
'formal\n' |
| 1642 |
'parameter receives a dictionary containing the excess keyword\n' |
| 1643 |
'arguments (using the keywords as keys and the argument values as\n' |
| 1644 |
'corresponding values), or a (new) empty dictionary if there were ' |
| 1645 |
'no\n' |
| 1646 |
'excess keyword arguments.\n' |
| 1647 |
'\n' |
| 1648 |
'If the syntax "*expression" appears in the function call, ' |
| 1649 |
'"expression"\n' |
| 1650 |
'must evaluate to an *iterable*. Elements from these iterables are\n' |
| 1651 |
'treated as if they were additional positional arguments. For the ' |
| 1652 |
'call\n' |
| 1653 |
'"f(x1, x2, *y, x3, x4)", if *y* evaluates to a sequence *y1*, …, ' |
| 1654 |
'*yM*,\n' |
| 1655 |
'this is equivalent to a call with M+4 positional arguments *x1*, ' |
| 1656 |
'*x2*,\n' |
| 1657 |
'*y1*, …, *yM*, *x3*, *x4*.\n' |
| 1658 |
'\n' |
| 1659 |
'A consequence of this is that although the "*expression" syntax ' |
| 1660 |
'may\n' |
| 1661 |
'appear *after* explicit keyword arguments, it is processed ' |
| 1662 |
'*before*\n' |
| 1663 |
'the keyword arguments (and any "**expression" arguments – see ' |
| 1664 |
'below).\n' |
| 1665 |
'So:\n' |
| 1666 |
'\n' |
| 1667 |
' >>> def f(a, b):\n' |
| 1668 |
' ... print(a, b)\n' |
| 1669 |
' ...\n' |
| 1670 |
' >>> f(b=1, *(2,))\n' |
| 1671 |
' 2 1\n' |
| 1672 |
' >>> f(a=1, *(2,))\n' |
| 1673 |
' Traceback (most recent call last):\n' |
| 1674 |
' File "<stdin>", line 1, in <module>\n' |
| 1675 |
" TypeError: f() got multiple values for keyword argument 'a'\n" |
| 1676 |
' >>> f(1, *(2,))\n' |
| 1677 |
' 1 2\n' |
| 1678 |
'\n' |
| 1679 |
'It is unusual for both keyword arguments and the "*expression" ' |
| 1680 |
'syntax\n' |
| 1681 |
'to be used in the same call, so in practice this confusion does ' |
| 1682 |
'not\n' |
| 1683 |
'often arise.\n' |
| 1684 |
'\n' |
| 1685 |
'If the syntax "**expression" appears in the function call,\n' |
| 1686 |
'"expression" must evaluate to a *mapping*, the contents of which ' |
| 1687 |
'are\n' |
| 1688 |
'treated as additional keyword arguments. If a parameter matching a ' |
| 1689 |
'key\n' |
| 1690 |
'has already been given a value (by an explicit keyword argument, ' |
| 1691 |
'or\n' |
| 1692 |
'from another unpacking), a "TypeError" exception is raised.\n' |
| 1693 |
'\n' |
| 1694 |
'When "**expression" is used, each key in this mapping must be a\n' |
| 1695 |
'string. Each value from the mapping is assigned to the first ' |
| 1696 |
'formal\n' |
| 1697 |
'parameter eligible for keyword assignment whose name is equal to ' |
| 1698 |
'the\n' |
| 1699 |
'key. A key need not be a Python identifier (e.g. ""max-temp °F"" ' |
| 1700 |
'is\n' |
| 1701 |
'acceptable, although it will not match any formal parameter that ' |
| 1702 |
'could\n' |
| 1703 |
'be declared). If there is no match to a formal parameter the ' |
| 1704 |
'key-value\n' |
| 1705 |
'pair is collected by the "**" parameter, if there is one, or if ' |
| 1706 |
'there\n' |
| 1707 |
'is not, a "TypeError" exception is raised.\n' |
| 1708 |
'\n' |
| 1709 |
'Formal parameters using the syntax "*identifier" or "**identifier"\n' |
| 1710 |
'cannot be used as positional argument slots or as keyword argument\n' |
| 1711 |
'names.\n' |
| 1712 |
'\n' |
| 1713 |
'Changed in version 3.5: Function calls accept any number of "*" ' |
| 1714 |
'and\n' |
| 1715 |
'"**" unpackings, positional arguments may follow iterable ' |
| 1716 |
'unpackings\n' |
| 1717 |
'("*"), and keyword arguments may follow dictionary unpackings ' |
| 1718 |
'("**").\n' |
| 1719 |
'Originally proposed by **PEP 448**.\n' |
| 1720 |
'\n' |
| 1721 |
'A call always returns some value, possibly "None", unless it raises ' |
| 1722 |
'an\n' |
| 1723 |
'exception. How this value is computed depends on the type of the\n' |
| 1724 |
'callable object.\n' |
| 1725 |
'\n' |
| 1726 |
'If it is—\n' |
| 1727 |
'\n' |
| 1728 |
'a user-defined function:\n' |
| 1729 |
' The code block for the function is executed, passing it the\n' |
| 1730 |
' argument list. The first thing the code block will do is bind ' |
| 1731 |
'the\n' |
| 1732 |
' formal parameters to the arguments; this is described in ' |
| 1733 |
'section\n' |
| 1734 |
' Function definitions. When the code block executes a "return"\n' |
| 1735 |
' statement, this specifies the return value of the function ' |
| 1736 |
'call.\n' |
| 1737 |
'\n' |
| 1738 |
'a built-in function or method:\n' |
| 1739 |
' The result is up to the interpreter; see Built-in Functions for ' |
| 1740 |
'the\n' |
| 1741 |
' descriptions of built-in functions and methods.\n' |
| 1742 |
'\n' |
| 1743 |
'a class object:\n' |
| 1744 |
' A new instance of that class is returned.\n' |
| 1745 |
'\n' |
| 1746 |
'a class instance method:\n' |
| 1747 |
' The corresponding user-defined function is called, with an ' |
| 1748 |
'argument\n' |
| 1749 |
' list that is one longer than the argument list of the call: the\n' |
| 1750 |
' instance becomes the first argument.\n' |
| 1751 |
'\n' |
| 1752 |
'a class instance:\n' |
| 1753 |
' The class must define a "__call__()" method; the effect is then ' |
| 1754 |
'the\n' |
| 1755 |
' same as if that method was called.\n', |
| 1756 |
'class': 'Class definitions\n' |
| 1757 |
'*****************\n' |
| 1758 |
'\n' |
| 1759 |
'A class definition defines a class object (see section The ' |
| 1760 |
'standard\n' |
| 1761 |
'type hierarchy):\n' |
| 1762 |
'\n' |
| 1763 |
' classdef ::= [decorators] "class" classname [inheritance] ":" ' |
| 1764 |
'suite\n' |
| 1765 |
' inheritance ::= "(" [argument_list] ")"\n' |
| 1766 |
' classname ::= identifier\n' |
| 1767 |
'\n' |
| 1768 |
'A class definition is an executable statement. The inheritance ' |
| 1769 |
'list\n' |
| 1770 |
'usually gives a list of base classes (see Metaclasses for more\n' |
| 1771 |
'advanced uses), so each item in the list should evaluate to a ' |
| 1772 |
'class\n' |
| 1773 |
'object which allows subclassing. Classes without an inheritance ' |
| 1774 |
'list\n' |
| 1775 |
'inherit, by default, from the base class "object"; hence,\n' |
| 1776 |
'\n' |
| 1777 |
' class Foo:\n' |
| 1778 |
' pass\n' |
| 1779 |
'\n' |
| 1780 |
'is equivalent to\n' |
| 1781 |
'\n' |
| 1782 |
' class Foo(object):\n' |
| 1783 |
' pass\n' |
| 1784 |
'\n' |
| 1785 |
'The class’s suite is then executed in a new execution frame (see\n' |
| 1786 |
'Naming and binding), using a newly created local namespace and the\n' |
| 1787 |
'original global namespace. (Usually, the suite contains mostly\n' |
| 1788 |
'function definitions.) When the class’s suite finishes execution, ' |
| 1789 |
'its\n' |
| 1790 |
'execution frame is discarded but its local namespace is saved. [5] ' |
| 1791 |
'A\n' |
| 1792 |
'class object is then created using the inheritance list for the ' |
| 1793 |
'base\n' |
| 1794 |
'classes and the saved local namespace for the attribute ' |
| 1795 |
'dictionary.\n' |
| 1796 |
'The class name is bound to this class object in the original local\n' |
| 1797 |
'namespace.\n' |
| 1798 |
'\n' |
| 1799 |
'The order in which attributes are defined in the class body is\n' |
| 1800 |
'preserved in the new class’s "__dict__". Note that this is ' |
| 1801 |
'reliable\n' |
| 1802 |
'only right after the class is created and only for classes that ' |
| 1803 |
'were\n' |
| 1804 |
'defined using the definition syntax.\n' |
| 1805 |
'\n' |
| 1806 |
'Class creation can be customized heavily using metaclasses.\n' |
| 1807 |
'\n' |
| 1808 |
'Classes can also be decorated: just like when decorating ' |
| 1809 |
'functions,\n' |
| 1810 |
'\n' |
| 1811 |
' @f1(arg)\n' |
| 1812 |
' @f2\n' |
| 1813 |
' class Foo: pass\n' |
| 1814 |
'\n' |
| 1815 |
'is roughly equivalent to\n' |
| 1816 |
'\n' |
| 1817 |
' class Foo: pass\n' |
| 1818 |
' Foo = f1(arg)(f2(Foo))\n' |
| 1819 |
'\n' |
| 1820 |
'The evaluation rules for the decorator expressions are the same as ' |
| 1821 |
'for\n' |
| 1822 |
'function decorators. The result is then bound to the class name.\n' |
| 1823 |
'\n' |
| 1824 |
'Changed in version 3.9: Classes may be decorated with any valid\n' |
| 1825 |
'"assignment_expression". Previously, the grammar was much more\n' |
| 1826 |
'restrictive; see **PEP 614** for details.\n' |
| 1827 |
'\n' |
| 1828 |
'**Programmer’s note:** Variables defined in the class definition ' |
| 1829 |
'are\n' |
| 1830 |
'class attributes; they are shared by instances. Instance ' |
| 1831 |
'attributes\n' |
| 1832 |
'can be set in a method with "self.name = value". Both class and\n' |
| 1833 |
'instance attributes are accessible through the notation ' |
| 1834 |
'“"self.name"”,\n' |
| 1835 |
'and an instance attribute hides a class attribute with the same ' |
| 1836 |
'name\n' |
| 1837 |
'when accessed in this way. Class attributes can be used as ' |
| 1838 |
'defaults\n' |
| 1839 |
'for instance attributes, but using mutable values there can lead ' |
| 1840 |
'to\n' |
| 1841 |
'unexpected results. Descriptors can be used to create instance\n' |
| 1842 |
'variables with different implementation details.\n' |
| 1843 |
'\n' |
| 1844 |
'See also:\n' |
| 1845 |
'\n' |
| 1846 |
' **PEP 3115** - Metaclasses in Python 3000\n' |
| 1847 |
' The proposal that changed the declaration of metaclasses to ' |
| 1848 |
'the\n' |
| 1849 |
' current syntax, and the semantics for how classes with\n' |
| 1850 |
' metaclasses are constructed.\n' |
| 1851 |
'\n' |
| 1852 |
' **PEP 3129** - Class Decorators\n' |
| 1853 |
' The proposal that added class decorators. Function and ' |
| 1854 |
'method\n' |
| 1855 |
' decorators were introduced in **PEP 318**.\n', |
| 1856 |
'comparisons': 'Comparisons\n' |
| 1857 |
'***********\n' |
| 1858 |
'\n' |
| 1859 |
'Unlike C, all comparison operations in Python have the same ' |
| 1860 |
'priority,\n' |
| 1861 |
'which is lower than that of any arithmetic, shifting or ' |
| 1862 |
'bitwise\n' |
| 1863 |
'operation. Also unlike C, expressions like "a < b < c" have ' |
| 1864 |
'the\n' |
| 1865 |
'interpretation that is conventional in mathematics:\n' |
| 1866 |
'\n' |
| 1867 |
' comparison ::= or_expr (comp_operator or_expr)*\n' |
| 1868 |
' comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!="\n' |
| 1869 |
' | "is" ["not"] | ["not"] "in"\n' |
| 1870 |
'\n' |
| 1871 |
'Comparisons yield boolean values: "True" or "False". Custom ' |
| 1872 |
'*rich\n' |
| 1873 |
'comparison methods* may return non-boolean values. In this ' |
| 1874 |
'case Python\n' |
| 1875 |
'will call "bool()" on such value in boolean contexts.\n' |
| 1876 |
'\n' |
| 1877 |
'Comparisons can be chained arbitrarily, e.g., "x < y <= z" ' |
| 1878 |
'is\n' |
| 1879 |
'equivalent to "x < y and y <= z", except that "y" is ' |
| 1880 |
'evaluated only\n' |
| 1881 |
'once (but in both cases "z" is not evaluated at all when "x < ' |
| 1882 |
'y" is\n' |
| 1883 |
'found to be false).\n' |
| 1884 |
'\n' |
| 1885 |
'Formally, if *a*, *b*, *c*, …, *y*, *z* are expressions and ' |
| 1886 |
'*op1*,\n' |
| 1887 |
'*op2*, …, *opN* are comparison operators, then "a op1 b op2 c ' |
| 1888 |
'... y\n' |
| 1889 |
'opN z" is equivalent to "a op1 b and b op2 c and ... y opN ' |
| 1890 |
'z", except\n' |
| 1891 |
'that each expression is evaluated at most once.\n' |
| 1892 |
'\n' |
| 1893 |
'Note that "a op1 b op2 c" doesn’t imply any kind of ' |
| 1894 |
'comparison between\n' |
| 1895 |
'*a* and *c*, so that, e.g., "x < y > z" is perfectly legal ' |
| 1896 |
'(though\n' |
| 1897 |
'perhaps not pretty).\n' |
| 1898 |
'\n' |
| 1899 |
'\n' |
| 1900 |
'Value comparisons\n' |
| 1901 |
'=================\n' |
| 1902 |
'\n' |
| 1903 |
'The operators "<", ">", "==", ">=", "<=", and "!=" compare ' |
| 1904 |
'the values\n' |
| 1905 |
'of two objects. The objects do not need to have the same ' |
| 1906 |
'type.\n' |
| 1907 |
'\n' |
| 1908 |
'Chapter Objects, values and types states that objects have a ' |
| 1909 |
'value (in\n' |
| 1910 |
'addition to type and identity). The value of an object is a ' |
| 1911 |
'rather\n' |
| 1912 |
'abstract notion in Python: For example, there is no canonical ' |
| 1913 |
'access\n' |
| 1914 |
'method for an object’s value. Also, there is no requirement ' |
| 1915 |
'that the\n' |
| 1916 |
'value of an object should be constructed in a particular way, ' |
| 1917 |
'e.g.\n' |
| 1918 |
'comprised of all its data attributes. Comparison operators ' |
| 1919 |
'implement a\n' |
| 1920 |
'particular notion of what the value of an object is. One can ' |
| 1921 |
'think of\n' |
| 1922 |
'them as defining the value of an object indirectly, by means ' |
| 1923 |
'of their\n' |
| 1924 |
'comparison implementation.\n' |
| 1925 |
'\n' |
| 1926 |
'Because all types are (direct or indirect) subtypes of ' |
| 1927 |
'"object", they\n' |
| 1928 |
'inherit the default comparison behavior from "object". Types ' |
| 1929 |
'can\n' |
| 1930 |
'customize their comparison behavior by implementing *rich ' |
| 1931 |
'comparison\n' |
| 1932 |
'methods* like "__lt__()", described in Basic customization.\n' |
| 1933 |
'\n' |
| 1934 |
'The default behavior for equality comparison ("==" and "!=") ' |
| 1935 |
'is based\n' |
| 1936 |
'on the identity of the objects. Hence, equality comparison ' |
| 1937 |
'of\n' |
| 1938 |
'instances with the same identity results in equality, and ' |
| 1939 |
'equality\n' |
| 1940 |
'comparison of instances with different identities results in\n' |
| 1941 |
'inequality. A motivation for this default behavior is the ' |
| 1942 |
'desire that\n' |
| 1943 |
'all objects should be reflexive (i.e. "x is y" implies "x == ' |
| 1944 |
'y").\n' |
| 1945 |
'\n' |
| 1946 |
'A default order comparison ("<", ">", "<=", and ">=") is not ' |
| 1947 |
'provided;\n' |
| 1948 |
'an attempt raises "TypeError". A motivation for this default ' |
| 1949 |
'behavior\n' |
| 1950 |
'is the lack of a similar invariant as for equality.\n' |
| 1951 |
'\n' |
| 1952 |
'The behavior of the default equality comparison, that ' |
| 1953 |
'instances with\n' |
| 1954 |
'different identities are always unequal, may be in contrast ' |
| 1955 |
'to what\n' |
| 1956 |
'types will need that have a sensible definition of object ' |
| 1957 |
'value and\n' |
| 1958 |
'value-based equality. Such types will need to customize ' |
| 1959 |
'their\n' |
| 1960 |
'comparison behavior, and in fact, a number of built-in types ' |
| 1961 |
'have done\n' |
| 1962 |
'that.\n' |
| 1963 |
'\n' |
| 1964 |
'The following list describes the comparison behavior of the ' |
| 1965 |
'most\n' |
| 1966 |
'important built-in types.\n' |
| 1967 |
'\n' |
| 1968 |
'* Numbers of built-in numeric types (Numeric Types — int, ' |
| 1969 |
'float,\n' |
| 1970 |
' complex) and of the standard library types ' |
| 1971 |
'"fractions.Fraction" and\n' |
| 1972 |
' "decimal.Decimal" can be compared within and across their ' |
| 1973 |
'types,\n' |
| 1974 |
' with the restriction that complex numbers do not support ' |
| 1975 |
'order\n' |
| 1976 |
' comparison. Within the limits of the types involved, they ' |
| 1977 |
'compare\n' |
| 1978 |
' mathematically (algorithmically) correct without loss of ' |
| 1979 |
'precision.\n' |
| 1980 |
'\n' |
| 1981 |
' The not-a-number values "float(\'NaN\')" and ' |
| 1982 |
'"decimal.Decimal(\'NaN\')"\n' |
| 1983 |
' are special. Any ordered comparison of a number to a ' |
| 1984 |
'not-a-number\n' |
| 1985 |
' value is false. A counter-intuitive implication is that ' |
| 1986 |
'not-a-number\n' |
| 1987 |
' values are not equal to themselves. For example, if "x =\n' |
| 1988 |
' float(\'NaN\')", "3 < x", "x < 3" and "x == x" are all ' |
| 1989 |
'false, while "x\n' |
| 1990 |
' != x" is true. This behavior is compliant with IEEE 754.\n' |
| 1991 |
'\n' |
| 1992 |
'* "None" and "NotImplemented" are singletons. **PEP 8** ' |
| 1993 |
'advises that\n' |
| 1994 |
' comparisons for singletons should always be done with "is" ' |
| 1995 |
'or "is\n' |
| 1996 |
' not", never the equality operators.\n' |
| 1997 |
'\n' |
| 1998 |
'* Binary sequences (instances of "bytes" or "bytearray") can ' |
| 1999 |
'be\n' |
| 2000 |
' compared within and across their types. They compare\n' |
| 2001 |
' lexicographically using the numeric values of their ' |
| 2002 |
'elements.\n' |
| 2003 |
'\n' |
| 2004 |
'* Strings (instances of "str") compare lexicographically ' |
| 2005 |
'using the\n' |
| 2006 |
' numerical Unicode code points (the result of the built-in ' |
| 2007 |
'function\n' |
| 2008 |
' "ord()") of their characters. [3]\n' |
| 2009 |
'\n' |
| 2010 |
' Strings and binary sequences cannot be directly compared.\n' |
| 2011 |
'\n' |
| 2012 |
'* Sequences (instances of "tuple", "list", or "range") can be ' |
| 2013 |
'compared\n' |
| 2014 |
' only within each of their types, with the restriction that ' |
| 2015 |
'ranges do\n' |
| 2016 |
' not support order comparison. Equality comparison across ' |
| 2017 |
'these\n' |
| 2018 |
' types results in inequality, and ordering comparison across ' |
| 2019 |
'these\n' |
| 2020 |
' types raises "TypeError".\n' |
| 2021 |
'\n' |
| 2022 |
' Sequences compare lexicographically using comparison of\n' |
| 2023 |
' corresponding elements. The built-in containers typically ' |
| 2024 |
'assume\n' |
| 2025 |
' identical objects are equal to themselves. That lets them ' |
| 2026 |
'bypass\n' |
| 2027 |
' equality tests for identical objects to improve performance ' |
| 2028 |
'and to\n' |
| 2029 |
' maintain their internal invariants.\n' |
| 2030 |
'\n' |
| 2031 |
' Lexicographical comparison between built-in collections ' |
| 2032 |
'works as\n' |
| 2033 |
' follows:\n' |
| 2034 |
'\n' |
| 2035 |
' * For two collections to compare equal, they must be of the ' |
| 2036 |
'same\n' |
| 2037 |
' type, have the same length, and each pair of ' |
| 2038 |
'corresponding\n' |
| 2039 |
' elements must compare equal (for example, "[1,2] == ' |
| 2040 |
'(1,2)" is\n' |
| 2041 |
' false because the type is not the same).\n' |
| 2042 |
'\n' |
| 2043 |
' * Collections that support order comparison are ordered the ' |
| 2044 |
'same as\n' |
| 2045 |
' their first unequal elements (for example, "[1,2,x] <= ' |
| 2046 |
'[1,2,y]"\n' |
| 2047 |
' has the same value as "x <= y"). If a corresponding ' |
| 2048 |
'element does\n' |
| 2049 |
' not exist, the shorter collection is ordered first (for ' |
| 2050 |
'example,\n' |
| 2051 |
' "[1,2] < [1,2,3]" is true).\n' |
| 2052 |
'\n' |
| 2053 |
'* Mappings (instances of "dict") compare equal if and only if ' |
| 2054 |
'they\n' |
| 2055 |
' have equal "(key, value)" pairs. Equality comparison of the ' |
| 2056 |
'keys and\n' |
| 2057 |
' values enforces reflexivity.\n' |
| 2058 |
'\n' |
| 2059 |
' Order comparisons ("<", ">", "<=", and ">=") raise ' |
| 2060 |
'"TypeError".\n' |
| 2061 |
'\n' |
| 2062 |
'* Sets (instances of "set" or "frozenset") can be compared ' |
| 2063 |
'within and\n' |
| 2064 |
' across their types.\n' |
| 2065 |
'\n' |
| 2066 |
' They define order comparison operators to mean subset and ' |
| 2067 |
'superset\n' |
| 2068 |
' tests. Those relations do not define total orderings (for ' |
| 2069 |
'example,\n' |
| 2070 |
' the two sets "{1,2}" and "{2,3}" are not equal, nor subsets ' |
| 2071 |
'of one\n' |
| 2072 |
' another, nor supersets of one another). Accordingly, sets ' |
| 2073 |
'are not\n' |
| 2074 |
' appropriate arguments for functions which depend on total ' |
| 2075 |
'ordering\n' |
| 2076 |
' (for example, "min()", "max()", and "sorted()" produce ' |
| 2077 |
'undefined\n' |
| 2078 |
' results given a list of sets as inputs).\n' |
| 2079 |
'\n' |
| 2080 |
' Comparison of sets enforces reflexivity of its elements.\n' |
| 2081 |
'\n' |
| 2082 |
'* Most other built-in types have no comparison methods ' |
| 2083 |
'implemented, so\n' |
| 2084 |
' they inherit the default comparison behavior.\n' |
| 2085 |
'\n' |
| 2086 |
'User-defined classes that customize their comparison behavior ' |
| 2087 |
'should\n' |
| 2088 |
'follow some consistency rules, if possible:\n' |
| 2089 |
'\n' |
| 2090 |
'* Equality comparison should be reflexive. In other words, ' |
| 2091 |
'identical\n' |
| 2092 |
' objects should compare equal:\n' |
| 2093 |
'\n' |
| 2094 |
' "x is y" implies "x == y"\n' |
| 2095 |
'\n' |
| 2096 |
'* Comparison should be symmetric. In other words, the ' |
| 2097 |
'following\n' |
| 2098 |
' expressions should have the same result:\n' |
| 2099 |
'\n' |
| 2100 |
' "x == y" and "y == x"\n' |
| 2101 |
'\n' |
| 2102 |
' "x != y" and "y != x"\n' |
| 2103 |
'\n' |
| 2104 |
' "x < y" and "y > x"\n' |
| 2105 |
'\n' |
| 2106 |
' "x <= y" and "y >= x"\n' |
| 2107 |
'\n' |
| 2108 |
'* Comparison should be transitive. The following ' |
| 2109 |
'(non-exhaustive)\n' |
| 2110 |
' examples illustrate that:\n' |
| 2111 |
'\n' |
| 2112 |
' "x > y and y > z" implies "x > z"\n' |
| 2113 |
'\n' |
| 2114 |
' "x < y and y <= z" implies "x < z"\n' |
| 2115 |
'\n' |
| 2116 |
'* Inverse comparison should result in the boolean negation. ' |
| 2117 |
'In other\n' |
| 2118 |
' words, the following expressions should have the same ' |
| 2119 |
'result:\n' |
| 2120 |
'\n' |
| 2121 |
' "x == y" and "not x != y"\n' |
| 2122 |
'\n' |
| 2123 |
' "x < y" and "not x >= y" (for total ordering)\n' |
| 2124 |
'\n' |
| 2125 |
' "x > y" and "not x <= y" (for total ordering)\n' |
| 2126 |
'\n' |
| 2127 |
' The last two expressions apply to totally ordered ' |
| 2128 |
'collections (e.g.\n' |
| 2129 |
' to sequences, but not to sets or mappings). See also the\n' |
| 2130 |
' "total_ordering()" decorator.\n' |
| 2131 |
'\n' |
| 2132 |
'* The "hash()" result should be consistent with equality. ' |
| 2133 |
'Objects that\n' |
| 2134 |
' are equal should either have the same hash value, or be ' |
| 2135 |
'marked as\n' |
| 2136 |
' unhashable.\n' |
| 2137 |
'\n' |
| 2138 |
'Python does not enforce these consistency rules. In fact, ' |
| 2139 |
'the\n' |
| 2140 |
'not-a-number values are an example for not following these ' |
| 2141 |
'rules.\n' |
| 2142 |
'\n' |
| 2143 |
'\n' |
| 2144 |
'Membership test operations\n' |
| 2145 |
'==========================\n' |
| 2146 |
'\n' |
| 2147 |
'The operators "in" and "not in" test for membership. "x in ' |
| 2148 |
's"\n' |
| 2149 |
'evaluates to "True" if *x* is a member of *s*, and "False" ' |
| 2150 |
'otherwise.\n' |
| 2151 |
'"x not in s" returns the negation of "x in s". All built-in ' |
| 2152 |
'sequences\n' |
| 2153 |
'and set types support this as well as dictionary, for which ' |
| 2154 |
'"in" tests\n' |
| 2155 |
'whether the dictionary has a given key. For container types ' |
| 2156 |
'such as\n' |
| 2157 |
'list, tuple, set, frozenset, dict, or collections.deque, the\n' |
| 2158 |
'expression "x in y" is equivalent to "any(x is e or x == e ' |
| 2159 |
'for e in\n' |
| 2160 |
'y)".\n' |
| 2161 |
'\n' |
| 2162 |
'For the string and bytes types, "x in y" is "True" if and ' |
| 2163 |
'only if *x*\n' |
| 2164 |
'is a substring of *y*. An equivalent test is "y.find(x) != ' |
| 2165 |
'-1".\n' |
| 2166 |
'Empty strings are always considered to be a substring of any ' |
| 2167 |
'other\n' |
| 2168 |
'string, so """ in "abc"" will return "True".\n' |
| 2169 |
'\n' |
| 2170 |
'For user-defined classes which define the "__contains__()" ' |
| 2171 |
'method, "x\n' |
| 2172 |
'in y" returns "True" if "y.__contains__(x)" returns a true ' |
| 2173 |
'value, and\n' |
| 2174 |
'"False" otherwise.\n' |
| 2175 |
'\n' |
| 2176 |
'For user-defined classes which do not define "__contains__()" ' |
| 2177 |
'but do\n' |
| 2178 |
'define "__iter__()", "x in y" is "True" if some value "z", ' |
| 2179 |
'for which\n' |
| 2180 |
'the expression "x is z or x == z" is true, is produced while ' |
| 2181 |
'iterating\n' |
| 2182 |
'over "y". If an exception is raised during the iteration, it ' |
| 2183 |
'is as if\n' |
| 2184 |
'"in" raised that exception.\n' |
| 2185 |
'\n' |
| 2186 |
'Lastly, the old-style iteration protocol is tried: if a class ' |
| 2187 |
'defines\n' |
| 2188 |
'"__getitem__()", "x in y" is "True" if and only if there is a ' |
| 2189 |
'non-\n' |
| 2190 |
'negative integer index *i* such that "x is y[i] or x == ' |
| 2191 |
'y[i]", and no\n' |
| 2192 |
'lower integer index raises the "IndexError" exception. (If ' |
| 2193 |
'any other\n' |
| 2194 |
'exception is raised, it is as if "in" raised that ' |
| 2195 |
'exception).\n' |
| 2196 |
'\n' |
| 2197 |
'The operator "not in" is defined to have the inverse truth ' |
| 2198 |
'value of\n' |
| 2199 |
'"in".\n' |
| 2200 |
'\n' |
| 2201 |
'\n' |
| 2202 |
'Identity comparisons\n' |
| 2203 |
'====================\n' |
| 2204 |
'\n' |
| 2205 |
'The operators "is" and "is not" test for an object’s ' |
| 2206 |
'identity: "x is\n' |
| 2207 |
'y" is true if and only if *x* and *y* are the same object. ' |
| 2208 |
'An\n' |
| 2209 |
'Object’s identity is determined using the "id()" function. ' |
| 2210 |
'"x is not\n' |
| 2211 |
'y" yields the inverse truth value. [4]\n', |
| 2212 |
'compound': 'Compound statements\n' |
| 2213 |
'*******************\n' |
| 2214 |
'\n' |
| 2215 |
'Compound statements contain (groups of) other statements; they ' |
| 2216 |
'affect\n' |
| 2217 |
'or control the execution of those other statements in some way. ' |
| 2218 |
'In\n' |
| 2219 |
'general, compound statements span multiple lines, although in ' |
| 2220 |
'simple\n' |
| 2221 |
'incarnations a whole compound statement may be contained in one ' |
| 2222 |
'line.\n' |
| 2223 |
'\n' |
| 2224 |
'The "if", "while" and "for" statements implement traditional ' |
| 2225 |
'control\n' |
| 2226 |
'flow constructs. "try" specifies exception handlers and/or ' |
| 2227 |
'cleanup\n' |
| 2228 |
'code for a group of statements, while the "with" statement ' |
| 2229 |
'allows the\n' |
| 2230 |
'execution of initialization and finalization code around a block ' |
| 2231 |
'of\n' |
| 2232 |
'code. Function and class definitions are also syntactically ' |
| 2233 |
'compound\n' |
| 2234 |
'statements.\n' |
| 2235 |
'\n' |
| 2236 |
'A compound statement consists of one or more ‘clauses.’ A ' |
| 2237 |
'clause\n' |
| 2238 |
'consists of a header and a ‘suite.’ The clause headers of a\n' |
| 2239 |
'particular compound statement are all at the same indentation ' |
| 2240 |
'level.\n' |
| 2241 |
'Each clause header begins with a uniquely identifying keyword ' |
| 2242 |
'and ends\n' |
| 2243 |
'with a colon. A suite is a group of statements controlled by a\n' |
| 2244 |
'clause. A suite can be one or more semicolon-separated simple\n' |
| 2245 |
'statements on the same line as the header, following the ' |
| 2246 |
'header’s\n' |
| 2247 |
'colon, or it can be one or more indented statements on ' |
| 2248 |
'subsequent\n' |
| 2249 |
'lines. Only the latter form of a suite can contain nested ' |
| 2250 |
'compound\n' |
| 2251 |
'statements; the following is illegal, mostly because it wouldn’t ' |
| 2252 |
'be\n' |
| 2253 |
'clear to which "if" clause a following "else" clause would ' |
| 2254 |
'belong:\n' |
| 2255 |
'\n' |
| 2256 |
' if test1: if test2: print(x)\n' |
| 2257 |
'\n' |
| 2258 |
'Also note that the semicolon binds tighter than the colon in ' |
| 2259 |
'this\n' |
| 2260 |
'context, so that in the following example, either all or none of ' |
| 2261 |
'the\n' |
| 2262 |
'"print()" calls are executed:\n' |
| 2263 |
'\n' |
| 2264 |
' if x < y < z: print(x); print(y); print(z)\n' |
| 2265 |
'\n' |
| 2266 |
'Summarizing:\n' |
| 2267 |
'\n' |
| 2268 |
' compound_stmt ::= if_stmt\n' |
| 2269 |
' | while_stmt\n' |
| 2270 |
' | for_stmt\n' |
| 2271 |
' | try_stmt\n' |
| 2272 |
' | with_stmt\n' |
| 2273 |
' | match_stmt\n' |
| 2274 |
' | funcdef\n' |
| 2275 |
' | classdef\n' |
| 2276 |
' | async_with_stmt\n' |
| 2277 |
' | async_for_stmt\n' |
| 2278 |
' | async_funcdef\n' |
| 2279 |
' suite ::= stmt_list NEWLINE | NEWLINE INDENT ' |
| 2280 |
'statement+ DEDENT\n' |
| 2281 |
' statement ::= stmt_list NEWLINE | compound_stmt\n' |
| 2282 |
' stmt_list ::= simple_stmt (";" simple_stmt)* [";"]\n' |
| 2283 |
'\n' |
| 2284 |
'Note that statements always end in a "NEWLINE" possibly followed ' |
| 2285 |
'by a\n' |
| 2286 |
'"DEDENT". Also note that optional continuation clauses always ' |
| 2287 |
'begin\n' |
| 2288 |
'with a keyword that cannot start a statement, thus there are no\n' |
| 2289 |
'ambiguities (the ‘dangling "else"’ problem is solved in Python ' |
| 2290 |
'by\n' |
| 2291 |
'requiring nested "if" statements to be indented).\n' |
| 2292 |
'\n' |
| 2293 |
'The formatting of the grammar rules in the following sections ' |
| 2294 |
'places\n' |
| 2295 |
'each clause on a separate line for clarity.\n' |
| 2296 |
'\n' |
| 2297 |
'\n' |
| 2298 |
'The "if" statement\n' |
| 2299 |
'==================\n' |
| 2300 |
'\n' |
| 2301 |
'The "if" statement is used for conditional execution:\n' |
| 2302 |
'\n' |
| 2303 |
' if_stmt ::= "if" assignment_expression ":" suite\n' |
| 2304 |
' ("elif" assignment_expression ":" suite)*\n' |
| 2305 |
' ["else" ":" suite]\n' |
| 2306 |
'\n' |
| 2307 |
'It selects exactly one of the suites by evaluating the ' |
| 2308 |
'expressions one\n' |
| 2309 |
'by one until one is found to be true (see section Boolean ' |
| 2310 |
'operations\n' |
| 2311 |
'for the definition of true and false); then that suite is ' |
| 2312 |
'executed\n' |
| 2313 |
'(and no other part of the "if" statement is executed or ' |
| 2314 |
'evaluated).\n' |
| 2315 |
'If all expressions are false, the suite of the "else" clause, ' |
| 2316 |
'if\n' |
| 2317 |
'present, is executed.\n' |
| 2318 |
'\n' |
| 2319 |
'\n' |
| 2320 |
'The "while" statement\n' |
| 2321 |
'=====================\n' |
| 2322 |
'\n' |
| 2323 |
'The "while" statement is used for repeated execution as long as ' |
| 2324 |
'an\n' |
| 2325 |
'expression is true:\n' |
| 2326 |
'\n' |
| 2327 |
' while_stmt ::= "while" assignment_expression ":" suite\n' |
| 2328 |
' ["else" ":" suite]\n' |
| 2329 |
'\n' |
| 2330 |
'This repeatedly tests the expression and, if it is true, ' |
| 2331 |
'executes the\n' |
| 2332 |
'first suite; if the expression is false (which may be the first ' |
| 2333 |
'time\n' |
| 2334 |
'it is tested) the suite of the "else" clause, if present, is ' |
| 2335 |
'executed\n' |
| 2336 |
'and the loop terminates.\n' |
| 2337 |
'\n' |
| 2338 |
'A "break" statement executed in the first suite terminates the ' |
| 2339 |
'loop\n' |
| 2340 |
'without executing the "else" clause’s suite. A "continue" ' |
| 2341 |
'statement\n' |
| 2342 |
'executed in the first suite skips the rest of the suite and goes ' |
| 2343 |
'back\n' |
| 2344 |
'to testing the expression.\n' |
| 2345 |
'\n' |
| 2346 |
'\n' |
| 2347 |
'The "for" statement\n' |
| 2348 |
'===================\n' |
| 2349 |
'\n' |
| 2350 |
'The "for" statement is used to iterate over the elements of a ' |
| 2351 |
'sequence\n' |
| 2352 |
'(such as a string, tuple or list) or other iterable object:\n' |
| 2353 |
'\n' |
| 2354 |
' for_stmt ::= "for" target_list "in" starred_list ":" suite\n' |
| 2355 |
' ["else" ":" suite]\n' |
| 2356 |
'\n' |
| 2357 |
'The "starred_list" expression is evaluated once; it should yield ' |
| 2358 |
'an\n' |
| 2359 |
'*iterable* object. An *iterator* is created for that iterable. ' |
| 2360 |
'The\n' |
| 2361 |
'first item provided by the iterator is then assigned to the ' |
| 2362 |
'target\n' |
| 2363 |
'list using the standard rules for assignments (see Assignment\n' |
| 2364 |
'statements), and the suite is executed. This repeats for each ' |
| 2365 |
'item\n' |
| 2366 |
'provided by the iterator. When the iterator is exhausted, the ' |
| 2367 |
'suite\n' |
| 2368 |
'in the "else" clause, if present, is executed, and the loop\n' |
| 2369 |
'terminates.\n' |
| 2370 |
'\n' |
| 2371 |
'A "break" statement executed in the first suite terminates the ' |
| 2372 |
'loop\n' |
| 2373 |
'without executing the "else" clause’s suite. A "continue" ' |
| 2374 |
'statement\n' |
| 2375 |
'executed in the first suite skips the rest of the suite and ' |
| 2376 |
'continues\n' |
| 2377 |
'with the next item, or with the "else" clause if there is no ' |
| 2378 |
'next\n' |
| 2379 |
'item.\n' |
| 2380 |
'\n' |
| 2381 |
'The for-loop makes assignments to the variables in the target ' |
| 2382 |
'list.\n' |
| 2383 |
'This overwrites all previous assignments to those variables ' |
| 2384 |
'including\n' |
| 2385 |
'those made in the suite of the for-loop:\n' |
| 2386 |
'\n' |
| 2387 |
' for i in range(10):\n' |
| 2388 |
' print(i)\n' |
| 2389 |
' i = 5 # this will not affect the for-loop\n' |
| 2390 |
' # because i will be overwritten with ' |
| 2391 |
'the next\n' |
| 2392 |
' # index in the range\n' |
| 2393 |
'\n' |
| 2394 |
'Names in the target list are not deleted when the loop is ' |
| 2395 |
'finished,\n' |
| 2396 |
'but if the sequence is empty, they will not have been assigned ' |
| 2397 |
'to at\n' |
| 2398 |
'all by the loop. Hint: the built-in type "range()" represents\n' |
| 2399 |
'immutable arithmetic sequences of integers. For instance, ' |
| 2400 |
'iterating\n' |
| 2401 |
'"range(3)" successively yields 0, 1, and then 2.\n' |
| 2402 |
'\n' |
| 2403 |
'Changed in version 3.11: Starred elements are now allowed in ' |
| 2404 |
'the\n' |
| 2405 |
'expression list.\n' |
| 2406 |
'\n' |
| 2407 |
'\n' |
| 2408 |
'The "try" statement\n' |
| 2409 |
'===================\n' |
| 2410 |
'\n' |
| 2411 |
'The "try" statement specifies exception handlers and/or cleanup ' |
| 2412 |
'code\n' |
| 2413 |
'for a group of statements:\n' |
| 2414 |
'\n' |
| 2415 |
' try_stmt ::= try1_stmt | try2_stmt | try3_stmt\n' |
| 2416 |
' try1_stmt ::= "try" ":" suite\n' |
| 2417 |
' ("except" [expression ["as" identifier]] ":" ' |
| 2418 |
'suite)+\n' |
| 2419 |
' ["else" ":" suite]\n' |
| 2420 |
' ["finally" ":" suite]\n' |
| 2421 |
' try2_stmt ::= "try" ":" suite\n' |
| 2422 |
' ("except" "*" expression ["as" identifier] ":" ' |
| 2423 |
'suite)+\n' |
| 2424 |
' ["else" ":" suite]\n' |
| 2425 |
' ["finally" ":" suite]\n' |
| 2426 |
' try3_stmt ::= "try" ":" suite\n' |
| 2427 |
' "finally" ":" suite\n' |
| 2428 |
'\n' |
| 2429 |
'Additional information on exceptions can be found in section\n' |
| 2430 |
'Exceptions, and information on using the "raise" statement to ' |
| 2431 |
'generate\n' |
| 2432 |
'exceptions may be found in section The raise statement.\n' |
| 2433 |
'\n' |
| 2434 |
'\n' |
| 2435 |
'"except" clause\n' |
| 2436 |
'---------------\n' |
| 2437 |
'\n' |
| 2438 |
'The "except" clause(s) specify one or more exception handlers. ' |
| 2439 |
'When no\n' |
| 2440 |
'exception occurs in the "try" clause, no exception handler is\n' |
| 2441 |
'executed. When an exception occurs in the "try" suite, a search ' |
| 2442 |
'for an\n' |
| 2443 |
'exception handler is started. This search inspects the "except"\n' |
| 2444 |
'clauses in turn until one is found that matches the exception. ' |
| 2445 |
'An\n' |
| 2446 |
'expression-less "except" clause, if present, must be last; it ' |
| 2447 |
'matches\n' |
| 2448 |
'any exception. For an "except" clause with an expression, that\n' |
| 2449 |
'expression is evaluated, and the clause matches the exception if ' |
| 2450 |
'the\n' |
| 2451 |
'resulting object is “compatible” with the exception. An object ' |
| 2452 |
'is\n' |
| 2453 |
'compatible with an exception if the object is the class or a ' |
| 2454 |
'*non-\n' |
| 2455 |
'virtual base class* of the exception object, or a tuple ' |
| 2456 |
'containing an\n' |
| 2457 |
'item that is the class or a non-virtual base class of the ' |
| 2458 |
'exception\n' |
| 2459 |
'object.\n' |
| 2460 |
'\n' |
| 2461 |
'If no "except" clause matches the exception, the search for an\n' |
| 2462 |
'exception handler continues in the surrounding code and on the\n' |
| 2463 |
'invocation stack. [1]\n' |
| 2464 |
'\n' |
| 2465 |
'If the evaluation of an expression in the header of an "except" ' |
| 2466 |
'clause\n' |
| 2467 |
'raises an exception, the original search for a handler is ' |
| 2468 |
'canceled and\n' |
| 2469 |
'a search starts for the new exception in the surrounding code ' |
| 2470 |
'and on\n' |
| 2471 |
'the call stack (it is treated as if the entire "try" statement ' |
| 2472 |
'raised\n' |
| 2473 |
'the exception).\n' |
| 2474 |
'\n' |
| 2475 |
'When a matching "except" clause is found, the exception is ' |
| 2476 |
'assigned to\n' |
| 2477 |
'the target specified after the "as" keyword in that "except" ' |
| 2478 |
'clause,\n' |
| 2479 |
'if present, and the "except" clause’s suite is executed. All ' |
| 2480 |
'"except"\n' |
| 2481 |
'clauses must have an executable block. When the end of this ' |
| 2482 |
'block is\n' |
| 2483 |
'reached, execution continues normally after the entire "try"\n' |
| 2484 |
'statement. (This means that if two nested handlers exist for the ' |
| 2485 |
'same\n' |
| 2486 |
'exception, and the exception occurs in the "try" clause of the ' |
| 2487 |
'inner\n' |
| 2488 |
'handler, the outer handler will not handle the exception.)\n' |
| 2489 |
'\n' |
| 2490 |
'When an exception has been assigned using "as target", it is ' |
| 2491 |
'cleared\n' |
| 2492 |
'at the end of the "except" clause. This is as if\n' |
| 2493 |
'\n' |
| 2494 |
' except E as N:\n' |
| 2495 |
' foo\n' |
| 2496 |
'\n' |
| 2497 |
'was translated to\n' |
| 2498 |
'\n' |
| 2499 |
' except E as N:\n' |
| 2500 |
' try:\n' |
| 2501 |
' foo\n' |
| 2502 |
' finally:\n' |
| 2503 |
' del N\n' |
| 2504 |
'\n' |
| 2505 |
'This means the exception must be assigned to a different name to ' |
| 2506 |
'be\n' |
| 2507 |
'able to refer to it after the "except" clause. Exceptions are ' |
| 2508 |
'cleared\n' |
| 2509 |
'because with the traceback attached to them, they form a ' |
| 2510 |
'reference\n' |
| 2511 |
'cycle with the stack frame, keeping all locals in that frame ' |
| 2512 |
'alive\n' |
| 2513 |
'until the next garbage collection occurs.\n' |
| 2514 |
'\n' |
| 2515 |
'Before an "except" clause’s suite is executed, the exception is ' |
| 2516 |
'stored\n' |
| 2517 |
'in the "sys" module, where it can be accessed from within the ' |
| 2518 |
'body of\n' |
| 2519 |
'the "except" clause by calling "sys.exception()". When leaving ' |
| 2520 |
'an\n' |
| 2521 |
'exception handler, the exception stored in the "sys" module is ' |
| 2522 |
'reset\n' |
| 2523 |
'to its previous value:\n' |
| 2524 |
'\n' |
| 2525 |
' >>> print(sys.exception())\n' |
| 2526 |
' None\n' |
| 2527 |
' >>> try:\n' |
| 2528 |
' ... raise TypeError\n' |
| 2529 |
' ... except:\n' |
| 2530 |
' ... print(repr(sys.exception()))\n' |
| 2531 |
' ... try:\n' |
| 2532 |
' ... raise ValueError\n' |
| 2533 |
' ... except:\n' |
| 2534 |
' ... print(repr(sys.exception()))\n' |
| 2535 |
' ... print(repr(sys.exception()))\n' |
| 2536 |
' ...\n' |
| 2537 |
' TypeError()\n' |
| 2538 |
' ValueError()\n' |
| 2539 |
' TypeError()\n' |
| 2540 |
' >>> print(sys.exception())\n' |
| 2541 |
' None\n' |
| 2542 |
'\n' |
| 2543 |
'\n' |
| 2544 |
'"except*" clause\n' |
| 2545 |
'----------------\n' |
| 2546 |
'\n' |
| 2547 |
'The "except*" clause(s) are used for handling "ExceptionGroup"s. ' |
| 2548 |
'The\n' |
| 2549 |
'exception type for matching is interpreted as in the case of ' |
| 2550 |
'"except",\n' |
| 2551 |
'but in the case of exception groups we can have partial matches ' |
| 2552 |
'when\n' |
| 2553 |
'the type matches some of the exceptions in the group. This means ' |
| 2554 |
'that\n' |
| 2555 |
'multiple "except*" clauses can execute, each handling part of ' |
| 2556 |
'the\n' |
| 2557 |
'exception group. Each clause executes at most once and handles ' |
| 2558 |
'an\n' |
| 2559 |
'exception group of all matching exceptions. Each exception in ' |
| 2560 |
'the\n' |
| 2561 |
'group is handled by at most one "except*" clause, the first ' |
| 2562 |
'that\n' |
| 2563 |
'matches it.\n' |
| 2564 |
'\n' |
| 2565 |
' >>> try:\n' |
| 2566 |
' ... raise ExceptionGroup("eg",\n' |
| 2567 |
' ... [ValueError(1), TypeError(2), OSError(3), ' |
| 2568 |
'OSError(4)])\n' |
| 2569 |
' ... except* TypeError as e:\n' |
| 2570 |
" ... print(f'caught {type(e)} with nested " |
| 2571 |
"{e.exceptions}')\n" |
| 2572 |
' ... except* OSError as e:\n' |
| 2573 |
" ... print(f'caught {type(e)} with nested " |
| 2574 |
"{e.exceptions}')\n" |
| 2575 |
' ...\n' |
| 2576 |
" caught <class 'ExceptionGroup'> with nested (TypeError(2),)\n" |
| 2577 |
" caught <class 'ExceptionGroup'> with nested (OSError(3), " |
| 2578 |
'OSError(4))\n' |
| 2579 |
' + Exception Group Traceback (most recent call last):\n' |
| 2580 |
' | File "<stdin>", line 2, in <module>\n' |
| 2581 |
' | ExceptionGroup: eg\n' |
| 2582 |
' +-+---------------- 1 ----------------\n' |
| 2583 |
' | ValueError: 1\n' |
| 2584 |
' +------------------------------------\n' |
| 2585 |
'\n' |
| 2586 |
'Any remaining exceptions that were not handled by any "except*" ' |
| 2587 |
'clause\n' |
| 2588 |
'are re-raised at the end, combined into an exception group along ' |
| 2589 |
'with\n' |
| 2590 |
'all exceptions that were raised from within "except*" clauses.\n' |
| 2591 |
'\n' |
| 2592 |
'From version 3.11.4, when the entire "ExceptionGroup" is handled ' |
| 2593 |
'and\n' |
| 2594 |
'only one exception is raised from an "except*" clause, this ' |
| 2595 |
'exception\n' |
| 2596 |
'is no longer wrapped to form a new "ExceptionGroup".\n' |
| 2597 |
'\n' |
| 2598 |
'If the raised exception is not an exception group and its type ' |
| 2599 |
'matches\n' |
| 2600 |
'one of the "except*" clauses, it is caught and wrapped by an ' |
| 2601 |
'exception\n' |
| 2602 |
'group with an empty message string.\n' |
| 2603 |
'\n' |
| 2604 |
' >>> try:\n' |
| 2605 |
' ... raise BlockingIOError\n' |
| 2606 |
' ... except* BlockingIOError as e:\n' |
| 2607 |
' ... print(repr(e))\n' |
| 2608 |
' ...\n' |
| 2609 |
" ExceptionGroup('', (BlockingIOError()))\n" |
| 2610 |
'\n' |
| 2611 |
'An "except*" clause must have a matching type, and this type ' |
| 2612 |
'cannot be\n' |
| 2613 |
'a subclass of "BaseExceptionGroup". It is not possible to mix ' |
| 2614 |
'"except"\n' |
| 2615 |
'and "except*" in the same "try". "break", "continue" and ' |
| 2616 |
'"return"\n' |
| 2617 |
'cannot appear in an "except*" clause.\n' |
| 2618 |
'\n' |
| 2619 |
'\n' |
| 2620 |
'"else" clause\n' |
| 2621 |
'-------------\n' |
| 2622 |
'\n' |
| 2623 |
'The optional "else" clause is executed if the control flow ' |
| 2624 |
'leaves the\n' |
| 2625 |
'"try" suite, no exception was raised, and no "return", ' |
| 2626 |
'"continue", or\n' |
| 2627 |
'"break" statement was executed. Exceptions in the "else" clause ' |
| 2628 |
'are\n' |
| 2629 |
'not handled by the preceding "except" clauses.\n' |
| 2630 |
'\n' |
| 2631 |
'\n' |
| 2632 |
'"finally" clause\n' |
| 2633 |
'----------------\n' |
| 2634 |
'\n' |
| 2635 |
'If "finally" is present, it specifies a ‘cleanup’ handler. The ' |
| 2636 |
'"try"\n' |
| 2637 |
'clause is executed, including any "except" and "else" clauses. ' |
| 2638 |
'If an\n' |
| 2639 |
'exception occurs in any of the clauses and is not handled, the\n' |
| 2640 |
'exception is temporarily saved. The "finally" clause is ' |
| 2641 |
'executed. If\n' |
| 2642 |
'there is a saved exception it is re-raised at the end of the ' |
| 2643 |
'"finally"\n' |
| 2644 |
'clause. If the "finally" clause raises another exception, the ' |
| 2645 |
'saved\n' |
| 2646 |
'exception is set as the context of the new exception. If the ' |
| 2647 |
'"finally"\n' |
| 2648 |
'clause executes a "return", "break" or "continue" statement, the ' |
| 2649 |
'saved\n' |
| 2650 |
'exception is discarded:\n' |
| 2651 |
'\n' |
| 2652 |
' >>> def f():\n' |
| 2653 |
' ... try:\n' |
| 2654 |
' ... 1/0\n' |
| 2655 |
' ... finally:\n' |
| 2656 |
' ... return 42\n' |
| 2657 |
' ...\n' |
| 2658 |
' >>> f()\n' |
| 2659 |
' 42\n' |
| 2660 |
'\n' |
| 2661 |
'The exception information is not available to the program ' |
| 2662 |
'during\n' |
| 2663 |
'execution of the "finally" clause.\n' |
| 2664 |
'\n' |
| 2665 |
'When a "return", "break" or "continue" statement is executed in ' |
| 2666 |
'the\n' |
| 2667 |
'"try" suite of a "try"…"finally" statement, the "finally" clause ' |
| 2668 |
'is\n' |
| 2669 |
'also executed ‘on the way out.’\n' |
| 2670 |
'\n' |
| 2671 |
'The return value of a function is determined by the last ' |
| 2672 |
'"return"\n' |
| 2673 |
'statement executed. Since the "finally" clause always executes, ' |
| 2674 |
'a\n' |
| 2675 |
'"return" statement executed in the "finally" clause will always ' |
| 2676 |
'be the\n' |
| 2677 |
'last one executed:\n' |
| 2678 |
'\n' |
| 2679 |
' >>> def foo():\n' |
| 2680 |
' ... try:\n' |
| 2681 |
" ... return 'try'\n" |
| 2682 |
' ... finally:\n' |
| 2683 |
" ... return 'finally'\n" |
| 2684 |
' ...\n' |
| 2685 |
' >>> foo()\n' |
| 2686 |
" 'finally'\n" |
| 2687 |
'\n' |
| 2688 |
'Changed in version 3.8: Prior to Python 3.8, a "continue" ' |
| 2689 |
'statement\n' |
| 2690 |
'was illegal in the "finally" clause due to a problem with the\n' |
| 2691 |
'implementation.\n' |
| 2692 |
'\n' |
| 2693 |
'\n' |
| 2694 |
'The "with" statement\n' |
| 2695 |
'====================\n' |
| 2696 |
'\n' |
| 2697 |
'The "with" statement is used to wrap the execution of a block ' |
| 2698 |
'with\n' |
| 2699 |
'methods defined by a context manager (see section With ' |
| 2700 |
'Statement\n' |
| 2701 |
'Context Managers). This allows common "try"…"except"…"finally" ' |
| 2702 |
'usage\n' |
| 2703 |
'patterns to be encapsulated for convenient reuse.\n' |
| 2704 |
'\n' |
| 2705 |
' with_stmt ::= "with" ( "(" with_stmt_contents ","? ' |
| 2706 |
'")" | with_stmt_contents ) ":" suite\n' |
| 2707 |
' with_stmt_contents ::= with_item ("," with_item)*\n' |
| 2708 |
' with_item ::= expression ["as" target]\n' |
| 2709 |
'\n' |
| 2710 |
'The execution of the "with" statement with one “item” proceeds ' |
| 2711 |
'as\n' |
| 2712 |
'follows:\n' |
| 2713 |
'\n' |
| 2714 |
'1. The context expression (the expression given in the ' |
| 2715 |
'"with_item") is\n' |
| 2716 |
' evaluated to obtain a context manager.\n' |
| 2717 |
'\n' |
| 2718 |
'2. The context manager’s "__enter__()" is loaded for later use.\n' |
| 2719 |
'\n' |
| 2720 |
'3. The context manager’s "__exit__()" is loaded for later use.\n' |
| 2721 |
'\n' |
| 2722 |
'4. The context manager’s "__enter__()" method is invoked.\n' |
| 2723 |
'\n' |
| 2724 |
'5. If a target was included in the "with" statement, the return ' |
| 2725 |
'value\n' |
| 2726 |
' from "__enter__()" is assigned to it.\n' |
| 2727 |
'\n' |
| 2728 |
' Note:\n' |
| 2729 |
'\n' |
| 2730 |
' The "with" statement guarantees that if the "__enter__()" ' |
| 2731 |
'method\n' |
| 2732 |
' returns without an error, then "__exit__()" will always be\n' |
| 2733 |
' called. Thus, if an error occurs during the assignment to ' |
| 2734 |
'the\n' |
| 2735 |
' target list, it will be treated the same as an error ' |
| 2736 |
'occurring\n' |
| 2737 |
' within the suite would be. See step 7 below.\n' |
| 2738 |
'\n' |
| 2739 |
'6. The suite is executed.\n' |
| 2740 |
'\n' |
| 2741 |
'7. The context manager’s "__exit__()" method is invoked. If an\n' |
| 2742 |
' exception caused the suite to be exited, its type, value, ' |
| 2743 |
'and\n' |
| 2744 |
' traceback are passed as arguments to "__exit__()". Otherwise, ' |
| 2745 |
'three\n' |
| 2746 |
' "None" arguments are supplied.\n' |
| 2747 |
'\n' |
| 2748 |
' If the suite was exited due to an exception, and the return ' |
| 2749 |
'value\n' |
| 2750 |
' from the "__exit__()" method was false, the exception is ' |
| 2751 |
'reraised.\n' |
| 2752 |
' If the return value was true, the exception is suppressed, ' |
| 2753 |
'and\n' |
| 2754 |
' execution continues with the statement following the "with"\n' |
| 2755 |
' statement.\n' |
| 2756 |
'\n' |
| 2757 |
' If the suite was exited for any reason other than an ' |
| 2758 |
'exception, the\n' |
| 2759 |
' return value from "__exit__()" is ignored, and execution ' |
| 2760 |
'proceeds\n' |
| 2761 |
' at the normal location for the kind of exit that was taken.\n' |
| 2762 |
'\n' |
| 2763 |
'The following code:\n' |
| 2764 |
'\n' |
| 2765 |
' with EXPRESSION as TARGET:\n' |
| 2766 |
' SUITE\n' |
| 2767 |
'\n' |
| 2768 |
'is semantically equivalent to:\n' |
| 2769 |
'\n' |
| 2770 |
' manager = (EXPRESSION)\n' |
| 2771 |
' enter = type(manager).__enter__\n' |
| 2772 |
' exit = type(manager).__exit__\n' |
| 2773 |
' value = enter(manager)\n' |
| 2774 |
' hit_except = False\n' |
| 2775 |
'\n' |
| 2776 |
' try:\n' |
| 2777 |
' TARGET = value\n' |
| 2778 |
' SUITE\n' |
| 2779 |
' except:\n' |
| 2780 |
' hit_except = True\n' |
| 2781 |
' if not exit(manager, *sys.exc_info()):\n' |
| 2782 |
' raise\n' |
| 2783 |
' finally:\n' |
| 2784 |
' if not hit_except:\n' |
| 2785 |
' exit(manager, None, None, None)\n' |
| 2786 |
'\n' |
| 2787 |
'With more than one item, the context managers are processed as ' |
| 2788 |
'if\n' |
| 2789 |
'multiple "with" statements were nested:\n' |
| 2790 |
'\n' |
| 2791 |
' with A() as a, B() as b:\n' |
| 2792 |
' SUITE\n' |
| 2793 |
'\n' |
| 2794 |
'is semantically equivalent to:\n' |
| 2795 |
'\n' |
| 2796 |
' with A() as a:\n' |
| 2797 |
' with B() as b:\n' |
| 2798 |
' SUITE\n' |
| 2799 |
'\n' |
| 2800 |
'You can also write multi-item context managers in multiple lines ' |
| 2801 |
'if\n' |
| 2802 |
'the items are surrounded by parentheses. For example:\n' |
| 2803 |
'\n' |
| 2804 |
' with (\n' |
| 2805 |
' A() as a,\n' |
| 2806 |
' B() as b,\n' |
| 2807 |
' ):\n' |
| 2808 |
' SUITE\n' |
| 2809 |
'\n' |
| 2810 |
'Changed in version 3.1: Support for multiple context ' |
| 2811 |
'expressions.\n' |
| 2812 |
'\n' |
| 2813 |
'Changed in version 3.10: Support for using grouping parentheses ' |
| 2814 |
'to\n' |
| 2815 |
'break the statement in multiple lines.\n' |
| 2816 |
'\n' |
| 2817 |
'See also:\n' |
| 2818 |
'\n' |
| 2819 |
' **PEP 343** - The “with” statement\n' |
| 2820 |
' The specification, background, and examples for the Python ' |
| 2821 |
'"with"\n' |
| 2822 |
' statement.\n' |
| 2823 |
'\n' |
| 2824 |
'\n' |
| 2825 |
'The "match" statement\n' |
| 2826 |
'=====================\n' |
| 2827 |
'\n' |
| 2828 |
'New in version 3.10.\n' |
| 2829 |
'\n' |
| 2830 |
'The match statement is used for pattern matching. Syntax:\n' |
| 2831 |
'\n' |
| 2832 |
' match_stmt ::= \'match\' subject_expr ":" NEWLINE INDENT ' |
| 2833 |
'case_block+ DEDENT\n' |
| 2834 |
' subject_expr ::= star_named_expression "," ' |
| 2835 |
'star_named_expressions?\n' |
| 2836 |
' | named_expression\n' |
| 2837 |
' case_block ::= \'case\' patterns [guard] ":" block\n' |
| 2838 |
'\n' |
| 2839 |
'Note:\n' |
| 2840 |
'\n' |
| 2841 |
' This section uses single quotes to denote soft keywords.\n' |
| 2842 |
'\n' |
| 2843 |
'Pattern matching takes a pattern as input (following "case") and ' |
| 2844 |
'a\n' |
| 2845 |
'subject value (following "match"). The pattern (which may ' |
| 2846 |
'contain\n' |
| 2847 |
'subpatterns) is matched against the subject value. The outcomes ' |
| 2848 |
'are:\n' |
| 2849 |
'\n' |
| 2850 |
'* A match success or failure (also termed a pattern success or\n' |
| 2851 |
' failure).\n' |
| 2852 |
'\n' |
| 2853 |
'* Possible binding of matched values to a name. The ' |
| 2854 |
'prerequisites for\n' |
| 2855 |
' this are further discussed below.\n' |
| 2856 |
'\n' |
| 2857 |
'The "match" and "case" keywords are soft keywords.\n' |
| 2858 |
'\n' |
| 2859 |
'See also:\n' |
| 2860 |
'\n' |
| 2861 |
' * **PEP 634** – Structural Pattern Matching: Specification\n' |
| 2862 |
'\n' |
| 2863 |
' * **PEP 636** – Structural Pattern Matching: Tutorial\n' |
| 2864 |
'\n' |
| 2865 |
'\n' |
| 2866 |
'Overview\n' |
| 2867 |
'--------\n' |
| 2868 |
'\n' |
| 2869 |
'Here’s an overview of the logical flow of a match statement:\n' |
| 2870 |
'\n' |
| 2871 |
'1. The subject expression "subject_expr" is evaluated and a ' |
| 2872 |
'resulting\n' |
| 2873 |
' subject value obtained. If the subject expression contains a ' |
| 2874 |
'comma,\n' |
| 2875 |
' a tuple is constructed using the standard rules.\n' |
| 2876 |
'\n' |
| 2877 |
'2. Each pattern in a "case_block" is attempted to match with ' |
| 2878 |
'the\n' |
| 2879 |
' subject value. The specific rules for success or failure are\n' |
| 2880 |
' described below. The match attempt can also bind some or all ' |
| 2881 |
'of the\n' |
| 2882 |
' standalone names within the pattern. The precise pattern ' |
| 2883 |
'binding\n' |
| 2884 |
' rules vary per pattern type and are specified below. **Name\n' |
| 2885 |
' bindings made during a successful pattern match outlive the\n' |
| 2886 |
' executed block and can be used after the match statement**.\n' |
| 2887 |
'\n' |
| 2888 |
' Note:\n' |
| 2889 |
'\n' |
| 2890 |
' During failed pattern matches, some subpatterns may ' |
| 2891 |
'succeed. Do\n' |
| 2892 |
' not rely on bindings being made for a failed match. ' |
| 2893 |
'Conversely,\n' |
| 2894 |
' do not rely on variables remaining unchanged after a ' |
| 2895 |
'failed\n' |
| 2896 |
' match. The exact behavior is dependent on implementation ' |
| 2897 |
'and may\n' |
| 2898 |
' vary. This is an intentional decision made to allow ' |
| 2899 |
'different\n' |
| 2900 |
' implementations to add optimizations.\n' |
| 2901 |
'\n' |
| 2902 |
'3. If the pattern succeeds, the corresponding guard (if present) ' |
| 2903 |
'is\n' |
| 2904 |
' evaluated. In this case all name bindings are guaranteed to ' |
| 2905 |
'have\n' |
| 2906 |
' happened.\n' |
| 2907 |
'\n' |
| 2908 |
' * If the guard evaluates as true or is missing, the "block" ' |
| 2909 |
'inside\n' |
| 2910 |
' "case_block" is executed.\n' |
| 2911 |
'\n' |
| 2912 |
' * Otherwise, the next "case_block" is attempted as described ' |
| 2913 |
'above.\n' |
| 2914 |
'\n' |
| 2915 |
' * If there are no further case blocks, the match statement ' |
| 2916 |
'is\n' |
| 2917 |
' completed.\n' |
| 2918 |
'\n' |
| 2919 |
'Note:\n' |
| 2920 |
'\n' |
| 2921 |
' Users should generally never rely on a pattern being ' |
| 2922 |
'evaluated.\n' |
| 2923 |
' Depending on implementation, the interpreter may cache values ' |
| 2924 |
'or use\n' |
| 2925 |
' other optimizations which skip repeated evaluations.\n' |
| 2926 |
'\n' |
| 2927 |
'A sample match statement:\n' |
| 2928 |
'\n' |
| 2929 |
' >>> flag = False\n' |
| 2930 |
' >>> match (100, 200):\n' |
| 2931 |
' ... case (100, 300): # Mismatch: 200 != 300\n' |
| 2932 |
" ... print('Case 1')\n" |
| 2933 |
' ... case (100, 200) if flag: # Successful match, but ' |
| 2934 |
'guard fails\n' |
| 2935 |
" ... print('Case 2')\n" |
| 2936 |
' ... case (100, y): # Matches and binds y to 200\n' |
| 2937 |
" ... print(f'Case 3, y: {y}')\n" |
| 2938 |
' ... case _: # Pattern not attempted\n' |
| 2939 |
" ... print('Case 4, I match anything!')\n" |
| 2940 |
' ...\n' |
| 2941 |
' Case 3, y: 200\n' |
| 2942 |
'\n' |
| 2943 |
'In this case, "if flag" is a guard. Read more about that in the ' |
| 2944 |
'next\n' |
| 2945 |
'section.\n' |
| 2946 |
'\n' |
| 2947 |
'\n' |
| 2948 |
'Guards\n' |
| 2949 |
'------\n' |
| 2950 |
'\n' |
| 2951 |
' guard ::= "if" named_expression\n' |
| 2952 |
'\n' |
| 2953 |
'A "guard" (which is part of the "case") must succeed for code ' |
| 2954 |
'inside\n' |
| 2955 |
'the "case" block to execute. It takes the form: "if" followed ' |
| 2956 |
'by an\n' |
| 2957 |
'expression.\n' |
| 2958 |
'\n' |
| 2959 |
'The logical flow of a "case" block with a "guard" follows:\n' |
| 2960 |
'\n' |
| 2961 |
'1. Check that the pattern in the "case" block succeeded. If ' |
| 2962 |
'the\n' |
| 2963 |
' pattern failed, the "guard" is not evaluated and the next ' |
| 2964 |
'"case"\n' |
| 2965 |
' block is checked.\n' |
| 2966 |
'\n' |
| 2967 |
'2. If the pattern succeeded, evaluate the "guard".\n' |
| 2968 |
'\n' |
| 2969 |
' * If the "guard" condition evaluates as true, the case block ' |
| 2970 |
'is\n' |
| 2971 |
' selected.\n' |
| 2972 |
'\n' |
| 2973 |
' * If the "guard" condition evaluates as false, the case block ' |
| 2974 |
'is\n' |
| 2975 |
' not selected.\n' |
| 2976 |
'\n' |
| 2977 |
' * If the "guard" raises an exception during evaluation, the\n' |
| 2978 |
' exception bubbles up.\n' |
| 2979 |
'\n' |
| 2980 |
'Guards are allowed to have side effects as they are ' |
| 2981 |
'expressions.\n' |
| 2982 |
'Guard evaluation must proceed from the first to the last case ' |
| 2983 |
'block,\n' |
| 2984 |
'one at a time, skipping case blocks whose pattern(s) don’t all\n' |
| 2985 |
'succeed. (I.e., guard evaluation must happen in order.) Guard\n' |
| 2986 |
'evaluation must stop once a case block is selected.\n' |
| 2987 |
'\n' |
| 2988 |
'\n' |
| 2989 |
'Irrefutable Case Blocks\n' |
| 2990 |
'-----------------------\n' |
| 2991 |
'\n' |
| 2992 |
'An irrefutable case block is a match-all case block. A match\n' |
| 2993 |
'statement may have at most one irrefutable case block, and it ' |
| 2994 |
'must be\n' |
| 2995 |
'last.\n' |
| 2996 |
'\n' |
| 2997 |
'A case block is considered irrefutable if it has no guard and ' |
| 2998 |
'its\n' |
| 2999 |
'pattern is irrefutable. A pattern is considered irrefutable if ' |
| 3000 |
'we can\n' |
| 3001 |
'prove from its syntax alone that it will always succeed. Only ' |
| 3002 |
'the\n' |
| 3003 |
'following patterns are irrefutable:\n' |
| 3004 |
'\n' |
| 3005 |
'* AS Patterns whose left-hand side is irrefutable\n' |
| 3006 |
'\n' |
| 3007 |
'* OR Patterns containing at least one irrefutable pattern\n' |
| 3008 |
'\n' |
| 3009 |
'* Capture Patterns\n' |
| 3010 |
'\n' |
| 3011 |
'* Wildcard Patterns\n' |
| 3012 |
'\n' |
| 3013 |
'* parenthesized irrefutable patterns\n' |
| 3014 |
'\n' |
| 3015 |
'\n' |
| 3016 |
'Patterns\n' |
| 3017 |
'--------\n' |
| 3018 |
'\n' |
| 3019 |
'Note:\n' |
| 3020 |
'\n' |
| 3021 |
' This section uses grammar notations beyond standard EBNF:\n' |
| 3022 |
'\n' |
| 3023 |
' * the notation "SEP.RULE+" is shorthand for "RULE (SEP ' |
| 3024 |
'RULE)*"\n' |
| 3025 |
'\n' |
| 3026 |
' * the notation "!RULE" is shorthand for a negative lookahead\n' |
| 3027 |
' assertion\n' |
| 3028 |
'\n' |
| 3029 |
'The top-level syntax for "patterns" is:\n' |
| 3030 |
'\n' |
| 3031 |
' patterns ::= open_sequence_pattern | pattern\n' |
| 3032 |
' pattern ::= as_pattern | or_pattern\n' |
| 3033 |
' closed_pattern ::= | literal_pattern\n' |
| 3034 |
' | capture_pattern\n' |
| 3035 |
' | wildcard_pattern\n' |
| 3036 |
' | value_pattern\n' |
| 3037 |
' | group_pattern\n' |
| 3038 |
' | sequence_pattern\n' |
| 3039 |
' | mapping_pattern\n' |
| 3040 |
' | class_pattern\n' |
| 3041 |
'\n' |
| 3042 |
'The descriptions below will include a description “in simple ' |
| 3043 |
'terms” of\n' |
| 3044 |
'what a pattern does for illustration purposes (credits to ' |
| 3045 |
'Raymond\n' |
| 3046 |
'Hettinger for a document that inspired most of the ' |
| 3047 |
'descriptions). Note\n' |
| 3048 |
'that these descriptions are purely for illustration purposes and ' |
| 3049 |
'**may\n' |
| 3050 |
'not** reflect the underlying implementation. Furthermore, they ' |
| 3051 |
'do not\n' |
| 3052 |
'cover all valid forms.\n' |
| 3053 |
'\n' |
| 3054 |
'\n' |
| 3055 |
'OR Patterns\n' |
| 3056 |
'~~~~~~~~~~~\n' |
| 3057 |
'\n' |
| 3058 |
'An OR pattern is two or more patterns separated by vertical bars ' |
| 3059 |
'"|".\n' |
| 3060 |
'Syntax:\n' |
| 3061 |
'\n' |
| 3062 |
' or_pattern ::= "|".closed_pattern+\n' |
| 3063 |
'\n' |
| 3064 |
'Only the final subpattern may be irrefutable, and each ' |
| 3065 |
'subpattern must\n' |
| 3066 |
'bind the same set of names to avoid ambiguity.\n' |
| 3067 |
'\n' |
| 3068 |
'An OR pattern matches each of its subpatterns in turn to the ' |
| 3069 |
'subject\n' |
| 3070 |
'value, until one succeeds. The OR pattern is then considered\n' |
| 3071 |
'successful. Otherwise, if none of the subpatterns succeed, the ' |
| 3072 |
'OR\n' |
| 3073 |
'pattern fails.\n' |
| 3074 |
'\n' |
| 3075 |
'In simple terms, "P1 | P2 | ..." will try to match "P1", if it ' |
| 3076 |
'fails\n' |
| 3077 |
'it will try to match "P2", succeeding immediately if any ' |
| 3078 |
'succeeds,\n' |
| 3079 |
'failing otherwise.\n' |
| 3080 |
'\n' |
| 3081 |
'\n' |
| 3082 |
'AS Patterns\n' |
| 3083 |
'~~~~~~~~~~~\n' |
| 3084 |
'\n' |
| 3085 |
'An AS pattern matches an OR pattern on the left of the "as" ' |
| 3086 |
'keyword\n' |
| 3087 |
'against a subject. Syntax:\n' |
| 3088 |
'\n' |
| 3089 |
' as_pattern ::= or_pattern "as" capture_pattern\n' |
| 3090 |
'\n' |
| 3091 |
'If the OR pattern fails, the AS pattern fails. Otherwise, the ' |
| 3092 |
'AS\n' |
| 3093 |
'pattern binds the subject to the name on the right of the as ' |
| 3094 |
'keyword\n' |
| 3095 |
'and succeeds. "capture_pattern" cannot be a "_".\n' |
| 3096 |
'\n' |
| 3097 |
'In simple terms "P as NAME" will match with "P", and on success ' |
| 3098 |
'it\n' |
| 3099 |
'will set "NAME = <subject>".\n' |
| 3100 |
'\n' |
| 3101 |
'\n' |
| 3102 |
'Literal Patterns\n' |
| 3103 |
'~~~~~~~~~~~~~~~~\n' |
| 3104 |
'\n' |
| 3105 |
'A literal pattern corresponds to most literals in Python. ' |
| 3106 |
'Syntax:\n' |
| 3107 |
'\n' |
| 3108 |
' literal_pattern ::= signed_number\n' |
| 3109 |
' | signed_number "+" NUMBER\n' |
| 3110 |
' | signed_number "-" NUMBER\n' |
| 3111 |
' | strings\n' |
| 3112 |
' | "None"\n' |
| 3113 |
' | "True"\n' |
| 3114 |
' | "False"\n' |
| 3115 |
' | signed_number: NUMBER | "-" NUMBER\n' |
| 3116 |
'\n' |
| 3117 |
'The rule "strings" and the token "NUMBER" are defined in the ' |
| 3118 |
'standard\n' |
| 3119 |
'Python grammar. Triple-quoted strings are supported. Raw ' |
| 3120 |
'strings and\n' |
| 3121 |
'byte strings are supported. f-strings are not supported.\n' |
| 3122 |
'\n' |
| 3123 |
'The forms "signed_number \'+\' NUMBER" and "signed_number \'-\' ' |
| 3124 |
'NUMBER"\n' |
| 3125 |
'are for expressing complex numbers; they require a real number ' |
| 3126 |
'on the\n' |
| 3127 |
'left and an imaginary number on the right. E.g. "3 + 4j".\n' |
| 3128 |
'\n' |
| 3129 |
'In simple terms, "LITERAL" will succeed only if "<subject> ==\n' |
| 3130 |
'LITERAL". For the singletons "None", "True" and "False", the ' |
| 3131 |
'"is"\n' |
| 3132 |
'operator is used.\n' |
| 3133 |
'\n' |
| 3134 |
'\n' |
| 3135 |
'Capture Patterns\n' |
| 3136 |
'~~~~~~~~~~~~~~~~\n' |
| 3137 |
'\n' |
| 3138 |
'A capture pattern binds the subject value to a name. Syntax:\n' |
| 3139 |
'\n' |
| 3140 |
" capture_pattern ::= !'_' NAME\n" |
| 3141 |
'\n' |
| 3142 |
'A single underscore "_" is not a capture pattern (this is what ' |
| 3143 |
'"!\'_\'"\n' |
| 3144 |
'expresses). It is instead treated as a "wildcard_pattern".\n' |
| 3145 |
'\n' |
| 3146 |
'In a given pattern, a given name can only be bound once. E.g. ' |
| 3147 |
'"case\n' |
| 3148 |
'x, x: ..." is invalid while "case [x] | x: ..." is allowed.\n' |
| 3149 |
'\n' |
| 3150 |
'Capture patterns always succeed. The binding follows scoping ' |
| 3151 |
'rules\n' |
| 3152 |
'established by the assignment expression operator in **PEP ' |
| 3153 |
'572**; the\n' |
| 3154 |
'name becomes a local variable in the closest containing function ' |
| 3155 |
'scope\n' |
| 3156 |
'unless there’s an applicable "global" or "nonlocal" statement.\n' |
| 3157 |
'\n' |
| 3158 |
'In simple terms "NAME" will always succeed and it will set "NAME ' |
| 3159 |
'=\n' |
| 3160 |
'<subject>".\n' |
| 3161 |
'\n' |
| 3162 |
'\n' |
| 3163 |
'Wildcard Patterns\n' |
| 3164 |
'~~~~~~~~~~~~~~~~~\n' |
| 3165 |
'\n' |
| 3166 |
'A wildcard pattern always succeeds (matches anything) and binds ' |
| 3167 |
'no\n' |
| 3168 |
'name. Syntax:\n' |
| 3169 |
'\n' |
| 3170 |
" wildcard_pattern ::= '_'\n" |
| 3171 |
'\n' |
| 3172 |
'"_" is a soft keyword within any pattern, but only within ' |
| 3173 |
'patterns.\n' |
| 3174 |
'It is an identifier, as usual, even within "match" subject\n' |
| 3175 |
'expressions, "guard"s, and "case" blocks.\n' |
| 3176 |
'\n' |
| 3177 |
'In simple terms, "_" will always succeed.\n' |
| 3178 |
'\n' |
| 3179 |
'\n' |
| 3180 |
'Value Patterns\n' |
| 3181 |
'~~~~~~~~~~~~~~\n' |
| 3182 |
'\n' |
| 3183 |
'A value pattern represents a named value in Python. Syntax:\n' |
| 3184 |
'\n' |
| 3185 |
' value_pattern ::= attr\n' |
| 3186 |
' attr ::= name_or_attr "." NAME\n' |
| 3187 |
' name_or_attr ::= attr | NAME\n' |
| 3188 |
'\n' |
| 3189 |
'The dotted name in the pattern is looked up using standard ' |
| 3190 |
'Python name\n' |
| 3191 |
'resolution rules. The pattern succeeds if the value found ' |
| 3192 |
'compares\n' |
| 3193 |
'equal to the subject value (using the "==" equality operator).\n' |
| 3194 |
'\n' |
| 3195 |
'In simple terms "NAME1.NAME2" will succeed only if "<subject> ' |
| 3196 |
'==\n' |
| 3197 |
'NAME1.NAME2"\n' |
| 3198 |
'\n' |
| 3199 |
'Note:\n' |
| 3200 |
'\n' |
| 3201 |
' If the same value occurs multiple times in the same match ' |
| 3202 |
'statement,\n' |
| 3203 |
' the interpreter may cache the first value found and reuse it ' |
| 3204 |
'rather\n' |
| 3205 |
' than repeat the same lookup. This cache is strictly tied to a ' |
| 3206 |
'given\n' |
| 3207 |
' execution of a given match statement.\n' |
| 3208 |
'\n' |
| 3209 |
'\n' |
| 3210 |
'Group Patterns\n' |
| 3211 |
'~~~~~~~~~~~~~~\n' |
| 3212 |
'\n' |
| 3213 |
'A group pattern allows users to add parentheses around patterns ' |
| 3214 |
'to\n' |
| 3215 |
'emphasize the intended grouping. Otherwise, it has no ' |
| 3216 |
'additional\n' |
| 3217 |
'syntax. Syntax:\n' |
| 3218 |
'\n' |
| 3219 |
' group_pattern ::= "(" pattern ")"\n' |
| 3220 |
'\n' |
| 3221 |
'In simple terms "(P)" has the same effect as "P".\n' |
| 3222 |
'\n' |
| 3223 |
'\n' |
| 3224 |
'Sequence Patterns\n' |
| 3225 |
'~~~~~~~~~~~~~~~~~\n' |
| 3226 |
'\n' |
| 3227 |
'A sequence pattern contains several subpatterns to be matched ' |
| 3228 |
'against\n' |
| 3229 |
'sequence elements. The syntax is similar to the unpacking of a ' |
| 3230 |
'list or\n' |
| 3231 |
'tuple.\n' |
| 3232 |
'\n' |
| 3233 |
' sequence_pattern ::= "[" [maybe_sequence_pattern] "]"\n' |
| 3234 |
' | "(" [open_sequence_pattern] ")"\n' |
| 3235 |
' open_sequence_pattern ::= maybe_star_pattern "," ' |
| 3236 |
'[maybe_sequence_pattern]\n' |
| 3237 |
' maybe_sequence_pattern ::= ",".maybe_star_pattern+ ","?\n' |
| 3238 |
' maybe_star_pattern ::= star_pattern | pattern\n' |
| 3239 |
' star_pattern ::= "*" (capture_pattern | ' |
| 3240 |
'wildcard_pattern)\n' |
| 3241 |
'\n' |
| 3242 |
'There is no difference if parentheses or square brackets are ' |
| 3243 |
'used for\n' |
| 3244 |
'sequence patterns (i.e. "(...)" vs "[...]" ).\n' |
| 3245 |
'\n' |
| 3246 |
'Note:\n' |
| 3247 |
'\n' |
| 3248 |
' A single pattern enclosed in parentheses without a trailing ' |
| 3249 |
'comma\n' |
| 3250 |
' (e.g. "(3 | 4)") is a group pattern. While a single pattern ' |
| 3251 |
'enclosed\n' |
| 3252 |
' in square brackets (e.g. "[3 | 4]") is still a sequence ' |
| 3253 |
'pattern.\n' |
| 3254 |
'\n' |
| 3255 |
'At most one star subpattern may be in a sequence pattern. The ' |
| 3256 |
'star\n' |
| 3257 |
'subpattern may occur in any position. If no star subpattern is\n' |
| 3258 |
'present, the sequence pattern is a fixed-length sequence ' |
| 3259 |
'pattern;\n' |
| 3260 |
'otherwise it is a variable-length sequence pattern.\n' |
| 3261 |
'\n' |
| 3262 |
'The following is the logical flow for matching a sequence ' |
| 3263 |
'pattern\n' |
| 3264 |
'against a subject value:\n' |
| 3265 |
'\n' |
| 3266 |
'1. If the subject value is not a sequence [2], the sequence ' |
| 3267 |
'pattern\n' |
| 3268 |
' fails.\n' |
| 3269 |
'\n' |
| 3270 |
'2. If the subject value is an instance of "str", "bytes" or\n' |
| 3271 |
' "bytearray" the sequence pattern fails.\n' |
| 3272 |
'\n' |
| 3273 |
'3. The subsequent steps depend on whether the sequence pattern ' |
| 3274 |
'is\n' |
| 3275 |
' fixed or variable-length.\n' |
| 3276 |
'\n' |
| 3277 |
' If the sequence pattern is fixed-length:\n' |
| 3278 |
'\n' |
| 3279 |
' 1. If the length of the subject sequence is not equal to the ' |
| 3280 |
'number\n' |
| 3281 |
' of subpatterns, the sequence pattern fails\n' |
| 3282 |
'\n' |
| 3283 |
' 2. Subpatterns in the sequence pattern are matched to their\n' |
| 3284 |
' corresponding items in the subject sequence from left to ' |
| 3285 |
'right.\n' |
| 3286 |
' Matching stops as soon as a subpattern fails. If all\n' |
| 3287 |
' subpatterns succeed in matching their corresponding item, ' |
| 3288 |
'the\n' |
| 3289 |
' sequence pattern succeeds.\n' |
| 3290 |
'\n' |
| 3291 |
' Otherwise, if the sequence pattern is variable-length:\n' |
| 3292 |
'\n' |
| 3293 |
' 1. If the length of the subject sequence is less than the ' |
| 3294 |
'number of\n' |
| 3295 |
' non-star subpatterns, the sequence pattern fails.\n' |
| 3296 |
'\n' |
| 3297 |
' 2. The leading non-star subpatterns are matched to their\n' |
| 3298 |
' corresponding items as for fixed-length sequences.\n' |
| 3299 |
'\n' |
| 3300 |
' 3. If the previous step succeeds, the star subpattern matches ' |
| 3301 |
'a\n' |
| 3302 |
' list formed of the remaining subject items, excluding the\n' |
| 3303 |
' remaining items corresponding to non-star subpatterns ' |
| 3304 |
'following\n' |
| 3305 |
' the star subpattern.\n' |
| 3306 |
'\n' |
| 3307 |
' 4. Remaining non-star subpatterns are matched to their\n' |
| 3308 |
' corresponding subject items, as for a fixed-length ' |
| 3309 |
'sequence.\n' |
| 3310 |
'\n' |
| 3311 |
' Note:\n' |
| 3312 |
'\n' |
| 3313 |
' The length of the subject sequence is obtained via "len()" ' |
| 3314 |
'(i.e.\n' |
| 3315 |
' via the "__len__()" protocol). This length may be cached ' |
| 3316 |
'by the\n' |
| 3317 |
' interpreter in a similar manner as value patterns.\n' |
| 3318 |
'\n' |
| 3319 |
'In simple terms "[P1, P2, P3," … ", P<N>]" matches only if all ' |
| 3320 |
'the\n' |
| 3321 |
'following happens:\n' |
| 3322 |
'\n' |
| 3323 |
'* check "<subject>" is a sequence\n' |
| 3324 |
'\n' |
| 3325 |
'* "len(subject) == <N>"\n' |
| 3326 |
'\n' |
| 3327 |
'* "P1" matches "<subject>[0]" (note that this match can also ' |
| 3328 |
'bind\n' |
| 3329 |
' names)\n' |
| 3330 |
'\n' |
| 3331 |
'* "P2" matches "<subject>[1]" (note that this match can also ' |
| 3332 |
'bind\n' |
| 3333 |
' names)\n' |
| 3334 |
'\n' |
| 3335 |
'* … and so on for the corresponding pattern/element.\n' |
| 3336 |
'\n' |
| 3337 |
'\n' |
| 3338 |
'Mapping Patterns\n' |
| 3339 |
'~~~~~~~~~~~~~~~~\n' |
| 3340 |
'\n' |
| 3341 |
'A mapping pattern contains one or more key-value patterns. The ' |
| 3342 |
'syntax\n' |
| 3343 |
'is similar to the construction of a dictionary. Syntax:\n' |
| 3344 |
'\n' |
| 3345 |
' mapping_pattern ::= "{" [items_pattern] "}"\n' |
| 3346 |
' items_pattern ::= ",".key_value_pattern+ ","?\n' |
| 3347 |
' key_value_pattern ::= (literal_pattern | value_pattern) ":" ' |
| 3348 |
'pattern\n' |
| 3349 |
' | double_star_pattern\n' |
| 3350 |
' double_star_pattern ::= "**" capture_pattern\n' |
| 3351 |
'\n' |
| 3352 |
'At most one double star pattern may be in a mapping pattern. ' |
| 3353 |
'The\n' |
| 3354 |
'double star pattern must be the last subpattern in the mapping\n' |
| 3355 |
'pattern.\n' |
| 3356 |
'\n' |
| 3357 |
'Duplicate keys in mapping patterns are disallowed. Duplicate ' |
| 3358 |
'literal\n' |
| 3359 |
'keys will raise a "SyntaxError". Two keys that otherwise have ' |
| 3360 |
'the same\n' |
| 3361 |
'value will raise a "ValueError" at runtime.\n' |
| 3362 |
'\n' |
| 3363 |
'The following is the logical flow for matching a mapping ' |
| 3364 |
'pattern\n' |
| 3365 |
'against a subject value:\n' |
| 3366 |
'\n' |
| 3367 |
'1. If the subject value is not a mapping [3],the mapping ' |
| 3368 |
'pattern\n' |
| 3369 |
' fails.\n' |
| 3370 |
'\n' |
| 3371 |
'2. If every key given in the mapping pattern is present in the ' |
| 3372 |
'subject\n' |
| 3373 |
' mapping, and the pattern for each key matches the ' |
| 3374 |
'corresponding\n' |
| 3375 |
' item of the subject mapping, the mapping pattern succeeds.\n' |
| 3376 |
'\n' |
| 3377 |
'3. If duplicate keys are detected in the mapping pattern, the ' |
| 3378 |
'pattern\n' |
| 3379 |
' is considered invalid. A "SyntaxError" is raised for ' |
| 3380 |
'duplicate\n' |
| 3381 |
' literal values; or a "ValueError" for named keys of the same ' |
| 3382 |
'value.\n' |
| 3383 |
'\n' |
| 3384 |
'Note:\n' |
| 3385 |
'\n' |
| 3386 |
' Key-value pairs are matched using the two-argument form of ' |
| 3387 |
'the\n' |
| 3388 |
' mapping subject’s "get()" method. Matched key-value pairs ' |
| 3389 |
'must\n' |
| 3390 |
' already be present in the mapping, and not created on-the-fly ' |
| 3391 |
'via\n' |
| 3392 |
' "__missing__()" or "__getitem__()".\n' |
| 3393 |
'\n' |
| 3394 |
'In simple terms "{KEY1: P1, KEY2: P2, ... }" matches only if all ' |
| 3395 |
'the\n' |
| 3396 |
'following happens:\n' |
| 3397 |
'\n' |
| 3398 |
'* check "<subject>" is a mapping\n' |
| 3399 |
'\n' |
| 3400 |
'* "KEY1 in <subject>"\n' |
| 3401 |
'\n' |
| 3402 |
'* "P1" matches "<subject>[KEY1]"\n' |
| 3403 |
'\n' |
| 3404 |
'* … and so on for the corresponding KEY/pattern pair.\n' |
| 3405 |
'\n' |
| 3406 |
'\n' |
| 3407 |
'Class Patterns\n' |
| 3408 |
'~~~~~~~~~~~~~~\n' |
| 3409 |
'\n' |
| 3410 |
'A class pattern represents a class and its positional and ' |
| 3411 |
'keyword\n' |
| 3412 |
'arguments (if any). Syntax:\n' |
| 3413 |
'\n' |
| 3414 |
' class_pattern ::= name_or_attr "(" [pattern_arguments ' |
| 3415 |
'","?] ")"\n' |
| 3416 |
' pattern_arguments ::= positional_patterns ["," ' |
| 3417 |
'keyword_patterns]\n' |
| 3418 |
' | keyword_patterns\n' |
| 3419 |
' positional_patterns ::= ",".pattern+\n' |
| 3420 |
' keyword_patterns ::= ",".keyword_pattern+\n' |
| 3421 |
' keyword_pattern ::= NAME "=" pattern\n' |
| 3422 |
'\n' |
| 3423 |
'The same keyword should not be repeated in class patterns.\n' |
| 3424 |
'\n' |
| 3425 |
'The following is the logical flow for matching a class pattern ' |
| 3426 |
'against\n' |
| 3427 |
'a subject value:\n' |
| 3428 |
'\n' |
| 3429 |
'1. If "name_or_attr" is not an instance of the builtin "type" , ' |
| 3430 |
'raise\n' |
| 3431 |
' "TypeError".\n' |
| 3432 |
'\n' |
| 3433 |
'2. If the subject value is not an instance of "name_or_attr" ' |
| 3434 |
'(tested\n' |
| 3435 |
' via "isinstance()"), the class pattern fails.\n' |
| 3436 |
'\n' |
| 3437 |
'3. If no pattern arguments are present, the pattern succeeds.\n' |
| 3438 |
' Otherwise, the subsequent steps depend on whether keyword or\n' |
| 3439 |
' positional argument patterns are present.\n' |
| 3440 |
'\n' |
| 3441 |
' For a number of built-in types (specified below), a single\n' |
| 3442 |
' positional subpattern is accepted which will match the ' |
| 3443 |
'entire\n' |
| 3444 |
' subject; for these types keyword patterns also work as for ' |
| 3445 |
'other\n' |
| 3446 |
' types.\n' |
| 3447 |
'\n' |
| 3448 |
' If only keyword patterns are present, they are processed as\n' |
| 3449 |
' follows, one by one:\n' |
| 3450 |
'\n' |
| 3451 |
' I. The keyword is looked up as an attribute on the subject.\n' |
| 3452 |
'\n' |
| 3453 |
' * If this raises an exception other than "AttributeError", ' |
| 3454 |
'the\n' |
| 3455 |
' exception bubbles up.\n' |
| 3456 |
'\n' |
| 3457 |
' * If this raises "AttributeError", the class pattern has ' |
| 3458 |
'failed.\n' |
| 3459 |
'\n' |
| 3460 |
' * Else, the subpattern associated with the keyword pattern ' |
| 3461 |
'is\n' |
| 3462 |
' matched against the subject’s attribute value. If this ' |
| 3463 |
'fails,\n' |
| 3464 |
' the class pattern fails; if this succeeds, the match ' |
| 3465 |
'proceeds\n' |
| 3466 |
' to the next keyword.\n' |
| 3467 |
'\n' |
| 3468 |
' II. If all keyword patterns succeed, the class pattern ' |
| 3469 |
'succeeds.\n' |
| 3470 |
'\n' |
| 3471 |
' If any positional patterns are present, they are converted ' |
| 3472 |
'to\n' |
| 3473 |
' keyword patterns using the "__match_args__" attribute on the ' |
| 3474 |
'class\n' |
| 3475 |
' "name_or_attr" before matching:\n' |
| 3476 |
'\n' |
| 3477 |
' I. The equivalent of "getattr(cls, "__match_args__", ())" is\n' |
| 3478 |
' called.\n' |
| 3479 |
'\n' |
| 3480 |
' * If this raises an exception, the exception bubbles up.\n' |
| 3481 |
'\n' |
| 3482 |
' * If the returned value is not a tuple, the conversion ' |
| 3483 |
'fails and\n' |
| 3484 |
' "TypeError" is raised.\n' |
| 3485 |
'\n' |
| 3486 |
' * If there are more positional patterns than\n' |
| 3487 |
' "len(cls.__match_args__)", "TypeError" is raised.\n' |
| 3488 |
'\n' |
| 3489 |
' * Otherwise, positional pattern "i" is converted to a ' |
| 3490 |
'keyword\n' |
| 3491 |
' pattern using "__match_args__[i]" as the keyword.\n' |
| 3492 |
' "__match_args__[i]" must be a string; if not "TypeError" ' |
| 3493 |
'is\n' |
| 3494 |
' raised.\n' |
| 3495 |
'\n' |
| 3496 |
' * If there are duplicate keywords, "TypeError" is raised.\n' |
| 3497 |
'\n' |
| 3498 |
' See also:\n' |
| 3499 |
'\n' |
| 3500 |
' Customizing positional arguments in class pattern ' |
| 3501 |
'matching\n' |
| 3502 |
'\n' |
| 3503 |
' II. Once all positional patterns have been converted to ' |
| 3504 |
'keyword\n' |
| 3505 |
' patterns,\n' |
| 3506 |
' the match proceeds as if there were only keyword ' |
| 3507 |
'patterns.\n' |
| 3508 |
'\n' |
| 3509 |
' For the following built-in types the handling of positional\n' |
| 3510 |
' subpatterns is different:\n' |
| 3511 |
'\n' |
| 3512 |
' * "bool"\n' |
| 3513 |
'\n' |
| 3514 |
' * "bytearray"\n' |
| 3515 |
'\n' |
| 3516 |
' * "bytes"\n' |
| 3517 |
'\n' |
| 3518 |
' * "dict"\n' |
| 3519 |
'\n' |
| 3520 |
' * "float"\n' |
| 3521 |
'\n' |
| 3522 |
' * "frozenset"\n' |
| 3523 |
'\n' |
| 3524 |
' * "int"\n' |
| 3525 |
'\n' |
| 3526 |
' * "list"\n' |
| 3527 |
'\n' |
| 3528 |
' * "set"\n' |
| 3529 |
'\n' |
| 3530 |
' * "str"\n' |
| 3531 |
'\n' |
| 3532 |
' * "tuple"\n' |
| 3533 |
'\n' |
| 3534 |
' These classes accept a single positional argument, and the ' |
| 3535 |
'pattern\n' |
| 3536 |
' there is matched against the whole object rather than an ' |
| 3537 |
'attribute.\n' |
| 3538 |
' For example "int(0|1)" matches the value "0", but not the ' |
| 3539 |
'value\n' |
| 3540 |
' "0.0".\n' |
| 3541 |
'\n' |
| 3542 |
'In simple terms "CLS(P1, attr=P2)" matches only if the ' |
| 3543 |
'following\n' |
| 3544 |
'happens:\n' |
| 3545 |
'\n' |
| 3546 |
'* "isinstance(<subject>, CLS)"\n' |
| 3547 |
'\n' |
| 3548 |
'* convert "P1" to a keyword pattern using "CLS.__match_args__"\n' |
| 3549 |
'\n' |
| 3550 |
'* For each keyword argument "attr=P2":\n' |
| 3551 |
'\n' |
| 3552 |
' * "hasattr(<subject>, "attr")"\n' |
| 3553 |
'\n' |
| 3554 |
' * "P2" matches "<subject>.attr"\n' |
| 3555 |
'\n' |
| 3556 |
'* … and so on for the corresponding keyword argument/pattern ' |
| 3557 |
'pair.\n' |
| 3558 |
'\n' |
| 3559 |
'See also:\n' |
| 3560 |
'\n' |
| 3561 |
' * **PEP 634** – Structural Pattern Matching: Specification\n' |
| 3562 |
'\n' |
| 3563 |
' * **PEP 636** – Structural Pattern Matching: Tutorial\n' |
| 3564 |
'\n' |
| 3565 |
'\n' |
| 3566 |
'Function definitions\n' |
| 3567 |
'====================\n' |
| 3568 |
'\n' |
| 3569 |
'A function definition defines a user-defined function object ' |
| 3570 |
'(see\n' |
| 3571 |
'section The standard type hierarchy):\n' |
| 3572 |
'\n' |
| 3573 |
' funcdef ::= [decorators] "def" funcname "(" ' |
| 3574 |
'[parameter_list] ")"\n' |
| 3575 |
' ["->" expression] ":" suite\n' |
| 3576 |
' decorators ::= decorator+\n' |
| 3577 |
' decorator ::= "@" assignment_expression ' |
| 3578 |
'NEWLINE\n' |
| 3579 |
' parameter_list ::= defparameter ("," ' |
| 3580 |
'defparameter)* "," "/" ["," [parameter_list_no_posonly]]\n' |
| 3581 |
' | parameter_list_no_posonly\n' |
| 3582 |
' parameter_list_no_posonly ::= defparameter ("," ' |
| 3583 |
'defparameter)* ["," [parameter_list_starargs]]\n' |
| 3584 |
' | parameter_list_starargs\n' |
| 3585 |
' parameter_list_starargs ::= "*" [parameter] ("," ' |
| 3586 |
'defparameter)* ["," ["**" parameter [","]]]\n' |
| 3587 |
' | "**" parameter [","]\n' |
| 3588 |
' parameter ::= identifier [":" expression]\n' |
| 3589 |
' defparameter ::= parameter ["=" expression]\n' |
| 3590 |
' funcname ::= identifier\n' |
| 3591 |
'\n' |
| 3592 |
'A function definition is an executable statement. Its execution ' |
| 3593 |
'binds\n' |
| 3594 |
'the function name in the current local namespace to a function ' |
| 3595 |
'object\n' |
| 3596 |
'(a wrapper around the executable code for the function). This\n' |
| 3597 |
'function object contains a reference to the current global ' |
| 3598 |
'namespace\n' |
| 3599 |
'as the global namespace to be used when the function is called.\n' |
| 3600 |
'\n' |
| 3601 |
'The function definition does not execute the function body; this ' |
| 3602 |
'gets\n' |
| 3603 |
'executed only when the function is called. [4]\n' |
| 3604 |
'\n' |
| 3605 |
'A function definition may be wrapped by one or more *decorator*\n' |
| 3606 |
'expressions. Decorator expressions are evaluated when the ' |
| 3607 |
'function is\n' |
| 3608 |
'defined, in the scope that contains the function definition. ' |
| 3609 |
'The\n' |
| 3610 |
'result must be a callable, which is invoked with the function ' |
| 3611 |
'object\n' |
| 3612 |
'as the only argument. The returned value is bound to the ' |
| 3613 |
'function name\n' |
| 3614 |
'instead of the function object. Multiple decorators are applied ' |
| 3615 |
'in\n' |
| 3616 |
'nested fashion. For example, the following code\n' |
| 3617 |
'\n' |
| 3618 |
' @f1(arg)\n' |
| 3619 |
' @f2\n' |
| 3620 |
' def func(): pass\n' |
| 3621 |
'\n' |
| 3622 |
'is roughly equivalent to\n' |
| 3623 |
'\n' |
| 3624 |
' def func(): pass\n' |
| 3625 |
' func = f1(arg)(f2(func))\n' |
| 3626 |
'\n' |
| 3627 |
'except that the original function is not temporarily bound to ' |
| 3628 |
'the name\n' |
| 3629 |
'"func".\n' |
| 3630 |
'\n' |
| 3631 |
'Changed in version 3.9: Functions may be decorated with any ' |
| 3632 |
'valid\n' |
| 3633 |
'"assignment_expression". Previously, the grammar was much more\n' |
| 3634 |
'restrictive; see **PEP 614** for details.\n' |
| 3635 |
'\n' |
| 3636 |
'When one or more *parameters* have the form *parameter* "="\n' |
| 3637 |
'*expression*, the function is said to have “default parameter ' |
| 3638 |
'values.”\n' |
| 3639 |
'For a parameter with a default value, the corresponding ' |
| 3640 |
'*argument* may\n' |
| 3641 |
'be omitted from a call, in which case the parameter’s default ' |
| 3642 |
'value is\n' |
| 3643 |
'substituted. If a parameter has a default value, all following\n' |
| 3644 |
'parameters up until the “"*"” must also have a default value — ' |
| 3645 |
'this is\n' |
| 3646 |
'a syntactic restriction that is not expressed by the grammar.\n' |
| 3647 |
'\n' |
| 3648 |
'**Default parameter values are evaluated from left to right when ' |
| 3649 |
'the\n' |
| 3650 |
'function definition is executed.** This means that the ' |
| 3651 |
'expression is\n' |
| 3652 |
'evaluated once, when the function is defined, and that the same ' |
| 3653 |
'“pre-\n' |
| 3654 |
'computed” value is used for each call. This is especially ' |
| 3655 |
'important\n' |
| 3656 |
'to understand when a default parameter value is a mutable ' |
| 3657 |
'object, such\n' |
| 3658 |
'as a list or a dictionary: if the function modifies the object ' |
| 3659 |
'(e.g.\n' |
| 3660 |
'by appending an item to a list), the default parameter value is ' |
| 3661 |
'in\n' |
| 3662 |
'effect modified. This is generally not what was intended. A ' |
| 3663 |
'way\n' |
| 3664 |
'around this is to use "None" as the default, and explicitly test ' |
| 3665 |
'for\n' |
| 3666 |
'it in the body of the function, e.g.:\n' |
| 3667 |
'\n' |
| 3668 |
' def whats_on_the_telly(penguin=None):\n' |
| 3669 |
' if penguin is None:\n' |
| 3670 |
' penguin = []\n' |
| 3671 |
' penguin.append("property of the zoo")\n' |
| 3672 |
' return penguin\n' |
| 3673 |
'\n' |
| 3674 |
'Function call semantics are described in more detail in section ' |
| 3675 |
'Calls.\n' |
| 3676 |
'A function call always assigns values to all parameters ' |
| 3677 |
'mentioned in\n' |
| 3678 |
'the parameter list, either from positional arguments, from ' |
| 3679 |
'keyword\n' |
| 3680 |
'arguments, or from default values. If the form “"*identifier"” ' |
| 3681 |
'is\n' |
| 3682 |
'present, it is initialized to a tuple receiving any excess ' |
| 3683 |
'positional\n' |
| 3684 |
'parameters, defaulting to the empty tuple. If the form\n' |
| 3685 |
'“"**identifier"” is present, it is initialized to a new ordered\n' |
| 3686 |
'mapping receiving any excess keyword arguments, defaulting to a ' |
| 3687 |
'new\n' |
| 3688 |
'empty mapping of the same type. Parameters after “"*"” or\n' |
| 3689 |
'“"*identifier"” are keyword-only parameters and may only be ' |
| 3690 |
'passed by\n' |
| 3691 |
'keyword arguments. Parameters before “"/"” are positional-only\n' |
| 3692 |
'parameters and may only be passed by positional arguments.\n' |
| 3693 |
'\n' |
| 3694 |
'Changed in version 3.8: The "/" function parameter syntax may be ' |
| 3695 |
'used\n' |
| 3696 |
'to indicate positional-only parameters. See **PEP 570** for ' |
| 3697 |
'details.\n' |
| 3698 |
'\n' |
| 3699 |
'Parameters may have an *annotation* of the form “": ' |
| 3700 |
'expression"”\n' |
| 3701 |
'following the parameter name. Any parameter may have an ' |
| 3702 |
'annotation,\n' |
| 3703 |
'even those of the form "*identifier" or "**identifier". ' |
| 3704 |
'Functions may\n' |
| 3705 |
'have “return” annotation of the form “"-> expression"” after ' |
| 3706 |
'the\n' |
| 3707 |
'parameter list. These annotations can be any valid Python ' |
| 3708 |
'expression.\n' |
| 3709 |
'The presence of annotations does not change the semantics of a\n' |
| 3710 |
'function. The annotation values are available as values of a\n' |
| 3711 |
'dictionary keyed by the parameters’ names in the ' |
| 3712 |
'"__annotations__"\n' |
| 3713 |
'attribute of the function object. If the "annotations" import ' |
| 3714 |
'from\n' |
| 3715 |
'"__future__" is used, annotations are preserved as strings at ' |
| 3716 |
'runtime\n' |
| 3717 |
'which enables postponed evaluation. Otherwise, they are ' |
| 3718 |
'evaluated\n' |
| 3719 |
'when the function definition is executed. In this case ' |
| 3720 |
'annotations\n' |
| 3721 |
'may be evaluated in a different order than they appear in the ' |
| 3722 |
'source\n' |
| 3723 |
'code.\n' |
| 3724 |
'\n' |
| 3725 |
'It is also possible to create anonymous functions (functions not ' |
| 3726 |
'bound\n' |
| 3727 |
'to a name), for immediate use in expressions. This uses lambda\n' |
| 3728 |
'expressions, described in section Lambdas. Note that the ' |
| 3729 |
'lambda\n' |
| 3730 |
'expression is merely a shorthand for a simplified function ' |
| 3731 |
'definition;\n' |
| 3732 |
'a function defined in a “"def"” statement can be passed around ' |
| 3733 |
'or\n' |
| 3734 |
'assigned to another name just like a function defined by a ' |
| 3735 |
'lambda\n' |
| 3736 |
'expression. The “"def"” form is actually more powerful since ' |
| 3737 |
'it\n' |
| 3738 |
'allows the execution of multiple statements and annotations.\n' |
| 3739 |
'\n' |
| 3740 |
'**Programmer’s note:** Functions are first-class objects. A ' |
| 3741 |
'“"def"”\n' |
| 3742 |
'statement executed inside a function definition defines a local\n' |
| 3743 |
'function that can be returned or passed around. Free variables ' |
| 3744 |
'used\n' |
| 3745 |
'in the nested function can access the local variables of the ' |
| 3746 |
'function\n' |
| 3747 |
'containing the def. See section Naming and binding for ' |
| 3748 |
'details.\n' |
| 3749 |
'\n' |
| 3750 |
'See also:\n' |
| 3751 |
'\n' |
| 3752 |
' **PEP 3107** - Function Annotations\n' |
| 3753 |
' The original specification for function annotations.\n' |
| 3754 |
'\n' |
| 3755 |
' **PEP 484** - Type Hints\n' |
| 3756 |
' Definition of a standard meaning for annotations: type ' |
| 3757 |
'hints.\n' |
| 3758 |
'\n' |
| 3759 |
' **PEP 526** - Syntax for Variable Annotations\n' |
| 3760 |
' Ability to type hint variable declarations, including ' |
| 3761 |
'class\n' |
| 3762 |
' variables and instance variables.\n' |
| 3763 |
'\n' |
| 3764 |
' **PEP 563** - Postponed Evaluation of Annotations\n' |
| 3765 |
' Support for forward references within annotations by ' |
| 3766 |
'preserving\n' |
| 3767 |
' annotations in a string form at runtime instead of eager\n' |
| 3768 |
' evaluation.\n' |
| 3769 |
'\n' |
| 3770 |
' **PEP 318** - Decorators for Functions and Methods\n' |
| 3771 |
' Function and method decorators were introduced. Class ' |
| 3772 |
'decorators\n' |
| 3773 |
' were introduced in **PEP 3129**.\n' |
| 3774 |
'\n' |
| 3775 |
'\n' |
| 3776 |
'Class definitions\n' |
| 3777 |
'=================\n' |
| 3778 |
'\n' |
| 3779 |
'A class definition defines a class object (see section The ' |
| 3780 |
'standard\n' |
| 3781 |
'type hierarchy):\n' |
| 3782 |
'\n' |
| 3783 |
' classdef ::= [decorators] "class" classname [inheritance] ' |
| 3784 |
'":" suite\n' |
| 3785 |
' inheritance ::= "(" [argument_list] ")"\n' |
| 3786 |
' classname ::= identifier\n' |
| 3787 |
'\n' |
| 3788 |
'A class definition is an executable statement. The inheritance ' |
| 3789 |
'list\n' |
| 3790 |
'usually gives a list of base classes (see Metaclasses for more\n' |
| 3791 |
'advanced uses), so each item in the list should evaluate to a ' |
| 3792 |
'class\n' |
| 3793 |
'object which allows subclassing. Classes without an inheritance ' |
| 3794 |
'list\n' |
| 3795 |
'inherit, by default, from the base class "object"; hence,\n' |
| 3796 |
'\n' |
| 3797 |
' class Foo:\n' |
| 3798 |
' pass\n' |
| 3799 |
'\n' |
| 3800 |
'is equivalent to\n' |
| 3801 |
'\n' |
| 3802 |
' class Foo(object):\n' |
| 3803 |
' pass\n' |
| 3804 |
'\n' |
| 3805 |
'The class’s suite is then executed in a new execution frame ' |
| 3806 |
'(see\n' |
| 3807 |
'Naming and binding), using a newly created local namespace and ' |
| 3808 |
'the\n' |
| 3809 |
'original global namespace. (Usually, the suite contains mostly\n' |
| 3810 |
'function definitions.) When the class’s suite finishes ' |
| 3811 |
'execution, its\n' |
| 3812 |
'execution frame is discarded but its local namespace is saved. ' |
| 3813 |
'[5] A\n' |
| 3814 |
'class object is then created using the inheritance list for the ' |
| 3815 |
'base\n' |
| 3816 |
'classes and the saved local namespace for the attribute ' |
| 3817 |
'dictionary.\n' |
| 3818 |
'The class name is bound to this class object in the original ' |
| 3819 |
'local\n' |
| 3820 |
'namespace.\n' |
| 3821 |
'\n' |
| 3822 |
'The order in which attributes are defined in the class body is\n' |
| 3823 |
'preserved in the new class’s "__dict__". Note that this is ' |
| 3824 |
'reliable\n' |
| 3825 |
'only right after the class is created and only for classes that ' |
| 3826 |
'were\n' |
| 3827 |
'defined using the definition syntax.\n' |
| 3828 |
'\n' |
| 3829 |
'Class creation can be customized heavily using metaclasses.\n' |
| 3830 |
'\n' |
| 3831 |
'Classes can also be decorated: just like when decorating ' |
| 3832 |
'functions,\n' |
| 3833 |
'\n' |
| 3834 |
' @f1(arg)\n' |
| 3835 |
' @f2\n' |
| 3836 |
' class Foo: pass\n' |
| 3837 |
'\n' |
| 3838 |
'is roughly equivalent to\n' |
| 3839 |
'\n' |
| 3840 |
' class Foo: pass\n' |
| 3841 |
' Foo = f1(arg)(f2(Foo))\n' |
| 3842 |
'\n' |
| 3843 |
'The evaluation rules for the decorator expressions are the same ' |
| 3844 |
'as for\n' |
| 3845 |
'function decorators. The result is then bound to the class ' |
| 3846 |
'name.\n' |
| 3847 |
'\n' |
| 3848 |
'Changed in version 3.9: Classes may be decorated with any valid\n' |
| 3849 |
'"assignment_expression". Previously, the grammar was much more\n' |
| 3850 |
'restrictive; see **PEP 614** for details.\n' |
| 3851 |
'\n' |
| 3852 |
'**Programmer’s note:** Variables defined in the class definition ' |
| 3853 |
'are\n' |
| 3854 |
'class attributes; they are shared by instances. Instance ' |
| 3855 |
'attributes\n' |
| 3856 |
'can be set in a method with "self.name = value". Both class ' |
| 3857 |
'and\n' |
| 3858 |
'instance attributes are accessible through the notation ' |
| 3859 |
'“"self.name"”,\n' |
| 3860 |
'and an instance attribute hides a class attribute with the same ' |
| 3861 |
'name\n' |
| 3862 |
'when accessed in this way. Class attributes can be used as ' |
| 3863 |
'defaults\n' |
| 3864 |
'for instance attributes, but using mutable values there can lead ' |
| 3865 |
'to\n' |
| 3866 |
'unexpected results. Descriptors can be used to create instance\n' |
| 3867 |
'variables with different implementation details.\n' |
| 3868 |
'\n' |
| 3869 |
'See also:\n' |
| 3870 |
'\n' |
| 3871 |
' **PEP 3115** - Metaclasses in Python 3000\n' |
| 3872 |
' The proposal that changed the declaration of metaclasses to ' |
| 3873 |
'the\n' |
| 3874 |
' current syntax, and the semantics for how classes with\n' |
| 3875 |
' metaclasses are constructed.\n' |
| 3876 |
'\n' |
| 3877 |
' **PEP 3129** - Class Decorators\n' |
| 3878 |
' The proposal that added class decorators. Function and ' |
| 3879 |
'method\n' |
| 3880 |
' decorators were introduced in **PEP 318**.\n' |
| 3881 |
'\n' |
| 3882 |
'\n' |
| 3883 |
'Coroutines\n' |
| 3884 |
'==========\n' |
| 3885 |
'\n' |
| 3886 |
'New in version 3.5.\n' |
| 3887 |
'\n' |
| 3888 |
'\n' |
| 3889 |
'Coroutine function definition\n' |
| 3890 |
'-----------------------------\n' |
| 3891 |
'\n' |
| 3892 |
' async_funcdef ::= [decorators] "async" "def" funcname "(" ' |
| 3893 |
'[parameter_list] ")"\n' |
| 3894 |
' ["->" expression] ":" suite\n' |
| 3895 |
'\n' |
| 3896 |
'Execution of Python coroutines can be suspended and resumed at ' |
| 3897 |
'many\n' |
| 3898 |
'points (see *coroutine*). "await" expressions, "async for" and ' |
| 3899 |
'"async\n' |
| 3900 |
'with" can only be used in the body of a coroutine function.\n' |
| 3901 |
'\n' |
| 3902 |
'Functions defined with "async def" syntax are always coroutine\n' |
| 3903 |
'functions, even if they do not contain "await" or "async" ' |
| 3904 |
'keywords.\n' |
| 3905 |
'\n' |
| 3906 |
'It is a "SyntaxError" to use a "yield from" expression inside ' |
| 3907 |
'the body\n' |
| 3908 |
'of a coroutine function.\n' |
| 3909 |
'\n' |
| 3910 |
'An example of a coroutine function:\n' |
| 3911 |
'\n' |
| 3912 |
' async def func(param1, param2):\n' |
| 3913 |
' do_stuff()\n' |
| 3914 |
' await some_coroutine()\n' |
| 3915 |
'\n' |
| 3916 |
'Changed in version 3.7: "await" and "async" are now keywords;\n' |
| 3917 |
'previously they were only treated as such inside the body of a\n' |
| 3918 |
'coroutine function.\n' |
| 3919 |
'\n' |
| 3920 |
'\n' |
| 3921 |
'The "async for" statement\n' |
| 3922 |
'-------------------------\n' |
| 3923 |
'\n' |
| 3924 |
' async_for_stmt ::= "async" for_stmt\n' |
| 3925 |
'\n' |
| 3926 |
'An *asynchronous iterable* provides an "__aiter__" method that\n' |
| 3927 |
'directly returns an *asynchronous iterator*, which can call\n' |
| 3928 |
'asynchronous code in its "__anext__" method.\n' |
| 3929 |
'\n' |
| 3930 |
'The "async for" statement allows convenient iteration over\n' |
| 3931 |
'asynchronous iterables.\n' |
| 3932 |
'\n' |
| 3933 |
'The following code:\n' |
| 3934 |
'\n' |
| 3935 |
' async for TARGET in ITER:\n' |
| 3936 |
' SUITE\n' |
| 3937 |
' else:\n' |
| 3938 |
' SUITE2\n' |
| 3939 |
'\n' |
| 3940 |
'Is semantically equivalent to:\n' |
| 3941 |
'\n' |
| 3942 |
' iter = (ITER)\n' |
| 3943 |
' iter = type(iter).__aiter__(iter)\n' |
| 3944 |
' running = True\n' |
| 3945 |
'\n' |
| 3946 |
' while running:\n' |
| 3947 |
' try:\n' |
| 3948 |
' TARGET = await type(iter).__anext__(iter)\n' |
| 3949 |
' except StopAsyncIteration:\n' |
| 3950 |
' running = False\n' |
| 3951 |
' else:\n' |
| 3952 |
' SUITE\n' |
| 3953 |
' else:\n' |
| 3954 |
' SUITE2\n' |
| 3955 |
'\n' |
| 3956 |
'See also "__aiter__()" and "__anext__()" for details.\n' |
| 3957 |
'\n' |
| 3958 |
'It is a "SyntaxError" to use an "async for" statement outside ' |
| 3959 |
'the body\n' |
| 3960 |
'of a coroutine function.\n' |
| 3961 |
'\n' |
| 3962 |
'\n' |
| 3963 |
'The "async with" statement\n' |
| 3964 |
'--------------------------\n' |
| 3965 |
'\n' |
| 3966 |
' async_with_stmt ::= "async" with_stmt\n' |
| 3967 |
'\n' |
| 3968 |
'An *asynchronous context manager* is a *context manager* that is ' |
| 3969 |
'able\n' |
| 3970 |
'to suspend execution in its *enter* and *exit* methods.\n' |
| 3971 |
'\n' |
| 3972 |
'The following code:\n' |
| 3973 |
'\n' |
| 3974 |
' async with EXPRESSION as TARGET:\n' |
| 3975 |
' SUITE\n' |
| 3976 |
'\n' |
| 3977 |
'is semantically equivalent to:\n' |
| 3978 |
'\n' |
| 3979 |
' manager = (EXPRESSION)\n' |
| 3980 |
' aenter = type(manager).__aenter__\n' |
| 3981 |
' aexit = type(manager).__aexit__\n' |
| 3982 |
' value = await aenter(manager)\n' |
| 3983 |
' hit_except = False\n' |
| 3984 |
'\n' |
| 3985 |
' try:\n' |
| 3986 |
' TARGET = value\n' |
| 3987 |
' SUITE\n' |
| 3988 |
' except:\n' |
| 3989 |
' hit_except = True\n' |
| 3990 |
' if not await aexit(manager, *sys.exc_info()):\n' |
| 3991 |
' raise\n' |
| 3992 |
' finally:\n' |
| 3993 |
' if not hit_except:\n' |
| 3994 |
' await aexit(manager, None, None, None)\n' |
| 3995 |
'\n' |
| 3996 |
'See also "__aenter__()" and "__aexit__()" for details.\n' |
| 3997 |
'\n' |
| 3998 |
'It is a "SyntaxError" to use an "async with" statement outside ' |
| 3999 |
'the\n' |
| 4000 |
'body of a coroutine function.\n' |
| 4001 |
'\n' |
| 4002 |
'See also:\n' |
| 4003 |
'\n' |
| 4004 |
' **PEP 492** - Coroutines with async and await syntax\n' |
| 4005 |
' The proposal that made coroutines a proper standalone ' |
| 4006 |
'concept in\n' |
| 4007 |
' Python, and added supporting syntax.\n' |
| 4008 |
'\n' |
| 4009 |
'-[ Footnotes ]-\n' |
| 4010 |
'\n' |
| 4011 |
'[1] The exception is propagated to the invocation stack unless ' |
| 4012 |
'there\n' |
| 4013 |
' is a "finally" clause which happens to raise another ' |
| 4014 |
'exception.\n' |
| 4015 |
' That new exception causes the old one to be lost.\n' |
| 4016 |
'\n' |
| 4017 |
'[2] In pattern matching, a sequence is defined as one of the\n' |
| 4018 |
' following:\n' |
| 4019 |
'\n' |
| 4020 |
' * a class that inherits from "collections.abc.Sequence"\n' |
| 4021 |
'\n' |
| 4022 |
' * a Python class that has been registered as\n' |
| 4023 |
' "collections.abc.Sequence"\n' |
| 4024 |
'\n' |
| 4025 |
' * a builtin class that has its (CPython) ' |
| 4026 |
'"Py_TPFLAGS_SEQUENCE" bit\n' |
| 4027 |
' set\n' |
| 4028 |
'\n' |
| 4029 |
' * a class that inherits from any of the above\n' |
| 4030 |
'\n' |
| 4031 |
' The following standard library classes are sequences:\n' |
| 4032 |
'\n' |
| 4033 |
' * "array.array"\n' |
| 4034 |
'\n' |
| 4035 |
' * "collections.deque"\n' |
| 4036 |
'\n' |
| 4037 |
' * "list"\n' |
| 4038 |
'\n' |
| 4039 |
' * "memoryview"\n' |
| 4040 |
'\n' |
| 4041 |
' * "range"\n' |
| 4042 |
'\n' |
| 4043 |
' * "tuple"\n' |
| 4044 |
'\n' |
| 4045 |
' Note:\n' |
| 4046 |
'\n' |
| 4047 |
' Subject values of type "str", "bytes", and "bytearray" do ' |
| 4048 |
'not\n' |
| 4049 |
' match sequence patterns.\n' |
| 4050 |
'\n' |
| 4051 |
'[3] In pattern matching, a mapping is defined as one of the ' |
| 4052 |
'following:\n' |
| 4053 |
'\n' |
| 4054 |
' * a class that inherits from "collections.abc.Mapping"\n' |
| 4055 |
'\n' |
| 4056 |
' * a Python class that has been registered as\n' |
| 4057 |
' "collections.abc.Mapping"\n' |
| 4058 |
'\n' |
| 4059 |
' * a builtin class that has its (CPython) ' |
| 4060 |
'"Py_TPFLAGS_MAPPING" bit\n' |
| 4061 |
' set\n' |
| 4062 |
'\n' |
| 4063 |
' * a class that inherits from any of the above\n' |
| 4064 |
'\n' |
| 4065 |
' The standard library classes "dict" and ' |
| 4066 |
'"types.MappingProxyType"\n' |
| 4067 |
' are mappings.\n' |
| 4068 |
'\n' |
| 4069 |
'[4] A string literal appearing as the first statement in the ' |
| 4070 |
'function\n' |
| 4071 |
' body is transformed into the function’s "__doc__" attribute ' |
| 4072 |
'and\n' |
| 4073 |
' therefore the function’s *docstring*.\n' |
| 4074 |
'\n' |
| 4075 |
'[5] A string literal appearing as the first statement in the ' |
| 4076 |
'class\n' |
| 4077 |
' body is transformed into the namespace’s "__doc__" item and\n' |
| 4078 |
' therefore the class’s *docstring*.\n', |
| 4079 |
'context-managers': 'With Statement Context Managers\n' |
| 4080 |
'*******************************\n' |
| 4081 |
'\n' |
| 4082 |
'A *context manager* is an object that defines the ' |
| 4083 |
'runtime context to\n' |
| 4084 |
'be established when executing a "with" statement. The ' |
| 4085 |
'context manager\n' |
| 4086 |
'handles the entry into, and the exit from, the desired ' |
| 4087 |
'runtime context\n' |
| 4088 |
'for the execution of the block of code. Context ' |
| 4089 |
'managers are normally\n' |
| 4090 |
'invoked using the "with" statement (described in section ' |
| 4091 |
'The with\n' |
| 4092 |
'statement), but can also be used by directly invoking ' |
| 4093 |
'their methods.\n' |
| 4094 |
'\n' |
| 4095 |
'Typical uses of context managers include saving and ' |
| 4096 |
'restoring various\n' |
| 4097 |
'kinds of global state, locking and unlocking resources, ' |
| 4098 |
'closing opened\n' |
| 4099 |
'files, etc.\n' |
| 4100 |
'\n' |
| 4101 |
'For more information on context managers, see Context ' |
| 4102 |
'Manager Types.\n' |
| 4103 |
'\n' |
| 4104 |
'object.__enter__(self)\n' |
| 4105 |
'\n' |
| 4106 |
' Enter the runtime context related to this object. The ' |
| 4107 |
'"with"\n' |
| 4108 |
' statement will bind this method’s return value to the ' |
| 4109 |
'target(s)\n' |
| 4110 |
' specified in the "as" clause of the statement, if ' |
| 4111 |
'any.\n' |
| 4112 |
'\n' |
| 4113 |
'object.__exit__(self, exc_type, exc_value, traceback)\n' |
| 4114 |
'\n' |
| 4115 |
' Exit the runtime context related to this object. The ' |
| 4116 |
'parameters\n' |
| 4117 |
' describe the exception that caused the context to be ' |
| 4118 |
'exited. If the\n' |
| 4119 |
' context was exited without an exception, all three ' |
| 4120 |
'arguments will\n' |
| 4121 |
' be "None".\n' |
| 4122 |
'\n' |
| 4123 |
' If an exception is supplied, and the method wishes to ' |
| 4124 |
'suppress the\n' |
| 4125 |
' exception (i.e., prevent it from being propagated), ' |
| 4126 |
'it should\n' |
| 4127 |
' return a true value. Otherwise, the exception will be ' |
| 4128 |
'processed\n' |
| 4129 |
' normally upon exit from this method.\n' |
| 4130 |
'\n' |
| 4131 |
' Note that "__exit__()" methods should not reraise the ' |
| 4132 |
'passed-in\n' |
| 4133 |
' exception; this is the caller’s responsibility.\n' |
| 4134 |
'\n' |
| 4135 |
'See also:\n' |
| 4136 |
'\n' |
| 4137 |
' **PEP 343** - The “with” statement\n' |
| 4138 |
' The specification, background, and examples for the ' |
| 4139 |
'Python "with"\n' |
| 4140 |
' statement.\n', |
| 4141 |
'continue': 'The "continue" statement\n' |
| 4142 |
'************************\n' |
| 4143 |
'\n' |
| 4144 |
' continue_stmt ::= "continue"\n' |
| 4145 |
'\n' |
| 4146 |
'"continue" may only occur syntactically nested in a "for" or ' |
| 4147 |
'"while"\n' |
| 4148 |
'loop, but not nested in a function or class definition within ' |
| 4149 |
'that\n' |
| 4150 |
'loop. It continues with the next cycle of the nearest enclosing ' |
| 4151 |
'loop.\n' |
| 4152 |
'\n' |
| 4153 |
'When "continue" passes control out of a "try" statement with a\n' |
| 4154 |
'"finally" clause, that "finally" clause is executed before ' |
| 4155 |
'really\n' |
| 4156 |
'starting the next loop cycle.\n', |
| 4157 |
'conversions': 'Arithmetic conversions\n' |
| 4158 |
'**********************\n' |
| 4159 |
'\n' |
| 4160 |
'When a description of an arithmetic operator below uses the ' |
| 4161 |
'phrase\n' |
| 4162 |
'“the numeric arguments are converted to a common type”, this ' |
| 4163 |
'means\n' |
| 4164 |
'that the operator implementation for built-in types works as ' |
| 4165 |
'follows:\n' |
| 4166 |
'\n' |
| 4167 |
'* If either argument is a complex number, the other is ' |
| 4168 |
'converted to\n' |
| 4169 |
' complex;\n' |
| 4170 |
'\n' |
| 4171 |
'* otherwise, if either argument is a floating point number, ' |
| 4172 |
'the other\n' |
| 4173 |
' is converted to floating point;\n' |
| 4174 |
'\n' |
| 4175 |
'* otherwise, both must be integers and no conversion is ' |
| 4176 |
'necessary.\n' |
| 4177 |
'\n' |
| 4178 |
'Some additional rules apply for certain operators (e.g., a ' |
| 4179 |
'string as a\n' |
| 4180 |
'left argument to the ‘%’ operator). Extensions must define ' |
| 4181 |
'their own\n' |
| 4182 |
'conversion behavior.\n', |
| 4183 |
'customization': 'Basic customization\n' |
| 4184 |
'*******************\n' |
| 4185 |
'\n' |
| 4186 |
'object.__new__(cls[, ...])\n' |
| 4187 |
'\n' |
| 4188 |
' Called to create a new instance of class *cls*. ' |
| 4189 |
'"__new__()" is a\n' |
| 4190 |
' static method (special-cased so you need not declare it ' |
| 4191 |
'as such)\n' |
| 4192 |
' that takes the class of which an instance was requested ' |
| 4193 |
'as its\n' |
| 4194 |
' first argument. The remaining arguments are those ' |
| 4195 |
'passed to the\n' |
| 4196 |
' object constructor expression (the call to the class). ' |
| 4197 |
'The return\n' |
| 4198 |
' value of "__new__()" should be the new object instance ' |
| 4199 |
'(usually an\n' |
| 4200 |
' instance of *cls*).\n' |
| 4201 |
'\n' |
| 4202 |
' Typical implementations create a new instance of the ' |
| 4203 |
'class by\n' |
| 4204 |
' invoking the superclass’s "__new__()" method using\n' |
| 4205 |
' "super().__new__(cls[, ...])" with appropriate arguments ' |
| 4206 |
'and then\n' |
| 4207 |
' modifying the newly created instance as necessary before ' |
| 4208 |
'returning\n' |
| 4209 |
' it.\n' |
| 4210 |
'\n' |
| 4211 |
' If "__new__()" is invoked during object construction and ' |
| 4212 |
'it returns\n' |
| 4213 |
' an instance of *cls*, then the new instance’s ' |
| 4214 |
'"__init__()" method\n' |
| 4215 |
' will be invoked like "__init__(self[, ...])", where ' |
| 4216 |
'*self* is the\n' |
| 4217 |
' new instance and the remaining arguments are the same as ' |
| 4218 |
'were\n' |
| 4219 |
' passed to the object constructor.\n' |
| 4220 |
'\n' |
| 4221 |
' If "__new__()" does not return an instance of *cls*, ' |
| 4222 |
'then the new\n' |
| 4223 |
' instance’s "__init__()" method will not be invoked.\n' |
| 4224 |
'\n' |
| 4225 |
' "__new__()" is intended mainly to allow subclasses of ' |
| 4226 |
'immutable\n' |
| 4227 |
' types (like int, str, or tuple) to customize instance ' |
| 4228 |
'creation. It\n' |
| 4229 |
' is also commonly overridden in custom metaclasses in ' |
| 4230 |
'order to\n' |
| 4231 |
' customize class creation.\n' |
| 4232 |
'\n' |
| 4233 |
'object.__init__(self[, ...])\n' |
| 4234 |
'\n' |
| 4235 |
' Called after the instance has been created (by ' |
| 4236 |
'"__new__()"), but\n' |
| 4237 |
' before it is returned to the caller. The arguments are ' |
| 4238 |
'those\n' |
| 4239 |
' passed to the class constructor expression. If a base ' |
| 4240 |
'class has an\n' |
| 4241 |
' "__init__()" method, the derived class’s "__init__()" ' |
| 4242 |
'method, if\n' |
| 4243 |
' any, must explicitly call it to ensure proper ' |
| 4244 |
'initialization of the\n' |
| 4245 |
' base class part of the instance; for example:\n' |
| 4246 |
' "super().__init__([args...])".\n' |
| 4247 |
'\n' |
| 4248 |
' Because "__new__()" and "__init__()" work together in ' |
| 4249 |
'constructing\n' |
| 4250 |
' objects ("__new__()" to create it, and "__init__()" to ' |
| 4251 |
'customize\n' |
| 4252 |
' it), no non-"None" value may be returned by ' |
| 4253 |
'"__init__()"; doing so\n' |
| 4254 |
' will cause a "TypeError" to be raised at runtime.\n' |
| 4255 |
'\n' |
| 4256 |
'object.__del__(self)\n' |
| 4257 |
'\n' |
| 4258 |
' Called when the instance is about to be destroyed. This ' |
| 4259 |
'is also\n' |
| 4260 |
' called a finalizer or (improperly) a destructor. If a ' |
| 4261 |
'base class\n' |
| 4262 |
' has a "__del__()" method, the derived class’s ' |
| 4263 |
'"__del__()" method,\n' |
| 4264 |
' if any, must explicitly call it to ensure proper ' |
| 4265 |
'deletion of the\n' |
| 4266 |
' base class part of the instance.\n' |
| 4267 |
'\n' |
| 4268 |
' It is possible (though not recommended!) for the ' |
| 4269 |
'"__del__()" method\n' |
| 4270 |
' to postpone destruction of the instance by creating a ' |
| 4271 |
'new reference\n' |
| 4272 |
' to it. This is called object *resurrection*. It is\n' |
| 4273 |
' implementation-dependent whether "__del__()" is called a ' |
| 4274 |
'second\n' |
| 4275 |
' time when a resurrected object is about to be destroyed; ' |
| 4276 |
'the\n' |
| 4277 |
' current *CPython* implementation only calls it once.\n' |
| 4278 |
'\n' |
| 4279 |
' It is not guaranteed that "__del__()" methods are called ' |
| 4280 |
'for\n' |
| 4281 |
' objects that still exist when the interpreter exits.\n' |
| 4282 |
'\n' |
| 4283 |
' Note:\n' |
| 4284 |
'\n' |
| 4285 |
' "del x" doesn’t directly call "x.__del__()" — the ' |
| 4286 |
'former\n' |
| 4287 |
' decrements the reference count for "x" by one, and the ' |
| 4288 |
'latter is\n' |
| 4289 |
' only called when "x"’s reference count reaches zero.\n' |
| 4290 |
'\n' |
| 4291 |
' **CPython implementation detail:** It is possible for a ' |
| 4292 |
'reference\n' |
| 4293 |
' cycle to prevent the reference count of an object from ' |
| 4294 |
'going to\n' |
| 4295 |
' zero. In this case, the cycle will be later detected ' |
| 4296 |
'and deleted\n' |
| 4297 |
' by the *cyclic garbage collector*. A common cause of ' |
| 4298 |
'reference\n' |
| 4299 |
' cycles is when an exception has been caught in a local ' |
| 4300 |
'variable.\n' |
| 4301 |
' The frame’s locals then reference the exception, which ' |
| 4302 |
'references\n' |
| 4303 |
' its own traceback, which references the locals of all ' |
| 4304 |
'frames caught\n' |
| 4305 |
' in the traceback.\n' |
| 4306 |
'\n' |
| 4307 |
' See also: Documentation for the "gc" module.\n' |
| 4308 |
'\n' |
| 4309 |
' Warning:\n' |
| 4310 |
'\n' |
| 4311 |
' Due to the precarious circumstances under which ' |
| 4312 |
'"__del__()"\n' |
| 4313 |
' methods are invoked, exceptions that occur during ' |
| 4314 |
'their execution\n' |
| 4315 |
' are ignored, and a warning is printed to "sys.stderr" ' |
| 4316 |
'instead.\n' |
| 4317 |
' In particular:\n' |
| 4318 |
'\n' |
| 4319 |
' * "__del__()" can be invoked when arbitrary code is ' |
| 4320 |
'being\n' |
| 4321 |
' executed, including from any arbitrary thread. If ' |
| 4322 |
'"__del__()"\n' |
| 4323 |
' needs to take a lock or invoke any other blocking ' |
| 4324 |
'resource, it\n' |
| 4325 |
' may deadlock as the resource may already be taken by ' |
| 4326 |
'the code\n' |
| 4327 |
' that gets interrupted to execute "__del__()".\n' |
| 4328 |
'\n' |
| 4329 |
' * "__del__()" can be executed during interpreter ' |
| 4330 |
'shutdown. As a\n' |
| 4331 |
' consequence, the global variables it needs to access ' |
| 4332 |
'(including\n' |
| 4333 |
' other modules) may already have been deleted or set ' |
| 4334 |
'to "None".\n' |
| 4335 |
' Python guarantees that globals whose name begins ' |
| 4336 |
'with a single\n' |
| 4337 |
' underscore are deleted from their module before ' |
| 4338 |
'other globals\n' |
| 4339 |
' are deleted; if no other references to such globals ' |
| 4340 |
'exist, this\n' |
| 4341 |
' may help in assuring that imported modules are still ' |
| 4342 |
'available\n' |
| 4343 |
' at the time when the "__del__()" method is called.\n' |
| 4344 |
'\n' |
| 4345 |
'object.__repr__(self)\n' |
| 4346 |
'\n' |
| 4347 |
' Called by the "repr()" built-in function to compute the ' |
| 4348 |
'“official”\n' |
| 4349 |
' string representation of an object. If at all possible, ' |
| 4350 |
'this\n' |
| 4351 |
' should look like a valid Python expression that could be ' |
| 4352 |
'used to\n' |
| 4353 |
' recreate an object with the same value (given an ' |
| 4354 |
'appropriate\n' |
| 4355 |
' environment). If this is not possible, a string of the ' |
| 4356 |
'form\n' |
| 4357 |
' "<...some useful description...>" should be returned. ' |
| 4358 |
'The return\n' |
| 4359 |
' value must be a string object. If a class defines ' |
| 4360 |
'"__repr__()" but\n' |
| 4361 |
' not "__str__()", then "__repr__()" is also used when an ' |
| 4362 |
'“informal”\n' |
| 4363 |
' string representation of instances of that class is ' |
| 4364 |
'required.\n' |
| 4365 |
'\n' |
| 4366 |
' This is typically used for debugging, so it is important ' |
| 4367 |
'that the\n' |
| 4368 |
' representation is information-rich and unambiguous.\n' |
| 4369 |
'\n' |
| 4370 |
'object.__str__(self)\n' |
| 4371 |
'\n' |
| 4372 |
' Called by "str(object)" and the built-in functions ' |
| 4373 |
'"format()" and\n' |
| 4374 |
' "print()" to compute the “informal” or nicely printable ' |
| 4375 |
'string\n' |
| 4376 |
' representation of an object. The return value must be a ' |
| 4377 |
'string\n' |
| 4378 |
' object.\n' |
| 4379 |
'\n' |
| 4380 |
' This method differs from "object.__repr__()" in that ' |
| 4381 |
'there is no\n' |
| 4382 |
' expectation that "__str__()" return a valid Python ' |
| 4383 |
'expression: a\n' |
| 4384 |
' more convenient or concise representation can be used.\n' |
| 4385 |
'\n' |
| 4386 |
' The default implementation defined by the built-in type ' |
| 4387 |
'"object"\n' |
| 4388 |
' calls "object.__repr__()".\n' |
| 4389 |
'\n' |
| 4390 |
'object.__bytes__(self)\n' |
| 4391 |
'\n' |
| 4392 |
' Called by bytes to compute a byte-string representation ' |
| 4393 |
'of an\n' |
| 4394 |
' object. This should return a "bytes" object.\n' |
| 4395 |
'\n' |
| 4396 |
'object.__format__(self, format_spec)\n' |
| 4397 |
'\n' |
| 4398 |
' Called by the "format()" built-in function, and by ' |
| 4399 |
'extension,\n' |
| 4400 |
' evaluation of formatted string literals and the ' |
| 4401 |
'"str.format()"\n' |
| 4402 |
' method, to produce a “formatted” string representation ' |
| 4403 |
'of an\n' |
| 4404 |
' object. The *format_spec* argument is a string that ' |
| 4405 |
'contains a\n' |
| 4406 |
' description of the formatting options desired. The ' |
| 4407 |
'interpretation\n' |
| 4408 |
' of the *format_spec* argument is up to the type ' |
| 4409 |
'implementing\n' |
| 4410 |
' "__format__()", however most classes will either ' |
| 4411 |
'delegate\n' |
| 4412 |
' formatting to one of the built-in types, or use a ' |
| 4413 |
'similar\n' |
| 4414 |
' formatting option syntax.\n' |
| 4415 |
'\n' |
| 4416 |
' See Format Specification Mini-Language for a description ' |
| 4417 |
'of the\n' |
| 4418 |
' standard formatting syntax.\n' |
| 4419 |
'\n' |
| 4420 |
' The return value must be a string object.\n' |
| 4421 |
'\n' |
| 4422 |
' Changed in version 3.4: The __format__ method of ' |
| 4423 |
'"object" itself\n' |
| 4424 |
' raises a "TypeError" if passed any non-empty string.\n' |
| 4425 |
'\n' |
| 4426 |
' Changed in version 3.7: "object.__format__(x, \'\')" is ' |
| 4427 |
'now\n' |
| 4428 |
' equivalent to "str(x)" rather than "format(str(x), ' |
| 4429 |
'\'\')".\n' |
| 4430 |
'\n' |
| 4431 |
'object.__lt__(self, other)\n' |
| 4432 |
'object.__le__(self, other)\n' |
| 4433 |
'object.__eq__(self, other)\n' |
| 4434 |
'object.__ne__(self, other)\n' |
| 4435 |
'object.__gt__(self, other)\n' |
| 4436 |
'object.__ge__(self, other)\n' |
| 4437 |
'\n' |
| 4438 |
' These are the so-called “rich comparison” methods. The\n' |
| 4439 |
' correspondence between operator symbols and method names ' |
| 4440 |
'is as\n' |
| 4441 |
' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls ' |
| 4442 |
'"x.__le__(y)",\n' |
| 4443 |
' "x==y" calls "x.__eq__(y)", "x!=y" calls "x.__ne__(y)", ' |
| 4444 |
'"x>y" calls\n' |
| 4445 |
' "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".\n' |
| 4446 |
'\n' |
| 4447 |
' A rich comparison method may return the singleton ' |
| 4448 |
'"NotImplemented"\n' |
| 4449 |
' if it does not implement the operation for a given pair ' |
| 4450 |
'of\n' |
| 4451 |
' arguments. By convention, "False" and "True" are ' |
| 4452 |
'returned for a\n' |
| 4453 |
' successful comparison. However, these methods can return ' |
| 4454 |
'any value,\n' |
| 4455 |
' so if the comparison operator is used in a Boolean ' |
| 4456 |
'context (e.g.,\n' |
| 4457 |
' in the condition of an "if" statement), Python will call ' |
| 4458 |
'"bool()"\n' |
| 4459 |
' on the value to determine if the result is true or ' |
| 4460 |
'false.\n' |
| 4461 |
'\n' |
| 4462 |
' By default, "object" implements "__eq__()" by using ' |
| 4463 |
'"is", returning\n' |
| 4464 |
' "NotImplemented" in the case of a false comparison: ' |
| 4465 |
'"True if x is y\n' |
| 4466 |
' else NotImplemented". For "__ne__()", by default it ' |
| 4467 |
'delegates to\n' |
| 4468 |
' "__eq__()" and inverts the result unless it is ' |
| 4469 |
'"NotImplemented".\n' |
| 4470 |
' There are no other implied relationships among the ' |
| 4471 |
'comparison\n' |
| 4472 |
' operators or default implementations; for example, the ' |
| 4473 |
'truth of\n' |
| 4474 |
' "(x<y or x==y)" does not imply "x<=y". To automatically ' |
| 4475 |
'generate\n' |
| 4476 |
' ordering operations from a single root operation, see\n' |
| 4477 |
' "functools.total_ordering()".\n' |
| 4478 |
'\n' |
| 4479 |
' See the paragraph on "__hash__()" for some important ' |
| 4480 |
'notes on\n' |
| 4481 |
' creating *hashable* objects which support custom ' |
| 4482 |
'comparison\n' |
| 4483 |
' operations and are usable as dictionary keys.\n' |
| 4484 |
'\n' |
| 4485 |
' There are no swapped-argument versions of these methods ' |
| 4486 |
'(to be used\n' |
| 4487 |
' when the left argument does not support the operation ' |
| 4488 |
'but the right\n' |
| 4489 |
' argument does); rather, "__lt__()" and "__gt__()" are ' |
| 4490 |
'each other’s\n' |
| 4491 |
' reflection, "__le__()" and "__ge__()" are each other’s ' |
| 4492 |
'reflection,\n' |
| 4493 |
' and "__eq__()" and "__ne__()" are their own reflection. ' |
| 4494 |
'If the\n' |
| 4495 |
' operands are of different types, and the right operand’s ' |
| 4496 |
'type is a\n' |
| 4497 |
' direct or indirect subclass of the left operand’s type, ' |
| 4498 |
'the\n' |
| 4499 |
' reflected method of the right operand has priority, ' |
| 4500 |
'otherwise the\n' |
| 4501 |
' left operand’s method has priority. Virtual subclassing ' |
| 4502 |
'is not\n' |
| 4503 |
' considered.\n' |
| 4504 |
'\n' |
| 4505 |
' When no appropriate method returns any value other than\n' |
| 4506 |
' "NotImplemented", the "==" and "!=" operators will fall ' |
| 4507 |
'back to\n' |
| 4508 |
' "is" and "is not", respectively.\n' |
| 4509 |
'\n' |
| 4510 |
'object.__hash__(self)\n' |
| 4511 |
'\n' |
| 4512 |
' Called by built-in function "hash()" and for operations ' |
| 4513 |
'on members\n' |
| 4514 |
' of hashed collections including "set", "frozenset", and ' |
| 4515 |
'"dict".\n' |
| 4516 |
' The "__hash__()" method should return an integer. The ' |
| 4517 |
'only required\n' |
| 4518 |
' property is that objects which compare equal have the ' |
| 4519 |
'same hash\n' |
| 4520 |
' value; it is advised to mix together the hash values of ' |
| 4521 |
'the\n' |
| 4522 |
' components of the object that also play a part in ' |
| 4523 |
'comparison of\n' |
| 4524 |
' objects by packing them into a tuple and hashing the ' |
| 4525 |
'tuple.\n' |
| 4526 |
' Example:\n' |
| 4527 |
'\n' |
| 4528 |
' def __hash__(self):\n' |
| 4529 |
' return hash((self.name, self.nick, self.color))\n' |
| 4530 |
'\n' |
| 4531 |
' Note:\n' |
| 4532 |
'\n' |
| 4533 |
' "hash()" truncates the value returned from an object’s ' |
| 4534 |
'custom\n' |
| 4535 |
' "__hash__()" method to the size of a "Py_ssize_t". ' |
| 4536 |
'This is\n' |
| 4537 |
' typically 8 bytes on 64-bit builds and 4 bytes on ' |
| 4538 |
'32-bit builds.\n' |
| 4539 |
' If an object’s "__hash__()" must interoperate on ' |
| 4540 |
'builds of\n' |
| 4541 |
' different bit sizes, be sure to check the width on all ' |
| 4542 |
'supported\n' |
| 4543 |
' builds. An easy way to do this is with "python -c ' |
| 4544 |
'"import sys;\n' |
| 4545 |
' print(sys.hash_info.width)"".\n' |
| 4546 |
'\n' |
| 4547 |
' If a class does not define an "__eq__()" method it ' |
| 4548 |
'should not\n' |
| 4549 |
' define a "__hash__()" operation either; if it defines ' |
| 4550 |
'"__eq__()"\n' |
| 4551 |
' but not "__hash__()", its instances will not be usable ' |
| 4552 |
'as items in\n' |
| 4553 |
' hashable collections. If a class defines mutable ' |
| 4554 |
'objects and\n' |
| 4555 |
' implements an "__eq__()" method, it should not ' |
| 4556 |
'implement\n' |
| 4557 |
' "__hash__()", since the implementation of *hashable* ' |
| 4558 |
'collections\n' |
| 4559 |
' requires that a key’s hash value is immutable (if the ' |
| 4560 |
'object’s hash\n' |
| 4561 |
' value changes, it will be in the wrong hash bucket).\n' |
| 4562 |
'\n' |
| 4563 |
' User-defined classes have "__eq__()" and "__hash__()" ' |
| 4564 |
'methods by\n' |
| 4565 |
' default; with them, all objects compare unequal (except ' |
| 4566 |
'with\n' |
| 4567 |
' themselves) and "x.__hash__()" returns an appropriate ' |
| 4568 |
'value such\n' |
| 4569 |
' that "x == y" implies both that "x is y" and "hash(x) == ' |
| 4570 |
'hash(y)".\n' |
| 4571 |
'\n' |
| 4572 |
' A class that overrides "__eq__()" and does not define ' |
| 4573 |
'"__hash__()"\n' |
| 4574 |
' will have its "__hash__()" implicitly set to "None". ' |
| 4575 |
'When the\n' |
| 4576 |
' "__hash__()" method of a class is "None", instances of ' |
| 4577 |
'the class\n' |
| 4578 |
' will raise an appropriate "TypeError" when a program ' |
| 4579 |
'attempts to\n' |
| 4580 |
' retrieve their hash value, and will also be correctly ' |
| 4581 |
'identified as\n' |
| 4582 |
' unhashable when checking "isinstance(obj,\n' |
| 4583 |
' collections.abc.Hashable)".\n' |
| 4584 |
'\n' |
| 4585 |
' If a class that overrides "__eq__()" needs to retain ' |
| 4586 |
'the\n' |
| 4587 |
' implementation of "__hash__()" from a parent class, the ' |
| 4588 |
'interpreter\n' |
| 4589 |
' must be told this explicitly by setting "__hash__ =\n' |
| 4590 |
' <ParentClass>.__hash__".\n' |
| 4591 |
'\n' |
| 4592 |
' If a class that does not override "__eq__()" wishes to ' |
| 4593 |
'suppress\n' |
| 4594 |
' hash support, it should include "__hash__ = None" in the ' |
| 4595 |
'class\n' |
| 4596 |
' definition. A class which defines its own "__hash__()" ' |
| 4597 |
'that\n' |
| 4598 |
' explicitly raises a "TypeError" would be incorrectly ' |
| 4599 |
'identified as\n' |
| 4600 |
' hashable by an "isinstance(obj, ' |
| 4601 |
'collections.abc.Hashable)" call.\n' |
| 4602 |
'\n' |
| 4603 |
' Note:\n' |
| 4604 |
'\n' |
| 4605 |
' By default, the "__hash__()" values of str and bytes ' |
| 4606 |
'objects are\n' |
| 4607 |
' “salted” with an unpredictable random value. Although ' |
| 4608 |
'they\n' |
| 4609 |
' remain constant within an individual Python process, ' |
| 4610 |
'they are not\n' |
| 4611 |
' predictable between repeated invocations of ' |
| 4612 |
'Python.This is\n' |
| 4613 |
' intended to provide protection against a ' |
| 4614 |
'denial-of-service caused\n' |
| 4615 |
' by carefully chosen inputs that exploit the worst ' |
| 4616 |
'case\n' |
| 4617 |
' performance of a dict insertion, *O*(*n*^2) ' |
| 4618 |
'complexity. See\n' |
| 4619 |
' http://ocert.org/advisories/ocert-2011-003.html for\n' |
| 4620 |
' details.Changing hash values affects the iteration ' |
| 4621 |
'order of sets.\n' |
| 4622 |
' Python has never made guarantees about this ordering ' |
| 4623 |
'(and it\n' |
| 4624 |
' typically varies between 32-bit and 64-bit builds).See ' |
| 4625 |
'also\n' |
| 4626 |
' "PYTHONHASHSEED".\n' |
| 4627 |
'\n' |
| 4628 |
' Changed in version 3.3: Hash randomization is enabled by ' |
| 4629 |
'default.\n' |
| 4630 |
'\n' |
| 4631 |
'object.__bool__(self)\n' |
| 4632 |
'\n' |
| 4633 |
' Called to implement truth value testing and the built-in ' |
| 4634 |
'operation\n' |
| 4635 |
' "bool()"; should return "False" or "True". When this ' |
| 4636 |
'method is not\n' |
| 4637 |
' defined, "__len__()" is called, if it is defined, and ' |
| 4638 |
'the object is\n' |
| 4639 |
' considered true if its result is nonzero. If a class ' |
| 4640 |
'defines\n' |
| 4641 |
' neither "__len__()" nor "__bool__()", all its instances ' |
| 4642 |
'are\n' |
| 4643 |
' considered true.\n', |
| 4644 |
'debugger': '"pdb" — The Python Debugger\n' |
| 4645 |
'***************************\n' |
| 4646 |
'\n' |
| 4647 |
'**Source code:** Lib/pdb.py\n' |
| 4648 |
'\n' |
| 4649 |
'======================================================================\n' |
| 4650 |
'\n' |
| 4651 |
'The module "pdb" defines an interactive source code debugger ' |
| 4652 |
'for\n' |
| 4653 |
'Python programs. It supports setting (conditional) breakpoints ' |
| 4654 |
'and\n' |
| 4655 |
'single stepping at the source line level, inspection of stack ' |
| 4656 |
'frames,\n' |
| 4657 |
'source code listing, and evaluation of arbitrary Python code in ' |
| 4658 |
'the\n' |
| 4659 |
'context of any stack frame. It also supports post-mortem ' |
| 4660 |
'debugging\n' |
| 4661 |
'and can be called under program control.\n' |
| 4662 |
'\n' |
| 4663 |
'The debugger is extensible – it is actually defined as the ' |
| 4664 |
'class\n' |
| 4665 |
'"Pdb". This is currently undocumented but easily understood by ' |
| 4666 |
'reading\n' |
| 4667 |
'the source. The extension interface uses the modules "bdb" and ' |
| 4668 |
'"cmd".\n' |
| 4669 |
'\n' |
| 4670 |
'See also:\n' |
| 4671 |
'\n' |
| 4672 |
' Module "faulthandler"\n' |
| 4673 |
' Used to dump Python tracebacks explicitly, on a fault, ' |
| 4674 |
'after a\n' |
| 4675 |
' timeout, or on a user signal.\n' |
| 4676 |
'\n' |
| 4677 |
' Module "traceback"\n' |
| 4678 |
' Standard interface to extract, format and print stack ' |
| 4679 |
'traces of\n' |
| 4680 |
' Python programs.\n' |
| 4681 |
'\n' |
| 4682 |
'The typical usage to break into the debugger is to insert:\n' |
| 4683 |
'\n' |
| 4684 |
' import pdb; pdb.set_trace()\n' |
| 4685 |
'\n' |
| 4686 |
'Or:\n' |
| 4687 |
'\n' |
| 4688 |
' breakpoint()\n' |
| 4689 |
'\n' |
| 4690 |
'at the location you want to break into the debugger, and then ' |
| 4691 |
'run the\n' |
| 4692 |
'program. You can then step through the code following this ' |
| 4693 |
'statement,\n' |
| 4694 |
'and continue running without the debugger using the "continue"\n' |
| 4695 |
'command.\n' |
| 4696 |
'\n' |
| 4697 |
'Changed in version 3.7: The built-in "breakpoint()", when called ' |
| 4698 |
'with\n' |
| 4699 |
'defaults, can be used instead of "import pdb; pdb.set_trace()".\n' |
| 4700 |
'\n' |
| 4701 |
' def double(x):\n' |
| 4702 |
' breakpoint()\n' |
| 4703 |
' return x * 2\n' |
| 4704 |
' val = 3\n' |
| 4705 |
' print(f"{val} * 2 is {double(val)}")\n' |
| 4706 |
'\n' |
| 4707 |
'The debugger’s prompt is "(Pdb)", which is the indicator that ' |
| 4708 |
'you are\n' |
| 4709 |
'in debug mode:\n' |
| 4710 |
'\n' |
| 4711 |
' > ...(3)double()\n' |
| 4712 |
' -> return x * 2\n' |
| 4713 |
' (Pdb) p x\n' |
| 4714 |
' 3\n' |
| 4715 |
' (Pdb) continue\n' |
| 4716 |
' 3 * 2 is 6\n' |
| 4717 |
'\n' |
| 4718 |
'Changed in version 3.3: Tab-completion via the "readline" module ' |
| 4719 |
'is\n' |
| 4720 |
'available for commands and command arguments, e.g. the current ' |
| 4721 |
'global\n' |
| 4722 |
'and local names are offered as arguments of the "p" command.\n' |
| 4723 |
'\n' |
| 4724 |
'You can also invoke "pdb" from the command line to debug other\n' |
| 4725 |
'scripts. For example:\n' |
| 4726 |
'\n' |
| 4727 |
' python -m pdb myscript.py\n' |
| 4728 |
'\n' |
| 4729 |
'When invoked as a module, pdb will automatically enter ' |
| 4730 |
'post-mortem\n' |
| 4731 |
'debugging if the program being debugged exits abnormally. After ' |
| 4732 |
'post-\n' |
| 4733 |
'mortem debugging (or after normal exit of the program), pdb ' |
| 4734 |
'will\n' |
| 4735 |
'restart the program. Automatic restarting preserves pdb’s state ' |
| 4736 |
'(such\n' |
| 4737 |
'as breakpoints) and in most cases is more useful than quitting ' |
| 4738 |
'the\n' |
| 4739 |
'debugger upon program’s exit.\n' |
| 4740 |
'\n' |
| 4741 |
'Changed in version 3.2: Added the "-c" option to execute ' |
| 4742 |
'commands as\n' |
| 4743 |
'if given in a ".pdbrc" file; see Debugger Commands.\n' |
| 4744 |
'\n' |
| 4745 |
'Changed in version 3.7: Added the "-m" option to execute ' |
| 4746 |
'modules\n' |
| 4747 |
'similar to the way "python -m" does. As with a script, the ' |
| 4748 |
'debugger\n' |
| 4749 |
'will pause execution just before the first line of the module.\n' |
| 4750 |
'\n' |
| 4751 |
'Typical usage to execute a statement under control of the ' |
| 4752 |
'debugger is:\n' |
| 4753 |
'\n' |
| 4754 |
' >>> import pdb\n' |
| 4755 |
' >>> def f(x):\n' |
| 4756 |
' ... print(1 / x)\n' |
| 4757 |
' >>> pdb.run("f(2)")\n' |
| 4758 |
' > <string>(1)<module>()\n' |
| 4759 |
' (Pdb) continue\n' |
| 4760 |
' 0.5\n' |
| 4761 |
' >>>\n' |
| 4762 |
'\n' |
| 4763 |
'The typical usage to inspect a crashed program is:\n' |
| 4764 |
'\n' |
| 4765 |
' >>> import pdb\n' |
| 4766 |
' >>> def f(x):\n' |
| 4767 |
' ... print(1 / x)\n' |
| 4768 |
' ...\n' |
| 4769 |
' >>> f(0)\n' |
| 4770 |
' Traceback (most recent call last):\n' |
| 4771 |
' File "<stdin>", line 1, in <module>\n' |
| 4772 |
' File "<stdin>", line 2, in f\n' |
| 4773 |
' ZeroDivisionError: division by zero\n' |
| 4774 |
' >>> pdb.pm()\n' |
| 4775 |
' > <stdin>(2)f()\n' |
| 4776 |
' (Pdb) p x\n' |
| 4777 |
' 0\n' |
| 4778 |
' (Pdb)\n' |
| 4779 |
'\n' |
| 4780 |
'The module defines the following functions; each enters the ' |
| 4781 |
'debugger\n' |
| 4782 |
'in a slightly different way:\n' |
| 4783 |
'\n' |
| 4784 |
'pdb.run(statement, globals=None, locals=None)\n' |
| 4785 |
'\n' |
| 4786 |
' Execute the *statement* (given as a string or a code object) ' |
| 4787 |
'under\n' |
| 4788 |
' debugger control. The debugger prompt appears before any ' |
| 4789 |
'code is\n' |
| 4790 |
' executed; you can set breakpoints and type "continue", or you ' |
| 4791 |
'can\n' |
| 4792 |
' step through the statement using "step" or "next" (all these\n' |
| 4793 |
' commands are explained below). The optional *globals* and ' |
| 4794 |
'*locals*\n' |
| 4795 |
' arguments specify the environment in which the code is ' |
| 4796 |
'executed; by\n' |
| 4797 |
' default the dictionary of the module "__main__" is used. ' |
| 4798 |
'(See the\n' |
| 4799 |
' explanation of the built-in "exec()" or "eval()" functions.)\n' |
| 4800 |
'\n' |
| 4801 |
'pdb.runeval(expression, globals=None, locals=None)\n' |
| 4802 |
'\n' |
| 4803 |
' Evaluate the *expression* (given as a string or a code ' |
| 4804 |
'object)\n' |
| 4805 |
' under debugger control. When "runeval()" returns, it returns ' |
| 4806 |
'the\n' |
| 4807 |
' value of the *expression*. Otherwise this function is ' |
| 4808 |
'similar to\n' |
| 4809 |
' "run()".\n' |
| 4810 |
'\n' |
| 4811 |
'pdb.runcall(function, *args, **kwds)\n' |
| 4812 |
'\n' |
| 4813 |
' Call the *function* (a function or method object, not a ' |
| 4814 |
'string)\n' |
| 4815 |
' with the given arguments. When "runcall()" returns, it ' |
| 4816 |
'returns\n' |
| 4817 |
' whatever the function call returned. The debugger prompt ' |
| 4818 |
'appears\n' |
| 4819 |
' as soon as the function is entered.\n' |
| 4820 |
'\n' |
| 4821 |
'pdb.set_trace(*, header=None)\n' |
| 4822 |
'\n' |
| 4823 |
' Enter the debugger at the calling stack frame. This is ' |
| 4824 |
'useful to\n' |
| 4825 |
' hard-code a breakpoint at a given point in a program, even if ' |
| 4826 |
'the\n' |
| 4827 |
' code is not otherwise being debugged (e.g. when an assertion\n' |
| 4828 |
' fails). If given, *header* is printed to the console just ' |
| 4829 |
'before\n' |
| 4830 |
' debugging begins.\n' |
| 4831 |
'\n' |
| 4832 |
' Changed in version 3.7: The keyword-only argument *header*.\n' |
| 4833 |
'\n' |
| 4834 |
'pdb.post_mortem(traceback=None)\n' |
| 4835 |
'\n' |
| 4836 |
' Enter post-mortem debugging of the given *traceback* object. ' |
| 4837 |
'If no\n' |
| 4838 |
' *traceback* is given, it uses the one of the exception that ' |
| 4839 |
'is\n' |
| 4840 |
' currently being handled (an exception must be being handled ' |
| 4841 |
'if the\n' |
| 4842 |
' default is to be used).\n' |
| 4843 |
'\n' |
| 4844 |
'pdb.pm()\n' |
| 4845 |
'\n' |
| 4846 |
' Enter post-mortem debugging of the traceback found in\n' |
| 4847 |
' "sys.last_traceback".\n' |
| 4848 |
'\n' |
| 4849 |
'The "run*" functions and "set_trace()" are aliases for ' |
| 4850 |
'instantiating\n' |
| 4851 |
'the "Pdb" class and calling the method of the same name. If you ' |
| 4852 |
'want\n' |
| 4853 |
'to access further features, you have to do this yourself:\n' |
| 4854 |
'\n' |
| 4855 |
"class pdb.Pdb(completekey='tab', stdin=None, stdout=None, " |
| 4856 |
'skip=None, nosigint=False, readrc=True)\n' |
| 4857 |
'\n' |
| 4858 |
' "Pdb" is the debugger class.\n' |
| 4859 |
'\n' |
| 4860 |
' The *completekey*, *stdin* and *stdout* arguments are passed ' |
| 4861 |
'to the\n' |
| 4862 |
' underlying "cmd.Cmd" class; see the description there.\n' |
| 4863 |
'\n' |
| 4864 |
' The *skip* argument, if given, must be an iterable of ' |
| 4865 |
'glob-style\n' |
| 4866 |
' module name patterns. The debugger will not step into frames ' |
| 4867 |
'that\n' |
| 4868 |
' originate in a module that matches one of these patterns. ' |
| 4869 |
'[1]\n' |
| 4870 |
'\n' |
| 4871 |
' By default, Pdb sets a handler for the SIGINT signal (which ' |
| 4872 |
'is sent\n' |
| 4873 |
' when the user presses "Ctrl-C" on the console) when you give ' |
| 4874 |
'a\n' |
| 4875 |
' "continue" command. This allows you to break into the ' |
| 4876 |
'debugger\n' |
| 4877 |
' again by pressing "Ctrl-C". If you want Pdb not to touch ' |
| 4878 |
'the\n' |
| 4879 |
' SIGINT handler, set *nosigint* to true.\n' |
| 4880 |
'\n' |
| 4881 |
' The *readrc* argument defaults to true and controls whether ' |
| 4882 |
'Pdb\n' |
| 4883 |
' will load .pdbrc files from the filesystem.\n' |
| 4884 |
'\n' |
| 4885 |
' Example call to enable tracing with *skip*:\n' |
| 4886 |
'\n' |
| 4887 |
" import pdb; pdb.Pdb(skip=['django.*']).set_trace()\n" |
| 4888 |
'\n' |
| 4889 |
' Raises an auditing event "pdb.Pdb" with no arguments.\n' |
| 4890 |
'\n' |
| 4891 |
' Changed in version 3.1: Added the *skip* parameter.\n' |
| 4892 |
'\n' |
| 4893 |
' Changed in version 3.2: Added the *nosigint* parameter. ' |
| 4894 |
'Previously,\n' |
| 4895 |
' a SIGINT handler was never set by Pdb.\n' |
| 4896 |
'\n' |
| 4897 |
' Changed in version 3.6: The *readrc* argument.\n' |
| 4898 |
'\n' |
| 4899 |
' run(statement, globals=None, locals=None)\n' |
| 4900 |
' runeval(expression, globals=None, locals=None)\n' |
| 4901 |
' runcall(function, *args, **kwds)\n' |
| 4902 |
' set_trace()\n' |
| 4903 |
'\n' |
| 4904 |
' See the documentation for the functions explained above.\n' |
| 4905 |
'\n' |
| 4906 |
'\n' |
| 4907 |
'Debugger Commands\n' |
| 4908 |
'=================\n' |
| 4909 |
'\n' |
| 4910 |
'The commands recognized by the debugger are listed below. Most\n' |
| 4911 |
'commands can be abbreviated to one or two letters as indicated; ' |
| 4912 |
'e.g.\n' |
| 4913 |
'"h(elp)" means that either "h" or "help" can be used to enter ' |
| 4914 |
'the help\n' |
| 4915 |
'command (but not "he" or "hel", nor "H" or "Help" or "HELP").\n' |
| 4916 |
'Arguments to commands must be separated by whitespace (spaces ' |
| 4917 |
'or\n' |
| 4918 |
'tabs). Optional arguments are enclosed in square brackets ' |
| 4919 |
'("[]") in\n' |
| 4920 |
'the command syntax; the square brackets must not be typed.\n' |
| 4921 |
'Alternatives in the command syntax are separated by a vertical ' |
| 4922 |
'bar\n' |
| 4923 |
'("|").\n' |
| 4924 |
'\n' |
| 4925 |
'Entering a blank line repeats the last command entered. ' |
| 4926 |
'Exception: if\n' |
| 4927 |
'the last command was a "list" command, the next 11 lines are ' |
| 4928 |
'listed.\n' |
| 4929 |
'\n' |
| 4930 |
'Commands that the debugger doesn’t recognize are assumed to be ' |
| 4931 |
'Python\n' |
| 4932 |
'statements and are executed in the context of the program being\n' |
| 4933 |
'debugged. Python statements can also be prefixed with an ' |
| 4934 |
'exclamation\n' |
| 4935 |
'point ("!"). This is a powerful way to inspect the program ' |
| 4936 |
'being\n' |
| 4937 |
'debugged; it is even possible to change a variable or call a ' |
| 4938 |
'function.\n' |
| 4939 |
'When an exception occurs in such a statement, the exception name ' |
| 4940 |
'is\n' |
| 4941 |
'printed but the debugger’s state is not changed.\n' |
| 4942 |
'\n' |
| 4943 |
'The debugger supports aliases. Aliases can have parameters ' |
| 4944 |
'which\n' |
| 4945 |
'allows one a certain level of adaptability to the context under\n' |
| 4946 |
'examination.\n' |
| 4947 |
'\n' |
| 4948 |
'Multiple commands may be entered on a single line, separated by ' |
| 4949 |
'";;".\n' |
| 4950 |
'(A single ";" is not used as it is the separator for multiple ' |
| 4951 |
'commands\n' |
| 4952 |
'in a line that is passed to the Python parser.) No intelligence ' |
| 4953 |
'is\n' |
| 4954 |
'applied to separating the commands; the input is split at the ' |
| 4955 |
'first\n' |
| 4956 |
'";;" pair, even if it is in the middle of a quoted string. A\n' |
| 4957 |
'workaround for strings with double semicolons is to use ' |
| 4958 |
'implicit\n' |
| 4959 |
'string concatenation "\';\'\';\'" or "";"";"".\n' |
| 4960 |
'\n' |
| 4961 |
'If a file ".pdbrc" exists in the user’s home directory or in ' |
| 4962 |
'the\n' |
| 4963 |
'current directory, it is read with "\'utf-8\'" encoding and ' |
| 4964 |
'executed as\n' |
| 4965 |
'if it had been typed at the debugger prompt, with the exception ' |
| 4966 |
'that\n' |
| 4967 |
'empty lines and lines starting with "#" are ignored. This is\n' |
| 4968 |
'particularly useful for aliases. If both files exist, the one ' |
| 4969 |
'in the\n' |
| 4970 |
'home directory is read first and aliases defined there can be\n' |
| 4971 |
'overridden by the local file.\n' |
| 4972 |
'\n' |
| 4973 |
'Changed in version 3.2: ".pdbrc" can now contain commands that\n' |
| 4974 |
'continue debugging, such as "continue" or "next". Previously, ' |
| 4975 |
'these\n' |
| 4976 |
'commands had no effect.\n' |
| 4977 |
'\n' |
| 4978 |
'Changed in version 3.11: ".pdbrc" is now read with "\'utf-8\'" ' |
| 4979 |
'encoding.\n' |
| 4980 |
'Previously, it was read with the system locale encoding.\n' |
| 4981 |
'\n' |
| 4982 |
'h(elp) [command]\n' |
| 4983 |
'\n' |
| 4984 |
' Without argument, print the list of available commands. With ' |
| 4985 |
'a\n' |
| 4986 |
' *command* as argument, print help about that command. "help ' |
| 4987 |
'pdb"\n' |
| 4988 |
' displays the full documentation (the docstring of the "pdb"\n' |
| 4989 |
' module). Since the *command* argument must be an identifier, ' |
| 4990 |
'"help\n' |
| 4991 |
' exec" must be entered to get help on the "!" command.\n' |
| 4992 |
'\n' |
| 4993 |
'w(here)\n' |
| 4994 |
'\n' |
| 4995 |
' Print a stack trace, with the most recent frame at the ' |
| 4996 |
'bottom. An\n' |
| 4997 |
' arrow (">") indicates the current frame, which determines ' |
| 4998 |
'the\n' |
| 4999 |
' context of most commands.\n' |
| 5000 |
'\n' |
| 5001 |
'd(own) [count]\n' |
| 5002 |
'\n' |
| 5003 |
' Move the current frame *count* (default one) levels down in ' |
| 5004 |
'the\n' |
| 5005 |
' stack trace (to a newer frame).\n' |
| 5006 |
'\n' |
| 5007 |
'u(p) [count]\n' |
| 5008 |
'\n' |
| 5009 |
' Move the current frame *count* (default one) levels up in the ' |
| 5010 |
'stack\n' |
| 5011 |
' trace (to an older frame).\n' |
| 5012 |
'\n' |
| 5013 |
'b(reak) [([filename:]lineno | function) [, condition]]\n' |
| 5014 |
'\n' |
| 5015 |
' With a *lineno* argument, set a break there in the current ' |
| 5016 |
'file.\n' |
| 5017 |
' With a *function* argument, set a break at the first ' |
| 5018 |
'executable\n' |
| 5019 |
' statement within that function. The line number may be ' |
| 5020 |
'prefixed\n' |
| 5021 |
' with a filename and a colon, to specify a breakpoint in ' |
| 5022 |
'another\n' |
| 5023 |
' file (probably one that hasn’t been loaded yet). The file ' |
| 5024 |
'is\n' |
| 5025 |
' searched on "sys.path". Note that each breakpoint is ' |
| 5026 |
'assigned a\n' |
| 5027 |
' number to which all the other breakpoint commands refer.\n' |
| 5028 |
'\n' |
| 5029 |
' If a second argument is present, it is an expression which ' |
| 5030 |
'must\n' |
| 5031 |
' evaluate to true before the breakpoint is honored.\n' |
| 5032 |
'\n' |
| 5033 |
' Without argument, list all breaks, including for each ' |
| 5034 |
'breakpoint,\n' |
| 5035 |
' the number of times that breakpoint has been hit, the ' |
| 5036 |
'current\n' |
| 5037 |
' ignore count, and the associated condition if any.\n' |
| 5038 |
'\n' |
| 5039 |
'tbreak [([filename:]lineno | function) [, condition]]\n' |
| 5040 |
'\n' |
| 5041 |
' Temporary breakpoint, which is removed automatically when it ' |
| 5042 |
'is\n' |
| 5043 |
' first hit. The arguments are the same as for "break".\n' |
| 5044 |
'\n' |
| 5045 |
'cl(ear) [filename:lineno | bpnumber ...]\n' |
| 5046 |
'\n' |
| 5047 |
' With a *filename:lineno* argument, clear all the breakpoints ' |
| 5048 |
'at\n' |
| 5049 |
' this line. With a space separated list of breakpoint numbers, ' |
| 5050 |
'clear\n' |
| 5051 |
' those breakpoints. Without argument, clear all breaks (but ' |
| 5052 |
'first\n' |
| 5053 |
' ask confirmation).\n' |
| 5054 |
'\n' |
| 5055 |
'disable [bpnumber ...]\n' |
| 5056 |
'\n' |
| 5057 |
' Disable the breakpoints given as a space separated list of\n' |
| 5058 |
' breakpoint numbers. Disabling a breakpoint means it cannot ' |
| 5059 |
'cause\n' |
| 5060 |
' the program to stop execution, but unlike clearing a ' |
| 5061 |
'breakpoint, it\n' |
| 5062 |
' remains in the list of breakpoints and can be (re-)enabled.\n' |
| 5063 |
'\n' |
| 5064 |
'enable [bpnumber ...]\n' |
| 5065 |
'\n' |
| 5066 |
' Enable the breakpoints specified.\n' |
| 5067 |
'\n' |
| 5068 |
'ignore bpnumber [count]\n' |
| 5069 |
'\n' |
| 5070 |
' Set the ignore count for the given breakpoint number. If ' |
| 5071 |
'*count*\n' |
| 5072 |
' is omitted, the ignore count is set to 0. A breakpoint ' |
| 5073 |
'becomes\n' |
| 5074 |
' active when the ignore count is zero. When non-zero, the ' |
| 5075 |
'*count*\n' |
| 5076 |
' is decremented each time the breakpoint is reached and the\n' |
| 5077 |
' breakpoint is not disabled and any associated condition ' |
| 5078 |
'evaluates\n' |
| 5079 |
' to true.\n' |
| 5080 |
'\n' |
| 5081 |
'condition bpnumber [condition]\n' |
| 5082 |
'\n' |
| 5083 |
' Set a new *condition* for the breakpoint, an expression which ' |
| 5084 |
'must\n' |
| 5085 |
' evaluate to true before the breakpoint is honored. If ' |
| 5086 |
'*condition*\n' |
| 5087 |
' is absent, any existing condition is removed; i.e., the ' |
| 5088 |
'breakpoint\n' |
| 5089 |
' is made unconditional.\n' |
| 5090 |
'\n' |
| 5091 |
'commands [bpnumber]\n' |
| 5092 |
'\n' |
| 5093 |
' Specify a list of commands for breakpoint number *bpnumber*. ' |
| 5094 |
'The\n' |
| 5095 |
' commands themselves appear on the following lines. Type a ' |
| 5096 |
'line\n' |
| 5097 |
' containing just "end" to terminate the commands. An example:\n' |
| 5098 |
'\n' |
| 5099 |
' (Pdb) commands 1\n' |
| 5100 |
' (com) p some_variable\n' |
| 5101 |
' (com) end\n' |
| 5102 |
' (Pdb)\n' |
| 5103 |
'\n' |
| 5104 |
' To remove all commands from a breakpoint, type "commands" ' |
| 5105 |
'and\n' |
| 5106 |
' follow it immediately with "end"; that is, give no commands.\n' |
| 5107 |
'\n' |
| 5108 |
' With no *bpnumber* argument, "commands" refers to the last\n' |
| 5109 |
' breakpoint set.\n' |
| 5110 |
'\n' |
| 5111 |
' You can use breakpoint commands to start your program up ' |
| 5112 |
'again.\n' |
| 5113 |
' Simply use the "continue" command, or "step", or any other ' |
| 5114 |
'command\n' |
| 5115 |
' that resumes execution.\n' |
| 5116 |
'\n' |
| 5117 |
' Specifying any command resuming execution (currently ' |
| 5118 |
'"continue",\n' |
| 5119 |
' "step", "next", "return", "jump", "quit" and their ' |
| 5120 |
'abbreviations)\n' |
| 5121 |
' terminates the command list (as if that command was ' |
| 5122 |
'immediately\n' |
| 5123 |
' followed by end). This is because any time you resume ' |
| 5124 |
'execution\n' |
| 5125 |
' (even with a simple next or step), you may encounter another\n' |
| 5126 |
' breakpoint—which could have its own command list, leading to\n' |
| 5127 |
' ambiguities about which list to execute.\n' |
| 5128 |
'\n' |
| 5129 |
' If you use the "silent" command in the command list, the ' |
| 5130 |
'usual\n' |
| 5131 |
' message about stopping at a breakpoint is not printed. This ' |
| 5132 |
'may be\n' |
| 5133 |
' desirable for breakpoints that are to print a specific ' |
| 5134 |
'message and\n' |
| 5135 |
' then continue. If none of the other commands print anything, ' |
| 5136 |
'you\n' |
| 5137 |
' see no sign that the breakpoint was reached.\n' |
| 5138 |
'\n' |
| 5139 |
's(tep)\n' |
| 5140 |
'\n' |
| 5141 |
' Execute the current line, stop at the first possible ' |
| 5142 |
'occasion\n' |
| 5143 |
' (either in a function that is called or on the next line in ' |
| 5144 |
'the\n' |
| 5145 |
' current function).\n' |
| 5146 |
'\n' |
| 5147 |
'n(ext)\n' |
| 5148 |
'\n' |
| 5149 |
' Continue execution until the next line in the current ' |
| 5150 |
'function is\n' |
| 5151 |
' reached or it returns. (The difference between "next" and ' |
| 5152 |
'"step"\n' |
| 5153 |
' is that "step" stops inside a called function, while "next"\n' |
| 5154 |
' executes called functions at (nearly) full speed, only ' |
| 5155 |
'stopping at\n' |
| 5156 |
' the next line in the current function.)\n' |
| 5157 |
'\n' |
| 5158 |
'unt(il) [lineno]\n' |
| 5159 |
'\n' |
| 5160 |
' Without argument, continue execution until the line with a ' |
| 5161 |
'number\n' |
| 5162 |
' greater than the current one is reached.\n' |
| 5163 |
'\n' |
| 5164 |
' With *lineno*, continue execution until a line with a number\n' |
| 5165 |
' greater or equal to *lineno* is reached. In both cases, also ' |
| 5166 |
'stop\n' |
| 5167 |
' when the current frame returns.\n' |
| 5168 |
'\n' |
| 5169 |
' Changed in version 3.2: Allow giving an explicit line ' |
| 5170 |
'number.\n' |
| 5171 |
'\n' |
| 5172 |
'r(eturn)\n' |
| 5173 |
'\n' |
| 5174 |
' Continue execution until the current function returns.\n' |
| 5175 |
'\n' |
| 5176 |
'c(ont(inue))\n' |
| 5177 |
'\n' |
| 5178 |
' Continue execution, only stop when a breakpoint is ' |
| 5179 |
'encountered.\n' |
| 5180 |
'\n' |
| 5181 |
'j(ump) lineno\n' |
| 5182 |
'\n' |
| 5183 |
' Set the next line that will be executed. Only available in ' |
| 5184 |
'the\n' |
| 5185 |
' bottom-most frame. This lets you jump back and execute code ' |
| 5186 |
'again,\n' |
| 5187 |
' or jump forward to skip code that you don’t want to run.\n' |
| 5188 |
'\n' |
| 5189 |
' It should be noted that not all jumps are allowed – for ' |
| 5190 |
'instance it\n' |
| 5191 |
' is not possible to jump into the middle of a "for" loop or ' |
| 5192 |
'out of a\n' |
| 5193 |
' "finally" clause.\n' |
| 5194 |
'\n' |
| 5195 |
'l(ist) [first[, last]]\n' |
| 5196 |
'\n' |
| 5197 |
' List source code for the current file. Without arguments, ' |
| 5198 |
'list 11\n' |
| 5199 |
' lines around the current line or continue the previous ' |
| 5200 |
'listing.\n' |
| 5201 |
' With "." as argument, list 11 lines around the current line. ' |
| 5202 |
'With\n' |
| 5203 |
' one argument, list 11 lines around at that line. With two\n' |
| 5204 |
' arguments, list the given range; if the second argument is ' |
| 5205 |
'less\n' |
| 5206 |
' than the first, it is interpreted as a count.\n' |
| 5207 |
'\n' |
| 5208 |
' The current line in the current frame is indicated by "->". ' |
| 5209 |
'If an\n' |
| 5210 |
' exception is being debugged, the line where the exception ' |
| 5211 |
'was\n' |
| 5212 |
' originally raised or propagated is indicated by ">>", if it ' |
| 5213 |
'differs\n' |
| 5214 |
' from the current line.\n' |
| 5215 |
'\n' |
| 5216 |
' Changed in version 3.2: Added the ">>" marker.\n' |
| 5217 |
'\n' |
| 5218 |
'll | longlist\n' |
| 5219 |
'\n' |
| 5220 |
' List all source code for the current function or frame.\n' |
| 5221 |
' Interesting lines are marked as for "list".\n' |
| 5222 |
'\n' |
| 5223 |
' New in version 3.2.\n' |
| 5224 |
'\n' |
| 5225 |
'a(rgs)\n' |
| 5226 |
'\n' |
| 5227 |
' Print the arguments of the current function and their ' |
| 5228 |
'current\n' |
| 5229 |
' values.\n' |
| 5230 |
'\n' |
| 5231 |
'p expression\n' |
| 5232 |
'\n' |
| 5233 |
' Evaluate *expression* in the current context and print its ' |
| 5234 |
'value.\n' |
| 5235 |
'\n' |
| 5236 |
' Note:\n' |
| 5237 |
'\n' |
| 5238 |
' "print()" can also be used, but is not a debugger command — ' |
| 5239 |
'this\n' |
| 5240 |
' executes the Python "print()" function.\n' |
| 5241 |
'\n' |
| 5242 |
'pp expression\n' |
| 5243 |
'\n' |
| 5244 |
' Like the "p" command, except the value of *expression* is ' |
| 5245 |
'pretty-\n' |
| 5246 |
' printed using the "pprint" module.\n' |
| 5247 |
'\n' |
| 5248 |
'whatis expression\n' |
| 5249 |
'\n' |
| 5250 |
' Print the type of *expression*.\n' |
| 5251 |
'\n' |
| 5252 |
'source expression\n' |
| 5253 |
'\n' |
| 5254 |
' Try to get source code of *expression* and display it.\n' |
| 5255 |
'\n' |
| 5256 |
' New in version 3.2.\n' |
| 5257 |
'\n' |
| 5258 |
'display [expression]\n' |
| 5259 |
'\n' |
| 5260 |
' Display the value of *expression* if it changed, each time\n' |
| 5261 |
' execution stops in the current frame.\n' |
| 5262 |
'\n' |
| 5263 |
' Without *expression*, list all display expressions for the ' |
| 5264 |
'current\n' |
| 5265 |
' frame.\n' |
| 5266 |
'\n' |
| 5267 |
' Note:\n' |
| 5268 |
'\n' |
| 5269 |
' Display evaluates *expression* and compares to the result ' |
| 5270 |
'of the\n' |
| 5271 |
' previous evaluation of *expression*, so when the result is\n' |
| 5272 |
' mutable, display may not be able to pick up the changes.\n' |
| 5273 |
'\n' |
| 5274 |
' Example:\n' |
| 5275 |
'\n' |
| 5276 |
' lst = []\n' |
| 5277 |
' breakpoint()\n' |
| 5278 |
' pass\n' |
| 5279 |
' lst.append(1)\n' |
| 5280 |
' print(lst)\n' |
| 5281 |
'\n' |
| 5282 |
' Display won’t realize "lst" has been changed because the ' |
| 5283 |
'result of\n' |
| 5284 |
' evaluation is modified in place by "lst.append(1)" before ' |
| 5285 |
'being\n' |
| 5286 |
' compared:\n' |
| 5287 |
'\n' |
| 5288 |
' > example.py(3)<module>()\n' |
| 5289 |
' -> pass\n' |
| 5290 |
' (Pdb) display lst\n' |
| 5291 |
' display lst: []\n' |
| 5292 |
' (Pdb) n\n' |
| 5293 |
' > example.py(4)<module>()\n' |
| 5294 |
' -> lst.append(1)\n' |
| 5295 |
' (Pdb) n\n' |
| 5296 |
' > example.py(5)<module>()\n' |
| 5297 |
' -> print(lst)\n' |
| 5298 |
' (Pdb)\n' |
| 5299 |
'\n' |
| 5300 |
' You can do some tricks with copy mechanism to make it work:\n' |
| 5301 |
'\n' |
| 5302 |
' > example.py(3)<module>()\n' |
| 5303 |
' -> pass\n' |
| 5304 |
' (Pdb) display lst[:]\n' |
| 5305 |
' display lst[:]: []\n' |
| 5306 |
' (Pdb) n\n' |
| 5307 |
' > example.py(4)<module>()\n' |
| 5308 |
' -> lst.append(1)\n' |
| 5309 |
' (Pdb) n\n' |
| 5310 |
' > example.py(5)<module>()\n' |
| 5311 |
' -> print(lst)\n' |
| 5312 |
' display lst[:]: [1] [old: []]\n' |
| 5313 |
' (Pdb)\n' |
| 5314 |
'\n' |
| 5315 |
' New in version 3.2.\n' |
| 5316 |
'\n' |
| 5317 |
'undisplay [expression]\n' |
| 5318 |
'\n' |
| 5319 |
' Do not display *expression* anymore in the current frame. ' |
| 5320 |
'Without\n' |
| 5321 |
' *expression*, clear all display expressions for the current ' |
| 5322 |
'frame.\n' |
| 5323 |
'\n' |
| 5324 |
' New in version 3.2.\n' |
| 5325 |
'\n' |
| 5326 |
'interact\n' |
| 5327 |
'\n' |
| 5328 |
' Start an interactive interpreter (using the "code" module) ' |
| 5329 |
'whose\n' |
| 5330 |
' global namespace contains all the (global and local) names ' |
| 5331 |
'found in\n' |
| 5332 |
' the current scope.\n' |
| 5333 |
'\n' |
| 5334 |
' New in version 3.2.\n' |
| 5335 |
'\n' |
| 5336 |
'alias [name [command]]\n' |
| 5337 |
'\n' |
| 5338 |
' Create an alias called *name* that executes *command*. The\n' |
| 5339 |
' *command* must *not* be enclosed in quotes. Replaceable ' |
| 5340 |
'parameters\n' |
| 5341 |
' can be indicated by "%1", "%2", and so on, while "%*" is ' |
| 5342 |
'replaced\n' |
| 5343 |
' by all the parameters. If *command* is omitted, the current ' |
| 5344 |
'alias\n' |
| 5345 |
' for *name* is shown. If no arguments are given, all aliases ' |
| 5346 |
'are\n' |
| 5347 |
' listed.\n' |
| 5348 |
'\n' |
| 5349 |
' Aliases may be nested and can contain anything that can be ' |
| 5350 |
'legally\n' |
| 5351 |
' typed at the pdb prompt. Note that internal pdb commands ' |
| 5352 |
'*can* be\n' |
| 5353 |
' overridden by aliases. Such a command is then hidden until ' |
| 5354 |
'the\n' |
| 5355 |
' alias is removed. Aliasing is recursively applied to the ' |
| 5356 |
'first\n' |
| 5357 |
' word of the command line; all other words in the line are ' |
| 5358 |
'left\n' |
| 5359 |
' alone.\n' |
| 5360 |
'\n' |
| 5361 |
' As an example, here are two useful aliases (especially when ' |
| 5362 |
'placed\n' |
| 5363 |
' in the ".pdbrc" file):\n' |
| 5364 |
'\n' |
| 5365 |
' # Print instance variables (usage "pi classInst")\n' |
| 5366 |
' alias pi for k in %1.__dict__.keys(): print(f"%1.{k} = ' |
| 5367 |
'{%1.__dict__[k]}")\n' |
| 5368 |
' # Print instance variables in self\n' |
| 5369 |
' alias ps pi self\n' |
| 5370 |
'\n' |
| 5371 |
'unalias name\n' |
| 5372 |
'\n' |
| 5373 |
' Delete the specified alias *name*.\n' |
| 5374 |
'\n' |
| 5375 |
'! statement\n' |
| 5376 |
'\n' |
| 5377 |
' Execute the (one-line) *statement* in the context of the ' |
| 5378 |
'current\n' |
| 5379 |
' stack frame. The exclamation point can be omitted unless the ' |
| 5380 |
'first\n' |
| 5381 |
' word of the statement resembles a debugger command. To set ' |
| 5382 |
'a\n' |
| 5383 |
' global variable, you can prefix the assignment command with ' |
| 5384 |
'a\n' |
| 5385 |
' "global" statement on the same line, e.g.:\n' |
| 5386 |
'\n' |
| 5387 |
" (Pdb) global list_options; list_options = ['-l']\n" |
| 5388 |
' (Pdb)\n' |
| 5389 |
'\n' |
| 5390 |
'run [args ...]\n' |
| 5391 |
'restart [args ...]\n' |
| 5392 |
'\n' |
| 5393 |
' Restart the debugged Python program. If *args* is supplied, ' |
| 5394 |
'it is\n' |
| 5395 |
' split with "shlex" and the result is used as the new ' |
| 5396 |
'"sys.argv".\n' |
| 5397 |
' History, breakpoints, actions and debugger options are ' |
| 5398 |
'preserved.\n' |
| 5399 |
' "restart" is an alias for "run".\n' |
| 5400 |
'\n' |
| 5401 |
'q(uit)\n' |
| 5402 |
'\n' |
| 5403 |
' Quit from the debugger. The program being executed is ' |
| 5404 |
'aborted.\n' |
| 5405 |
'\n' |
| 5406 |
'debug code\n' |
| 5407 |
'\n' |
| 5408 |
' Enter a recursive debugger that steps through *code* (which ' |
| 5409 |
'is an\n' |
| 5410 |
' arbitrary expression or statement to be executed in the ' |
| 5411 |
'current\n' |
| 5412 |
' environment).\n' |
| 5413 |
'\n' |
| 5414 |
'retval\n' |
| 5415 |
'\n' |
| 5416 |
' Print the return value for the last return of the current ' |
| 5417 |
'function.\n' |
| 5418 |
'\n' |
| 5419 |
'-[ Footnotes ]-\n' |
| 5420 |
'\n' |
| 5421 |
'[1] Whether a frame is considered to originate in a certain ' |
| 5422 |
'module is\n' |
| 5423 |
' determined by the "__name__" in the frame globals.\n', |
| 5424 |
'del': 'The "del" statement\n' |
| 5425 |
'*******************\n' |
| 5426 |
'\n' |
| 5427 |
' del_stmt ::= "del" target_list\n' |
| 5428 |
'\n' |
| 5429 |
'Deletion is recursively defined very similar to the way assignment ' |
| 5430 |
'is\n' |
| 5431 |
'defined. Rather than spelling it out in full details, here are some\n' |
| 5432 |
'hints.\n' |
| 5433 |
'\n' |
| 5434 |
'Deletion of a target list recursively deletes each target, from left\n' |
| 5435 |
'to right.\n' |
| 5436 |
'\n' |
| 5437 |
'Deletion of a name removes the binding of that name from the local ' |
| 5438 |
'or\n' |
| 5439 |
'global namespace, depending on whether the name occurs in a "global"\n' |
| 5440 |
'statement in the same code block. If the name is unbound, a\n' |
| 5441 |
'"NameError" exception will be raised.\n' |
| 5442 |
'\n' |
| 5443 |
'Deletion of attribute references, subscriptions and slicings is ' |
| 5444 |
'passed\n' |
| 5445 |
'to the primary object involved; deletion of a slicing is in general\n' |
| 5446 |
'equivalent to assignment of an empty slice of the right type (but ' |
| 5447 |
'even\n' |
| 5448 |
'this is determined by the sliced object).\n' |
| 5449 |
'\n' |
| 5450 |
'Changed in version 3.2: Previously it was illegal to delete a name\n' |
| 5451 |
'from the local namespace if it occurs as a free variable in a nested\n' |
| 5452 |
'block.\n', |
| 5453 |
'dict': 'Dictionary displays\n' |
| 5454 |
'*******************\n' |
| 5455 |
'\n' |
| 5456 |
'A dictionary display is a possibly empty series of dict items\n' |
| 5457 |
'(key/value pairs) enclosed in curly braces:\n' |
| 5458 |
'\n' |
| 5459 |
' dict_display ::= "{" [dict_item_list | dict_comprehension] ' |
| 5460 |
'"}"\n' |
| 5461 |
' dict_item_list ::= dict_item ("," dict_item)* [","]\n' |
| 5462 |
' dict_item ::= expression ":" expression | "**" or_expr\n' |
| 5463 |
' dict_comprehension ::= expression ":" expression comp_for\n' |
| 5464 |
'\n' |
| 5465 |
'A dictionary display yields a new dictionary object.\n' |
| 5466 |
'\n' |
| 5467 |
'If a comma-separated sequence of dict items is given, they are\n' |
| 5468 |
'evaluated from left to right to define the entries of the ' |
| 5469 |
'dictionary:\n' |
| 5470 |
'each key object is used as a key into the dictionary to store the\n' |
| 5471 |
'corresponding value. This means that you can specify the same key\n' |
| 5472 |
'multiple times in the dict item list, and the final dictionary’s ' |
| 5473 |
'value\n' |
| 5474 |
'for that key will be the last one given.\n' |
| 5475 |
'\n' |
| 5476 |
'A double asterisk "**" denotes *dictionary unpacking*. Its operand\n' |
| 5477 |
'must be a *mapping*. Each mapping item is added to the new\n' |
| 5478 |
'dictionary. Later values replace values already set by earlier ' |
| 5479 |
'dict\n' |
| 5480 |
'items and earlier dictionary unpackings.\n' |
| 5481 |
'\n' |
| 5482 |
'New in version 3.5: Unpacking into dictionary displays, originally\n' |
| 5483 |
'proposed by **PEP 448**.\n' |
| 5484 |
'\n' |
| 5485 |
'A dict comprehension, in contrast to list and set comprehensions,\n' |
| 5486 |
'needs two expressions separated with a colon followed by the usual\n' |
| 5487 |
'“for” and “if” clauses. When the comprehension is run, the ' |
| 5488 |
'resulting\n' |
| 5489 |
'key and value elements are inserted in the new dictionary in the ' |
| 5490 |
'order\n' |
| 5491 |
'they are produced.\n' |
| 5492 |
'\n' |
| 5493 |
'Restrictions on the types of the key values are listed earlier in\n' |
| 5494 |
'section The standard type hierarchy. (To summarize, the key type\n' |
| 5495 |
'should be *hashable*, which excludes all mutable objects.) Clashes\n' |
| 5496 |
'between duplicate keys are not detected; the last value (textually\n' |
| 5497 |
'rightmost in the display) stored for a given key value prevails.\n' |
| 5498 |
'\n' |
| 5499 |
'Changed in version 3.8: Prior to Python 3.8, in dict ' |
| 5500 |
'comprehensions,\n' |
| 5501 |
'the evaluation order of key and value was not well-defined. In\n' |
| 5502 |
'CPython, the value was evaluated before the key. Starting with ' |
| 5503 |
'3.8,\n' |
| 5504 |
'the key is evaluated before the value, as proposed by **PEP 572**.\n', |
| 5505 |
'dynamic-features': 'Interaction with dynamic features\n' |
| 5506 |
'*********************************\n' |
| 5507 |
'\n' |
| 5508 |
'Name resolution of free variables occurs at runtime, not ' |
| 5509 |
'at compile\n' |
| 5510 |
'time. This means that the following code will print 42:\n' |
| 5511 |
'\n' |
| 5512 |
' i = 10\n' |
| 5513 |
' def f():\n' |
| 5514 |
' print(i)\n' |
| 5515 |
' i = 42\n' |
| 5516 |
' f()\n' |
| 5517 |
'\n' |
| 5518 |
'The "eval()" and "exec()" functions do not have access ' |
| 5519 |
'to the full\n' |
| 5520 |
'environment for resolving names. Names may be resolved ' |
| 5521 |
'in the local\n' |
| 5522 |
'and global namespaces of the caller. Free variables are ' |
| 5523 |
'not resolved\n' |
| 5524 |
'in the nearest enclosing namespace, but in the global ' |
| 5525 |
'namespace. [1]\n' |
| 5526 |
'The "exec()" and "eval()" functions have optional ' |
| 5527 |
'arguments to\n' |
| 5528 |
'override the global and local namespace. If only one ' |
| 5529 |
'namespace is\n' |
| 5530 |
'specified, it is used for both.\n', |
| 5531 |
'else': 'The "if" statement\n' |
| 5532 |
'******************\n' |
| 5533 |
'\n' |
| 5534 |
'The "if" statement is used for conditional execution:\n' |
| 5535 |
'\n' |
| 5536 |
' if_stmt ::= "if" assignment_expression ":" suite\n' |
| 5537 |
' ("elif" assignment_expression ":" suite)*\n' |
| 5538 |
' ["else" ":" suite]\n' |
| 5539 |
'\n' |
| 5540 |
'It selects exactly one of the suites by evaluating the expressions ' |
| 5541 |
'one\n' |
| 5542 |
'by one until one is found to be true (see section Boolean ' |
| 5543 |
'operations\n' |
| 5544 |
'for the definition of true and false); then that suite is executed\n' |
| 5545 |
'(and no other part of the "if" statement is executed or evaluated).\n' |
| 5546 |
'If all expressions are false, the suite of the "else" clause, if\n' |
| 5547 |
'present, is executed.\n', |
| 5548 |
'exceptions': 'Exceptions\n' |
| 5549 |
'**********\n' |
| 5550 |
'\n' |
| 5551 |
'Exceptions are a means of breaking out of the normal flow of ' |
| 5552 |
'control\n' |
| 5553 |
'of a code block in order to handle errors or other ' |
| 5554 |
'exceptional\n' |
| 5555 |
'conditions. An exception is *raised* at the point where the ' |
| 5556 |
'error is\n' |
| 5557 |
'detected; it may be *handled* by the surrounding code block or ' |
| 5558 |
'by any\n' |
| 5559 |
'code block that directly or indirectly invoked the code block ' |
| 5560 |
'where\n' |
| 5561 |
'the error occurred.\n' |
| 5562 |
'\n' |
| 5563 |
'The Python interpreter raises an exception when it detects a ' |
| 5564 |
'run-time\n' |
| 5565 |
'error (such as division by zero). A Python program can also\n' |
| 5566 |
'explicitly raise an exception with the "raise" statement. ' |
| 5567 |
'Exception\n' |
| 5568 |
'handlers are specified with the "try" … "except" statement. ' |
| 5569 |
'The\n' |
| 5570 |
'"finally" clause of such a statement can be used to specify ' |
| 5571 |
'cleanup\n' |
| 5572 |
'code which does not handle the exception, but is executed ' |
| 5573 |
'whether an\n' |
| 5574 |
'exception occurred or not in the preceding code.\n' |
| 5575 |
'\n' |
| 5576 |
'Python uses the “termination” model of error handling: an ' |
| 5577 |
'exception\n' |
| 5578 |
'handler can find out what happened and continue execution at ' |
| 5579 |
'an outer\n' |
| 5580 |
'level, but it cannot repair the cause of the error and retry ' |
| 5581 |
'the\n' |
| 5582 |
'failing operation (except by re-entering the offending piece ' |
| 5583 |
'of code\n' |
| 5584 |
'from the top).\n' |
| 5585 |
'\n' |
| 5586 |
'When an exception is not handled at all, the interpreter ' |
| 5587 |
'terminates\n' |
| 5588 |
'execution of the program, or returns to its interactive main ' |
| 5589 |
'loop. In\n' |
| 5590 |
'either case, it prints a stack traceback, except when the ' |
| 5591 |
'exception is\n' |
| 5592 |
'"SystemExit".\n' |
| 5593 |
'\n' |
| 5594 |
'Exceptions are identified by class instances. The "except" ' |
| 5595 |
'clause is\n' |
| 5596 |
'selected depending on the class of the instance: it must ' |
| 5597 |
'reference the\n' |
| 5598 |
'class of the instance or a *non-virtual base class* thereof. ' |
| 5599 |
'The\n' |
| 5600 |
'instance can be received by the handler and can carry ' |
| 5601 |
'additional\n' |
| 5602 |
'information about the exceptional condition.\n' |
| 5603 |
'\n' |
| 5604 |
'Note:\n' |
| 5605 |
'\n' |
| 5606 |
' Exception messages are not part of the Python API. Their ' |
| 5607 |
'contents\n' |
| 5608 |
' may change from one version of Python to the next without ' |
| 5609 |
'warning\n' |
| 5610 |
' and should not be relied on by code which will run under ' |
| 5611 |
'multiple\n' |
| 5612 |
' versions of the interpreter.\n' |
| 5613 |
'\n' |
| 5614 |
'See also the description of the "try" statement in section The ' |
| 5615 |
'try\n' |
| 5616 |
'statement and "raise" statement in section The raise ' |
| 5617 |
'statement.\n' |
| 5618 |
'\n' |
| 5619 |
'-[ Footnotes ]-\n' |
| 5620 |
'\n' |
| 5621 |
'[1] This limitation occurs because the code that is executed ' |
| 5622 |
'by these\n' |
| 5623 |
' operations is not available at the time the module is ' |
| 5624 |
'compiled.\n', |
| 5625 |
'execmodel': 'Execution model\n' |
| 5626 |
'***************\n' |
| 5627 |
'\n' |
| 5628 |
'\n' |
| 5629 |
'Structure of a program\n' |
| 5630 |
'======================\n' |
| 5631 |
'\n' |
| 5632 |
'A Python program is constructed from code blocks. A *block* is ' |
| 5633 |
'a piece\n' |
| 5634 |
'of Python program text that is executed as a unit. The ' |
| 5635 |
'following are\n' |
| 5636 |
'blocks: a module, a function body, and a class definition. ' |
| 5637 |
'Each\n' |
| 5638 |
'command typed interactively is a block. A script file (a file ' |
| 5639 |
'given\n' |
| 5640 |
'as standard input to the interpreter or specified as a command ' |
| 5641 |
'line\n' |
| 5642 |
'argument to the interpreter) is a code block. A script command ' |
| 5643 |
'(a\n' |
| 5644 |
'command specified on the interpreter command line with the ' |
| 5645 |
'"-c"\n' |
| 5646 |
'option) is a code block. A module run as a top level script (as ' |
| 5647 |
'module\n' |
| 5648 |
'"__main__") from the command line using a "-m" argument is also ' |
| 5649 |
'a code\n' |
| 5650 |
'block. The string argument passed to the built-in functions ' |
| 5651 |
'"eval()"\n' |
| 5652 |
'and "exec()" is a code block.\n' |
| 5653 |
'\n' |
| 5654 |
'A code block is executed in an *execution frame*. A frame ' |
| 5655 |
'contains\n' |
| 5656 |
'some administrative information (used for debugging) and ' |
| 5657 |
'determines\n' |
| 5658 |
'where and how execution continues after the code block’s ' |
| 5659 |
'execution has\n' |
| 5660 |
'completed.\n' |
| 5661 |
'\n' |
| 5662 |
'\n' |
| 5663 |
'Naming and binding\n' |
| 5664 |
'==================\n' |
| 5665 |
'\n' |
| 5666 |
'\n' |
| 5667 |
'Binding of names\n' |
| 5668 |
'----------------\n' |
| 5669 |
'\n' |
| 5670 |
'*Names* refer to objects. Names are introduced by name ' |
| 5671 |
'binding\n' |
| 5672 |
'operations.\n' |
| 5673 |
'\n' |
| 5674 |
'The following constructs bind names:\n' |
| 5675 |
'\n' |
| 5676 |
'* formal parameters to functions,\n' |
| 5677 |
'\n' |
| 5678 |
'* class definitions,\n' |
| 5679 |
'\n' |
| 5680 |
'* function definitions,\n' |
| 5681 |
'\n' |
| 5682 |
'* assignment expressions,\n' |
| 5683 |
'\n' |
| 5684 |
'* targets that are identifiers if occurring in an assignment:\n' |
| 5685 |
'\n' |
| 5686 |
' * "for" loop header,\n' |
| 5687 |
'\n' |
| 5688 |
' * after "as" in a "with" statement, "except" clause, ' |
| 5689 |
'"except*"\n' |
| 5690 |
' clause, or in the as-pattern in structural pattern ' |
| 5691 |
'matching,\n' |
| 5692 |
'\n' |
| 5693 |
' * in a capture pattern in structural pattern matching\n' |
| 5694 |
'\n' |
| 5695 |
'* "import" statements.\n' |
| 5696 |
'\n' |
| 5697 |
'The "import" statement of the form "from ... import *" binds ' |
| 5698 |
'all names\n' |
| 5699 |
'defined in the imported module, except those beginning with an\n' |
| 5700 |
'underscore. This form may only be used at the module level.\n' |
| 5701 |
'\n' |
| 5702 |
'A target occurring in a "del" statement is also considered ' |
| 5703 |
'bound for\n' |
| 5704 |
'this purpose (though the actual semantics are to unbind the ' |
| 5705 |
'name).\n' |
| 5706 |
'\n' |
| 5707 |
'Each assignment or import statement occurs within a block ' |
| 5708 |
'defined by a\n' |
| 5709 |
'class or function definition or at the module level (the ' |
| 5710 |
'top-level\n' |
| 5711 |
'code block).\n' |
| 5712 |
'\n' |
| 5713 |
'If a name is bound in a block, it is a local variable of that ' |
| 5714 |
'block,\n' |
| 5715 |
'unless declared as "nonlocal" or "global". If a name is bound ' |
| 5716 |
'at the\n' |
| 5717 |
'module level, it is a global variable. (The variables of the ' |
| 5718 |
'module\n' |
| 5719 |
'code block are local and global.) If a variable is used in a ' |
| 5720 |
'code\n' |
| 5721 |
'block but not defined there, it is a *free variable*.\n' |
| 5722 |
'\n' |
| 5723 |
'Each occurrence of a name in the program text refers to the ' |
| 5724 |
'*binding*\n' |
| 5725 |
'of that name established by the following name resolution ' |
| 5726 |
'rules.\n' |
| 5727 |
'\n' |
| 5728 |
'\n' |
| 5729 |
'Resolution of names\n' |
| 5730 |
'-------------------\n' |
| 5731 |
'\n' |
| 5732 |
'A *scope* defines the visibility of a name within a block. If ' |
| 5733 |
'a local\n' |
| 5734 |
'variable is defined in a block, its scope includes that block. ' |
| 5735 |
'If the\n' |
| 5736 |
'definition occurs in a function block, the scope extends to any ' |
| 5737 |
'blocks\n' |
| 5738 |
'contained within the defining one, unless a contained block ' |
| 5739 |
'introduces\n' |
| 5740 |
'a different binding for the name.\n' |
| 5741 |
'\n' |
| 5742 |
'When a name is used in a code block, it is resolved using the ' |
| 5743 |
'nearest\n' |
| 5744 |
'enclosing scope. The set of all such scopes visible to a code ' |
| 5745 |
'block\n' |
| 5746 |
'is called the block’s *environment*.\n' |
| 5747 |
'\n' |
| 5748 |
'When a name is not found at all, a "NameError" exception is ' |
| 5749 |
'raised. If\n' |
| 5750 |
'the current scope is a function scope, and the name refers to a ' |
| 5751 |
'local\n' |
| 5752 |
'variable that has not yet been bound to a value at the point ' |
| 5753 |
'where the\n' |
| 5754 |
'name is used, an "UnboundLocalError" exception is raised.\n' |
| 5755 |
'"UnboundLocalError" is a subclass of "NameError".\n' |
| 5756 |
'\n' |
| 5757 |
'If a name binding operation occurs anywhere within a code ' |
| 5758 |
'block, all\n' |
| 5759 |
'uses of the name within the block are treated as references to ' |
| 5760 |
'the\n' |
| 5761 |
'current block. This can lead to errors when a name is used ' |
| 5762 |
'within a\n' |
| 5763 |
'block before it is bound. This rule is subtle. Python lacks\n' |
| 5764 |
'declarations and allows name binding operations to occur ' |
| 5765 |
'anywhere\n' |
| 5766 |
'within a code block. The local variables of a code block can ' |
| 5767 |
'be\n' |
| 5768 |
'determined by scanning the entire text of the block for name ' |
| 5769 |
'binding\n' |
| 5770 |
'operations. See the FAQ entry on UnboundLocalError for ' |
| 5771 |
'examples.\n' |
| 5772 |
'\n' |
| 5773 |
'If the "global" statement occurs within a block, all uses of ' |
| 5774 |
'the names\n' |
| 5775 |
'specified in the statement refer to the bindings of those names ' |
| 5776 |
'in the\n' |
| 5777 |
'top-level namespace. Names are resolved in the top-level ' |
| 5778 |
'namespace by\n' |
| 5779 |
'searching the global namespace, i.e. the namespace of the ' |
| 5780 |
'module\n' |
| 5781 |
'containing the code block, and the builtins namespace, the ' |
| 5782 |
'namespace\n' |
| 5783 |
'of the module "builtins". The global namespace is searched ' |
| 5784 |
'first. If\n' |
| 5785 |
'the names are not found there, the builtins namespace is ' |
| 5786 |
'searched.\n' |
| 5787 |
'The "global" statement must precede all uses of the listed ' |
| 5788 |
'names.\n' |
| 5789 |
'\n' |
| 5790 |
'The "global" statement has the same scope as a name binding ' |
| 5791 |
'operation\n' |
| 5792 |
'in the same block. If the nearest enclosing scope for a free ' |
| 5793 |
'variable\n' |
| 5794 |
'contains a global statement, the free variable is treated as a ' |
| 5795 |
'global.\n' |
| 5796 |
'\n' |
| 5797 |
'The "nonlocal" statement causes corresponding names to refer ' |
| 5798 |
'to\n' |
| 5799 |
'previously bound variables in the nearest enclosing function ' |
| 5800 |
'scope.\n' |
| 5801 |
'"SyntaxError" is raised at compile time if the given name does ' |
| 5802 |
'not\n' |
| 5803 |
'exist in any enclosing function scope.\n' |
| 5804 |
'\n' |
| 5805 |
'The namespace for a module is automatically created the first ' |
| 5806 |
'time a\n' |
| 5807 |
'module is imported. The main module for a script is always ' |
| 5808 |
'called\n' |
| 5809 |
'"__main__".\n' |
| 5810 |
'\n' |
| 5811 |
'Class definition blocks and arguments to "exec()" and "eval()" ' |
| 5812 |
'are\n' |
| 5813 |
'special in the context of name resolution. A class definition ' |
| 5814 |
'is an\n' |
| 5815 |
'executable statement that may use and define names. These ' |
| 5816 |
'references\n' |
| 5817 |
'follow the normal rules for name resolution with an exception ' |
| 5818 |
'that\n' |
| 5819 |
'unbound local variables are looked up in the global namespace. ' |
| 5820 |
'The\n' |
| 5821 |
'namespace of the class definition becomes the attribute ' |
| 5822 |
'dictionary of\n' |
| 5823 |
'the class. The scope of names defined in a class block is ' |
| 5824 |
'limited to\n' |
| 5825 |
'the class block; it does not extend to the code blocks of ' |
| 5826 |
'methods –\n' |
| 5827 |
'this includes comprehensions and generator expressions since ' |
| 5828 |
'they are\n' |
| 5829 |
'implemented using a function scope. This means that the ' |
| 5830 |
'following\n' |
| 5831 |
'will fail:\n' |
| 5832 |
'\n' |
| 5833 |
' class A:\n' |
| 5834 |
' a = 42\n' |
| 5835 |
' b = list(a + i for i in range(10))\n' |
| 5836 |
'\n' |
| 5837 |
'\n' |
| 5838 |
'Builtins and restricted execution\n' |
| 5839 |
'---------------------------------\n' |
| 5840 |
'\n' |
| 5841 |
'**CPython implementation detail:** Users should not touch\n' |
| 5842 |
'"__builtins__"; it is strictly an implementation detail. ' |
| 5843 |
'Users\n' |
| 5844 |
'wanting to override values in the builtins namespace should ' |
| 5845 |
'"import"\n' |
| 5846 |
'the "builtins" module and modify its attributes appropriately.\n' |
| 5847 |
'\n' |
| 5848 |
'The builtins namespace associated with the execution of a code ' |
| 5849 |
'block\n' |
| 5850 |
'is actually found by looking up the name "__builtins__" in its ' |
| 5851 |
'global\n' |
| 5852 |
'namespace; this should be a dictionary or a module (in the ' |
| 5853 |
'latter case\n' |
| 5854 |
'the module’s dictionary is used). By default, when in the ' |
| 5855 |
'"__main__"\n' |
| 5856 |
'module, "__builtins__" is the built-in module "builtins"; when ' |
| 5857 |
'in any\n' |
| 5858 |
'other module, "__builtins__" is an alias for the dictionary of ' |
| 5859 |
'the\n' |
| 5860 |
'"builtins" module itself.\n' |
| 5861 |
'\n' |
| 5862 |
'\n' |
| 5863 |
'Interaction with dynamic features\n' |
| 5864 |
'---------------------------------\n' |
| 5865 |
'\n' |
| 5866 |
'Name resolution of free variables occurs at runtime, not at ' |
| 5867 |
'compile\n' |
| 5868 |
'time. This means that the following code will print 42:\n' |
| 5869 |
'\n' |
| 5870 |
' i = 10\n' |
| 5871 |
' def f():\n' |
| 5872 |
' print(i)\n' |
| 5873 |
' i = 42\n' |
| 5874 |
' f()\n' |
| 5875 |
'\n' |
| 5876 |
'The "eval()" and "exec()" functions do not have access to the ' |
| 5877 |
'full\n' |
| 5878 |
'environment for resolving names. Names may be resolved in the ' |
| 5879 |
'local\n' |
| 5880 |
'and global namespaces of the caller. Free variables are not ' |
| 5881 |
'resolved\n' |
| 5882 |
'in the nearest enclosing namespace, but in the global ' |
| 5883 |
'namespace. [1]\n' |
| 5884 |
'The "exec()" and "eval()" functions have optional arguments to\n' |
| 5885 |
'override the global and local namespace. If only one namespace ' |
| 5886 |
'is\n' |
| 5887 |
'specified, it is used for both.\n' |
| 5888 |
'\n' |
| 5889 |
'\n' |
| 5890 |
'Exceptions\n' |
| 5891 |
'==========\n' |
| 5892 |
'\n' |
| 5893 |
'Exceptions are a means of breaking out of the normal flow of ' |
| 5894 |
'control\n' |
| 5895 |
'of a code block in order to handle errors or other exceptional\n' |
| 5896 |
'conditions. An exception is *raised* at the point where the ' |
| 5897 |
'error is\n' |
| 5898 |
'detected; it may be *handled* by the surrounding code block or ' |
| 5899 |
'by any\n' |
| 5900 |
'code block that directly or indirectly invoked the code block ' |
| 5901 |
'where\n' |
| 5902 |
'the error occurred.\n' |
| 5903 |
'\n' |
| 5904 |
'The Python interpreter raises an exception when it detects a ' |
| 5905 |
'run-time\n' |
| 5906 |
'error (such as division by zero). A Python program can also\n' |
| 5907 |
'explicitly raise an exception with the "raise" statement. ' |
| 5908 |
'Exception\n' |
| 5909 |
'handlers are specified with the "try" … "except" statement. ' |
| 5910 |
'The\n' |
| 5911 |
'"finally" clause of such a statement can be used to specify ' |
| 5912 |
'cleanup\n' |
| 5913 |
'code which does not handle the exception, but is executed ' |
| 5914 |
'whether an\n' |
| 5915 |
'exception occurred or not in the preceding code.\n' |
| 5916 |
'\n' |
| 5917 |
'Python uses the “termination” model of error handling: an ' |
| 5918 |
'exception\n' |
| 5919 |
'handler can find out what happened and continue execution at an ' |
| 5920 |
'outer\n' |
| 5921 |
'level, but it cannot repair the cause of the error and retry ' |
| 5922 |
'the\n' |
| 5923 |
'failing operation (except by re-entering the offending piece of ' |
| 5924 |
'code\n' |
| 5925 |
'from the top).\n' |
| 5926 |
'\n' |
| 5927 |
'When an exception is not handled at all, the interpreter ' |
| 5928 |
'terminates\n' |
| 5929 |
'execution of the program, or returns to its interactive main ' |
| 5930 |
'loop. In\n' |
| 5931 |
'either case, it prints a stack traceback, except when the ' |
| 5932 |
'exception is\n' |
| 5933 |
'"SystemExit".\n' |
| 5934 |
'\n' |
| 5935 |
'Exceptions are identified by class instances. The "except" ' |
| 5936 |
'clause is\n' |
| 5937 |
'selected depending on the class of the instance: it must ' |
| 5938 |
'reference the\n' |
| 5939 |
'class of the instance or a *non-virtual base class* thereof. ' |
| 5940 |
'The\n' |
| 5941 |
'instance can be received by the handler and can carry ' |
| 5942 |
'additional\n' |
| 5943 |
'information about the exceptional condition.\n' |
| 5944 |
'\n' |
| 5945 |
'Note:\n' |
| 5946 |
'\n' |
| 5947 |
' Exception messages are not part of the Python API. Their ' |
| 5948 |
'contents\n' |
| 5949 |
' may change from one version of Python to the next without ' |
| 5950 |
'warning\n' |
| 5951 |
' and should not be relied on by code which will run under ' |
| 5952 |
'multiple\n' |
| 5953 |
' versions of the interpreter.\n' |
| 5954 |
'\n' |
| 5955 |
'See also the description of the "try" statement in section The ' |
| 5956 |
'try\n' |
| 5957 |
'statement and "raise" statement in section The raise ' |
| 5958 |
'statement.\n' |
| 5959 |
'\n' |
| 5960 |
'-[ Footnotes ]-\n' |
| 5961 |
'\n' |
| 5962 |
'[1] This limitation occurs because the code that is executed by ' |
| 5963 |
'these\n' |
| 5964 |
' operations is not available at the time the module is ' |
| 5965 |
'compiled.\n', |
| 5966 |
'exprlists': 'Expression lists\n' |
| 5967 |
'****************\n' |
| 5968 |
'\n' |
| 5969 |
' expression_list ::= expression ("," expression)* [","]\n' |
| 5970 |
' starred_list ::= starred_item ("," starred_item)* ' |
| 5971 |
'[","]\n' |
| 5972 |
' starred_expression ::= expression | (starred_item ",")* ' |
| 5973 |
'[starred_item]\n' |
| 5974 |
' starred_item ::= assignment_expression | "*" or_expr\n' |
| 5975 |
'\n' |
| 5976 |
'Except when part of a list or set display, an expression list\n' |
| 5977 |
'containing at least one comma yields a tuple. The length of ' |
| 5978 |
'the tuple\n' |
| 5979 |
'is the number of expressions in the list. The expressions are\n' |
| 5980 |
'evaluated from left to right.\n' |
| 5981 |
'\n' |
| 5982 |
'An asterisk "*" denotes *iterable unpacking*. Its operand must ' |
| 5983 |
'be an\n' |
| 5984 |
'*iterable*. The iterable is expanded into a sequence of items, ' |
| 5985 |
'which\n' |
| 5986 |
'are included in the new tuple, list, or set, at the site of ' |
| 5987 |
'the\n' |
| 5988 |
'unpacking.\n' |
| 5989 |
'\n' |
| 5990 |
'New in version 3.5: Iterable unpacking in expression lists, ' |
| 5991 |
'originally\n' |
| 5992 |
'proposed by **PEP 448**.\n' |
| 5993 |
'\n' |
| 5994 |
'A trailing comma is required only to create a one-item tuple, ' |
| 5995 |
'such as\n' |
| 5996 |
'"1,"; it is optional in all other cases. A single expression ' |
| 5997 |
'without a\n' |
| 5998 |
'trailing comma doesn’t create a tuple, but rather yields the ' |
| 5999 |
'value of\n' |
| 6000 |
'that expression. (To create an empty tuple, use an empty pair ' |
| 6001 |
'of\n' |
| 6002 |
'parentheses: "()".)\n', |
| 6003 |
'floating': 'Floating point literals\n' |
| 6004 |
'***********************\n' |
| 6005 |
'\n' |
| 6006 |
'Floating point literals are described by the following lexical\n' |
| 6007 |
'definitions:\n' |
| 6008 |
'\n' |
| 6009 |
' floatnumber ::= pointfloat | exponentfloat\n' |
| 6010 |
' pointfloat ::= [digitpart] fraction | digitpart "."\n' |
| 6011 |
' exponentfloat ::= (digitpart | pointfloat) exponent\n' |
| 6012 |
' digitpart ::= digit (["_"] digit)*\n' |
| 6013 |
' fraction ::= "." digitpart\n' |
| 6014 |
' exponent ::= ("e" | "E") ["+" | "-"] digitpart\n' |
| 6015 |
'\n' |
| 6016 |
'Note that the integer and exponent parts are always interpreted ' |
| 6017 |
'using\n' |
| 6018 |
'radix 10. For example, "077e010" is legal, and denotes the same ' |
| 6019 |
'number\n' |
| 6020 |
'as "77e10". The allowed range of floating point literals is\n' |
| 6021 |
'implementation-dependent. As in integer literals, underscores ' |
| 6022 |
'are\n' |
| 6023 |
'supported for digit grouping.\n' |
| 6024 |
'\n' |
| 6025 |
'Some examples of floating point literals:\n' |
| 6026 |
'\n' |
| 6027 |
' 3.14 10. .001 1e100 3.14e-10 0e0 ' |
| 6028 |
'3.14_15_93\n' |
| 6029 |
'\n' |
| 6030 |
'Changed in version 3.6: Underscores are now allowed for ' |
| 6031 |
'grouping\n' |
| 6032 |
'purposes in literals.\n', |
| 6033 |
'for': 'The "for" statement\n' |
| 6034 |
'*******************\n' |
| 6035 |
'\n' |
| 6036 |
'The "for" statement is used to iterate over the elements of a ' |
| 6037 |
'sequence\n' |
| 6038 |
'(such as a string, tuple or list) or other iterable object:\n' |
| 6039 |
'\n' |
| 6040 |
' for_stmt ::= "for" target_list "in" starred_list ":" suite\n' |
| 6041 |
' ["else" ":" suite]\n' |
| 6042 |
'\n' |
| 6043 |
'The "starred_list" expression is evaluated once; it should yield an\n' |
| 6044 |
'*iterable* object. An *iterator* is created for that iterable. The\n' |
| 6045 |
'first item provided by the iterator is then assigned to the target\n' |
| 6046 |
'list using the standard rules for assignments (see Assignment\n' |
| 6047 |
'statements), and the suite is executed. This repeats for each item\n' |
| 6048 |
'provided by the iterator. When the iterator is exhausted, the suite\n' |
| 6049 |
'in the "else" clause, if present, is executed, and the loop\n' |
| 6050 |
'terminates.\n' |
| 6051 |
'\n' |
| 6052 |
'A "break" statement executed in the first suite terminates the loop\n' |
| 6053 |
'without executing the "else" clause’s suite. A "continue" statement\n' |
| 6054 |
'executed in the first suite skips the rest of the suite and ' |
| 6055 |
'continues\n' |
| 6056 |
'with the next item, or with the "else" clause if there is no next\n' |
| 6057 |
'item.\n' |
| 6058 |
'\n' |
| 6059 |
'The for-loop makes assignments to the variables in the target list.\n' |
| 6060 |
'This overwrites all previous assignments to those variables ' |
| 6061 |
'including\n' |
| 6062 |
'those made in the suite of the for-loop:\n' |
| 6063 |
'\n' |
| 6064 |
' for i in range(10):\n' |
| 6065 |
' print(i)\n' |
| 6066 |
' i = 5 # this will not affect the for-loop\n' |
| 6067 |
' # because i will be overwritten with the ' |
| 6068 |
'next\n' |
| 6069 |
' # index in the range\n' |
| 6070 |
'\n' |
| 6071 |
'Names in the target list are not deleted when the loop is finished,\n' |
| 6072 |
'but if the sequence is empty, they will not have been assigned to at\n' |
| 6073 |
'all by the loop. Hint: the built-in type "range()" represents\n' |
| 6074 |
'immutable arithmetic sequences of integers. For instance, iterating\n' |
| 6075 |
'"range(3)" successively yields 0, 1, and then 2.\n' |
| 6076 |
'\n' |
| 6077 |
'Changed in version 3.11: Starred elements are now allowed in the\n' |
| 6078 |
'expression list.\n', |
| 6079 |
'formatstrings': 'Format String Syntax\n' |
| 6080 |
'********************\n' |
| 6081 |
'\n' |
| 6082 |
'The "str.format()" method and the "Formatter" class share ' |
| 6083 |
'the same\n' |
| 6084 |
'syntax for format strings (although in the case of ' |
| 6085 |
'"Formatter",\n' |
| 6086 |
'subclasses can define their own format string syntax). The ' |
| 6087 |
'syntax is\n' |
| 6088 |
'related to that of formatted string literals, but it is ' |
| 6089 |
'less\n' |
| 6090 |
'sophisticated and, in particular, does not support ' |
| 6091 |
'arbitrary\n' |
| 6092 |
'expressions.\n' |
| 6093 |
'\n' |
| 6094 |
'Format strings contain “replacement fields” surrounded by ' |
| 6095 |
'curly braces\n' |
| 6096 |
'"{}". Anything that is not contained in braces is ' |
| 6097 |
'considered literal\n' |
| 6098 |
'text, which is copied unchanged to the output. If you need ' |
| 6099 |
'to include\n' |
| 6100 |
'a brace character in the literal text, it can be escaped by ' |
| 6101 |
'doubling:\n' |
| 6102 |
'"{{" and "}}".\n' |
| 6103 |
'\n' |
| 6104 |
'The grammar for a replacement field is as follows:\n' |
| 6105 |
'\n' |
| 6106 |
' replacement_field ::= "{" [field_name] ["!" conversion] ' |
| 6107 |
'[":" format_spec] "}"\n' |
| 6108 |
' field_name ::= arg_name ("." attribute_name | "[" ' |
| 6109 |
'element_index "]")*\n' |
| 6110 |
' arg_name ::= [identifier | digit+]\n' |
| 6111 |
' attribute_name ::= identifier\n' |
| 6112 |
' element_index ::= digit+ | index_string\n' |
| 6113 |
' index_string ::= <any source character except "]"> ' |
| 6114 |
'+\n' |
| 6115 |
' conversion ::= "r" | "s" | "a"\n' |
| 6116 |
' format_spec ::= format-spec:format_spec\n' |
| 6117 |
'\n' |
| 6118 |
'In less formal terms, the replacement field can start with ' |
| 6119 |
'a\n' |
| 6120 |
'*field_name* that specifies the object whose value is to be ' |
| 6121 |
'formatted\n' |
| 6122 |
'and inserted into the output instead of the replacement ' |
| 6123 |
'field. The\n' |
| 6124 |
'*field_name* is optionally followed by a *conversion* ' |
| 6125 |
'field, which is\n' |
| 6126 |
'preceded by an exclamation point "\'!\'", and a ' |
| 6127 |
'*format_spec*, which is\n' |
| 6128 |
'preceded by a colon "\':\'". These specify a non-default ' |
| 6129 |
'format for the\n' |
| 6130 |
'replacement value.\n' |
| 6131 |
'\n' |
| 6132 |
'See also the Format Specification Mini-Language section.\n' |
| 6133 |
'\n' |
| 6134 |
'The *field_name* itself begins with an *arg_name* that is ' |
| 6135 |
'either a\n' |
| 6136 |
'number or a keyword. If it’s a number, it refers to a ' |
| 6137 |
'positional\n' |
| 6138 |
'argument, and if it’s a keyword, it refers to a named ' |
| 6139 |
'keyword\n' |
| 6140 |
'argument. An *arg_name* is treated as a number if a call ' |
| 6141 |
'to\n' |
| 6142 |
'"str.isdecimal()" on the string would return true. If the ' |
| 6143 |
'numerical\n' |
| 6144 |
'arg_names in a format string are 0, 1, 2, … in sequence, ' |
| 6145 |
'they can all\n' |
| 6146 |
'be omitted (not just some) and the numbers 0, 1, 2, … will ' |
| 6147 |
'be\n' |
| 6148 |
'automatically inserted in that order. Because *arg_name* is ' |
| 6149 |
'not quote-\n' |
| 6150 |
'delimited, it is not possible to specify arbitrary ' |
| 6151 |
'dictionary keys\n' |
| 6152 |
'(e.g., the strings "\'10\'" or "\':-]\'") within a format ' |
| 6153 |
'string. The\n' |
| 6154 |
'*arg_name* can be followed by any number of index or ' |
| 6155 |
'attribute\n' |
| 6156 |
'expressions. An expression of the form "\'.name\'" selects ' |
| 6157 |
'the named\n' |
| 6158 |
'attribute using "getattr()", while an expression of the ' |
| 6159 |
'form\n' |
| 6160 |
'"\'[index]\'" does an index lookup using "__getitem__()".\n' |
| 6161 |
'\n' |
| 6162 |
'Changed in version 3.1: The positional argument specifiers ' |
| 6163 |
'can be\n' |
| 6164 |
'omitted for "str.format()", so "\'{} {}\'.format(a, b)" is ' |
| 6165 |
'equivalent to\n' |
| 6166 |
'"\'{0} {1}\'.format(a, b)".\n' |
| 6167 |
'\n' |
| 6168 |
'Changed in version 3.4: The positional argument specifiers ' |
| 6169 |
'can be\n' |
| 6170 |
'omitted for "Formatter".\n' |
| 6171 |
'\n' |
| 6172 |
'Some simple format string examples:\n' |
| 6173 |
'\n' |
| 6174 |
' "First, thou shalt count to {0}" # References first ' |
| 6175 |
'positional argument\n' |
| 6176 |
' "Bring me a {}" # Implicitly ' |
| 6177 |
'references the first positional argument\n' |
| 6178 |
' "From {} to {}" # Same as "From {0} to ' |
| 6179 |
'{1}"\n' |
| 6180 |
' "My quest is {name}" # References keyword ' |
| 6181 |
"argument 'name'\n" |
| 6182 |
' "Weight in tons {0.weight}" # \'weight\' attribute ' |
| 6183 |
'of first positional arg\n' |
| 6184 |
' "Units destroyed: {players[0]}" # First element of ' |
| 6185 |
"keyword argument 'players'.\n" |
| 6186 |
'\n' |
| 6187 |
'The *conversion* field causes a type coercion before ' |
| 6188 |
'formatting.\n' |
| 6189 |
'Normally, the job of formatting a value is done by the ' |
| 6190 |
'"__format__()"\n' |
| 6191 |
'method of the value itself. However, in some cases it is ' |
| 6192 |
'desirable to\n' |
| 6193 |
'force a type to be formatted as a string, overriding its ' |
| 6194 |
'own\n' |
| 6195 |
'definition of formatting. By converting the value to a ' |
| 6196 |
'string before\n' |
| 6197 |
'calling "__format__()", the normal formatting logic is ' |
| 6198 |
'bypassed.\n' |
| 6199 |
'\n' |
| 6200 |
'Three conversion flags are currently supported: "\'!s\'" ' |
| 6201 |
'which calls\n' |
| 6202 |
'"str()" on the value, "\'!r\'" which calls "repr()" and ' |
| 6203 |
'"\'!a\'" which\n' |
| 6204 |
'calls "ascii()".\n' |
| 6205 |
'\n' |
| 6206 |
'Some examples:\n' |
| 6207 |
'\n' |
| 6208 |
' "Harold\'s a clever {0!s}" # Calls str() on the ' |
| 6209 |
'argument first\n' |
| 6210 |
' "Bring out the holy {name!r}" # Calls repr() on the ' |
| 6211 |
'argument first\n' |
| 6212 |
' "More {!a}" # Calls ascii() on the ' |
| 6213 |
'argument first\n' |
| 6214 |
'\n' |
| 6215 |
'The *format_spec* field contains a specification of how the ' |
| 6216 |
'value\n' |
| 6217 |
'should be presented, including such details as field width, ' |
| 6218 |
'alignment,\n' |
| 6219 |
'padding, decimal precision and so on. Each value type can ' |
| 6220 |
'define its\n' |
| 6221 |
'own “formatting mini-language” or interpretation of the ' |
| 6222 |
'*format_spec*.\n' |
| 6223 |
'\n' |
| 6224 |
'Most built-in types support a common formatting ' |
| 6225 |
'mini-language, which\n' |
| 6226 |
'is described in the next section.\n' |
| 6227 |
'\n' |
| 6228 |
'A *format_spec* field can also include nested replacement ' |
| 6229 |
'fields\n' |
| 6230 |
'within it. These nested replacement fields may contain a ' |
| 6231 |
'field name,\n' |
| 6232 |
'conversion flag and format specification, but deeper ' |
| 6233 |
'nesting is not\n' |
| 6234 |
'allowed. The replacement fields within the format_spec ' |
| 6235 |
'are\n' |
| 6236 |
'substituted before the *format_spec* string is interpreted. ' |
| 6237 |
'This\n' |
| 6238 |
'allows the formatting of a value to be dynamically ' |
| 6239 |
'specified.\n' |
| 6240 |
'\n' |
| 6241 |
'See the Format examples section for some examples.\n' |
| 6242 |
'\n' |
| 6243 |
'\n' |
| 6244 |
'Format Specification Mini-Language\n' |
| 6245 |
'==================================\n' |
| 6246 |
'\n' |
| 6247 |
'“Format specifications” are used within replacement fields ' |
| 6248 |
'contained\n' |
| 6249 |
'within a format string to define how individual values are ' |
| 6250 |
'presented\n' |
| 6251 |
'(see Format String Syntax and f-strings). They can also be ' |
| 6252 |
'passed\n' |
| 6253 |
'directly to the built-in "format()" function. Each ' |
| 6254 |
'formattable type\n' |
| 6255 |
'may define how the format specification is to be ' |
| 6256 |
'interpreted.\n' |
| 6257 |
'\n' |
| 6258 |
'Most built-in types implement the following options for ' |
| 6259 |
'format\n' |
| 6260 |
'specifications, although some of the formatting options are ' |
| 6261 |
'only\n' |
| 6262 |
'supported by the numeric types.\n' |
| 6263 |
'\n' |
| 6264 |
'A general convention is that an empty format specification ' |
| 6265 |
'produces\n' |
| 6266 |
'the same result as if you had called "str()" on the value. ' |
| 6267 |
'A non-empty\n' |
| 6268 |
'format specification typically modifies the result.\n' |
| 6269 |
'\n' |
| 6270 |
'The general form of a *standard format specifier* is:\n' |
| 6271 |
'\n' |
| 6272 |
' format_spec ::= ' |
| 6273 |
'[[fill]align][sign]["z"]["#"]["0"][width][grouping_option]["." ' |
| 6274 |
'precision][type]\n' |
| 6275 |
' fill ::= <any character>\n' |
| 6276 |
' align ::= "<" | ">" | "=" | "^"\n' |
| 6277 |
' sign ::= "+" | "-" | " "\n' |
| 6278 |
' width ::= digit+\n' |
| 6279 |
' grouping_option ::= "_" | ","\n' |
| 6280 |
' precision ::= digit+\n' |
| 6281 |
' type ::= "b" | "c" | "d" | "e" | "E" | "f" | ' |
| 6282 |
'"F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"\n' |
| 6283 |
'\n' |
| 6284 |
'If a valid *align* value is specified, it can be preceded ' |
| 6285 |
'by a *fill*\n' |
| 6286 |
'character that can be any character and defaults to a space ' |
| 6287 |
'if\n' |
| 6288 |
'omitted. It is not possible to use a literal curly brace ' |
| 6289 |
'(”"{"” or\n' |
| 6290 |
'“"}"”) as the *fill* character in a formatted string ' |
| 6291 |
'literal or when\n' |
| 6292 |
'using the "str.format()" method. However, it is possible ' |
| 6293 |
'to insert a\n' |
| 6294 |
'curly brace with a nested replacement field. This ' |
| 6295 |
'limitation doesn’t\n' |
| 6296 |
'affect the "format()" function.\n' |
| 6297 |
'\n' |
| 6298 |
'The meaning of the various alignment options is as ' |
| 6299 |
'follows:\n' |
| 6300 |
'\n' |
| 6301 |
'+-----------+------------------------------------------------------------+\n' |
| 6302 |
'| Option | ' |
| 6303 |
'Meaning ' |
| 6304 |
'|\n' |
| 6305 |
'|===========|============================================================|\n' |
| 6306 |
'| "\'<\'" | Forces the field to be left-aligned within ' |
| 6307 |
'the available |\n' |
| 6308 |
'| | space (this is the default for most ' |
| 6309 |
'objects). |\n' |
| 6310 |
'+-----------+------------------------------------------------------------+\n' |
| 6311 |
'| "\'>\'" | Forces the field to be right-aligned within ' |
| 6312 |
'the available |\n' |
| 6313 |
'| | space (this is the default for ' |
| 6314 |
'numbers). |\n' |
| 6315 |
'+-----------+------------------------------------------------------------+\n' |
| 6316 |
'| "\'=\'" | Forces the padding to be placed after the ' |
| 6317 |
'sign (if any) |\n' |
| 6318 |
'| | but before the digits. This is used for ' |
| 6319 |
'printing fields |\n' |
| 6320 |
'| | in the form ‘+000000120’. This alignment ' |
| 6321 |
'option is only |\n' |
| 6322 |
'| | valid for numeric types. It becomes the ' |
| 6323 |
'default for |\n' |
| 6324 |
'| | numbers when ‘0’ immediately precedes the ' |
| 6325 |
'field width. |\n' |
| 6326 |
'+-----------+------------------------------------------------------------+\n' |
| 6327 |
'| "\'^\'" | Forces the field to be centered within the ' |
| 6328 |
'available |\n' |
| 6329 |
'| | ' |
| 6330 |
'space. ' |
| 6331 |
'|\n' |
| 6332 |
'+-----------+------------------------------------------------------------+\n' |
| 6333 |
'\n' |
| 6334 |
'Note that unless a minimum field width is defined, the ' |
| 6335 |
'field width\n' |
| 6336 |
'will always be the same size as the data to fill it, so ' |
| 6337 |
'that the\n' |
| 6338 |
'alignment option has no meaning in this case.\n' |
| 6339 |
'\n' |
| 6340 |
'The *sign* option is only valid for number types, and can ' |
| 6341 |
'be one of\n' |
| 6342 |
'the following:\n' |
| 6343 |
'\n' |
| 6344 |
'+-----------+------------------------------------------------------------+\n' |
| 6345 |
'| Option | ' |
| 6346 |
'Meaning ' |
| 6347 |
'|\n' |
| 6348 |
'|===========|============================================================|\n' |
| 6349 |
'| "\'+\'" | indicates that a sign should be used for ' |
| 6350 |
'both positive as |\n' |
| 6351 |
'| | well as negative ' |
| 6352 |
'numbers. |\n' |
| 6353 |
'+-----------+------------------------------------------------------------+\n' |
| 6354 |
'| "\'-\'" | indicates that a sign should be used only ' |
| 6355 |
'for negative |\n' |
| 6356 |
'| | numbers (this is the default ' |
| 6357 |
'behavior). |\n' |
| 6358 |
'+-----------+------------------------------------------------------------+\n' |
| 6359 |
'| space | indicates that a leading space should be used ' |
| 6360 |
'on positive |\n' |
| 6361 |
'| | numbers, and a minus sign on negative ' |
| 6362 |
'numbers. |\n' |
| 6363 |
'+-----------+------------------------------------------------------------+\n' |
| 6364 |
'\n' |
| 6365 |
'The "\'z\'" option coerces negative zero floating-point ' |
| 6366 |
'values to\n' |
| 6367 |
'positive zero after rounding to the format precision. This ' |
| 6368 |
'option is\n' |
| 6369 |
'only valid for floating-point presentation types.\n' |
| 6370 |
'\n' |
| 6371 |
'Changed in version 3.11: Added the "\'z\'" option (see also ' |
| 6372 |
'**PEP\n' |
| 6373 |
'682**).\n' |
| 6374 |
'\n' |
| 6375 |
'The "\'#\'" option causes the “alternate form” to be used ' |
| 6376 |
'for the\n' |
| 6377 |
'conversion. The alternate form is defined differently for ' |
| 6378 |
'different\n' |
| 6379 |
'types. This option is only valid for integer, float and ' |
| 6380 |
'complex\n' |
| 6381 |
'types. For integers, when binary, octal, or hexadecimal ' |
| 6382 |
'output is\n' |
| 6383 |
'used, this option adds the respective prefix "\'0b\'", ' |
| 6384 |
'"\'0o\'", "\'0x\'",\n' |
| 6385 |
'or "\'0X\'" to the output value. For float and complex the ' |
| 6386 |
'alternate\n' |
| 6387 |
'form causes the result of the conversion to always contain ' |
| 6388 |
'a decimal-\n' |
| 6389 |
'point character, even if no digits follow it. Normally, a ' |
| 6390 |
'decimal-\n' |
| 6391 |
'point character appears in the result of these conversions ' |
| 6392 |
'only if a\n' |
| 6393 |
'digit follows it. In addition, for "\'g\'" and "\'G\'" ' |
| 6394 |
'conversions,\n' |
| 6395 |
'trailing zeros are not removed from the result.\n' |
| 6396 |
'\n' |
| 6397 |
'The "\',\'" option signals the use of a comma for a ' |
| 6398 |
'thousands separator.\n' |
| 6399 |
'For a locale aware separator, use the "\'n\'" integer ' |
| 6400 |
'presentation type\n' |
| 6401 |
'instead.\n' |
| 6402 |
'\n' |
| 6403 |
'Changed in version 3.1: Added the "\',\'" option (see also ' |
| 6404 |
'**PEP 378**).\n' |
| 6405 |
'\n' |
| 6406 |
'The "\'_\'" option signals the use of an underscore for a ' |
| 6407 |
'thousands\n' |
| 6408 |
'separator for floating point presentation types and for ' |
| 6409 |
'integer\n' |
| 6410 |
'presentation type "\'d\'". For integer presentation types ' |
| 6411 |
'"\'b\'", "\'o\'",\n' |
| 6412 |
'"\'x\'", and "\'X\'", underscores will be inserted every 4 ' |
| 6413 |
'digits. For\n' |
| 6414 |
'other presentation types, specifying this option is an ' |
| 6415 |
'error.\n' |
| 6416 |
'\n' |
| 6417 |
'Changed in version 3.6: Added the "\'_\'" option (see also ' |
| 6418 |
'**PEP 515**).\n' |
| 6419 |
'\n' |
| 6420 |
'*width* is a decimal integer defining the minimum total ' |
| 6421 |
'field width,\n' |
| 6422 |
'including any prefixes, separators, and other formatting ' |
| 6423 |
'characters.\n' |
| 6424 |
'If not specified, then the field width will be determined ' |
| 6425 |
'by the\n' |
| 6426 |
'content.\n' |
| 6427 |
'\n' |
| 6428 |
'When no explicit alignment is given, preceding the *width* ' |
| 6429 |
'field by a\n' |
| 6430 |
'zero ("\'0\'") character enables sign-aware zero-padding ' |
| 6431 |
'for numeric\n' |
| 6432 |
'types. This is equivalent to a *fill* character of "\'0\'" ' |
| 6433 |
'with an\n' |
| 6434 |
'*alignment* type of "\'=\'".\n' |
| 6435 |
'\n' |
| 6436 |
'Changed in version 3.10: Preceding the *width* field by ' |
| 6437 |
'"\'0\'" no\n' |
| 6438 |
'longer affects the default alignment for strings.\n' |
| 6439 |
'\n' |
| 6440 |
'The *precision* is a decimal integer indicating how many ' |
| 6441 |
'digits should\n' |
| 6442 |
'be displayed after the decimal point for presentation types ' |
| 6443 |
'"\'f\'" and\n' |
| 6444 |
'"\'F\'", or before and after the decimal point for ' |
| 6445 |
'presentation types\n' |
| 6446 |
'"\'g\'" or "\'G\'". For string presentation types the ' |
| 6447 |
'field indicates the\n' |
| 6448 |
'maximum field size - in other words, how many characters ' |
| 6449 |
'will be used\n' |
| 6450 |
'from the field content. The *precision* is not allowed for ' |
| 6451 |
'integer\n' |
| 6452 |
'presentation types.\n' |
| 6453 |
'\n' |
| 6454 |
'Finally, the *type* determines how the data should be ' |
| 6455 |
'presented.\n' |
| 6456 |
'\n' |
| 6457 |
'The available string presentation types are:\n' |
| 6458 |
'\n' |
| 6459 |
' ' |
| 6460 |
'+-----------+------------------------------------------------------------+\n' |
| 6461 |
' | Type | ' |
| 6462 |
'Meaning ' |
| 6463 |
'|\n' |
| 6464 |
' ' |
| 6465 |
'|===========|============================================================|\n' |
| 6466 |
' | "\'s\'" | String format. This is the default type ' |
| 6467 |
'for strings and |\n' |
| 6468 |
' | | may be ' |
| 6469 |
'omitted. |\n' |
| 6470 |
' ' |
| 6471 |
'+-----------+------------------------------------------------------------+\n' |
| 6472 |
' | None | The same as ' |
| 6473 |
'"\'s\'". |\n' |
| 6474 |
' ' |
| 6475 |
'+-----------+------------------------------------------------------------+\n' |
| 6476 |
'\n' |
| 6477 |
'The available integer presentation types are:\n' |
| 6478 |
'\n' |
| 6479 |
' ' |
| 6480 |
'+-----------+------------------------------------------------------------+\n' |
| 6481 |
' | Type | ' |
| 6482 |
'Meaning ' |
| 6483 |
'|\n' |
| 6484 |
' ' |
| 6485 |
'|===========|============================================================|\n' |
| 6486 |
' | "\'b\'" | Binary format. Outputs the number in ' |
| 6487 |
'base 2. |\n' |
| 6488 |
' ' |
| 6489 |
'+-----------+------------------------------------------------------------+\n' |
| 6490 |
' | "\'c\'" | Character. Converts the integer to the ' |
| 6491 |
'corresponding |\n' |
| 6492 |
' | | unicode character before ' |
| 6493 |
'printing. |\n' |
| 6494 |
' ' |
| 6495 |
'+-----------+------------------------------------------------------------+\n' |
| 6496 |
' | "\'d\'" | Decimal Integer. Outputs the number in ' |
| 6497 |
'base 10. |\n' |
| 6498 |
' ' |
| 6499 |
'+-----------+------------------------------------------------------------+\n' |
| 6500 |
' | "\'o\'" | Octal format. Outputs the number in base ' |
| 6501 |
'8. |\n' |
| 6502 |
' ' |
| 6503 |
'+-----------+------------------------------------------------------------+\n' |
| 6504 |
' | "\'x\'" | Hex format. Outputs the number in base ' |
| 6505 |
'16, using lower- |\n' |
| 6506 |
' | | case letters for the digits above ' |
| 6507 |
'9. |\n' |
| 6508 |
' ' |
| 6509 |
'+-----------+------------------------------------------------------------+\n' |
| 6510 |
' | "\'X\'" | Hex format. Outputs the number in base ' |
| 6511 |
'16, using upper- |\n' |
| 6512 |
' | | case letters for the digits above 9. In ' |
| 6513 |
'case "\'#\'" is |\n' |
| 6514 |
' | | specified, the prefix "\'0x\'" will be ' |
| 6515 |
'upper-cased to "\'0X\'" |\n' |
| 6516 |
' | | as ' |
| 6517 |
'well. |\n' |
| 6518 |
' ' |
| 6519 |
'+-----------+------------------------------------------------------------+\n' |
| 6520 |
' | "\'n\'" | Number. This is the same as "\'d\'", ' |
| 6521 |
'except that it uses the |\n' |
| 6522 |
' | | current locale setting to insert the ' |
| 6523 |
'appropriate number |\n' |
| 6524 |
' | | separator ' |
| 6525 |
'characters. |\n' |
| 6526 |
' ' |
| 6527 |
'+-----------+------------------------------------------------------------+\n' |
| 6528 |
' | None | The same as ' |
| 6529 |
'"\'d\'". |\n' |
| 6530 |
' ' |
| 6531 |
'+-----------+------------------------------------------------------------+\n' |
| 6532 |
'\n' |
| 6533 |
'In addition to the above presentation types, integers can ' |
| 6534 |
'be formatted\n' |
| 6535 |
'with the floating point presentation types listed below ' |
| 6536 |
'(except "\'n\'"\n' |
| 6537 |
'and "None"). When doing so, "float()" is used to convert ' |
| 6538 |
'the integer\n' |
| 6539 |
'to a floating point number before formatting.\n' |
| 6540 |
'\n' |
| 6541 |
'The available presentation types for "float" and "Decimal" ' |
| 6542 |
'values are:\n' |
| 6543 |
'\n' |
| 6544 |
' ' |
| 6545 |
'+-----------+------------------------------------------------------------+\n' |
| 6546 |
' | Type | ' |
| 6547 |
'Meaning ' |
| 6548 |
'|\n' |
| 6549 |
' ' |
| 6550 |
'|===========|============================================================|\n' |
| 6551 |
' | "\'e\'" | Scientific notation. For a given ' |
| 6552 |
'precision "p", formats |\n' |
| 6553 |
' | | the number in scientific notation with the ' |
| 6554 |
'letter ‘e’ |\n' |
| 6555 |
' | | separating the coefficient from the ' |
| 6556 |
'exponent. The |\n' |
| 6557 |
' | | coefficient has one digit before and "p" ' |
| 6558 |
'digits after the |\n' |
| 6559 |
' | | decimal point, for a total of "p + 1" ' |
| 6560 |
'significant digits. |\n' |
| 6561 |
' | | With no precision given, uses a precision ' |
| 6562 |
'of "6" digits |\n' |
| 6563 |
' | | after the decimal point for "float", and ' |
| 6564 |
'shows all |\n' |
| 6565 |
' | | coefficient digits for "Decimal". If no ' |
| 6566 |
'digits follow the |\n' |
| 6567 |
' | | decimal point, the decimal point is also ' |
| 6568 |
'removed unless |\n' |
| 6569 |
' | | the "#" option is ' |
| 6570 |
'used. |\n' |
| 6571 |
' ' |
| 6572 |
'+-----------+------------------------------------------------------------+\n' |
| 6573 |
' | "\'E\'" | Scientific notation. Same as "\'e\'" ' |
| 6574 |
'except it uses an upper |\n' |
| 6575 |
' | | case ‘E’ as the separator ' |
| 6576 |
'character. |\n' |
| 6577 |
' ' |
| 6578 |
'+-----------+------------------------------------------------------------+\n' |
| 6579 |
' | "\'f\'" | Fixed-point notation. For a given ' |
| 6580 |
'precision "p", formats |\n' |
| 6581 |
' | | the number as a decimal number with ' |
| 6582 |
'exactly "p" digits |\n' |
| 6583 |
' | | following the decimal point. With no ' |
| 6584 |
'precision given, uses |\n' |
| 6585 |
' | | a precision of "6" digits after the ' |
| 6586 |
'decimal point for |\n' |
| 6587 |
' | | "float", and uses a precision large enough ' |
| 6588 |
'to show all |\n' |
| 6589 |
' | | coefficient digits for "Decimal". If no ' |
| 6590 |
'digits follow the |\n' |
| 6591 |
' | | decimal point, the decimal point is also ' |
| 6592 |
'removed unless |\n' |
| 6593 |
' | | the "#" option is ' |
| 6594 |
'used. |\n' |
| 6595 |
' ' |
| 6596 |
'+-----------+------------------------------------------------------------+\n' |
| 6597 |
' | "\'F\'" | Fixed-point notation. Same as "\'f\'", ' |
| 6598 |
'but converts "nan" to |\n' |
| 6599 |
' | | "NAN" and "inf" to ' |
| 6600 |
'"INF". |\n' |
| 6601 |
' ' |
| 6602 |
'+-----------+------------------------------------------------------------+\n' |
| 6603 |
' | "\'g\'" | General format. For a given precision ' |
| 6604 |
'"p >= 1", this |\n' |
| 6605 |
' | | rounds the number to "p" significant ' |
| 6606 |
'digits and then |\n' |
| 6607 |
' | | formats the result in either fixed-point ' |
| 6608 |
'format or in |\n' |
| 6609 |
' | | scientific notation, depending on its ' |
| 6610 |
'magnitude. A |\n' |
| 6611 |
' | | precision of "0" is treated as equivalent ' |
| 6612 |
'to a precision |\n' |
| 6613 |
' | | of "1". The precise rules are as follows: ' |
| 6614 |
'suppose that |\n' |
| 6615 |
' | | the result formatted with presentation ' |
| 6616 |
'type "\'e\'" and |\n' |
| 6617 |
' | | precision "p-1" would have exponent ' |
| 6618 |
'"exp". Then, if "m <= |\n' |
| 6619 |
' | | exp < p", where "m" is -4 for floats and ' |
| 6620 |
'-6 for |\n' |
| 6621 |
' | | "Decimals", the number is formatted with ' |
| 6622 |
'presentation type |\n' |
| 6623 |
' | | "\'f\'" and precision "p-1-exp". ' |
| 6624 |
'Otherwise, the number is |\n' |
| 6625 |
' | | formatted with presentation type "\'e\'" ' |
| 6626 |
'and precision |\n' |
| 6627 |
' | | "p-1". In both cases insignificant ' |
| 6628 |
'trailing zeros are |\n' |
| 6629 |
' | | removed from the significand, and the ' |
| 6630 |
'decimal point is |\n' |
| 6631 |
' | | also removed if there are no remaining ' |
| 6632 |
'digits following |\n' |
| 6633 |
' | | it, unless the "\'#\'" option is used. ' |
| 6634 |
'With no precision |\n' |
| 6635 |
' | | given, uses a precision of "6" significant ' |
| 6636 |
'digits for |\n' |
| 6637 |
' | | "float". For "Decimal", the coefficient of ' |
| 6638 |
'the result is |\n' |
| 6639 |
' | | formed from the coefficient digits of the ' |
| 6640 |
'value; |\n' |
| 6641 |
' | | scientific notation is used for values ' |
| 6642 |
'smaller than "1e-6" |\n' |
| 6643 |
' | | in absolute value and values where the ' |
| 6644 |
'place value of the |\n' |
| 6645 |
' | | least significant digit is larger than 1, ' |
| 6646 |
'and fixed-point |\n' |
| 6647 |
' | | notation is used otherwise. Positive and ' |
| 6648 |
'negative |\n' |
| 6649 |
' | | infinity, positive and negative zero, and ' |
| 6650 |
'nans, are |\n' |
| 6651 |
' | | formatted as "inf", "-inf", "0", "-0" and ' |
| 6652 |
'"nan" |\n' |
| 6653 |
' | | respectively, regardless of the ' |
| 6654 |
'precision. |\n' |
| 6655 |
' ' |
| 6656 |
'+-----------+------------------------------------------------------------+\n' |
| 6657 |
' | "\'G\'" | General format. Same as "\'g\'" except ' |
| 6658 |
'switches to "\'E\'" if |\n' |
| 6659 |
' | | the number gets too large. The ' |
| 6660 |
'representations of infinity |\n' |
| 6661 |
' | | and NaN are uppercased, ' |
| 6662 |
'too. |\n' |
| 6663 |
' ' |
| 6664 |
'+-----------+------------------------------------------------------------+\n' |
| 6665 |
' | "\'n\'" | Number. This is the same as "\'g\'", ' |
| 6666 |
'except that it uses the |\n' |
| 6667 |
' | | current locale setting to insert the ' |
| 6668 |
'appropriate number |\n' |
| 6669 |
' | | separator ' |
| 6670 |
'characters. |\n' |
| 6671 |
' ' |
| 6672 |
'+-----------+------------------------------------------------------------+\n' |
| 6673 |
' | "\'%\'" | Percentage. Multiplies the number by 100 ' |
| 6674 |
'and displays in |\n' |
| 6675 |
' | | fixed ("\'f\'") format, followed by a ' |
| 6676 |
'percent sign. |\n' |
| 6677 |
' ' |
| 6678 |
'+-----------+------------------------------------------------------------+\n' |
| 6679 |
' | None | For "float" this is the same as "\'g\'", ' |
| 6680 |
'except that when |\n' |
| 6681 |
' | | fixed-point notation is used to format the ' |
| 6682 |
'result, it |\n' |
| 6683 |
' | | always includes at least one digit past ' |
| 6684 |
'the decimal point. |\n' |
| 6685 |
' | | The precision used is as large as needed ' |
| 6686 |
'to represent the |\n' |
| 6687 |
' | | given value faithfully. For "Decimal", ' |
| 6688 |
'this is the same |\n' |
| 6689 |
' | | as either "\'g\'" or "\'G\'" depending on ' |
| 6690 |
'the value of |\n' |
| 6691 |
' | | "context.capitals" for the current decimal ' |
| 6692 |
'context. The |\n' |
| 6693 |
' | | overall effect is to match the output of ' |
| 6694 |
'"str()" as |\n' |
| 6695 |
' | | altered by the other format ' |
| 6696 |
'modifiers. |\n' |
| 6697 |
' ' |
| 6698 |
'+-----------+------------------------------------------------------------+\n' |
| 6699 |
'\n' |
| 6700 |
'\n' |
| 6701 |
'Format examples\n' |
| 6702 |
'===============\n' |
| 6703 |
'\n' |
| 6704 |
'This section contains examples of the "str.format()" syntax ' |
| 6705 |
'and\n' |
| 6706 |
'comparison with the old "%"-formatting.\n' |
| 6707 |
'\n' |
| 6708 |
'In most of the cases the syntax is similar to the old ' |
| 6709 |
'"%"-formatting,\n' |
| 6710 |
'with the addition of the "{}" and with ":" used instead of ' |
| 6711 |
'"%". For\n' |
| 6712 |
'example, "\'%03.2f\'" can be translated to "\'{:03.2f}\'".\n' |
| 6713 |
'\n' |
| 6714 |
'The new format syntax also supports new and different ' |
| 6715 |
'options, shown\n' |
| 6716 |
'in the following examples.\n' |
| 6717 |
'\n' |
| 6718 |
'Accessing arguments by position:\n' |
| 6719 |
'\n' |
| 6720 |
" >>> '{0}, {1}, {2}'.format('a', 'b', 'c')\n" |
| 6721 |
" 'a, b, c'\n" |
| 6722 |
" >>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only\n" |
| 6723 |
" 'a, b, c'\n" |
| 6724 |
" >>> '{2}, {1}, {0}'.format('a', 'b', 'c')\n" |
| 6725 |
" 'c, b, a'\n" |
| 6726 |
" >>> '{2}, {1}, {0}'.format(*'abc') # unpacking " |
| 6727 |
'argument sequence\n' |
| 6728 |
" 'c, b, a'\n" |
| 6729 |
" >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' " |
| 6730 |
'indices can be repeated\n' |
| 6731 |
" 'abracadabra'\n" |
| 6732 |
'\n' |
| 6733 |
'Accessing arguments by name:\n' |
| 6734 |
'\n' |
| 6735 |
" >>> 'Coordinates: {latitude}, " |
| 6736 |
"{longitude}'.format(latitude='37.24N', " |
| 6737 |
"longitude='-115.81W')\n" |
| 6738 |
" 'Coordinates: 37.24N, -115.81W'\n" |
| 6739 |
" >>> coord = {'latitude': '37.24N', 'longitude': " |
| 6740 |
"'-115.81W'}\n" |
| 6741 |
" >>> 'Coordinates: {latitude}, " |
| 6742 |
"{longitude}'.format(**coord)\n" |
| 6743 |
" 'Coordinates: 37.24N, -115.81W'\n" |
| 6744 |
'\n' |
| 6745 |
'Accessing arguments’ attributes:\n' |
| 6746 |
'\n' |
| 6747 |
' >>> c = 3-5j\n' |
| 6748 |
" >>> ('The complex number {0} is formed from the real " |
| 6749 |
"part {0.real} '\n" |
| 6750 |
" ... 'and the imaginary part {0.imag}.').format(c)\n" |
| 6751 |
" 'The complex number (3-5j) is formed from the real part " |
| 6752 |
"3.0 and the imaginary part -5.0.'\n" |
| 6753 |
' >>> class Point:\n' |
| 6754 |
' ... def __init__(self, x, y):\n' |
| 6755 |
' ... self.x, self.y = x, y\n' |
| 6756 |
' ... def __str__(self):\n' |
| 6757 |
" ... return 'Point({self.x}, " |
| 6758 |
"{self.y})'.format(self=self)\n" |
| 6759 |
' ...\n' |
| 6760 |
' >>> str(Point(4, 2))\n' |
| 6761 |
" 'Point(4, 2)'\n" |
| 6762 |
'\n' |
| 6763 |
'Accessing arguments’ items:\n' |
| 6764 |
'\n' |
| 6765 |
' >>> coord = (3, 5)\n' |
| 6766 |
" >>> 'X: {0[0]}; Y: {0[1]}'.format(coord)\n" |
| 6767 |
" 'X: 3; Y: 5'\n" |
| 6768 |
'\n' |
| 6769 |
'Replacing "%s" and "%r":\n' |
| 6770 |
'\n' |
| 6771 |
' >>> "repr() shows quotes: {!r}; str() doesn\'t: ' |
| 6772 |
'{!s}".format(\'test1\', \'test2\')\n' |
| 6773 |
' "repr() shows quotes: \'test1\'; str() doesn\'t: test2"\n' |
| 6774 |
'\n' |
| 6775 |
'Aligning the text and specifying a width:\n' |
| 6776 |
'\n' |
| 6777 |
" >>> '{:<30}'.format('left aligned')\n" |
| 6778 |
" 'left aligned '\n" |
| 6779 |
" >>> '{:>30}'.format('right aligned')\n" |
| 6780 |
" ' right aligned'\n" |
| 6781 |
" >>> '{:^30}'.format('centered')\n" |
| 6782 |
" ' centered '\n" |
| 6783 |
" >>> '{:*^30}'.format('centered') # use '*' as a fill " |
| 6784 |
'char\n' |
| 6785 |
" '***********centered***********'\n" |
| 6786 |
'\n' |
| 6787 |
'Replacing "%+f", "%-f", and "% f" and specifying a sign:\n' |
| 6788 |
'\n' |
| 6789 |
" >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it " |
| 6790 |
'always\n' |
| 6791 |
" '+3.140000; -3.140000'\n" |
| 6792 |
" >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space " |
| 6793 |
'for positive numbers\n' |
| 6794 |
" ' 3.140000; -3.140000'\n" |
| 6795 |
" >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the " |
| 6796 |
"minus -- same as '{:f}; {:f}'\n" |
| 6797 |
" '3.140000; -3.140000'\n" |
| 6798 |
'\n' |
| 6799 |
'Replacing "%x" and "%o" and converting the value to ' |
| 6800 |
'different bases:\n' |
| 6801 |
'\n' |
| 6802 |
' >>> # format also supports binary numbers\n' |
| 6803 |
' >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: ' |
| 6804 |
'{0:b}".format(42)\n' |
| 6805 |
" 'int: 42; hex: 2a; oct: 52; bin: 101010'\n" |
| 6806 |
' >>> # with 0x, 0o, or 0b as prefix:\n' |
| 6807 |
' >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: ' |
| 6808 |
'{0:#b}".format(42)\n' |
| 6809 |
" 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'\n" |
| 6810 |
'\n' |
| 6811 |
'Using the comma as a thousands separator:\n' |
| 6812 |
'\n' |
| 6813 |
" >>> '{:,}'.format(1234567890)\n" |
| 6814 |
" '1,234,567,890'\n" |
| 6815 |
'\n' |
| 6816 |
'Expressing a percentage:\n' |
| 6817 |
'\n' |
| 6818 |
' >>> points = 19\n' |
| 6819 |
' >>> total = 22\n' |
| 6820 |
" >>> 'Correct answers: {:.2%}'.format(points/total)\n" |
| 6821 |
" 'Correct answers: 86.36%'\n" |
| 6822 |
'\n' |
| 6823 |
'Using type-specific formatting:\n' |
| 6824 |
'\n' |
| 6825 |
' >>> import datetime\n' |
| 6826 |
' >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)\n' |
| 6827 |
" >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)\n" |
| 6828 |
" '2010-07-04 12:15:58'\n" |
| 6829 |
'\n' |
| 6830 |
'Nesting arguments and more complex examples:\n' |
| 6831 |
'\n' |
| 6832 |
" >>> for align, text in zip('<^>', ['left', 'center', " |
| 6833 |
"'right']):\n" |
| 6834 |
" ... '{0:{fill}{align}16}'.format(text, fill=align, " |
| 6835 |
'align=align)\n' |
| 6836 |
' ...\n' |
| 6837 |
" 'left<<<<<<<<<<<<'\n" |
| 6838 |
" '^^^^^center^^^^^'\n" |
| 6839 |
" '>>>>>>>>>>>right'\n" |
| 6840 |
' >>>\n' |
| 6841 |
' >>> octets = [192, 168, 0, 1]\n' |
| 6842 |
" >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)\n" |
| 6843 |
" 'C0A80001'\n" |
| 6844 |
' >>> int(_, 16)\n' |
| 6845 |
' 3232235521\n' |
| 6846 |
' >>>\n' |
| 6847 |
' >>> width = 5\n' |
| 6848 |
' >>> for num in range(5,12): \n' |
| 6849 |
" ... for base in 'dXob':\n" |
| 6850 |
" ... print('{0:{width}{base}}'.format(num, " |
| 6851 |
"base=base, width=width), end=' ')\n" |
| 6852 |
' ... print()\n' |
| 6853 |
' ...\n' |
| 6854 |
' 5 5 5 101\n' |
| 6855 |
' 6 6 6 110\n' |
| 6856 |
' 7 7 7 111\n' |
| 6857 |
' 8 8 10 1000\n' |
| 6858 |
' 9 9 11 1001\n' |
| 6859 |
' 10 A 12 1010\n' |
| 6860 |
' 11 B 13 1011\n', |
| 6861 |
'function': 'Function definitions\n' |
| 6862 |
'********************\n' |
| 6863 |
'\n' |
| 6864 |
'A function definition defines a user-defined function object ' |
| 6865 |
'(see\n' |
| 6866 |
'section The standard type hierarchy):\n' |
| 6867 |
'\n' |
| 6868 |
' funcdef ::= [decorators] "def" funcname "(" ' |
| 6869 |
'[parameter_list] ")"\n' |
| 6870 |
' ["->" expression] ":" suite\n' |
| 6871 |
' decorators ::= decorator+\n' |
| 6872 |
' decorator ::= "@" assignment_expression ' |
| 6873 |
'NEWLINE\n' |
| 6874 |
' parameter_list ::= defparameter ("," ' |
| 6875 |
'defparameter)* "," "/" ["," [parameter_list_no_posonly]]\n' |
| 6876 |
' | parameter_list_no_posonly\n' |
| 6877 |
' parameter_list_no_posonly ::= defparameter ("," ' |
| 6878 |
'defparameter)* ["," [parameter_list_starargs]]\n' |
| 6879 |
' | parameter_list_starargs\n' |
| 6880 |
' parameter_list_starargs ::= "*" [parameter] ("," ' |
| 6881 |
'defparameter)* ["," ["**" parameter [","]]]\n' |
| 6882 |
' | "**" parameter [","]\n' |
| 6883 |
' parameter ::= identifier [":" expression]\n' |
| 6884 |
' defparameter ::= parameter ["=" expression]\n' |
| 6885 |
' funcname ::= identifier\n' |
| 6886 |
'\n' |
| 6887 |
'A function definition is an executable statement. Its execution ' |
| 6888 |
'binds\n' |
| 6889 |
'the function name in the current local namespace to a function ' |
| 6890 |
'object\n' |
| 6891 |
'(a wrapper around the executable code for the function). This\n' |
| 6892 |
'function object contains a reference to the current global ' |
| 6893 |
'namespace\n' |
| 6894 |
'as the global namespace to be used when the function is called.\n' |
| 6895 |
'\n' |
| 6896 |
'The function definition does not execute the function body; this ' |
| 6897 |
'gets\n' |
| 6898 |
'executed only when the function is called. [4]\n' |
| 6899 |
'\n' |
| 6900 |
'A function definition may be wrapped by one or more *decorator*\n' |
| 6901 |
'expressions. Decorator expressions are evaluated when the ' |
| 6902 |
'function is\n' |
| 6903 |
'defined, in the scope that contains the function definition. ' |
| 6904 |
'The\n' |
| 6905 |
'result must be a callable, which is invoked with the function ' |
| 6906 |
'object\n' |
| 6907 |
'as the only argument. The returned value is bound to the ' |
| 6908 |
'function name\n' |
| 6909 |
'instead of the function object. Multiple decorators are applied ' |
| 6910 |
'in\n' |
| 6911 |
'nested fashion. For example, the following code\n' |
| 6912 |
'\n' |
| 6913 |
' @f1(arg)\n' |
| 6914 |
' @f2\n' |
| 6915 |
' def func(): pass\n' |
| 6916 |
'\n' |
| 6917 |
'is roughly equivalent to\n' |
| 6918 |
'\n' |
| 6919 |
' def func(): pass\n' |
| 6920 |
' func = f1(arg)(f2(func))\n' |
| 6921 |
'\n' |
| 6922 |
'except that the original function is not temporarily bound to ' |
| 6923 |
'the name\n' |
| 6924 |
'"func".\n' |
| 6925 |
'\n' |
| 6926 |
'Changed in version 3.9: Functions may be decorated with any ' |
| 6927 |
'valid\n' |
| 6928 |
'"assignment_expression". Previously, the grammar was much more\n' |
| 6929 |
'restrictive; see **PEP 614** for details.\n' |
| 6930 |
'\n' |
| 6931 |
'When one or more *parameters* have the form *parameter* "="\n' |
| 6932 |
'*expression*, the function is said to have “default parameter ' |
| 6933 |
'values.”\n' |
| 6934 |
'For a parameter with a default value, the corresponding ' |
| 6935 |
'*argument* may\n' |
| 6936 |
'be omitted from a call, in which case the parameter’s default ' |
| 6937 |
'value is\n' |
| 6938 |
'substituted. If a parameter has a default value, all following\n' |
| 6939 |
'parameters up until the “"*"” must also have a default value — ' |
| 6940 |
'this is\n' |
| 6941 |
'a syntactic restriction that is not expressed by the grammar.\n' |
| 6942 |
'\n' |
| 6943 |
'**Default parameter values are evaluated from left to right when ' |
| 6944 |
'the\n' |
| 6945 |
'function definition is executed.** This means that the ' |
| 6946 |
'expression is\n' |
| 6947 |
'evaluated once, when the function is defined, and that the same ' |
| 6948 |
'“pre-\n' |
| 6949 |
'computed” value is used for each call. This is especially ' |
| 6950 |
'important\n' |
| 6951 |
'to understand when a default parameter value is a mutable ' |
| 6952 |
'object, such\n' |
| 6953 |
'as a list or a dictionary: if the function modifies the object ' |
| 6954 |
'(e.g.\n' |
| 6955 |
'by appending an item to a list), the default parameter value is ' |
| 6956 |
'in\n' |
| 6957 |
'effect modified. This is generally not what was intended. A ' |
| 6958 |
'way\n' |
| 6959 |
'around this is to use "None" as the default, and explicitly test ' |
| 6960 |
'for\n' |
| 6961 |
'it in the body of the function, e.g.:\n' |
| 6962 |
'\n' |
| 6963 |
' def whats_on_the_telly(penguin=None):\n' |
| 6964 |
' if penguin is None:\n' |
| 6965 |
' penguin = []\n' |
| 6966 |
' penguin.append("property of the zoo")\n' |
| 6967 |
' return penguin\n' |
| 6968 |
'\n' |
| 6969 |
'Function call semantics are described in more detail in section ' |
| 6970 |
'Calls.\n' |
| 6971 |
'A function call always assigns values to all parameters ' |
| 6972 |
'mentioned in\n' |
| 6973 |
'the parameter list, either from positional arguments, from ' |
| 6974 |
'keyword\n' |
| 6975 |
'arguments, or from default values. If the form “"*identifier"” ' |
| 6976 |
'is\n' |
| 6977 |
'present, it is initialized to a tuple receiving any excess ' |
| 6978 |
'positional\n' |
| 6979 |
'parameters, defaulting to the empty tuple. If the form\n' |
| 6980 |
'“"**identifier"” is present, it is initialized to a new ordered\n' |
| 6981 |
'mapping receiving any excess keyword arguments, defaulting to a ' |
| 6982 |
'new\n' |
| 6983 |
'empty mapping of the same type. Parameters after “"*"” or\n' |
| 6984 |
'“"*identifier"” are keyword-only parameters and may only be ' |
| 6985 |
'passed by\n' |
| 6986 |
'keyword arguments. Parameters before “"/"” are positional-only\n' |
| 6987 |
'parameters and may only be passed by positional arguments.\n' |
| 6988 |
'\n' |
| 6989 |
'Changed in version 3.8: The "/" function parameter syntax may be ' |
| 6990 |
'used\n' |
| 6991 |
'to indicate positional-only parameters. See **PEP 570** for ' |
| 6992 |
'details.\n' |
| 6993 |
'\n' |
| 6994 |
'Parameters may have an *annotation* of the form “": ' |
| 6995 |
'expression"”\n' |
| 6996 |
'following the parameter name. Any parameter may have an ' |
| 6997 |
'annotation,\n' |
| 6998 |
'even those of the form "*identifier" or "**identifier". ' |
| 6999 |
'Functions may\n' |
| 7000 |
'have “return” annotation of the form “"-> expression"” after ' |
| 7001 |
'the\n' |
| 7002 |
'parameter list. These annotations can be any valid Python ' |
| 7003 |
'expression.\n' |
| 7004 |
'The presence of annotations does not change the semantics of a\n' |
| 7005 |
'function. The annotation values are available as values of a\n' |
| 7006 |
'dictionary keyed by the parameters’ names in the ' |
| 7007 |
'"__annotations__"\n' |
| 7008 |
'attribute of the function object. If the "annotations" import ' |
| 7009 |
'from\n' |
| 7010 |
'"__future__" is used, annotations are preserved as strings at ' |
| 7011 |
'runtime\n' |
| 7012 |
'which enables postponed evaluation. Otherwise, they are ' |
| 7013 |
'evaluated\n' |
| 7014 |
'when the function definition is executed. In this case ' |
| 7015 |
'annotations\n' |
| 7016 |
'may be evaluated in a different order than they appear in the ' |
| 7017 |
'source\n' |
| 7018 |
'code.\n' |
| 7019 |
'\n' |
| 7020 |
'It is also possible to create anonymous functions (functions not ' |
| 7021 |
'bound\n' |
| 7022 |
'to a name), for immediate use in expressions. This uses lambda\n' |
| 7023 |
'expressions, described in section Lambdas. Note that the ' |
| 7024 |
'lambda\n' |
| 7025 |
'expression is merely a shorthand for a simplified function ' |
| 7026 |
'definition;\n' |
| 7027 |
'a function defined in a “"def"” statement can be passed around ' |
| 7028 |
'or\n' |
| 7029 |
'assigned to another name just like a function defined by a ' |
| 7030 |
'lambda\n' |
| 7031 |
'expression. The “"def"” form is actually more powerful since ' |
| 7032 |
'it\n' |
| 7033 |
'allows the execution of multiple statements and annotations.\n' |
| 7034 |
'\n' |
| 7035 |
'**Programmer’s note:** Functions are first-class objects. A ' |
| 7036 |
'“"def"”\n' |
| 7037 |
'statement executed inside a function definition defines a local\n' |
| 7038 |
'function that can be returned or passed around. Free variables ' |
| 7039 |
'used\n' |
| 7040 |
'in the nested function can access the local variables of the ' |
| 7041 |
'function\n' |
| 7042 |
'containing the def. See section Naming and binding for ' |
| 7043 |
'details.\n' |
| 7044 |
'\n' |
| 7045 |
'See also:\n' |
| 7046 |
'\n' |
| 7047 |
' **PEP 3107** - Function Annotations\n' |
| 7048 |
' The original specification for function annotations.\n' |
| 7049 |
'\n' |
| 7050 |
' **PEP 484** - Type Hints\n' |
| 7051 |
' Definition of a standard meaning for annotations: type ' |
| 7052 |
'hints.\n' |
| 7053 |
'\n' |
| 7054 |
' **PEP 526** - Syntax for Variable Annotations\n' |
| 7055 |
' Ability to type hint variable declarations, including ' |
| 7056 |
'class\n' |
| 7057 |
' variables and instance variables.\n' |
| 7058 |
'\n' |
| 7059 |
' **PEP 563** - Postponed Evaluation of Annotations\n' |
| 7060 |
' Support for forward references within annotations by ' |
| 7061 |
'preserving\n' |
| 7062 |
' annotations in a string form at runtime instead of eager\n' |
| 7063 |
' evaluation.\n' |
| 7064 |
'\n' |
| 7065 |
' **PEP 318** - Decorators for Functions and Methods\n' |
| 7066 |
' Function and method decorators were introduced. Class ' |
| 7067 |
'decorators\n' |
| 7068 |
' were introduced in **PEP 3129**.\n', |
| 7069 |
'global': 'The "global" statement\n' |
| 7070 |
'**********************\n' |
| 7071 |
'\n' |
| 7072 |
' global_stmt ::= "global" identifier ("," identifier)*\n' |
| 7073 |
'\n' |
| 7074 |
'The "global" statement is a declaration which holds for the ' |
| 7075 |
'entire\n' |
| 7076 |
'current code block. It means that the listed identifiers are to ' |
| 7077 |
'be\n' |
| 7078 |
'interpreted as globals. It would be impossible to assign to a ' |
| 7079 |
'global\n' |
| 7080 |
'variable without "global", although free variables may refer to\n' |
| 7081 |
'globals without being declared global.\n' |
| 7082 |
'\n' |
| 7083 |
'Names listed in a "global" statement must not be used in the same ' |
| 7084 |
'code\n' |
| 7085 |
'block textually preceding that "global" statement.\n' |
| 7086 |
'\n' |
| 7087 |
'Names listed in a "global" statement must not be defined as ' |
| 7088 |
'formal\n' |
| 7089 |
'parameters, or as targets in "with" statements or "except" ' |
| 7090 |
'clauses, or\n' |
| 7091 |
'in a "for" target list, "class" definition, function definition,\n' |
| 7092 |
'"import" statement, or variable annotation.\n' |
| 7093 |
'\n' |
| 7094 |
'**CPython implementation detail:** The current implementation does ' |
| 7095 |
'not\n' |
| 7096 |
'enforce some of these restrictions, but programs should not abuse ' |
| 7097 |
'this\n' |
| 7098 |
'freedom, as future implementations may enforce them or silently ' |
| 7099 |
'change\n' |
| 7100 |
'the meaning of the program.\n' |
| 7101 |
'\n' |
| 7102 |
'**Programmer’s note:** "global" is a directive to the parser. It\n' |
| 7103 |
'applies only to code parsed at the same time as the "global"\n' |
| 7104 |
'statement. In particular, a "global" statement contained in a ' |
| 7105 |
'string\n' |
| 7106 |
'or code object supplied to the built-in "exec()" function does ' |
| 7107 |
'not\n' |
| 7108 |
'affect the code block *containing* the function call, and code\n' |
| 7109 |
'contained in such a string is unaffected by "global" statements in ' |
| 7110 |
'the\n' |
| 7111 |
'code containing the function call. The same applies to the ' |
| 7112 |
'"eval()"\n' |
| 7113 |
'and "compile()" functions.\n', |
| 7114 |
'id-classes': 'Reserved classes of identifiers\n' |
| 7115 |
'*******************************\n' |
| 7116 |
'\n' |
| 7117 |
'Certain classes of identifiers (besides keywords) have ' |
| 7118 |
'special\n' |
| 7119 |
'meanings. These classes are identified by the patterns of ' |
| 7120 |
'leading and\n' |
| 7121 |
'trailing underscore characters:\n' |
| 7122 |
'\n' |
| 7123 |
'"_*"\n' |
| 7124 |
' Not imported by "from module import *".\n' |
| 7125 |
'\n' |
| 7126 |
'"_"\n' |
| 7127 |
' In a "case" pattern within a "match" statement, "_" is a ' |
| 7128 |
'soft\n' |
| 7129 |
' keyword that denotes a wildcard.\n' |
| 7130 |
'\n' |
| 7131 |
' Separately, the interactive interpreter makes the result of ' |
| 7132 |
'the\n' |
| 7133 |
' last evaluation available in the variable "_". (It is ' |
| 7134 |
'stored in the\n' |
| 7135 |
' "builtins" module, alongside built-in functions like ' |
| 7136 |
'"print".)\n' |
| 7137 |
'\n' |
| 7138 |
' Elsewhere, "_" is a regular identifier. It is often used to ' |
| 7139 |
'name\n' |
| 7140 |
' “special” items, but it is not special to Python itself.\n' |
| 7141 |
'\n' |
| 7142 |
' Note:\n' |
| 7143 |
'\n' |
| 7144 |
' The name "_" is often used in conjunction with\n' |
| 7145 |
' internationalization; refer to the documentation for the\n' |
| 7146 |
' "gettext" module for more information on this ' |
| 7147 |
'convention.It is\n' |
| 7148 |
' also commonly used for unused variables.\n' |
| 7149 |
'\n' |
| 7150 |
'"__*__"\n' |
| 7151 |
' System-defined names, informally known as “dunder” names. ' |
| 7152 |
'These\n' |
| 7153 |
' names are defined by the interpreter and its ' |
| 7154 |
'implementation\n' |
| 7155 |
' (including the standard library). Current system names are\n' |
| 7156 |
' discussed in the Special method names section and ' |
| 7157 |
'elsewhere. More\n' |
| 7158 |
' will likely be defined in future versions of Python. *Any* ' |
| 7159 |
'use of\n' |
| 7160 |
' "__*__" names, in any context, that does not follow ' |
| 7161 |
'explicitly\n' |
| 7162 |
' documented use, is subject to breakage without warning.\n' |
| 7163 |
'\n' |
| 7164 |
'"__*"\n' |
| 7165 |
' Class-private names. Names in this category, when used ' |
| 7166 |
'within the\n' |
| 7167 |
' context of a class definition, are re-written to use a ' |
| 7168 |
'mangled form\n' |
| 7169 |
' to help avoid name clashes between “private” attributes of ' |
| 7170 |
'base and\n' |
| 7171 |
' derived classes. See section Identifiers (Names).\n', |
| 7172 |
'identifiers': 'Identifiers and keywords\n' |
| 7173 |
'************************\n' |
| 7174 |
'\n' |
| 7175 |
'Identifiers (also referred to as *names*) are described by ' |
| 7176 |
'the\n' |
| 7177 |
'following lexical definitions.\n' |
| 7178 |
'\n' |
| 7179 |
'The syntax of identifiers in Python is based on the Unicode ' |
| 7180 |
'standard\n' |
| 7181 |
'annex UAX-31, with elaboration and changes as defined below; ' |
| 7182 |
'see also\n' |
| 7183 |
'**PEP 3131** for further details.\n' |
| 7184 |
'\n' |
| 7185 |
'Within the ASCII range (U+0001..U+007F), the valid characters ' |
| 7186 |
'for\n' |
| 7187 |
'identifiers are the same as in Python 2.x: the uppercase and ' |
| 7188 |
'lowercase\n' |
| 7189 |
'letters "A" through "Z", the underscore "_" and, except for ' |
| 7190 |
'the first\n' |
| 7191 |
'character, the digits "0" through "9".\n' |
| 7192 |
'\n' |
| 7193 |
'Python 3.0 introduces additional characters from outside the ' |
| 7194 |
'ASCII\n' |
| 7195 |
'range (see **PEP 3131**). For these characters, the ' |
| 7196 |
'classification\n' |
| 7197 |
'uses the version of the Unicode Character Database as ' |
| 7198 |
'included in the\n' |
| 7199 |
'"unicodedata" module.\n' |
| 7200 |
'\n' |
| 7201 |
'Identifiers are unlimited in length. Case is significant.\n' |
| 7202 |
'\n' |
| 7203 |
' identifier ::= xid_start xid_continue*\n' |
| 7204 |
' id_start ::= <all characters in general categories Lu, ' |
| 7205 |
'Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the ' |
| 7206 |
'Other_ID_Start property>\n' |
| 7207 |
' id_continue ::= <all characters in id_start, plus ' |
| 7208 |
'characters in the categories Mn, Mc, Nd, Pc and others with ' |
| 7209 |
'the Other_ID_Continue property>\n' |
| 7210 |
' xid_start ::= <all characters in id_start whose NFKC ' |
| 7211 |
'normalization is in "id_start xid_continue*">\n' |
| 7212 |
' xid_continue ::= <all characters in id_continue whose NFKC ' |
| 7213 |
'normalization is in "id_continue*">\n' |
| 7214 |
'\n' |
| 7215 |
'The Unicode category codes mentioned above stand for:\n' |
| 7216 |
'\n' |
| 7217 |
'* *Lu* - uppercase letters\n' |
| 7218 |
'\n' |
| 7219 |
'* *Ll* - lowercase letters\n' |
| 7220 |
'\n' |
| 7221 |
'* *Lt* - titlecase letters\n' |
| 7222 |
'\n' |
| 7223 |
'* *Lm* - modifier letters\n' |
| 7224 |
'\n' |
| 7225 |
'* *Lo* - other letters\n' |
| 7226 |
'\n' |
| 7227 |
'* *Nl* - letter numbers\n' |
| 7228 |
'\n' |
| 7229 |
'* *Mn* - nonspacing marks\n' |
| 7230 |
'\n' |
| 7231 |
'* *Mc* - spacing combining marks\n' |
| 7232 |
'\n' |
| 7233 |
'* *Nd* - decimal numbers\n' |
| 7234 |
'\n' |
| 7235 |
'* *Pc* - connector punctuations\n' |
| 7236 |
'\n' |
| 7237 |
'* *Other_ID_Start* - explicit list of characters in ' |
| 7238 |
'PropList.txt to\n' |
| 7239 |
' support backwards compatibility\n' |
| 7240 |
'\n' |
| 7241 |
'* *Other_ID_Continue* - likewise\n' |
| 7242 |
'\n' |
| 7243 |
'All identifiers are converted into the normal form NFKC while ' |
| 7244 |
'parsing;\n' |
| 7245 |
'comparison of identifiers is based on NFKC.\n' |
| 7246 |
'\n' |
| 7247 |
'A non-normative HTML file listing all valid identifier ' |
| 7248 |
'characters for\n' |
| 7249 |
'Unicode 14.0.0 can be found at\n' |
| 7250 |
'https://www.unicode.org/Public/14.0.0/ucd/DerivedCoreProperties.txt\n' |
| 7251 |
'\n' |
| 7252 |
'\n' |
| 7253 |
'Keywords\n' |
| 7254 |
'========\n' |
| 7255 |
'\n' |
| 7256 |
'The following identifiers are used as reserved words, or ' |
| 7257 |
'*keywords* of\n' |
| 7258 |
'the language, and cannot be used as ordinary identifiers. ' |
| 7259 |
'They must\n' |
| 7260 |
'be spelled exactly as written here:\n' |
| 7261 |
'\n' |
| 7262 |
' False await else import pass\n' |
| 7263 |
' None break except in raise\n' |
| 7264 |
' True class finally is return\n' |
| 7265 |
' and continue for lambda try\n' |
| 7266 |
' as def from nonlocal while\n' |
| 7267 |
' assert del global not with\n' |
| 7268 |
' async elif if or yield\n' |
| 7269 |
'\n' |
| 7270 |
'\n' |
| 7271 |
'Soft Keywords\n' |
| 7272 |
'=============\n' |
| 7273 |
'\n' |
| 7274 |
'New in version 3.10.\n' |
| 7275 |
'\n' |
| 7276 |
'Some identifiers are only reserved under specific contexts. ' |
| 7277 |
'These are\n' |
| 7278 |
'known as *soft keywords*. The identifiers "match", "case" ' |
| 7279 |
'and "_" can\n' |
| 7280 |
'syntactically act as keywords in contexts related to the ' |
| 7281 |
'pattern\n' |
| 7282 |
'matching statement, but this distinction is done at the ' |
| 7283 |
'parser level,\n' |
| 7284 |
'not when tokenizing.\n' |
| 7285 |
'\n' |
| 7286 |
'As soft keywords, their use with pattern matching is possible ' |
| 7287 |
'while\n' |
| 7288 |
'still preserving compatibility with existing code that uses ' |
| 7289 |
'"match",\n' |
| 7290 |
'"case" and "_" as identifier names.\n' |
| 7291 |
'\n' |
| 7292 |
'\n' |
| 7293 |
'Reserved classes of identifiers\n' |
| 7294 |
'===============================\n' |
| 7295 |
'\n' |
| 7296 |
'Certain classes of identifiers (besides keywords) have ' |
| 7297 |
'special\n' |
| 7298 |
'meanings. These classes are identified by the patterns of ' |
| 7299 |
'leading and\n' |
| 7300 |
'trailing underscore characters:\n' |
| 7301 |
'\n' |
| 7302 |
'"_*"\n' |
| 7303 |
' Not imported by "from module import *".\n' |
| 7304 |
'\n' |
| 7305 |
'"_"\n' |
| 7306 |
' In a "case" pattern within a "match" statement, "_" is a ' |
| 7307 |
'soft\n' |
| 7308 |
' keyword that denotes a wildcard.\n' |
| 7309 |
'\n' |
| 7310 |
' Separately, the interactive interpreter makes the result ' |
| 7311 |
'of the\n' |
| 7312 |
' last evaluation available in the variable "_". (It is ' |
| 7313 |
'stored in the\n' |
| 7314 |
' "builtins" module, alongside built-in functions like ' |
| 7315 |
'"print".)\n' |
| 7316 |
'\n' |
| 7317 |
' Elsewhere, "_" is a regular identifier. It is often used ' |
| 7318 |
'to name\n' |
| 7319 |
' “special” items, but it is not special to Python itself.\n' |
| 7320 |
'\n' |
| 7321 |
' Note:\n' |
| 7322 |
'\n' |
| 7323 |
' The name "_" is often used in conjunction with\n' |
| 7324 |
' internationalization; refer to the documentation for ' |
| 7325 |
'the\n' |
| 7326 |
' "gettext" module for more information on this ' |
| 7327 |
'convention.It is\n' |
| 7328 |
' also commonly used for unused variables.\n' |
| 7329 |
'\n' |
| 7330 |
'"__*__"\n' |
| 7331 |
' System-defined names, informally known as “dunder” names. ' |
| 7332 |
'These\n' |
| 7333 |
' names are defined by the interpreter and its ' |
| 7334 |
'implementation\n' |
| 7335 |
' (including the standard library). Current system names ' |
| 7336 |
'are\n' |
| 7337 |
' discussed in the Special method names section and ' |
| 7338 |
'elsewhere. More\n' |
| 7339 |
' will likely be defined in future versions of Python. ' |
| 7340 |
'*Any* use of\n' |
| 7341 |
' "__*__" names, in any context, that does not follow ' |
| 7342 |
'explicitly\n' |
| 7343 |
' documented use, is subject to breakage without warning.\n' |
| 7344 |
'\n' |
| 7345 |
'"__*"\n' |
| 7346 |
' Class-private names. Names in this category, when used ' |
| 7347 |
'within the\n' |
| 7348 |
' context of a class definition, are re-written to use a ' |
| 7349 |
'mangled form\n' |
| 7350 |
' to help avoid name clashes between “private” attributes of ' |
| 7351 |
'base and\n' |
| 7352 |
' derived classes. See section Identifiers (Names).\n', |
| 7353 |
'if': 'The "if" statement\n' |
| 7354 |
'******************\n' |
| 7355 |
'\n' |
| 7356 |
'The "if" statement is used for conditional execution:\n' |
| 7357 |
'\n' |
| 7358 |
' if_stmt ::= "if" assignment_expression ":" suite\n' |
| 7359 |
' ("elif" assignment_expression ":" suite)*\n' |
| 7360 |
' ["else" ":" suite]\n' |
| 7361 |
'\n' |
| 7362 |
'It selects exactly one of the suites by evaluating the expressions ' |
| 7363 |
'one\n' |
| 7364 |
'by one until one is found to be true (see section Boolean operations\n' |
| 7365 |
'for the definition of true and false); then that suite is executed\n' |
| 7366 |
'(and no other part of the "if" statement is executed or evaluated).\n' |
| 7367 |
'If all expressions are false, the suite of the "else" clause, if\n' |
| 7368 |
'present, is executed.\n', |
| 7369 |
'imaginary': 'Imaginary literals\n' |
| 7370 |
'******************\n' |
| 7371 |
'\n' |
| 7372 |
'Imaginary literals are described by the following lexical ' |
| 7373 |
'definitions:\n' |
| 7374 |
'\n' |
| 7375 |
' imagnumber ::= (floatnumber | digitpart) ("j" | "J")\n' |
| 7376 |
'\n' |
| 7377 |
'An imaginary literal yields a complex number with a real part ' |
| 7378 |
'of 0.0.\n' |
| 7379 |
'Complex numbers are represented as a pair of floating point ' |
| 7380 |
'numbers\n' |
| 7381 |
'and have the same restrictions on their range. To create a ' |
| 7382 |
'complex\n' |
| 7383 |
'number with a nonzero real part, add a floating point number to ' |
| 7384 |
'it,\n' |
| 7385 |
'e.g., "(3+4j)". Some examples of imaginary literals:\n' |
| 7386 |
'\n' |
| 7387 |
' 3.14j 10.j 10j .001j 1e100j 3.14e-10j ' |
| 7388 |
'3.14_15_93j\n', |
| 7389 |
'import': 'The "import" statement\n' |
| 7390 |
'**********************\n' |
| 7391 |
'\n' |
| 7392 |
' import_stmt ::= "import" module ["as" identifier] ("," ' |
| 7393 |
'module ["as" identifier])*\n' |
| 7394 |
' | "from" relative_module "import" identifier ' |
| 7395 |
'["as" identifier]\n' |
| 7396 |
' ("," identifier ["as" identifier])*\n' |
| 7397 |
' | "from" relative_module "import" "(" ' |
| 7398 |
'identifier ["as" identifier]\n' |
| 7399 |
' ("," identifier ["as" identifier])* [","] ")"\n' |
| 7400 |
' | "from" relative_module "import" "*"\n' |
| 7401 |
' module ::= (identifier ".")* identifier\n' |
| 7402 |
' relative_module ::= "."* module | "."+\n' |
| 7403 |
'\n' |
| 7404 |
'The basic import statement (no "from" clause) is executed in two\n' |
| 7405 |
'steps:\n' |
| 7406 |
'\n' |
| 7407 |
'1. find a module, loading and initializing it if necessary\n' |
| 7408 |
'\n' |
| 7409 |
'2. define a name or names in the local namespace for the scope ' |
| 7410 |
'where\n' |
| 7411 |
' the "import" statement occurs.\n' |
| 7412 |
'\n' |
| 7413 |
'When the statement contains multiple clauses (separated by commas) ' |
| 7414 |
'the\n' |
| 7415 |
'two steps are carried out separately for each clause, just as ' |
| 7416 |
'though\n' |
| 7417 |
'the clauses had been separated out into individual import ' |
| 7418 |
'statements.\n' |
| 7419 |
'\n' |
| 7420 |
'The details of the first step, finding and loading modules, are\n' |
| 7421 |
'described in greater detail in the section on the import system, ' |
| 7422 |
'which\n' |
| 7423 |
'also describes the various types of packages and modules that can ' |
| 7424 |
'be\n' |
| 7425 |
'imported, as well as all the hooks that can be used to customize ' |
| 7426 |
'the\n' |
| 7427 |
'import system. Note that failures in this step may indicate ' |
| 7428 |
'either\n' |
| 7429 |
'that the module could not be located, *or* that an error occurred\n' |
| 7430 |
'while initializing the module, which includes execution of the\n' |
| 7431 |
'module’s code.\n' |
| 7432 |
'\n' |
| 7433 |
'If the requested module is retrieved successfully, it will be ' |
| 7434 |
'made\n' |
| 7435 |
'available in the local namespace in one of three ways:\n' |
| 7436 |
'\n' |
| 7437 |
'* If the module name is followed by "as", then the name following ' |
| 7438 |
'"as"\n' |
| 7439 |
' is bound directly to the imported module.\n' |
| 7440 |
'\n' |
| 7441 |
'* If no other name is specified, and the module being imported is ' |
| 7442 |
'a\n' |
| 7443 |
' top level module, the module’s name is bound in the local ' |
| 7444 |
'namespace\n' |
| 7445 |
' as a reference to the imported module\n' |
| 7446 |
'\n' |
| 7447 |
'* If the module being imported is *not* a top level module, then ' |
| 7448 |
'the\n' |
| 7449 |
' name of the top level package that contains the module is bound ' |
| 7450 |
'in\n' |
| 7451 |
' the local namespace as a reference to the top level package. ' |
| 7452 |
'The\n' |
| 7453 |
' imported module must be accessed using its full qualified name\n' |
| 7454 |
' rather than directly\n' |
| 7455 |
'\n' |
| 7456 |
'The "from" form uses a slightly more complex process:\n' |
| 7457 |
'\n' |
| 7458 |
'1. find the module specified in the "from" clause, loading and\n' |
| 7459 |
' initializing it if necessary;\n' |
| 7460 |
'\n' |
| 7461 |
'2. for each of the identifiers specified in the "import" clauses:\n' |
| 7462 |
'\n' |
| 7463 |
' 1. check if the imported module has an attribute by that name\n' |
| 7464 |
'\n' |
| 7465 |
' 2. if not, attempt to import a submodule with that name and ' |
| 7466 |
'then\n' |
| 7467 |
' check the imported module again for that attribute\n' |
| 7468 |
'\n' |
| 7469 |
' 3. if the attribute is not found, "ImportError" is raised.\n' |
| 7470 |
'\n' |
| 7471 |
' 4. otherwise, a reference to that value is stored in the local\n' |
| 7472 |
' namespace, using the name in the "as" clause if it is ' |
| 7473 |
'present,\n' |
| 7474 |
' otherwise using the attribute name\n' |
| 7475 |
'\n' |
| 7476 |
'Examples:\n' |
| 7477 |
'\n' |
| 7478 |
' import foo # foo imported and bound locally\n' |
| 7479 |
' import foo.bar.baz # foo, foo.bar, and foo.bar.baz ' |
| 7480 |
'imported, foo bound locally\n' |
| 7481 |
' import foo.bar.baz as fbb # foo, foo.bar, and foo.bar.baz ' |
| 7482 |
'imported, foo.bar.baz bound as fbb\n' |
| 7483 |
' from foo.bar import baz # foo, foo.bar, and foo.bar.baz ' |
| 7484 |
'imported, foo.bar.baz bound as baz\n' |
| 7485 |
' from foo import attr # foo imported and foo.attr bound as ' |
| 7486 |
'attr\n' |
| 7487 |
'\n' |
| 7488 |
'If the list of identifiers is replaced by a star ("\'*\'"), all ' |
| 7489 |
'public\n' |
| 7490 |
'names defined in the module are bound in the local namespace for ' |
| 7491 |
'the\n' |
| 7492 |
'scope where the "import" statement occurs.\n' |
| 7493 |
'\n' |
| 7494 |
'The *public names* defined by a module are determined by checking ' |
| 7495 |
'the\n' |
| 7496 |
'module’s namespace for a variable named "__all__"; if defined, it ' |
| 7497 |
'must\n' |
| 7498 |
'be a sequence of strings which are names defined or imported by ' |
| 7499 |
'that\n' |
| 7500 |
'module. The names given in "__all__" are all considered public ' |
| 7501 |
'and\n' |
| 7502 |
'are required to exist. If "__all__" is not defined, the set of ' |
| 7503 |
'public\n' |
| 7504 |
'names includes all names found in the module’s namespace which do ' |
| 7505 |
'not\n' |
| 7506 |
'begin with an underscore character ("\'_\'"). "__all__" should ' |
| 7507 |
'contain\n' |
| 7508 |
'the entire public API. It is intended to avoid accidentally ' |
| 7509 |
'exporting\n' |
| 7510 |
'items that are not part of the API (such as library modules which ' |
| 7511 |
'were\n' |
| 7512 |
'imported and used within the module).\n' |
| 7513 |
'\n' |
| 7514 |
'The wild card form of import — "from module import *" — is only\n' |
| 7515 |
'allowed at the module level. Attempting to use it in class or\n' |
| 7516 |
'function definitions will raise a "SyntaxError".\n' |
| 7517 |
'\n' |
| 7518 |
'When specifying what module to import you do not have to specify ' |
| 7519 |
'the\n' |
| 7520 |
'absolute name of the module. When a module or package is ' |
| 7521 |
'contained\n' |
| 7522 |
'within another package it is possible to make a relative import ' |
| 7523 |
'within\n' |
| 7524 |
'the same top package without having to mention the package name. ' |
| 7525 |
'By\n' |
| 7526 |
'using leading dots in the specified module or package after "from" ' |
| 7527 |
'you\n' |
| 7528 |
'can specify how high to traverse up the current package hierarchy\n' |
| 7529 |
'without specifying exact names. One leading dot means the current\n' |
| 7530 |
'package where the module making the import exists. Two dots means ' |
| 7531 |
'up\n' |
| 7532 |
'one package level. Three dots is up two levels, etc. So if you ' |
| 7533 |
'execute\n' |
| 7534 |
'"from . import mod" from a module in the "pkg" package then you ' |
| 7535 |
'will\n' |
| 7536 |
'end up importing "pkg.mod". If you execute "from ..subpkg2 import ' |
| 7537 |
'mod"\n' |
| 7538 |
'from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The\n' |
| 7539 |
'specification for relative imports is contained in the Package\n' |
| 7540 |
'Relative Imports section.\n' |
| 7541 |
'\n' |
| 7542 |
'"importlib.import_module()" is provided to support applications ' |
| 7543 |
'that\n' |
| 7544 |
'determine dynamically the modules to be loaded.\n' |
| 7545 |
'\n' |
| 7546 |
'Raises an auditing event "import" with arguments "module", ' |
| 7547 |
'"filename",\n' |
| 7548 |
'"sys.path", "sys.meta_path", "sys.path_hooks".\n' |
| 7549 |
'\n' |
| 7550 |
'\n' |
| 7551 |
'Future statements\n' |
| 7552 |
'=================\n' |
| 7553 |
'\n' |
| 7554 |
'A *future statement* is a directive to the compiler that a ' |
| 7555 |
'particular\n' |
| 7556 |
'module should be compiled using syntax or semantics that will be\n' |
| 7557 |
'available in a specified future release of Python where the ' |
| 7558 |
'feature\n' |
| 7559 |
'becomes standard.\n' |
| 7560 |
'\n' |
| 7561 |
'The future statement is intended to ease migration to future ' |
| 7562 |
'versions\n' |
| 7563 |
'of Python that introduce incompatible changes to the language. ' |
| 7564 |
'It\n' |
| 7565 |
'allows use of the new features on a per-module basis before the\n' |
| 7566 |
'release in which the feature becomes standard.\n' |
| 7567 |
'\n' |
| 7568 |
' future_stmt ::= "from" "__future__" "import" feature ["as" ' |
| 7569 |
'identifier]\n' |
| 7570 |
' ("," feature ["as" identifier])*\n' |
| 7571 |
' | "from" "__future__" "import" "(" feature ' |
| 7572 |
'["as" identifier]\n' |
| 7573 |
' ("," feature ["as" identifier])* [","] ")"\n' |
| 7574 |
' feature ::= identifier\n' |
| 7575 |
'\n' |
| 7576 |
'A future statement must appear near the top of the module. The ' |
| 7577 |
'only\n' |
| 7578 |
'lines that can appear before a future statement are:\n' |
| 7579 |
'\n' |
| 7580 |
'* the module docstring (if any),\n' |
| 7581 |
'\n' |
| 7582 |
'* comments,\n' |
| 7583 |
'\n' |
| 7584 |
'* blank lines, and\n' |
| 7585 |
'\n' |
| 7586 |
'* other future statements.\n' |
| 7587 |
'\n' |
| 7588 |
'The only feature that requires using the future statement is\n' |
| 7589 |
'"annotations" (see **PEP 563**).\n' |
| 7590 |
'\n' |
| 7591 |
'All historical features enabled by the future statement are still\n' |
| 7592 |
'recognized by Python 3. The list includes "absolute_import",\n' |
| 7593 |
'"division", "generators", "generator_stop", "unicode_literals",\n' |
| 7594 |
'"print_function", "nested_scopes" and "with_statement". They are ' |
| 7595 |
'all\n' |
| 7596 |
'redundant because they are always enabled, and only kept for ' |
| 7597 |
'backwards\n' |
| 7598 |
'compatibility.\n' |
| 7599 |
'\n' |
| 7600 |
'A future statement is recognized and treated specially at compile\n' |
| 7601 |
'time: Changes to the semantics of core constructs are often\n' |
| 7602 |
'implemented by generating different code. It may even be the ' |
| 7603 |
'case\n' |
| 7604 |
'that a new feature introduces new incompatible syntax (such as a ' |
| 7605 |
'new\n' |
| 7606 |
'reserved word), in which case the compiler may need to parse the\n' |
| 7607 |
'module differently. Such decisions cannot be pushed off until\n' |
| 7608 |
'runtime.\n' |
| 7609 |
'\n' |
| 7610 |
'For any given release, the compiler knows which feature names ' |
| 7611 |
'have\n' |
| 7612 |
'been defined, and raises a compile-time error if a future ' |
| 7613 |
'statement\n' |
| 7614 |
'contains a feature not known to it.\n' |
| 7615 |
'\n' |
| 7616 |
'The direct runtime semantics are the same as for any import ' |
| 7617 |
'statement:\n' |
| 7618 |
'there is a standard module "__future__", described later, and it ' |
| 7619 |
'will\n' |
| 7620 |
'be imported in the usual way at the time the future statement is\n' |
| 7621 |
'executed.\n' |
| 7622 |
'\n' |
| 7623 |
'The interesting runtime semantics depend on the specific feature\n' |
| 7624 |
'enabled by the future statement.\n' |
| 7625 |
'\n' |
| 7626 |
'Note that there is nothing special about the statement:\n' |
| 7627 |
'\n' |
| 7628 |
' import __future__ [as name]\n' |
| 7629 |
'\n' |
| 7630 |
'That is not a future statement; it’s an ordinary import statement ' |
| 7631 |
'with\n' |
| 7632 |
'no special semantics or syntax restrictions.\n' |
| 7633 |
'\n' |
| 7634 |
'Code compiled by calls to the built-in functions "exec()" and\n' |
| 7635 |
'"compile()" that occur in a module "M" containing a future ' |
| 7636 |
'statement\n' |
| 7637 |
'will, by default, use the new syntax or semantics associated with ' |
| 7638 |
'the\n' |
| 7639 |
'future statement. This can be controlled by optional arguments ' |
| 7640 |
'to\n' |
| 7641 |
'"compile()" — see the documentation of that function for details.\n' |
| 7642 |
'\n' |
| 7643 |
'A future statement typed at an interactive interpreter prompt ' |
| 7644 |
'will\n' |
| 7645 |
'take effect for the rest of the interpreter session. If an\n' |
| 7646 |
'interpreter is started with the "-i" option, is passed a script ' |
| 7647 |
'name\n' |
| 7648 |
'to execute, and the script includes a future statement, it will be ' |
| 7649 |
'in\n' |
| 7650 |
'effect in the interactive session started after the script is\n' |
| 7651 |
'executed.\n' |
| 7652 |
'\n' |
| 7653 |
'See also:\n' |
| 7654 |
'\n' |
| 7655 |
' **PEP 236** - Back to the __future__\n' |
| 7656 |
' The original proposal for the __future__ mechanism.\n', |
| 7657 |
'in': 'Membership test operations\n' |
| 7658 |
'**************************\n' |
| 7659 |
'\n' |
| 7660 |
'The operators "in" and "not in" test for membership. "x in s"\n' |
| 7661 |
'evaluates to "True" if *x* is a member of *s*, and "False" otherwise.\n' |
| 7662 |
'"x not in s" returns the negation of "x in s". All built-in ' |
| 7663 |
'sequences\n' |
| 7664 |
'and set types support this as well as dictionary, for which "in" ' |
| 7665 |
'tests\n' |
| 7666 |
'whether the dictionary has a given key. For container types such as\n' |
| 7667 |
'list, tuple, set, frozenset, dict, or collections.deque, the\n' |
| 7668 |
'expression "x in y" is equivalent to "any(x is e or x == e for e in\n' |
| 7669 |
'y)".\n' |
| 7670 |
'\n' |
| 7671 |
'For the string and bytes types, "x in y" is "True" if and only if *x*\n' |
| 7672 |
'is a substring of *y*. An equivalent test is "y.find(x) != -1".\n' |
| 7673 |
'Empty strings are always considered to be a substring of any other\n' |
| 7674 |
'string, so """ in "abc"" will return "True".\n' |
| 7675 |
'\n' |
| 7676 |
'For user-defined classes which define the "__contains__()" method, "x\n' |
| 7677 |
'in y" returns "True" if "y.__contains__(x)" returns a true value, and\n' |
| 7678 |
'"False" otherwise.\n' |
| 7679 |
'\n' |
| 7680 |
'For user-defined classes which do not define "__contains__()" but do\n' |
| 7681 |
'define "__iter__()", "x in y" is "True" if some value "z", for which\n' |
| 7682 |
'the expression "x is z or x == z" is true, is produced while ' |
| 7683 |
'iterating\n' |
| 7684 |
'over "y". If an exception is raised during the iteration, it is as if\n' |
| 7685 |
'"in" raised that exception.\n' |
| 7686 |
'\n' |
| 7687 |
'Lastly, the old-style iteration protocol is tried: if a class defines\n' |
| 7688 |
'"__getitem__()", "x in y" is "True" if and only if there is a non-\n' |
| 7689 |
'negative integer index *i* such that "x is y[i] or x == y[i]", and no\n' |
| 7690 |
'lower integer index raises the "IndexError" exception. (If any other\n' |
| 7691 |
'exception is raised, it is as if "in" raised that exception).\n' |
| 7692 |
'\n' |
| 7693 |
'The operator "not in" is defined to have the inverse truth value of\n' |
| 7694 |
'"in".\n', |
| 7695 |
'integers': 'Integer literals\n' |
| 7696 |
'****************\n' |
| 7697 |
'\n' |
| 7698 |
'Integer literals are described by the following lexical ' |
| 7699 |
'definitions:\n' |
| 7700 |
'\n' |
| 7701 |
' integer ::= decinteger | bininteger | octinteger | ' |
| 7702 |
'hexinteger\n' |
| 7703 |
' decinteger ::= nonzerodigit (["_"] digit)* | "0"+ (["_"] ' |
| 7704 |
'"0")*\n' |
| 7705 |
' bininteger ::= "0" ("b" | "B") (["_"] bindigit)+\n' |
| 7706 |
' octinteger ::= "0" ("o" | "O") (["_"] octdigit)+\n' |
| 7707 |
' hexinteger ::= "0" ("x" | "X") (["_"] hexdigit)+\n' |
| 7708 |
' nonzerodigit ::= "1"..."9"\n' |
| 7709 |
' digit ::= "0"..."9"\n' |
| 7710 |
' bindigit ::= "0" | "1"\n' |
| 7711 |
' octdigit ::= "0"..."7"\n' |
| 7712 |
' hexdigit ::= digit | "a"..."f" | "A"..."F"\n' |
| 7713 |
'\n' |
| 7714 |
'There is no limit for the length of integer literals apart from ' |
| 7715 |
'what\n' |
| 7716 |
'can be stored in available memory.\n' |
| 7717 |
'\n' |
| 7718 |
'Underscores are ignored for determining the numeric value of ' |
| 7719 |
'the\n' |
| 7720 |
'literal. They can be used to group digits for enhanced ' |
| 7721 |
'readability.\n' |
| 7722 |
'One underscore can occur between digits, and after base ' |
| 7723 |
'specifiers\n' |
| 7724 |
'like "0x".\n' |
| 7725 |
'\n' |
| 7726 |
'Note that leading zeros in a non-zero decimal number are not ' |
| 7727 |
'allowed.\n' |
| 7728 |
'This is for disambiguation with C-style octal literals, which ' |
| 7729 |
'Python\n' |
| 7730 |
'used before version 3.0.\n' |
| 7731 |
'\n' |
| 7732 |
'Some examples of integer literals:\n' |
| 7733 |
'\n' |
| 7734 |
' 7 2147483647 0o177 0b100110111\n' |
| 7735 |
' 3 79228162514264337593543950336 0o377 0xdeadbeef\n' |
| 7736 |
' 100_000_000_000 0b_1110_0101\n' |
| 7737 |
'\n' |
| 7738 |
'Changed in version 3.6: Underscores are now allowed for ' |
| 7739 |
'grouping\n' |
| 7740 |
'purposes in literals.\n', |
| 7741 |
'lambda': 'Lambdas\n' |
| 7742 |
'*******\n' |
| 7743 |
'\n' |
| 7744 |
' lambda_expr ::= "lambda" [parameter_list] ":" expression\n' |
| 7745 |
'\n' |
| 7746 |
'Lambda expressions (sometimes called lambda forms) are used to ' |
| 7747 |
'create\n' |
| 7748 |
'anonymous functions. The expression "lambda parameters: ' |
| 7749 |
'expression"\n' |
| 7750 |
'yields a function object. The unnamed object behaves like a ' |
| 7751 |
'function\n' |
| 7752 |
'object defined with:\n' |
| 7753 |
'\n' |
| 7754 |
' def <lambda>(parameters):\n' |
| 7755 |
' return expression\n' |
| 7756 |
'\n' |
| 7757 |
'See section Function definitions for the syntax of parameter ' |
| 7758 |
'lists.\n' |
| 7759 |
'Note that functions created with lambda expressions cannot ' |
| 7760 |
'contain\n' |
| 7761 |
'statements or annotations.\n', |
| 7762 |
'lists': 'List displays\n' |
| 7763 |
'*************\n' |
| 7764 |
'\n' |
| 7765 |
'A list display is a possibly empty series of expressions enclosed ' |
| 7766 |
'in\n' |
| 7767 |
'square brackets:\n' |
| 7768 |
'\n' |
| 7769 |
' list_display ::= "[" [starred_list | comprehension] "]"\n' |
| 7770 |
'\n' |
| 7771 |
'A list display yields a new list object, the contents being ' |
| 7772 |
'specified\n' |
| 7773 |
'by either a list of expressions or a comprehension. When a comma-\n' |
| 7774 |
'separated list of expressions is supplied, its elements are ' |
| 7775 |
'evaluated\n' |
| 7776 |
'from left to right and placed into the list object in that order.\n' |
| 7777 |
'When a comprehension is supplied, the list is constructed from the\n' |
| 7778 |
'elements resulting from the comprehension.\n', |
| 7779 |
'naming': 'Naming and binding\n' |
| 7780 |
'******************\n' |
| 7781 |
'\n' |
| 7782 |
'\n' |
| 7783 |
'Binding of names\n' |
| 7784 |
'================\n' |
| 7785 |
'\n' |
| 7786 |
'*Names* refer to objects. Names are introduced by name binding\n' |
| 7787 |
'operations.\n' |
| 7788 |
'\n' |
| 7789 |
'The following constructs bind names:\n' |
| 7790 |
'\n' |
| 7791 |
'* formal parameters to functions,\n' |
| 7792 |
'\n' |
| 7793 |
'* class definitions,\n' |
| 7794 |
'\n' |
| 7795 |
'* function definitions,\n' |
| 7796 |
'\n' |
| 7797 |
'* assignment expressions,\n' |
| 7798 |
'\n' |
| 7799 |
'* targets that are identifiers if occurring in an assignment:\n' |
| 7800 |
'\n' |
| 7801 |
' * "for" loop header,\n' |
| 7802 |
'\n' |
| 7803 |
' * after "as" in a "with" statement, "except" clause, "except*"\n' |
| 7804 |
' clause, or in the as-pattern in structural pattern matching,\n' |
| 7805 |
'\n' |
| 7806 |
' * in a capture pattern in structural pattern matching\n' |
| 7807 |
'\n' |
| 7808 |
'* "import" statements.\n' |
| 7809 |
'\n' |
| 7810 |
'The "import" statement of the form "from ... import *" binds all ' |
| 7811 |
'names\n' |
| 7812 |
'defined in the imported module, except those beginning with an\n' |
| 7813 |
'underscore. This form may only be used at the module level.\n' |
| 7814 |
'\n' |
| 7815 |
'A target occurring in a "del" statement is also considered bound ' |
| 7816 |
'for\n' |
| 7817 |
'this purpose (though the actual semantics are to unbind the ' |
| 7818 |
'name).\n' |
| 7819 |
'\n' |
| 7820 |
'Each assignment or import statement occurs within a block defined ' |
| 7821 |
'by a\n' |
| 7822 |
'class or function definition or at the module level (the ' |
| 7823 |
'top-level\n' |
| 7824 |
'code block).\n' |
| 7825 |
'\n' |
| 7826 |
'If a name is bound in a block, it is a local variable of that ' |
| 7827 |
'block,\n' |
| 7828 |
'unless declared as "nonlocal" or "global". If a name is bound at ' |
| 7829 |
'the\n' |
| 7830 |
'module level, it is a global variable. (The variables of the ' |
| 7831 |
'module\n' |
| 7832 |
'code block are local and global.) If a variable is used in a ' |
| 7833 |
'code\n' |
| 7834 |
'block but not defined there, it is a *free variable*.\n' |
| 7835 |
'\n' |
| 7836 |
'Each occurrence of a name in the program text refers to the ' |
| 7837 |
'*binding*\n' |
| 7838 |
'of that name established by the following name resolution rules.\n' |
| 7839 |
'\n' |
| 7840 |
'\n' |
| 7841 |
'Resolution of names\n' |
| 7842 |
'===================\n' |
| 7843 |
'\n' |
| 7844 |
'A *scope* defines the visibility of a name within a block. If a ' |
| 7845 |
'local\n' |
| 7846 |
'variable is defined in a block, its scope includes that block. If ' |
| 7847 |
'the\n' |
| 7848 |
'definition occurs in a function block, the scope extends to any ' |
| 7849 |
'blocks\n' |
| 7850 |
'contained within the defining one, unless a contained block ' |
| 7851 |
'introduces\n' |
| 7852 |
'a different binding for the name.\n' |
| 7853 |
'\n' |
| 7854 |
'When a name is used in a code block, it is resolved using the ' |
| 7855 |
'nearest\n' |
| 7856 |
'enclosing scope. The set of all such scopes visible to a code ' |
| 7857 |
'block\n' |
| 7858 |
'is called the block’s *environment*.\n' |
| 7859 |
'\n' |
| 7860 |
'When a name is not found at all, a "NameError" exception is ' |
| 7861 |
'raised. If\n' |
| 7862 |
'the current scope is a function scope, and the name refers to a ' |
| 7863 |
'local\n' |
| 7864 |
'variable that has not yet been bound to a value at the point where ' |
| 7865 |
'the\n' |
| 7866 |
'name is used, an "UnboundLocalError" exception is raised.\n' |
| 7867 |
'"UnboundLocalError" is a subclass of "NameError".\n' |
| 7868 |
'\n' |
| 7869 |
'If a name binding operation occurs anywhere within a code block, ' |
| 7870 |
'all\n' |
| 7871 |
'uses of the name within the block are treated as references to ' |
| 7872 |
'the\n' |
| 7873 |
'current block. This can lead to errors when a name is used within ' |
| 7874 |
'a\n' |
| 7875 |
'block before it is bound. This rule is subtle. Python lacks\n' |
| 7876 |
'declarations and allows name binding operations to occur anywhere\n' |
| 7877 |
'within a code block. The local variables of a code block can be\n' |
| 7878 |
'determined by scanning the entire text of the block for name ' |
| 7879 |
'binding\n' |
| 7880 |
'operations. See the FAQ entry on UnboundLocalError for examples.\n' |
| 7881 |
'\n' |
| 7882 |
'If the "global" statement occurs within a block, all uses of the ' |
| 7883 |
'names\n' |
| 7884 |
'specified in the statement refer to the bindings of those names in ' |
| 7885 |
'the\n' |
| 7886 |
'top-level namespace. Names are resolved in the top-level ' |
| 7887 |
'namespace by\n' |
| 7888 |
'searching the global namespace, i.e. the namespace of the module\n' |
| 7889 |
'containing the code block, and the builtins namespace, the ' |
| 7890 |
'namespace\n' |
| 7891 |
'of the module "builtins". The global namespace is searched ' |
| 7892 |
'first. If\n' |
| 7893 |
'the names are not found there, the builtins namespace is ' |
| 7894 |
'searched.\n' |
| 7895 |
'The "global" statement must precede all uses of the listed names.\n' |
| 7896 |
'\n' |
| 7897 |
'The "global" statement has the same scope as a name binding ' |
| 7898 |
'operation\n' |
| 7899 |
'in the same block. If the nearest enclosing scope for a free ' |
| 7900 |
'variable\n' |
| 7901 |
'contains a global statement, the free variable is treated as a ' |
| 7902 |
'global.\n' |
| 7903 |
'\n' |
| 7904 |
'The "nonlocal" statement causes corresponding names to refer to\n' |
| 7905 |
'previously bound variables in the nearest enclosing function ' |
| 7906 |
'scope.\n' |
| 7907 |
'"SyntaxError" is raised at compile time if the given name does ' |
| 7908 |
'not\n' |
| 7909 |
'exist in any enclosing function scope.\n' |
| 7910 |
'\n' |
| 7911 |
'The namespace for a module is automatically created the first time ' |
| 7912 |
'a\n' |
| 7913 |
'module is imported. The main module for a script is always ' |
| 7914 |
'called\n' |
| 7915 |
'"__main__".\n' |
| 7916 |
'\n' |
| 7917 |
'Class definition blocks and arguments to "exec()" and "eval()" ' |
| 7918 |
'are\n' |
| 7919 |
'special in the context of name resolution. A class definition is ' |
| 7920 |
'an\n' |
| 7921 |
'executable statement that may use and define names. These ' |
| 7922 |
'references\n' |
| 7923 |
'follow the normal rules for name resolution with an exception ' |
| 7924 |
'that\n' |
| 7925 |
'unbound local variables are looked up in the global namespace. ' |
| 7926 |
'The\n' |
| 7927 |
'namespace of the class definition becomes the attribute dictionary ' |
| 7928 |
'of\n' |
| 7929 |
'the class. The scope of names defined in a class block is limited ' |
| 7930 |
'to\n' |
| 7931 |
'the class block; it does not extend to the code blocks of methods ' |
| 7932 |
'–\n' |
| 7933 |
'this includes comprehensions and generator expressions since they ' |
| 7934 |
'are\n' |
| 7935 |
'implemented using a function scope. This means that the ' |
| 7936 |
'following\n' |
| 7937 |
'will fail:\n' |
| 7938 |
'\n' |
| 7939 |
' class A:\n' |
| 7940 |
' a = 42\n' |
| 7941 |
' b = list(a + i for i in range(10))\n' |
| 7942 |
'\n' |
| 7943 |
'\n' |
| 7944 |
'Builtins and restricted execution\n' |
| 7945 |
'=================================\n' |
| 7946 |
'\n' |
| 7947 |
'**CPython implementation detail:** Users should not touch\n' |
| 7948 |
'"__builtins__"; it is strictly an implementation detail. Users\n' |
| 7949 |
'wanting to override values in the builtins namespace should ' |
| 7950 |
'"import"\n' |
| 7951 |
'the "builtins" module and modify its attributes appropriately.\n' |
| 7952 |
'\n' |
| 7953 |
'The builtins namespace associated with the execution of a code ' |
| 7954 |
'block\n' |
| 7955 |
'is actually found by looking up the name "__builtins__" in its ' |
| 7956 |
'global\n' |
| 7957 |
'namespace; this should be a dictionary or a module (in the latter ' |
| 7958 |
'case\n' |
| 7959 |
'the module’s dictionary is used). By default, when in the ' |
| 7960 |
'"__main__"\n' |
| 7961 |
'module, "__builtins__" is the built-in module "builtins"; when in ' |
| 7962 |
'any\n' |
| 7963 |
'other module, "__builtins__" is an alias for the dictionary of ' |
| 7964 |
'the\n' |
| 7965 |
'"builtins" module itself.\n' |
| 7966 |
'\n' |
| 7967 |
'\n' |
| 7968 |
'Interaction with dynamic features\n' |
| 7969 |
'=================================\n' |
| 7970 |
'\n' |
| 7971 |
'Name resolution of free variables occurs at runtime, not at ' |
| 7972 |
'compile\n' |
| 7973 |
'time. This means that the following code will print 42:\n' |
| 7974 |
'\n' |
| 7975 |
' i = 10\n' |
| 7976 |
' def f():\n' |
| 7977 |
' print(i)\n' |
| 7978 |
' i = 42\n' |
| 7979 |
' f()\n' |
| 7980 |
'\n' |
| 7981 |
'The "eval()" and "exec()" functions do not have access to the ' |
| 7982 |
'full\n' |
| 7983 |
'environment for resolving names. Names may be resolved in the ' |
| 7984 |
'local\n' |
| 7985 |
'and global namespaces of the caller. Free variables are not ' |
| 7986 |
'resolved\n' |
| 7987 |
'in the nearest enclosing namespace, but in the global namespace. ' |
| 7988 |
'[1]\n' |
| 7989 |
'The "exec()" and "eval()" functions have optional arguments to\n' |
| 7990 |
'override the global and local namespace. If only one namespace ' |
| 7991 |
'is\n' |
| 7992 |
'specified, it is used for both.\n', |
| 7993 |
'nonlocal': 'The "nonlocal" statement\n' |
| 7994 |
'************************\n' |
| 7995 |
'\n' |
| 7996 |
' nonlocal_stmt ::= "nonlocal" identifier ("," identifier)*\n' |
| 7997 |
'\n' |
| 7998 |
'The "nonlocal" statement causes the listed identifiers to refer ' |
| 7999 |
'to\n' |
| 8000 |
'previously bound variables in the nearest enclosing scope ' |
| 8001 |
'excluding\n' |
| 8002 |
'globals. This is important because the default behavior for ' |
| 8003 |
'binding is\n' |
| 8004 |
'to search the local namespace first. The statement allows\n' |
| 8005 |
'encapsulated code to rebind variables outside of the local ' |
| 8006 |
'scope\n' |
| 8007 |
'besides the global (module) scope.\n' |
| 8008 |
'\n' |
| 8009 |
'Names listed in a "nonlocal" statement, unlike those listed in ' |
| 8010 |
'a\n' |
| 8011 |
'"global" statement, must refer to pre-existing bindings in an\n' |
| 8012 |
'enclosing scope (the scope in which a new binding should be ' |
| 8013 |
'created\n' |
| 8014 |
'cannot be determined unambiguously).\n' |
| 8015 |
'\n' |
| 8016 |
'Names listed in a "nonlocal" statement must not collide with ' |
| 8017 |
'pre-\n' |
| 8018 |
'existing bindings in the local scope.\n' |
| 8019 |
'\n' |
| 8020 |
'See also:\n' |
| 8021 |
'\n' |
| 8022 |
' **PEP 3104** - Access to Names in Outer Scopes\n' |
| 8023 |
' The specification for the "nonlocal" statement.\n', |
| 8024 |
'numbers': 'Numeric literals\n' |
| 8025 |
'****************\n' |
| 8026 |
'\n' |
| 8027 |
'There are three types of numeric literals: integers, floating ' |
| 8028 |
'point\n' |
| 8029 |
'numbers, and imaginary numbers. There are no complex literals\n' |
| 8030 |
'(complex numbers can be formed by adding a real number and an\n' |
| 8031 |
'imaginary number).\n' |
| 8032 |
'\n' |
| 8033 |
'Note that numeric literals do not include a sign; a phrase like ' |
| 8034 |
'"-1"\n' |
| 8035 |
'is actually an expression composed of the unary operator ‘"-"’ ' |
| 8036 |
'and the\n' |
| 8037 |
'literal "1".\n', |
| 8038 |
'numeric-types': 'Emulating numeric types\n' |
| 8039 |
'***********************\n' |
| 8040 |
'\n' |
| 8041 |
'The following methods can be defined to emulate numeric ' |
| 8042 |
'objects.\n' |
| 8043 |
'Methods corresponding to operations that are not supported ' |
| 8044 |
'by the\n' |
| 8045 |
'particular kind of number implemented (e.g., bitwise ' |
| 8046 |
'operations for\n' |
| 8047 |
'non-integral numbers) should be left undefined.\n' |
| 8048 |
'\n' |
| 8049 |
'object.__add__(self, other)\n' |
| 8050 |
'object.__sub__(self, other)\n' |
| 8051 |
'object.__mul__(self, other)\n' |
| 8052 |
'object.__matmul__(self, other)\n' |
| 8053 |
'object.__truediv__(self, other)\n' |
| 8054 |
'object.__floordiv__(self, other)\n' |
| 8055 |
'object.__mod__(self, other)\n' |
| 8056 |
'object.__divmod__(self, other)\n' |
| 8057 |
'object.__pow__(self, other[, modulo])\n' |
| 8058 |
'object.__lshift__(self, other)\n' |
| 8059 |
'object.__rshift__(self, other)\n' |
| 8060 |
'object.__and__(self, other)\n' |
| 8061 |
'object.__xor__(self, other)\n' |
| 8062 |
'object.__or__(self, other)\n' |
| 8063 |
'\n' |
| 8064 |
' These methods are called to implement the binary ' |
| 8065 |
'arithmetic\n' |
| 8066 |
' operations ("+", "-", "*", "@", "/", "//", "%", ' |
| 8067 |
'"divmod()",\n' |
| 8068 |
' "pow()", "**", "<<", ">>", "&", "^", "|"). For ' |
| 8069 |
'instance, to\n' |
| 8070 |
' evaluate the expression "x + y", where *x* is an ' |
| 8071 |
'instance of a\n' |
| 8072 |
' class that has an "__add__()" method, ' |
| 8073 |
'"type(x).__add__(x, y)" is\n' |
| 8074 |
' called. The "__divmod__()" method should be the ' |
| 8075 |
'equivalent to\n' |
| 8076 |
' using "__floordiv__()" and "__mod__()"; it should not be ' |
| 8077 |
'related to\n' |
| 8078 |
' "__truediv__()". Note that "__pow__()" should be ' |
| 8079 |
'defined to accept\n' |
| 8080 |
' an optional third argument if the ternary version of the ' |
| 8081 |
'built-in\n' |
| 8082 |
' "pow()" function is to be supported.\n' |
| 8083 |
'\n' |
| 8084 |
' If one of those methods does not support the operation ' |
| 8085 |
'with the\n' |
| 8086 |
' supplied arguments, it should return "NotImplemented".\n' |
| 8087 |
'\n' |
| 8088 |
'object.__radd__(self, other)\n' |
| 8089 |
'object.__rsub__(self, other)\n' |
| 8090 |
'object.__rmul__(self, other)\n' |
| 8091 |
'object.__rmatmul__(self, other)\n' |
| 8092 |
'object.__rtruediv__(self, other)\n' |
| 8093 |
'object.__rfloordiv__(self, other)\n' |
| 8094 |
'object.__rmod__(self, other)\n' |
| 8095 |
'object.__rdivmod__(self, other)\n' |
| 8096 |
'object.__rpow__(self, other[, modulo])\n' |
| 8097 |
'object.__rlshift__(self, other)\n' |
| 8098 |
'object.__rrshift__(self, other)\n' |
| 8099 |
'object.__rand__(self, other)\n' |
| 8100 |
'object.__rxor__(self, other)\n' |
| 8101 |
'object.__ror__(self, other)\n' |
| 8102 |
'\n' |
| 8103 |
' These methods are called to implement the binary ' |
| 8104 |
'arithmetic\n' |
| 8105 |
' operations ("+", "-", "*", "@", "/", "//", "%", ' |
| 8106 |
'"divmod()",\n' |
| 8107 |
' "pow()", "**", "<<", ">>", "&", "^", "|") with reflected ' |
| 8108 |
'(swapped)\n' |
| 8109 |
' operands. These functions are only called if the left ' |
| 8110 |
'operand does\n' |
| 8111 |
' not support the corresponding operation [3] and the ' |
| 8112 |
'operands are of\n' |
| 8113 |
' different types. [4] For instance, to evaluate the ' |
| 8114 |
'expression "x -\n' |
| 8115 |
' y", where *y* is an instance of a class that has an ' |
| 8116 |
'"__rsub__()"\n' |
| 8117 |
' method, "type(y).__rsub__(y, x)" is called if ' |
| 8118 |
'"type(x).__sub__(x,\n' |
| 8119 |
' y)" returns "NotImplemented".\n' |
| 8120 |
'\n' |
| 8121 |
' Note that ternary "pow()" will not try calling ' |
| 8122 |
'"__rpow__()" (the\n' |
| 8123 |
' coercion rules would become too complicated).\n' |
| 8124 |
'\n' |
| 8125 |
' Note:\n' |
| 8126 |
'\n' |
| 8127 |
' If the right operand’s type is a subclass of the left ' |
| 8128 |
'operand’s\n' |
| 8129 |
' type and that subclass provides a different ' |
| 8130 |
'implementation of the\n' |
| 8131 |
' reflected method for the operation, this method will ' |
| 8132 |
'be called\n' |
| 8133 |
' before the left operand’s non-reflected method. This ' |
| 8134 |
'behavior\n' |
| 8135 |
' allows subclasses to override their ancestors’ ' |
| 8136 |
'operations.\n' |
| 8137 |
'\n' |
| 8138 |
'object.__iadd__(self, other)\n' |
| 8139 |
'object.__isub__(self, other)\n' |
| 8140 |
'object.__imul__(self, other)\n' |
| 8141 |
'object.__imatmul__(self, other)\n' |
| 8142 |
'object.__itruediv__(self, other)\n' |
| 8143 |
'object.__ifloordiv__(self, other)\n' |
| 8144 |
'object.__imod__(self, other)\n' |
| 8145 |
'object.__ipow__(self, other[, modulo])\n' |
| 8146 |
'object.__ilshift__(self, other)\n' |
| 8147 |
'object.__irshift__(self, other)\n' |
| 8148 |
'object.__iand__(self, other)\n' |
| 8149 |
'object.__ixor__(self, other)\n' |
| 8150 |
'object.__ior__(self, other)\n' |
| 8151 |
'\n' |
| 8152 |
' These methods are called to implement the augmented ' |
| 8153 |
'arithmetic\n' |
| 8154 |
' assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", ' |
| 8155 |
'"**=",\n' |
| 8156 |
' "<<=", ">>=", "&=", "^=", "|="). These methods should ' |
| 8157 |
'attempt to\n' |
| 8158 |
' do the operation in-place (modifying *self*) and return ' |
| 8159 |
'the result\n' |
| 8160 |
' (which could be, but does not have to be, *self*). If a ' |
| 8161 |
'specific\n' |
| 8162 |
' method is not defined, or if that method returns ' |
| 8163 |
'"NotImplemented",\n' |
| 8164 |
' the augmented assignment falls back to the normal ' |
| 8165 |
'methods. For\n' |
| 8166 |
' instance, if *x* is an instance of a class with an ' |
| 8167 |
'"__iadd__()"\n' |
| 8168 |
' method, "x += y" is equivalent to "x = x.__iadd__(y)" . ' |
| 8169 |
'If\n' |
| 8170 |
' "__iadd__()" does not exist, or if "x.__iadd__(y)" ' |
| 8171 |
'returns\n' |
| 8172 |
' "NotImplemented", "x.__add__(y)" and "y.__radd__(x)" ' |
| 8173 |
'are\n' |
| 8174 |
' considered, as with the evaluation of "x + y". In ' |
| 8175 |
'certain\n' |
| 8176 |
' situations, augmented assignment can result in ' |
| 8177 |
'unexpected errors\n' |
| 8178 |
' (see Why does a_tuple[i] += [‘item’] raise an exception ' |
| 8179 |
'when the\n' |
| 8180 |
' addition works?), but this behavior is in fact part of ' |
| 8181 |
'the data\n' |
| 8182 |
' model.\n' |
| 8183 |
'\n' |
| 8184 |
'object.__neg__(self)\n' |
| 8185 |
'object.__pos__(self)\n' |
| 8186 |
'object.__abs__(self)\n' |
| 8187 |
'object.__invert__(self)\n' |
| 8188 |
'\n' |
| 8189 |
' Called to implement the unary arithmetic operations ' |
| 8190 |
'("-", "+",\n' |
| 8191 |
' "abs()" and "~").\n' |
| 8192 |
'\n' |
| 8193 |
'object.__complex__(self)\n' |
| 8194 |
'object.__int__(self)\n' |
| 8195 |
'object.__float__(self)\n' |
| 8196 |
'\n' |
| 8197 |
' Called to implement the built-in functions "complex()", ' |
| 8198 |
'"int()" and\n' |
| 8199 |
' "float()". Should return a value of the appropriate ' |
| 8200 |
'type.\n' |
| 8201 |
'\n' |
| 8202 |
'object.__index__(self)\n' |
| 8203 |
'\n' |
| 8204 |
' Called to implement "operator.index()", and whenever ' |
| 8205 |
'Python needs\n' |
| 8206 |
' to losslessly convert the numeric object to an integer ' |
| 8207 |
'object (such\n' |
| 8208 |
' as in slicing, or in the built-in "bin()", "hex()" and ' |
| 8209 |
'"oct()"\n' |
| 8210 |
' functions). Presence of this method indicates that the ' |
| 8211 |
'numeric\n' |
| 8212 |
' object is an integer type. Must return an integer.\n' |
| 8213 |
'\n' |
| 8214 |
' If "__int__()", "__float__()" and "__complex__()" are ' |
| 8215 |
'not defined\n' |
| 8216 |
' then corresponding built-in functions "int()", "float()" ' |
| 8217 |
'and\n' |
| 8218 |
' "complex()" fall back to "__index__()".\n' |
| 8219 |
'\n' |
| 8220 |
'object.__round__(self[, ndigits])\n' |
| 8221 |
'object.__trunc__(self)\n' |
| 8222 |
'object.__floor__(self)\n' |
| 8223 |
'object.__ceil__(self)\n' |
| 8224 |
'\n' |
| 8225 |
' Called to implement the built-in function "round()" and ' |
| 8226 |
'"math"\n' |
| 8227 |
' functions "trunc()", "floor()" and "ceil()". Unless ' |
| 8228 |
'*ndigits* is\n' |
| 8229 |
' passed to "__round__()" all these methods should return ' |
| 8230 |
'the value\n' |
| 8231 |
' of the object truncated to an "Integral" (typically an ' |
| 8232 |
'"int").\n' |
| 8233 |
'\n' |
| 8234 |
' The built-in function "int()" falls back to ' |
| 8235 |
'"__trunc__()" if\n' |
| 8236 |
' neither "__int__()" nor "__index__()" is defined.\n' |
| 8237 |
'\n' |
| 8238 |
' Changed in version 3.11: The delegation of "int()" to ' |
| 8239 |
'"__trunc__()"\n' |
| 8240 |
' is deprecated.\n', |
| 8241 |
'objects': 'Objects, values and types\n' |
| 8242 |
'*************************\n' |
| 8243 |
'\n' |
| 8244 |
'*Objects* are Python’s abstraction for data. All data in a ' |
| 8245 |
'Python\n' |
| 8246 |
'program is represented by objects or by relations between ' |
| 8247 |
'objects. (In\n' |
| 8248 |
'a sense, and in conformance to Von Neumann’s model of a “stored\n' |
| 8249 |
'program computer”, code is also represented by objects.)\n' |
| 8250 |
'\n' |
| 8251 |
'Every object has an identity, a type and a value. An object’s\n' |
| 8252 |
'*identity* never changes once it has been created; you may think ' |
| 8253 |
'of it\n' |
| 8254 |
'as the object’s address in memory. The "is" operator compares ' |
| 8255 |
'the\n' |
| 8256 |
'identity of two objects; the "id()" function returns an integer\n' |
| 8257 |
'representing its identity.\n' |
| 8258 |
'\n' |
| 8259 |
'**CPython implementation detail:** For CPython, "id(x)" is the ' |
| 8260 |
'memory\n' |
| 8261 |
'address where "x" is stored.\n' |
| 8262 |
'\n' |
| 8263 |
'An object’s type determines the operations that the object ' |
| 8264 |
'supports\n' |
| 8265 |
'(e.g., “does it have a length?”) and also defines the possible ' |
| 8266 |
'values\n' |
| 8267 |
'for objects of that type. The "type()" function returns an ' |
| 8268 |
'object’s\n' |
| 8269 |
'type (which is an object itself). Like its identity, an ' |
| 8270 |
'object’s\n' |
| 8271 |
'*type* is also unchangeable. [1]\n' |
| 8272 |
'\n' |
| 8273 |
'The *value* of some objects can change. Objects whose value can\n' |
| 8274 |
'change are said to be *mutable*; objects whose value is ' |
| 8275 |
'unchangeable\n' |
| 8276 |
'once they are created are called *immutable*. (The value of an\n' |
| 8277 |
'immutable container object that contains a reference to a ' |
| 8278 |
'mutable\n' |
| 8279 |
'object can change when the latter’s value is changed; however ' |
| 8280 |
'the\n' |
| 8281 |
'container is still considered immutable, because the collection ' |
| 8282 |
'of\n' |
| 8283 |
'objects it contains cannot be changed. So, immutability is not\n' |
| 8284 |
'strictly the same as having an unchangeable value, it is more ' |
| 8285 |
'subtle.)\n' |
| 8286 |
'An object’s mutability is determined by its type; for instance,\n' |
| 8287 |
'numbers, strings and tuples are immutable, while dictionaries ' |
| 8288 |
'and\n' |
| 8289 |
'lists are mutable.\n' |
| 8290 |
'\n' |
| 8291 |
'Objects are never explicitly destroyed; however, when they ' |
| 8292 |
'become\n' |
| 8293 |
'unreachable they may be garbage-collected. An implementation is\n' |
| 8294 |
'allowed to postpone garbage collection or omit it altogether — it ' |
| 8295 |
'is a\n' |
| 8296 |
'matter of implementation quality how garbage collection is\n' |
| 8297 |
'implemented, as long as no objects are collected that are still\n' |
| 8298 |
'reachable.\n' |
| 8299 |
'\n' |
| 8300 |
'**CPython implementation detail:** CPython currently uses a ' |
| 8301 |
'reference-\n' |
| 8302 |
'counting scheme with (optional) delayed detection of cyclically ' |
| 8303 |
'linked\n' |
| 8304 |
'garbage, which collects most objects as soon as they become\n' |
| 8305 |
'unreachable, but is not guaranteed to collect garbage containing\n' |
| 8306 |
'circular references. See the documentation of the "gc" module ' |
| 8307 |
'for\n' |
| 8308 |
'information on controlling the collection of cyclic garbage. ' |
| 8309 |
'Other\n' |
| 8310 |
'implementations act differently and CPython may change. Do not ' |
| 8311 |
'depend\n' |
| 8312 |
'on immediate finalization of objects when they become unreachable ' |
| 8313 |
'(so\n' |
| 8314 |
'you should always close files explicitly).\n' |
| 8315 |
'\n' |
| 8316 |
'Note that the use of the implementation’s tracing or debugging\n' |
| 8317 |
'facilities may keep objects alive that would normally be ' |
| 8318 |
'collectable.\n' |
| 8319 |
'Also note that catching an exception with a "try"…"except" ' |
| 8320 |
'statement\n' |
| 8321 |
'may keep objects alive.\n' |
| 8322 |
'\n' |
| 8323 |
'Some objects contain references to “external” resources such as ' |
| 8324 |
'open\n' |
| 8325 |
'files or windows. It is understood that these resources are ' |
| 8326 |
'freed\n' |
| 8327 |
'when the object is garbage-collected, but since garbage ' |
| 8328 |
'collection is\n' |
| 8329 |
'not guaranteed to happen, such objects also provide an explicit ' |
| 8330 |
'way to\n' |
| 8331 |
'release the external resource, usually a "close()" method. ' |
| 8332 |
'Programs\n' |
| 8333 |
'are strongly recommended to explicitly close such objects. The\n' |
| 8334 |
'"try"…"finally" statement and the "with" statement provide ' |
| 8335 |
'convenient\n' |
| 8336 |
'ways to do this.\n' |
| 8337 |
'\n' |
| 8338 |
'Some objects contain references to other objects; these are ' |
| 8339 |
'called\n' |
| 8340 |
'*containers*. Examples of containers are tuples, lists and\n' |
| 8341 |
'dictionaries. The references are part of a container’s value. ' |
| 8342 |
'In\n' |
| 8343 |
'most cases, when we talk about the value of a container, we imply ' |
| 8344 |
'the\n' |
| 8345 |
'values, not the identities of the contained objects; however, ' |
| 8346 |
'when we\n' |
| 8347 |
'talk about the mutability of a container, only the identities of ' |
| 8348 |
'the\n' |
| 8349 |
'immediately contained objects are implied. So, if an immutable\n' |
| 8350 |
'container (like a tuple) contains a reference to a mutable ' |
| 8351 |
'object, its\n' |
| 8352 |
'value changes if that mutable object is changed.\n' |
| 8353 |
'\n' |
| 8354 |
'Types affect almost all aspects of object behavior. Even the\n' |
| 8355 |
'importance of object identity is affected in some sense: for ' |
| 8356 |
'immutable\n' |
| 8357 |
'types, operations that compute new values may actually return a\n' |
| 8358 |
'reference to any existing object with the same type and value, ' |
| 8359 |
'while\n' |
| 8360 |
'for mutable objects this is not allowed. E.g., after "a = 1; b = ' |
| 8361 |
'1",\n' |
| 8362 |
'"a" and "b" may or may not refer to the same object with the ' |
| 8363 |
'value\n' |
| 8364 |
'one, depending on the implementation, but after "c = []; d = []", ' |
| 8365 |
'"c"\n' |
| 8366 |
'and "d" are guaranteed to refer to two different, unique, newly\n' |
| 8367 |
'created empty lists. (Note that "c = d = []" assigns the same ' |
| 8368 |
'object\n' |
| 8369 |
'to both "c" and "d".)\n', |
| 8370 |
'operator-summary': 'Operator precedence\n' |
| 8371 |
'*******************\n' |
| 8372 |
'\n' |
| 8373 |
'The following table summarizes the operator precedence ' |
| 8374 |
'in Python, from\n' |
| 8375 |
'highest precedence (most binding) to lowest precedence ' |
| 8376 |
'(least\n' |
| 8377 |
'binding). Operators in the same box have the same ' |
| 8378 |
'precedence. Unless\n' |
| 8379 |
'the syntax is explicitly given, operators are binary. ' |
| 8380 |
'Operators in\n' |
| 8381 |
'the same box group left to right (except for ' |
| 8382 |
'exponentiation and\n' |
| 8383 |
'conditional expressions, which group from right to ' |
| 8384 |
'left).\n' |
| 8385 |
'\n' |
| 8386 |
'Note that comparisons, membership tests, and identity ' |
| 8387 |
'tests, all have\n' |
| 8388 |
'the same precedence and have a left-to-right chaining ' |
| 8389 |
'feature as\n' |
| 8390 |
'described in the Comparisons section.\n' |
| 8391 |
'\n' |
| 8392 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8393 |
'| Operator | ' |
| 8394 |
'Description |\n' |
| 8395 |
'|=================================================|=======================================|\n' |
| 8396 |
'| "(expressions...)", "[expressions...]", "{key: | ' |
| 8397 |
'Binding or parenthesized expression, |\n' |
| 8398 |
'| value...}", "{expressions...}" | list ' |
| 8399 |
'display, dictionary display, set |\n' |
| 8400 |
'| | ' |
| 8401 |
'display |\n' |
| 8402 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8403 |
'| "x[index]", "x[index:index]", | ' |
| 8404 |
'Subscription, slicing, call, |\n' |
| 8405 |
'| "x(arguments...)", "x.attribute" | ' |
| 8406 |
'attribute reference |\n' |
| 8407 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8408 |
'| "await x" | ' |
| 8409 |
'Await expression |\n' |
| 8410 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8411 |
'| "**" | ' |
| 8412 |
'Exponentiation [5] |\n' |
| 8413 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8414 |
'| "+x", "-x", "~x" | ' |
| 8415 |
'Positive, negative, bitwise NOT |\n' |
| 8416 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8417 |
'| "*", "@", "/", "//", "%" | ' |
| 8418 |
'Multiplication, matrix |\n' |
| 8419 |
'| | ' |
| 8420 |
'multiplication, division, floor |\n' |
| 8421 |
'| | ' |
| 8422 |
'division, remainder [6] |\n' |
| 8423 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8424 |
'| "+", "-" | ' |
| 8425 |
'Addition and subtraction |\n' |
| 8426 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8427 |
'| "<<", ">>" | ' |
| 8428 |
'Shifts |\n' |
| 8429 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8430 |
'| "&" | ' |
| 8431 |
'Bitwise AND |\n' |
| 8432 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8433 |
'| "^" | ' |
| 8434 |
'Bitwise XOR |\n' |
| 8435 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8436 |
'| "|" | ' |
| 8437 |
'Bitwise OR |\n' |
| 8438 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8439 |
'| "in", "not in", "is", "is not", "<", "<=", ">", | ' |
| 8440 |
'Comparisons, including membership |\n' |
| 8441 |
'| ">=", "!=", "==" | ' |
| 8442 |
'tests and identity tests |\n' |
| 8443 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8444 |
'| "not x" | ' |
| 8445 |
'Boolean NOT |\n' |
| 8446 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8447 |
'| "and" | ' |
| 8448 |
'Boolean AND |\n' |
| 8449 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8450 |
'| "or" | ' |
| 8451 |
'Boolean OR |\n' |
| 8452 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8453 |
'| "if" – "else" | ' |
| 8454 |
'Conditional expression |\n' |
| 8455 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8456 |
'| "lambda" | ' |
| 8457 |
'Lambda expression |\n' |
| 8458 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8459 |
'| ":=" | ' |
| 8460 |
'Assignment expression |\n' |
| 8461 |
'+-------------------------------------------------+---------------------------------------+\n' |
| 8462 |
'\n' |
| 8463 |
'-[ Footnotes ]-\n' |
| 8464 |
'\n' |
| 8465 |
'[1] While "abs(x%y) < abs(y)" is true mathematically, ' |
| 8466 |
'for floats it\n' |
| 8467 |
' may not be true numerically due to roundoff. For ' |
| 8468 |
'example, and\n' |
| 8469 |
' assuming a platform on which a Python float is an ' |
| 8470 |
'IEEE 754 double-\n' |
| 8471 |
' precision number, in order that "-1e-100 % 1e100" ' |
| 8472 |
'have the same\n' |
| 8473 |
' sign as "1e100", the computed result is "-1e-100 + ' |
| 8474 |
'1e100", which\n' |
| 8475 |
' is numerically exactly equal to "1e100". The ' |
| 8476 |
'function\n' |
| 8477 |
' "math.fmod()" returns a result whose sign matches ' |
| 8478 |
'the sign of the\n' |
| 8479 |
' first argument instead, and so returns "-1e-100" in ' |
| 8480 |
'this case.\n' |
| 8481 |
' Which approach is more appropriate depends on the ' |
| 8482 |
'application.\n' |
| 8483 |
'\n' |
| 8484 |
'[2] If x is very close to an exact integer multiple of ' |
| 8485 |
'y, it’s\n' |
| 8486 |
' possible for "x//y" to be one larger than ' |
| 8487 |
'"(x-x%y)//y" due to\n' |
| 8488 |
' rounding. In such cases, Python returns the latter ' |
| 8489 |
'result, in\n' |
| 8490 |
' order to preserve that "divmod(x,y)[0] * y + x % y" ' |
| 8491 |
'be very close\n' |
| 8492 |
' to "x".\n' |
| 8493 |
'\n' |
| 8494 |
'[3] The Unicode standard distinguishes between *code ' |
| 8495 |
'points* (e.g.\n' |
| 8496 |
' U+0041) and *abstract characters* (e.g. “LATIN ' |
| 8497 |
'CAPITAL LETTER A”).\n' |
| 8498 |
' While most abstract characters in Unicode are only ' |
| 8499 |
'represented\n' |
| 8500 |
' using one code point, there is a number of abstract ' |
| 8501 |
'characters\n' |
| 8502 |
' that can in addition be represented using a sequence ' |
| 8503 |
'of more than\n' |
| 8504 |
' one code point. For example, the abstract character ' |
| 8505 |
'“LATIN\n' |
| 8506 |
' CAPITAL LETTER C WITH CEDILLA” can be represented as ' |
| 8507 |
'a single\n' |
| 8508 |
' *precomposed character* at code position U+00C7, or ' |
| 8509 |
'as a sequence\n' |
| 8510 |
' of a *base character* at code position U+0043 (LATIN ' |
| 8511 |
'CAPITAL\n' |
| 8512 |
' LETTER C), followed by a *combining character* at ' |
| 8513 |
'code position\n' |
| 8514 |
' U+0327 (COMBINING CEDILLA).\n' |
| 8515 |
'\n' |
| 8516 |
' The comparison operators on strings compare at the ' |
| 8517 |
'level of\n' |
| 8518 |
' Unicode code points. This may be counter-intuitive ' |
| 8519 |
'to humans. For\n' |
| 8520 |
' example, ""\\u00C7" == "\\u0043\\u0327"" is "False", ' |
| 8521 |
'even though both\n' |
| 8522 |
' strings represent the same abstract character “LATIN ' |
| 8523 |
'CAPITAL\n' |
| 8524 |
' LETTER C WITH CEDILLA”.\n' |
| 8525 |
'\n' |
| 8526 |
' To compare strings at the level of abstract ' |
| 8527 |
'characters (that is,\n' |
| 8528 |
' in a way intuitive to humans), use ' |
| 8529 |
'"unicodedata.normalize()".\n' |
| 8530 |
'\n' |
| 8531 |
'[4] Due to automatic garbage-collection, free lists, and ' |
| 8532 |
'the dynamic\n' |
| 8533 |
' nature of descriptors, you may notice seemingly ' |
| 8534 |
'unusual behaviour\n' |
| 8535 |
' in certain uses of the "is" operator, like those ' |
| 8536 |
'involving\n' |
| 8537 |
' comparisons between instance methods, or constants. ' |
| 8538 |
'Check their\n' |
| 8539 |
' documentation for more info.\n' |
| 8540 |
'\n' |
| 8541 |
'[5] The power operator "**" binds less tightly than an ' |
| 8542 |
'arithmetic or\n' |
| 8543 |
' bitwise unary operator on its right, that is, ' |
| 8544 |
'"2**-1" is "0.5".\n' |
| 8545 |
'\n' |
| 8546 |
'[6] The "%" operator is also used for string formatting; ' |
| 8547 |
'the same\n' |
| 8548 |
' precedence applies.\n', |
| 8549 |
'pass': 'The "pass" statement\n' |
| 8550 |
'********************\n' |
| 8551 |
'\n' |
| 8552 |
' pass_stmt ::= "pass"\n' |
| 8553 |
'\n' |
| 8554 |
'"pass" is a null operation — when it is executed, nothing happens. ' |
| 8555 |
'It\n' |
| 8556 |
'is useful as a placeholder when a statement is required ' |
| 8557 |
'syntactically,\n' |
| 8558 |
'but no code needs to be executed, for example:\n' |
| 8559 |
'\n' |
| 8560 |
' def f(arg): pass # a function that does nothing (yet)\n' |
| 8561 |
'\n' |
| 8562 |
' class C: pass # a class with no methods (yet)\n', |
| 8563 |
'power': 'The power operator\n' |
| 8564 |
'******************\n' |
| 8565 |
'\n' |
| 8566 |
'The power operator binds more tightly than unary operators on its\n' |
| 8567 |
'left; it binds less tightly than unary operators on its right. ' |
| 8568 |
'The\n' |
| 8569 |
'syntax is:\n' |
| 8570 |
'\n' |
| 8571 |
' power ::= (await_expr | primary) ["**" u_expr]\n' |
| 8572 |
'\n' |
| 8573 |
'Thus, in an unparenthesized sequence of power and unary operators, ' |
| 8574 |
'the\n' |
| 8575 |
'operators are evaluated from right to left (this does not ' |
| 8576 |
'constrain\n' |
| 8577 |
'the evaluation order for the operands): "-1**2" results in "-1".\n' |
| 8578 |
'\n' |
| 8579 |
'The power operator has the same semantics as the built-in "pow()"\n' |
| 8580 |
'function, when called with two arguments: it yields its left ' |
| 8581 |
'argument\n' |
| 8582 |
'raised to the power of its right argument. The numeric arguments ' |
| 8583 |
'are\n' |
| 8584 |
'first converted to a common type, and the result is of that type.\n' |
| 8585 |
'\n' |
| 8586 |
'For int operands, the result has the same type as the operands ' |
| 8587 |
'unless\n' |
| 8588 |
'the second argument is negative; in that case, all arguments are\n' |
| 8589 |
'converted to float and a float result is delivered. For example,\n' |
| 8590 |
'"10**2" returns "100", but "10**-2" returns "0.01".\n' |
| 8591 |
'\n' |
| 8592 |
'Raising "0.0" to a negative power results in a ' |
| 8593 |
'"ZeroDivisionError".\n' |
| 8594 |
'Raising a negative number to a fractional power results in a ' |
| 8595 |
'"complex"\n' |
| 8596 |
'number. (In earlier versions it raised a "ValueError".)\n' |
| 8597 |
'\n' |
| 8598 |
'This operation can be customized using the special "__pow__()" ' |
| 8599 |
'method.\n', |
| 8600 |
'raise': 'The "raise" statement\n' |
| 8601 |
'*********************\n' |
| 8602 |
'\n' |
| 8603 |
' raise_stmt ::= "raise" [expression ["from" expression]]\n' |
| 8604 |
'\n' |
| 8605 |
'If no expressions are present, "raise" re-raises the exception that ' |
| 8606 |
'is\n' |
| 8607 |
'currently being handled, which is also known as the *active\n' |
| 8608 |
'exception*. If there isn’t currently an active exception, a\n' |
| 8609 |
'"RuntimeError" exception is raised indicating that this is an ' |
| 8610 |
'error.\n' |
| 8611 |
'\n' |
| 8612 |
'Otherwise, "raise" evaluates the first expression as the exception\n' |
| 8613 |
'object. It must be either a subclass or an instance of\n' |
| 8614 |
'"BaseException". If it is a class, the exception instance will be\n' |
| 8615 |
'obtained when needed by instantiating the class with no arguments.\n' |
| 8616 |
'\n' |
| 8617 |
'The *type* of the exception is the exception instance’s class, the\n' |
| 8618 |
'*value* is the instance itself.\n' |
| 8619 |
'\n' |
| 8620 |
'A traceback object is normally created automatically when an ' |
| 8621 |
'exception\n' |
| 8622 |
'is raised and attached to it as the "__traceback__" attribute. You ' |
| 8623 |
'can\n' |
| 8624 |
'create an exception and set your own traceback in one step using ' |
| 8625 |
'the\n' |
| 8626 |
'"with_traceback()" exception method (which returns the same ' |
| 8627 |
'exception\n' |
| 8628 |
'instance, with its traceback set to its argument), like so:\n' |
| 8629 |
'\n' |
| 8630 |
' raise Exception("foo occurred").with_traceback(tracebackobj)\n' |
| 8631 |
'\n' |
| 8632 |
'The "from" clause is used for exception chaining: if given, the ' |
| 8633 |
'second\n' |
| 8634 |
'*expression* must be another exception class or instance. If the\n' |
| 8635 |
'second expression is an exception instance, it will be attached to ' |
| 8636 |
'the\n' |
| 8637 |
'raised exception as the "__cause__" attribute (which is writable). ' |
| 8638 |
'If\n' |
| 8639 |
'the expression is an exception class, the class will be ' |
| 8640 |
'instantiated\n' |
| 8641 |
'and the resulting exception instance will be attached to the ' |
| 8642 |
'raised\n' |
| 8643 |
'exception as the "__cause__" attribute. If the raised exception is ' |
| 8644 |
'not\n' |
| 8645 |
'handled, both exceptions will be printed:\n' |
| 8646 |
'\n' |
| 8647 |
' >>> try:\n' |
| 8648 |
' ... print(1 / 0)\n' |
| 8649 |
' ... except Exception as exc:\n' |
| 8650 |
' ... raise RuntimeError("Something bad happened") from exc\n' |
| 8651 |
' ...\n' |
| 8652 |
' Traceback (most recent call last):\n' |
| 8653 |
' File "<stdin>", line 2, in <module>\n' |
| 8654 |
' print(1 / 0)\n' |
| 8655 |
' ~~^~~\n' |
| 8656 |
' ZeroDivisionError: division by zero\n' |
| 8657 |
'\n' |
| 8658 |
' The above exception was the direct cause of the following ' |
| 8659 |
'exception:\n' |
| 8660 |
'\n' |
| 8661 |
' Traceback (most recent call last):\n' |
| 8662 |
' File "<stdin>", line 4, in <module>\n' |
| 8663 |
' raise RuntimeError("Something bad happened") from exc\n' |
| 8664 |
' RuntimeError: Something bad happened\n' |
| 8665 |
'\n' |
| 8666 |
'A similar mechanism works implicitly if a new exception is raised ' |
| 8667 |
'when\n' |
| 8668 |
'an exception is already being handled. An exception may be ' |
| 8669 |
'handled\n' |
| 8670 |
'when an "except" or "finally" clause, or a "with" statement, is ' |
| 8671 |
'used.\n' |
| 8672 |
'The previous exception is then attached as the new exception’s\n' |
| 8673 |
'"__context__" attribute:\n' |
| 8674 |
'\n' |
| 8675 |
' >>> try:\n' |
| 8676 |
' ... print(1 / 0)\n' |
| 8677 |
' ... except:\n' |
| 8678 |
' ... raise RuntimeError("Something bad happened")\n' |
| 8679 |
' ...\n' |
| 8680 |
' Traceback (most recent call last):\n' |
| 8681 |
' File "<stdin>", line 2, in <module>\n' |
| 8682 |
' print(1 / 0)\n' |
| 8683 |
' ~~^~~\n' |
| 8684 |
' ZeroDivisionError: division by zero\n' |
| 8685 |
'\n' |
| 8686 |
' During handling of the above exception, another exception ' |
| 8687 |
'occurred:\n' |
| 8688 |
'\n' |
| 8689 |
' Traceback (most recent call last):\n' |
| 8690 |
' File "<stdin>", line 4, in <module>\n' |
| 8691 |
' raise RuntimeError("Something bad happened")\n' |
| 8692 |
' RuntimeError: Something bad happened\n' |
| 8693 |
'\n' |
| 8694 |
'Exception chaining can be explicitly suppressed by specifying ' |
| 8695 |
'"None"\n' |
| 8696 |
'in the "from" clause:\n' |
| 8697 |
'\n' |
| 8698 |
' >>> try:\n' |
| 8699 |
' ... print(1 / 0)\n' |
| 8700 |
' ... except:\n' |
| 8701 |
' ... raise RuntimeError("Something bad happened") from None\n' |
| 8702 |
' ...\n' |
| 8703 |
' Traceback (most recent call last):\n' |
| 8704 |
' File "<stdin>", line 4, in <module>\n' |
| 8705 |
' RuntimeError: Something bad happened\n' |
| 8706 |
'\n' |
| 8707 |
'Additional information on exceptions can be found in section\n' |
| 8708 |
'Exceptions, and information about handling exceptions is in ' |
| 8709 |
'section\n' |
| 8710 |
'The try statement.\n' |
| 8711 |
'\n' |
| 8712 |
'Changed in version 3.3: "None" is now permitted as "Y" in "raise X\n' |
| 8713 |
'from Y".Added the "__suppress_context__" attribute to suppress\n' |
| 8714 |
'automatic display of the exception context.\n' |
| 8715 |
'\n' |
| 8716 |
'Changed in version 3.11: If the traceback of the active exception ' |
| 8717 |
'is\n' |
| 8718 |
'modified in an "except" clause, a subsequent "raise" statement re-\n' |
| 8719 |
'raises the exception with the modified traceback. Previously, the\n' |
| 8720 |
'exception was re-raised with the traceback it had when it was ' |
| 8721 |
'caught.\n', |
| 8722 |
'return': 'The "return" statement\n' |
| 8723 |
'**********************\n' |
| 8724 |
'\n' |
| 8725 |
' return_stmt ::= "return" [expression_list]\n' |
| 8726 |
'\n' |
| 8727 |
'"return" may only occur syntactically nested in a function ' |
| 8728 |
'definition,\n' |
| 8729 |
'not within a nested class definition.\n' |
| 8730 |
'\n' |
| 8731 |
'If an expression list is present, it is evaluated, else "None" is\n' |
| 8732 |
'substituted.\n' |
| 8733 |
'\n' |
| 8734 |
'"return" leaves the current function call with the expression list ' |
| 8735 |
'(or\n' |
| 8736 |
'"None") as return value.\n' |
| 8737 |
'\n' |
| 8738 |
'When "return" passes control out of a "try" statement with a ' |
| 8739 |
'"finally"\n' |
| 8740 |
'clause, that "finally" clause is executed before really leaving ' |
| 8741 |
'the\n' |
| 8742 |
'function.\n' |
| 8743 |
'\n' |
| 8744 |
'In a generator function, the "return" statement indicates that ' |
| 8745 |
'the\n' |
| 8746 |
'generator is done and will cause "StopIteration" to be raised. ' |
| 8747 |
'The\n' |
| 8748 |
'returned value (if any) is used as an argument to construct\n' |
| 8749 |
'"StopIteration" and becomes the "StopIteration.value" attribute.\n' |
| 8750 |
'\n' |
| 8751 |
'In an asynchronous generator function, an empty "return" ' |
| 8752 |
'statement\n' |
| 8753 |
'indicates that the asynchronous generator is done and will cause\n' |
| 8754 |
'"StopAsyncIteration" to be raised. A non-empty "return" statement ' |
| 8755 |
'is\n' |
| 8756 |
'a syntax error in an asynchronous generator function.\n', |
| 8757 |
'sequence-types': 'Emulating container types\n' |
| 8758 |
'*************************\n' |
| 8759 |
'\n' |
| 8760 |
'The following methods can be defined to implement ' |
| 8761 |
'container objects.\n' |
| 8762 |
'Containers usually are *sequences* (such as "lists" or ' |
| 8763 |
'"tuples") or\n' |
| 8764 |
'*mappings* (like "dictionaries"), but can represent other ' |
| 8765 |
'containers\n' |
| 8766 |
'as well. The first set of methods is used either to ' |
| 8767 |
'emulate a\n' |
| 8768 |
'sequence or to emulate a mapping; the difference is that ' |
| 8769 |
'for a\n' |
| 8770 |
'sequence, the allowable keys should be the integers *k* ' |
| 8771 |
'for which "0\n' |
| 8772 |
'<= k < N" where *N* is the length of the sequence, or ' |
| 8773 |
'"slice" objects,\n' |
| 8774 |
'which define a range of items. It is also recommended ' |
| 8775 |
'that mappings\n' |
| 8776 |
'provide the methods "keys()", "values()", "items()", ' |
| 8777 |
'"get()",\n' |
| 8778 |
'"clear()", "setdefault()", "pop()", "popitem()", "copy()", ' |
| 8779 |
'and\n' |
| 8780 |
'"update()" behaving similar to those for Python’s ' |
| 8781 |
'standard\n' |
| 8782 |
'"dictionary" objects. The "collections.abc" module ' |
| 8783 |
'provides a\n' |
| 8784 |
'"MutableMapping" *abstract base class* to help create ' |
| 8785 |
'those methods\n' |
| 8786 |
'from a base set of "__getitem__()", "__setitem__()", ' |
| 8787 |
'"__delitem__()",\n' |
| 8788 |
'and "keys()". Mutable sequences should provide methods ' |
| 8789 |
'"append()",\n' |
| 8790 |
'"count()", "index()", "extend()", "insert()", "pop()", ' |
| 8791 |
'"remove()",\n' |
| 8792 |
'"reverse()" and "sort()", like Python standard "list" ' |
| 8793 |
'objects.\n' |
| 8794 |
'Finally, sequence types should implement addition ' |
| 8795 |
'(meaning\n' |
| 8796 |
'concatenation) and multiplication (meaning repetition) by ' |
| 8797 |
'defining the\n' |
| 8798 |
'methods "__add__()", "__radd__()", "__iadd__()", ' |
| 8799 |
'"__mul__()",\n' |
| 8800 |
'"__rmul__()" and "__imul__()" described below; they should ' |
| 8801 |
'not define\n' |
| 8802 |
'other numerical operators. It is recommended that both ' |
| 8803 |
'mappings and\n' |
| 8804 |
'sequences implement the "__contains__()" method to allow ' |
| 8805 |
'efficient use\n' |
| 8806 |
'of the "in" operator; for mappings, "in" should search the ' |
| 8807 |
'mapping’s\n' |
| 8808 |
'keys; for sequences, it should search through the values. ' |
| 8809 |
'It is\n' |
| 8810 |
'further recommended that both mappings and sequences ' |
| 8811 |
'implement the\n' |
| 8812 |
'"__iter__()" method to allow efficient iteration through ' |
| 8813 |
'the\n' |
| 8814 |
'container; for mappings, "__iter__()" should iterate ' |
| 8815 |
'through the\n' |
| 8816 |
'object’s keys; for sequences, it should iterate through ' |
| 8817 |
'the values.\n' |
| 8818 |
'\n' |
| 8819 |
'object.__len__(self)\n' |
| 8820 |
'\n' |
| 8821 |
' Called to implement the built-in function "len()". ' |
| 8822 |
'Should return\n' |
| 8823 |
' the length of the object, an integer ">=" 0. Also, an ' |
| 8824 |
'object that\n' |
| 8825 |
' doesn’t define a "__bool__()" method and whose ' |
| 8826 |
'"__len__()" method\n' |
| 8827 |
' returns zero is considered to be false in a Boolean ' |
| 8828 |
'context.\n' |
| 8829 |
'\n' |
| 8830 |
' **CPython implementation detail:** In CPython, the ' |
| 8831 |
'length is\n' |
| 8832 |
' required to be at most "sys.maxsize". If the length is ' |
| 8833 |
'larger than\n' |
| 8834 |
' "sys.maxsize" some features (such as "len()") may ' |
| 8835 |
'raise\n' |
| 8836 |
' "OverflowError". To prevent raising "OverflowError" by ' |
| 8837 |
'truth value\n' |
| 8838 |
' testing, an object must define a "__bool__()" method.\n' |
| 8839 |
'\n' |
| 8840 |
'object.__length_hint__(self)\n' |
| 8841 |
'\n' |
| 8842 |
' Called to implement "operator.length_hint()". Should ' |
| 8843 |
'return an\n' |
| 8844 |
' estimated length for the object (which may be greater ' |
| 8845 |
'or less than\n' |
| 8846 |
' the actual length). The length must be an integer ">=" ' |
| 8847 |
'0. The\n' |
| 8848 |
' return value may also be "NotImplemented", which is ' |
| 8849 |
'treated the\n' |
| 8850 |
' same as if the "__length_hint__" method didn’t exist at ' |
| 8851 |
'all. This\n' |
| 8852 |
' method is purely an optimization and is never required ' |
| 8853 |
'for\n' |
| 8854 |
' correctness.\n' |
| 8855 |
'\n' |
| 8856 |
' New in version 3.4.\n' |
| 8857 |
'\n' |
| 8858 |
'Note:\n' |
| 8859 |
'\n' |
| 8860 |
' Slicing is done exclusively with the following three ' |
| 8861 |
'methods. A\n' |
| 8862 |
' call like\n' |
| 8863 |
'\n' |
| 8864 |
' a[1:2] = b\n' |
| 8865 |
'\n' |
| 8866 |
' is translated to\n' |
| 8867 |
'\n' |
| 8868 |
' a[slice(1, 2, None)] = b\n' |
| 8869 |
'\n' |
| 8870 |
' and so forth. Missing slice items are always filled in ' |
| 8871 |
'with "None".\n' |
| 8872 |
'\n' |
| 8873 |
'object.__getitem__(self, key)\n' |
| 8874 |
'\n' |
| 8875 |
' Called to implement evaluation of "self[key]". For ' |
| 8876 |
'*sequence*\n' |
| 8877 |
' types, the accepted keys should be integers. ' |
| 8878 |
'Optionally, they may\n' |
| 8879 |
' support "slice" objects as well. Negative index ' |
| 8880 |
'support is also\n' |
| 8881 |
' optional. If *key* is of an inappropriate type, ' |
| 8882 |
'"TypeError" may be\n' |
| 8883 |
' raised; if *key* is a value outside the set of indexes ' |
| 8884 |
'for the\n' |
| 8885 |
' sequence (after any special interpretation of negative ' |
| 8886 |
'values),\n' |
| 8887 |
' "IndexError" should be raised. For *mapping* types, if ' |
| 8888 |
'*key* is\n' |
| 8889 |
' missing (not in the container), "KeyError" should be ' |
| 8890 |
'raised.\n' |
| 8891 |
'\n' |
| 8892 |
' Note:\n' |
| 8893 |
'\n' |
| 8894 |
' "for" loops expect that an "IndexError" will be ' |
| 8895 |
'raised for\n' |
| 8896 |
' illegal indexes to allow proper detection of the end ' |
| 8897 |
'of the\n' |
| 8898 |
' sequence.\n' |
| 8899 |
'\n' |
| 8900 |
' Note:\n' |
| 8901 |
'\n' |
| 8902 |
' When subscripting a *class*, the special class ' |
| 8903 |
'method\n' |
| 8904 |
' "__class_getitem__()" may be called instead of ' |
| 8905 |
'"__getitem__()".\n' |
| 8906 |
' See __class_getitem__ versus __getitem__ for more ' |
| 8907 |
'details.\n' |
| 8908 |
'\n' |
| 8909 |
'object.__setitem__(self, key, value)\n' |
| 8910 |
'\n' |
| 8911 |
' Called to implement assignment to "self[key]". Same ' |
| 8912 |
'note as for\n' |
| 8913 |
' "__getitem__()". This should only be implemented for ' |
| 8914 |
'mappings if\n' |
| 8915 |
' the objects support changes to the values for keys, or ' |
| 8916 |
'if new keys\n' |
| 8917 |
' can be added, or for sequences if elements can be ' |
| 8918 |
'replaced. The\n' |
| 8919 |
' same exceptions should be raised for improper *key* ' |
| 8920 |
'values as for\n' |
| 8921 |
' the "__getitem__()" method.\n' |
| 8922 |
'\n' |
| 8923 |
'object.__delitem__(self, key)\n' |
| 8924 |
'\n' |
| 8925 |
' Called to implement deletion of "self[key]". Same note ' |
| 8926 |
'as for\n' |
| 8927 |
' "__getitem__()". This should only be implemented for ' |
| 8928 |
'mappings if\n' |
| 8929 |
' the objects support removal of keys, or for sequences ' |
| 8930 |
'if elements\n' |
| 8931 |
' can be removed from the sequence. The same exceptions ' |
| 8932 |
'should be\n' |
| 8933 |
' raised for improper *key* values as for the ' |
| 8934 |
'"__getitem__()" method.\n' |
| 8935 |
'\n' |
| 8936 |
'object.__missing__(self, key)\n' |
| 8937 |
'\n' |
| 8938 |
' Called by "dict"."__getitem__()" to implement ' |
| 8939 |
'"self[key]" for dict\n' |
| 8940 |
' subclasses when key is not in the dictionary.\n' |
| 8941 |
'\n' |
| 8942 |
'object.__iter__(self)\n' |
| 8943 |
'\n' |
| 8944 |
' This method is called when an *iterator* is required ' |
| 8945 |
'for a\n' |
| 8946 |
' container. This method should return a new iterator ' |
| 8947 |
'object that can\n' |
| 8948 |
' iterate over all the objects in the container. For ' |
| 8949 |
'mappings, it\n' |
| 8950 |
' should iterate over the keys of the container.\n' |
| 8951 |
'\n' |
| 8952 |
'object.__reversed__(self)\n' |
| 8953 |
'\n' |
| 8954 |
' Called (if present) by the "reversed()" built-in to ' |
| 8955 |
'implement\n' |
| 8956 |
' reverse iteration. It should return a new iterator ' |
| 8957 |
'object that\n' |
| 8958 |
' iterates over all the objects in the container in ' |
| 8959 |
'reverse order.\n' |
| 8960 |
'\n' |
| 8961 |
' If the "__reversed__()" method is not provided, the ' |
| 8962 |
'"reversed()"\n' |
| 8963 |
' built-in will fall back to using the sequence protocol ' |
| 8964 |
'("__len__()"\n' |
| 8965 |
' and "__getitem__()"). Objects that support the ' |
| 8966 |
'sequence protocol\n' |
| 8967 |
' should only provide "__reversed__()" if they can ' |
| 8968 |
'provide an\n' |
| 8969 |
' implementation that is more efficient than the one ' |
| 8970 |
'provided by\n' |
| 8971 |
' "reversed()".\n' |
| 8972 |
'\n' |
| 8973 |
'The membership test operators ("in" and "not in") are ' |
| 8974 |
'normally\n' |
| 8975 |
'implemented as an iteration through a container. However, ' |
| 8976 |
'container\n' |
| 8977 |
'objects can supply the following special method with a ' |
| 8978 |
'more efficient\n' |
| 8979 |
'implementation, which also does not require the object be ' |
| 8980 |
'iterable.\n' |
| 8981 |
'\n' |
| 8982 |
'object.__contains__(self, item)\n' |
| 8983 |
'\n' |
| 8984 |
' Called to implement membership test operators. Should ' |
| 8985 |
'return true\n' |
| 8986 |
' if *item* is in *self*, false otherwise. For mapping ' |
| 8987 |
'objects, this\n' |
| 8988 |
' should consider the keys of the mapping rather than the ' |
| 8989 |
'values or\n' |
| 8990 |
' the key-item pairs.\n' |
| 8991 |
'\n' |
| 8992 |
' For objects that don’t define "__contains__()", the ' |
| 8993 |
'membership test\n' |
| 8994 |
' first tries iteration via "__iter__()", then the old ' |
| 8995 |
'sequence\n' |
| 8996 |
' iteration protocol via "__getitem__()", see this ' |
| 8997 |
'section in the\n' |
| 8998 |
' language reference.\n', |
| 8999 |
'shifting': 'Shifting operations\n' |
| 9000 |
'*******************\n' |
| 9001 |
'\n' |
| 9002 |
'The shifting operations have lower priority than the arithmetic\n' |
| 9003 |
'operations:\n' |
| 9004 |
'\n' |
| 9005 |
' shift_expr ::= a_expr | shift_expr ("<<" | ">>") a_expr\n' |
| 9006 |
'\n' |
| 9007 |
'These operators accept integers as arguments. They shift the ' |
| 9008 |
'first\n' |
| 9009 |
'argument to the left or right by the number of bits given by ' |
| 9010 |
'the\n' |
| 9011 |
'second argument.\n' |
| 9012 |
'\n' |
| 9013 |
'This operation can be customized using the special ' |
| 9014 |
'"__lshift__()" and\n' |
| 9015 |
'"__rshift__()" methods.\n' |
| 9016 |
'\n' |
| 9017 |
'A right shift by *n* bits is defined as floor division by ' |
| 9018 |
'"pow(2,n)".\n' |
| 9019 |
'A left shift by *n* bits is defined as multiplication with ' |
| 9020 |
'"pow(2,n)".\n', |
| 9021 |
'slicings': 'Slicings\n' |
| 9022 |
'********\n' |
| 9023 |
'\n' |
| 9024 |
'A slicing selects a range of items in a sequence object (e.g., ' |
| 9025 |
'a\n' |
| 9026 |
'string, tuple or list). Slicings may be used as expressions or ' |
| 9027 |
'as\n' |
| 9028 |
'targets in assignment or "del" statements. The syntax for a ' |
| 9029 |
'slicing:\n' |
| 9030 |
'\n' |
| 9031 |
' slicing ::= primary "[" slice_list "]"\n' |
| 9032 |
' slice_list ::= slice_item ("," slice_item)* [","]\n' |
| 9033 |
' slice_item ::= expression | proper_slice\n' |
| 9034 |
' proper_slice ::= [lower_bound] ":" [upper_bound] [ ":" ' |
| 9035 |
'[stride] ]\n' |
| 9036 |
' lower_bound ::= expression\n' |
| 9037 |
' upper_bound ::= expression\n' |
| 9038 |
' stride ::= expression\n' |
| 9039 |
'\n' |
| 9040 |
'There is ambiguity in the formal syntax here: anything that ' |
| 9041 |
'looks like\n' |
| 9042 |
'an expression list also looks like a slice list, so any ' |
| 9043 |
'subscription\n' |
| 9044 |
'can be interpreted as a slicing. Rather than further ' |
| 9045 |
'complicating the\n' |
| 9046 |
'syntax, this is disambiguated by defining that in this case the\n' |
| 9047 |
'interpretation as a subscription takes priority over the\n' |
| 9048 |
'interpretation as a slicing (this is the case if the slice list\n' |
| 9049 |
'contains no proper slice).\n' |
| 9050 |
'\n' |
| 9051 |
'The semantics for a slicing are as follows. The primary is ' |
| 9052 |
'indexed\n' |
| 9053 |
'(using the same "__getitem__()" method as normal subscription) ' |
| 9054 |
'with a\n' |
| 9055 |
'key that is constructed from the slice list, as follows. If the ' |
| 9056 |
'slice\n' |
| 9057 |
'list contains at least one comma, the key is a tuple containing ' |
| 9058 |
'the\n' |
| 9059 |
'conversion of the slice items; otherwise, the conversion of the ' |
| 9060 |
'lone\n' |
| 9061 |
'slice item is the key. The conversion of a slice item that is ' |
| 9062 |
'an\n' |
| 9063 |
'expression is that expression. The conversion of a proper slice ' |
| 9064 |
'is a\n' |
| 9065 |
'slice object (see section The standard type hierarchy) whose ' |
| 9066 |
'"start",\n' |
| 9067 |
'"stop" and "step" attributes are the values of the expressions ' |
| 9068 |
'given\n' |
| 9069 |
'as lower bound, upper bound and stride, respectively, ' |
| 9070 |
'substituting\n' |
| 9071 |
'"None" for missing expressions.\n', |
| 9072 |
'specialattrs': 'Special Attributes\n' |
| 9073 |
'******************\n' |
| 9074 |
'\n' |
| 9075 |
'The implementation adds a few special read-only attributes ' |
| 9076 |
'to several\n' |
| 9077 |
'object types, where they are relevant. Some of these are ' |
| 9078 |
'not reported\n' |
| 9079 |
'by the "dir()" built-in function.\n' |
| 9080 |
'\n' |
| 9081 |
'object.__dict__\n' |
| 9082 |
'\n' |
| 9083 |
' A dictionary or other mapping object used to store an ' |
| 9084 |
'object’s\n' |
| 9085 |
' (writable) attributes.\n' |
| 9086 |
'\n' |
| 9087 |
'instance.__class__\n' |
| 9088 |
'\n' |
| 9089 |
' The class to which a class instance belongs.\n' |
| 9090 |
'\n' |
| 9091 |
'class.__bases__\n' |
| 9092 |
'\n' |
| 9093 |
' The tuple of base classes of a class object.\n' |
| 9094 |
'\n' |
| 9095 |
'definition.__name__\n' |
| 9096 |
'\n' |
| 9097 |
' The name of the class, function, method, descriptor, or ' |
| 9098 |
'generator\n' |
| 9099 |
' instance.\n' |
| 9100 |
'\n' |
| 9101 |
'definition.__qualname__\n' |
| 9102 |
'\n' |
| 9103 |
' The *qualified name* of the class, function, method, ' |
| 9104 |
'descriptor, or\n' |
| 9105 |
' generator instance.\n' |
| 9106 |
'\n' |
| 9107 |
' New in version 3.3.\n' |
| 9108 |
'\n' |
| 9109 |
'class.__mro__\n' |
| 9110 |
'\n' |
| 9111 |
' This attribute is a tuple of classes that are considered ' |
| 9112 |
'when\n' |
| 9113 |
' looking for base classes during method resolution.\n' |
| 9114 |
'\n' |
| 9115 |
'class.mro()\n' |
| 9116 |
'\n' |
| 9117 |
' This method can be overridden by a metaclass to customize ' |
| 9118 |
'the\n' |
| 9119 |
' method resolution order for its instances. It is called ' |
| 9120 |
'at class\n' |
| 9121 |
' instantiation, and its result is stored in "__mro__".\n' |
| 9122 |
'\n' |
| 9123 |
'class.__subclasses__()\n' |
| 9124 |
'\n' |
| 9125 |
' Each class keeps a list of weak references to its ' |
| 9126 |
'immediate\n' |
| 9127 |
' subclasses. This method returns a list of all those ' |
| 9128 |
'references\n' |
| 9129 |
' still alive. The list is in definition order. Example:\n' |
| 9130 |
'\n' |
| 9131 |
' >>> int.__subclasses__()\n' |
| 9132 |
" [<class 'bool'>, <enum 'IntEnum'>, <flag 'IntFlag'>, " |
| 9133 |
"<class 're._constants._NamedIntConstant'>]\n", |
| 9134 |
'specialnames': 'Special method names\n' |
| 9135 |
'********************\n' |
| 9136 |
'\n' |
| 9137 |
'A class can implement certain operations that are invoked by ' |
| 9138 |
'special\n' |
| 9139 |
'syntax (such as arithmetic operations or subscripting and ' |
| 9140 |
'slicing) by\n' |
| 9141 |
'defining methods with special names. This is Python’s ' |
| 9142 |
'approach to\n' |
| 9143 |
'*operator overloading*, allowing classes to define their own ' |
| 9144 |
'behavior\n' |
| 9145 |
'with respect to language operators. For instance, if a ' |
| 9146 |
'class defines\n' |
| 9147 |
'a method named "__getitem__()", and "x" is an instance of ' |
| 9148 |
'this class,\n' |
| 9149 |
'then "x[i]" is roughly equivalent to "type(x).__getitem__(x, ' |
| 9150 |
'i)".\n' |
| 9151 |
'Except where mentioned, attempts to execute an operation ' |
| 9152 |
'raise an\n' |
| 9153 |
'exception when no appropriate method is defined (typically\n' |
| 9154 |
'"AttributeError" or "TypeError").\n' |
| 9155 |
'\n' |
| 9156 |
'Setting a special method to "None" indicates that the ' |
| 9157 |
'corresponding\n' |
| 9158 |
'operation is not available. For example, if a class sets ' |
| 9159 |
'"__iter__()"\n' |
| 9160 |
'to "None", the class is not iterable, so calling "iter()" on ' |
| 9161 |
'its\n' |
| 9162 |
'instances will raise a "TypeError" (without falling back to\n' |
| 9163 |
'"__getitem__()"). [2]\n' |
| 9164 |
'\n' |
| 9165 |
'When implementing a class that emulates any built-in type, ' |
| 9166 |
'it is\n' |
| 9167 |
'important that the emulation only be implemented to the ' |
| 9168 |
'degree that it\n' |
| 9169 |
'makes sense for the object being modelled. For example, ' |
| 9170 |
'some\n' |
| 9171 |
'sequences may work well with retrieval of individual ' |
| 9172 |
'elements, but\n' |
| 9173 |
'extracting a slice may not make sense. (One example of this ' |
| 9174 |
'is the\n' |
| 9175 |
'"NodeList" interface in the W3C’s Document Object Model.)\n' |
| 9176 |
'\n' |
| 9177 |
'\n' |
| 9178 |
'Basic customization\n' |
| 9179 |
'===================\n' |
| 9180 |
'\n' |
| 9181 |
'object.__new__(cls[, ...])\n' |
| 9182 |
'\n' |
| 9183 |
' Called to create a new instance of class *cls*. ' |
| 9184 |
'"__new__()" is a\n' |
| 9185 |
' static method (special-cased so you need not declare it ' |
| 9186 |
'as such)\n' |
| 9187 |
' that takes the class of which an instance was requested ' |
| 9188 |
'as its\n' |
| 9189 |
' first argument. The remaining arguments are those passed ' |
| 9190 |
'to the\n' |
| 9191 |
' object constructor expression (the call to the class). ' |
| 9192 |
'The return\n' |
| 9193 |
' value of "__new__()" should be the new object instance ' |
| 9194 |
'(usually an\n' |
| 9195 |
' instance of *cls*).\n' |
| 9196 |
'\n' |
| 9197 |
' Typical implementations create a new instance of the ' |
| 9198 |
'class by\n' |
| 9199 |
' invoking the superclass’s "__new__()" method using\n' |
| 9200 |
' "super().__new__(cls[, ...])" with appropriate arguments ' |
| 9201 |
'and then\n' |
| 9202 |
' modifying the newly created instance as necessary before ' |
| 9203 |
'returning\n' |
| 9204 |
' it.\n' |
| 9205 |
'\n' |
| 9206 |
' If "__new__()" is invoked during object construction and ' |
| 9207 |
'it returns\n' |
| 9208 |
' an instance of *cls*, then the new instance’s ' |
| 9209 |
'"__init__()" method\n' |
| 9210 |
' will be invoked like "__init__(self[, ...])", where ' |
| 9211 |
'*self* is the\n' |
| 9212 |
' new instance and the remaining arguments are the same as ' |
| 9213 |
'were\n' |
| 9214 |
' passed to the object constructor.\n' |
| 9215 |
'\n' |
| 9216 |
' If "__new__()" does not return an instance of *cls*, then ' |
| 9217 |
'the new\n' |
| 9218 |
' instance’s "__init__()" method will not be invoked.\n' |
| 9219 |
'\n' |
| 9220 |
' "__new__()" is intended mainly to allow subclasses of ' |
| 9221 |
'immutable\n' |
| 9222 |
' types (like int, str, or tuple) to customize instance ' |
| 9223 |
'creation. It\n' |
| 9224 |
' is also commonly overridden in custom metaclasses in ' |
| 9225 |
'order to\n' |
| 9226 |
' customize class creation.\n' |
| 9227 |
'\n' |
| 9228 |
'object.__init__(self[, ...])\n' |
| 9229 |
'\n' |
| 9230 |
' Called after the instance has been created (by ' |
| 9231 |
'"__new__()"), but\n' |
| 9232 |
' before it is returned to the caller. The arguments are ' |
| 9233 |
'those\n' |
| 9234 |
' passed to the class constructor expression. If a base ' |
| 9235 |
'class has an\n' |
| 9236 |
' "__init__()" method, the derived class’s "__init__()" ' |
| 9237 |
'method, if\n' |
| 9238 |
' any, must explicitly call it to ensure proper ' |
| 9239 |
'initialization of the\n' |
| 9240 |
' base class part of the instance; for example:\n' |
| 9241 |
' "super().__init__([args...])".\n' |
| 9242 |
'\n' |
| 9243 |
' Because "__new__()" and "__init__()" work together in ' |
| 9244 |
'constructing\n' |
| 9245 |
' objects ("__new__()" to create it, and "__init__()" to ' |
| 9246 |
'customize\n' |
| 9247 |
' it), no non-"None" value may be returned by "__init__()"; ' |
| 9248 |
'doing so\n' |
| 9249 |
' will cause a "TypeError" to be raised at runtime.\n' |
| 9250 |
'\n' |
| 9251 |
'object.__del__(self)\n' |
| 9252 |
'\n' |
| 9253 |
' Called when the instance is about to be destroyed. This ' |
| 9254 |
'is also\n' |
| 9255 |
' called a finalizer or (improperly) a destructor. If a ' |
| 9256 |
'base class\n' |
| 9257 |
' has a "__del__()" method, the derived class’s "__del__()" ' |
| 9258 |
'method,\n' |
| 9259 |
' if any, must explicitly call it to ensure proper deletion ' |
| 9260 |
'of the\n' |
| 9261 |
' base class part of the instance.\n' |
| 9262 |
'\n' |
| 9263 |
' It is possible (though not recommended!) for the ' |
| 9264 |
'"__del__()" method\n' |
| 9265 |
' to postpone destruction of the instance by creating a new ' |
| 9266 |
'reference\n' |
| 9267 |
' to it. This is called object *resurrection*. It is\n' |
| 9268 |
' implementation-dependent whether "__del__()" is called a ' |
| 9269 |
'second\n' |
| 9270 |
' time when a resurrected object is about to be destroyed; ' |
| 9271 |
'the\n' |
| 9272 |
' current *CPython* implementation only calls it once.\n' |
| 9273 |
'\n' |
| 9274 |
' It is not guaranteed that "__del__()" methods are called ' |
| 9275 |
'for\n' |
| 9276 |
' objects that still exist when the interpreter exits.\n' |
| 9277 |
'\n' |
| 9278 |
' Note:\n' |
| 9279 |
'\n' |
| 9280 |
' "del x" doesn’t directly call "x.__del__()" — the ' |
| 9281 |
'former\n' |
| 9282 |
' decrements the reference count for "x" by one, and the ' |
| 9283 |
'latter is\n' |
| 9284 |
' only called when "x"’s reference count reaches zero.\n' |
| 9285 |
'\n' |
| 9286 |
' **CPython implementation detail:** It is possible for a ' |
| 9287 |
'reference\n' |
| 9288 |
' cycle to prevent the reference count of an object from ' |
| 9289 |
'going to\n' |
| 9290 |
' zero. In this case, the cycle will be later detected and ' |
| 9291 |
'deleted\n' |
| 9292 |
' by the *cyclic garbage collector*. A common cause of ' |
| 9293 |
'reference\n' |
| 9294 |
' cycles is when an exception has been caught in a local ' |
| 9295 |
'variable.\n' |
| 9296 |
' The frame’s locals then reference the exception, which ' |
| 9297 |
'references\n' |
| 9298 |
' its own traceback, which references the locals of all ' |
| 9299 |
'frames caught\n' |
| 9300 |
' in the traceback.\n' |
| 9301 |
'\n' |
| 9302 |
' See also: Documentation for the "gc" module.\n' |
| 9303 |
'\n' |
| 9304 |
' Warning:\n' |
| 9305 |
'\n' |
| 9306 |
' Due to the precarious circumstances under which ' |
| 9307 |
'"__del__()"\n' |
| 9308 |
' methods are invoked, exceptions that occur during their ' |
| 9309 |
'execution\n' |
| 9310 |
' are ignored, and a warning is printed to "sys.stderr" ' |
| 9311 |
'instead.\n' |
| 9312 |
' In particular:\n' |
| 9313 |
'\n' |
| 9314 |
' * "__del__()" can be invoked when arbitrary code is ' |
| 9315 |
'being\n' |
| 9316 |
' executed, including from any arbitrary thread. If ' |
| 9317 |
'"__del__()"\n' |
| 9318 |
' needs to take a lock or invoke any other blocking ' |
| 9319 |
'resource, it\n' |
| 9320 |
' may deadlock as the resource may already be taken by ' |
| 9321 |
'the code\n' |
| 9322 |
' that gets interrupted to execute "__del__()".\n' |
| 9323 |
'\n' |
| 9324 |
' * "__del__()" can be executed during interpreter ' |
| 9325 |
'shutdown. As a\n' |
| 9326 |
' consequence, the global variables it needs to access ' |
| 9327 |
'(including\n' |
| 9328 |
' other modules) may already have been deleted or set ' |
| 9329 |
'to "None".\n' |
| 9330 |
' Python guarantees that globals whose name begins with ' |
| 9331 |
'a single\n' |
| 9332 |
' underscore are deleted from their module before other ' |
| 9333 |
'globals\n' |
| 9334 |
' are deleted; if no other references to such globals ' |
| 9335 |
'exist, this\n' |
| 9336 |
' may help in assuring that imported modules are still ' |
| 9337 |
'available\n' |
| 9338 |
' at the time when the "__del__()" method is called.\n' |
| 9339 |
'\n' |
| 9340 |
'object.__repr__(self)\n' |
| 9341 |
'\n' |
| 9342 |
' Called by the "repr()" built-in function to compute the ' |
| 9343 |
'“official”\n' |
| 9344 |
' string representation of an object. If at all possible, ' |
| 9345 |
'this\n' |
| 9346 |
' should look like a valid Python expression that could be ' |
| 9347 |
'used to\n' |
| 9348 |
' recreate an object with the same value (given an ' |
| 9349 |
'appropriate\n' |
| 9350 |
' environment). If this is not possible, a string of the ' |
| 9351 |
'form\n' |
| 9352 |
' "<...some useful description...>" should be returned. The ' |
| 9353 |
'return\n' |
| 9354 |
' value must be a string object. If a class defines ' |
| 9355 |
'"__repr__()" but\n' |
| 9356 |
' not "__str__()", then "__repr__()" is also used when an ' |
| 9357 |
'“informal”\n' |
| 9358 |
' string representation of instances of that class is ' |
| 9359 |
'required.\n' |
| 9360 |
'\n' |
| 9361 |
' This is typically used for debugging, so it is important ' |
| 9362 |
'that the\n' |
| 9363 |
' representation is information-rich and unambiguous.\n' |
| 9364 |
'\n' |
| 9365 |
'object.__str__(self)\n' |
| 9366 |
'\n' |
| 9367 |
' Called by "str(object)" and the built-in functions ' |
| 9368 |
'"format()" and\n' |
| 9369 |
' "print()" to compute the “informal” or nicely printable ' |
| 9370 |
'string\n' |
| 9371 |
' representation of an object. The return value must be a ' |
| 9372 |
'string\n' |
| 9373 |
' object.\n' |
| 9374 |
'\n' |
| 9375 |
' This method differs from "object.__repr__()" in that ' |
| 9376 |
'there is no\n' |
| 9377 |
' expectation that "__str__()" return a valid Python ' |
| 9378 |
'expression: a\n' |
| 9379 |
' more convenient or concise representation can be used.\n' |
| 9380 |
'\n' |
| 9381 |
' The default implementation defined by the built-in type ' |
| 9382 |
'"object"\n' |
| 9383 |
' calls "object.__repr__()".\n' |
| 9384 |
'\n' |
| 9385 |
'object.__bytes__(self)\n' |
| 9386 |
'\n' |
| 9387 |
' Called by bytes to compute a byte-string representation ' |
| 9388 |
'of an\n' |
| 9389 |
' object. This should return a "bytes" object.\n' |
| 9390 |
'\n' |
| 9391 |
'object.__format__(self, format_spec)\n' |
| 9392 |
'\n' |
| 9393 |
' Called by the "format()" built-in function, and by ' |
| 9394 |
'extension,\n' |
| 9395 |
' evaluation of formatted string literals and the ' |
| 9396 |
'"str.format()"\n' |
| 9397 |
' method, to produce a “formatted” string representation of ' |
| 9398 |
'an\n' |
| 9399 |
' object. The *format_spec* argument is a string that ' |
| 9400 |
'contains a\n' |
| 9401 |
' description of the formatting options desired. The ' |
| 9402 |
'interpretation\n' |
| 9403 |
' of the *format_spec* argument is up to the type ' |
| 9404 |
'implementing\n' |
| 9405 |
' "__format__()", however most classes will either ' |
| 9406 |
'delegate\n' |
| 9407 |
' formatting to one of the built-in types, or use a ' |
| 9408 |
'similar\n' |
| 9409 |
' formatting option syntax.\n' |
| 9410 |
'\n' |
| 9411 |
' See Format Specification Mini-Language for a description ' |
| 9412 |
'of the\n' |
| 9413 |
' standard formatting syntax.\n' |
| 9414 |
'\n' |
| 9415 |
' The return value must be a string object.\n' |
| 9416 |
'\n' |
| 9417 |
' Changed in version 3.4: The __format__ method of "object" ' |
| 9418 |
'itself\n' |
| 9419 |
' raises a "TypeError" if passed any non-empty string.\n' |
| 9420 |
'\n' |
| 9421 |
' Changed in version 3.7: "object.__format__(x, \'\')" is ' |
| 9422 |
'now\n' |
| 9423 |
' equivalent to "str(x)" rather than "format(str(x), ' |
| 9424 |
'\'\')".\n' |
| 9425 |
'\n' |
| 9426 |
'object.__lt__(self, other)\n' |
| 9427 |
'object.__le__(self, other)\n' |
| 9428 |
'object.__eq__(self, other)\n' |
| 9429 |
'object.__ne__(self, other)\n' |
| 9430 |
'object.__gt__(self, other)\n' |
| 9431 |
'object.__ge__(self, other)\n' |
| 9432 |
'\n' |
| 9433 |
' These are the so-called “rich comparison” methods. The\n' |
| 9434 |
' correspondence between operator symbols and method names ' |
| 9435 |
'is as\n' |
| 9436 |
' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls ' |
| 9437 |
'"x.__le__(y)",\n' |
| 9438 |
' "x==y" calls "x.__eq__(y)", "x!=y" calls "x.__ne__(y)", ' |
| 9439 |
'"x>y" calls\n' |
| 9440 |
' "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".\n' |
| 9441 |
'\n' |
| 9442 |
' A rich comparison method may return the singleton ' |
| 9443 |
'"NotImplemented"\n' |
| 9444 |
' if it does not implement the operation for a given pair ' |
| 9445 |
'of\n' |
| 9446 |
' arguments. By convention, "False" and "True" are returned ' |
| 9447 |
'for a\n' |
| 9448 |
' successful comparison. However, these methods can return ' |
| 9449 |
'any value,\n' |
| 9450 |
' so if the comparison operator is used in a Boolean ' |
| 9451 |
'context (e.g.,\n' |
| 9452 |
' in the condition of an "if" statement), Python will call ' |
| 9453 |
'"bool()"\n' |
| 9454 |
' on the value to determine if the result is true or ' |
| 9455 |
'false.\n' |
| 9456 |
'\n' |
| 9457 |
' By default, "object" implements "__eq__()" by using "is", ' |
| 9458 |
'returning\n' |
| 9459 |
' "NotImplemented" in the case of a false comparison: "True ' |
| 9460 |
'if x is y\n' |
| 9461 |
' else NotImplemented". For "__ne__()", by default it ' |
| 9462 |
'delegates to\n' |
| 9463 |
' "__eq__()" and inverts the result unless it is ' |
| 9464 |
'"NotImplemented".\n' |
| 9465 |
' There are no other implied relationships among the ' |
| 9466 |
'comparison\n' |
| 9467 |
' operators or default implementations; for example, the ' |
| 9468 |
'truth of\n' |
| 9469 |
' "(x<y or x==y)" does not imply "x<=y". To automatically ' |
| 9470 |
'generate\n' |
| 9471 |
' ordering operations from a single root operation, see\n' |
| 9472 |
' "functools.total_ordering()".\n' |
| 9473 |
'\n' |
| 9474 |
' See the paragraph on "__hash__()" for some important ' |
| 9475 |
'notes on\n' |
| 9476 |
' creating *hashable* objects which support custom ' |
| 9477 |
'comparison\n' |
| 9478 |
' operations and are usable as dictionary keys.\n' |
| 9479 |
'\n' |
| 9480 |
' There are no swapped-argument versions of these methods ' |
| 9481 |
'(to be used\n' |
| 9482 |
' when the left argument does not support the operation but ' |
| 9483 |
'the right\n' |
| 9484 |
' argument does); rather, "__lt__()" and "__gt__()" are ' |
| 9485 |
'each other’s\n' |
| 9486 |
' reflection, "__le__()" and "__ge__()" are each other’s ' |
| 9487 |
'reflection,\n' |
| 9488 |
' and "__eq__()" and "__ne__()" are their own reflection. ' |
| 9489 |
'If the\n' |
| 9490 |
' operands are of different types, and the right operand’s ' |
| 9491 |
'type is a\n' |
| 9492 |
' direct or indirect subclass of the left operand’s type, ' |
| 9493 |
'the\n' |
| 9494 |
' reflected method of the right operand has priority, ' |
| 9495 |
'otherwise the\n' |
| 9496 |
' left operand’s method has priority. Virtual subclassing ' |
| 9497 |
'is not\n' |
| 9498 |
' considered.\n' |
| 9499 |
'\n' |
| 9500 |
' When no appropriate method returns any value other than\n' |
| 9501 |
' "NotImplemented", the "==" and "!=" operators will fall ' |
| 9502 |
'back to\n' |
| 9503 |
' "is" and "is not", respectively.\n' |
| 9504 |
'\n' |
| 9505 |
'object.__hash__(self)\n' |
| 9506 |
'\n' |
| 9507 |
' Called by built-in function "hash()" and for operations ' |
| 9508 |
'on members\n' |
| 9509 |
' of hashed collections including "set", "frozenset", and ' |
| 9510 |
'"dict".\n' |
| 9511 |
' The "__hash__()" method should return an integer. The ' |
| 9512 |
'only required\n' |
| 9513 |
' property is that objects which compare equal have the ' |
| 9514 |
'same hash\n' |
| 9515 |
' value; it is advised to mix together the hash values of ' |
| 9516 |
'the\n' |
| 9517 |
' components of the object that also play a part in ' |
| 9518 |
'comparison of\n' |
| 9519 |
' objects by packing them into a tuple and hashing the ' |
| 9520 |
'tuple.\n' |
| 9521 |
' Example:\n' |
| 9522 |
'\n' |
| 9523 |
' def __hash__(self):\n' |
| 9524 |
' return hash((self.name, self.nick, self.color))\n' |
| 9525 |
'\n' |
| 9526 |
' Note:\n' |
| 9527 |
'\n' |
| 9528 |
' "hash()" truncates the value returned from an object’s ' |
| 9529 |
'custom\n' |
| 9530 |
' "__hash__()" method to the size of a "Py_ssize_t". ' |
| 9531 |
'This is\n' |
| 9532 |
' typically 8 bytes on 64-bit builds and 4 bytes on ' |
| 9533 |
'32-bit builds.\n' |
| 9534 |
' If an object’s "__hash__()" must interoperate on ' |
| 9535 |
'builds of\n' |
| 9536 |
' different bit sizes, be sure to check the width on all ' |
| 9537 |
'supported\n' |
| 9538 |
' builds. An easy way to do this is with "python -c ' |
| 9539 |
'"import sys;\n' |
| 9540 |
' print(sys.hash_info.width)"".\n' |
| 9541 |
'\n' |
| 9542 |
' If a class does not define an "__eq__()" method it should ' |
| 9543 |
'not\n' |
| 9544 |
' define a "__hash__()" operation either; if it defines ' |
| 9545 |
'"__eq__()"\n' |
| 9546 |
' but not "__hash__()", its instances will not be usable as ' |
| 9547 |
'items in\n' |
| 9548 |
' hashable collections. If a class defines mutable objects ' |
| 9549 |
'and\n' |
| 9550 |
' implements an "__eq__()" method, it should not implement\n' |
| 9551 |
' "__hash__()", since the implementation of *hashable* ' |
| 9552 |
'collections\n' |
| 9553 |
' requires that a key’s hash value is immutable (if the ' |
| 9554 |
'object’s hash\n' |
| 9555 |
' value changes, it will be in the wrong hash bucket).\n' |
| 9556 |
'\n' |
| 9557 |
' User-defined classes have "__eq__()" and "__hash__()" ' |
| 9558 |
'methods by\n' |
| 9559 |
' default; with them, all objects compare unequal (except ' |
| 9560 |
'with\n' |
| 9561 |
' themselves) and "x.__hash__()" returns an appropriate ' |
| 9562 |
'value such\n' |
| 9563 |
' that "x == y" implies both that "x is y" and "hash(x) == ' |
| 9564 |
'hash(y)".\n' |
| 9565 |
'\n' |
| 9566 |
' A class that overrides "__eq__()" and does not define ' |
| 9567 |
'"__hash__()"\n' |
| 9568 |
' will have its "__hash__()" implicitly set to "None". ' |
| 9569 |
'When the\n' |
| 9570 |
' "__hash__()" method of a class is "None", instances of ' |
| 9571 |
'the class\n' |
| 9572 |
' will raise an appropriate "TypeError" when a program ' |
| 9573 |
'attempts to\n' |
| 9574 |
' retrieve their hash value, and will also be correctly ' |
| 9575 |
'identified as\n' |
| 9576 |
' unhashable when checking "isinstance(obj,\n' |
| 9577 |
' collections.abc.Hashable)".\n' |
| 9578 |
'\n' |
| 9579 |
' If a class that overrides "__eq__()" needs to retain the\n' |
| 9580 |
' implementation of "__hash__()" from a parent class, the ' |
| 9581 |
'interpreter\n' |
| 9582 |
' must be told this explicitly by setting "__hash__ =\n' |
| 9583 |
' <ParentClass>.__hash__".\n' |
| 9584 |
'\n' |
| 9585 |
' If a class that does not override "__eq__()" wishes to ' |
| 9586 |
'suppress\n' |
| 9587 |
' hash support, it should include "__hash__ = None" in the ' |
| 9588 |
'class\n' |
| 9589 |
' definition. A class which defines its own "__hash__()" ' |
| 9590 |
'that\n' |
| 9591 |
' explicitly raises a "TypeError" would be incorrectly ' |
| 9592 |
'identified as\n' |
| 9593 |
' hashable by an "isinstance(obj, ' |
| 9594 |
'collections.abc.Hashable)" call.\n' |
| 9595 |
'\n' |
| 9596 |
' Note:\n' |
| 9597 |
'\n' |
| 9598 |
' By default, the "__hash__()" values of str and bytes ' |
| 9599 |
'objects are\n' |
| 9600 |
' “salted” with an unpredictable random value. Although ' |
| 9601 |
'they\n' |
| 9602 |
' remain constant within an individual Python process, ' |
| 9603 |
'they are not\n' |
| 9604 |
' predictable between repeated invocations of Python.This ' |
| 9605 |
'is\n' |
| 9606 |
' intended to provide protection against a ' |
| 9607 |
'denial-of-service caused\n' |
| 9608 |
' by carefully chosen inputs that exploit the worst case\n' |
| 9609 |
' performance of a dict insertion, *O*(*n*^2) ' |
| 9610 |
'complexity. See\n' |
| 9611 |
' http://ocert.org/advisories/ocert-2011-003.html for\n' |
| 9612 |
' details.Changing hash values affects the iteration ' |
| 9613 |
'order of sets.\n' |
| 9614 |
' Python has never made guarantees about this ordering ' |
| 9615 |
'(and it\n' |
| 9616 |
' typically varies between 32-bit and 64-bit builds).See ' |
| 9617 |
'also\n' |
| 9618 |
' "PYTHONHASHSEED".\n' |
| 9619 |
'\n' |
| 9620 |
' Changed in version 3.3: Hash randomization is enabled by ' |
| 9621 |
'default.\n' |
| 9622 |
'\n' |
| 9623 |
'object.__bool__(self)\n' |
| 9624 |
'\n' |
| 9625 |
' Called to implement truth value testing and the built-in ' |
| 9626 |
'operation\n' |
| 9627 |
' "bool()"; should return "False" or "True". When this ' |
| 9628 |
'method is not\n' |
| 9629 |
' defined, "__len__()" is called, if it is defined, and the ' |
| 9630 |
'object is\n' |
| 9631 |
' considered true if its result is nonzero. If a class ' |
| 9632 |
'defines\n' |
| 9633 |
' neither "__len__()" nor "__bool__()", all its instances ' |
| 9634 |
'are\n' |
| 9635 |
' considered true.\n' |
| 9636 |
'\n' |
| 9637 |
'\n' |
| 9638 |
'Customizing attribute access\n' |
| 9639 |
'============================\n' |
| 9640 |
'\n' |
| 9641 |
'The following methods can be defined to customize the ' |
| 9642 |
'meaning of\n' |
| 9643 |
'attribute access (use of, assignment to, or deletion of ' |
| 9644 |
'"x.name") for\n' |
| 9645 |
'class instances.\n' |
| 9646 |
'\n' |
| 9647 |
'object.__getattr__(self, name)\n' |
| 9648 |
'\n' |
| 9649 |
' Called when the default attribute access fails with an\n' |
| 9650 |
' "AttributeError" (either "__getattribute__()" raises an\n' |
| 9651 |
' "AttributeError" because *name* is not an instance ' |
| 9652 |
'attribute or an\n' |
| 9653 |
' attribute in the class tree for "self"; or "__get__()" of ' |
| 9654 |
'a *name*\n' |
| 9655 |
' property raises "AttributeError"). This method should ' |
| 9656 |
'either\n' |
| 9657 |
' return the (computed) attribute value or raise an ' |
| 9658 |
'"AttributeError"\n' |
| 9659 |
' exception.\n' |
| 9660 |
'\n' |
| 9661 |
' Note that if the attribute is found through the normal ' |
| 9662 |
'mechanism,\n' |
| 9663 |
' "__getattr__()" is not called. (This is an intentional ' |
| 9664 |
'asymmetry\n' |
| 9665 |
' between "__getattr__()" and "__setattr__()".) This is ' |
| 9666 |
'done both for\n' |
| 9667 |
' efficiency reasons and because otherwise "__getattr__()" ' |
| 9668 |
'would have\n' |
| 9669 |
' no way to access other attributes of the instance. Note ' |
| 9670 |
'that at\n' |
| 9671 |
' least for instance variables, you can fake total control ' |
| 9672 |
'by not\n' |
| 9673 |
' inserting any values in the instance attribute dictionary ' |
| 9674 |
'(but\n' |
| 9675 |
' instead inserting them in another object). See the\n' |
| 9676 |
' "__getattribute__()" method below for a way to actually ' |
| 9677 |
'get total\n' |
| 9678 |
' control over attribute access.\n' |
| 9679 |
'\n' |
| 9680 |
'object.__getattribute__(self, name)\n' |
| 9681 |
'\n' |
| 9682 |
' Called unconditionally to implement attribute accesses ' |
| 9683 |
'for\n' |
| 9684 |
' instances of the class. If the class also defines ' |
| 9685 |
'"__getattr__()",\n' |
| 9686 |
' the latter will not be called unless "__getattribute__()" ' |
| 9687 |
'either\n' |
| 9688 |
' calls it explicitly or raises an "AttributeError". This ' |
| 9689 |
'method\n' |
| 9690 |
' should return the (computed) attribute value or raise an\n' |
| 9691 |
' "AttributeError" exception. In order to avoid infinite ' |
| 9692 |
'recursion in\n' |
| 9693 |
' this method, its implementation should always call the ' |
| 9694 |
'base class\n' |
| 9695 |
' method with the same name to access any attributes it ' |
| 9696 |
'needs, for\n' |
| 9697 |
' example, "object.__getattribute__(self, name)".\n' |
| 9698 |
'\n' |
| 9699 |
' Note:\n' |
| 9700 |
'\n' |
| 9701 |
' This method may still be bypassed when looking up ' |
| 9702 |
'special methods\n' |
| 9703 |
' as the result of implicit invocation via language ' |
| 9704 |
'syntax or\n' |
| 9705 |
' built-in functions. See Special method lookup.\n' |
| 9706 |
'\n' |
| 9707 |
' For certain sensitive attribute accesses, raises an ' |
| 9708 |
'auditing event\n' |
| 9709 |
' "object.__getattr__" with arguments "obj" and "name".\n' |
| 9710 |
'\n' |
| 9711 |
'object.__setattr__(self, name, value)\n' |
| 9712 |
'\n' |
| 9713 |
' Called when an attribute assignment is attempted. This ' |
| 9714 |
'is called\n' |
| 9715 |
' instead of the normal mechanism (i.e. store the value in ' |
| 9716 |
'the\n' |
| 9717 |
' instance dictionary). *name* is the attribute name, ' |
| 9718 |
'*value* is the\n' |
| 9719 |
' value to be assigned to it.\n' |
| 9720 |
'\n' |
| 9721 |
' If "__setattr__()" wants to assign to an instance ' |
| 9722 |
'attribute, it\n' |
| 9723 |
' should call the base class method with the same name, for ' |
| 9724 |
'example,\n' |
| 9725 |
' "object.__setattr__(self, name, value)".\n' |
| 9726 |
'\n' |
| 9727 |
' For certain sensitive attribute assignments, raises an ' |
| 9728 |
'auditing\n' |
| 9729 |
' event "object.__setattr__" with arguments "obj", "name", ' |
| 9730 |
'"value".\n' |
| 9731 |
'\n' |
| 9732 |
'object.__delattr__(self, name)\n' |
| 9733 |
'\n' |
| 9734 |
' Like "__setattr__()" but for attribute deletion instead ' |
| 9735 |
'of\n' |
| 9736 |
' assignment. This should only be implemented if "del ' |
| 9737 |
'obj.name" is\n' |
| 9738 |
' meaningful for the object.\n' |
| 9739 |
'\n' |
| 9740 |
' For certain sensitive attribute deletions, raises an ' |
| 9741 |
'auditing event\n' |
| 9742 |
' "object.__delattr__" with arguments "obj" and "name".\n' |
| 9743 |
'\n' |
| 9744 |
'object.__dir__(self)\n' |
| 9745 |
'\n' |
| 9746 |
' Called when "dir()" is called on the object. An iterable ' |
| 9747 |
'must be\n' |
| 9748 |
' returned. "dir()" converts the returned iterable to a ' |
| 9749 |
'list and\n' |
| 9750 |
' sorts it.\n' |
| 9751 |
'\n' |
| 9752 |
'\n' |
| 9753 |
'Customizing module attribute access\n' |
| 9754 |
'-----------------------------------\n' |
| 9755 |
'\n' |
| 9756 |
'Special names "__getattr__" and "__dir__" can be also used ' |
| 9757 |
'to\n' |
| 9758 |
'customize access to module attributes. The "__getattr__" ' |
| 9759 |
'function at\n' |
| 9760 |
'the module level should accept one argument which is the ' |
| 9761 |
'name of an\n' |
| 9762 |
'attribute and return the computed value or raise an ' |
| 9763 |
'"AttributeError".\n' |
| 9764 |
'If an attribute is not found on a module object through the ' |
| 9765 |
'normal\n' |
| 9766 |
'lookup, i.e. "object.__getattribute__()", then "__getattr__" ' |
| 9767 |
'is\n' |
| 9768 |
'searched in the module "__dict__" before raising an ' |
| 9769 |
'"AttributeError".\n' |
| 9770 |
'If found, it is called with the attribute name and the ' |
| 9771 |
'result is\n' |
| 9772 |
'returned.\n' |
| 9773 |
'\n' |
| 9774 |
'The "__dir__" function should accept no arguments, and ' |
| 9775 |
'return an\n' |
| 9776 |
'iterable of strings that represents the names accessible on ' |
| 9777 |
'module. If\n' |
| 9778 |
'present, this function overrides the standard "dir()" search ' |
| 9779 |
'on a\n' |
| 9780 |
'module.\n' |
| 9781 |
'\n' |
| 9782 |
'For a more fine grained customization of the module behavior ' |
| 9783 |
'(setting\n' |
| 9784 |
'attributes, properties, etc.), one can set the "__class__" ' |
| 9785 |
'attribute\n' |
| 9786 |
'of a module object to a subclass of "types.ModuleType". For ' |
| 9787 |
'example:\n' |
| 9788 |
'\n' |
| 9789 |
' import sys\n' |
| 9790 |
' from types import ModuleType\n' |
| 9791 |
'\n' |
| 9792 |
' class VerboseModule(ModuleType):\n' |
| 9793 |
' def __repr__(self):\n' |
| 9794 |
" return f'Verbose {self.__name__}'\n" |
| 9795 |
'\n' |
| 9796 |
' def __setattr__(self, attr, value):\n' |
| 9797 |
" print(f'Setting {attr}...')\n" |
| 9798 |
' super().__setattr__(attr, value)\n' |
| 9799 |
'\n' |
| 9800 |
' sys.modules[__name__].__class__ = VerboseModule\n' |
| 9801 |
'\n' |
| 9802 |
'Note:\n' |
| 9803 |
'\n' |
| 9804 |
' Defining module "__getattr__" and setting module ' |
| 9805 |
'"__class__" only\n' |
| 9806 |
' affect lookups made using the attribute access syntax – ' |
| 9807 |
'directly\n' |
| 9808 |
' accessing the module globals (whether by code within the ' |
| 9809 |
'module, or\n' |
| 9810 |
' via a reference to the module’s globals dictionary) is ' |
| 9811 |
'unaffected.\n' |
| 9812 |
'\n' |
| 9813 |
'Changed in version 3.5: "__class__" module attribute is now ' |
| 9814 |
'writable.\n' |
| 9815 |
'\n' |
| 9816 |
'New in version 3.7: "__getattr__" and "__dir__" module ' |
| 9817 |
'attributes.\n' |
| 9818 |
'\n' |
| 9819 |
'See also:\n' |
| 9820 |
'\n' |
| 9821 |
' **PEP 562** - Module __getattr__ and __dir__\n' |
| 9822 |
' Describes the "__getattr__" and "__dir__" functions on ' |
| 9823 |
'modules.\n' |
| 9824 |
'\n' |
| 9825 |
'\n' |
| 9826 |
'Implementing Descriptors\n' |
| 9827 |
'------------------------\n' |
| 9828 |
'\n' |
| 9829 |
'The following methods only apply when an instance of the ' |
| 9830 |
'class\n' |
| 9831 |
'containing the method (a so-called *descriptor* class) ' |
| 9832 |
'appears in an\n' |
| 9833 |
'*owner* class (the descriptor must be in either the owner’s ' |
| 9834 |
'class\n' |
| 9835 |
'dictionary or in the class dictionary for one of its ' |
| 9836 |
'parents). In the\n' |
| 9837 |
'examples below, “the attribute” refers to the attribute ' |
| 9838 |
'whose name is\n' |
| 9839 |
'the key of the property in the owner class’ "__dict__".\n' |
| 9840 |
'\n' |
| 9841 |
'object.__get__(self, instance, owner=None)\n' |
| 9842 |
'\n' |
| 9843 |
' Called to get the attribute of the owner class (class ' |
| 9844 |
'attribute\n' |
| 9845 |
' access) or of an instance of that class (instance ' |
| 9846 |
'attribute\n' |
| 9847 |
' access). The optional *owner* argument is the owner ' |
| 9848 |
'class, while\n' |
| 9849 |
' *instance* is the instance that the attribute was ' |
| 9850 |
'accessed through,\n' |
| 9851 |
' or "None" when the attribute is accessed through the ' |
| 9852 |
'*owner*.\n' |
| 9853 |
'\n' |
| 9854 |
' This method should return the computed attribute value or ' |
| 9855 |
'raise an\n' |
| 9856 |
' "AttributeError" exception.\n' |
| 9857 |
'\n' |
| 9858 |
' **PEP 252** specifies that "__get__()" is callable with ' |
| 9859 |
'one or two\n' |
| 9860 |
' arguments. Python’s own built-in descriptors support ' |
| 9861 |
'this\n' |
| 9862 |
' specification; however, it is likely that some ' |
| 9863 |
'third-party tools\n' |
| 9864 |
' have descriptors that require both arguments. Python’s ' |
| 9865 |
'own\n' |
| 9866 |
' "__getattribute__()" implementation always passes in both ' |
| 9867 |
'arguments\n' |
| 9868 |
' whether they are required or not.\n' |
| 9869 |
'\n' |
| 9870 |
'object.__set__(self, instance, value)\n' |
| 9871 |
'\n' |
| 9872 |
' Called to set the attribute on an instance *instance* of ' |
| 9873 |
'the owner\n' |
| 9874 |
' class to a new value, *value*.\n' |
| 9875 |
'\n' |
| 9876 |
' Note, adding "__set__()" or "__delete__()" changes the ' |
| 9877 |
'kind of\n' |
| 9878 |
' descriptor to a “data descriptor”. See Invoking ' |
| 9879 |
'Descriptors for\n' |
| 9880 |
' more details.\n' |
| 9881 |
'\n' |
| 9882 |
'object.__delete__(self, instance)\n' |
| 9883 |
'\n' |
| 9884 |
' Called to delete the attribute on an instance *instance* ' |
| 9885 |
'of the\n' |
| 9886 |
' owner class.\n' |
| 9887 |
'\n' |
| 9888 |
'Instances of descriptors may also have the "__objclass__" ' |
| 9889 |
'attribute\n' |
| 9890 |
'present:\n' |
| 9891 |
'\n' |
| 9892 |
'object.__objclass__\n' |
| 9893 |
'\n' |
| 9894 |
' The attribute "__objclass__" is interpreted by the ' |
| 9895 |
'"inspect" module\n' |
| 9896 |
' as specifying the class where this object was defined ' |
| 9897 |
'(setting this\n' |
| 9898 |
' appropriately can assist in runtime introspection of ' |
| 9899 |
'dynamic class\n' |
| 9900 |
' attributes). For callables, it may indicate that an ' |
| 9901 |
'instance of the\n' |
| 9902 |
' given type (or a subclass) is expected or required as the ' |
| 9903 |
'first\n' |
| 9904 |
' positional argument (for example, CPython sets this ' |
| 9905 |
'attribute for\n' |
| 9906 |
' unbound methods that are implemented in C).\n' |
| 9907 |
'\n' |
| 9908 |
'\n' |
| 9909 |
'Invoking Descriptors\n' |
| 9910 |
'--------------------\n' |
| 9911 |
'\n' |
| 9912 |
'In general, a descriptor is an object attribute with ' |
| 9913 |
'“binding\n' |
| 9914 |
'behavior”, one whose attribute access has been overridden by ' |
| 9915 |
'methods\n' |
| 9916 |
'in the descriptor protocol: "__get__()", "__set__()", and\n' |
| 9917 |
'"__delete__()". If any of those methods are defined for an ' |
| 9918 |
'object, it\n' |
| 9919 |
'is said to be a descriptor.\n' |
| 9920 |
'\n' |
| 9921 |
'The default behavior for attribute access is to get, set, or ' |
| 9922 |
'delete\n' |
| 9923 |
'the attribute from an object’s dictionary. For instance, ' |
| 9924 |
'"a.x" has a\n' |
| 9925 |
'lookup chain starting with "a.__dict__[\'x\']", then\n' |
| 9926 |
'"type(a).__dict__[\'x\']", and continuing through the base ' |
| 9927 |
'classes of\n' |
| 9928 |
'"type(a)" excluding metaclasses.\n' |
| 9929 |
'\n' |
| 9930 |
'However, if the looked-up value is an object defining one of ' |
| 9931 |
'the\n' |
| 9932 |
'descriptor methods, then Python may override the default ' |
| 9933 |
'behavior and\n' |
| 9934 |
'invoke the descriptor method instead. Where this occurs in ' |
| 9935 |
'the\n' |
| 9936 |
'precedence chain depends on which descriptor methods were ' |
| 9937 |
'defined and\n' |
| 9938 |
'how they were called.\n' |
| 9939 |
'\n' |
| 9940 |
'The starting point for descriptor invocation is a binding, ' |
| 9941 |
'"a.x". How\n' |
| 9942 |
'the arguments are assembled depends on "a":\n' |
| 9943 |
'\n' |
| 9944 |
'Direct Call\n' |
| 9945 |
' The simplest and least common call is when user code ' |
| 9946 |
'directly\n' |
| 9947 |
' invokes a descriptor method: "x.__get__(a)".\n' |
| 9948 |
'\n' |
| 9949 |
'Instance Binding\n' |
| 9950 |
' If binding to an object instance, "a.x" is transformed ' |
| 9951 |
'into the\n' |
| 9952 |
' call: "type(a).__dict__[\'x\'].__get__(a, type(a))".\n' |
| 9953 |
'\n' |
| 9954 |
'Class Binding\n' |
| 9955 |
' If binding to a class, "A.x" is transformed into the ' |
| 9956 |
'call:\n' |
| 9957 |
' "A.__dict__[\'x\'].__get__(None, A)".\n' |
| 9958 |
'\n' |
| 9959 |
'Super Binding\n' |
| 9960 |
' A dotted lookup such as "super(A, a).x" searches\n' |
| 9961 |
' "a.__class__.__mro__" for a base class "B" following "A" ' |
| 9962 |
'and then\n' |
| 9963 |
' returns "B.__dict__[\'x\'].__get__(a, A)". If not a ' |
| 9964 |
'descriptor, "x"\n' |
| 9965 |
' is returned unchanged.\n' |
| 9966 |
'\n' |
| 9967 |
'For instance bindings, the precedence of descriptor ' |
| 9968 |
'invocation depends\n' |
| 9969 |
'on which descriptor methods are defined. A descriptor can ' |
| 9970 |
'define any\n' |
| 9971 |
'combination of "__get__()", "__set__()" and "__delete__()". ' |
| 9972 |
'If it\n' |
| 9973 |
'does not define "__get__()", then accessing the attribute ' |
| 9974 |
'will return\n' |
| 9975 |
'the descriptor object itself unless there is a value in the ' |
| 9976 |
'object’s\n' |
| 9977 |
'instance dictionary. If the descriptor defines "__set__()" ' |
| 9978 |
'and/or\n' |
| 9979 |
'"__delete__()", it is a data descriptor; if it defines ' |
| 9980 |
'neither, it is\n' |
| 9981 |
'a non-data descriptor. Normally, data descriptors define ' |
| 9982 |
'both\n' |
| 9983 |
'"__get__()" and "__set__()", while non-data descriptors have ' |
| 9984 |
'just the\n' |
| 9985 |
'"__get__()" method. Data descriptors with "__get__()" and ' |
| 9986 |
'"__set__()"\n' |
| 9987 |
'(and/or "__delete__()") defined always override a ' |
| 9988 |
'redefinition in an\n' |
| 9989 |
'instance dictionary. In contrast, non-data descriptors can ' |
| 9990 |
'be\n' |
| 9991 |
'overridden by instances.\n' |
| 9992 |
'\n' |
| 9993 |
'Python methods (including those decorated with ' |
| 9994 |
'"@staticmethod" and\n' |
| 9995 |
'"@classmethod") are implemented as non-data descriptors. ' |
| 9996 |
'Accordingly,\n' |
| 9997 |
'instances can redefine and override methods. This allows ' |
| 9998 |
'individual\n' |
| 9999 |
'instances to acquire behaviors that differ from other ' |
| 10000 |
'instances of the\n' |
| 10001 |
'same class.\n' |
| 10002 |
'\n' |
| 10003 |
'The "property()" function is implemented as a data ' |
| 10004 |
'descriptor.\n' |
| 10005 |
'Accordingly, instances cannot override the behavior of a ' |
| 10006 |
'property.\n' |
| 10007 |
'\n' |
| 10008 |
'\n' |
| 10009 |
'__slots__\n' |
| 10010 |
'---------\n' |
| 10011 |
'\n' |
| 10012 |
'*__slots__* allow us to explicitly declare data members ' |
| 10013 |
'(like\n' |
| 10014 |
'properties) and deny the creation of "__dict__" and ' |
| 10015 |
'*__weakref__*\n' |
| 10016 |
'(unless explicitly declared in *__slots__* or available in a ' |
| 10017 |
'parent.)\n' |
| 10018 |
'\n' |
| 10019 |
'The space saved over using "__dict__" can be significant. ' |
| 10020 |
'Attribute\n' |
| 10021 |
'lookup speed can be significantly improved as well.\n' |
| 10022 |
'\n' |
| 10023 |
'object.__slots__\n' |
| 10024 |
'\n' |
| 10025 |
' This class variable can be assigned a string, iterable, ' |
| 10026 |
'or sequence\n' |
| 10027 |
' of strings with variable names used by instances. ' |
| 10028 |
'*__slots__*\n' |
| 10029 |
' reserves space for the declared variables and prevents ' |
| 10030 |
'the\n' |
| 10031 |
' automatic creation of "__dict__" and *__weakref__* for ' |
| 10032 |
'each\n' |
| 10033 |
' instance.\n' |
| 10034 |
'\n' |
| 10035 |
'Notes on using *__slots__*:\n' |
| 10036 |
'\n' |
| 10037 |
'* When inheriting from a class without *__slots__*, the ' |
| 10038 |
'"__dict__" and\n' |
| 10039 |
' *__weakref__* attribute of the instances will always be ' |
| 10040 |
'accessible.\n' |
| 10041 |
'\n' |
| 10042 |
'* Without a "__dict__" variable, instances cannot be ' |
| 10043 |
'assigned new\n' |
| 10044 |
' variables not listed in the *__slots__* definition. ' |
| 10045 |
'Attempts to\n' |
| 10046 |
' assign to an unlisted variable name raises ' |
| 10047 |
'"AttributeError". If\n' |
| 10048 |
' dynamic assignment of new variables is desired, then add\n' |
| 10049 |
' "\'__dict__\'" to the sequence of strings in the ' |
| 10050 |
'*__slots__*\n' |
| 10051 |
' declaration.\n' |
| 10052 |
'\n' |
| 10053 |
'* Without a *__weakref__* variable for each instance, ' |
| 10054 |
'classes defining\n' |
| 10055 |
' *__slots__* do not support "weak references" to its ' |
| 10056 |
'instances. If\n' |
| 10057 |
' weak reference support is needed, then add ' |
| 10058 |
'"\'__weakref__\'" to the\n' |
| 10059 |
' sequence of strings in the *__slots__* declaration.\n' |
| 10060 |
'\n' |
| 10061 |
'* *__slots__* are implemented at the class level by ' |
| 10062 |
'creating\n' |
| 10063 |
' descriptors for each variable name. As a result, class ' |
| 10064 |
'attributes\n' |
| 10065 |
' cannot be used to set default values for instance ' |
| 10066 |
'variables defined\n' |
| 10067 |
' by *__slots__*; otherwise, the class attribute would ' |
| 10068 |
'overwrite the\n' |
| 10069 |
' descriptor assignment.\n' |
| 10070 |
'\n' |
| 10071 |
'* The action of a *__slots__* declaration is not limited to ' |
| 10072 |
'the class\n' |
| 10073 |
' where it is defined. *__slots__* declared in parents are ' |
| 10074 |
'available\n' |
| 10075 |
' in child classes. However, child subclasses will get a ' |
| 10076 |
'"__dict__"\n' |
| 10077 |
' and *__weakref__* unless they also define *__slots__* ' |
| 10078 |
'(which should\n' |
| 10079 |
' only contain names of any *additional* slots).\n' |
| 10080 |
'\n' |
| 10081 |
'* If a class defines a slot also defined in a base class, ' |
| 10082 |
'the instance\n' |
| 10083 |
' variable defined by the base class slot is inaccessible ' |
| 10084 |
'(except by\n' |
| 10085 |
' retrieving its descriptor directly from the base class). ' |
| 10086 |
'This\n' |
| 10087 |
' renders the meaning of the program undefined. In the ' |
| 10088 |
'future, a\n' |
| 10089 |
' check may be added to prevent this.\n' |
| 10090 |
'\n' |
| 10091 |
'* "TypeError" will be raised if nonempty *__slots__* are ' |
| 10092 |
'defined for a\n' |
| 10093 |
' class derived from a ""variable-length" built-in type" ' |
| 10094 |
'such as\n' |
| 10095 |
' "int", "bytes", and "tuple".\n' |
| 10096 |
'\n' |
| 10097 |
'* Any non-string *iterable* may be assigned to *__slots__*.\n' |
| 10098 |
'\n' |
| 10099 |
'* If a "dictionary" is used to assign *__slots__*, the ' |
| 10100 |
'dictionary keys\n' |
| 10101 |
' will be used as the slot names. The values of the ' |
| 10102 |
'dictionary can be\n' |
| 10103 |
' used to provide per-attribute docstrings that will be ' |
| 10104 |
'recognised by\n' |
| 10105 |
' "inspect.getdoc()" and displayed in the output of ' |
| 10106 |
'"help()".\n' |
| 10107 |
'\n' |
| 10108 |
'* "__class__" assignment works only if both classes have the ' |
| 10109 |
'same\n' |
| 10110 |
' *__slots__*.\n' |
| 10111 |
'\n' |
| 10112 |
'* Multiple inheritance with multiple slotted parent classes ' |
| 10113 |
'can be\n' |
| 10114 |
' used, but only one parent is allowed to have attributes ' |
| 10115 |
'created by\n' |
| 10116 |
' slots (the other bases must have empty slot layouts) - ' |
| 10117 |
'violations\n' |
| 10118 |
' raise "TypeError".\n' |
| 10119 |
'\n' |
| 10120 |
'* If an *iterator* is used for *__slots__* then a ' |
| 10121 |
'*descriptor* is\n' |
| 10122 |
' created for each of the iterator’s values. However, the ' |
| 10123 |
'*__slots__*\n' |
| 10124 |
' attribute will be an empty iterator.\n' |
| 10125 |
'\n' |
| 10126 |
'\n' |
| 10127 |
'Customizing class creation\n' |
| 10128 |
'==========================\n' |
| 10129 |
'\n' |
| 10130 |
'Whenever a class inherits from another class, ' |
| 10131 |
'"__init_subclass__()" is\n' |
| 10132 |
'called on the parent class. This way, it is possible to ' |
| 10133 |
'write classes\n' |
| 10134 |
'which change the behavior of subclasses. This is closely ' |
| 10135 |
'related to\n' |
| 10136 |
'class decorators, but where class decorators only affect the ' |
| 10137 |
'specific\n' |
| 10138 |
'class they’re applied to, "__init_subclass__" solely applies ' |
| 10139 |
'to future\n' |
| 10140 |
'subclasses of the class defining the method.\n' |
| 10141 |
'\n' |
| 10142 |
'classmethod object.__init_subclass__(cls)\n' |
| 10143 |
'\n' |
| 10144 |
' This method is called whenever the containing class is ' |
| 10145 |
'subclassed.\n' |
| 10146 |
' *cls* is then the new subclass. If defined as a normal ' |
| 10147 |
'instance\n' |
| 10148 |
' method, this method is implicitly converted to a class ' |
| 10149 |
'method.\n' |
| 10150 |
'\n' |
| 10151 |
' Keyword arguments which are given to a new class are ' |
| 10152 |
'passed to the\n' |
| 10153 |
' parent class’s "__init_subclass__". For compatibility ' |
| 10154 |
'with other\n' |
| 10155 |
' classes using "__init_subclass__", one should take out ' |
| 10156 |
'the needed\n' |
| 10157 |
' keyword arguments and pass the others over to the base ' |
| 10158 |
'class, as\n' |
| 10159 |
' in:\n' |
| 10160 |
'\n' |
| 10161 |
' class Philosopher:\n' |
| 10162 |
' def __init_subclass__(cls, /, default_name, ' |
| 10163 |
'**kwargs):\n' |
| 10164 |
' super().__init_subclass__(**kwargs)\n' |
| 10165 |
' cls.default_name = default_name\n' |
| 10166 |
'\n' |
| 10167 |
' class AustralianPhilosopher(Philosopher, ' |
| 10168 |
'default_name="Bruce"):\n' |
| 10169 |
' pass\n' |
| 10170 |
'\n' |
| 10171 |
' The default implementation "object.__init_subclass__" ' |
| 10172 |
'does nothing,\n' |
| 10173 |
' but raises an error if it is called with any arguments.\n' |
| 10174 |
'\n' |
| 10175 |
' Note:\n' |
| 10176 |
'\n' |
| 10177 |
' The metaclass hint "metaclass" is consumed by the rest ' |
| 10178 |
'of the\n' |
| 10179 |
' type machinery, and is never passed to ' |
| 10180 |
'"__init_subclass__"\n' |
| 10181 |
' implementations. The actual metaclass (rather than the ' |
| 10182 |
'explicit\n' |
| 10183 |
' hint) can be accessed as "type(cls)".\n' |
| 10184 |
'\n' |
| 10185 |
' New in version 3.6.\n' |
| 10186 |
'\n' |
| 10187 |
'When a class is created, "type.__new__()" scans the class ' |
| 10188 |
'variables\n' |
| 10189 |
'and makes callbacks to those with a "__set_name__()" hook.\n' |
| 10190 |
'\n' |
| 10191 |
'object.__set_name__(self, owner, name)\n' |
| 10192 |
'\n' |
| 10193 |
' Automatically called at the time the owning class *owner* ' |
| 10194 |
'is\n' |
| 10195 |
' created. The object has been assigned to *name* in that ' |
| 10196 |
'class:\n' |
| 10197 |
'\n' |
| 10198 |
' class A:\n' |
| 10199 |
' x = C() # Automatically calls: x.__set_name__(A, ' |
| 10200 |
"'x')\n" |
| 10201 |
'\n' |
| 10202 |
' If the class variable is assigned after the class is ' |
| 10203 |
'created,\n' |
| 10204 |
' "__set_name__()" will not be called automatically. If ' |
| 10205 |
'needed,\n' |
| 10206 |
' "__set_name__()" can be called directly:\n' |
| 10207 |
'\n' |
| 10208 |
' class A:\n' |
| 10209 |
' pass\n' |
| 10210 |
'\n' |
| 10211 |
' c = C()\n' |
| 10212 |
' A.x = c # The hook is not called\n' |
| 10213 |
" c.__set_name__(A, 'x') # Manually invoke the hook\n" |
| 10214 |
'\n' |
| 10215 |
' See Creating the class object for more details.\n' |
| 10216 |
'\n' |
| 10217 |
' New in version 3.6.\n' |
| 10218 |
'\n' |
| 10219 |
'\n' |
| 10220 |
'Metaclasses\n' |
| 10221 |
'-----------\n' |
| 10222 |
'\n' |
| 10223 |
'By default, classes are constructed using "type()". The ' |
| 10224 |
'class body is\n' |
| 10225 |
'executed in a new namespace and the class name is bound ' |
| 10226 |
'locally to the\n' |
| 10227 |
'result of "type(name, bases, namespace)".\n' |
| 10228 |
'\n' |
| 10229 |
'The class creation process can be customized by passing the\n' |
| 10230 |
'"metaclass" keyword argument in the class definition line, ' |
| 10231 |
'or by\n' |
| 10232 |
'inheriting from an existing class that included such an ' |
| 10233 |
'argument. In\n' |
| 10234 |
'the following example, both "MyClass" and "MySubclass" are ' |
| 10235 |
'instances\n' |
| 10236 |
'of "Meta":\n' |
| 10237 |
'\n' |
| 10238 |
' class Meta(type):\n' |
| 10239 |
' pass\n' |
| 10240 |
'\n' |
| 10241 |
' class MyClass(metaclass=Meta):\n' |
| 10242 |
' pass\n' |
| 10243 |
'\n' |
| 10244 |
' class MySubclass(MyClass):\n' |
| 10245 |
' pass\n' |
| 10246 |
'\n' |
| 10247 |
'Any other keyword arguments that are specified in the class ' |
| 10248 |
'definition\n' |
| 10249 |
'are passed through to all metaclass operations described ' |
| 10250 |
'below.\n' |
| 10251 |
'\n' |
| 10252 |
'When a class definition is executed, the following steps ' |
| 10253 |
'occur:\n' |
| 10254 |
'\n' |
| 10255 |
'* MRO entries are resolved;\n' |
| 10256 |
'\n' |
| 10257 |
'* the appropriate metaclass is determined;\n' |
| 10258 |
'\n' |
| 10259 |
'* the class namespace is prepared;\n' |
| 10260 |
'\n' |
| 10261 |
'* the class body is executed;\n' |
| 10262 |
'\n' |
| 10263 |
'* the class object is created.\n' |
| 10264 |
'\n' |
| 10265 |
'\n' |
| 10266 |
'Resolving MRO entries\n' |
| 10267 |
'---------------------\n' |
| 10268 |
'\n' |
| 10269 |
'object.__mro_entries__(self, bases)\n' |
| 10270 |
'\n' |
| 10271 |
' If a base that appears in a class definition is not an ' |
| 10272 |
'instance of\n' |
| 10273 |
' "type", then an "__mro_entries__()" method is searched on ' |
| 10274 |
'the base.\n' |
| 10275 |
' If an "__mro_entries__()" method is found, the base is ' |
| 10276 |
'substituted\n' |
| 10277 |
' with the result of a call to "__mro_entries__()" when ' |
| 10278 |
'creating the\n' |
| 10279 |
' class. The method is called with the original bases tuple ' |
| 10280 |
'passed to\n' |
| 10281 |
' the *bases* parameter, and must return a tuple of classes ' |
| 10282 |
'that will\n' |
| 10283 |
' be used instead of the base. The returned tuple may be ' |
| 10284 |
'empty: in\n' |
| 10285 |
' these cases, the original base is ignored.\n' |
| 10286 |
'\n' |
| 10287 |
'See also:\n' |
| 10288 |
'\n' |
| 10289 |
' "types.resolve_bases()"\n' |
| 10290 |
' Dynamically resolve bases that are not instances of ' |
| 10291 |
'"type".\n' |
| 10292 |
'\n' |
| 10293 |
' **PEP 560**\n' |
| 10294 |
' Core support for typing module and generic types.\n' |
| 10295 |
'\n' |
| 10296 |
'\n' |
| 10297 |
'Determining the appropriate metaclass\n' |
| 10298 |
'-------------------------------------\n' |
| 10299 |
'\n' |
| 10300 |
'The appropriate metaclass for a class definition is ' |
| 10301 |
'determined as\n' |
| 10302 |
'follows:\n' |
| 10303 |
'\n' |
| 10304 |
'* if no bases and no explicit metaclass are given, then ' |
| 10305 |
'"type()" is\n' |
| 10306 |
' used;\n' |
| 10307 |
'\n' |
| 10308 |
'* if an explicit metaclass is given and it is *not* an ' |
| 10309 |
'instance of\n' |
| 10310 |
' "type()", then it is used directly as the metaclass;\n' |
| 10311 |
'\n' |
| 10312 |
'* if an instance of "type()" is given as the explicit ' |
| 10313 |
'metaclass, or\n' |
| 10314 |
' bases are defined, then the most derived metaclass is ' |
| 10315 |
'used.\n' |
| 10316 |
'\n' |
| 10317 |
'The most derived metaclass is selected from the explicitly ' |
| 10318 |
'specified\n' |
| 10319 |
'metaclass (if any) and the metaclasses (i.e. "type(cls)") of ' |
| 10320 |
'all\n' |
| 10321 |
'specified base classes. The most derived metaclass is one ' |
| 10322 |
'which is a\n' |
| 10323 |
'subtype of *all* of these candidate metaclasses. If none of ' |
| 10324 |
'the\n' |
| 10325 |
'candidate metaclasses meets that criterion, then the class ' |
| 10326 |
'definition\n' |
| 10327 |
'will fail with "TypeError".\n' |
| 10328 |
'\n' |
| 10329 |
'\n' |
| 10330 |
'Preparing the class namespace\n' |
| 10331 |
'-----------------------------\n' |
| 10332 |
'\n' |
| 10333 |
'Once the appropriate metaclass has been identified, then the ' |
| 10334 |
'class\n' |
| 10335 |
'namespace is prepared. If the metaclass has a "__prepare__" ' |
| 10336 |
'attribute,\n' |
| 10337 |
'it is called as "namespace = metaclass.__prepare__(name, ' |
| 10338 |
'bases,\n' |
| 10339 |
'**kwds)" (where the additional keyword arguments, if any, ' |
| 10340 |
'come from\n' |
| 10341 |
'the class definition). The "__prepare__" method should be ' |
| 10342 |
'implemented\n' |
| 10343 |
'as a "classmethod". The namespace returned by "__prepare__" ' |
| 10344 |
'is passed\n' |
| 10345 |
'in to "__new__", but when the final class object is created ' |
| 10346 |
'the\n' |
| 10347 |
'namespace is copied into a new "dict".\n' |
| 10348 |
'\n' |
| 10349 |
'If the metaclass has no "__prepare__" attribute, then the ' |
| 10350 |
'class\n' |
| 10351 |
'namespace is initialised as an empty ordered mapping.\n' |
| 10352 |
'\n' |
| 10353 |
'See also:\n' |
| 10354 |
'\n' |
| 10355 |
' **PEP 3115** - Metaclasses in Python 3000\n' |
| 10356 |
' Introduced the "__prepare__" namespace hook\n' |
| 10357 |
'\n' |
| 10358 |
'\n' |
| 10359 |
'Executing the class body\n' |
| 10360 |
'------------------------\n' |
| 10361 |
'\n' |
| 10362 |
'The class body is executed (approximately) as "exec(body, ' |
| 10363 |
'globals(),\n' |
| 10364 |
'namespace)". The key difference from a normal call to ' |
| 10365 |
'"exec()" is that\n' |
| 10366 |
'lexical scoping allows the class body (including any ' |
| 10367 |
'methods) to\n' |
| 10368 |
'reference names from the current and outer scopes when the ' |
| 10369 |
'class\n' |
| 10370 |
'definition occurs inside a function.\n' |
| 10371 |
'\n' |
| 10372 |
'However, even when the class definition occurs inside the ' |
| 10373 |
'function,\n' |
| 10374 |
'methods defined inside the class still cannot see names ' |
| 10375 |
'defined at the\n' |
| 10376 |
'class scope. Class variables must be accessed through the ' |
| 10377 |
'first\n' |
| 10378 |
'parameter of instance or class methods, or through the ' |
| 10379 |
'implicit\n' |
| 10380 |
'lexically scoped "__class__" reference described in the next ' |
| 10381 |
'section.\n' |
| 10382 |
'\n' |
| 10383 |
'\n' |
| 10384 |
'Creating the class object\n' |
| 10385 |
'-------------------------\n' |
| 10386 |
'\n' |
| 10387 |
'Once the class namespace has been populated by executing the ' |
| 10388 |
'class\n' |
| 10389 |
'body, the class object is created by calling ' |
| 10390 |
'"metaclass(name, bases,\n' |
| 10391 |
'namespace, **kwds)" (the additional keywords passed here are ' |
| 10392 |
'the same\n' |
| 10393 |
'as those passed to "__prepare__").\n' |
| 10394 |
'\n' |
| 10395 |
'This class object is the one that will be referenced by the ' |
| 10396 |
'zero-\n' |
| 10397 |
'argument form of "super()". "__class__" is an implicit ' |
| 10398 |
'closure\n' |
| 10399 |
'reference created by the compiler if any methods in a class ' |
| 10400 |
'body refer\n' |
| 10401 |
'to either "__class__" or "super". This allows the zero ' |
| 10402 |
'argument form\n' |
| 10403 |
'of "super()" to correctly identify the class being defined ' |
| 10404 |
'based on\n' |
| 10405 |
'lexical scoping, while the class or instance that was used ' |
| 10406 |
'to make the\n' |
| 10407 |
'current call is identified based on the first argument ' |
| 10408 |
'passed to the\n' |
| 10409 |
'method.\n' |
| 10410 |
'\n' |
| 10411 |
'**CPython implementation detail:** In CPython 3.6 and later, ' |
| 10412 |
'the\n' |
| 10413 |
'"__class__" cell is passed to the metaclass as a ' |
| 10414 |
'"__classcell__" entry\n' |
| 10415 |
'in the class namespace. If present, this must be propagated ' |
| 10416 |
'up to the\n' |
| 10417 |
'"type.__new__" call in order for the class to be ' |
| 10418 |
'initialised\n' |
| 10419 |
'correctly. Failing to do so will result in a "RuntimeError" ' |
| 10420 |
'in Python\n' |
| 10421 |
'3.8.\n' |
| 10422 |
'\n' |
| 10423 |
'When using the default metaclass "type", or any metaclass ' |
| 10424 |
'that\n' |
| 10425 |
'ultimately calls "type.__new__", the following additional\n' |
| 10426 |
'customization steps are invoked after creating the class ' |
| 10427 |
'object:\n' |
| 10428 |
'\n' |
| 10429 |
'1. The "type.__new__" method collects all of the attributes ' |
| 10430 |
'in the\n' |
| 10431 |
' class namespace that define a "__set_name__()" method;\n' |
| 10432 |
'\n' |
| 10433 |
'2. Those "__set_name__" methods are called with the class ' |
| 10434 |
'being\n' |
| 10435 |
' defined and the assigned name of that particular ' |
| 10436 |
'attribute;\n' |
| 10437 |
'\n' |
| 10438 |
'3. The "__init_subclass__()" hook is called on the immediate ' |
| 10439 |
'parent of\n' |
| 10440 |
' the new class in its method resolution order.\n' |
| 10441 |
'\n' |
| 10442 |
'After the class object is created, it is passed to the ' |
| 10443 |
'class\n' |
| 10444 |
'decorators included in the class definition (if any) and the ' |
| 10445 |
'resulting\n' |
| 10446 |
'object is bound in the local namespace as the defined ' |
| 10447 |
'class.\n' |
| 10448 |
'\n' |
| 10449 |
'When a new class is created by "type.__new__", the object ' |
| 10450 |
'provided as\n' |
| 10451 |
'the namespace parameter is copied to a new ordered mapping ' |
| 10452 |
'and the\n' |
| 10453 |
'original object is discarded. The new copy is wrapped in a ' |
| 10454 |
'read-only\n' |
| 10455 |
'proxy, which becomes the "__dict__" attribute of the class ' |
| 10456 |
'object.\n' |
| 10457 |
'\n' |
| 10458 |
'See also:\n' |
| 10459 |
'\n' |
| 10460 |
' **PEP 3135** - New super\n' |
| 10461 |
' Describes the implicit "__class__" closure reference\n' |
| 10462 |
'\n' |
| 10463 |
'\n' |
| 10464 |
'Uses for metaclasses\n' |
| 10465 |
'--------------------\n' |
| 10466 |
'\n' |
| 10467 |
'The potential uses for metaclasses are boundless. Some ideas ' |
| 10468 |
'that have\n' |
| 10469 |
'been explored include enum, logging, interface checking, ' |
| 10470 |
'automatic\n' |
| 10471 |
'delegation, automatic property creation, proxies, ' |
| 10472 |
'frameworks, and\n' |
| 10473 |
'automatic resource locking/synchronization.\n' |
| 10474 |
'\n' |
| 10475 |
'\n' |
| 10476 |
'Customizing instance and subclass checks\n' |
| 10477 |
'========================================\n' |
| 10478 |
'\n' |
| 10479 |
'The following methods are used to override the default ' |
| 10480 |
'behavior of the\n' |
| 10481 |
'"isinstance()" and "issubclass()" built-in functions.\n' |
| 10482 |
'\n' |
| 10483 |
'In particular, the metaclass "abc.ABCMeta" implements these ' |
| 10484 |
'methods in\n' |
| 10485 |
'order to allow the addition of Abstract Base Classes (ABCs) ' |
| 10486 |
'as\n' |
| 10487 |
'“virtual base classes” to any class or type (including ' |
| 10488 |
'built-in\n' |
| 10489 |
'types), including other ABCs.\n' |
| 10490 |
'\n' |
| 10491 |
'class.__instancecheck__(self, instance)\n' |
| 10492 |
'\n' |
| 10493 |
' Return true if *instance* should be considered a (direct ' |
| 10494 |
'or\n' |
| 10495 |
' indirect) instance of *class*. If defined, called to ' |
| 10496 |
'implement\n' |
| 10497 |
' "isinstance(instance, class)".\n' |
| 10498 |
'\n' |
| 10499 |
'class.__subclasscheck__(self, subclass)\n' |
| 10500 |
'\n' |
| 10501 |
' Return true if *subclass* should be considered a (direct ' |
| 10502 |
'or\n' |
| 10503 |
' indirect) subclass of *class*. If defined, called to ' |
| 10504 |
'implement\n' |
| 10505 |
' "issubclass(subclass, class)".\n' |
| 10506 |
'\n' |
| 10507 |
'Note that these methods are looked up on the type ' |
| 10508 |
'(metaclass) of a\n' |
| 10509 |
'class. They cannot be defined as class methods in the ' |
| 10510 |
'actual class.\n' |
| 10511 |
'This is consistent with the lookup of special methods that ' |
| 10512 |
'are called\n' |
| 10513 |
'on instances, only in this case the instance is itself a ' |
| 10514 |
'class.\n' |
| 10515 |
'\n' |
| 10516 |
'See also:\n' |
| 10517 |
'\n' |
| 10518 |
' **PEP 3119** - Introducing Abstract Base Classes\n' |
| 10519 |
' Includes the specification for customizing ' |
| 10520 |
'"isinstance()" and\n' |
| 10521 |
' "issubclass()" behavior through "__instancecheck__()" ' |
| 10522 |
'and\n' |
| 10523 |
' "__subclasscheck__()", with motivation for this ' |
| 10524 |
'functionality in\n' |
| 10525 |
' the context of adding Abstract Base Classes (see the ' |
| 10526 |
'"abc"\n' |
| 10527 |
' module) to the language.\n' |
| 10528 |
'\n' |
| 10529 |
'\n' |
| 10530 |
'Emulating generic types\n' |
| 10531 |
'=======================\n' |
| 10532 |
'\n' |
| 10533 |
'When using *type annotations*, it is often useful to ' |
| 10534 |
'*parameterize* a\n' |
| 10535 |
'*generic type* using Python’s square-brackets notation. For ' |
| 10536 |
'example,\n' |
| 10537 |
'the annotation "list[int]" might be used to signify a "list" ' |
| 10538 |
'in which\n' |
| 10539 |
'all the elements are of type "int".\n' |
| 10540 |
'\n' |
| 10541 |
'See also:\n' |
| 10542 |
'\n' |
| 10543 |
' **PEP 484** - Type Hints\n' |
| 10544 |
' Introducing Python’s framework for type annotations\n' |
| 10545 |
'\n' |
| 10546 |
' Generic Alias Types\n' |
| 10547 |
' Documentation for objects representing parameterized ' |
| 10548 |
'generic\n' |
| 10549 |
' classes\n' |
| 10550 |
'\n' |
| 10551 |
' Generics, user-defined generics and "typing.Generic"\n' |
| 10552 |
' Documentation on how to implement generic classes that ' |
| 10553 |
'can be\n' |
| 10554 |
' parameterized at runtime and understood by static ' |
| 10555 |
'type-checkers.\n' |
| 10556 |
'\n' |
| 10557 |
'A class can *generally* only be parameterized if it defines ' |
| 10558 |
'the\n' |
| 10559 |
'special class method "__class_getitem__()".\n' |
| 10560 |
'\n' |
| 10561 |
'classmethod object.__class_getitem__(cls, key)\n' |
| 10562 |
'\n' |
| 10563 |
' Return an object representing the specialization of a ' |
| 10564 |
'generic class\n' |
| 10565 |
' by type arguments found in *key*.\n' |
| 10566 |
'\n' |
| 10567 |
' When defined on a class, "__class_getitem__()" is ' |
| 10568 |
'automatically a\n' |
| 10569 |
' class method. As such, there is no need for it to be ' |
| 10570 |
'decorated with\n' |
| 10571 |
' "@classmethod" when it is defined.\n' |
| 10572 |
'\n' |
| 10573 |
'\n' |
| 10574 |
'The purpose of *__class_getitem__*\n' |
| 10575 |
'----------------------------------\n' |
| 10576 |
'\n' |
| 10577 |
'The purpose of "__class_getitem__()" is to allow runtime\n' |
| 10578 |
'parameterization of standard-library generic classes in ' |
| 10579 |
'order to more\n' |
| 10580 |
'easily apply *type hints* to these classes.\n' |
| 10581 |
'\n' |
| 10582 |
'To implement custom generic classes that can be ' |
| 10583 |
'parameterized at\n' |
| 10584 |
'runtime and understood by static type-checkers, users should ' |
| 10585 |
'either\n' |
| 10586 |
'inherit from a standard library class that already ' |
| 10587 |
'implements\n' |
| 10588 |
'"__class_getitem__()", or inherit from "typing.Generic", ' |
| 10589 |
'which has its\n' |
| 10590 |
'own implementation of "__class_getitem__()".\n' |
| 10591 |
'\n' |
| 10592 |
'Custom implementations of "__class_getitem__()" on classes ' |
| 10593 |
'defined\n' |
| 10594 |
'outside of the standard library may not be understood by ' |
| 10595 |
'third-party\n' |
| 10596 |
'type-checkers such as mypy. Using "__class_getitem__()" on ' |
| 10597 |
'any class\n' |
| 10598 |
'for purposes other than type hinting is discouraged.\n' |
| 10599 |
'\n' |
| 10600 |
'\n' |
| 10601 |
'*__class_getitem__* versus *__getitem__*\n' |
| 10602 |
'----------------------------------------\n' |
| 10603 |
'\n' |
| 10604 |
'Usually, the subscription of an object using square brackets ' |
| 10605 |
'will call\n' |
| 10606 |
'the "__getitem__()" instance method defined on the object’s ' |
| 10607 |
'class.\n' |
| 10608 |
'However, if the object being subscribed is itself a class, ' |
| 10609 |
'the class\n' |
| 10610 |
'method "__class_getitem__()" may be called instead.\n' |
| 10611 |
'"__class_getitem__()" should return a GenericAlias object if ' |
| 10612 |
'it is\n' |
| 10613 |
'properly defined.\n' |
| 10614 |
'\n' |
| 10615 |
'Presented with the *expression* "obj[x]", the Python ' |
| 10616 |
'interpreter\n' |
| 10617 |
'follows something like the following process to decide ' |
| 10618 |
'whether\n' |
| 10619 |
'"__getitem__()" or "__class_getitem__()" should be called:\n' |
| 10620 |
'\n' |
| 10621 |
' from inspect import isclass\n' |
| 10622 |
'\n' |
| 10623 |
' def subscribe(obj, x):\n' |
| 10624 |
' """Return the result of the expression \'obj[x]\'"""\n' |
| 10625 |
'\n' |
| 10626 |
' class_of_obj = type(obj)\n' |
| 10627 |
'\n' |
| 10628 |
' # If the class of obj defines __getitem__,\n' |
| 10629 |
' # call class_of_obj.__getitem__(obj, x)\n' |
| 10630 |
" if hasattr(class_of_obj, '__getitem__'):\n" |
| 10631 |
' return class_of_obj.__getitem__(obj, x)\n' |
| 10632 |
'\n' |
| 10633 |
' # Else, if obj is a class and defines ' |
| 10634 |
'__class_getitem__,\n' |
| 10635 |
' # call obj.__class_getitem__(x)\n' |
| 10636 |
' elif isclass(obj) and hasattr(obj, ' |
| 10637 |
"'__class_getitem__'):\n" |
| 10638 |
' return obj.__class_getitem__(x)\n' |
| 10639 |
'\n' |
| 10640 |
' # Else, raise an exception\n' |
| 10641 |
' else:\n' |
| 10642 |
' raise TypeError(\n' |
| 10643 |
' f"\'{class_of_obj.__name__}\' object is not ' |
| 10644 |
'subscriptable"\n' |
| 10645 |
' )\n' |
| 10646 |
'\n' |
| 10647 |
'In Python, all classes are themselves instances of other ' |
| 10648 |
'classes. The\n' |
| 10649 |
'class of a class is known as that class’s *metaclass*, and ' |
| 10650 |
'most\n' |
| 10651 |
'classes have the "type" class as their metaclass. "type" ' |
| 10652 |
'does not\n' |
| 10653 |
'define "__getitem__()", meaning that expressions such as ' |
| 10654 |
'"list[int]",\n' |
| 10655 |
'"dict[str, float]" and "tuple[str, bytes]" all result in\n' |
| 10656 |
'"__class_getitem__()" being called:\n' |
| 10657 |
'\n' |
| 10658 |
' >>> # list has class "type" as its metaclass, like most ' |
| 10659 |
'classes:\n' |
| 10660 |
' >>> type(list)\n' |
| 10661 |
" <class 'type'>\n" |
| 10662 |
' >>> type(dict) == type(list) == type(tuple) == type(str) ' |
| 10663 |
'== type(bytes)\n' |
| 10664 |
' True\n' |
| 10665 |
' >>> # "list[int]" calls "list.__class_getitem__(int)"\n' |
| 10666 |
' >>> list[int]\n' |
| 10667 |
' list[int]\n' |
| 10668 |
' >>> # list.__class_getitem__ returns a GenericAlias ' |
| 10669 |
'object:\n' |
| 10670 |
' >>> type(list[int])\n' |
| 10671 |
" <class 'types.GenericAlias'>\n" |
| 10672 |
'\n' |
| 10673 |
'However, if a class has a custom metaclass that defines\n' |
| 10674 |
'"__getitem__()", subscribing the class may result in ' |
| 10675 |
'different\n' |
| 10676 |
'behaviour. An example of this can be found in the "enum" ' |
| 10677 |
'module:\n' |
| 10678 |
'\n' |
| 10679 |
' >>> from enum import Enum\n' |
| 10680 |
' >>> class Menu(Enum):\n' |
| 10681 |
' ... """A breakfast menu"""\n' |
| 10682 |
" ... SPAM = 'spam'\n" |
| 10683 |
" ... BACON = 'bacon'\n" |
| 10684 |
' ...\n' |
| 10685 |
' >>> # Enum classes have a custom metaclass:\n' |
| 10686 |
' >>> type(Menu)\n' |
| 10687 |
" <class 'enum.EnumMeta'>\n" |
| 10688 |
' >>> # EnumMeta defines __getitem__,\n' |
| 10689 |
' >>> # so __class_getitem__ is not called,\n' |
| 10690 |
' >>> # and the result is not a GenericAlias object:\n' |
| 10691 |
" >>> Menu['SPAM']\n" |
| 10692 |
" <Menu.SPAM: 'spam'>\n" |
| 10693 |
" >>> type(Menu['SPAM'])\n" |
| 10694 |
" <enum 'Menu'>\n" |
| 10695 |
'\n' |
| 10696 |
'See also:\n' |
| 10697 |
'\n' |
| 10698 |
' **PEP 560** - Core Support for typing module and generic ' |
| 10699 |
'types\n' |
| 10700 |
' Introducing "__class_getitem__()", and outlining when ' |
| 10701 |
'a\n' |
| 10702 |
' subscription results in "__class_getitem__()" being ' |
| 10703 |
'called\n' |
| 10704 |
' instead of "__getitem__()"\n' |
| 10705 |
'\n' |
| 10706 |
'\n' |
| 10707 |
'Emulating callable objects\n' |
| 10708 |
'==========================\n' |
| 10709 |
'\n' |
| 10710 |
'object.__call__(self[, args...])\n' |
| 10711 |
'\n' |
| 10712 |
' Called when the instance is “called” as a function; if ' |
| 10713 |
'this method\n' |
| 10714 |
' is defined, "x(arg1, arg2, ...)" roughly translates to\n' |
| 10715 |
' "type(x).__call__(x, arg1, ...)".\n' |
| 10716 |
'\n' |
| 10717 |
'\n' |
| 10718 |
'Emulating container types\n' |
| 10719 |
'=========================\n' |
| 10720 |
'\n' |
| 10721 |
'The following methods can be defined to implement container ' |
| 10722 |
'objects.\n' |
| 10723 |
'Containers usually are *sequences* (such as "lists" or ' |
| 10724 |
'"tuples") or\n' |
| 10725 |
'*mappings* (like "dictionaries"), but can represent other ' |
| 10726 |
'containers\n' |
| 10727 |
'as well. The first set of methods is used either to emulate ' |
| 10728 |
'a\n' |
| 10729 |
'sequence or to emulate a mapping; the difference is that for ' |
| 10730 |
'a\n' |
| 10731 |
'sequence, the allowable keys should be the integers *k* for ' |
| 10732 |
'which "0\n' |
| 10733 |
'<= k < N" where *N* is the length of the sequence, or ' |
| 10734 |
'"slice" objects,\n' |
| 10735 |
'which define a range of items. It is also recommended that ' |
| 10736 |
'mappings\n' |
| 10737 |
'provide the methods "keys()", "values()", "items()", ' |
| 10738 |
'"get()",\n' |
| 10739 |
'"clear()", "setdefault()", "pop()", "popitem()", "copy()", ' |
| 10740 |
'and\n' |
| 10741 |
'"update()" behaving similar to those for Python’s standard\n' |
| 10742 |
'"dictionary" objects. The "collections.abc" module provides ' |
| 10743 |
'a\n' |
| 10744 |
'"MutableMapping" *abstract base class* to help create those ' |
| 10745 |
'methods\n' |
| 10746 |
'from a base set of "__getitem__()", "__setitem__()", ' |
| 10747 |
'"__delitem__()",\n' |
| 10748 |
'and "keys()". Mutable sequences should provide methods ' |
| 10749 |
'"append()",\n' |
| 10750 |
'"count()", "index()", "extend()", "insert()", "pop()", ' |
| 10751 |
'"remove()",\n' |
| 10752 |
'"reverse()" and "sort()", like Python standard "list" ' |
| 10753 |
'objects.\n' |
| 10754 |
'Finally, sequence types should implement addition (meaning\n' |
| 10755 |
'concatenation) and multiplication (meaning repetition) by ' |
| 10756 |
'defining the\n' |
| 10757 |
'methods "__add__()", "__radd__()", "__iadd__()", ' |
| 10758 |
'"__mul__()",\n' |
| 10759 |
'"__rmul__()" and "__imul__()" described below; they should ' |
| 10760 |
'not define\n' |
| 10761 |
'other numerical operators. It is recommended that both ' |
| 10762 |
'mappings and\n' |
| 10763 |
'sequences implement the "__contains__()" method to allow ' |
| 10764 |
'efficient use\n' |
| 10765 |
'of the "in" operator; for mappings, "in" should search the ' |
| 10766 |
'mapping’s\n' |
| 10767 |
'keys; for sequences, it should search through the values. ' |
| 10768 |
'It is\n' |
| 10769 |
'further recommended that both mappings and sequences ' |
| 10770 |
'implement the\n' |
| 10771 |
'"__iter__()" method to allow efficient iteration through ' |
| 10772 |
'the\n' |
| 10773 |
'container; for mappings, "__iter__()" should iterate through ' |
| 10774 |
'the\n' |
| 10775 |
'object’s keys; for sequences, it should iterate through the ' |
| 10776 |
'values.\n' |
| 10777 |
'\n' |
| 10778 |
'object.__len__(self)\n' |
| 10779 |
'\n' |
| 10780 |
' Called to implement the built-in function "len()". ' |
| 10781 |
'Should return\n' |
| 10782 |
' the length of the object, an integer ">=" 0. Also, an ' |
| 10783 |
'object that\n' |
| 10784 |
' doesn’t define a "__bool__()" method and whose ' |
| 10785 |
'"__len__()" method\n' |
| 10786 |
' returns zero is considered to be false in a Boolean ' |
| 10787 |
'context.\n' |
| 10788 |
'\n' |
| 10789 |
' **CPython implementation detail:** In CPython, the length ' |
| 10790 |
'is\n' |
| 10791 |
' required to be at most "sys.maxsize". If the length is ' |
| 10792 |
'larger than\n' |
| 10793 |
' "sys.maxsize" some features (such as "len()") may raise\n' |
| 10794 |
' "OverflowError". To prevent raising "OverflowError" by ' |
| 10795 |
'truth value\n' |
| 10796 |
' testing, an object must define a "__bool__()" method.\n' |
| 10797 |
'\n' |
| 10798 |
'object.__length_hint__(self)\n' |
| 10799 |
'\n' |
| 10800 |
' Called to implement "operator.length_hint()". Should ' |
| 10801 |
'return an\n' |
| 10802 |
' estimated length for the object (which may be greater or ' |
| 10803 |
'less than\n' |
| 10804 |
' the actual length). The length must be an integer ">=" 0. ' |
| 10805 |
'The\n' |
| 10806 |
' return value may also be "NotImplemented", which is ' |
| 10807 |
'treated the\n' |
| 10808 |
' same as if the "__length_hint__" method didn’t exist at ' |
| 10809 |
'all. This\n' |
| 10810 |
' method is purely an optimization and is never required ' |
| 10811 |
'for\n' |
| 10812 |
' correctness.\n' |
| 10813 |
'\n' |
| 10814 |
' New in version 3.4.\n' |
| 10815 |
'\n' |
| 10816 |
'Note:\n' |
| 10817 |
'\n' |
| 10818 |
' Slicing is done exclusively with the following three ' |
| 10819 |
'methods. A\n' |
| 10820 |
' call like\n' |
| 10821 |
'\n' |
| 10822 |
' a[1:2] = b\n' |
| 10823 |
'\n' |
| 10824 |
' is translated to\n' |
| 10825 |
'\n' |
| 10826 |
' a[slice(1, 2, None)] = b\n' |
| 10827 |
'\n' |
| 10828 |
' and so forth. Missing slice items are always filled in ' |
| 10829 |
'with "None".\n' |
| 10830 |
'\n' |
| 10831 |
'object.__getitem__(self, key)\n' |
| 10832 |
'\n' |
| 10833 |
' Called to implement evaluation of "self[key]". For ' |
| 10834 |
'*sequence*\n' |
| 10835 |
' types, the accepted keys should be integers. Optionally, ' |
| 10836 |
'they may\n' |
| 10837 |
' support "slice" objects as well. Negative index support ' |
| 10838 |
'is also\n' |
| 10839 |
' optional. If *key* is of an inappropriate type, ' |
| 10840 |
'"TypeError" may be\n' |
| 10841 |
' raised; if *key* is a value outside the set of indexes ' |
| 10842 |
'for the\n' |
| 10843 |
' sequence (after any special interpretation of negative ' |
| 10844 |
'values),\n' |
| 10845 |
' "IndexError" should be raised. For *mapping* types, if ' |
| 10846 |
'*key* is\n' |
| 10847 |
' missing (not in the container), "KeyError" should be ' |
| 10848 |
'raised.\n' |
| 10849 |
'\n' |
| 10850 |
' Note:\n' |
| 10851 |
'\n' |
| 10852 |
' "for" loops expect that an "IndexError" will be raised ' |
| 10853 |
'for\n' |
| 10854 |
' illegal indexes to allow proper detection of the end of ' |
| 10855 |
'the\n' |
| 10856 |
' sequence.\n' |
| 10857 |
'\n' |
| 10858 |
' Note:\n' |
| 10859 |
'\n' |
| 10860 |
' When subscripting a *class*, the special class method\n' |
| 10861 |
' "__class_getitem__()" may be called instead of ' |
| 10862 |
'"__getitem__()".\n' |
| 10863 |
' See __class_getitem__ versus __getitem__ for more ' |
| 10864 |
'details.\n' |
| 10865 |
'\n' |
| 10866 |
'object.__setitem__(self, key, value)\n' |
| 10867 |
'\n' |
| 10868 |
' Called to implement assignment to "self[key]". Same note ' |
| 10869 |
'as for\n' |
| 10870 |
' "__getitem__()". This should only be implemented for ' |
| 10871 |
'mappings if\n' |
| 10872 |
' the objects support changes to the values for keys, or if ' |
| 10873 |
'new keys\n' |
| 10874 |
' can be added, or for sequences if elements can be ' |
| 10875 |
'replaced. The\n' |
| 10876 |
' same exceptions should be raised for improper *key* ' |
| 10877 |
'values as for\n' |
| 10878 |
' the "__getitem__()" method.\n' |
| 10879 |
'\n' |
| 10880 |
'object.__delitem__(self, key)\n' |
| 10881 |
'\n' |
| 10882 |
' Called to implement deletion of "self[key]". Same note ' |
| 10883 |
'as for\n' |
| 10884 |
' "__getitem__()". This should only be implemented for ' |
| 10885 |
'mappings if\n' |
| 10886 |
' the objects support removal of keys, or for sequences if ' |
| 10887 |
'elements\n' |
| 10888 |
' can be removed from the sequence. The same exceptions ' |
| 10889 |
'should be\n' |
| 10890 |
' raised for improper *key* values as for the ' |
| 10891 |
'"__getitem__()" method.\n' |
| 10892 |
'\n' |
| 10893 |
'object.__missing__(self, key)\n' |
| 10894 |
'\n' |
| 10895 |
' Called by "dict"."__getitem__()" to implement "self[key]" ' |
| 10896 |
'for dict\n' |
| 10897 |
' subclasses when key is not in the dictionary.\n' |
| 10898 |
'\n' |
| 10899 |
'object.__iter__(self)\n' |
| 10900 |
'\n' |
| 10901 |
' This method is called when an *iterator* is required for ' |
| 10902 |
'a\n' |
| 10903 |
' container. This method should return a new iterator ' |
| 10904 |
'object that can\n' |
| 10905 |
' iterate over all the objects in the container. For ' |
| 10906 |
'mappings, it\n' |
| 10907 |
' should iterate over the keys of the container.\n' |
| 10908 |
'\n' |
| 10909 |
'object.__reversed__(self)\n' |
| 10910 |
'\n' |
| 10911 |
' Called (if present) by the "reversed()" built-in to ' |
| 10912 |
'implement\n' |
| 10913 |
' reverse iteration. It should return a new iterator ' |
| 10914 |
'object that\n' |
| 10915 |
' iterates over all the objects in the container in reverse ' |
| 10916 |
'order.\n' |
| 10917 |
'\n' |
| 10918 |
' If the "__reversed__()" method is not provided, the ' |
| 10919 |
'"reversed()"\n' |
| 10920 |
' built-in will fall back to using the sequence protocol ' |
| 10921 |
'("__len__()"\n' |
| 10922 |
' and "__getitem__()"). Objects that support the sequence ' |
| 10923 |
'protocol\n' |
| 10924 |
' should only provide "__reversed__()" if they can provide ' |
| 10925 |
'an\n' |
| 10926 |
' implementation that is more efficient than the one ' |
| 10927 |
'provided by\n' |
| 10928 |
' "reversed()".\n' |
| 10929 |
'\n' |
| 10930 |
'The membership test operators ("in" and "not in") are ' |
| 10931 |
'normally\n' |
| 10932 |
'implemented as an iteration through a container. However, ' |
| 10933 |
'container\n' |
| 10934 |
'objects can supply the following special method with a more ' |
| 10935 |
'efficient\n' |
| 10936 |
'implementation, which also does not require the object be ' |
| 10937 |
'iterable.\n' |
| 10938 |
'\n' |
| 10939 |
'object.__contains__(self, item)\n' |
| 10940 |
'\n' |
| 10941 |
' Called to implement membership test operators. Should ' |
| 10942 |
'return true\n' |
| 10943 |
' if *item* is in *self*, false otherwise. For mapping ' |
| 10944 |
'objects, this\n' |
| 10945 |
' should consider the keys of the mapping rather than the ' |
| 10946 |
'values or\n' |
| 10947 |
' the key-item pairs.\n' |
| 10948 |
'\n' |
| 10949 |
' For objects that don’t define "__contains__()", the ' |
| 10950 |
'membership test\n' |
| 10951 |
' first tries iteration via "__iter__()", then the old ' |
| 10952 |
'sequence\n' |
| 10953 |
' iteration protocol via "__getitem__()", see this section ' |
| 10954 |
'in the\n' |
| 10955 |
' language reference.\n' |
| 10956 |
'\n' |
| 10957 |
'\n' |
| 10958 |
'Emulating numeric types\n' |
| 10959 |
'=======================\n' |
| 10960 |
'\n' |
| 10961 |
'The following methods can be defined to emulate numeric ' |
| 10962 |
'objects.\n' |
| 10963 |
'Methods corresponding to operations that are not supported ' |
| 10964 |
'by the\n' |
| 10965 |
'particular kind of number implemented (e.g., bitwise ' |
| 10966 |
'operations for\n' |
| 10967 |
'non-integral numbers) should be left undefined.\n' |
| 10968 |
'\n' |
| 10969 |
'object.__add__(self, other)\n' |
| 10970 |
'object.__sub__(self, other)\n' |
| 10971 |
'object.__mul__(self, other)\n' |
| 10972 |
'object.__matmul__(self, other)\n' |
| 10973 |
'object.__truediv__(self, other)\n' |
| 10974 |
'object.__floordiv__(self, other)\n' |
| 10975 |
'object.__mod__(self, other)\n' |
| 10976 |
'object.__divmod__(self, other)\n' |
| 10977 |
'object.__pow__(self, other[, modulo])\n' |
| 10978 |
'object.__lshift__(self, other)\n' |
| 10979 |
'object.__rshift__(self, other)\n' |
| 10980 |
'object.__and__(self, other)\n' |
| 10981 |
'object.__xor__(self, other)\n' |
| 10982 |
'object.__or__(self, other)\n' |
| 10983 |
'\n' |
| 10984 |
' These methods are called to implement the binary ' |
| 10985 |
'arithmetic\n' |
| 10986 |
' operations ("+", "-", "*", "@", "/", "//", "%", ' |
| 10987 |
'"divmod()",\n' |
| 10988 |
' "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, ' |
| 10989 |
'to\n' |
| 10990 |
' evaluate the expression "x + y", where *x* is an instance ' |
| 10991 |
'of a\n' |
| 10992 |
' class that has an "__add__()" method, "type(x).__add__(x, ' |
| 10993 |
'y)" is\n' |
| 10994 |
' called. The "__divmod__()" method should be the ' |
| 10995 |
'equivalent to\n' |
| 10996 |
' using "__floordiv__()" and "__mod__()"; it should not be ' |
| 10997 |
'related to\n' |
| 10998 |
' "__truediv__()". Note that "__pow__()" should be defined ' |
| 10999 |
'to accept\n' |
| 11000 |
' an optional third argument if the ternary version of the ' |
| 11001 |
'built-in\n' |
| 11002 |
' "pow()" function is to be supported.\n' |
| 11003 |
'\n' |
| 11004 |
' If one of those methods does not support the operation ' |
| 11005 |
'with the\n' |
| 11006 |
' supplied arguments, it should return "NotImplemented".\n' |
| 11007 |
'\n' |
| 11008 |
'object.__radd__(self, other)\n' |
| 11009 |
'object.__rsub__(self, other)\n' |
| 11010 |
'object.__rmul__(self, other)\n' |
| 11011 |
'object.__rmatmul__(self, other)\n' |
| 11012 |
'object.__rtruediv__(self, other)\n' |
| 11013 |
'object.__rfloordiv__(self, other)\n' |
| 11014 |
'object.__rmod__(self, other)\n' |
| 11015 |
'object.__rdivmod__(self, other)\n' |
| 11016 |
'object.__rpow__(self, other[, modulo])\n' |
| 11017 |
'object.__rlshift__(self, other)\n' |
| 11018 |
'object.__rrshift__(self, other)\n' |
| 11019 |
'object.__rand__(self, other)\n' |
| 11020 |
'object.__rxor__(self, other)\n' |
| 11021 |
'object.__ror__(self, other)\n' |
| 11022 |
'\n' |
| 11023 |
' These methods are called to implement the binary ' |
| 11024 |
'arithmetic\n' |
| 11025 |
' operations ("+", "-", "*", "@", "/", "//", "%", ' |
| 11026 |
'"divmod()",\n' |
| 11027 |
' "pow()", "**", "<<", ">>", "&", "^", "|") with reflected ' |
| 11028 |
'(swapped)\n' |
| 11029 |
' operands. These functions are only called if the left ' |
| 11030 |
'operand does\n' |
| 11031 |
' not support the corresponding operation [3] and the ' |
| 11032 |
'operands are of\n' |
| 11033 |
' different types. [4] For instance, to evaluate the ' |
| 11034 |
'expression "x -\n' |
| 11035 |
' y", where *y* is an instance of a class that has an ' |
| 11036 |
'"__rsub__()"\n' |
| 11037 |
' method, "type(y).__rsub__(y, x)" is called if ' |
| 11038 |
'"type(x).__sub__(x,\n' |
| 11039 |
' y)" returns "NotImplemented".\n' |
| 11040 |
'\n' |
| 11041 |
' Note that ternary "pow()" will not try calling ' |
| 11042 |
'"__rpow__()" (the\n' |
| 11043 |
' coercion rules would become too complicated).\n' |
| 11044 |
'\n' |
| 11045 |
' Note:\n' |
| 11046 |
'\n' |
| 11047 |
' If the right operand’s type is a subclass of the left ' |
| 11048 |
'operand’s\n' |
| 11049 |
' type and that subclass provides a different ' |
| 11050 |
'implementation of the\n' |
| 11051 |
' reflected method for the operation, this method will be ' |
| 11052 |
'called\n' |
| 11053 |
' before the left operand’s non-reflected method. This ' |
| 11054 |
'behavior\n' |
| 11055 |
' allows subclasses to override their ancestors’ ' |
| 11056 |
'operations.\n' |
| 11057 |
'\n' |
| 11058 |
'object.__iadd__(self, other)\n' |
| 11059 |
'object.__isub__(self, other)\n' |
| 11060 |
'object.__imul__(self, other)\n' |
| 11061 |
'object.__imatmul__(self, other)\n' |
| 11062 |
'object.__itruediv__(self, other)\n' |
| 11063 |
'object.__ifloordiv__(self, other)\n' |
| 11064 |
'object.__imod__(self, other)\n' |
| 11065 |
'object.__ipow__(self, other[, modulo])\n' |
| 11066 |
'object.__ilshift__(self, other)\n' |
| 11067 |
'object.__irshift__(self, other)\n' |
| 11068 |
'object.__iand__(self, other)\n' |
| 11069 |
'object.__ixor__(self, other)\n' |
| 11070 |
'object.__ior__(self, other)\n' |
| 11071 |
'\n' |
| 11072 |
' These methods are called to implement the augmented ' |
| 11073 |
'arithmetic\n' |
| 11074 |
' assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", ' |
| 11075 |
'"**=",\n' |
| 11076 |
' "<<=", ">>=", "&=", "^=", "|="). These methods should ' |
| 11077 |
'attempt to\n' |
| 11078 |
' do the operation in-place (modifying *self*) and return ' |
| 11079 |
'the result\n' |
| 11080 |
' (which could be, but does not have to be, *self*). If a ' |
| 11081 |
'specific\n' |
| 11082 |
' method is not defined, or if that method returns ' |
| 11083 |
'"NotImplemented",\n' |
| 11084 |
' the augmented assignment falls back to the normal ' |
| 11085 |
'methods. For\n' |
| 11086 |
' instance, if *x* is an instance of a class with an ' |
| 11087 |
'"__iadd__()"\n' |
| 11088 |
' method, "x += y" is equivalent to "x = x.__iadd__(y)" . ' |
| 11089 |
'If\n' |
| 11090 |
' "__iadd__()" does not exist, or if "x.__iadd__(y)" ' |
| 11091 |
'returns\n' |
| 11092 |
' "NotImplemented", "x.__add__(y)" and "y.__radd__(x)" are\n' |
| 11093 |
' considered, as with the evaluation of "x + y". In ' |
| 11094 |
'certain\n' |
| 11095 |
' situations, augmented assignment can result in unexpected ' |
| 11096 |
'errors\n' |
| 11097 |
' (see Why does a_tuple[i] += [‘item’] raise an exception ' |
| 11098 |
'when the\n' |
| 11099 |
' addition works?), but this behavior is in fact part of ' |
| 11100 |
'the data\n' |
| 11101 |
' model.\n' |
| 11102 |
'\n' |
| 11103 |
'object.__neg__(self)\n' |
| 11104 |
'object.__pos__(self)\n' |
| 11105 |
'object.__abs__(self)\n' |
| 11106 |
'object.__invert__(self)\n' |
| 11107 |
'\n' |
| 11108 |
' Called to implement the unary arithmetic operations ("-", ' |
| 11109 |
'"+",\n' |
| 11110 |
' "abs()" and "~").\n' |
| 11111 |
'\n' |
| 11112 |
'object.__complex__(self)\n' |
| 11113 |
'object.__int__(self)\n' |
| 11114 |
'object.__float__(self)\n' |
| 11115 |
'\n' |
| 11116 |
' Called to implement the built-in functions "complex()", ' |
| 11117 |
'"int()" and\n' |
| 11118 |
' "float()". Should return a value of the appropriate ' |
| 11119 |
'type.\n' |
| 11120 |
'\n' |
| 11121 |
'object.__index__(self)\n' |
| 11122 |
'\n' |
| 11123 |
' Called to implement "operator.index()", and whenever ' |
| 11124 |
'Python needs\n' |
| 11125 |
' to losslessly convert the numeric object to an integer ' |
| 11126 |
'object (such\n' |
| 11127 |
' as in slicing, or in the built-in "bin()", "hex()" and ' |
| 11128 |
'"oct()"\n' |
| 11129 |
' functions). Presence of this method indicates that the ' |
| 11130 |
'numeric\n' |
| 11131 |
' object is an integer type. Must return an integer.\n' |
| 11132 |
'\n' |
| 11133 |
' If "__int__()", "__float__()" and "__complex__()" are not ' |
| 11134 |
'defined\n' |
| 11135 |
' then corresponding built-in functions "int()", "float()" ' |
| 11136 |
'and\n' |
| 11137 |
' "complex()" fall back to "__index__()".\n' |
| 11138 |
'\n' |
| 11139 |
'object.__round__(self[, ndigits])\n' |
| 11140 |
'object.__trunc__(self)\n' |
| 11141 |
'object.__floor__(self)\n' |
| 11142 |
'object.__ceil__(self)\n' |
| 11143 |
'\n' |
| 11144 |
' Called to implement the built-in function "round()" and ' |
| 11145 |
'"math"\n' |
| 11146 |
' functions "trunc()", "floor()" and "ceil()". Unless ' |
| 11147 |
'*ndigits* is\n' |
| 11148 |
' passed to "__round__()" all these methods should return ' |
| 11149 |
'the value\n' |
| 11150 |
' of the object truncated to an "Integral" (typically an ' |
| 11151 |
'"int").\n' |
| 11152 |
'\n' |
| 11153 |
' The built-in function "int()" falls back to "__trunc__()" ' |
| 11154 |
'if\n' |
| 11155 |
' neither "__int__()" nor "__index__()" is defined.\n' |
| 11156 |
'\n' |
| 11157 |
' Changed in version 3.11: The delegation of "int()" to ' |
| 11158 |
'"__trunc__()"\n' |
| 11159 |
' is deprecated.\n' |
| 11160 |
'\n' |
| 11161 |
'\n' |
| 11162 |
'With Statement Context Managers\n' |
| 11163 |
'===============================\n' |
| 11164 |
'\n' |
| 11165 |
'A *context manager* is an object that defines the runtime ' |
| 11166 |
'context to\n' |
| 11167 |
'be established when executing a "with" statement. The ' |
| 11168 |
'context manager\n' |
| 11169 |
'handles the entry into, and the exit from, the desired ' |
| 11170 |
'runtime context\n' |
| 11171 |
'for the execution of the block of code. Context managers ' |
| 11172 |
'are normally\n' |
| 11173 |
'invoked using the "with" statement (described in section The ' |
| 11174 |
'with\n' |
| 11175 |
'statement), but can also be used by directly invoking their ' |
| 11176 |
'methods.\n' |
| 11177 |
'\n' |
| 11178 |
'Typical uses of context managers include saving and ' |
| 11179 |
'restoring various\n' |
| 11180 |
'kinds of global state, locking and unlocking resources, ' |
| 11181 |
'closing opened\n' |
| 11182 |
'files, etc.\n' |
| 11183 |
'\n' |
| 11184 |
'For more information on context managers, see Context ' |
| 11185 |
'Manager Types.\n' |
| 11186 |
'\n' |
| 11187 |
'object.__enter__(self)\n' |
| 11188 |
'\n' |
| 11189 |
' Enter the runtime context related to this object. The ' |
| 11190 |
'"with"\n' |
| 11191 |
' statement will bind this method’s return value to the ' |
| 11192 |
'target(s)\n' |
| 11193 |
' specified in the "as" clause of the statement, if any.\n' |
| 11194 |
'\n' |
| 11195 |
'object.__exit__(self, exc_type, exc_value, traceback)\n' |
| 11196 |
'\n' |
| 11197 |
' Exit the runtime context related to this object. The ' |
| 11198 |
'parameters\n' |
| 11199 |
' describe the exception that caused the context to be ' |
| 11200 |
'exited. If the\n' |
| 11201 |
' context was exited without an exception, all three ' |
| 11202 |
'arguments will\n' |
| 11203 |
' be "None".\n' |
| 11204 |
'\n' |
| 11205 |
' If an exception is supplied, and the method wishes to ' |
| 11206 |
'suppress the\n' |
| 11207 |
' exception (i.e., prevent it from being propagated), it ' |
| 11208 |
'should\n' |
| 11209 |
' return a true value. Otherwise, the exception will be ' |
| 11210 |
'processed\n' |
| 11211 |
' normally upon exit from this method.\n' |
| 11212 |
'\n' |
| 11213 |
' Note that "__exit__()" methods should not reraise the ' |
| 11214 |
'passed-in\n' |
| 11215 |
' exception; this is the caller’s responsibility.\n' |
| 11216 |
'\n' |
| 11217 |
'See also:\n' |
| 11218 |
'\n' |
| 11219 |
' **PEP 343** - The “with” statement\n' |
| 11220 |
' The specification, background, and examples for the ' |
| 11221 |
'Python "with"\n' |
| 11222 |
' statement.\n' |
| 11223 |
'\n' |
| 11224 |
'\n' |
| 11225 |
'Customizing positional arguments in class pattern matching\n' |
| 11226 |
'==========================================================\n' |
| 11227 |
'\n' |
| 11228 |
'When using a class name in a pattern, positional arguments ' |
| 11229 |
'in the\n' |
| 11230 |
'pattern are not allowed by default, i.e. "case MyClass(x, ' |
| 11231 |
'y)" is\n' |
| 11232 |
'typically invalid without special support in "MyClass". To ' |
| 11233 |
'be able to\n' |
| 11234 |
'use that kind of pattern, the class needs to define a ' |
| 11235 |
'*__match_args__*\n' |
| 11236 |
'attribute.\n' |
| 11237 |
'\n' |
| 11238 |
'object.__match_args__\n' |
| 11239 |
'\n' |
| 11240 |
' This class variable can be assigned a tuple of strings. ' |
| 11241 |
'When this\n' |
| 11242 |
' class is used in a class pattern with positional ' |
| 11243 |
'arguments, each\n' |
| 11244 |
' positional argument will be converted into a keyword ' |
| 11245 |
'argument,\n' |
| 11246 |
' using the corresponding value in *__match_args__* as the ' |
| 11247 |
'keyword.\n' |
| 11248 |
' The absence of this attribute is equivalent to setting it ' |
| 11249 |
'to "()".\n' |
| 11250 |
'\n' |
| 11251 |
'For example, if "MyClass.__match_args__" is "("left", ' |
| 11252 |
'"center",\n' |
| 11253 |
'"right")" that means that "case MyClass(x, y)" is equivalent ' |
| 11254 |
'to "case\n' |
| 11255 |
'MyClass(left=x, center=y)". Note that the number of ' |
| 11256 |
'arguments in the\n' |
| 11257 |
'pattern must be smaller than or equal to the number of ' |
| 11258 |
'elements in\n' |
| 11259 |
'*__match_args__*; if it is larger, the pattern match attempt ' |
| 11260 |
'will\n' |
| 11261 |
'raise a "TypeError".\n' |
| 11262 |
'\n' |
| 11263 |
'New in version 3.10.\n' |
| 11264 |
'\n' |
| 11265 |
'See also:\n' |
| 11266 |
'\n' |
| 11267 |
' **PEP 634** - Structural Pattern Matching\n' |
| 11268 |
' The specification for the Python "match" statement.\n' |
| 11269 |
'\n' |
| 11270 |
'\n' |
| 11271 |
'Special method lookup\n' |
| 11272 |
'=====================\n' |
| 11273 |
'\n' |
| 11274 |
'For custom classes, implicit invocations of special methods ' |
| 11275 |
'are only\n' |
| 11276 |
'guaranteed to work correctly if defined on an object’s type, ' |
| 11277 |
'not in\n' |
| 11278 |
'the object’s instance dictionary. That behaviour is the ' |
| 11279 |
'reason why\n' |
| 11280 |
'the following code raises an exception:\n' |
| 11281 |
'\n' |
| 11282 |
' >>> class C:\n' |
| 11283 |
' ... pass\n' |
| 11284 |
' ...\n' |
| 11285 |
' >>> c = C()\n' |
| 11286 |
' >>> c.__len__ = lambda: 5\n' |
| 11287 |
' >>> len(c)\n' |
| 11288 |
' Traceback (most recent call last):\n' |
| 11289 |
' File "<stdin>", line 1, in <module>\n' |
| 11290 |
" TypeError: object of type 'C' has no len()\n" |
| 11291 |
'\n' |
| 11292 |
'The rationale behind this behaviour lies with a number of ' |
| 11293 |
'special\n' |
| 11294 |
'methods such as "__hash__()" and "__repr__()" that are ' |
| 11295 |
'implemented by\n' |
| 11296 |
'all objects, including type objects. If the implicit lookup ' |
| 11297 |
'of these\n' |
| 11298 |
'methods used the conventional lookup process, they would ' |
| 11299 |
'fail when\n' |
| 11300 |
'invoked on the type object itself:\n' |
| 11301 |
'\n' |
| 11302 |
' >>> 1 .__hash__() == hash(1)\n' |
| 11303 |
' True\n' |
| 11304 |
' >>> int.__hash__() == hash(int)\n' |
| 11305 |
' Traceback (most recent call last):\n' |
| 11306 |
' File "<stdin>", line 1, in <module>\n' |
| 11307 |
" TypeError: descriptor '__hash__' of 'int' object needs an " |
| 11308 |
'argument\n' |
| 11309 |
'\n' |
| 11310 |
'Incorrectly attempting to invoke an unbound method of a ' |
| 11311 |
'class in this\n' |
| 11312 |
'way is sometimes referred to as ‘metaclass confusion’, and ' |
| 11313 |
'is avoided\n' |
| 11314 |
'by bypassing the instance when looking up special methods:\n' |
| 11315 |
'\n' |
| 11316 |
' >>> type(1).__hash__(1) == hash(1)\n' |
| 11317 |
' True\n' |
| 11318 |
' >>> type(int).__hash__(int) == hash(int)\n' |
| 11319 |
' True\n' |
| 11320 |
'\n' |
| 11321 |
'In addition to bypassing any instance attributes in the ' |
| 11322 |
'interest of\n' |
| 11323 |
'correctness, implicit special method lookup generally also ' |
| 11324 |
'bypasses\n' |
| 11325 |
'the "__getattribute__()" method even of the object’s ' |
| 11326 |
'metaclass:\n' |
| 11327 |
'\n' |
| 11328 |
' >>> class Meta(type):\n' |
| 11329 |
' ... def __getattribute__(*args):\n' |
| 11330 |
' ... print("Metaclass getattribute invoked")\n' |
| 11331 |
' ... return type.__getattribute__(*args)\n' |
| 11332 |
' ...\n' |
| 11333 |
' >>> class C(object, metaclass=Meta):\n' |
| 11334 |
' ... def __len__(self):\n' |
| 11335 |
' ... return 10\n' |
| 11336 |
' ... def __getattribute__(*args):\n' |
| 11337 |
' ... print("Class getattribute invoked")\n' |
| 11338 |
' ... return object.__getattribute__(*args)\n' |
| 11339 |
' ...\n' |
| 11340 |
' >>> c = C()\n' |
| 11341 |
' >>> c.__len__() # Explicit lookup via ' |
| 11342 |
'instance\n' |
| 11343 |
' Class getattribute invoked\n' |
| 11344 |
' 10\n' |
| 11345 |
' >>> type(c).__len__(c) # Explicit lookup via ' |
| 11346 |
'type\n' |
| 11347 |
' Metaclass getattribute invoked\n' |
| 11348 |
' 10\n' |
| 11349 |
' >>> len(c) # Implicit lookup\n' |
| 11350 |
' 10\n' |
| 11351 |
'\n' |
| 11352 |
'Bypassing the "__getattribute__()" machinery in this fashion ' |
| 11353 |
'provides\n' |
| 11354 |
'significant scope for speed optimisations within the ' |
| 11355 |
'interpreter, at\n' |
| 11356 |
'the cost of some flexibility in the handling of special ' |
| 11357 |
'methods (the\n' |
| 11358 |
'special method *must* be set on the class object itself in ' |
| 11359 |
'order to be\n' |
| 11360 |
'consistently invoked by the interpreter).\n', |
| 11361 |
'string-methods': 'String Methods\n' |
| 11362 |
'**************\n' |
| 11363 |
'\n' |
| 11364 |
'Strings implement all of the common sequence operations, ' |
| 11365 |
'along with\n' |
| 11366 |
'the additional methods described below.\n' |
| 11367 |
'\n' |
| 11368 |
'Strings also support two styles of string formatting, one ' |
| 11369 |
'providing a\n' |
| 11370 |
'large degree of flexibility and customization (see ' |
| 11371 |
'"str.format()",\n' |
| 11372 |
'Format String Syntax and Custom String Formatting) and the ' |
| 11373 |
'other based\n' |
| 11374 |
'on C "printf" style formatting that handles a narrower ' |
| 11375 |
'range of types\n' |
| 11376 |
'and is slightly harder to use correctly, but is often ' |
| 11377 |
'faster for the\n' |
| 11378 |
'cases it can handle (printf-style String Formatting).\n' |
| 11379 |
'\n' |
| 11380 |
'The Text Processing Services section of the standard ' |
| 11381 |
'library covers a\n' |
| 11382 |
'number of other modules that provide various text related ' |
| 11383 |
'utilities\n' |
| 11384 |
'(including regular expression support in the "re" ' |
| 11385 |
'module).\n' |
| 11386 |
'\n' |
| 11387 |
'str.capitalize()\n' |
| 11388 |
'\n' |
| 11389 |
' Return a copy of the string with its first character ' |
| 11390 |
'capitalized\n' |
| 11391 |
' and the rest lowercased.\n' |
| 11392 |
'\n' |
| 11393 |
' Changed in version 3.8: The first character is now put ' |
| 11394 |
'into\n' |
| 11395 |
' titlecase rather than uppercase. This means that ' |
| 11396 |
'characters like\n' |
| 11397 |
' digraphs will only have their first letter capitalized, ' |
| 11398 |
'instead of\n' |
| 11399 |
' the full character.\n' |
| 11400 |
'\n' |
| 11401 |
'str.casefold()\n' |
| 11402 |
'\n' |
| 11403 |
' Return a casefolded copy of the string. Casefolded ' |
| 11404 |
'strings may be\n' |
| 11405 |
' used for caseless matching.\n' |
| 11406 |
'\n' |
| 11407 |
' Casefolding is similar to lowercasing but more ' |
| 11408 |
'aggressive because\n' |
| 11409 |
' it is intended to remove all case distinctions in a ' |
| 11410 |
'string. For\n' |
| 11411 |
' example, the German lowercase letter "\'ß\'" is ' |
| 11412 |
'equivalent to ""ss"".\n' |
| 11413 |
' Since it is already lowercase, "lower()" would do ' |
| 11414 |
'nothing to "\'ß\'";\n' |
| 11415 |
' "casefold()" converts it to ""ss"".\n' |
| 11416 |
'\n' |
| 11417 |
' The casefolding algorithm is described in section 3.13 ' |
| 11418 |
'of the\n' |
| 11419 |
' Unicode Standard.\n' |
| 11420 |
'\n' |
| 11421 |
' New in version 3.3.\n' |
| 11422 |
'\n' |
| 11423 |
'str.center(width[, fillchar])\n' |
| 11424 |
'\n' |
| 11425 |
' Return centered in a string of length *width*. Padding ' |
| 11426 |
'is done\n' |
| 11427 |
' using the specified *fillchar* (default is an ASCII ' |
| 11428 |
'space). The\n' |
| 11429 |
' original string is returned if *width* is less than or ' |
| 11430 |
'equal to\n' |
| 11431 |
' "len(s)".\n' |
| 11432 |
'\n' |
| 11433 |
'str.count(sub[, start[, end]])\n' |
| 11434 |
'\n' |
| 11435 |
' Return the number of non-overlapping occurrences of ' |
| 11436 |
'substring *sub*\n' |
| 11437 |
' in the range [*start*, *end*]. Optional arguments ' |
| 11438 |
'*start* and\n' |
| 11439 |
' *end* are interpreted as in slice notation.\n' |
| 11440 |
'\n' |
| 11441 |
' If *sub* is empty, returns the number of empty strings ' |
| 11442 |
'between\n' |
| 11443 |
' characters which is the length of the string plus one.\n' |
| 11444 |
'\n' |
| 11445 |
"str.encode(encoding='utf-8', errors='strict')\n" |
| 11446 |
'\n' |
| 11447 |
' Return the string encoded to "bytes".\n' |
| 11448 |
'\n' |
| 11449 |
' *encoding* defaults to "\'utf-8\'"; see Standard ' |
| 11450 |
'Encodings for\n' |
| 11451 |
' possible values.\n' |
| 11452 |
'\n' |
| 11453 |
' *errors* controls how encoding errors are handled. If ' |
| 11454 |
'"\'strict\'"\n' |
| 11455 |
' (the default), a "UnicodeError" exception is raised. ' |
| 11456 |
'Other possible\n' |
| 11457 |
' values are "\'ignore\'", "\'replace\'", ' |
| 11458 |
'"\'xmlcharrefreplace\'",\n' |
| 11459 |
' "\'backslashreplace\'" and any other name registered ' |
| 11460 |
'via\n' |
| 11461 |
' "codecs.register_error()". See Error Handlers for ' |
| 11462 |
'details.\n' |
| 11463 |
'\n' |
| 11464 |
' For performance reasons, the value of *errors* is not ' |
| 11465 |
'checked for\n' |
| 11466 |
' validity unless an encoding error actually occurs, ' |
| 11467 |
'Python\n' |
| 11468 |
' Development Mode is enabled or a debug build is used.\n' |
| 11469 |
'\n' |
| 11470 |
' Changed in version 3.1: Added support for keyword ' |
| 11471 |
'arguments.\n' |
| 11472 |
'\n' |
| 11473 |
' Changed in version 3.9: The value of the *errors* ' |
| 11474 |
'argument is now\n' |
| 11475 |
' checked in Python Development Mode and in debug mode.\n' |
| 11476 |
'\n' |
| 11477 |
'str.endswith(suffix[, start[, end]])\n' |
| 11478 |
'\n' |
| 11479 |
' Return "True" if the string ends with the specified ' |
| 11480 |
'*suffix*,\n' |
| 11481 |
' otherwise return "False". *suffix* can also be a tuple ' |
| 11482 |
'of suffixes\n' |
| 11483 |
' to look for. With optional *start*, test beginning at ' |
| 11484 |
'that\n' |
| 11485 |
' position. With optional *end*, stop comparing at that ' |
| 11486 |
'position.\n' |
| 11487 |
'\n' |
| 11488 |
'str.expandtabs(tabsize=8)\n' |
| 11489 |
'\n' |
| 11490 |
' Return a copy of the string where all tab characters ' |
| 11491 |
'are replaced\n' |
| 11492 |
' by one or more spaces, depending on the current column ' |
| 11493 |
'and the\n' |
| 11494 |
' given tab size. Tab positions occur every *tabsize* ' |
| 11495 |
'characters\n' |
| 11496 |
' (default is 8, giving tab positions at columns 0, 8, 16 ' |
| 11497 |
'and so on).\n' |
| 11498 |
' To expand the string, the current column is set to zero ' |
| 11499 |
'and the\n' |
| 11500 |
' string is examined character by character. If the ' |
| 11501 |
'character is a\n' |
| 11502 |
' tab ("\\t"), one or more space characters are inserted ' |
| 11503 |
'in the result\n' |
| 11504 |
' until the current column is equal to the next tab ' |
| 11505 |
'position. (The\n' |
| 11506 |
' tab character itself is not copied.) If the character ' |
| 11507 |
'is a newline\n' |
| 11508 |
' ("\\n") or return ("\\r"), it is copied and the current ' |
| 11509 |
'column is\n' |
| 11510 |
' reset to zero. Any other character is copied unchanged ' |
| 11511 |
'and the\n' |
| 11512 |
' current column is incremented by one regardless of how ' |
| 11513 |
'the\n' |
| 11514 |
' character is represented when printed.\n' |
| 11515 |
'\n' |
| 11516 |
" >>> '01\\t012\\t0123\\t01234'.expandtabs()\n" |
| 11517 |
" '01 012 0123 01234'\n" |
| 11518 |
" >>> '01\\t012\\t0123\\t01234'.expandtabs(4)\n" |
| 11519 |
" '01 012 0123 01234'\n" |
| 11520 |
'\n' |
| 11521 |
'str.find(sub[, start[, end]])\n' |
| 11522 |
'\n' |
| 11523 |
' Return the lowest index in the string where substring ' |
| 11524 |
'*sub* is\n' |
| 11525 |
' found within the slice "s[start:end]". Optional ' |
| 11526 |
'arguments *start*\n' |
| 11527 |
' and *end* are interpreted as in slice notation. Return ' |
| 11528 |
'"-1" if\n' |
| 11529 |
' *sub* is not found.\n' |
| 11530 |
'\n' |
| 11531 |
' Note:\n' |
| 11532 |
'\n' |
| 11533 |
' The "find()" method should be used only if you need ' |
| 11534 |
'to know the\n' |
| 11535 |
' position of *sub*. To check if *sub* is a substring ' |
| 11536 |
'or not, use\n' |
| 11537 |
' the "in" operator:\n' |
| 11538 |
'\n' |
| 11539 |
" >>> 'Py' in 'Python'\n" |
| 11540 |
' True\n' |
| 11541 |
'\n' |
| 11542 |
'str.format(*args, **kwargs)\n' |
| 11543 |
'\n' |
| 11544 |
' Perform a string formatting operation. The string on ' |
| 11545 |
'which this\n' |
| 11546 |
' method is called can contain literal text or ' |
| 11547 |
'replacement fields\n' |
| 11548 |
' delimited by braces "{}". Each replacement field ' |
| 11549 |
'contains either\n' |
| 11550 |
' the numeric index of a positional argument, or the name ' |
| 11551 |
'of a\n' |
| 11552 |
' keyword argument. Returns a copy of the string where ' |
| 11553 |
'each\n' |
| 11554 |
' replacement field is replaced with the string value of ' |
| 11555 |
'the\n' |
| 11556 |
' corresponding argument.\n' |
| 11557 |
'\n' |
| 11558 |
' >>> "The sum of 1 + 2 is {0}".format(1+2)\n' |
| 11559 |
" 'The sum of 1 + 2 is 3'\n" |
| 11560 |
'\n' |
| 11561 |
' See Format String Syntax for a description of the ' |
| 11562 |
'various\n' |
| 11563 |
' formatting options that can be specified in format ' |
| 11564 |
'strings.\n' |
| 11565 |
'\n' |
| 11566 |
' Note:\n' |
| 11567 |
'\n' |
| 11568 |
' When formatting a number ("int", "float", "complex",\n' |
| 11569 |
' "decimal.Decimal" and subclasses) with the "n" type ' |
| 11570 |
'(ex:\n' |
| 11571 |
' "\'{:n}\'.format(1234)"), the function temporarily ' |
| 11572 |
'sets the\n' |
| 11573 |
' "LC_CTYPE" locale to the "LC_NUMERIC" locale to ' |
| 11574 |
'decode\n' |
| 11575 |
' "decimal_point" and "thousands_sep" fields of ' |
| 11576 |
'"localeconv()" if\n' |
| 11577 |
' they are non-ASCII or longer than 1 byte, and the ' |
| 11578 |
'"LC_NUMERIC"\n' |
| 11579 |
' locale is different than the "LC_CTYPE" locale. This ' |
| 11580 |
'temporary\n' |
| 11581 |
' change affects other threads.\n' |
| 11582 |
'\n' |
| 11583 |
' Changed in version 3.7: When formatting a number with ' |
| 11584 |
'the "n" type,\n' |
| 11585 |
' the function sets temporarily the "LC_CTYPE" locale to ' |
| 11586 |
'the\n' |
| 11587 |
' "LC_NUMERIC" locale in some cases.\n' |
| 11588 |
'\n' |
| 11589 |
'str.format_map(mapping)\n' |
| 11590 |
'\n' |
| 11591 |
' Similar to "str.format(**mapping)", except that ' |
| 11592 |
'"mapping" is used\n' |
| 11593 |
' directly and not copied to a "dict". This is useful if ' |
| 11594 |
'for example\n' |
| 11595 |
' "mapping" is a dict subclass:\n' |
| 11596 |
'\n' |
| 11597 |
' >>> class Default(dict):\n' |
| 11598 |
' ... def __missing__(self, key):\n' |
| 11599 |
' ... return key\n' |
| 11600 |
' ...\n' |
| 11601 |
" >>> '{name} was born in " |
| 11602 |
"{country}'.format_map(Default(name='Guido'))\n" |
| 11603 |
" 'Guido was born in country'\n" |
| 11604 |
'\n' |
| 11605 |
' New in version 3.2.\n' |
| 11606 |
'\n' |
| 11607 |
'str.index(sub[, start[, end]])\n' |
| 11608 |
'\n' |
| 11609 |
' Like "find()", but raise "ValueError" when the ' |
| 11610 |
'substring is not\n' |
| 11611 |
' found.\n' |
| 11612 |
'\n' |
| 11613 |
'str.isalnum()\n' |
| 11614 |
'\n' |
| 11615 |
' Return "True" if all characters in the string are ' |
| 11616 |
'alphanumeric and\n' |
| 11617 |
' there is at least one character, "False" otherwise. A ' |
| 11618 |
'character\n' |
| 11619 |
' "c" is alphanumeric if one of the following returns ' |
| 11620 |
'"True":\n' |
| 11621 |
' "c.isalpha()", "c.isdecimal()", "c.isdigit()", or ' |
| 11622 |
'"c.isnumeric()".\n' |
| 11623 |
'\n' |
| 11624 |
'str.isalpha()\n' |
| 11625 |
'\n' |
| 11626 |
' Return "True" if all characters in the string are ' |
| 11627 |
'alphabetic and\n' |
| 11628 |
' there is at least one character, "False" otherwise. ' |
| 11629 |
'Alphabetic\n' |
| 11630 |
' characters are those characters defined in the Unicode ' |
| 11631 |
'character\n' |
| 11632 |
' database as “Letter”, i.e., those with general category ' |
| 11633 |
'property\n' |
| 11634 |
' being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note ' |
| 11635 |
'that this is\n' |
| 11636 |
' different from the “Alphabetic” property defined in the ' |
| 11637 |
'Unicode\n' |
| 11638 |
' Standard.\n' |
| 11639 |
'\n' |
| 11640 |
'str.isascii()\n' |
| 11641 |
'\n' |
| 11642 |
' Return "True" if the string is empty or all characters ' |
| 11643 |
'in the\n' |
| 11644 |
' string are ASCII, "False" otherwise. ASCII characters ' |
| 11645 |
'have code\n' |
| 11646 |
' points in the range U+0000-U+007F.\n' |
| 11647 |
'\n' |
| 11648 |
' New in version 3.7.\n' |
| 11649 |
'\n' |
| 11650 |
'str.isdecimal()\n' |
| 11651 |
'\n' |
| 11652 |
' Return "True" if all characters in the string are ' |
| 11653 |
'decimal\n' |
| 11654 |
' characters and there is at least one character, "False" ' |
| 11655 |
'otherwise.\n' |
| 11656 |
' Decimal characters are those that can be used to form ' |
| 11657 |
'numbers in\n' |
| 11658 |
' base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. ' |
| 11659 |
'Formally a decimal\n' |
| 11660 |
' character is a character in the Unicode General ' |
| 11661 |
'Category “Nd”.\n' |
| 11662 |
'\n' |
| 11663 |
'str.isdigit()\n' |
| 11664 |
'\n' |
| 11665 |
' Return "True" if all characters in the string are ' |
| 11666 |
'digits and there\n' |
| 11667 |
' is at least one character, "False" otherwise. Digits ' |
| 11668 |
'include\n' |
| 11669 |
' decimal characters and digits that need special ' |
| 11670 |
'handling, such as\n' |
| 11671 |
' the compatibility superscript digits. This covers ' |
| 11672 |
'digits which\n' |
| 11673 |
' cannot be used to form numbers in base 10, like the ' |
| 11674 |
'Kharosthi\n' |
| 11675 |
' numbers. Formally, a digit is a character that has the ' |
| 11676 |
'property\n' |
| 11677 |
' value Numeric_Type=Digit or Numeric_Type=Decimal.\n' |
| 11678 |
'\n' |
| 11679 |
'str.isidentifier()\n' |
| 11680 |
'\n' |
| 11681 |
' Return "True" if the string is a valid identifier ' |
| 11682 |
'according to the\n' |
| 11683 |
' language definition, section Identifiers and keywords.\n' |
| 11684 |
'\n' |
| 11685 |
' Call "keyword.iskeyword()" to test whether string "s" ' |
| 11686 |
'is a reserved\n' |
| 11687 |
' identifier, such as "def" and "class".\n' |
| 11688 |
'\n' |
| 11689 |
' Example:\n' |
| 11690 |
'\n' |
| 11691 |
' >>> from keyword import iskeyword\n' |
| 11692 |
'\n' |
| 11693 |
" >>> 'hello'.isidentifier(), iskeyword('hello')\n" |
| 11694 |
' (True, False)\n' |
| 11695 |
" >>> 'def'.isidentifier(), iskeyword('def')\n" |
| 11696 |
' (True, True)\n' |
| 11697 |
'\n' |
| 11698 |
'str.islower()\n' |
| 11699 |
'\n' |
| 11700 |
' Return "True" if all cased characters [4] in the string ' |
| 11701 |
'are\n' |
| 11702 |
' lowercase and there is at least one cased character, ' |
| 11703 |
'"False"\n' |
| 11704 |
' otherwise.\n' |
| 11705 |
'\n' |
| 11706 |
'str.isnumeric()\n' |
| 11707 |
'\n' |
| 11708 |
' Return "True" if all characters in the string are ' |
| 11709 |
'numeric\n' |
| 11710 |
' characters, and there is at least one character, ' |
| 11711 |
'"False" otherwise.\n' |
| 11712 |
' Numeric characters include digit characters, and all ' |
| 11713 |
'characters\n' |
| 11714 |
' that have the Unicode numeric value property, e.g. ' |
| 11715 |
'U+2155, VULGAR\n' |
| 11716 |
' FRACTION ONE FIFTH. Formally, numeric characters are ' |
| 11717 |
'those with\n' |
| 11718 |
' the property value Numeric_Type=Digit, ' |
| 11719 |
'Numeric_Type=Decimal or\n' |
| 11720 |
' Numeric_Type=Numeric.\n' |
| 11721 |
'\n' |
| 11722 |
'str.isprintable()\n' |
| 11723 |
'\n' |
| 11724 |
' Return "True" if all characters in the string are ' |
| 11725 |
'printable or the\n' |
| 11726 |
' string is empty, "False" otherwise. Nonprintable ' |
| 11727 |
'characters are\n' |
| 11728 |
' those characters defined in the Unicode character ' |
| 11729 |
'database as\n' |
| 11730 |
' “Other” or “Separator”, excepting the ASCII space ' |
| 11731 |
'(0x20) which is\n' |
| 11732 |
' considered printable. (Note that printable characters ' |
| 11733 |
'in this\n' |
| 11734 |
' context are those which should not be escaped when ' |
| 11735 |
'"repr()" is\n' |
| 11736 |
' invoked on a string. It has no bearing on the handling ' |
| 11737 |
'of strings\n' |
| 11738 |
' written to "sys.stdout" or "sys.stderr".)\n' |
| 11739 |
'\n' |
| 11740 |
'str.isspace()\n' |
| 11741 |
'\n' |
| 11742 |
' Return "True" if there are only whitespace characters ' |
| 11743 |
'in the string\n' |
| 11744 |
' and there is at least one character, "False" ' |
| 11745 |
'otherwise.\n' |
| 11746 |
'\n' |
| 11747 |
' A character is *whitespace* if in the Unicode character ' |
| 11748 |
'database\n' |
| 11749 |
' (see "unicodedata"), either its general category is ' |
| 11750 |
'"Zs"\n' |
| 11751 |
' (“Separator, space”), or its bidirectional class is one ' |
| 11752 |
'of "WS",\n' |
| 11753 |
' "B", or "S".\n' |
| 11754 |
'\n' |
| 11755 |
'str.istitle()\n' |
| 11756 |
'\n' |
| 11757 |
' Return "True" if the string is a titlecased string and ' |
| 11758 |
'there is at\n' |
| 11759 |
' least one character, for example uppercase characters ' |
| 11760 |
'may only\n' |
| 11761 |
' follow uncased characters and lowercase characters only ' |
| 11762 |
'cased ones.\n' |
| 11763 |
' Return "False" otherwise.\n' |
| 11764 |
'\n' |
| 11765 |
'str.isupper()\n' |
| 11766 |
'\n' |
| 11767 |
' Return "True" if all cased characters [4] in the string ' |
| 11768 |
'are\n' |
| 11769 |
' uppercase and there is at least one cased character, ' |
| 11770 |
'"False"\n' |
| 11771 |
' otherwise.\n' |
| 11772 |
'\n' |
| 11773 |
" >>> 'BANANA'.isupper()\n" |
| 11774 |
' True\n' |
| 11775 |
" >>> 'banana'.isupper()\n" |
| 11776 |
' False\n' |
| 11777 |
" >>> 'baNana'.isupper()\n" |
| 11778 |
' False\n' |
| 11779 |
" >>> ' '.isupper()\n" |
| 11780 |
' False\n' |
| 11781 |
'\n' |
| 11782 |
'str.join(iterable)\n' |
| 11783 |
'\n' |
| 11784 |
' Return a string which is the concatenation of the ' |
| 11785 |
'strings in\n' |
| 11786 |
' *iterable*. A "TypeError" will be raised if there are ' |
| 11787 |
'any non-\n' |
| 11788 |
' string values in *iterable*, including "bytes" ' |
| 11789 |
'objects. The\n' |
| 11790 |
' separator between elements is the string providing this ' |
| 11791 |
'method.\n' |
| 11792 |
'\n' |
| 11793 |
'str.ljust(width[, fillchar])\n' |
| 11794 |
'\n' |
| 11795 |
' Return the string left justified in a string of length ' |
| 11796 |
'*width*.\n' |
| 11797 |
' Padding is done using the specified *fillchar* (default ' |
| 11798 |
'is an ASCII\n' |
| 11799 |
' space). The original string is returned if *width* is ' |
| 11800 |
'less than or\n' |
| 11801 |
' equal to "len(s)".\n' |
| 11802 |
'\n' |
| 11803 |
'str.lower()\n' |
| 11804 |
'\n' |
| 11805 |
' Return a copy of the string with all the cased ' |
| 11806 |
'characters [4]\n' |
| 11807 |
' converted to lowercase.\n' |
| 11808 |
'\n' |
| 11809 |
' The lowercasing algorithm used is described in section ' |
| 11810 |
'3.13 of the\n' |
| 11811 |
' Unicode Standard.\n' |
| 11812 |
'\n' |
| 11813 |
'str.lstrip([chars])\n' |
| 11814 |
'\n' |
| 11815 |
' Return a copy of the string with leading characters ' |
| 11816 |
'removed. The\n' |
| 11817 |
' *chars* argument is a string specifying the set of ' |
| 11818 |
'characters to be\n' |
| 11819 |
' removed. If omitted or "None", the *chars* argument ' |
| 11820 |
'defaults to\n' |
| 11821 |
' removing whitespace. The *chars* argument is not a ' |
| 11822 |
'prefix; rather,\n' |
| 11823 |
' all combinations of its values are stripped:\n' |
| 11824 |
'\n' |
| 11825 |
" >>> ' spacious '.lstrip()\n" |
| 11826 |
" 'spacious '\n" |
| 11827 |
" >>> 'www.example.com'.lstrip('cmowz.')\n" |
| 11828 |
" 'example.com'\n" |
| 11829 |
'\n' |
| 11830 |
' See "str.removeprefix()" for a method that will remove ' |
| 11831 |
'a single\n' |
| 11832 |
' prefix string rather than all of a set of characters. ' |
| 11833 |
'For example:\n' |
| 11834 |
'\n' |
| 11835 |
" >>> 'Arthur: three!'.lstrip('Arthur: ')\n" |
| 11836 |
" 'ee!'\n" |
| 11837 |
" >>> 'Arthur: three!'.removeprefix('Arthur: ')\n" |
| 11838 |
" 'three!'\n" |
| 11839 |
'\n' |
| 11840 |
'static str.maketrans(x[, y[, z]])\n' |
| 11841 |
'\n' |
| 11842 |
' This static method returns a translation table usable ' |
| 11843 |
'for\n' |
| 11844 |
' "str.translate()".\n' |
| 11845 |
'\n' |
| 11846 |
' If there is only one argument, it must be a dictionary ' |
| 11847 |
'mapping\n' |
| 11848 |
' Unicode ordinals (integers) or characters (strings of ' |
| 11849 |
'length 1) to\n' |
| 11850 |
' Unicode ordinals, strings (of arbitrary lengths) or ' |
| 11851 |
'"None".\n' |
| 11852 |
' Character keys will then be converted to ordinals.\n' |
| 11853 |
'\n' |
| 11854 |
' If there are two arguments, they must be strings of ' |
| 11855 |
'equal length,\n' |
| 11856 |
' and in the resulting dictionary, each character in x ' |
| 11857 |
'will be mapped\n' |
| 11858 |
' to the character at the same position in y. If there ' |
| 11859 |
'is a third\n' |
| 11860 |
' argument, it must be a string, whose characters will be ' |
| 11861 |
'mapped to\n' |
| 11862 |
' "None" in the result.\n' |
| 11863 |
'\n' |
| 11864 |
'str.partition(sep)\n' |
| 11865 |
'\n' |
| 11866 |
' Split the string at the first occurrence of *sep*, and ' |
| 11867 |
'return a\n' |
| 11868 |
' 3-tuple containing the part before the separator, the ' |
| 11869 |
'separator\n' |
| 11870 |
' itself, and the part after the separator. If the ' |
| 11871 |
'separator is not\n' |
| 11872 |
' found, return a 3-tuple containing the string itself, ' |
| 11873 |
'followed by\n' |
| 11874 |
' two empty strings.\n' |
| 11875 |
'\n' |
| 11876 |
'str.removeprefix(prefix, /)\n' |
| 11877 |
'\n' |
| 11878 |
' If the string starts with the *prefix* string, return\n' |
| 11879 |
' "string[len(prefix):]". Otherwise, return a copy of the ' |
| 11880 |
'original\n' |
| 11881 |
' string:\n' |
| 11882 |
'\n' |
| 11883 |
" >>> 'TestHook'.removeprefix('Test')\n" |
| 11884 |
" 'Hook'\n" |
| 11885 |
" >>> 'BaseTestCase'.removeprefix('Test')\n" |
| 11886 |
" 'BaseTestCase'\n" |
| 11887 |
'\n' |
| 11888 |
' New in version 3.9.\n' |
| 11889 |
'\n' |
| 11890 |
'str.removesuffix(suffix, /)\n' |
| 11891 |
'\n' |
| 11892 |
' If the string ends with the *suffix* string and that ' |
| 11893 |
'*suffix* is\n' |
| 11894 |
' not empty, return "string[:-len(suffix)]". Otherwise, ' |
| 11895 |
'return a copy\n' |
| 11896 |
' of the original string:\n' |
| 11897 |
'\n' |
| 11898 |
" >>> 'MiscTests'.removesuffix('Tests')\n" |
| 11899 |
" 'Misc'\n" |
| 11900 |
" >>> 'TmpDirMixin'.removesuffix('Tests')\n" |
| 11901 |
" 'TmpDirMixin'\n" |
| 11902 |
'\n' |
| 11903 |
' New in version 3.9.\n' |
| 11904 |
'\n' |
| 11905 |
'str.replace(old, new[, count])\n' |
| 11906 |
'\n' |
| 11907 |
' Return a copy of the string with all occurrences of ' |
| 11908 |
'substring *old*\n' |
| 11909 |
' replaced by *new*. If the optional argument *count* is ' |
| 11910 |
'given, only\n' |
| 11911 |
' the first *count* occurrences are replaced.\n' |
| 11912 |
'\n' |
| 11913 |
'str.rfind(sub[, start[, end]])\n' |
| 11914 |
'\n' |
| 11915 |
' Return the highest index in the string where substring ' |
| 11916 |
'*sub* is\n' |
| 11917 |
' found, such that *sub* is contained within ' |
| 11918 |
'"s[start:end]".\n' |
| 11919 |
' Optional arguments *start* and *end* are interpreted as ' |
| 11920 |
'in slice\n' |
| 11921 |
' notation. Return "-1" on failure.\n' |
| 11922 |
'\n' |
| 11923 |
'str.rindex(sub[, start[, end]])\n' |
| 11924 |
'\n' |
| 11925 |
' Like "rfind()" but raises "ValueError" when the ' |
| 11926 |
'substring *sub* is\n' |
| 11927 |
' not found.\n' |
| 11928 |
'\n' |
| 11929 |
'str.rjust(width[, fillchar])\n' |
| 11930 |
'\n' |
| 11931 |
' Return the string right justified in a string of length ' |
| 11932 |
'*width*.\n' |
| 11933 |
' Padding is done using the specified *fillchar* (default ' |
| 11934 |
'is an ASCII\n' |
| 11935 |
' space). The original string is returned if *width* is ' |
| 11936 |
'less than or\n' |
| 11937 |
' equal to "len(s)".\n' |
| 11938 |
'\n' |
| 11939 |
'str.rpartition(sep)\n' |
| 11940 |
'\n' |
| 11941 |
' Split the string at the last occurrence of *sep*, and ' |
| 11942 |
'return a\n' |
| 11943 |
' 3-tuple containing the part before the separator, the ' |
| 11944 |
'separator\n' |
| 11945 |
' itself, and the part after the separator. If the ' |
| 11946 |
'separator is not\n' |
| 11947 |
' found, return a 3-tuple containing two empty strings, ' |
| 11948 |
'followed by\n' |
| 11949 |
' the string itself.\n' |
| 11950 |
'\n' |
| 11951 |
'str.rsplit(sep=None, maxsplit=-1)\n' |
| 11952 |
'\n' |
| 11953 |
' Return a list of the words in the string, using *sep* ' |
| 11954 |
'as the\n' |
| 11955 |
' delimiter string. If *maxsplit* is given, at most ' |
| 11956 |
'*maxsplit* splits\n' |
| 11957 |
' are done, the *rightmost* ones. If *sep* is not ' |
| 11958 |
'specified or\n' |
| 11959 |
' "None", any whitespace string is a separator. Except ' |
| 11960 |
'for splitting\n' |
| 11961 |
' from the right, "rsplit()" behaves like "split()" which ' |
| 11962 |
'is\n' |
| 11963 |
' described in detail below.\n' |
| 11964 |
'\n' |
| 11965 |
'str.rstrip([chars])\n' |
| 11966 |
'\n' |
| 11967 |
' Return a copy of the string with trailing characters ' |
| 11968 |
'removed. The\n' |
| 11969 |
' *chars* argument is a string specifying the set of ' |
| 11970 |
'characters to be\n' |
| 11971 |
' removed. If omitted or "None", the *chars* argument ' |
| 11972 |
'defaults to\n' |
| 11973 |
' removing whitespace. The *chars* argument is not a ' |
| 11974 |
'suffix; rather,\n' |
| 11975 |
' all combinations of its values are stripped:\n' |
| 11976 |
'\n' |
| 11977 |
" >>> ' spacious '.rstrip()\n" |
| 11978 |
" ' spacious'\n" |
| 11979 |
" >>> 'mississippi'.rstrip('ipz')\n" |
| 11980 |
" 'mississ'\n" |
| 11981 |
'\n' |
| 11982 |
' See "str.removesuffix()" for a method that will remove ' |
| 11983 |
'a single\n' |
| 11984 |
' suffix string rather than all of a set of characters. ' |
| 11985 |
'For example:\n' |
| 11986 |
'\n' |
| 11987 |
" >>> 'Monty Python'.rstrip(' Python')\n" |
| 11988 |
" 'M'\n" |
| 11989 |
" >>> 'Monty Python'.removesuffix(' Python')\n" |
| 11990 |
" 'Monty'\n" |
| 11991 |
'\n' |
| 11992 |
'str.split(sep=None, maxsplit=-1)\n' |
| 11993 |
'\n' |
| 11994 |
' Return a list of the words in the string, using *sep* ' |
| 11995 |
'as the\n' |
| 11996 |
' delimiter string. If *maxsplit* is given, at most ' |
| 11997 |
'*maxsplit*\n' |
| 11998 |
' splits are done (thus, the list will have at most ' |
| 11999 |
'"maxsplit+1"\n' |
| 12000 |
' elements). If *maxsplit* is not specified or "-1", ' |
| 12001 |
'then there is\n' |
| 12002 |
' no limit on the number of splits (all possible splits ' |
| 12003 |
'are made).\n' |
| 12004 |
'\n' |
| 12005 |
' If *sep* is given, consecutive delimiters are not ' |
| 12006 |
'grouped together\n' |
| 12007 |
' and are deemed to delimit empty strings (for example,\n' |
| 12008 |
' "\'1,,2\'.split(\',\')" returns "[\'1\', \'\', ' |
| 12009 |
'\'2\']"). The *sep* argument\n' |
| 12010 |
' may consist of multiple characters (for example,\n' |
| 12011 |
' "\'1<>2<>3\'.split(\'<>\')" returns "[\'1\', \'2\', ' |
| 12012 |
'\'3\']"). Splitting an\n' |
| 12013 |
' empty string with a specified separator returns ' |
| 12014 |
'"[\'\']".\n' |
| 12015 |
'\n' |
| 12016 |
' For example:\n' |
| 12017 |
'\n' |
| 12018 |
" >>> '1,2,3'.split(',')\n" |
| 12019 |
" ['1', '2', '3']\n" |
| 12020 |
" >>> '1,2,3'.split(',', maxsplit=1)\n" |
| 12021 |
" ['1', '2,3']\n" |
| 12022 |
" >>> '1,2,,3,'.split(',')\n" |
| 12023 |
" ['1', '2', '', '3', '']\n" |
| 12024 |
'\n' |
| 12025 |
' If *sep* is not specified or is "None", a different ' |
| 12026 |
'splitting\n' |
| 12027 |
' algorithm is applied: runs of consecutive whitespace ' |
| 12028 |
'are regarded\n' |
| 12029 |
' as a single separator, and the result will contain no ' |
| 12030 |
'empty strings\n' |
| 12031 |
' at the start or end if the string has leading or ' |
| 12032 |
'trailing\n' |
| 12033 |
' whitespace. Consequently, splitting an empty string or ' |
| 12034 |
'a string\n' |
| 12035 |
' consisting of just whitespace with a "None" separator ' |
| 12036 |
'returns "[]".\n' |
| 12037 |
'\n' |
| 12038 |
' For example:\n' |
| 12039 |
'\n' |
| 12040 |
" >>> '1 2 3'.split()\n" |
| 12041 |
" ['1', '2', '3']\n" |
| 12042 |
" >>> '1 2 3'.split(maxsplit=1)\n" |
| 12043 |
" ['1', '2 3']\n" |
| 12044 |
" >>> ' 1 2 3 '.split()\n" |
| 12045 |
" ['1', '2', '3']\n" |
| 12046 |
'\n' |
| 12047 |
'str.splitlines(keepends=False)\n' |
| 12048 |
'\n' |
| 12049 |
' Return a list of the lines in the string, breaking at ' |
| 12050 |
'line\n' |
| 12051 |
' boundaries. Line breaks are not included in the ' |
| 12052 |
'resulting list\n' |
| 12053 |
' unless *keepends* is given and true.\n' |
| 12054 |
'\n' |
| 12055 |
' This method splits on the following line boundaries. ' |
| 12056 |
'In\n' |
| 12057 |
' particular, the boundaries are a superset of *universal ' |
| 12058 |
'newlines*.\n' |
| 12059 |
'\n' |
| 12060 |
' ' |
| 12061 |
'+-------------------------+-------------------------------+\n' |
| 12062 |
' | Representation | ' |
| 12063 |
'Description |\n' |
| 12064 |
' ' |
| 12065 |
'|=========================|===============================|\n' |
| 12066 |
' | "\\n" | Line ' |
| 12067 |
'Feed |\n' |
| 12068 |
' ' |
| 12069 |
'+-------------------------+-------------------------------+\n' |
| 12070 |
' | "\\r" | Carriage ' |
| 12071 |
'Return |\n' |
| 12072 |
' ' |
| 12073 |
'+-------------------------+-------------------------------+\n' |
| 12074 |
' | "\\r\\n" | Carriage Return + Line ' |
| 12075 |
'Feed |\n' |
| 12076 |
' ' |
| 12077 |
'+-------------------------+-------------------------------+\n' |
| 12078 |
' | "\\v" or "\\x0b" | Line ' |
| 12079 |
'Tabulation |\n' |
| 12080 |
' ' |
| 12081 |
'+-------------------------+-------------------------------+\n' |
| 12082 |
' | "\\f" or "\\x0c" | Form ' |
| 12083 |
'Feed |\n' |
| 12084 |
' ' |
| 12085 |
'+-------------------------+-------------------------------+\n' |
| 12086 |
' | "\\x1c" | File ' |
| 12087 |
'Separator |\n' |
| 12088 |
' ' |
| 12089 |
'+-------------------------+-------------------------------+\n' |
| 12090 |
' | "\\x1d" | Group ' |
| 12091 |
'Separator |\n' |
| 12092 |
' ' |
| 12093 |
'+-------------------------+-------------------------------+\n' |
| 12094 |
' | "\\x1e" | Record ' |
| 12095 |
'Separator |\n' |
| 12096 |
' ' |
| 12097 |
'+-------------------------+-------------------------------+\n' |
| 12098 |
' | "\\x85" | Next Line (C1 Control ' |
| 12099 |
'Code) |\n' |
| 12100 |
' ' |
| 12101 |
'+-------------------------+-------------------------------+\n' |
| 12102 |
' | "\\u2028" | Line ' |
| 12103 |
'Separator |\n' |
| 12104 |
' ' |
| 12105 |
'+-------------------------+-------------------------------+\n' |
| 12106 |
' | "\\u2029" | Paragraph ' |
| 12107 |
'Separator |\n' |
| 12108 |
' ' |
| 12109 |
'+-------------------------+-------------------------------+\n' |
| 12110 |
'\n' |
| 12111 |
' Changed in version 3.2: "\\v" and "\\f" added to list ' |
| 12112 |
'of line\n' |
| 12113 |
' boundaries.\n' |
| 12114 |
'\n' |
| 12115 |
' For example:\n' |
| 12116 |
'\n' |
| 12117 |
" >>> 'ab c\\n\\nde fg\\rkl\\r\\n'.splitlines()\n" |
| 12118 |
" ['ab c', '', 'de fg', 'kl']\n" |
| 12119 |
" >>> 'ab c\\n\\nde " |
| 12120 |
"fg\\rkl\\r\\n'.splitlines(keepends=True)\n" |
| 12121 |
" ['ab c\\n', '\\n', 'de fg\\r', 'kl\\r\\n']\n" |
| 12122 |
'\n' |
| 12123 |
' Unlike "split()" when a delimiter string *sep* is ' |
| 12124 |
'given, this\n' |
| 12125 |
' method returns an empty list for the empty string, and ' |
| 12126 |
'a terminal\n' |
| 12127 |
' line break does not result in an extra line:\n' |
| 12128 |
'\n' |
| 12129 |
' >>> "".splitlines()\n' |
| 12130 |
' []\n' |
| 12131 |
' >>> "One line\\n".splitlines()\n' |
| 12132 |
" ['One line']\n" |
| 12133 |
'\n' |
| 12134 |
' For comparison, "split(\'\\n\')" gives:\n' |
| 12135 |
'\n' |
| 12136 |
" >>> ''.split('\\n')\n" |
| 12137 |
" ['']\n" |
| 12138 |
" >>> 'Two lines\\n'.split('\\n')\n" |
| 12139 |
" ['Two lines', '']\n" |
| 12140 |
'\n' |
| 12141 |
'str.startswith(prefix[, start[, end]])\n' |
| 12142 |
'\n' |
| 12143 |
' Return "True" if string starts with the *prefix*, ' |
| 12144 |
'otherwise return\n' |
| 12145 |
' "False". *prefix* can also be a tuple of prefixes to ' |
| 12146 |
'look for.\n' |
| 12147 |
' With optional *start*, test string beginning at that ' |
| 12148 |
'position.\n' |
| 12149 |
' With optional *end*, stop comparing string at that ' |
| 12150 |
'position.\n' |
| 12151 |
'\n' |
| 12152 |
'str.strip([chars])\n' |
| 12153 |
'\n' |
| 12154 |
' Return a copy of the string with the leading and ' |
| 12155 |
'trailing\n' |
| 12156 |
' characters removed. The *chars* argument is a string ' |
| 12157 |
'specifying the\n' |
| 12158 |
' set of characters to be removed. If omitted or "None", ' |
| 12159 |
'the *chars*\n' |
| 12160 |
' argument defaults to removing whitespace. The *chars* ' |
| 12161 |
'argument is\n' |
| 12162 |
' not a prefix or suffix; rather, all combinations of its ' |
| 12163 |
'values are\n' |
| 12164 |
' stripped:\n' |
| 12165 |
'\n' |
| 12166 |
" >>> ' spacious '.strip()\n" |
| 12167 |
" 'spacious'\n" |
| 12168 |
" >>> 'www.example.com'.strip('cmowz.')\n" |
| 12169 |
" 'example'\n" |
| 12170 |
'\n' |
| 12171 |
' The outermost leading and trailing *chars* argument ' |
| 12172 |
'values are\n' |
| 12173 |
' stripped from the string. Characters are removed from ' |
| 12174 |
'the leading\n' |
| 12175 |
' end until reaching a string character that is not ' |
| 12176 |
'contained in the\n' |
| 12177 |
' set of characters in *chars*. A similar action takes ' |
| 12178 |
'place on the\n' |
| 12179 |
' trailing end. For example:\n' |
| 12180 |
'\n' |
| 12181 |
" >>> comment_string = '#....... Section 3.2.1 Issue " |
| 12182 |
"#32 .......'\n" |
| 12183 |
" >>> comment_string.strip('.#! ')\n" |
| 12184 |
" 'Section 3.2.1 Issue #32'\n" |
| 12185 |
'\n' |
| 12186 |
'str.swapcase()\n' |
| 12187 |
'\n' |
| 12188 |
' Return a copy of the string with uppercase characters ' |
| 12189 |
'converted to\n' |
| 12190 |
' lowercase and vice versa. Note that it is not ' |
| 12191 |
'necessarily true that\n' |
| 12192 |
' "s.swapcase().swapcase() == s".\n' |
| 12193 |
'\n' |
| 12194 |
'str.title()\n' |
| 12195 |
'\n' |
| 12196 |
' Return a titlecased version of the string where words ' |
| 12197 |
'start with an\n' |
| 12198 |
' uppercase character and the remaining characters are ' |
| 12199 |
'lowercase.\n' |
| 12200 |
'\n' |
| 12201 |
' For example:\n' |
| 12202 |
'\n' |
| 12203 |
" >>> 'Hello world'.title()\n" |
| 12204 |
" 'Hello World'\n" |
| 12205 |
'\n' |
| 12206 |
' The algorithm uses a simple language-independent ' |
| 12207 |
'definition of a\n' |
| 12208 |
' word as groups of consecutive letters. The definition ' |
| 12209 |
'works in\n' |
| 12210 |
' many contexts but it means that apostrophes in ' |
| 12211 |
'contractions and\n' |
| 12212 |
' possessives form word boundaries, which may not be the ' |
| 12213 |
'desired\n' |
| 12214 |
' result:\n' |
| 12215 |
'\n' |
| 12216 |
' >>> "they\'re bill\'s friends from the UK".title()\n' |
| 12217 |
' "They\'Re Bill\'S Friends From The Uk"\n' |
| 12218 |
'\n' |
| 12219 |
' The "string.capwords()" function does not have this ' |
| 12220 |
'problem, as it\n' |
| 12221 |
' splits words on spaces only.\n' |
| 12222 |
'\n' |
| 12223 |
' Alternatively, a workaround for apostrophes can be ' |
| 12224 |
'constructed\n' |
| 12225 |
' using regular expressions:\n' |
| 12226 |
'\n' |
| 12227 |
' >>> import re\n' |
| 12228 |
' >>> def titlecase(s):\n' |
| 12229 |
' ... return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n' |
| 12230 |
' ... lambda mo: ' |
| 12231 |
'mo.group(0).capitalize(),\n' |
| 12232 |
' ... s)\n' |
| 12233 |
' ...\n' |
| 12234 |
' >>> titlecase("they\'re bill\'s friends.")\n' |
| 12235 |
' "They\'re Bill\'s Friends."\n' |
| 12236 |
'\n' |
| 12237 |
'str.translate(table)\n' |
| 12238 |
'\n' |
| 12239 |
' Return a copy of the string in which each character has ' |
| 12240 |
'been mapped\n' |
| 12241 |
' through the given translation table. The table must be ' |
| 12242 |
'an object\n' |
| 12243 |
' that implements indexing via "__getitem__()", typically ' |
| 12244 |
'a *mapping*\n' |
| 12245 |
' or *sequence*. When indexed by a Unicode ordinal (an ' |
| 12246 |
'integer), the\n' |
| 12247 |
' table object can do any of the following: return a ' |
| 12248 |
'Unicode ordinal\n' |
| 12249 |
' or a string, to map the character to one or more other ' |
| 12250 |
'characters;\n' |
| 12251 |
' return "None", to delete the character from the return ' |
| 12252 |
'string; or\n' |
| 12253 |
' raise a "LookupError" exception, to map the character ' |
| 12254 |
'to itself.\n' |
| 12255 |
'\n' |
| 12256 |
' You can use "str.maketrans()" to create a translation ' |
| 12257 |
'map from\n' |
| 12258 |
' character-to-character mappings in different formats.\n' |
| 12259 |
'\n' |
| 12260 |
' See also the "codecs" module for a more flexible ' |
| 12261 |
'approach to custom\n' |
| 12262 |
' character mappings.\n' |
| 12263 |
'\n' |
| 12264 |
'str.upper()\n' |
| 12265 |
'\n' |
| 12266 |
' Return a copy of the string with all the cased ' |
| 12267 |
'characters [4]\n' |
| 12268 |
' converted to uppercase. Note that ' |
| 12269 |
'"s.upper().isupper()" might be\n' |
| 12270 |
' "False" if "s" contains uncased characters or if the ' |
| 12271 |
'Unicode\n' |
| 12272 |
' category of the resulting character(s) is not “Lu” ' |
| 12273 |
'(Letter,\n' |
| 12274 |
' uppercase), but e.g. “Lt” (Letter, titlecase).\n' |
| 12275 |
'\n' |
| 12276 |
' The uppercasing algorithm used is described in section ' |
| 12277 |
'3.13 of the\n' |
| 12278 |
' Unicode Standard.\n' |
| 12279 |
'\n' |
| 12280 |
'str.zfill(width)\n' |
| 12281 |
'\n' |
| 12282 |
' Return a copy of the string left filled with ASCII ' |
| 12283 |
'"\'0\'" digits to\n' |
| 12284 |
' make a string of length *width*. A leading sign prefix\n' |
| 12285 |
' ("\'+\'"/"\'-\'") is handled by inserting the padding ' |
| 12286 |
'*after* the sign\n' |
| 12287 |
' character rather than before. The original string is ' |
| 12288 |
'returned if\n' |
| 12289 |
' *width* is less than or equal to "len(s)".\n' |
| 12290 |
'\n' |
| 12291 |
' For example:\n' |
| 12292 |
'\n' |
| 12293 |
' >>> "42".zfill(5)\n' |
| 12294 |
" '00042'\n" |
| 12295 |
' >>> "-42".zfill(5)\n' |
| 12296 |
" '-0042'\n", |
| 12297 |
'strings': 'String and Bytes literals\n' |
| 12298 |
'*************************\n' |
| 12299 |
'\n' |
| 12300 |
'String literals are described by the following lexical ' |
| 12301 |
'definitions:\n' |
| 12302 |
'\n' |
| 12303 |
' stringliteral ::= [stringprefix](shortstring | longstring)\n' |
| 12304 |
' stringprefix ::= "r" | "u" | "R" | "U" | "f" | "F"\n' |
| 12305 |
' | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | ' |
| 12306 |
'"Rf" | "RF"\n' |
| 12307 |
' shortstring ::= "\'" shortstringitem* "\'" | \'"\' ' |
| 12308 |
'shortstringitem* \'"\'\n' |
| 12309 |
' longstring ::= "\'\'\'" longstringitem* "\'\'\'" | ' |
| 12310 |
'\'"""\' longstringitem* \'"""\'\n' |
| 12311 |
' shortstringitem ::= shortstringchar | stringescapeseq\n' |
| 12312 |
' longstringitem ::= longstringchar | stringescapeseq\n' |
| 12313 |
' shortstringchar ::= <any source character except "\\" or ' |
| 12314 |
'newline or the quote>\n' |
| 12315 |
' longstringchar ::= <any source character except "\\">\n' |
| 12316 |
' stringescapeseq ::= "\\" <any source character>\n' |
| 12317 |
'\n' |
| 12318 |
' bytesliteral ::= bytesprefix(shortbytes | longbytes)\n' |
| 12319 |
' bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" | ' |
| 12320 |
'"rb" | "rB" | "Rb" | "RB"\n' |
| 12321 |
' shortbytes ::= "\'" shortbytesitem* "\'" | \'"\' ' |
| 12322 |
'shortbytesitem* \'"\'\n' |
| 12323 |
' longbytes ::= "\'\'\'" longbytesitem* "\'\'\'" | \'"""\' ' |
| 12324 |
'longbytesitem* \'"""\'\n' |
| 12325 |
' shortbytesitem ::= shortbyteschar | bytesescapeseq\n' |
| 12326 |
' longbytesitem ::= longbyteschar | bytesescapeseq\n' |
| 12327 |
' shortbyteschar ::= <any ASCII character except "\\" or newline ' |
| 12328 |
'or the quote>\n' |
| 12329 |
' longbyteschar ::= <any ASCII character except "\\">\n' |
| 12330 |
' bytesescapeseq ::= "\\" <any ASCII character>\n' |
| 12331 |
'\n' |
| 12332 |
'One syntactic restriction not indicated by these productions is ' |
| 12333 |
'that\n' |
| 12334 |
'whitespace is not allowed between the "stringprefix" or ' |
| 12335 |
'"bytesprefix"\n' |
| 12336 |
'and the rest of the literal. The source character set is defined ' |
| 12337 |
'by\n' |
| 12338 |
'the encoding declaration; it is UTF-8 if no encoding declaration ' |
| 12339 |
'is\n' |
| 12340 |
'given in the source file; see section Encoding declarations.\n' |
| 12341 |
'\n' |
| 12342 |
'In plain English: Both types of literals can be enclosed in ' |
| 12343 |
'matching\n' |
| 12344 |
'single quotes ("\'") or double quotes ("""). They can also be ' |
| 12345 |
'enclosed\n' |
| 12346 |
'in matching groups of three single or double quotes (these are\n' |
| 12347 |
'generally referred to as *triple-quoted strings*). The ' |
| 12348 |
'backslash\n' |
| 12349 |
'("\\") character is used to escape characters that otherwise have ' |
| 12350 |
'a\n' |
| 12351 |
'special meaning, such as newline, backslash itself, or the quote\n' |
| 12352 |
'character.\n' |
| 12353 |
'\n' |
| 12354 |
'Bytes literals are always prefixed with "\'b\'" or "\'B\'"; they ' |
| 12355 |
'produce\n' |
| 12356 |
'an instance of the "bytes" type instead of the "str" type. They ' |
| 12357 |
'may\n' |
| 12358 |
'only contain ASCII characters; bytes with a numeric value of 128 ' |
| 12359 |
'or\n' |
| 12360 |
'greater must be expressed with escapes.\n' |
| 12361 |
'\n' |
| 12362 |
'Both string and bytes literals may optionally be prefixed with a\n' |
| 12363 |
'letter "\'r\'" or "\'R\'"; such strings are called *raw strings* ' |
| 12364 |
'and treat\n' |
| 12365 |
'backslashes as literal characters. As a result, in string ' |
| 12366 |
'literals,\n' |
| 12367 |
'"\'\\U\'" and "\'\\u\'" escapes in raw strings are not treated ' |
| 12368 |
'specially.\n' |
| 12369 |
'Given that Python 2.x’s raw unicode literals behave differently ' |
| 12370 |
'than\n' |
| 12371 |
'Python 3.x’s the "\'ur\'" syntax is not supported.\n' |
| 12372 |
'\n' |
| 12373 |
'New in version 3.3: The "\'rb\'" prefix of raw bytes literals has ' |
| 12374 |
'been\n' |
| 12375 |
'added as a synonym of "\'br\'".Support for the unicode legacy ' |
| 12376 |
'literal\n' |
| 12377 |
'("u\'value\'") was reintroduced to simplify the maintenance of ' |
| 12378 |
'dual\n' |
| 12379 |
'Python 2.x and 3.x codebases. See **PEP 414** for more ' |
| 12380 |
'information.\n' |
| 12381 |
'\n' |
| 12382 |
'A string literal with "\'f\'" or "\'F\'" in its prefix is a ' |
| 12383 |
'*formatted\n' |
| 12384 |
'string literal*; see f-strings. The "\'f\'" may be combined with ' |
| 12385 |
'"\'r\'",\n' |
| 12386 |
'but not with "\'b\'" or "\'u\'", therefore raw formatted strings ' |
| 12387 |
'are\n' |
| 12388 |
'possible, but formatted bytes literals are not.\n' |
| 12389 |
'\n' |
| 12390 |
'In triple-quoted literals, unescaped newlines and quotes are ' |
| 12391 |
'allowed\n' |
| 12392 |
'(and are retained), except that three unescaped quotes in a row\n' |
| 12393 |
'terminate the literal. (A “quote” is the character used to open ' |
| 12394 |
'the\n' |
| 12395 |
'literal, i.e. either "\'" or """.)\n' |
| 12396 |
'\n' |
| 12397 |
'\n' |
| 12398 |
'Escape sequences\n' |
| 12399 |
'================\n' |
| 12400 |
'\n' |
| 12401 |
'Unless an "\'r\'" or "\'R\'" prefix is present, escape sequences ' |
| 12402 |
'in string\n' |
| 12403 |
'and bytes literals are interpreted according to rules similar to ' |
| 12404 |
'those\n' |
| 12405 |
'used by Standard C. The recognized escape sequences are:\n' |
| 12406 |
'\n' |
| 12407 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12408 |
'| Escape Sequence | Meaning | ' |
| 12409 |
'Notes |\n' |
| 12410 |
'|===========================|===================================|=========|\n' |
| 12411 |
'| "\\"<newline> | Backslash and newline ignored ' |
| 12412 |
'| (1) |\n' |
| 12413 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12414 |
'| "\\\\" | Backslash ' |
| 12415 |
'("\\") | |\n' |
| 12416 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12417 |
'| "\\\'" | Single quote ' |
| 12418 |
'("\'") | |\n' |
| 12419 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12420 |
'| "\\"" | Double quote (""") ' |
| 12421 |
'| |\n' |
| 12422 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12423 |
'| "\\a" | ASCII Bell (BEL) ' |
| 12424 |
'| |\n' |
| 12425 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12426 |
'| "\\b" | ASCII Backspace (BS) ' |
| 12427 |
'| |\n' |
| 12428 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12429 |
'| "\\f" | ASCII Formfeed (FF) ' |
| 12430 |
'| |\n' |
| 12431 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12432 |
'| "\\n" | ASCII Linefeed (LF) ' |
| 12433 |
'| |\n' |
| 12434 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12435 |
'| "\\r" | ASCII Carriage Return (CR) ' |
| 12436 |
'| |\n' |
| 12437 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12438 |
'| "\\t" | ASCII Horizontal Tab (TAB) ' |
| 12439 |
'| |\n' |
| 12440 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12441 |
'| "\\v" | ASCII Vertical Tab (VT) ' |
| 12442 |
'| |\n' |
| 12443 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12444 |
'| "\\*ooo*" | Character with octal value *ooo* ' |
| 12445 |
'| (2,4) |\n' |
| 12446 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12447 |
'| "\\x*hh*" | Character with hex value *hh* ' |
| 12448 |
'| (3,4) |\n' |
| 12449 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12450 |
'\n' |
| 12451 |
'Escape sequences only recognized in string literals are:\n' |
| 12452 |
'\n' |
| 12453 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12454 |
'| Escape Sequence | Meaning | ' |
| 12455 |
'Notes |\n' |
| 12456 |
'|===========================|===================================|=========|\n' |
| 12457 |
'| "\\N{*name*}" | Character named *name* in the ' |
| 12458 |
'| (5) |\n' |
| 12459 |
'| | Unicode database ' |
| 12460 |
'| |\n' |
| 12461 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12462 |
'| "\\u*xxxx*" | Character with 16-bit hex value ' |
| 12463 |
'| (6) |\n' |
| 12464 |
'| | *xxxx* ' |
| 12465 |
'| |\n' |
| 12466 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12467 |
'| "\\U*xxxxxxxx*" | Character with 32-bit hex value ' |
| 12468 |
'| (7) |\n' |
| 12469 |
'| | *xxxxxxxx* ' |
| 12470 |
'| |\n' |
| 12471 |
'+---------------------------+-----------------------------------+---------+\n' |
| 12472 |
'\n' |
| 12473 |
'Notes:\n' |
| 12474 |
'\n' |
| 12475 |
'1. A backslash can be added at the end of a line to ignore the\n' |
| 12476 |
' newline:\n' |
| 12477 |
'\n' |
| 12478 |
" >>> 'This string will not include \\\n" |
| 12479 |
" ... backslashes or newline characters.'\n" |
| 12480 |
" 'This string will not include backslashes or newline " |
| 12481 |
"characters.'\n" |
| 12482 |
'\n' |
| 12483 |
' The same result can be achieved using triple-quoted strings, ' |
| 12484 |
'or\n' |
| 12485 |
' parentheses and string literal concatenation.\n' |
| 12486 |
'\n' |
| 12487 |
'2. As in Standard C, up to three octal digits are accepted.\n' |
| 12488 |
'\n' |
| 12489 |
' Changed in version 3.11: Octal escapes with value larger than\n' |
| 12490 |
' "0o377" produce a "DeprecationWarning". In a future Python ' |
| 12491 |
'version\n' |
| 12492 |
' they will be a "SyntaxWarning" and eventually a ' |
| 12493 |
'"SyntaxError".\n' |
| 12494 |
'\n' |
| 12495 |
'3. Unlike in Standard C, exactly two hex digits are required.\n' |
| 12496 |
'\n' |
| 12497 |
'4. In a bytes literal, hexadecimal and octal escapes denote the ' |
| 12498 |
'byte\n' |
| 12499 |
' with the given value. In a string literal, these escapes ' |
| 12500 |
'denote a\n' |
| 12501 |
' Unicode character with the given value.\n' |
| 12502 |
'\n' |
| 12503 |
'5. Changed in version 3.3: Support for name aliases [1] has been\n' |
| 12504 |
' added.\n' |
| 12505 |
'\n' |
| 12506 |
'6. Exactly four hex digits are required.\n' |
| 12507 |
'\n' |
| 12508 |
'7. Any Unicode character can be encoded this way. Exactly eight ' |
| 12509 |
'hex\n' |
| 12510 |
' digits are required.\n' |
| 12511 |
'\n' |
| 12512 |
'Unlike Standard C, all unrecognized escape sequences are left in ' |
| 12513 |
'the\n' |
| 12514 |
'string unchanged, i.e., *the backslash is left in the result*. ' |
| 12515 |
'(This\n' |
| 12516 |
'behavior is useful when debugging: if an escape sequence is ' |
| 12517 |
'mistyped,\n' |
| 12518 |
'the resulting output is more easily recognized as broken.) It is ' |
| 12519 |
'also\n' |
| 12520 |
'important to note that the escape sequences only recognized in ' |
| 12521 |
'string\n' |
| 12522 |
'literals fall into the category of unrecognized escapes for ' |
| 12523 |
'bytes\n' |
| 12524 |
'literals.\n' |
| 12525 |
'\n' |
| 12526 |
'Changed in version 3.6: Unrecognized escape sequences produce a\n' |
| 12527 |
'"DeprecationWarning". In a future Python version they will be a\n' |
| 12528 |
'"SyntaxWarning" and eventually a "SyntaxError".\n' |
| 12529 |
'\n' |
| 12530 |
'Even in a raw literal, quotes can be escaped with a backslash, ' |
| 12531 |
'but the\n' |
| 12532 |
'backslash remains in the result; for example, "r"\\""" is a ' |
| 12533 |
'valid\n' |
| 12534 |
'string literal consisting of two characters: a backslash and a ' |
| 12535 |
'double\n' |
| 12536 |
'quote; "r"\\"" is not a valid string literal (even a raw string ' |
| 12537 |
'cannot\n' |
| 12538 |
'end in an odd number of backslashes). Specifically, *a raw ' |
| 12539 |
'literal\n' |
| 12540 |
'cannot end in a single backslash* (since the backslash would ' |
| 12541 |
'escape\n' |
| 12542 |
'the following quote character). Note also that a single ' |
| 12543 |
'backslash\n' |
| 12544 |
'followed by a newline is interpreted as those two characters as ' |
| 12545 |
'part\n' |
| 12546 |
'of the literal, *not* as a line continuation.\n', |
| 12547 |
'subscriptions': 'Subscriptions\n' |
| 12548 |
'*************\n' |
| 12549 |
'\n' |
| 12550 |
'The subscription of an instance of a container class will ' |
| 12551 |
'generally\n' |
| 12552 |
'select an element from the container. The subscription of a ' |
| 12553 |
'*generic\n' |
| 12554 |
'class* will generally return a GenericAlias object.\n' |
| 12555 |
'\n' |
| 12556 |
' subscription ::= primary "[" expression_list "]"\n' |
| 12557 |
'\n' |
| 12558 |
'When an object is subscripted, the interpreter will ' |
| 12559 |
'evaluate the\n' |
| 12560 |
'primary and the expression list.\n' |
| 12561 |
'\n' |
| 12562 |
'The primary must evaluate to an object that supports ' |
| 12563 |
'subscription. An\n' |
| 12564 |
'object may support subscription through defining one or ' |
| 12565 |
'both of\n' |
| 12566 |
'"__getitem__()" and "__class_getitem__()". When the primary ' |
| 12567 |
'is\n' |
| 12568 |
'subscripted, the evaluated result of the expression list ' |
| 12569 |
'will be\n' |
| 12570 |
'passed to one of these methods. For more details on when\n' |
| 12571 |
'"__class_getitem__" is called instead of "__getitem__", ' |
| 12572 |
'see\n' |
| 12573 |
'__class_getitem__ versus __getitem__.\n' |
| 12574 |
'\n' |
| 12575 |
'If the expression list contains at least one comma, it will ' |
| 12576 |
'evaluate\n' |
| 12577 |
'to a "tuple" containing the items of the expression list. ' |
| 12578 |
'Otherwise,\n' |
| 12579 |
'the expression list will evaluate to the value of the ' |
| 12580 |
'list’s sole\n' |
| 12581 |
'member.\n' |
| 12582 |
'\n' |
| 12583 |
'For built-in objects, there are two types of objects that ' |
| 12584 |
'support\n' |
| 12585 |
'subscription via "__getitem__()":\n' |
| 12586 |
'\n' |
| 12587 |
'1. Mappings. If the primary is a *mapping*, the expression ' |
| 12588 |
'list must\n' |
| 12589 |
' evaluate to an object whose value is one of the keys of ' |
| 12590 |
'the\n' |
| 12591 |
' mapping, and the subscription selects the value in the ' |
| 12592 |
'mapping that\n' |
| 12593 |
' corresponds to that key. An example of a builtin mapping ' |
| 12594 |
'class is\n' |
| 12595 |
' the "dict" class.\n' |
| 12596 |
'\n' |
| 12597 |
'2. Sequences. If the primary is a *sequence*, the ' |
| 12598 |
'expression list must\n' |
| 12599 |
' evaluate to an "int" or a "slice" (as discussed in the ' |
| 12600 |
'following\n' |
| 12601 |
' section). Examples of builtin sequence classes include ' |
| 12602 |
'the "str",\n' |
| 12603 |
' "list" and "tuple" classes.\n' |
| 12604 |
'\n' |
| 12605 |
'The formal syntax makes no special provision for negative ' |
| 12606 |
'indices in\n' |
| 12607 |
'*sequences*. However, built-in sequences all provide a ' |
| 12608 |
'"__getitem__()"\n' |
| 12609 |
'method that interprets negative indices by adding the ' |
| 12610 |
'length of the\n' |
| 12611 |
'sequence to the index so that, for example, "x[-1]" selects ' |
| 12612 |
'the last\n' |
| 12613 |
'item of "x". The resulting value must be a nonnegative ' |
| 12614 |
'integer less\n' |
| 12615 |
'than the number of items in the sequence, and the ' |
| 12616 |
'subscription selects\n' |
| 12617 |
'the item whose index is that value (counting from zero). ' |
| 12618 |
'Since the\n' |
| 12619 |
'support for negative indices and slicing occurs in the ' |
| 12620 |
'object’s\n' |
| 12621 |
'"__getitem__()" method, subclasses overriding this method ' |
| 12622 |
'will need to\n' |
| 12623 |
'explicitly add that support.\n' |
| 12624 |
'\n' |
| 12625 |
'A "string" is a special kind of sequence whose items are ' |
| 12626 |
'*characters*.\n' |
| 12627 |
'A character is not a separate data type but a string of ' |
| 12628 |
'exactly one\n' |
| 12629 |
'character.\n', |
| 12630 |
'truth': 'Truth Value Testing\n' |
| 12631 |
'*******************\n' |
| 12632 |
'\n' |
| 12633 |
'Any object can be tested for truth value, for use in an "if" or\n' |
| 12634 |
'"while" condition or as operand of the Boolean operations below.\n' |
| 12635 |
'\n' |
| 12636 |
'By default, an object is considered true unless its class defines\n' |
| 12637 |
'either a "__bool__()" method that returns "False" or a "__len__()"\n' |
| 12638 |
'method that returns zero, when called with the object. [1] Here ' |
| 12639 |
'are\n' |
| 12640 |
'most of the built-in objects considered false:\n' |
| 12641 |
'\n' |
| 12642 |
'* constants defined to be false: "None" and "False"\n' |
| 12643 |
'\n' |
| 12644 |
'* zero of any numeric type: "0", "0.0", "0j", "Decimal(0)",\n' |
| 12645 |
' "Fraction(0, 1)"\n' |
| 12646 |
'\n' |
| 12647 |
'* empty sequences and collections: "\'\'", "()", "[]", "{}", ' |
| 12648 |
'"set()",\n' |
| 12649 |
' "range(0)"\n' |
| 12650 |
'\n' |
| 12651 |
'Operations and built-in functions that have a Boolean result ' |
| 12652 |
'always\n' |
| 12653 |
'return "0" or "False" for false and "1" or "True" for true, unless\n' |
| 12654 |
'otherwise stated. (Important exception: the Boolean operations ' |
| 12655 |
'"or"\n' |
| 12656 |
'and "and" always return one of their operands.)\n', |
| 12657 |
'try': 'The "try" statement\n' |
| 12658 |
'*******************\n' |
| 12659 |
'\n' |
| 12660 |
'The "try" statement specifies exception handlers and/or cleanup code\n' |
| 12661 |
'for a group of statements:\n' |
| 12662 |
'\n' |
| 12663 |
' try_stmt ::= try1_stmt | try2_stmt | try3_stmt\n' |
| 12664 |
' try1_stmt ::= "try" ":" suite\n' |
| 12665 |
' ("except" [expression ["as" identifier]] ":" ' |
| 12666 |
'suite)+\n' |
| 12667 |
' ["else" ":" suite]\n' |
| 12668 |
' ["finally" ":" suite]\n' |
| 12669 |
' try2_stmt ::= "try" ":" suite\n' |
| 12670 |
' ("except" "*" expression ["as" identifier] ":" ' |
| 12671 |
'suite)+\n' |
| 12672 |
' ["else" ":" suite]\n' |
| 12673 |
' ["finally" ":" suite]\n' |
| 12674 |
' try3_stmt ::= "try" ":" suite\n' |
| 12675 |
' "finally" ":" suite\n' |
| 12676 |
'\n' |
| 12677 |
'Additional information on exceptions can be found in section\n' |
| 12678 |
'Exceptions, and information on using the "raise" statement to ' |
| 12679 |
'generate\n' |
| 12680 |
'exceptions may be found in section The raise statement.\n' |
| 12681 |
'\n' |
| 12682 |
'\n' |
| 12683 |
'"except" clause\n' |
| 12684 |
'===============\n' |
| 12685 |
'\n' |
| 12686 |
'The "except" clause(s) specify one or more exception handlers. When ' |
| 12687 |
'no\n' |
| 12688 |
'exception occurs in the "try" clause, no exception handler is\n' |
| 12689 |
'executed. When an exception occurs in the "try" suite, a search for ' |
| 12690 |
'an\n' |
| 12691 |
'exception handler is started. This search inspects the "except"\n' |
| 12692 |
'clauses in turn until one is found that matches the exception. An\n' |
| 12693 |
'expression-less "except" clause, if present, must be last; it ' |
| 12694 |
'matches\n' |
| 12695 |
'any exception. For an "except" clause with an expression, that\n' |
| 12696 |
'expression is evaluated, and the clause matches the exception if the\n' |
| 12697 |
'resulting object is “compatible” with the exception. An object is\n' |
| 12698 |
'compatible with an exception if the object is the class or a *non-\n' |
| 12699 |
'virtual base class* of the exception object, or a tuple containing ' |
| 12700 |
'an\n' |
| 12701 |
'item that is the class or a non-virtual base class of the exception\n' |
| 12702 |
'object.\n' |
| 12703 |
'\n' |
| 12704 |
'If no "except" clause matches the exception, the search for an\n' |
| 12705 |
'exception handler continues in the surrounding code and on the\n' |
| 12706 |
'invocation stack. [1]\n' |
| 12707 |
'\n' |
| 12708 |
'If the evaluation of an expression in the header of an "except" ' |
| 12709 |
'clause\n' |
| 12710 |
'raises an exception, the original search for a handler is canceled ' |
| 12711 |
'and\n' |
| 12712 |
'a search starts for the new exception in the surrounding code and on\n' |
| 12713 |
'the call stack (it is treated as if the entire "try" statement ' |
| 12714 |
'raised\n' |
| 12715 |
'the exception).\n' |
| 12716 |
'\n' |
| 12717 |
'When a matching "except" clause is found, the exception is assigned ' |
| 12718 |
'to\n' |
| 12719 |
'the target specified after the "as" keyword in that "except" clause,\n' |
| 12720 |
'if present, and the "except" clause’s suite is executed. All ' |
| 12721 |
'"except"\n' |
| 12722 |
'clauses must have an executable block. When the end of this block is\n' |
| 12723 |
'reached, execution continues normally after the entire "try"\n' |
| 12724 |
'statement. (This means that if two nested handlers exist for the ' |
| 12725 |
'same\n' |
| 12726 |
'exception, and the exception occurs in the "try" clause of the inner\n' |
| 12727 |
'handler, the outer handler will not handle the exception.)\n' |
| 12728 |
'\n' |
| 12729 |
'When an exception has been assigned using "as target", it is cleared\n' |
| 12730 |
'at the end of the "except" clause. This is as if\n' |
| 12731 |
'\n' |
| 12732 |
' except E as N:\n' |
| 12733 |
' foo\n' |
| 12734 |
'\n' |
| 12735 |
'was translated to\n' |
| 12736 |
'\n' |
| 12737 |
' except E as N:\n' |
| 12738 |
' try:\n' |
| 12739 |
' foo\n' |
| 12740 |
' finally:\n' |
| 12741 |
' del N\n' |
| 12742 |
'\n' |
| 12743 |
'This means the exception must be assigned to a different name to be\n' |
| 12744 |
'able to refer to it after the "except" clause. Exceptions are ' |
| 12745 |
'cleared\n' |
| 12746 |
'because with the traceback attached to them, they form a reference\n' |
| 12747 |
'cycle with the stack frame, keeping all locals in that frame alive\n' |
| 12748 |
'until the next garbage collection occurs.\n' |
| 12749 |
'\n' |
| 12750 |
'Before an "except" clause’s suite is executed, the exception is ' |
| 12751 |
'stored\n' |
| 12752 |
'in the "sys" module, where it can be accessed from within the body ' |
| 12753 |
'of\n' |
| 12754 |
'the "except" clause by calling "sys.exception()". When leaving an\n' |
| 12755 |
'exception handler, the exception stored in the "sys" module is reset\n' |
| 12756 |
'to its previous value:\n' |
| 12757 |
'\n' |
| 12758 |
' >>> print(sys.exception())\n' |
| 12759 |
' None\n' |
| 12760 |
' >>> try:\n' |
| 12761 |
' ... raise TypeError\n' |
| 12762 |
' ... except:\n' |
| 12763 |
' ... print(repr(sys.exception()))\n' |
| 12764 |
' ... try:\n' |
| 12765 |
' ... raise ValueError\n' |
| 12766 |
' ... except:\n' |
| 12767 |
' ... print(repr(sys.exception()))\n' |
| 12768 |
' ... print(repr(sys.exception()))\n' |
| 12769 |
' ...\n' |
| 12770 |
' TypeError()\n' |
| 12771 |
' ValueError()\n' |
| 12772 |
' TypeError()\n' |
| 12773 |
' >>> print(sys.exception())\n' |
| 12774 |
' None\n' |
| 12775 |
'\n' |
| 12776 |
'\n' |
| 12777 |
'"except*" clause\n' |
| 12778 |
'================\n' |
| 12779 |
'\n' |
| 12780 |
'The "except*" clause(s) are used for handling "ExceptionGroup"s. The\n' |
| 12781 |
'exception type for matching is interpreted as in the case of ' |
| 12782 |
'"except",\n' |
| 12783 |
'but in the case of exception groups we can have partial matches when\n' |
| 12784 |
'the type matches some of the exceptions in the group. This means ' |
| 12785 |
'that\n' |
| 12786 |
'multiple "except*" clauses can execute, each handling part of the\n' |
| 12787 |
'exception group. Each clause executes at most once and handles an\n' |
| 12788 |
'exception group of all matching exceptions. Each exception in the\n' |
| 12789 |
'group is handled by at most one "except*" clause, the first that\n' |
| 12790 |
'matches it.\n' |
| 12791 |
'\n' |
| 12792 |
' >>> try:\n' |
| 12793 |
' ... raise ExceptionGroup("eg",\n' |
| 12794 |
' ... [ValueError(1), TypeError(2), OSError(3), ' |
| 12795 |
'OSError(4)])\n' |
| 12796 |
' ... except* TypeError as e:\n' |
| 12797 |
" ... print(f'caught {type(e)} with nested {e.exceptions}')\n" |
| 12798 |
' ... except* OSError as e:\n' |
| 12799 |
" ... print(f'caught {type(e)} with nested {e.exceptions}')\n" |
| 12800 |
' ...\n' |
| 12801 |
" caught <class 'ExceptionGroup'> with nested (TypeError(2),)\n" |
| 12802 |
" caught <class 'ExceptionGroup'> with nested (OSError(3), " |
| 12803 |
'OSError(4))\n' |
| 12804 |
' + Exception Group Traceback (most recent call last):\n' |
| 12805 |
' | File "<stdin>", line 2, in <module>\n' |
| 12806 |
' | ExceptionGroup: eg\n' |
| 12807 |
' +-+---------------- 1 ----------------\n' |
| 12808 |
' | ValueError: 1\n' |
| 12809 |
' +------------------------------------\n' |
| 12810 |
'\n' |
| 12811 |
'Any remaining exceptions that were not handled by any "except*" ' |
| 12812 |
'clause\n' |
| 12813 |
'are re-raised at the end, combined into an exception group along ' |
| 12814 |
'with\n' |
| 12815 |
'all exceptions that were raised from within "except*" clauses.\n' |
| 12816 |
'\n' |
| 12817 |
'From version 3.11.4, when the entire "ExceptionGroup" is handled and\n' |
| 12818 |
'only one exception is raised from an "except*" clause, this ' |
| 12819 |
'exception\n' |
| 12820 |
'is no longer wrapped to form a new "ExceptionGroup".\n' |
| 12821 |
'\n' |
| 12822 |
'If the raised exception is not an exception group and its type ' |
| 12823 |
'matches\n' |
| 12824 |
'one of the "except*" clauses, it is caught and wrapped by an ' |
| 12825 |
'exception\n' |
| 12826 |
'group with an empty message string.\n' |
| 12827 |
'\n' |
| 12828 |
' >>> try:\n' |
| 12829 |
' ... raise BlockingIOError\n' |
| 12830 |
' ... except* BlockingIOError as e:\n' |
| 12831 |
' ... print(repr(e))\n' |
| 12832 |
' ...\n' |
| 12833 |
" ExceptionGroup('', (BlockingIOError()))\n" |
| 12834 |
'\n' |
| 12835 |
'An "except*" clause must have a matching type, and this type cannot ' |
| 12836 |
'be\n' |
| 12837 |
'a subclass of "BaseExceptionGroup". It is not possible to mix ' |
| 12838 |
'"except"\n' |
| 12839 |
'and "except*" in the same "try". "break", "continue" and "return"\n' |
| 12840 |
'cannot appear in an "except*" clause.\n' |
| 12841 |
'\n' |
| 12842 |
'\n' |
| 12843 |
'"else" clause\n' |
| 12844 |
'=============\n' |
| 12845 |
'\n' |
| 12846 |
'The optional "else" clause is executed if the control flow leaves ' |
| 12847 |
'the\n' |
| 12848 |
'"try" suite, no exception was raised, and no "return", "continue", ' |
| 12849 |
'or\n' |
| 12850 |
'"break" statement was executed. Exceptions in the "else" clause are\n' |
| 12851 |
'not handled by the preceding "except" clauses.\n' |
| 12852 |
'\n' |
| 12853 |
'\n' |
| 12854 |
'"finally" clause\n' |
| 12855 |
'================\n' |
| 12856 |
'\n' |
| 12857 |
'If "finally" is present, it specifies a ‘cleanup’ handler. The ' |
| 12858 |
'"try"\n' |
| 12859 |
'clause is executed, including any "except" and "else" clauses. If ' |
| 12860 |
'an\n' |
| 12861 |
'exception occurs in any of the clauses and is not handled, the\n' |
| 12862 |
'exception is temporarily saved. The "finally" clause is executed. ' |
| 12863 |
'If\n' |
| 12864 |
'there is a saved exception it is re-raised at the end of the ' |
| 12865 |
'"finally"\n' |
| 12866 |
'clause. If the "finally" clause raises another exception, the saved\n' |
| 12867 |
'exception is set as the context of the new exception. If the ' |
| 12868 |
'"finally"\n' |
| 12869 |
'clause executes a "return", "break" or "continue" statement, the ' |
| 12870 |
'saved\n' |
| 12871 |
'exception is discarded:\n' |
| 12872 |
'\n' |
| 12873 |
' >>> def f():\n' |
| 12874 |
' ... try:\n' |
| 12875 |
' ... 1/0\n' |
| 12876 |
' ... finally:\n' |
| 12877 |
' ... return 42\n' |
| 12878 |
' ...\n' |
| 12879 |
' >>> f()\n' |
| 12880 |
' 42\n' |
| 12881 |
'\n' |
| 12882 |
'The exception information is not available to the program during\n' |
| 12883 |
'execution of the "finally" clause.\n' |
| 12884 |
'\n' |
| 12885 |
'When a "return", "break" or "continue" statement is executed in the\n' |
| 12886 |
'"try" suite of a "try"…"finally" statement, the "finally" clause is\n' |
| 12887 |
'also executed ‘on the way out.’\n' |
| 12888 |
'\n' |
| 12889 |
'The return value of a function is determined by the last "return"\n' |
| 12890 |
'statement executed. Since the "finally" clause always executes, a\n' |
| 12891 |
'"return" statement executed in the "finally" clause will always be ' |
| 12892 |
'the\n' |
| 12893 |
'last one executed:\n' |
| 12894 |
'\n' |
| 12895 |
' >>> def foo():\n' |
| 12896 |
' ... try:\n' |
| 12897 |
" ... return 'try'\n" |
| 12898 |
' ... finally:\n' |
| 12899 |
" ... return 'finally'\n" |
| 12900 |
' ...\n' |
| 12901 |
' >>> foo()\n' |
| 12902 |
" 'finally'\n" |
| 12903 |
'\n' |
| 12904 |
'Changed in version 3.8: Prior to Python 3.8, a "continue" statement\n' |
| 12905 |
'was illegal in the "finally" clause due to a problem with the\n' |
| 12906 |
'implementation.\n', |
| 12907 |
'types': 'The standard type hierarchy\n' |
| 12908 |
'***************************\n' |
| 12909 |
'\n' |
| 12910 |
'Below is a list of the types that are built into Python. ' |
| 12911 |
'Extension\n' |
| 12912 |
'modules (written in C, Java, or other languages, depending on the\n' |
| 12913 |
'implementation) can define additional types. Future versions of\n' |
| 12914 |
'Python may add types to the type hierarchy (e.g., rational ' |
| 12915 |
'numbers,\n' |
| 12916 |
'efficiently stored arrays of integers, etc.), although such ' |
| 12917 |
'additions\n' |
| 12918 |
'will often be provided via the standard library instead.\n' |
| 12919 |
'\n' |
| 12920 |
'Some of the type descriptions below contain a paragraph listing\n' |
| 12921 |
'‘special attributes.’ These are attributes that provide access to ' |
| 12922 |
'the\n' |
| 12923 |
'implementation and are not intended for general use. Their ' |
| 12924 |
'definition\n' |
| 12925 |
'may change in the future.\n' |
| 12926 |
'\n' |
| 12927 |
'\n' |
| 12928 |
'None\n' |
| 12929 |
'====\n' |
| 12930 |
'\n' |
| 12931 |
'This type has a single value. There is a single object with this\n' |
| 12932 |
'value. This object is accessed through the built-in name "None". It ' |
| 12933 |
'is\n' |
| 12934 |
'used to signify the absence of a value in many situations, e.g., it ' |
| 12935 |
'is\n' |
| 12936 |
'returned from functions that don’t explicitly return anything. Its\n' |
| 12937 |
'truth value is false.\n' |
| 12938 |
'\n' |
| 12939 |
'\n' |
| 12940 |
'NotImplemented\n' |
| 12941 |
'==============\n' |
| 12942 |
'\n' |
| 12943 |
'This type has a single value. There is a single object with this\n' |
| 12944 |
'value. This object is accessed through the built-in name\n' |
| 12945 |
'"NotImplemented". Numeric methods and rich comparison methods ' |
| 12946 |
'should\n' |
| 12947 |
'return this value if they do not implement the operation for the\n' |
| 12948 |
'operands provided. (The interpreter will then try the reflected\n' |
| 12949 |
'operation, or some other fallback, depending on the operator.) It\n' |
| 12950 |
'should not be evaluated in a boolean context.\n' |
| 12951 |
'\n' |
| 12952 |
'See Implementing the arithmetic operations for more details.\n' |
| 12953 |
'\n' |
| 12954 |
'Changed in version 3.9: Evaluating "NotImplemented" in a boolean\n' |
| 12955 |
'context is deprecated. While it currently evaluates as true, it ' |
| 12956 |
'will\n' |
| 12957 |
'emit a "DeprecationWarning". It will raise a "TypeError" in a ' |
| 12958 |
'future\n' |
| 12959 |
'version of Python.\n' |
| 12960 |
'\n' |
| 12961 |
'\n' |
| 12962 |
'Ellipsis\n' |
| 12963 |
'========\n' |
| 12964 |
'\n' |
| 12965 |
'This type has a single value. There is a single object with this\n' |
| 12966 |
'value. This object is accessed through the literal "..." or the ' |
| 12967 |
'built-\n' |
| 12968 |
'in name "Ellipsis". Its truth value is true.\n' |
| 12969 |
'\n' |
| 12970 |
'\n' |
| 12971 |
'"numbers.Number"\n' |
| 12972 |
'================\n' |
| 12973 |
'\n' |
| 12974 |
'These are created by numeric literals and returned as results by\n' |
| 12975 |
'arithmetic operators and arithmetic built-in functions. Numeric\n' |
| 12976 |
'objects are immutable; once created their value never changes. ' |
| 12977 |
'Python\n' |
| 12978 |
'numbers are of course strongly related to mathematical numbers, ' |
| 12979 |
'but\n' |
| 12980 |
'subject to the limitations of numerical representation in ' |
| 12981 |
'computers.\n' |
| 12982 |
'\n' |
| 12983 |
'The string representations of the numeric classes, computed by\n' |
| 12984 |
'"__repr__()" and "__str__()", have the following properties:\n' |
| 12985 |
'\n' |
| 12986 |
'* They are valid numeric literals which, when passed to their ' |
| 12987 |
'class\n' |
| 12988 |
' constructor, produce an object having the value of the original\n' |
| 12989 |
' numeric.\n' |
| 12990 |
'\n' |
| 12991 |
'* The representation is in base 10, when possible.\n' |
| 12992 |
'\n' |
| 12993 |
'* Leading zeros, possibly excepting a single zero before a decimal\n' |
| 12994 |
' point, are not shown.\n' |
| 12995 |
'\n' |
| 12996 |
'* Trailing zeros, possibly excepting a single zero after a decimal\n' |
| 12997 |
' point, are not shown.\n' |
| 12998 |
'\n' |
| 12999 |
'* A sign is shown only when the number is negative.\n' |
| 13000 |
'\n' |
| 13001 |
'Python distinguishes between integers, floating point numbers, and\n' |
| 13002 |
'complex numbers:\n' |
| 13003 |
'\n' |
| 13004 |
'\n' |
| 13005 |
'"numbers.Integral"\n' |
| 13006 |
'------------------\n' |
| 13007 |
'\n' |
| 13008 |
'These represent elements from the mathematical set of integers\n' |
| 13009 |
'(positive and negative).\n' |
| 13010 |
'\n' |
| 13011 |
'Note:\n' |
| 13012 |
'\n' |
| 13013 |
' The rules for integer representation are intended to give the ' |
| 13014 |
'most\n' |
| 13015 |
' meaningful interpretation of shift and mask operations involving\n' |
| 13016 |
' negative integers.\n' |
| 13017 |
'\n' |
| 13018 |
'There are two types of integers:\n' |
| 13019 |
'\n' |
| 13020 |
'Integers ("int")\n' |
| 13021 |
' These represent numbers in an unlimited range, subject to ' |
| 13022 |
'available\n' |
| 13023 |
' (virtual) memory only. For the purpose of shift and mask\n' |
| 13024 |
' operations, a binary representation is assumed, and negative\n' |
| 13025 |
' numbers are represented in a variant of 2’s complement which ' |
| 13026 |
'gives\n' |
| 13027 |
' the illusion of an infinite string of sign bits extending to ' |
| 13028 |
'the\n' |
| 13029 |
' left.\n' |
| 13030 |
'\n' |
| 13031 |
'Booleans ("bool")\n' |
| 13032 |
' These represent the truth values False and True. The two ' |
| 13033 |
'objects\n' |
| 13034 |
' representing the values "False" and "True" are the only Boolean\n' |
| 13035 |
' objects. The Boolean type is a subtype of the integer type, and\n' |
| 13036 |
' Boolean values behave like the values 0 and 1, respectively, in\n' |
| 13037 |
' almost all contexts, the exception being that when converted to ' |
| 13038 |
'a\n' |
| 13039 |
' string, the strings ""False"" or ""True"" are returned,\n' |
| 13040 |
' respectively.\n' |
| 13041 |
'\n' |
| 13042 |
'\n' |
| 13043 |
'"numbers.Real" ("float")\n' |
| 13044 |
'------------------------\n' |
| 13045 |
'\n' |
| 13046 |
'These represent machine-level double precision floating point ' |
| 13047 |
'numbers.\n' |
| 13048 |
'You are at the mercy of the underlying machine architecture (and C ' |
| 13049 |
'or\n' |
| 13050 |
'Java implementation) for the accepted range and handling of ' |
| 13051 |
'overflow.\n' |
| 13052 |
'Python does not support single-precision floating point numbers; ' |
| 13053 |
'the\n' |
| 13054 |
'savings in processor and memory usage that are usually the reason ' |
| 13055 |
'for\n' |
| 13056 |
'using these are dwarfed by the overhead of using objects in Python, ' |
| 13057 |
'so\n' |
| 13058 |
'there is no reason to complicate the language with two kinds of\n' |
| 13059 |
'floating point numbers.\n' |
| 13060 |
'\n' |
| 13061 |
'\n' |
| 13062 |
'"numbers.Complex" ("complex")\n' |
| 13063 |
'-----------------------------\n' |
| 13064 |
'\n' |
| 13065 |
'These represent complex numbers as a pair of machine-level double\n' |
| 13066 |
'precision floating point numbers. The same caveats apply as for\n' |
| 13067 |
'floating point numbers. The real and imaginary parts of a complex\n' |
| 13068 |
'number "z" can be retrieved through the read-only attributes ' |
| 13069 |
'"z.real"\n' |
| 13070 |
'and "z.imag".\n' |
| 13071 |
'\n' |
| 13072 |
'\n' |
| 13073 |
'Sequences\n' |
| 13074 |
'=========\n' |
| 13075 |
'\n' |
| 13076 |
'These represent finite ordered sets indexed by non-negative ' |
| 13077 |
'numbers.\n' |
| 13078 |
'The built-in function "len()" returns the number of items of a\n' |
| 13079 |
'sequence. When the length of a sequence is *n*, the index set ' |
| 13080 |
'contains\n' |
| 13081 |
'the numbers 0, 1, …, *n*-1. Item *i* of sequence *a* is selected ' |
| 13082 |
'by\n' |
| 13083 |
'"a[i]". Some sequences, including built-in sequences, interpret\n' |
| 13084 |
'negative subscripts by adding the sequence length. For example,\n' |
| 13085 |
'"a[-2]" equals "a[n-2]", the second to last item of sequence a ' |
| 13086 |
'with\n' |
| 13087 |
'length "n".\n' |
| 13088 |
'\n' |
| 13089 |
'Sequences also support slicing: "a[i:j]" selects all items with ' |
| 13090 |
'index\n' |
| 13091 |
'*k* such that *i* "<=" *k* "<" *j*. When used as an expression, a\n' |
| 13092 |
'slice is a sequence of the same type. The comment above about ' |
| 13093 |
'negative\n' |
| 13094 |
'indexes also applies to negative slice positions.\n' |
| 13095 |
'\n' |
| 13096 |
'Some sequences also support “extended slicing” with a third “step”\n' |
| 13097 |
'parameter: "a[i:j:k]" selects all items of *a* with index *x* where ' |
| 13098 |
'"x\n' |
| 13099 |
'= i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*.\n' |
| 13100 |
'\n' |
| 13101 |
'Sequences are distinguished according to their mutability:\n' |
| 13102 |
'\n' |
| 13103 |
'\n' |
| 13104 |
'Immutable sequences\n' |
| 13105 |
'-------------------\n' |
| 13106 |
'\n' |
| 13107 |
'An object of an immutable sequence type cannot change once it is\n' |
| 13108 |
'created. (If the object contains references to other objects, ' |
| 13109 |
'these\n' |
| 13110 |
'other objects may be mutable and may be changed; however, the\n' |
| 13111 |
'collection of objects directly referenced by an immutable object\n' |
| 13112 |
'cannot change.)\n' |
| 13113 |
'\n' |
| 13114 |
'The following types are immutable sequences:\n' |
| 13115 |
'\n' |
| 13116 |
'Strings\n' |
| 13117 |
' A string is a sequence of values that represent Unicode code\n' |
| 13118 |
' points. All the code points in the range "U+0000 - U+10FFFF" can ' |
| 13119 |
'be\n' |
| 13120 |
' represented in a string. Python doesn’t have a char type; ' |
| 13121 |
'instead,\n' |
| 13122 |
' every code point in the string is represented as a string ' |
| 13123 |
'object\n' |
| 13124 |
' with length "1". The built-in function "ord()" converts a code\n' |
| 13125 |
' point from its string form to an integer in the range "0 - ' |
| 13126 |
'10FFFF";\n' |
| 13127 |
' "chr()" converts an integer in the range "0 - 10FFFF" to the\n' |
| 13128 |
' corresponding length "1" string object. "str.encode()" can be ' |
| 13129 |
'used\n' |
| 13130 |
' to convert a "str" to "bytes" using the given text encoding, ' |
| 13131 |
'and\n' |
| 13132 |
' "bytes.decode()" can be used to achieve the opposite.\n' |
| 13133 |
'\n' |
| 13134 |
'Tuples\n' |
| 13135 |
' The items of a tuple are arbitrary Python objects. Tuples of two ' |
| 13136 |
'or\n' |
| 13137 |
' more items are formed by comma-separated lists of expressions. ' |
| 13138 |
'A\n' |
| 13139 |
' tuple of one item (a ‘singleton’) can be formed by affixing a ' |
| 13140 |
'comma\n' |
| 13141 |
' to an expression (an expression by itself does not create a ' |
| 13142 |
'tuple,\n' |
| 13143 |
' since parentheses must be usable for grouping of expressions). ' |
| 13144 |
'An\n' |
| 13145 |
' empty tuple can be formed by an empty pair of parentheses.\n' |
| 13146 |
'\n' |
| 13147 |
'Bytes\n' |
| 13148 |
' A bytes object is an immutable array. The items are 8-bit ' |
| 13149 |
'bytes,\n' |
| 13150 |
' represented by integers in the range 0 <= x < 256. Bytes ' |
| 13151 |
'literals\n' |
| 13152 |
' (like "b\'abc\'") and the built-in "bytes()" constructor can be ' |
| 13153 |
'used\n' |
| 13154 |
' to create bytes objects. Also, bytes objects can be decoded to\n' |
| 13155 |
' strings via the "decode()" method.\n' |
| 13156 |
'\n' |
| 13157 |
'\n' |
| 13158 |
'Mutable sequences\n' |
| 13159 |
'-----------------\n' |
| 13160 |
'\n' |
| 13161 |
'Mutable sequences can be changed after they are created. The\n' |
| 13162 |
'subscription and slicing notations can be used as the target of\n' |
| 13163 |
'assignment and "del" (delete) statements.\n' |
| 13164 |
'\n' |
| 13165 |
'Note:\n' |
| 13166 |
'\n' |
| 13167 |
' The "collections" and "array" module provide additional examples ' |
| 13168 |
'of\n' |
| 13169 |
' mutable sequence types.\n' |
| 13170 |
'\n' |
| 13171 |
'There are currently two intrinsic mutable sequence types:\n' |
| 13172 |
'\n' |
| 13173 |
'Lists\n' |
| 13174 |
' The items of a list are arbitrary Python objects. Lists are ' |
| 13175 |
'formed\n' |
| 13176 |
' by placing a comma-separated list of expressions in square\n' |
| 13177 |
' brackets. (Note that there are no special cases needed to form\n' |
| 13178 |
' lists of length 0 or 1.)\n' |
| 13179 |
'\n' |
| 13180 |
'Byte Arrays\n' |
| 13181 |
' A bytearray object is a mutable array. They are created by the\n' |
| 13182 |
' built-in "bytearray()" constructor. Aside from being mutable ' |
| 13183 |
'(and\n' |
| 13184 |
' hence unhashable), byte arrays otherwise provide the same ' |
| 13185 |
'interface\n' |
| 13186 |
' and functionality as immutable "bytes" objects.\n' |
| 13187 |
'\n' |
| 13188 |
'\n' |
| 13189 |
'Set types\n' |
| 13190 |
'=========\n' |
| 13191 |
'\n' |
| 13192 |
'These represent unordered, finite sets of unique, immutable ' |
| 13193 |
'objects.\n' |
| 13194 |
'As such, they cannot be indexed by any subscript. However, they can ' |
| 13195 |
'be\n' |
| 13196 |
'iterated over, and the built-in function "len()" returns the number ' |
| 13197 |
'of\n' |
| 13198 |
'items in a set. Common uses for sets are fast membership testing,\n' |
| 13199 |
'removing duplicates from a sequence, and computing mathematical\n' |
| 13200 |
'operations such as intersection, union, difference, and symmetric\n' |
| 13201 |
'difference.\n' |
| 13202 |
'\n' |
| 13203 |
'For set elements, the same immutability rules apply as for ' |
| 13204 |
'dictionary\n' |
| 13205 |
'keys. Note that numeric types obey the normal rules for numeric\n' |
| 13206 |
'comparison: if two numbers compare equal (e.g., "1" and "1.0"), ' |
| 13207 |
'only\n' |
| 13208 |
'one of them can be contained in a set.\n' |
| 13209 |
'\n' |
| 13210 |
'There are currently two intrinsic set types:\n' |
| 13211 |
'\n' |
| 13212 |
'Sets\n' |
| 13213 |
' These represent a mutable set. They are created by the built-in\n' |
| 13214 |
' "set()" constructor and can be modified afterwards by several\n' |
| 13215 |
' methods, such as "add()".\n' |
| 13216 |
'\n' |
| 13217 |
'Frozen sets\n' |
| 13218 |
' These represent an immutable set. They are created by the ' |
| 13219 |
'built-in\n' |
| 13220 |
' "frozenset()" constructor. As a frozenset is immutable and\n' |
| 13221 |
' *hashable*, it can be used again as an element of another set, ' |
| 13222 |
'or\n' |
| 13223 |
' as a dictionary key.\n' |
| 13224 |
'\n' |
| 13225 |
'\n' |
| 13226 |
'Mappings\n' |
| 13227 |
'========\n' |
| 13228 |
'\n' |
| 13229 |
'These represent finite sets of objects indexed by arbitrary index\n' |
| 13230 |
'sets. The subscript notation "a[k]" selects the item indexed by ' |
| 13231 |
'"k"\n' |
| 13232 |
'from the mapping "a"; this can be used in expressions and as the\n' |
| 13233 |
'target of assignments or "del" statements. The built-in function\n' |
| 13234 |
'"len()" returns the number of items in a mapping.\n' |
| 13235 |
'\n' |
| 13236 |
'There is currently a single intrinsic mapping type:\n' |
| 13237 |
'\n' |
| 13238 |
'\n' |
| 13239 |
'Dictionaries\n' |
| 13240 |
'------------\n' |
| 13241 |
'\n' |
| 13242 |
'These represent finite sets of objects indexed by nearly arbitrary\n' |
| 13243 |
'values. The only types of values not acceptable as keys are ' |
| 13244 |
'values\n' |
| 13245 |
'containing lists or dictionaries or other mutable types that are\n' |
| 13246 |
'compared by value rather than by object identity, the reason being\n' |
| 13247 |
'that the efficient implementation of dictionaries requires a key’s\n' |
| 13248 |
'hash value to remain constant. Numeric types used for keys obey ' |
| 13249 |
'the\n' |
| 13250 |
'normal rules for numeric comparison: if two numbers compare equal\n' |
| 13251 |
'(e.g., "1" and "1.0") then they can be used interchangeably to ' |
| 13252 |
'index\n' |
| 13253 |
'the same dictionary entry.\n' |
| 13254 |
'\n' |
| 13255 |
'Dictionaries preserve insertion order, meaning that keys will be\n' |
| 13256 |
'produced in the same order they were added sequentially over the\n' |
| 13257 |
'dictionary. Replacing an existing key does not change the order,\n' |
| 13258 |
'however removing a key and re-inserting it will add it to the end\n' |
| 13259 |
'instead of keeping its old place.\n' |
| 13260 |
'\n' |
| 13261 |
'Dictionaries are mutable; they can be created by the "{...}" ' |
| 13262 |
'notation\n' |
| 13263 |
'(see section Dictionary displays).\n' |
| 13264 |
'\n' |
| 13265 |
'The extension modules "dbm.ndbm" and "dbm.gnu" provide additional\n' |
| 13266 |
'examples of mapping types, as does the "collections" module.\n' |
| 13267 |
'\n' |
| 13268 |
'Changed in version 3.7: Dictionaries did not preserve insertion ' |
| 13269 |
'order\n' |
| 13270 |
'in versions of Python before 3.6. In CPython 3.6, insertion order ' |
| 13271 |
'was\n' |
| 13272 |
'preserved, but it was considered an implementation detail at that ' |
| 13273 |
'time\n' |
| 13274 |
'rather than a language guarantee.\n' |
| 13275 |
'\n' |
| 13276 |
'\n' |
| 13277 |
'Callable types\n' |
| 13278 |
'==============\n' |
| 13279 |
'\n' |
| 13280 |
'These are the types to which the function call operation (see ' |
| 13281 |
'section\n' |
| 13282 |
'Calls) can be applied:\n' |
| 13283 |
'\n' |
| 13284 |
'\n' |
| 13285 |
'User-defined functions\n' |
| 13286 |
'----------------------\n' |
| 13287 |
'\n' |
| 13288 |
'A user-defined function object is created by a function definition\n' |
| 13289 |
'(see section Function definitions). It should be called with an\n' |
| 13290 |
'argument list containing the same number of items as the ' |
| 13291 |
'function’s\n' |
| 13292 |
'formal parameter list.\n' |
| 13293 |
'\n' |
| 13294 |
'\n' |
| 13295 |
'Special read-only attributes\n' |
| 13296 |
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' |
| 13297 |
'\n' |
| 13298 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13299 |
'| Attribute | ' |
| 13300 |
'Meaning |\n' |
| 13301 |
'|====================================================|====================================================|\n' |
| 13302 |
'| function.__globals__ | A reference ' |
| 13303 |
'to the "dictionary" that holds the |\n' |
| 13304 |
'| | function’s ' |
| 13305 |
'global variables – the global namespace |\n' |
| 13306 |
'| | of the ' |
| 13307 |
'module in which the function was defined. |\n' |
| 13308 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13309 |
'| function.__closure__ | "None" or a ' |
| 13310 |
'"tuple" of cells that contain bindings |\n' |
| 13311 |
'| | for the ' |
| 13312 |
'function’s free variables. A cell object |\n' |
| 13313 |
'| | has the ' |
| 13314 |
'attribute "cell_contents". This can be |\n' |
| 13315 |
'| | used to get ' |
| 13316 |
'the value of the cell, as well as set |\n' |
| 13317 |
'| | the ' |
| 13318 |
'value. |\n' |
| 13319 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13320 |
'\n' |
| 13321 |
'\n' |
| 13322 |
'Special writable attributes\n' |
| 13323 |
'~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' |
| 13324 |
'\n' |
| 13325 |
'Most of these attributes check the type of the assigned value:\n' |
| 13326 |
'\n' |
| 13327 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13328 |
'| Attribute | ' |
| 13329 |
'Meaning |\n' |
| 13330 |
'|====================================================|====================================================|\n' |
| 13331 |
'| function.__doc__ | The ' |
| 13332 |
'function’s documentation string, or "None" if |\n' |
| 13333 |
'| | unavailable. ' |
| 13334 |
'Not inherited by subclasses. |\n' |
| 13335 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13336 |
'| function.__name__ | The ' |
| 13337 |
'function’s name. See also: "__name__ |\n' |
| 13338 |
'| | ' |
| 13339 |
'attributes". |\n' |
| 13340 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13341 |
'| function.__qualname__ | The ' |
| 13342 |
'function’s *qualified name*. See also: |\n' |
| 13343 |
'| | ' |
| 13344 |
'"__qualname__ attributes". New in version 3.3. |\n' |
| 13345 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13346 |
'| function.__module__ | The name of ' |
| 13347 |
'the module the function was defined |\n' |
| 13348 |
'| | in, or ' |
| 13349 |
'"None" if unavailable. |\n' |
| 13350 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13351 |
'| function.__defaults__ | A "tuple" ' |
| 13352 |
'containing default *parameter* values |\n' |
| 13353 |
'| | for those ' |
| 13354 |
'parameters that have defaults, or "None" |\n' |
| 13355 |
'| | if no ' |
| 13356 |
'parameters have a default value. |\n' |
| 13357 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13358 |
'| function.__code__ | The code ' |
| 13359 |
'object representing the compiled function |\n' |
| 13360 |
'| | ' |
| 13361 |
'body. |\n' |
| 13362 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13363 |
'| function.__dict__ | The ' |
| 13364 |
'namespace supporting arbitrary function |\n' |
| 13365 |
'| | attributes. ' |
| 13366 |
'See also: "__dict__ attributes". |\n' |
| 13367 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13368 |
'| function.__annotations__ | A ' |
| 13369 |
'"dictionary" containing annotations of |\n' |
| 13370 |
'| | ' |
| 13371 |
'*parameters*. The keys of the dictionary are the |\n' |
| 13372 |
'| | parameter ' |
| 13373 |
'names, and "\'return\'" for the return |\n' |
| 13374 |
'| | annotation, ' |
| 13375 |
'if provided. See also: Annotations |\n' |
| 13376 |
'| | Best ' |
| 13377 |
'Practices. |\n' |
| 13378 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13379 |
'| function.__kwdefaults__ | A ' |
| 13380 |
'"dictionary" containing defaults for keyword- |\n' |
| 13381 |
'| | only ' |
| 13382 |
'*parameters*. |\n' |
| 13383 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13384 |
'\n' |
| 13385 |
'Function objects also support getting and setting arbitrary\n' |
| 13386 |
'attributes, which can be used, for example, to attach metadata to\n' |
| 13387 |
'functions. Regular attribute dot-notation is used to get and set ' |
| 13388 |
'such\n' |
| 13389 |
'attributes.\n' |
| 13390 |
'\n' |
| 13391 |
'**CPython implementation detail:** CPython’s current ' |
| 13392 |
'implementation\n' |
| 13393 |
'only supports function attributes on user-defined functions. ' |
| 13394 |
'Function\n' |
| 13395 |
'attributes on built-in functions may be supported in the future.\n' |
| 13396 |
'\n' |
| 13397 |
'Additional information about a function’s definition can be ' |
| 13398 |
'retrieved\n' |
| 13399 |
'from its code object (accessible via the "__code__" attribute).\n' |
| 13400 |
'\n' |
| 13401 |
'\n' |
| 13402 |
'Instance methods\n' |
| 13403 |
'----------------\n' |
| 13404 |
'\n' |
| 13405 |
'An instance method object combines a class, a class instance and ' |
| 13406 |
'any\n' |
| 13407 |
'callable object (normally a user-defined function).\n' |
| 13408 |
'\n' |
| 13409 |
'Special read-only attributes:\n' |
| 13410 |
'\n' |
| 13411 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13412 |
'| method.__self__ | Refers to ' |
| 13413 |
'the class instance object to which the |\n' |
| 13414 |
'| | method is ' |
| 13415 |
'bound |\n' |
| 13416 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13417 |
'| method.__func__ | Refers to ' |
| 13418 |
'the original function object |\n' |
| 13419 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13420 |
'| method.__doc__ | The method’s ' |
| 13421 |
'documentation (same as |\n' |
| 13422 |
'| | ' |
| 13423 |
'"method.__func__.__doc__"). A "string" if the |\n' |
| 13424 |
'| | original ' |
| 13425 |
'function had a docstring, else "None". |\n' |
| 13426 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13427 |
'| method.__name__ | The name of ' |
| 13428 |
'the method (same as |\n' |
| 13429 |
'| | ' |
| 13430 |
'"method.__func__.__name__") |\n' |
| 13431 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13432 |
'| method.__module__ | The name of ' |
| 13433 |
'the module the method was defined in, |\n' |
| 13434 |
'| | or "None" if ' |
| 13435 |
'unavailable. |\n' |
| 13436 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13437 |
'\n' |
| 13438 |
'Methods also support accessing (but not setting) the arbitrary\n' |
| 13439 |
'function attributes on the underlying function object.\n' |
| 13440 |
'\n' |
| 13441 |
'User-defined method objects may be created when getting an ' |
| 13442 |
'attribute\n' |
| 13443 |
'of a class (perhaps via an instance of that class), if that ' |
| 13444 |
'attribute\n' |
| 13445 |
'is a user-defined function object or a "classmethod" object.\n' |
| 13446 |
'\n' |
| 13447 |
'When an instance method object is created by retrieving a ' |
| 13448 |
'user-defined\n' |
| 13449 |
'function object from a class via one of its instances, its ' |
| 13450 |
'"__self__"\n' |
| 13451 |
'attribute is the instance, and the method object is said to be\n' |
| 13452 |
'*bound*. The new method’s "__func__" attribute is the original\n' |
| 13453 |
'function object.\n' |
| 13454 |
'\n' |
| 13455 |
'When an instance method object is created by retrieving a\n' |
| 13456 |
'"classmethod" object from a class or instance, its "__self__"\n' |
| 13457 |
'attribute is the class itself, and its "__func__" attribute is the\n' |
| 13458 |
'function object underlying the class method.\n' |
| 13459 |
'\n' |
| 13460 |
'When an instance method object is called, the underlying function\n' |
| 13461 |
'("__func__") is called, inserting the class instance ("__self__") ' |
| 13462 |
'in\n' |
| 13463 |
'front of the argument list. For instance, when "C" is a class ' |
| 13464 |
'which\n' |
| 13465 |
'contains a definition for a function "f()", and "x" is an instance ' |
| 13466 |
'of\n' |
| 13467 |
'"C", calling "x.f(1)" is equivalent to calling "C.f(x, 1)".\n' |
| 13468 |
'\n' |
| 13469 |
'When an instance method object is derived from a "classmethod" ' |
| 13470 |
'object,\n' |
| 13471 |
'the “class instance” stored in "__self__" will actually be the ' |
| 13472 |
'class\n' |
| 13473 |
'itself, so that calling either "x.f(1)" or "C.f(1)" is equivalent ' |
| 13474 |
'to\n' |
| 13475 |
'calling "f(C,1)" where "f" is the underlying function.\n' |
| 13476 |
'\n' |
| 13477 |
'Note that the transformation from function object to instance ' |
| 13478 |
'method\n' |
| 13479 |
'object happens each time the attribute is retrieved from the ' |
| 13480 |
'instance.\n' |
| 13481 |
'In some cases, a fruitful optimization is to assign the attribute ' |
| 13482 |
'to a\n' |
| 13483 |
'local variable and call that local variable. Also notice that this\n' |
| 13484 |
'transformation only happens for user-defined functions; other ' |
| 13485 |
'callable\n' |
| 13486 |
'objects (and all non-callable objects) are retrieved without\n' |
| 13487 |
'transformation. It is also important to note that user-defined\n' |
| 13488 |
'functions which are attributes of a class instance are not ' |
| 13489 |
'converted\n' |
| 13490 |
'to bound methods; this *only* happens when the function is an\n' |
| 13491 |
'attribute of the class.\n' |
| 13492 |
'\n' |
| 13493 |
'\n' |
| 13494 |
'Generator functions\n' |
| 13495 |
'-------------------\n' |
| 13496 |
'\n' |
| 13497 |
'A function or method which uses the "yield" statement (see section ' |
| 13498 |
'The\n' |
| 13499 |
'yield statement) is called a *generator function*. Such a ' |
| 13500 |
'function,\n' |
| 13501 |
'when called, always returns an *iterator* object which can be used ' |
| 13502 |
'to\n' |
| 13503 |
'execute the body of the function: calling the iterator’s\n' |
| 13504 |
'"iterator.__next__()" method will cause the function to execute ' |
| 13505 |
'until\n' |
| 13506 |
'it provides a value using the "yield" statement. When the ' |
| 13507 |
'function\n' |
| 13508 |
'executes a "return" statement or falls off the end, a ' |
| 13509 |
'"StopIteration"\n' |
| 13510 |
'exception is raised and the iterator will have reached the end of ' |
| 13511 |
'the\n' |
| 13512 |
'set of values to be returned.\n' |
| 13513 |
'\n' |
| 13514 |
'\n' |
| 13515 |
'Coroutine functions\n' |
| 13516 |
'-------------------\n' |
| 13517 |
'\n' |
| 13518 |
'A function or method which is defined using "async def" is called ' |
| 13519 |
'a\n' |
| 13520 |
'*coroutine function*. Such a function, when called, returns a\n' |
| 13521 |
'*coroutine* object. It may contain "await" expressions, as well ' |
| 13522 |
'as\n' |
| 13523 |
'"async with" and "async for" statements. See also the Coroutine\n' |
| 13524 |
'Objects section.\n' |
| 13525 |
'\n' |
| 13526 |
'\n' |
| 13527 |
'Asynchronous generator functions\n' |
| 13528 |
'--------------------------------\n' |
| 13529 |
'\n' |
| 13530 |
'A function or method which is defined using "async def" and which ' |
| 13531 |
'uses\n' |
| 13532 |
'the "yield" statement is called a *asynchronous generator ' |
| 13533 |
'function*.\n' |
| 13534 |
'Such a function, when called, returns an *asynchronous iterator*\n' |
| 13535 |
'object which can be used in an "async for" statement to execute ' |
| 13536 |
'the\n' |
| 13537 |
'body of the function.\n' |
| 13538 |
'\n' |
| 13539 |
'Calling the asynchronous iterator’s "aiterator.__anext__" method ' |
| 13540 |
'will\n' |
| 13541 |
'return an *awaitable* which when awaited will execute until it\n' |
| 13542 |
'provides a value using the "yield" expression. When the function\n' |
| 13543 |
'executes an empty "return" statement or falls off the end, a\n' |
| 13544 |
'"StopAsyncIteration" exception is raised and the asynchronous ' |
| 13545 |
'iterator\n' |
| 13546 |
'will have reached the end of the set of values to be yielded.\n' |
| 13547 |
'\n' |
| 13548 |
'\n' |
| 13549 |
'Built-in functions\n' |
| 13550 |
'------------------\n' |
| 13551 |
'\n' |
| 13552 |
'A built-in function object is a wrapper around a C function. ' |
| 13553 |
'Examples\n' |
| 13554 |
'of built-in functions are "len()" and "math.sin()" ("math" is a\n' |
| 13555 |
'standard built-in module). The number and type of the arguments ' |
| 13556 |
'are\n' |
| 13557 |
'determined by the C function. Special read-only attributes:\n' |
| 13558 |
'\n' |
| 13559 |
'* "__doc__" is the function’s documentation string, or "None" if\n' |
| 13560 |
' unavailable. See "function.__doc__".\n' |
| 13561 |
'\n' |
| 13562 |
'* "__name__" is the function’s name. See "function.__name__".\n' |
| 13563 |
'\n' |
| 13564 |
'* "__self__" is set to "None" (but see the next item).\n' |
| 13565 |
'\n' |
| 13566 |
'* "__module__" is the name of the module the function was defined ' |
| 13567 |
'in\n' |
| 13568 |
' or "None" if unavailable. See "function.__module__".\n' |
| 13569 |
'\n' |
| 13570 |
'\n' |
| 13571 |
'Built-in methods\n' |
| 13572 |
'----------------\n' |
| 13573 |
'\n' |
| 13574 |
'This is really a different disguise of a built-in function, this ' |
| 13575 |
'time\n' |
| 13576 |
'containing an object passed to the C function as an implicit extra\n' |
| 13577 |
'argument. An example of a built-in method is "alist.append()",\n' |
| 13578 |
'assuming *alist* is a list object. In this case, the special ' |
| 13579 |
'read-only\n' |
| 13580 |
'attribute "__self__" is set to the object denoted by *alist*. (The\n' |
| 13581 |
'attribute has the same semantics as it does with "other instance\n' |
| 13582 |
'methods".)\n' |
| 13583 |
'\n' |
| 13584 |
'\n' |
| 13585 |
'Classes\n' |
| 13586 |
'-------\n' |
| 13587 |
'\n' |
| 13588 |
'Classes are callable. These objects normally act as factories for ' |
| 13589 |
'new\n' |
| 13590 |
'instances of themselves, but variations are possible for class ' |
| 13591 |
'types\n' |
| 13592 |
'that override "__new__()". The arguments of the call are passed ' |
| 13593 |
'to\n' |
| 13594 |
'"__new__()" and, in the typical case, to "__init__()" to ' |
| 13595 |
'initialize\n' |
| 13596 |
'the new instance.\n' |
| 13597 |
'\n' |
| 13598 |
'\n' |
| 13599 |
'Class Instances\n' |
| 13600 |
'---------------\n' |
| 13601 |
'\n' |
| 13602 |
'Instances of arbitrary classes can be made callable by defining a\n' |
| 13603 |
'"__call__()" method in their class.\n' |
| 13604 |
'\n' |
| 13605 |
'\n' |
| 13606 |
'Modules\n' |
| 13607 |
'=======\n' |
| 13608 |
'\n' |
| 13609 |
'Modules are a basic organizational unit of Python code, and are\n' |
| 13610 |
'created by the import system as invoked either by the "import"\n' |
| 13611 |
'statement, or by calling functions such as ' |
| 13612 |
'"importlib.import_module()"\n' |
| 13613 |
'and built-in "__import__()". A module object has a namespace\n' |
| 13614 |
'implemented by a "dictionary" object (this is the dictionary\n' |
| 13615 |
'referenced by the "__globals__" attribute of functions defined in ' |
| 13616 |
'the\n' |
| 13617 |
'module). Attribute references are translated to lookups in this\n' |
| 13618 |
'dictionary, e.g., "m.x" is equivalent to "m.__dict__["x"]". A ' |
| 13619 |
'module\n' |
| 13620 |
'object does not contain the code object used to initialize the ' |
| 13621 |
'module\n' |
| 13622 |
'(since it isn’t needed once the initialization is done).\n' |
| 13623 |
'\n' |
| 13624 |
'Attribute assignment updates the module’s namespace dictionary, ' |
| 13625 |
'e.g.,\n' |
| 13626 |
'"m.x = 1" is equivalent to "m.__dict__["x"] = 1".\n' |
| 13627 |
'\n' |
| 13628 |
'Predefined (writable) attributes:\n' |
| 13629 |
'\n' |
| 13630 |
' "__name__"\n' |
| 13631 |
' The module’s name.\n' |
| 13632 |
'\n' |
| 13633 |
' "__doc__"\n' |
| 13634 |
' The module’s documentation string, or "None" if unavailable.\n' |
| 13635 |
'\n' |
| 13636 |
' "__file__"\n' |
| 13637 |
' The pathname of the file from which the module was loaded, if ' |
| 13638 |
'it\n' |
| 13639 |
' was loaded from a file. The "__file__" attribute may be ' |
| 13640 |
'missing\n' |
| 13641 |
' for certain types of modules, such as C modules that are\n' |
| 13642 |
' statically linked into the interpreter. For extension ' |
| 13643 |
'modules\n' |
| 13644 |
' loaded dynamically from a shared library, it’s the pathname ' |
| 13645 |
'of\n' |
| 13646 |
' the shared library file.\n' |
| 13647 |
'\n' |
| 13648 |
' "__annotations__"\n' |
| 13649 |
' A dictionary containing *variable annotations* collected ' |
| 13650 |
'during\n' |
| 13651 |
' module body execution. For best practices on working with\n' |
| 13652 |
' "__annotations__", please see Annotations Best Practices.\n' |
| 13653 |
'\n' |
| 13654 |
'Special read-only attribute: "__dict__" is the module’s namespace ' |
| 13655 |
'as a\n' |
| 13656 |
'dictionary object.\n' |
| 13657 |
'\n' |
| 13658 |
'**CPython implementation detail:** Because of the way CPython ' |
| 13659 |
'clears\n' |
| 13660 |
'module dictionaries, the module dictionary will be cleared when ' |
| 13661 |
'the\n' |
| 13662 |
'module falls out of scope even if the dictionary still has live\n' |
| 13663 |
'references. To avoid this, copy the dictionary or keep the module\n' |
| 13664 |
'around while using its dictionary directly.\n' |
| 13665 |
'\n' |
| 13666 |
'\n' |
| 13667 |
'Custom classes\n' |
| 13668 |
'==============\n' |
| 13669 |
'\n' |
| 13670 |
'Custom class types are typically created by class definitions (see\n' |
| 13671 |
'section Class definitions). A class has a namespace implemented by ' |
| 13672 |
'a\n' |
| 13673 |
'dictionary object. Class attribute references are translated to\n' |
| 13674 |
'lookups in this dictionary, e.g., "C.x" is translated to\n' |
| 13675 |
'"C.__dict__["x"]" (although there are a number of hooks which ' |
| 13676 |
'allow\n' |
| 13677 |
'for other means of locating attributes). When the attribute name ' |
| 13678 |
'is\n' |
| 13679 |
'not found there, the attribute search continues in the base ' |
| 13680 |
'classes.\n' |
| 13681 |
'This search of the base classes uses the C3 method resolution ' |
| 13682 |
'order\n' |
| 13683 |
'which behaves correctly even in the presence of ‘diamond’ ' |
| 13684 |
'inheritance\n' |
| 13685 |
'structures where there are multiple inheritance paths leading back ' |
| 13686 |
'to\n' |
| 13687 |
'a common ancestor. Additional details on the C3 MRO used by Python ' |
| 13688 |
'can\n' |
| 13689 |
'be found in the documentation accompanying the 2.3 release at\n' |
| 13690 |
'https://www.python.org/download/releases/2.3/mro/.\n' |
| 13691 |
'\n' |
| 13692 |
'When a class attribute reference (for class "C", say) would yield ' |
| 13693 |
'a\n' |
| 13694 |
'class method object, it is transformed into an instance method ' |
| 13695 |
'object\n' |
| 13696 |
'whose "__self__" attribute is "C". When it would yield a\n' |
| 13697 |
'"staticmethod" object, it is transformed into the object wrapped ' |
| 13698 |
'by\n' |
| 13699 |
'the static method object. See section Implementing Descriptors for\n' |
| 13700 |
'another way in which attributes retrieved from a class may differ ' |
| 13701 |
'from\n' |
| 13702 |
'those actually contained in its "__dict__".\n' |
| 13703 |
'\n' |
| 13704 |
'Class attribute assignments update the class’s dictionary, never ' |
| 13705 |
'the\n' |
| 13706 |
'dictionary of a base class.\n' |
| 13707 |
'\n' |
| 13708 |
'A class object can be called (see above) to yield a class instance\n' |
| 13709 |
'(see below).\n' |
| 13710 |
'\n' |
| 13711 |
'Special attributes:\n' |
| 13712 |
'\n' |
| 13713 |
' "__name__"\n' |
| 13714 |
' The class name.\n' |
| 13715 |
'\n' |
| 13716 |
' "__module__"\n' |
| 13717 |
' The name of the module in which the class was defined.\n' |
| 13718 |
'\n' |
| 13719 |
' "__dict__"\n' |
| 13720 |
' The dictionary containing the class’s namespace.\n' |
| 13721 |
'\n' |
| 13722 |
' "__bases__"\n' |
| 13723 |
' A tuple containing the base classes, in the order of their\n' |
| 13724 |
' occurrence in the base class list.\n' |
| 13725 |
'\n' |
| 13726 |
' "__doc__"\n' |
| 13727 |
' The class’s documentation string, or "None" if undefined.\n' |
| 13728 |
'\n' |
| 13729 |
' "__annotations__"\n' |
| 13730 |
' A dictionary containing *variable annotations* collected ' |
| 13731 |
'during\n' |
| 13732 |
' class body execution. For best practices on working with\n' |
| 13733 |
' "__annotations__", please see Annotations Best Practices.\n' |
| 13734 |
'\n' |
| 13735 |
'\n' |
| 13736 |
'Class instances\n' |
| 13737 |
'===============\n' |
| 13738 |
'\n' |
| 13739 |
'A class instance is created by calling a class object (see above). ' |
| 13740 |
'A\n' |
| 13741 |
'class instance has a namespace implemented as a dictionary which ' |
| 13742 |
'is\n' |
| 13743 |
'the first place in which attribute references are searched. When ' |
| 13744 |
'an\n' |
| 13745 |
'attribute is not found there, and the instance’s class has an\n' |
| 13746 |
'attribute by that name, the search continues with the class\n' |
| 13747 |
'attributes. If a class attribute is found that is a user-defined\n' |
| 13748 |
'function object, it is transformed into an instance method object\n' |
| 13749 |
'whose "__self__" attribute is the instance. Static method and ' |
| 13750 |
'class\n' |
| 13751 |
'method objects are also transformed; see above under “Classes”. ' |
| 13752 |
'See\n' |
| 13753 |
'section Implementing Descriptors for another way in which ' |
| 13754 |
'attributes\n' |
| 13755 |
'of a class retrieved via its instances may differ from the objects\n' |
| 13756 |
'actually stored in the class’s "__dict__". If no class attribute ' |
| 13757 |
'is\n' |
| 13758 |
'found, and the object’s class has a "__getattr__()" method, that ' |
| 13759 |
'is\n' |
| 13760 |
'called to satisfy the lookup.\n' |
| 13761 |
'\n' |
| 13762 |
'Attribute assignments and deletions update the instance’s ' |
| 13763 |
'dictionary,\n' |
| 13764 |
'never a class’s dictionary. If the class has a "__setattr__()" or\n' |
| 13765 |
'"__delattr__()" method, this is called instead of updating the\n' |
| 13766 |
'instance dictionary directly.\n' |
| 13767 |
'\n' |
| 13768 |
'Class instances can pretend to be numbers, sequences, or mappings ' |
| 13769 |
'if\n' |
| 13770 |
'they have methods with certain special names. See section Special\n' |
| 13771 |
'method names.\n' |
| 13772 |
'\n' |
| 13773 |
'Special attributes: "__dict__" is the attribute dictionary;\n' |
| 13774 |
'"__class__" is the instance’s class.\n' |
| 13775 |
'\n' |
| 13776 |
'\n' |
| 13777 |
'I/O objects (also known as file objects)\n' |
| 13778 |
'========================================\n' |
| 13779 |
'\n' |
| 13780 |
'A *file object* represents an open file. Various shortcuts are\n' |
| 13781 |
'available to create file objects: the "open()" built-in function, ' |
| 13782 |
'and\n' |
| 13783 |
'also "os.popen()", "os.fdopen()", and the "makefile()" method of\n' |
| 13784 |
'socket objects (and perhaps by other functions or methods provided ' |
| 13785 |
'by\n' |
| 13786 |
'extension modules).\n' |
| 13787 |
'\n' |
| 13788 |
'The objects "sys.stdin", "sys.stdout" and "sys.stderr" are ' |
| 13789 |
'initialized\n' |
| 13790 |
'to file objects corresponding to the interpreter’s standard input,\n' |
| 13791 |
'output and error streams; they are all open in text mode and ' |
| 13792 |
'therefore\n' |
| 13793 |
'follow the interface defined by the "io.TextIOBase" abstract ' |
| 13794 |
'class.\n' |
| 13795 |
'\n' |
| 13796 |
'\n' |
| 13797 |
'Internal types\n' |
| 13798 |
'==============\n' |
| 13799 |
'\n' |
| 13800 |
'A few types used internally by the interpreter are exposed to the\n' |
| 13801 |
'user. Their definitions may change with future versions of the\n' |
| 13802 |
'interpreter, but they are mentioned here for completeness.\n' |
| 13803 |
'\n' |
| 13804 |
'\n' |
| 13805 |
'Code objects\n' |
| 13806 |
'------------\n' |
| 13807 |
'\n' |
| 13808 |
'Code objects represent *byte-compiled* executable Python code, or\n' |
| 13809 |
'*bytecode*. The difference between a code object and a function ' |
| 13810 |
'object\n' |
| 13811 |
'is that the function object contains an explicit reference to the\n' |
| 13812 |
'function’s globals (the module in which it was defined), while a ' |
| 13813 |
'code\n' |
| 13814 |
'object contains no context; also the default argument values are\n' |
| 13815 |
'stored in the function object, not in the code object (because ' |
| 13816 |
'they\n' |
| 13817 |
'represent values calculated at run-time). Unlike function ' |
| 13818 |
'objects,\n' |
| 13819 |
'code objects are immutable and contain no references (directly or\n' |
| 13820 |
'indirectly) to mutable objects.\n' |
| 13821 |
'\n' |
| 13822 |
'\n' |
| 13823 |
'Special read-only attributes\n' |
| 13824 |
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' |
| 13825 |
'\n' |
| 13826 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13827 |
'| codeobject.co_name | The function ' |
| 13828 |
'name |\n' |
| 13829 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13830 |
'| codeobject.co_qualname | The fully ' |
| 13831 |
'qualified function name New in version |\n' |
| 13832 |
'| | ' |
| 13833 |
'3.11. |\n' |
| 13834 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13835 |
'| codeobject.co_argcount | The total ' |
| 13836 |
'number of positional *parameters* |\n' |
| 13837 |
'| | (including ' |
| 13838 |
'positional-only parameters and |\n' |
| 13839 |
'| | parameters ' |
| 13840 |
'with default values) that the function |\n' |
| 13841 |
'| | ' |
| 13842 |
'has |\n' |
| 13843 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13844 |
'| codeobject.co_posonlyargcount | The number ' |
| 13845 |
'of positional-only *parameters* |\n' |
| 13846 |
'| | (including ' |
| 13847 |
'arguments with default values) that the |\n' |
| 13848 |
'| | function ' |
| 13849 |
'has |\n' |
| 13850 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13851 |
'| codeobject.co_kwonlyargcount | The number ' |
| 13852 |
'of keyword-only *parameters* (including |\n' |
| 13853 |
'| | arguments ' |
| 13854 |
'with default values) that the function |\n' |
| 13855 |
'| | ' |
| 13856 |
'has |\n' |
| 13857 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13858 |
'| codeobject.co_nlocals | The number ' |
| 13859 |
'of local variables used by the function |\n' |
| 13860 |
'| | (including ' |
| 13861 |
'parameters) |\n' |
| 13862 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13863 |
'| codeobject.co_varnames | A "tuple" ' |
| 13864 |
'containing the names of the local |\n' |
| 13865 |
'| | variables in ' |
| 13866 |
'the function (starting with the |\n' |
| 13867 |
'| | parameter ' |
| 13868 |
'names) |\n' |
| 13869 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13870 |
'| codeobject.co_cellvars | A "tuple" ' |
| 13871 |
'containing the names of local variables |\n' |
| 13872 |
'| | that are ' |
| 13873 |
'referenced by nested functions inside the |\n' |
| 13874 |
'| | ' |
| 13875 |
'function |\n' |
| 13876 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13877 |
'| codeobject.co_freevars | A "tuple" ' |
| 13878 |
'containing the names of free variables |\n' |
| 13879 |
'| | in the ' |
| 13880 |
'function |\n' |
| 13881 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13882 |
'| codeobject.co_code | A string ' |
| 13883 |
'representing the sequence of *bytecode* |\n' |
| 13884 |
'| | instructions ' |
| 13885 |
'in the function |\n' |
| 13886 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13887 |
'| codeobject.co_consts | A "tuple" ' |
| 13888 |
'containing the literals used by the |\n' |
| 13889 |
'| | *bytecode* ' |
| 13890 |
'in the function |\n' |
| 13891 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13892 |
'| codeobject.co_names | A "tuple" ' |
| 13893 |
'containing the names used by the |\n' |
| 13894 |
'| | *bytecode* ' |
| 13895 |
'in the function |\n' |
| 13896 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13897 |
'| codeobject.co_filename | The name of ' |
| 13898 |
'the file from which the code was |\n' |
| 13899 |
'| | ' |
| 13900 |
'compiled |\n' |
| 13901 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13902 |
'| codeobject.co_firstlineno | The line ' |
| 13903 |
'number of the first line of the function |\n' |
| 13904 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13905 |
'| codeobject.co_lnotab | A string ' |
| 13906 |
'encoding the mapping from *bytecode* |\n' |
| 13907 |
'| | offsets to ' |
| 13908 |
'line numbers. For details, see the |\n' |
| 13909 |
'| | source code ' |
| 13910 |
'of the interpreter. |\n' |
| 13911 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13912 |
'| codeobject.co_stacksize | The required ' |
| 13913 |
'stack size of the code object |\n' |
| 13914 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13915 |
'| codeobject.co_flags | An "integer" ' |
| 13916 |
'encoding a number of flags for the |\n' |
| 13917 |
'| | ' |
| 13918 |
'interpreter. |\n' |
| 13919 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 13920 |
'\n' |
| 13921 |
'The following flag bits are defined for "co_flags": bit "0x04" is ' |
| 13922 |
'set\n' |
| 13923 |
'if the function uses the "*arguments" syntax to accept an ' |
| 13924 |
'arbitrary\n' |
| 13925 |
'number of positional arguments; bit "0x08" is set if the function ' |
| 13926 |
'uses\n' |
| 13927 |
'the "**keywords" syntax to accept arbitrary keyword arguments; bit\n' |
| 13928 |
'"0x20" is set if the function is a generator. See Code Objects Bit\n' |
| 13929 |
'Flags for details on the semantics of each flags that might be\n' |
| 13930 |
'present.\n' |
| 13931 |
'\n' |
| 13932 |
'Future feature declarations ("from __future__ import division") ' |
| 13933 |
'also\n' |
| 13934 |
'use bits in "co_flags" to indicate whether a code object was ' |
| 13935 |
'compiled\n' |
| 13936 |
'with a particular feature enabled: bit "0x2000" is set if the ' |
| 13937 |
'function\n' |
| 13938 |
'was compiled with future division enabled; bits "0x10" and ' |
| 13939 |
'"0x1000"\n' |
| 13940 |
'were used in earlier versions of Python.\n' |
| 13941 |
'\n' |
| 13942 |
'Other bits in "co_flags" are reserved for internal use.\n' |
| 13943 |
'\n' |
| 13944 |
'If a code object represents a function, the first item in ' |
| 13945 |
'"co_consts"\n' |
| 13946 |
'is the documentation string of the function, or "None" if ' |
| 13947 |
'undefined.\n' |
| 13948 |
'\n' |
| 13949 |
'\n' |
| 13950 |
'Methods on code objects\n' |
| 13951 |
'~~~~~~~~~~~~~~~~~~~~~~~\n' |
| 13952 |
'\n' |
| 13953 |
'codeobject.co_positions()\n' |
| 13954 |
'\n' |
| 13955 |
' Returns an iterable over the source code positions of each\n' |
| 13956 |
' *bytecode* instruction in the code object.\n' |
| 13957 |
'\n' |
| 13958 |
' The iterator returns "tuple"s containing the "(start_line,\n' |
| 13959 |
' end_line, start_column, end_column)". The *i-th* tuple ' |
| 13960 |
'corresponds\n' |
| 13961 |
' to the position of the source code that compiled to the *i-th* ' |
| 13962 |
'code\n' |
| 13963 |
' unit. Column information is 0-indexed utf-8 byte offsets on the\n' |
| 13964 |
' given source line.\n' |
| 13965 |
'\n' |
| 13966 |
' This positional information can be missing. A non-exhaustive ' |
| 13967 |
'lists\n' |
| 13968 |
' of cases where this may happen:\n' |
| 13969 |
'\n' |
| 13970 |
' * Running the interpreter with "-X" "no_debug_ranges".\n' |
| 13971 |
'\n' |
| 13972 |
' * Loading a pyc file compiled while using "-X" ' |
| 13973 |
'"no_debug_ranges".\n' |
| 13974 |
'\n' |
| 13975 |
' * Position tuples corresponding to artificial instructions.\n' |
| 13976 |
'\n' |
| 13977 |
' * Line and column numbers that can’t be represented due to\n' |
| 13978 |
' implementation specific limitations.\n' |
| 13979 |
'\n' |
| 13980 |
' When this occurs, some or all of the tuple elements can be ' |
| 13981 |
'"None".\n' |
| 13982 |
'\n' |
| 13983 |
' New in version 3.11.\n' |
| 13984 |
'\n' |
| 13985 |
' Note:\n' |
| 13986 |
'\n' |
| 13987 |
' This feature requires storing column positions in code ' |
| 13988 |
'objects\n' |
| 13989 |
' which may result in a small increase of disk usage of ' |
| 13990 |
'compiled\n' |
| 13991 |
' Python files or interpreter memory usage. To avoid storing ' |
| 13992 |
'the\n' |
| 13993 |
' extra information and/or deactivate printing the extra ' |
| 13994 |
'traceback\n' |
| 13995 |
' information, the "-X" "no_debug_ranges" command line flag or ' |
| 13996 |
'the\n' |
| 13997 |
' "PYTHONNODEBUGRANGES" environment variable can be used.\n' |
| 13998 |
'\n' |
| 13999 |
'codeobject.co_lines()\n' |
| 14000 |
'\n' |
| 14001 |
' Returns an iterator that yields information about successive ' |
| 14002 |
'ranges\n' |
| 14003 |
' of *bytecode*s. Each item yielded is a "(start, end, lineno)"\n' |
| 14004 |
' "tuple":\n' |
| 14005 |
'\n' |
| 14006 |
' * "start" (an "int") represents the offset (inclusive) of the ' |
| 14007 |
'start\n' |
| 14008 |
' of the *bytecode* range\n' |
| 14009 |
'\n' |
| 14010 |
' * "end" (an "int") represents the offset (exclusive) of the end ' |
| 14011 |
'of\n' |
| 14012 |
' the *bytecode* range\n' |
| 14013 |
'\n' |
| 14014 |
' * "lineno" is an "int" representing the line number of the\n' |
| 14015 |
' *bytecode* range, or "None" if the bytecodes in the given ' |
| 14016 |
'range\n' |
| 14017 |
' have no line number\n' |
| 14018 |
'\n' |
| 14019 |
' The items yielded will have the following properties:\n' |
| 14020 |
'\n' |
| 14021 |
' * The first range yielded will have a "start" of 0.\n' |
| 14022 |
'\n' |
| 14023 |
' * The "(start, end)" ranges will be non-decreasing and ' |
| 14024 |
'consecutive.\n' |
| 14025 |
' That is, for any pair of "tuple"s, the "start" of the second ' |
| 14026 |
'will\n' |
| 14027 |
' be equal to the "end" of the first.\n' |
| 14028 |
'\n' |
| 14029 |
' * No range will be backwards: "end >= start" for all triples.\n' |
| 14030 |
'\n' |
| 14031 |
' * The last "tuple" yielded will have "end" equal to the size of ' |
| 14032 |
'the\n' |
| 14033 |
' *bytecode*.\n' |
| 14034 |
'\n' |
| 14035 |
' Zero-width ranges, where "start == end", are allowed. ' |
| 14036 |
'Zero-width\n' |
| 14037 |
' ranges are used for lines that are present in the source code, ' |
| 14038 |
'but\n' |
| 14039 |
' have been eliminated by the *bytecode* compiler.\n' |
| 14040 |
'\n' |
| 14041 |
' New in version 3.10.\n' |
| 14042 |
'\n' |
| 14043 |
' See also:\n' |
| 14044 |
'\n' |
| 14045 |
' **PEP 626** - Precise line numbers for debugging and other ' |
| 14046 |
'tools.\n' |
| 14047 |
' The PEP that introduced the "co_lines()" method.\n' |
| 14048 |
'\n' |
| 14049 |
'codeobject.replace(**kwargs)\n' |
| 14050 |
'\n' |
| 14051 |
' Return a copy of the code object with new values for the ' |
| 14052 |
'specified\n' |
| 14053 |
' fields.\n' |
| 14054 |
'\n' |
| 14055 |
' New in version 3.8.\n' |
| 14056 |
'\n' |
| 14057 |
'\n' |
| 14058 |
'Frame objects\n' |
| 14059 |
'-------------\n' |
| 14060 |
'\n' |
| 14061 |
'Frame objects represent execution frames. They may occur in ' |
| 14062 |
'traceback\n' |
| 14063 |
'objects, and are also passed to registered trace functions.\n' |
| 14064 |
'\n' |
| 14065 |
'\n' |
| 14066 |
'Special read-only attributes\n' |
| 14067 |
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' |
| 14068 |
'\n' |
| 14069 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14070 |
'| frame.f_back | Points to ' |
| 14071 |
'the previous stack frame (towards the |\n' |
| 14072 |
'| | caller), or ' |
| 14073 |
'"None" if this is the bottom stack |\n' |
| 14074 |
'| | ' |
| 14075 |
'frame |\n' |
| 14076 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14077 |
'| frame.f_code | The code ' |
| 14078 |
'object being executed in this frame. |\n' |
| 14079 |
'| | Accessing ' |
| 14080 |
'this attribute raises an auditing event |\n' |
| 14081 |
'| | ' |
| 14082 |
'"object.__getattr__" with arguments "obj" and |\n' |
| 14083 |
'| | ' |
| 14084 |
'""f_code"". |\n' |
| 14085 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14086 |
'| frame.f_locals | The ' |
| 14087 |
'dictionary used by the frame to look up local |\n' |
| 14088 |
'| | ' |
| 14089 |
'variables |\n' |
| 14090 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14091 |
'| frame.f_globals | The ' |
| 14092 |
'dictionary used by the frame to look up global |\n' |
| 14093 |
'| | ' |
| 14094 |
'variables |\n' |
| 14095 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14096 |
'| frame.f_builtins | The ' |
| 14097 |
'dictionary used by the frame to look up built- |\n' |
| 14098 |
'| | in ' |
| 14099 |
'(intrinsic) names |\n' |
| 14100 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14101 |
'| frame.f_lasti | The “precise ' |
| 14102 |
'instruction” of the frame object |\n' |
| 14103 |
'| | (this is an ' |
| 14104 |
'index into the *bytecode* string of |\n' |
| 14105 |
'| | the code ' |
| 14106 |
'object) |\n' |
| 14107 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14108 |
'\n' |
| 14109 |
'\n' |
| 14110 |
'Special writable attributes\n' |
| 14111 |
'~~~~~~~~~~~~~~~~~~~~~~~~~~~\n' |
| 14112 |
'\n' |
| 14113 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14114 |
'| frame.f_trace | If not ' |
| 14115 |
'"None", this is a function called for |\n' |
| 14116 |
'| | various ' |
| 14117 |
'events during code execution (this is used |\n' |
| 14118 |
'| | by ' |
| 14119 |
'debuggers). Normally an event is triggered for |\n' |
| 14120 |
'| | each new ' |
| 14121 |
'source line (see "f_trace_lines"). |\n' |
| 14122 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14123 |
'| frame.f_trace_lines | Set this ' |
| 14124 |
'attribute to "False" to disable |\n' |
| 14125 |
'| | triggering a ' |
| 14126 |
'tracing event for each source line. |\n' |
| 14127 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14128 |
'| frame.f_trace_opcodes | Set this ' |
| 14129 |
'attribute to "True" to allow per-opcode |\n' |
| 14130 |
'| | events to be ' |
| 14131 |
'requested. Note that this may lead to |\n' |
| 14132 |
'| | undefined ' |
| 14133 |
'interpreter behaviour if exceptions |\n' |
| 14134 |
'| | raised by ' |
| 14135 |
'the trace function escape to the |\n' |
| 14136 |
'| | function ' |
| 14137 |
'being traced. |\n' |
| 14138 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14139 |
'| frame.f_lineno | The current ' |
| 14140 |
'line number of the frame – writing to |\n' |
| 14141 |
'| | this from ' |
| 14142 |
'within a trace function jumps to the |\n' |
| 14143 |
'| | given line ' |
| 14144 |
'(only for the bottom-most frame). A |\n' |
| 14145 |
'| | debugger can ' |
| 14146 |
'implement a Jump command (aka Set |\n' |
| 14147 |
'| | Next ' |
| 14148 |
'Statement) by writing to this attribute. |\n' |
| 14149 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14150 |
'\n' |
| 14151 |
'\n' |
| 14152 |
'Frame object methods\n' |
| 14153 |
'~~~~~~~~~~~~~~~~~~~~\n' |
| 14154 |
'\n' |
| 14155 |
'Frame objects support one method:\n' |
| 14156 |
'\n' |
| 14157 |
'frame.clear()\n' |
| 14158 |
'\n' |
| 14159 |
' This method clears all references to local variables held by ' |
| 14160 |
'the\n' |
| 14161 |
' frame. Also, if the frame belonged to a *generator*, the ' |
| 14162 |
'generator\n' |
| 14163 |
' is finalized. This helps break reference cycles involving ' |
| 14164 |
'frame\n' |
| 14165 |
' objects (for example when catching an exception and storing its\n' |
| 14166 |
' traceback for later use).\n' |
| 14167 |
'\n' |
| 14168 |
' "RuntimeError" is raised if the frame is currently executing.\n' |
| 14169 |
'\n' |
| 14170 |
' New in version 3.4.\n' |
| 14171 |
'\n' |
| 14172 |
'\n' |
| 14173 |
'Traceback objects\n' |
| 14174 |
'-----------------\n' |
| 14175 |
'\n' |
| 14176 |
'Traceback objects represent the stack trace of an exception. A\n' |
| 14177 |
'traceback object is implicitly created when an exception occurs, ' |
| 14178 |
'and\n' |
| 14179 |
'may also be explicitly created by calling "types.TracebackType".\n' |
| 14180 |
'\n' |
| 14181 |
'Changed in version 3.7: Traceback objects can now be explicitly\n' |
| 14182 |
'instantiated from Python code.\n' |
| 14183 |
'\n' |
| 14184 |
'For implicitly created tracebacks, when the search for an ' |
| 14185 |
'exception\n' |
| 14186 |
'handler unwinds the execution stack, at each unwound level a ' |
| 14187 |
'traceback\n' |
| 14188 |
'object is inserted in front of the current traceback. When an\n' |
| 14189 |
'exception handler is entered, the stack trace is made available to ' |
| 14190 |
'the\n' |
| 14191 |
'program. (See section The try statement.) It is accessible as the\n' |
| 14192 |
'third item of the tuple returned by "sys.exc_info()", and as the\n' |
| 14193 |
'"__traceback__" attribute of the caught exception.\n' |
| 14194 |
'\n' |
| 14195 |
'When the program contains no suitable handler, the stack trace is\n' |
| 14196 |
'written (nicely formatted) to the standard error stream; if the\n' |
| 14197 |
'interpreter is interactive, it is also made available to the user ' |
| 14198 |
'as\n' |
| 14199 |
'"sys.last_traceback".\n' |
| 14200 |
'\n' |
| 14201 |
'For explicitly created tracebacks, it is up to the creator of the\n' |
| 14202 |
'traceback to determine how the "tb_next" attributes should be ' |
| 14203 |
'linked\n' |
| 14204 |
'to form a full stack trace.\n' |
| 14205 |
'\n' |
| 14206 |
'Special read-only attributes:\n' |
| 14207 |
'\n' |
| 14208 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14209 |
'| traceback.tb_frame | Points to ' |
| 14210 |
'the execution frame of the current |\n' |
| 14211 |
'| | level. ' |
| 14212 |
'Accessing this attribute raises an |\n' |
| 14213 |
'| | auditing ' |
| 14214 |
'event "object.__getattr__" with arguments |\n' |
| 14215 |
'| | "obj" and ' |
| 14216 |
'""tb_frame"". |\n' |
| 14217 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14218 |
'| traceback.tb_lineno | Gives the ' |
| 14219 |
'line number where the exception occurred |\n' |
| 14220 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14221 |
'| traceback.tb_lasti | Indicates ' |
| 14222 |
'the “precise instruction”. |\n' |
| 14223 |
'+----------------------------------------------------+----------------------------------------------------+\n' |
| 14224 |
'\n' |
| 14225 |
'The line number and last instruction in the traceback may differ ' |
| 14226 |
'from\n' |
| 14227 |
'the line number of its frame object if the exception occurred in a\n' |
| 14228 |
'"try" statement with no matching except clause or with a "finally"\n' |
| 14229 |
'clause.\n' |
| 14230 |
'\n' |
| 14231 |
'traceback.tb_next\n' |
| 14232 |
'\n' |
| 14233 |
' The special writable attribute "tb_next" is the next level in ' |
| 14234 |
'the\n' |
| 14235 |
' stack trace (towards the frame where the exception occurred), ' |
| 14236 |
'or\n' |
| 14237 |
' "None" if there is no next level.\n' |
| 14238 |
'\n' |
| 14239 |
' Changed in version 3.7: This attribute is now writable\n' |
| 14240 |
'\n' |
| 14241 |
'\n' |
| 14242 |
'Slice objects\n' |
| 14243 |
'-------------\n' |
| 14244 |
'\n' |
| 14245 |
'Slice objects are used to represent slices for "__getitem__()"\n' |
| 14246 |
'methods. They are also created by the built-in "slice()" ' |
| 14247 |
'function.\n' |
| 14248 |
'\n' |
| 14249 |
'Special read-only attributes: "start" is the lower bound; "stop" ' |
| 14250 |
'is\n' |
| 14251 |
'the upper bound; "step" is the step value; each is "None" if ' |
| 14252 |
'omitted.\n' |
| 14253 |
'These attributes can have any type.\n' |
| 14254 |
'\n' |
| 14255 |
'Slice objects support one method:\n' |
| 14256 |
'\n' |
| 14257 |
'slice.indices(self, length)\n' |
| 14258 |
'\n' |
| 14259 |
' This method takes a single integer argument *length* and ' |
| 14260 |
'computes\n' |
| 14261 |
' information about the slice that the slice object would describe ' |
| 14262 |
'if\n' |
| 14263 |
' applied to a sequence of *length* items. It returns a tuple of\n' |
| 14264 |
' three integers; respectively these are the *start* and *stop*\n' |
| 14265 |
' indices and the *step* or stride length of the slice. Missing ' |
| 14266 |
'or\n' |
| 14267 |
' out-of-bounds indices are handled in a manner consistent with\n' |
| 14268 |
' regular slices.\n' |
| 14269 |
'\n' |
| 14270 |
'\n' |
| 14271 |
'Static method objects\n' |
| 14272 |
'---------------------\n' |
| 14273 |
'\n' |
| 14274 |
'Static method objects provide a way of defeating the transformation ' |
| 14275 |
'of\n' |
| 14276 |
'function objects to method objects described above. A static ' |
| 14277 |
'method\n' |
| 14278 |
'object is a wrapper around any other object, usually a ' |
| 14279 |
'user-defined\n' |
| 14280 |
'method object. When a static method object is retrieved from a ' |
| 14281 |
'class\n' |
| 14282 |
'or a class instance, the object actually returned is the wrapped\n' |
| 14283 |
'object, which is not subject to any further transformation. Static\n' |
| 14284 |
'method objects are also callable. Static method objects are created ' |
| 14285 |
'by\n' |
| 14286 |
'the built-in "staticmethod()" constructor.\n' |
| 14287 |
'\n' |
| 14288 |
'\n' |
| 14289 |
'Class method objects\n' |
| 14290 |
'--------------------\n' |
| 14291 |
'\n' |
| 14292 |
'A class method object, like a static method object, is a wrapper\n' |
| 14293 |
'around another object that alters the way in which that object is\n' |
| 14294 |
'retrieved from classes and class instances. The behaviour of class\n' |
| 14295 |
'method objects upon such retrieval is described above, under ' |
| 14296 |
'“instance\n' |
| 14297 |
'methods”. Class method objects are created by the built-in\n' |
| 14298 |
'"classmethod()" constructor.\n', |
| 14299 |
'typesfunctions': 'Functions\n' |
| 14300 |
'*********\n' |
| 14301 |
'\n' |
| 14302 |
'Function objects are created by function definitions. The ' |
| 14303 |
'only\n' |
| 14304 |
'operation on a function object is to call it: ' |
| 14305 |
'"func(argument-list)".\n' |
| 14306 |
'\n' |
| 14307 |
'There are really two flavors of function objects: built-in ' |
| 14308 |
'functions\n' |
| 14309 |
'and user-defined functions. Both support the same ' |
| 14310 |
'operation (to call\n' |
| 14311 |
'the function), but the implementation is different, hence ' |
| 14312 |
'the\n' |
| 14313 |
'different object types.\n' |
| 14314 |
'\n' |
| 14315 |
'See Function definitions for more information.\n', |
| 14316 |
'typesmapping': 'Mapping Types — "dict"\n' |
| 14317 |
'**********************\n' |
| 14318 |
'\n' |
| 14319 |
'A *mapping* object maps *hashable* values to arbitrary ' |
| 14320 |
'objects.\n' |
| 14321 |
'Mappings are mutable objects. There is currently only one ' |
| 14322 |
'standard\n' |
| 14323 |
'mapping type, the *dictionary*. (For other containers see ' |
| 14324 |
'the built-\n' |
| 14325 |
'in "list", "set", and "tuple" classes, and the "collections" ' |
| 14326 |
'module.)\n' |
| 14327 |
'\n' |
| 14328 |
'A dictionary’s keys are *almost* arbitrary values. Values ' |
| 14329 |
'that are\n' |
| 14330 |
'not *hashable*, that is, values containing lists, ' |
| 14331 |
'dictionaries or\n' |
| 14332 |
'other mutable types (that are compared by value rather than ' |
| 14333 |
'by object\n' |
| 14334 |
'identity) may not be used as keys. Values that compare equal ' |
| 14335 |
'(such as\n' |
| 14336 |
'"1", "1.0", and "True") can be used interchangeably to index ' |
| 14337 |
'the same\n' |
| 14338 |
'dictionary entry.\n' |
| 14339 |
'\n' |
| 14340 |
'class dict(**kwargs)\n' |
| 14341 |
'class dict(mapping, **kwargs)\n' |
| 14342 |
'class dict(iterable, **kwargs)\n' |
| 14343 |
'\n' |
| 14344 |
' Return a new dictionary initialized from an optional ' |
| 14345 |
'positional\n' |
| 14346 |
' argument and a possibly empty set of keyword arguments.\n' |
| 14347 |
'\n' |
| 14348 |
' Dictionaries can be created by several means:\n' |
| 14349 |
'\n' |
| 14350 |
' * Use a comma-separated list of "key: value" pairs within ' |
| 14351 |
'braces:\n' |
| 14352 |
' "{\'jack\': 4098, \'sjoerd\': 4127}" or "{4098: ' |
| 14353 |
"'jack', 4127:\n" |
| 14354 |
' \'sjoerd\'}"\n' |
| 14355 |
'\n' |
| 14356 |
' * Use a dict comprehension: "{}", "{x: x ** 2 for x in ' |
| 14357 |
'range(10)}"\n' |
| 14358 |
'\n' |
| 14359 |
' * Use the type constructor: "dict()", "dict([(\'foo\', ' |
| 14360 |
"100), ('bar',\n" |
| 14361 |
' 200)])", "dict(foo=100, bar=200)"\n' |
| 14362 |
'\n' |
| 14363 |
' If no positional argument is given, an empty dictionary ' |
| 14364 |
'is created.\n' |
| 14365 |
' If a positional argument is given and it is a mapping ' |
| 14366 |
'object, a\n' |
| 14367 |
' dictionary is created with the same key-value pairs as ' |
| 14368 |
'the mapping\n' |
| 14369 |
' object. Otherwise, the positional argument must be an ' |
| 14370 |
'*iterable*\n' |
| 14371 |
' object. Each item in the iterable must itself be an ' |
| 14372 |
'iterable with\n' |
| 14373 |
' exactly two objects. The first object of each item ' |
| 14374 |
'becomes a key\n' |
| 14375 |
' in the new dictionary, and the second object the ' |
| 14376 |
'corresponding\n' |
| 14377 |
' value. If a key occurs more than once, the last value ' |
| 14378 |
'for that key\n' |
| 14379 |
' becomes the corresponding value in the new dictionary.\n' |
| 14380 |
'\n' |
| 14381 |
' If keyword arguments are given, the keyword arguments and ' |
| 14382 |
'their\n' |
| 14383 |
' values are added to the dictionary created from the ' |
| 14384 |
'positional\n' |
| 14385 |
' argument. If a key being added is already present, the ' |
| 14386 |
'value from\n' |
| 14387 |
' the keyword argument replaces the value from the ' |
| 14388 |
'positional\n' |
| 14389 |
' argument.\n' |
| 14390 |
'\n' |
| 14391 |
' To illustrate, the following examples all return a ' |
| 14392 |
'dictionary equal\n' |
| 14393 |
' to "{"one": 1, "two": 2, "three": 3}":\n' |
| 14394 |
'\n' |
| 14395 |
' >>> a = dict(one=1, two=2, three=3)\n' |
| 14396 |
" >>> b = {'one': 1, 'two': 2, 'three': 3}\n" |
| 14397 |
" >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))\n" |
| 14398 |
" >>> d = dict([('two', 2), ('one', 1), ('three', 3)])\n" |
| 14399 |
" >>> e = dict({'three': 3, 'one': 1, 'two': 2})\n" |
| 14400 |
" >>> f = dict({'one': 1, 'three': 3}, two=2)\n" |
| 14401 |
' >>> a == b == c == d == e == f\n' |
| 14402 |
' True\n' |
| 14403 |
'\n' |
| 14404 |
' Providing keyword arguments as in the first example only ' |
| 14405 |
'works for\n' |
| 14406 |
' keys that are valid Python identifiers. Otherwise, any ' |
| 14407 |
'valid keys\n' |
| 14408 |
' can be used.\n' |
| 14409 |
'\n' |
| 14410 |
' These are the operations that dictionaries support (and ' |
| 14411 |
'therefore,\n' |
| 14412 |
' custom mapping types should support too):\n' |
| 14413 |
'\n' |
| 14414 |
' list(d)\n' |
| 14415 |
'\n' |
| 14416 |
' Return a list of all the keys used in the dictionary ' |
| 14417 |
'*d*.\n' |
| 14418 |
'\n' |
| 14419 |
' len(d)\n' |
| 14420 |
'\n' |
| 14421 |
' Return the number of items in the dictionary *d*.\n' |
| 14422 |
'\n' |
| 14423 |
' d[key]\n' |
| 14424 |
'\n' |
| 14425 |
' Return the item of *d* with key *key*. Raises a ' |
| 14426 |
'"KeyError" if\n' |
| 14427 |
' *key* is not in the map.\n' |
| 14428 |
'\n' |
| 14429 |
' If a subclass of dict defines a method "__missing__()" ' |
| 14430 |
'and *key*\n' |
| 14431 |
' is not present, the "d[key]" operation calls that ' |
| 14432 |
'method with\n' |
| 14433 |
' the key *key* as argument. The "d[key]" operation ' |
| 14434 |
'then returns\n' |
| 14435 |
' or raises whatever is returned or raised by the\n' |
| 14436 |
' "__missing__(key)" call. No other operations or ' |
| 14437 |
'methods invoke\n' |
| 14438 |
' "__missing__()". If "__missing__()" is not defined, ' |
| 14439 |
'"KeyError"\n' |
| 14440 |
' is raised. "__missing__()" must be a method; it cannot ' |
| 14441 |
'be an\n' |
| 14442 |
' instance variable:\n' |
| 14443 |
'\n' |
| 14444 |
' >>> class Counter(dict):\n' |
| 14445 |
' ... def __missing__(self, key):\n' |
| 14446 |
' ... return 0\n' |
| 14447 |
' >>> c = Counter()\n' |
| 14448 |
" >>> c['red']\n" |
| 14449 |
' 0\n' |
| 14450 |
" >>> c['red'] += 1\n" |
| 14451 |
" >>> c['red']\n" |
| 14452 |
' 1\n' |
| 14453 |
'\n' |
| 14454 |
' The example above shows part of the implementation of\n' |
| 14455 |
' "collections.Counter". A different "__missing__" ' |
| 14456 |
'method is used\n' |
| 14457 |
' by "collections.defaultdict".\n' |
| 14458 |
'\n' |
| 14459 |
' d[key] = value\n' |
| 14460 |
'\n' |
| 14461 |
' Set "d[key]" to *value*.\n' |
| 14462 |
'\n' |
| 14463 |
' del d[key]\n' |
| 14464 |
'\n' |
| 14465 |
' Remove "d[key]" from *d*. Raises a "KeyError" if ' |
| 14466 |
'*key* is not\n' |
| 14467 |
' in the map.\n' |
| 14468 |
'\n' |
| 14469 |
' key in d\n' |
| 14470 |
'\n' |
| 14471 |
' Return "True" if *d* has a key *key*, else "False".\n' |
| 14472 |
'\n' |
| 14473 |
' key not in d\n' |
| 14474 |
'\n' |
| 14475 |
' Equivalent to "not key in d".\n' |
| 14476 |
'\n' |
| 14477 |
' iter(d)\n' |
| 14478 |
'\n' |
| 14479 |
' Return an iterator over the keys of the dictionary. ' |
| 14480 |
'This is a\n' |
| 14481 |
' shortcut for "iter(d.keys())".\n' |
| 14482 |
'\n' |
| 14483 |
' clear()\n' |
| 14484 |
'\n' |
| 14485 |
' Remove all items from the dictionary.\n' |
| 14486 |
'\n' |
| 14487 |
' copy()\n' |
| 14488 |
'\n' |
| 14489 |
' Return a shallow copy of the dictionary.\n' |
| 14490 |
'\n' |
| 14491 |
' classmethod fromkeys(iterable[, value])\n' |
| 14492 |
'\n' |
| 14493 |
' Create a new dictionary with keys from *iterable* and ' |
| 14494 |
'values set\n' |
| 14495 |
' to *value*.\n' |
| 14496 |
'\n' |
| 14497 |
' "fromkeys()" is a class method that returns a new ' |
| 14498 |
'dictionary.\n' |
| 14499 |
' *value* defaults to "None". All of the values refer ' |
| 14500 |
'to just a\n' |
| 14501 |
' single instance, so it generally doesn’t make sense ' |
| 14502 |
'for *value*\n' |
| 14503 |
' to be a mutable object such as an empty list. To get ' |
| 14504 |
'distinct\n' |
| 14505 |
' values, use a dict comprehension instead.\n' |
| 14506 |
'\n' |
| 14507 |
' get(key[, default])\n' |
| 14508 |
'\n' |
| 14509 |
' Return the value for *key* if *key* is in the ' |
| 14510 |
'dictionary, else\n' |
| 14511 |
' *default*. If *default* is not given, it defaults to ' |
| 14512 |
'"None", so\n' |
| 14513 |
' that this method never raises a "KeyError".\n' |
| 14514 |
'\n' |
| 14515 |
' items()\n' |
| 14516 |
'\n' |
| 14517 |
' Return a new view of the dictionary’s items ("(key, ' |
| 14518 |
'value)"\n' |
| 14519 |
' pairs). See the documentation of view objects.\n' |
| 14520 |
'\n' |
| 14521 |
' keys()\n' |
| 14522 |
'\n' |
| 14523 |
' Return a new view of the dictionary’s keys. See the\n' |
| 14524 |
' documentation of view objects.\n' |
| 14525 |
'\n' |
| 14526 |
' pop(key[, default])\n' |
| 14527 |
'\n' |
| 14528 |
' If *key* is in the dictionary, remove it and return ' |
| 14529 |
'its value,\n' |
| 14530 |
' else return *default*. If *default* is not given and ' |
| 14531 |
'*key* is\n' |
| 14532 |
' not in the dictionary, a "KeyError" is raised.\n' |
| 14533 |
'\n' |
| 14534 |
' popitem()\n' |
| 14535 |
'\n' |
| 14536 |
' Remove and return a "(key, value)" pair from the ' |
| 14537 |
'dictionary.\n' |
| 14538 |
' Pairs are returned in LIFO (last-in, first-out) ' |
| 14539 |
'order.\n' |
| 14540 |
'\n' |
| 14541 |
' "popitem()" is useful to destructively iterate over a\n' |
| 14542 |
' dictionary, as often used in set algorithms. If the ' |
| 14543 |
'dictionary\n' |
| 14544 |
' is empty, calling "popitem()" raises a "KeyError".\n' |
| 14545 |
'\n' |
| 14546 |
' Changed in version 3.7: LIFO order is now guaranteed. ' |
| 14547 |
'In prior\n' |
| 14548 |
' versions, "popitem()" would return an arbitrary ' |
| 14549 |
'key/value pair.\n' |
| 14550 |
'\n' |
| 14551 |
' reversed(d)\n' |
| 14552 |
'\n' |
| 14553 |
' Return a reverse iterator over the keys of the ' |
| 14554 |
'dictionary. This\n' |
| 14555 |
' is a shortcut for "reversed(d.keys())".\n' |
| 14556 |
'\n' |
| 14557 |
' New in version 3.8.\n' |
| 14558 |
'\n' |
| 14559 |
' setdefault(key[, default])\n' |
| 14560 |
'\n' |
| 14561 |
' If *key* is in the dictionary, return its value. If ' |
| 14562 |
'not, insert\n' |
| 14563 |
' *key* with a value of *default* and return *default*. ' |
| 14564 |
'*default*\n' |
| 14565 |
' defaults to "None".\n' |
| 14566 |
'\n' |
| 14567 |
' update([other])\n' |
| 14568 |
'\n' |
| 14569 |
' Update the dictionary with the key/value pairs from ' |
| 14570 |
'*other*,\n' |
| 14571 |
' overwriting existing keys. Return "None".\n' |
| 14572 |
'\n' |
| 14573 |
' "update()" accepts either another dictionary object or ' |
| 14574 |
'an\n' |
| 14575 |
' iterable of key/value pairs (as tuples or other ' |
| 14576 |
'iterables of\n' |
| 14577 |
' length two). If keyword arguments are specified, the ' |
| 14578 |
'dictionary\n' |
| 14579 |
' is then updated with those key/value pairs: ' |
| 14580 |
'"d.update(red=1,\n' |
| 14581 |
' blue=2)".\n' |
| 14582 |
'\n' |
| 14583 |
' values()\n' |
| 14584 |
'\n' |
| 14585 |
' Return a new view of the dictionary’s values. See ' |
| 14586 |
'the\n' |
| 14587 |
' documentation of view objects.\n' |
| 14588 |
'\n' |
| 14589 |
' An equality comparison between one "dict.values()" ' |
| 14590 |
'view and\n' |
| 14591 |
' another will always return "False". This also applies ' |
| 14592 |
'when\n' |
| 14593 |
' comparing "dict.values()" to itself:\n' |
| 14594 |
'\n' |
| 14595 |
" >>> d = {'a': 1}\n" |
| 14596 |
' >>> d.values() == d.values()\n' |
| 14597 |
' False\n' |
| 14598 |
'\n' |
| 14599 |
' d | other\n' |
| 14600 |
'\n' |
| 14601 |
' Create a new dictionary with the merged keys and ' |
| 14602 |
'values of *d*\n' |
| 14603 |
' and *other*, which must both be dictionaries. The ' |
| 14604 |
'values of\n' |
| 14605 |
' *other* take priority when *d* and *other* share ' |
| 14606 |
'keys.\n' |
| 14607 |
'\n' |
| 14608 |
' New in version 3.9.\n' |
| 14609 |
'\n' |
| 14610 |
' d |= other\n' |
| 14611 |
'\n' |
| 14612 |
' Update the dictionary *d* with keys and values from ' |
| 14613 |
'*other*,\n' |
| 14614 |
' which may be either a *mapping* or an *iterable* of ' |
| 14615 |
'key/value\n' |
| 14616 |
' pairs. The values of *other* take priority when *d* ' |
| 14617 |
'and *other*\n' |
| 14618 |
' share keys.\n' |
| 14619 |
'\n' |
| 14620 |
' New in version 3.9.\n' |
| 14621 |
'\n' |
| 14622 |
' Dictionaries compare equal if and only if they have the ' |
| 14623 |
'same "(key,\n' |
| 14624 |
' value)" pairs (regardless of ordering). Order comparisons ' |
| 14625 |
'(‘<’,\n' |
| 14626 |
' ‘<=’, ‘>=’, ‘>’) raise "TypeError".\n' |
| 14627 |
'\n' |
| 14628 |
' Dictionaries preserve insertion order. Note that ' |
| 14629 |
'updating a key\n' |
| 14630 |
' does not affect the order. Keys added after deletion are ' |
| 14631 |
'inserted\n' |
| 14632 |
' at the end.\n' |
| 14633 |
'\n' |
| 14634 |
' >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}\n' |
| 14635 |
' >>> d\n' |
| 14636 |
" {'one': 1, 'two': 2, 'three': 3, 'four': 4}\n" |
| 14637 |
' >>> list(d)\n' |
| 14638 |
" ['one', 'two', 'three', 'four']\n" |
| 14639 |
' >>> list(d.values())\n' |
| 14640 |
' [1, 2, 3, 4]\n' |
| 14641 |
' >>> d["one"] = 42\n' |
| 14642 |
' >>> d\n' |
| 14643 |
" {'one': 42, 'two': 2, 'three': 3, 'four': 4}\n" |
| 14644 |
' >>> del d["two"]\n' |
| 14645 |
' >>> d["two"] = None\n' |
| 14646 |
' >>> d\n' |
| 14647 |
" {'one': 42, 'three': 3, 'four': 4, 'two': None}\n" |
| 14648 |
'\n' |
| 14649 |
' Changed in version 3.7: Dictionary order is guaranteed to ' |
| 14650 |
'be\n' |
| 14651 |
' insertion order. This behavior was an implementation ' |
| 14652 |
'detail of\n' |
| 14653 |
' CPython from 3.6.\n' |
| 14654 |
'\n' |
| 14655 |
' Dictionaries and dictionary views are reversible.\n' |
| 14656 |
'\n' |
| 14657 |
' >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}\n' |
| 14658 |
' >>> d\n' |
| 14659 |
" {'one': 1, 'two': 2, 'three': 3, 'four': 4}\n" |
| 14660 |
' >>> list(reversed(d))\n' |
| 14661 |
" ['four', 'three', 'two', 'one']\n" |
| 14662 |
' >>> list(reversed(d.values()))\n' |
| 14663 |
' [4, 3, 2, 1]\n' |
| 14664 |
' >>> list(reversed(d.items()))\n' |
| 14665 |
" [('four', 4), ('three', 3), ('two', 2), ('one', 1)]\n" |
| 14666 |
'\n' |
| 14667 |
' Changed in version 3.8: Dictionaries are now reversible.\n' |
| 14668 |
'\n' |
| 14669 |
'See also:\n' |
| 14670 |
'\n' |
| 14671 |
' "types.MappingProxyType" can be used to create a read-only ' |
| 14672 |
'view of a\n' |
| 14673 |
' "dict".\n' |
| 14674 |
'\n' |
| 14675 |
'\n' |
| 14676 |
'Dictionary view objects\n' |
| 14677 |
'=======================\n' |
| 14678 |
'\n' |
| 14679 |
'The objects returned by "dict.keys()", "dict.values()" and\n' |
| 14680 |
'"dict.items()" are *view objects*. They provide a dynamic ' |
| 14681 |
'view on the\n' |
| 14682 |
'dictionary’s entries, which means that when the dictionary ' |
| 14683 |
'changes,\n' |
| 14684 |
'the view reflects these changes.\n' |
| 14685 |
'\n' |
| 14686 |
'Dictionary views can be iterated over to yield their ' |
| 14687 |
'respective data,\n' |
| 14688 |
'and support membership tests:\n' |
| 14689 |
'\n' |
| 14690 |
'len(dictview)\n' |
| 14691 |
'\n' |
| 14692 |
' Return the number of entries in the dictionary.\n' |
| 14693 |
'\n' |
| 14694 |
'iter(dictview)\n' |
| 14695 |
'\n' |
| 14696 |
' Return an iterator over the keys, values or items ' |
| 14697 |
'(represented as\n' |
| 14698 |
' tuples of "(key, value)") in the dictionary.\n' |
| 14699 |
'\n' |
| 14700 |
' Keys and values are iterated over in insertion order. ' |
| 14701 |
'This allows\n' |
| 14702 |
' the creation of "(value, key)" pairs using "zip()": ' |
| 14703 |
'"pairs =\n' |
| 14704 |
' zip(d.values(), d.keys())". Another way to create the ' |
| 14705 |
'same list is\n' |
| 14706 |
' "pairs = [(v, k) for (k, v) in d.items()]".\n' |
| 14707 |
'\n' |
| 14708 |
' Iterating views while adding or deleting entries in the ' |
| 14709 |
'dictionary\n' |
| 14710 |
' may raise a "RuntimeError" or fail to iterate over all ' |
| 14711 |
'entries.\n' |
| 14712 |
'\n' |
| 14713 |
' Changed in version 3.7: Dictionary order is guaranteed to ' |
| 14714 |
'be\n' |
| 14715 |
' insertion order.\n' |
| 14716 |
'\n' |
| 14717 |
'x in dictview\n' |
| 14718 |
'\n' |
| 14719 |
' Return "True" if *x* is in the underlying dictionary’s ' |
| 14720 |
'keys, values\n' |
| 14721 |
' or items (in the latter case, *x* should be a "(key, ' |
| 14722 |
'value)"\n' |
| 14723 |
' tuple).\n' |
| 14724 |
'\n' |
| 14725 |
'reversed(dictview)\n' |
| 14726 |
'\n' |
| 14727 |
' Return a reverse iterator over the keys, values or items ' |
| 14728 |
'of the\n' |
| 14729 |
' dictionary. The view will be iterated in reverse order of ' |
| 14730 |
'the\n' |
| 14731 |
' insertion.\n' |
| 14732 |
'\n' |
| 14733 |
' Changed in version 3.8: Dictionary views are now ' |
| 14734 |
'reversible.\n' |
| 14735 |
'\n' |
| 14736 |
'dictview.mapping\n' |
| 14737 |
'\n' |
| 14738 |
' Return a "types.MappingProxyType" that wraps the ' |
| 14739 |
'original\n' |
| 14740 |
' dictionary to which the view refers.\n' |
| 14741 |
'\n' |
| 14742 |
' New in version 3.10.\n' |
| 14743 |
'\n' |
| 14744 |
'Keys views are set-like since their entries are unique and ' |
| 14745 |
'*hashable*.\n' |
| 14746 |
'If all values are hashable, so that "(key, value)" pairs are ' |
| 14747 |
'unique\n' |
| 14748 |
'and hashable, then the items view is also set-like. (Values ' |
| 14749 |
'views are\n' |
| 14750 |
'not treated as set-like since the entries are generally not ' |
| 14751 |
'unique.)\n' |
| 14752 |
'For set-like views, all of the operations defined for the ' |
| 14753 |
'abstract\n' |
| 14754 |
'base class "collections.abc.Set" are available (for example, ' |
| 14755 |
'"==",\n' |
| 14756 |
'"<", or "^").\n' |
| 14757 |
'\n' |
| 14758 |
'An example of dictionary view usage:\n' |
| 14759 |
'\n' |
| 14760 |
" >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, " |
| 14761 |
"'spam': 500}\n" |
| 14762 |
' >>> keys = dishes.keys()\n' |
| 14763 |
' >>> values = dishes.values()\n' |
| 14764 |
'\n' |
| 14765 |
' >>> # iteration\n' |
| 14766 |
' >>> n = 0\n' |
| 14767 |
' >>> for val in values:\n' |
| 14768 |
' ... n += val\n' |
| 14769 |
' >>> print(n)\n' |
| 14770 |
' 504\n' |
| 14771 |
'\n' |
| 14772 |
' >>> # keys and values are iterated over in the same order ' |
| 14773 |
'(insertion order)\n' |
| 14774 |
' >>> list(keys)\n' |
| 14775 |
" ['eggs', 'sausage', 'bacon', 'spam']\n" |
| 14776 |
' >>> list(values)\n' |
| 14777 |
' [2, 1, 1, 500]\n' |
| 14778 |
'\n' |
| 14779 |
' >>> # view objects are dynamic and reflect dict changes\n' |
| 14780 |
" >>> del dishes['eggs']\n" |
| 14781 |
" >>> del dishes['sausage']\n" |
| 14782 |
' >>> list(keys)\n' |
| 14783 |
" ['bacon', 'spam']\n" |
| 14784 |
'\n' |
| 14785 |
' >>> # set operations\n' |
| 14786 |
" >>> keys & {'eggs', 'bacon', 'salad'}\n" |
| 14787 |
" {'bacon'}\n" |
| 14788 |
" >>> keys ^ {'sausage', 'juice'} == {'juice', 'sausage', " |
| 14789 |
"'bacon', 'spam'}\n" |
| 14790 |
' True\n' |
| 14791 |
" >>> keys | ['juice', 'juice', 'juice'] == {'bacon', " |
| 14792 |
"'spam', 'juice'}\n" |
| 14793 |
' True\n' |
| 14794 |
'\n' |
| 14795 |
' >>> # get back a read-only proxy for the original ' |
| 14796 |
'dictionary\n' |
| 14797 |
' >>> values.mapping\n' |
| 14798 |
" mappingproxy({'bacon': 1, 'spam': 500})\n" |
| 14799 |
" >>> values.mapping['spam']\n" |
| 14800 |
' 500\n', |
| 14801 |
'typesmethods': 'Methods\n' |
| 14802 |
'*******\n' |
| 14803 |
'\n' |
| 14804 |
'Methods are functions that are called using the attribute ' |
| 14805 |
'notation.\n' |
| 14806 |
'There are two flavors: built-in methods (such as "append()" ' |
| 14807 |
'on lists)\n' |
| 14808 |
'and class instance method. Built-in methods are described ' |
| 14809 |
'with the\n' |
| 14810 |
'types that support them.\n' |
| 14811 |
'\n' |
| 14812 |
'If you access a method (a function defined in a class ' |
| 14813 |
'namespace)\n' |
| 14814 |
'through an instance, you get a special object: a *bound ' |
| 14815 |
'method* (also\n' |
| 14816 |
'called instance method) object. When called, it will add the ' |
| 14817 |
'"self"\n' |
| 14818 |
'argument to the argument list. Bound methods have two ' |
| 14819 |
'special read-\n' |
| 14820 |
'only attributes: "m.__self__" is the object on which the ' |
| 14821 |
'method\n' |
| 14822 |
'operates, and "m.__func__" is the function implementing the ' |
| 14823 |
'method.\n' |
| 14824 |
'Calling "m(arg-1, arg-2, ..., arg-n)" is completely ' |
| 14825 |
'equivalent to\n' |
| 14826 |
'calling "m.__func__(m.__self__, arg-1, arg-2, ..., arg-n)".\n' |
| 14827 |
'\n' |
| 14828 |
'Like function objects, bound method objects support getting ' |
| 14829 |
'arbitrary\n' |
| 14830 |
'attributes. However, since method attributes are actually ' |
| 14831 |
'stored on\n' |
| 14832 |
'the underlying function object ("method.__func__"), setting ' |
| 14833 |
'method\n' |
| 14834 |
'attributes on bound methods is disallowed. Attempting to ' |
| 14835 |
'set an\n' |
| 14836 |
'attribute on a method results in an "AttributeError" being ' |
| 14837 |
'raised. In\n' |
| 14838 |
'order to set a method attribute, you need to explicitly set ' |
| 14839 |
'it on the\n' |
| 14840 |
'underlying function object:\n' |
| 14841 |
'\n' |
| 14842 |
' >>> class C:\n' |
| 14843 |
' ... def method(self):\n' |
| 14844 |
' ... pass\n' |
| 14845 |
' ...\n' |
| 14846 |
' >>> c = C()\n' |
| 14847 |
" >>> c.method.whoami = 'my name is method' # can't set on " |
| 14848 |
'the method\n' |
| 14849 |
' Traceback (most recent call last):\n' |
| 14850 |
' File "<stdin>", line 1, in <module>\n' |
| 14851 |
" AttributeError: 'method' object has no attribute " |
| 14852 |
"'whoami'\n" |
| 14853 |
" >>> c.method.__func__.whoami = 'my name is method'\n" |
| 14854 |
' >>> c.method.whoami\n' |
| 14855 |
" 'my name is method'\n" |
| 14856 |
'\n' |
| 14857 |
'See Instance methods for more information.\n', |
| 14858 |
'typesmodules': 'Modules\n' |
| 14859 |
'*******\n' |
| 14860 |
'\n' |
| 14861 |
'The only special operation on a module is attribute access: ' |
| 14862 |
'"m.name",\n' |
| 14863 |
'where *m* is a module and *name* accesses a name defined in ' |
| 14864 |
'*m*’s\n' |
| 14865 |
'symbol table. Module attributes can be assigned to. (Note ' |
| 14866 |
'that the\n' |
| 14867 |
'"import" statement is not, strictly speaking, an operation ' |
| 14868 |
'on a module\n' |
| 14869 |
'object; "import foo" does not require a module object named ' |
| 14870 |
'*foo* to\n' |
| 14871 |
'exist, rather it requires an (external) *definition* for a ' |
| 14872 |
'module\n' |
| 14873 |
'named *foo* somewhere.)\n' |
| 14874 |
'\n' |
| 14875 |
'A special attribute of every module is "__dict__". This is ' |
| 14876 |
'the\n' |
| 14877 |
'dictionary containing the module’s symbol table. Modifying ' |
| 14878 |
'this\n' |
| 14879 |
'dictionary will actually change the module’s symbol table, ' |
| 14880 |
'but direct\n' |
| 14881 |
'assignment to the "__dict__" attribute is not possible (you ' |
| 14882 |
'can write\n' |
| 14883 |
'"m.__dict__[\'a\'] = 1", which defines "m.a" to be "1", but ' |
| 14884 |
'you can’t\n' |
| 14885 |
'write "m.__dict__ = {}"). Modifying "__dict__" directly is ' |
| 14886 |
'not\n' |
| 14887 |
'recommended.\n' |
| 14888 |
'\n' |
| 14889 |
'Modules built into the interpreter are written like this: ' |
| 14890 |
'"<module\n' |
| 14891 |
'\'sys\' (built-in)>". If loaded from a file, they are ' |
| 14892 |
'written as\n' |
| 14893 |
'"<module \'os\' from ' |
| 14894 |
'\'/usr/local/lib/pythonX.Y/os.pyc\'>".\n', |
| 14895 |
'typesseq': 'Sequence Types — "list", "tuple", "range"\n' |
| 14896 |
'*****************************************\n' |
| 14897 |
'\n' |
| 14898 |
'There are three basic sequence types: lists, tuples, and range\n' |
| 14899 |
'objects. Additional sequence types tailored for processing of ' |
| 14900 |
'binary\n' |
| 14901 |
'data and text strings are described in dedicated sections.\n' |
| 14902 |
'\n' |
| 14903 |
'\n' |
| 14904 |
'Common Sequence Operations\n' |
| 14905 |
'==========================\n' |
| 14906 |
'\n' |
| 14907 |
'The operations in the following table are supported by most ' |
| 14908 |
'sequence\n' |
| 14909 |
'types, both mutable and immutable. The ' |
| 14910 |
'"collections.abc.Sequence" ABC\n' |
| 14911 |
'is provided to make it easier to correctly implement these ' |
| 14912 |
'operations\n' |
| 14913 |
'on custom sequence types.\n' |
| 14914 |
'\n' |
| 14915 |
'This table lists the sequence operations sorted in ascending ' |
| 14916 |
'priority.\n' |
| 14917 |
'In the table, *s* and *t* are sequences of the same type, *n*, ' |
| 14918 |
'*i*,\n' |
| 14919 |
'*j* and *k* are integers and *x* is an arbitrary object that ' |
| 14920 |
'meets any\n' |
| 14921 |
'type and value restrictions imposed by *s*.\n' |
| 14922 |
'\n' |
| 14923 |
'The "in" and "not in" operations have the same priorities as ' |
| 14924 |
'the\n' |
| 14925 |
'comparison operations. The "+" (concatenation) and "*" ' |
| 14926 |
'(repetition)\n' |
| 14927 |
'operations have the same priority as the corresponding numeric\n' |
| 14928 |
'operations. [3]\n' |
| 14929 |
'\n' |
| 14930 |
'+----------------------------+----------------------------------+------------+\n' |
| 14931 |
'| Operation | Result ' |
| 14932 |
'| Notes |\n' |
| 14933 |
'|============================|==================================|============|\n' |
| 14934 |
'| "x in s" | "True" if an item of *s* is ' |
| 14935 |
'| (1) |\n' |
| 14936 |
'| | equal to *x*, else "False" ' |
| 14937 |
'| |\n' |
| 14938 |
'+----------------------------+----------------------------------+------------+\n' |
| 14939 |
'| "x not in s" | "False" if an item of *s* is ' |
| 14940 |
'| (1) |\n' |
| 14941 |
'| | equal to *x*, else "True" ' |
| 14942 |
'| |\n' |
| 14943 |
'+----------------------------+----------------------------------+------------+\n' |
| 14944 |
'| "s + t" | the concatenation of *s* and *t* ' |
| 14945 |
'| (6)(7) |\n' |
| 14946 |
'+----------------------------+----------------------------------+------------+\n' |
| 14947 |
'| "s * n" or "n * s" | equivalent to adding *s* to ' |
| 14948 |
'| (2)(7) |\n' |
| 14949 |
'| | itself *n* times ' |
| 14950 |
'| |\n' |
| 14951 |
'+----------------------------+----------------------------------+------------+\n' |
| 14952 |
'| "s[i]" | *i*th item of *s*, origin 0 ' |
| 14953 |
'| (3) |\n' |
| 14954 |
'+----------------------------+----------------------------------+------------+\n' |
| 14955 |
'| "s[i:j]" | slice of *s* from *i* to *j* ' |
| 14956 |
'| (3)(4) |\n' |
| 14957 |
'+----------------------------+----------------------------------+------------+\n' |
| 14958 |
'| "s[i:j:k]" | slice of *s* from *i* to *j* ' |
| 14959 |
'| (3)(5) |\n' |
| 14960 |
'| | with step *k* ' |
| 14961 |
'| |\n' |
| 14962 |
'+----------------------------+----------------------------------+------------+\n' |
| 14963 |
'| "len(s)" | length of *s* ' |
| 14964 |
'| |\n' |
| 14965 |
'+----------------------------+----------------------------------+------------+\n' |
| 14966 |
'| "min(s)" | smallest item of *s* ' |
| 14967 |
'| |\n' |
| 14968 |
'+----------------------------+----------------------------------+------------+\n' |
| 14969 |
'| "max(s)" | largest item of *s* ' |
| 14970 |
'| |\n' |
| 14971 |
'+----------------------------+----------------------------------+------------+\n' |
| 14972 |
'| "s.index(x[, i[, j]])" | index of the first occurrence of ' |
| 14973 |
'| (8) |\n' |
| 14974 |
'| | *x* in *s* (at or after index ' |
| 14975 |
'| |\n' |
| 14976 |
'| | *i* and before index *j*) ' |
| 14977 |
'| |\n' |
| 14978 |
'+----------------------------+----------------------------------+------------+\n' |
| 14979 |
'| "s.count(x)" | total number of occurrences of ' |
| 14980 |
'| |\n' |
| 14981 |
'| | *x* in *s* ' |
| 14982 |
'| |\n' |
| 14983 |
'+----------------------------+----------------------------------+------------+\n' |
| 14984 |
'\n' |
| 14985 |
'Sequences of the same type also support comparisons. In ' |
| 14986 |
'particular,\n' |
| 14987 |
'tuples and lists are compared lexicographically by comparing\n' |
| 14988 |
'corresponding elements. This means that to compare equal, every\n' |
| 14989 |
'element must compare equal and the two sequences must be of the ' |
| 14990 |
'same\n' |
| 14991 |
'type and have the same length. (For full details see ' |
| 14992 |
'Comparisons in\n' |
| 14993 |
'the language reference.)\n' |
| 14994 |
'\n' |
| 14995 |
'Forward and reversed iterators over mutable sequences access ' |
| 14996 |
'values\n' |
| 14997 |
'using an index. That index will continue to march forward (or\n' |
| 14998 |
'backward) even if the underlying sequence is mutated. The ' |
| 14999 |
'iterator\n' |
| 15000 |
'terminates only when an "IndexError" or a "StopIteration" is\n' |
| 15001 |
'encountered (or when the index drops below zero).\n' |
| 15002 |
'\n' |
| 15003 |
'Notes:\n' |
| 15004 |
'\n' |
| 15005 |
'1. While the "in" and "not in" operations are used only for ' |
| 15006 |
'simple\n' |
| 15007 |
' containment testing in the general case, some specialised ' |
| 15008 |
'sequences\n' |
| 15009 |
' (such as "str", "bytes" and "bytearray") also use them for\n' |
| 15010 |
' subsequence testing:\n' |
| 15011 |
'\n' |
| 15012 |
' >>> "gg" in "eggs"\n' |
| 15013 |
' True\n' |
| 15014 |
'\n' |
| 15015 |
'2. Values of *n* less than "0" are treated as "0" (which yields ' |
| 15016 |
'an\n' |
| 15017 |
' empty sequence of the same type as *s*). Note that items in ' |
| 15018 |
'the\n' |
| 15019 |
' sequence *s* are not copied; they are referenced multiple ' |
| 15020 |
'times.\n' |
| 15021 |
' This often haunts new Python programmers; consider:\n' |
| 15022 |
'\n' |
| 15023 |
' >>> lists = [[]] * 3\n' |
| 15024 |
' >>> lists\n' |
| 15025 |
' [[], [], []]\n' |
| 15026 |
' >>> lists[0].append(3)\n' |
| 15027 |
' >>> lists\n' |
| 15028 |
' [[3], [3], [3]]\n' |
| 15029 |
'\n' |
| 15030 |
' What has happened is that "[[]]" is a one-element list ' |
| 15031 |
'containing\n' |
| 15032 |
' an empty list, so all three elements of "[[]] * 3" are ' |
| 15033 |
'references\n' |
| 15034 |
' to this single empty list. Modifying any of the elements of\n' |
| 15035 |
' "lists" modifies this single list. You can create a list of\n' |
| 15036 |
' different lists this way:\n' |
| 15037 |
'\n' |
| 15038 |
' >>> lists = [[] for i in range(3)]\n' |
| 15039 |
' >>> lists[0].append(3)\n' |
| 15040 |
' >>> lists[1].append(5)\n' |
| 15041 |
' >>> lists[2].append(7)\n' |
| 15042 |
' >>> lists\n' |
| 15043 |
' [[3], [5], [7]]\n' |
| 15044 |
'\n' |
| 15045 |
' Further explanation is available in the FAQ entry How do I ' |
| 15046 |
'create a\n' |
| 15047 |
' multidimensional list?.\n' |
| 15048 |
'\n' |
| 15049 |
'3. If *i* or *j* is negative, the index is relative to the end ' |
| 15050 |
'of\n' |
| 15051 |
' sequence *s*: "len(s) + i" or "len(s) + j" is substituted. ' |
| 15052 |
'But\n' |
| 15053 |
' note that "-0" is still "0".\n' |
| 15054 |
'\n' |
| 15055 |
'4. The slice of *s* from *i* to *j* is defined as the sequence ' |
| 15056 |
'of\n' |
| 15057 |
' items with index *k* such that "i <= k < j". If *i* or *j* ' |
| 15058 |
'is\n' |
| 15059 |
' greater than "len(s)", use "len(s)". If *i* is omitted or ' |
| 15060 |
'"None",\n' |
| 15061 |
' use "0". If *j* is omitted or "None", use "len(s)". If *i* ' |
| 15062 |
'is\n' |
| 15063 |
' greater than or equal to *j*, the slice is empty.\n' |
| 15064 |
'\n' |
| 15065 |
'5. The slice of *s* from *i* to *j* with step *k* is defined as ' |
| 15066 |
'the\n' |
| 15067 |
' sequence of items with index "x = i + n*k" such that "0 <= n ' |
| 15068 |
'<\n' |
| 15069 |
' (j-i)/k". In other words, the indices are "i", "i+k", ' |
| 15070 |
'"i+2*k",\n' |
| 15071 |
' "i+3*k" and so on, stopping when *j* is reached (but never\n' |
| 15072 |
' including *j*). When *k* is positive, *i* and *j* are ' |
| 15073 |
'reduced to\n' |
| 15074 |
' "len(s)" if they are greater. When *k* is negative, *i* and ' |
| 15075 |
'*j* are\n' |
| 15076 |
' reduced to "len(s) - 1" if they are greater. If *i* or *j* ' |
| 15077 |
'are\n' |
| 15078 |
' omitted or "None", they become “end” values (which end ' |
| 15079 |
'depends on\n' |
| 15080 |
' the sign of *k*). Note, *k* cannot be zero. If *k* is ' |
| 15081 |
'"None", it\n' |
| 15082 |
' is treated like "1".\n' |
| 15083 |
'\n' |
| 15084 |
'6. Concatenating immutable sequences always results in a new ' |
| 15085 |
'object.\n' |
| 15086 |
' This means that building up a sequence by repeated ' |
| 15087 |
'concatenation\n' |
| 15088 |
' will have a quadratic runtime cost in the total sequence ' |
| 15089 |
'length.\n' |
| 15090 |
' To get a linear runtime cost, you must switch to one of the\n' |
| 15091 |
' alternatives below:\n' |
| 15092 |
'\n' |
| 15093 |
' * if concatenating "str" objects, you can build a list and ' |
| 15094 |
'use\n' |
| 15095 |
' "str.join()" at the end or else write to an "io.StringIO"\n' |
| 15096 |
' instance and retrieve its value when complete\n' |
| 15097 |
'\n' |
| 15098 |
' * if concatenating "bytes" objects, you can similarly use\n' |
| 15099 |
' "bytes.join()" or "io.BytesIO", or you can do in-place\n' |
| 15100 |
' concatenation with a "bytearray" object. "bytearray" ' |
| 15101 |
'objects are\n' |
| 15102 |
' mutable and have an efficient overallocation mechanism\n' |
| 15103 |
'\n' |
| 15104 |
' * if concatenating "tuple" objects, extend a "list" instead\n' |
| 15105 |
'\n' |
| 15106 |
' * for other types, investigate the relevant class ' |
| 15107 |
'documentation\n' |
| 15108 |
'\n' |
| 15109 |
'7. Some sequence types (such as "range") only support item ' |
| 15110 |
'sequences\n' |
| 15111 |
' that follow specific patterns, and hence don’t support ' |
| 15112 |
'sequence\n' |
| 15113 |
' concatenation or repetition.\n' |
| 15114 |
'\n' |
| 15115 |
'8. "index" raises "ValueError" when *x* is not found in *s*. Not ' |
| 15116 |
'all\n' |
| 15117 |
' implementations support passing the additional arguments *i* ' |
| 15118 |
'and\n' |
| 15119 |
' *j*. These arguments allow efficient searching of subsections ' |
| 15120 |
'of\n' |
| 15121 |
' the sequence. Passing the extra arguments is roughly ' |
| 15122 |
'equivalent to\n' |
| 15123 |
' using "s[i:j].index(x)", only without copying any data and ' |
| 15124 |
'with the\n' |
| 15125 |
' returned index being relative to the start of the sequence ' |
| 15126 |
'rather\n' |
| 15127 |
' than the start of the slice.\n' |
| 15128 |
'\n' |
| 15129 |
'\n' |
| 15130 |
'Immutable Sequence Types\n' |
| 15131 |
'========================\n' |
| 15132 |
'\n' |
| 15133 |
'The only operation that immutable sequence types generally ' |
| 15134 |
'implement\n' |
| 15135 |
'that is not also implemented by mutable sequence types is ' |
| 15136 |
'support for\n' |
| 15137 |
'the "hash()" built-in.\n' |
| 15138 |
'\n' |
| 15139 |
'This support allows immutable sequences, such as "tuple" ' |
| 15140 |
'instances, to\n' |
| 15141 |
'be used as "dict" keys and stored in "set" and "frozenset" ' |
| 15142 |
'instances.\n' |
| 15143 |
'\n' |
| 15144 |
'Attempting to hash an immutable sequence that contains ' |
| 15145 |
'unhashable\n' |
| 15146 |
'values will result in "TypeError".\n' |
| 15147 |
'\n' |
| 15148 |
'\n' |
| 15149 |
'Mutable Sequence Types\n' |
| 15150 |
'======================\n' |
| 15151 |
'\n' |
| 15152 |
'The operations in the following table are defined on mutable ' |
| 15153 |
'sequence\n' |
| 15154 |
'types. The "collections.abc.MutableSequence" ABC is provided to ' |
| 15155 |
'make\n' |
| 15156 |
'it easier to correctly implement these operations on custom ' |
| 15157 |
'sequence\n' |
| 15158 |
'types.\n' |
| 15159 |
'\n' |
| 15160 |
'In the table *s* is an instance of a mutable sequence type, *t* ' |
| 15161 |
'is any\n' |
| 15162 |
'iterable object and *x* is an arbitrary object that meets any ' |
| 15163 |
'type and\n' |
| 15164 |
'value restrictions imposed by *s* (for example, "bytearray" ' |
| 15165 |
'only\n' |
| 15166 |
'accepts integers that meet the value restriction "0 <= x <= ' |
| 15167 |
'255").\n' |
| 15168 |
'\n' |
| 15169 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15170 |
'| Operation | ' |
| 15171 |
'Result | Notes |\n' |
| 15172 |
'|================================|==================================|=======================|\n' |
| 15173 |
'| "s[i] = x" | item *i* of *s* is replaced ' |
| 15174 |
'by | |\n' |
| 15175 |
'| | ' |
| 15176 |
'*x* | |\n' |
| 15177 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15178 |
'| "s[i:j] = t" | slice of *s* from *i* to *j* ' |
| 15179 |
'is | |\n' |
| 15180 |
'| | replaced by the contents of ' |
| 15181 |
'the | |\n' |
| 15182 |
'| | iterable ' |
| 15183 |
'*t* | |\n' |
| 15184 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15185 |
'| "del s[i:j]" | same as "s[i:j] = ' |
| 15186 |
'[]" | |\n' |
| 15187 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15188 |
'| "s[i:j:k] = t" | the elements of "s[i:j:k]" ' |
| 15189 |
'are | (1) |\n' |
| 15190 |
'| | replaced by those of ' |
| 15191 |
'*t* | |\n' |
| 15192 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15193 |
'| "del s[i:j:k]" | removes the elements ' |
| 15194 |
'of | |\n' |
| 15195 |
'| | "s[i:j:k]" from the ' |
| 15196 |
'list | |\n' |
| 15197 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15198 |
'| "s.append(x)" | appends *x* to the end of ' |
| 15199 |
'the | |\n' |
| 15200 |
'| | sequence (same ' |
| 15201 |
'as | |\n' |
| 15202 |
'| | "s[len(s):len(s)] = ' |
| 15203 |
'[x]") | |\n' |
| 15204 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15205 |
'| "s.clear()" | removes all items from *s* ' |
| 15206 |
'(same | (5) |\n' |
| 15207 |
'| | as "del ' |
| 15208 |
's[:]") | |\n' |
| 15209 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15210 |
'| "s.copy()" | creates a shallow copy of ' |
| 15211 |
'*s* | (5) |\n' |
| 15212 |
'| | (same as ' |
| 15213 |
'"s[:]") | |\n' |
| 15214 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15215 |
'| "s.extend(t)" or "s += t" | extends *s* with the contents ' |
| 15216 |
'of | |\n' |
| 15217 |
'| | *t* (for the most part the ' |
| 15218 |
'same | |\n' |
| 15219 |
'| | as "s[len(s):len(s)] = ' |
| 15220 |
't") | |\n' |
| 15221 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15222 |
'| "s *= n" | updates *s* with its ' |
| 15223 |
'contents | (6) |\n' |
| 15224 |
'| | repeated *n* ' |
| 15225 |
'times | |\n' |
| 15226 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15227 |
'| "s.insert(i, x)" | inserts *x* into *s* at ' |
| 15228 |
'the | |\n' |
| 15229 |
'| | index given by *i* (same ' |
| 15230 |
'as | |\n' |
| 15231 |
'| | "s[i:i] = ' |
| 15232 |
'[x]") | |\n' |
| 15233 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15234 |
'| "s.pop()" or "s.pop(i)" | retrieves the item at *i* ' |
| 15235 |
'and | (2) |\n' |
| 15236 |
'| | also removes it from ' |
| 15237 |
'*s* | |\n' |
| 15238 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15239 |
'| "s.remove(x)" | remove the first item from ' |
| 15240 |
'*s* | (3) |\n' |
| 15241 |
'| | where "s[i]" is equal to ' |
| 15242 |
'*x* | |\n' |
| 15243 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15244 |
'| "s.reverse()" | reverses the items of *s* ' |
| 15245 |
'in | (4) |\n' |
| 15246 |
'| | ' |
| 15247 |
'place | |\n' |
| 15248 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15249 |
'\n' |
| 15250 |
'Notes:\n' |
| 15251 |
'\n' |
| 15252 |
'1. *t* must have the same length as the slice it is replacing.\n' |
| 15253 |
'\n' |
| 15254 |
'2. The optional argument *i* defaults to "-1", so that by ' |
| 15255 |
'default the\n' |
| 15256 |
' last item is removed and returned.\n' |
| 15257 |
'\n' |
| 15258 |
'3. "remove()" raises "ValueError" when *x* is not found in *s*.\n' |
| 15259 |
'\n' |
| 15260 |
'4. The "reverse()" method modifies the sequence in place for ' |
| 15261 |
'economy\n' |
| 15262 |
' of space when reversing a large sequence. To remind users ' |
| 15263 |
'that it\n' |
| 15264 |
' operates by side effect, it does not return the reversed ' |
| 15265 |
'sequence.\n' |
| 15266 |
'\n' |
| 15267 |
'5. "clear()" and "copy()" are included for consistency with the\n' |
| 15268 |
' interfaces of mutable containers that don’t support slicing\n' |
| 15269 |
' operations (such as "dict" and "set"). "copy()" is not part ' |
| 15270 |
'of the\n' |
| 15271 |
' "collections.abc.MutableSequence" ABC, but most concrete ' |
| 15272 |
'mutable\n' |
| 15273 |
' sequence classes provide it.\n' |
| 15274 |
'\n' |
| 15275 |
' New in version 3.3: "clear()" and "copy()" methods.\n' |
| 15276 |
'\n' |
| 15277 |
'6. The value *n* is an integer, or an object implementing\n' |
| 15278 |
' "__index__()". Zero and negative values of *n* clear the ' |
| 15279 |
'sequence.\n' |
| 15280 |
' Items in the sequence are not copied; they are referenced ' |
| 15281 |
'multiple\n' |
| 15282 |
' times, as explained for "s * n" under Common Sequence ' |
| 15283 |
'Operations.\n' |
| 15284 |
'\n' |
| 15285 |
'\n' |
| 15286 |
'Lists\n' |
| 15287 |
'=====\n' |
| 15288 |
'\n' |
| 15289 |
'Lists are mutable sequences, typically used to store collections ' |
| 15290 |
'of\n' |
| 15291 |
'homogeneous items (where the precise degree of similarity will ' |
| 15292 |
'vary by\n' |
| 15293 |
'application).\n' |
| 15294 |
'\n' |
| 15295 |
'class list([iterable])\n' |
| 15296 |
'\n' |
| 15297 |
' Lists may be constructed in several ways:\n' |
| 15298 |
'\n' |
| 15299 |
' * Using a pair of square brackets to denote the empty list: ' |
| 15300 |
'"[]"\n' |
| 15301 |
'\n' |
| 15302 |
' * Using square brackets, separating items with commas: "[a]", ' |
| 15303 |
'"[a,\n' |
| 15304 |
' b, c]"\n' |
| 15305 |
'\n' |
| 15306 |
' * Using a list comprehension: "[x for x in iterable]"\n' |
| 15307 |
'\n' |
| 15308 |
' * Using the type constructor: "list()" or "list(iterable)"\n' |
| 15309 |
'\n' |
| 15310 |
' The constructor builds a list whose items are the same and in ' |
| 15311 |
'the\n' |
| 15312 |
' same order as *iterable*’s items. *iterable* may be either ' |
| 15313 |
'a\n' |
| 15314 |
' sequence, a container that supports iteration, or an ' |
| 15315 |
'iterator\n' |
| 15316 |
' object. If *iterable* is already a list, a copy is made and\n' |
| 15317 |
' returned, similar to "iterable[:]". For example, ' |
| 15318 |
'"list(\'abc\')"\n' |
| 15319 |
' returns "[\'a\', \'b\', \'c\']" and "list( (1, 2, 3) )" ' |
| 15320 |
'returns "[1, 2,\n' |
| 15321 |
' 3]". If no argument is given, the constructor creates a new ' |
| 15322 |
'empty\n' |
| 15323 |
' list, "[]".\n' |
| 15324 |
'\n' |
| 15325 |
' Many other operations also produce lists, including the ' |
| 15326 |
'"sorted()"\n' |
| 15327 |
' built-in.\n' |
| 15328 |
'\n' |
| 15329 |
' Lists implement all of the common and mutable sequence ' |
| 15330 |
'operations.\n' |
| 15331 |
' Lists also provide the following additional method:\n' |
| 15332 |
'\n' |
| 15333 |
' sort(*, key=None, reverse=False)\n' |
| 15334 |
'\n' |
| 15335 |
' This method sorts the list in place, using only "<" ' |
| 15336 |
'comparisons\n' |
| 15337 |
' between items. Exceptions are not suppressed - if any ' |
| 15338 |
'comparison\n' |
| 15339 |
' operations fail, the entire sort operation will fail (and ' |
| 15340 |
'the\n' |
| 15341 |
' list will likely be left in a partially modified state).\n' |
| 15342 |
'\n' |
| 15343 |
' "sort()" accepts two arguments that can only be passed by\n' |
| 15344 |
' keyword (keyword-only arguments):\n' |
| 15345 |
'\n' |
| 15346 |
' *key* specifies a function of one argument that is used ' |
| 15347 |
'to\n' |
| 15348 |
' extract a comparison key from each list element (for ' |
| 15349 |
'example,\n' |
| 15350 |
' "key=str.lower"). The key corresponding to each item in ' |
| 15351 |
'the list\n' |
| 15352 |
' is calculated once and then used for the entire sorting ' |
| 15353 |
'process.\n' |
| 15354 |
' The default value of "None" means that list items are ' |
| 15355 |
'sorted\n' |
| 15356 |
' directly without calculating a separate key value.\n' |
| 15357 |
'\n' |
| 15358 |
' The "functools.cmp_to_key()" utility is available to ' |
| 15359 |
'convert a\n' |
| 15360 |
' 2.x style *cmp* function to a *key* function.\n' |
| 15361 |
'\n' |
| 15362 |
' *reverse* is a boolean value. If set to "True", then the ' |
| 15363 |
'list\n' |
| 15364 |
' elements are sorted as if each comparison were reversed.\n' |
| 15365 |
'\n' |
| 15366 |
' This method modifies the sequence in place for economy of ' |
| 15367 |
'space\n' |
| 15368 |
' when sorting a large sequence. To remind users that it ' |
| 15369 |
'operates\n' |
| 15370 |
' by side effect, it does not return the sorted sequence ' |
| 15371 |
'(use\n' |
| 15372 |
' "sorted()" to explicitly request a new sorted list ' |
| 15373 |
'instance).\n' |
| 15374 |
'\n' |
| 15375 |
' The "sort()" method is guaranteed to be stable. A sort ' |
| 15376 |
'is\n' |
| 15377 |
' stable if it guarantees not to change the relative order ' |
| 15378 |
'of\n' |
| 15379 |
' elements that compare equal — this is helpful for sorting ' |
| 15380 |
'in\n' |
| 15381 |
' multiple passes (for example, sort by department, then by ' |
| 15382 |
'salary\n' |
| 15383 |
' grade).\n' |
| 15384 |
'\n' |
| 15385 |
' For sorting examples and a brief sorting tutorial, see ' |
| 15386 |
'Sorting\n' |
| 15387 |
' HOW TO.\n' |
| 15388 |
'\n' |
| 15389 |
' **CPython implementation detail:** While a list is being ' |
| 15390 |
'sorted,\n' |
| 15391 |
' the effect of attempting to mutate, or even inspect, the ' |
| 15392 |
'list is\n' |
| 15393 |
' undefined. The C implementation of Python makes the list ' |
| 15394 |
'appear\n' |
| 15395 |
' empty for the duration, and raises "ValueError" if it can ' |
| 15396 |
'detect\n' |
| 15397 |
' that the list has been mutated during a sort.\n' |
| 15398 |
'\n' |
| 15399 |
'\n' |
| 15400 |
'Tuples\n' |
| 15401 |
'======\n' |
| 15402 |
'\n' |
| 15403 |
'Tuples are immutable sequences, typically used to store ' |
| 15404 |
'collections of\n' |
| 15405 |
'heterogeneous data (such as the 2-tuples produced by the ' |
| 15406 |
'"enumerate()"\n' |
| 15407 |
'built-in). Tuples are also used for cases where an immutable ' |
| 15408 |
'sequence\n' |
| 15409 |
'of homogeneous data is needed (such as allowing storage in a ' |
| 15410 |
'"set" or\n' |
| 15411 |
'"dict" instance).\n' |
| 15412 |
'\n' |
| 15413 |
'class tuple([iterable])\n' |
| 15414 |
'\n' |
| 15415 |
' Tuples may be constructed in a number of ways:\n' |
| 15416 |
'\n' |
| 15417 |
' * Using a pair of parentheses to denote the empty tuple: ' |
| 15418 |
'"()"\n' |
| 15419 |
'\n' |
| 15420 |
' * Using a trailing comma for a singleton tuple: "a," or ' |
| 15421 |
'"(a,)"\n' |
| 15422 |
'\n' |
| 15423 |
' * Separating items with commas: "a, b, c" or "(a, b, c)"\n' |
| 15424 |
'\n' |
| 15425 |
' * Using the "tuple()" built-in: "tuple()" or ' |
| 15426 |
'"tuple(iterable)"\n' |
| 15427 |
'\n' |
| 15428 |
' The constructor builds a tuple whose items are the same and ' |
| 15429 |
'in the\n' |
| 15430 |
' same order as *iterable*’s items. *iterable* may be either ' |
| 15431 |
'a\n' |
| 15432 |
' sequence, a container that supports iteration, or an ' |
| 15433 |
'iterator\n' |
| 15434 |
' object. If *iterable* is already a tuple, it is returned\n' |
| 15435 |
' unchanged. For example, "tuple(\'abc\')" returns "(\'a\', ' |
| 15436 |
'\'b\', \'c\')"\n' |
| 15437 |
' and "tuple( [1, 2, 3] )" returns "(1, 2, 3)". If no argument ' |
| 15438 |
'is\n' |
| 15439 |
' given, the constructor creates a new empty tuple, "()".\n' |
| 15440 |
'\n' |
| 15441 |
' Note that it is actually the comma which makes a tuple, not ' |
| 15442 |
'the\n' |
| 15443 |
' parentheses. The parentheses are optional, except in the ' |
| 15444 |
'empty\n' |
| 15445 |
' tuple case, or when they are needed to avoid syntactic ' |
| 15446 |
'ambiguity.\n' |
| 15447 |
' For example, "f(a, b, c)" is a function call with three ' |
| 15448 |
'arguments,\n' |
| 15449 |
' while "f((a, b, c))" is a function call with a 3-tuple as the ' |
| 15450 |
'sole\n' |
| 15451 |
' argument.\n' |
| 15452 |
'\n' |
| 15453 |
' Tuples implement all of the common sequence operations.\n' |
| 15454 |
'\n' |
| 15455 |
'For heterogeneous collections of data where access by name is ' |
| 15456 |
'clearer\n' |
| 15457 |
'than access by index, "collections.namedtuple()" may be a more\n' |
| 15458 |
'appropriate choice than a simple tuple object.\n' |
| 15459 |
'\n' |
| 15460 |
'\n' |
| 15461 |
'Ranges\n' |
| 15462 |
'======\n' |
| 15463 |
'\n' |
| 15464 |
'The "range" type represents an immutable sequence of numbers and ' |
| 15465 |
'is\n' |
| 15466 |
'commonly used for looping a specific number of times in "for" ' |
| 15467 |
'loops.\n' |
| 15468 |
'\n' |
| 15469 |
'class range(stop)\n' |
| 15470 |
'class range(start, stop[, step])\n' |
| 15471 |
'\n' |
| 15472 |
' The arguments to the range constructor must be integers ' |
| 15473 |
'(either\n' |
| 15474 |
' built-in "int" or any object that implements the ' |
| 15475 |
'"__index__()"\n' |
| 15476 |
' special method). If the *step* argument is omitted, it ' |
| 15477 |
'defaults to\n' |
| 15478 |
' "1". If the *start* argument is omitted, it defaults to "0". ' |
| 15479 |
'If\n' |
| 15480 |
' *step* is zero, "ValueError" is raised.\n' |
| 15481 |
'\n' |
| 15482 |
' For a positive *step*, the contents of a range "r" are ' |
| 15483 |
'determined\n' |
| 15484 |
' by the formula "r[i] = start + step*i" where "i >= 0" and ' |
| 15485 |
'"r[i] <\n' |
| 15486 |
' stop".\n' |
| 15487 |
'\n' |
| 15488 |
' For a negative *step*, the contents of the range are still\n' |
| 15489 |
' determined by the formula "r[i] = start + step*i", but the\n' |
| 15490 |
' constraints are "i >= 0" and "r[i] > stop".\n' |
| 15491 |
'\n' |
| 15492 |
' A range object will be empty if "r[0]" does not meet the ' |
| 15493 |
'value\n' |
| 15494 |
' constraint. Ranges do support negative indices, but these ' |
| 15495 |
'are\n' |
| 15496 |
' interpreted as indexing from the end of the sequence ' |
| 15497 |
'determined by\n' |
| 15498 |
' the positive indices.\n' |
| 15499 |
'\n' |
| 15500 |
' Ranges containing absolute values larger than "sys.maxsize" ' |
| 15501 |
'are\n' |
| 15502 |
' permitted but some features (such as "len()") may raise\n' |
| 15503 |
' "OverflowError".\n' |
| 15504 |
'\n' |
| 15505 |
' Range examples:\n' |
| 15506 |
'\n' |
| 15507 |
' >>> list(range(10))\n' |
| 15508 |
' [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n' |
| 15509 |
' >>> list(range(1, 11))\n' |
| 15510 |
' [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n' |
| 15511 |
' >>> list(range(0, 30, 5))\n' |
| 15512 |
' [0, 5, 10, 15, 20, 25]\n' |
| 15513 |
' >>> list(range(0, 10, 3))\n' |
| 15514 |
' [0, 3, 6, 9]\n' |
| 15515 |
' >>> list(range(0, -10, -1))\n' |
| 15516 |
' [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]\n' |
| 15517 |
' >>> list(range(0))\n' |
| 15518 |
' []\n' |
| 15519 |
' >>> list(range(1, 0))\n' |
| 15520 |
' []\n' |
| 15521 |
'\n' |
| 15522 |
' Ranges implement all of the common sequence operations ' |
| 15523 |
'except\n' |
| 15524 |
' concatenation and repetition (due to the fact that range ' |
| 15525 |
'objects\n' |
| 15526 |
' can only represent sequences that follow a strict pattern ' |
| 15527 |
'and\n' |
| 15528 |
' repetition and concatenation will usually violate that ' |
| 15529 |
'pattern).\n' |
| 15530 |
'\n' |
| 15531 |
' start\n' |
| 15532 |
'\n' |
| 15533 |
' The value of the *start* parameter (or "0" if the ' |
| 15534 |
'parameter was\n' |
| 15535 |
' not supplied)\n' |
| 15536 |
'\n' |
| 15537 |
' stop\n' |
| 15538 |
'\n' |
| 15539 |
' The value of the *stop* parameter\n' |
| 15540 |
'\n' |
| 15541 |
' step\n' |
| 15542 |
'\n' |
| 15543 |
' The value of the *step* parameter (or "1" if the parameter ' |
| 15544 |
'was\n' |
| 15545 |
' not supplied)\n' |
| 15546 |
'\n' |
| 15547 |
'The advantage of the "range" type over a regular "list" or ' |
| 15548 |
'"tuple" is\n' |
| 15549 |
'that a "range" object will always take the same (small) amount ' |
| 15550 |
'of\n' |
| 15551 |
'memory, no matter the size of the range it represents (as it ' |
| 15552 |
'only\n' |
| 15553 |
'stores the "start", "stop" and "step" values, calculating ' |
| 15554 |
'individual\n' |
| 15555 |
'items and subranges as needed).\n' |
| 15556 |
'\n' |
| 15557 |
'Range objects implement the "collections.abc.Sequence" ABC, and\n' |
| 15558 |
'provide features such as containment tests, element index ' |
| 15559 |
'lookup,\n' |
| 15560 |
'slicing and support for negative indices (see Sequence Types — ' |
| 15561 |
'list,\n' |
| 15562 |
'tuple, range):\n' |
| 15563 |
'\n' |
| 15564 |
'>>> r = range(0, 20, 2)\n' |
| 15565 |
'>>> r\n' |
| 15566 |
'range(0, 20, 2)\n' |
| 15567 |
'>>> 11 in r\n' |
| 15568 |
'False\n' |
| 15569 |
'>>> 10 in r\n' |
| 15570 |
'True\n' |
| 15571 |
'>>> r.index(10)\n' |
| 15572 |
'5\n' |
| 15573 |
'>>> r[5]\n' |
| 15574 |
'10\n' |
| 15575 |
'>>> r[:5]\n' |
| 15576 |
'range(0, 10, 2)\n' |
| 15577 |
'>>> r[-1]\n' |
| 15578 |
'18\n' |
| 15579 |
'\n' |
| 15580 |
'Testing range objects for equality with "==" and "!=" compares ' |
| 15581 |
'them as\n' |
| 15582 |
'sequences. That is, two range objects are considered equal if ' |
| 15583 |
'they\n' |
| 15584 |
'represent the same sequence of values. (Note that two range ' |
| 15585 |
'objects\n' |
| 15586 |
'that compare equal might have different "start", "stop" and ' |
| 15587 |
'"step"\n' |
| 15588 |
'attributes, for example "range(0) == range(2, 1, 3)" or ' |
| 15589 |
'"range(0, 3,\n' |
| 15590 |
'2) == range(0, 4, 2)".)\n' |
| 15591 |
'\n' |
| 15592 |
'Changed in version 3.2: Implement the Sequence ABC. Support ' |
| 15593 |
'slicing\n' |
| 15594 |
'and negative indices. Test "int" objects for membership in ' |
| 15595 |
'constant\n' |
| 15596 |
'time instead of iterating through all items.\n' |
| 15597 |
'\n' |
| 15598 |
'Changed in version 3.3: Define ‘==’ and ‘!=’ to compare range ' |
| 15599 |
'objects\n' |
| 15600 |
'based on the sequence of values they define (instead of ' |
| 15601 |
'comparing\n' |
| 15602 |
'based on object identity).Added the "start", "stop" and "step"\n' |
| 15603 |
'attributes.\n' |
| 15604 |
'\n' |
| 15605 |
'See also:\n' |
| 15606 |
'\n' |
| 15607 |
' * The linspace recipe shows how to implement a lazy version of ' |
| 15608 |
'range\n' |
| 15609 |
' suitable for floating point applications.\n', |
| 15610 |
'typesseq-mutable': 'Mutable Sequence Types\n' |
| 15611 |
'**********************\n' |
| 15612 |
'\n' |
| 15613 |
'The operations in the following table are defined on ' |
| 15614 |
'mutable sequence\n' |
| 15615 |
'types. The "collections.abc.MutableSequence" ABC is ' |
| 15616 |
'provided to make\n' |
| 15617 |
'it easier to correctly implement these operations on ' |
| 15618 |
'custom sequence\n' |
| 15619 |
'types.\n' |
| 15620 |
'\n' |
| 15621 |
'In the table *s* is an instance of a mutable sequence ' |
| 15622 |
'type, *t* is any\n' |
| 15623 |
'iterable object and *x* is an arbitrary object that ' |
| 15624 |
'meets any type and\n' |
| 15625 |
'value restrictions imposed by *s* (for example, ' |
| 15626 |
'"bytearray" only\n' |
| 15627 |
'accepts integers that meet the value restriction "0 <= x ' |
| 15628 |
'<= 255").\n' |
| 15629 |
'\n' |
| 15630 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15631 |
'| Operation | ' |
| 15632 |
'Result | Notes ' |
| 15633 |
'|\n' |
| 15634 |
'|================================|==================================|=======================|\n' |
| 15635 |
'| "s[i] = x" | item *i* of *s* is ' |
| 15636 |
'replaced by | |\n' |
| 15637 |
'| | ' |
| 15638 |
'*x* | ' |
| 15639 |
'|\n' |
| 15640 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15641 |
'| "s[i:j] = t" | slice of *s* from *i* ' |
| 15642 |
'to *j* is | |\n' |
| 15643 |
'| | replaced by the ' |
| 15644 |
'contents of the | |\n' |
| 15645 |
'| | iterable ' |
| 15646 |
'*t* | |\n' |
| 15647 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15648 |
'| "del s[i:j]" | same as "s[i:j] = ' |
| 15649 |
'[]" | |\n' |
| 15650 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15651 |
'| "s[i:j:k] = t" | the elements of ' |
| 15652 |
'"s[i:j:k]" are | (1) |\n' |
| 15653 |
'| | replaced by those of ' |
| 15654 |
'*t* | |\n' |
| 15655 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15656 |
'| "del s[i:j:k]" | removes the elements ' |
| 15657 |
'of | |\n' |
| 15658 |
'| | "s[i:j:k]" from the ' |
| 15659 |
'list | |\n' |
| 15660 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15661 |
'| "s.append(x)" | appends *x* to the ' |
| 15662 |
'end of the | |\n' |
| 15663 |
'| | sequence (same ' |
| 15664 |
'as | |\n' |
| 15665 |
'| | "s[len(s):len(s)] = ' |
| 15666 |
'[x]") | |\n' |
| 15667 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15668 |
'| "s.clear()" | removes all items ' |
| 15669 |
'from *s* (same | (5) |\n' |
| 15670 |
'| | as "del ' |
| 15671 |
's[:]") | |\n' |
| 15672 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15673 |
'| "s.copy()" | creates a shallow ' |
| 15674 |
'copy of *s* | (5) |\n' |
| 15675 |
'| | (same as ' |
| 15676 |
'"s[:]") | |\n' |
| 15677 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15678 |
'| "s.extend(t)" or "s += t" | extends *s* with the ' |
| 15679 |
'contents of | |\n' |
| 15680 |
'| | *t* (for the most ' |
| 15681 |
'part the same | |\n' |
| 15682 |
'| | as "s[len(s):len(s)] ' |
| 15683 |
'= t") | |\n' |
| 15684 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15685 |
'| "s *= n" | updates *s* with its ' |
| 15686 |
'contents | (6) |\n' |
| 15687 |
'| | repeated *n* ' |
| 15688 |
'times | |\n' |
| 15689 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15690 |
'| "s.insert(i, x)" | inserts *x* into *s* ' |
| 15691 |
'at the | |\n' |
| 15692 |
'| | index given by *i* ' |
| 15693 |
'(same as | |\n' |
| 15694 |
'| | "s[i:i] = ' |
| 15695 |
'[x]") | |\n' |
| 15696 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15697 |
'| "s.pop()" or "s.pop(i)" | retrieves the item at ' |
| 15698 |
'*i* and | (2) |\n' |
| 15699 |
'| | also removes it from ' |
| 15700 |
'*s* | |\n' |
| 15701 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15702 |
'| "s.remove(x)" | remove the first item ' |
| 15703 |
'from *s* | (3) |\n' |
| 15704 |
'| | where "s[i]" is equal ' |
| 15705 |
'to *x* | |\n' |
| 15706 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15707 |
'| "s.reverse()" | reverses the items of ' |
| 15708 |
'*s* in | (4) |\n' |
| 15709 |
'| | ' |
| 15710 |
'place | ' |
| 15711 |
'|\n' |
| 15712 |
'+--------------------------------+----------------------------------+-----------------------+\n' |
| 15713 |
'\n' |
| 15714 |
'Notes:\n' |
| 15715 |
'\n' |
| 15716 |
'1. *t* must have the same length as the slice it is ' |
| 15717 |
'replacing.\n' |
| 15718 |
'\n' |
| 15719 |
'2. The optional argument *i* defaults to "-1", so that ' |
| 15720 |
'by default the\n' |
| 15721 |
' last item is removed and returned.\n' |
| 15722 |
'\n' |
| 15723 |
'3. "remove()" raises "ValueError" when *x* is not found ' |
| 15724 |
'in *s*.\n' |
| 15725 |
'\n' |
| 15726 |
'4. The "reverse()" method modifies the sequence in place ' |
| 15727 |
'for economy\n' |
| 15728 |
' of space when reversing a large sequence. To remind ' |
| 15729 |
'users that it\n' |
| 15730 |
' operates by side effect, it does not return the ' |
| 15731 |
'reversed sequence.\n' |
| 15732 |
'\n' |
| 15733 |
'5. "clear()" and "copy()" are included for consistency ' |
| 15734 |
'with the\n' |
| 15735 |
' interfaces of mutable containers that don’t support ' |
| 15736 |
'slicing\n' |
| 15737 |
' operations (such as "dict" and "set"). "copy()" is ' |
| 15738 |
'not part of the\n' |
| 15739 |
' "collections.abc.MutableSequence" ABC, but most ' |
| 15740 |
'concrete mutable\n' |
| 15741 |
' sequence classes provide it.\n' |
| 15742 |
'\n' |
| 15743 |
' New in version 3.3: "clear()" and "copy()" methods.\n' |
| 15744 |
'\n' |
| 15745 |
'6. The value *n* is an integer, or an object ' |
| 15746 |
'implementing\n' |
| 15747 |
' "__index__()". Zero and negative values of *n* clear ' |
| 15748 |
'the sequence.\n' |
| 15749 |
' Items in the sequence are not copied; they are ' |
| 15750 |
'referenced multiple\n' |
| 15751 |
' times, as explained for "s * n" under Common Sequence ' |
| 15752 |
'Operations.\n', |
| 15753 |
'unary': 'Unary arithmetic and bitwise operations\n' |
| 15754 |
'***************************************\n' |
| 15755 |
'\n' |
| 15756 |
'All unary arithmetic and bitwise operations have the same ' |
| 15757 |
'priority:\n' |
| 15758 |
'\n' |
| 15759 |
' u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr\n' |
| 15760 |
'\n' |
| 15761 |
'The unary "-" (minus) operator yields the negation of its numeric\n' |
| 15762 |
'argument; the operation can be overridden with the "__neg__()" ' |
| 15763 |
'special\n' |
| 15764 |
'method.\n' |
| 15765 |
'\n' |
| 15766 |
'The unary "+" (plus) operator yields its numeric argument ' |
| 15767 |
'unchanged;\n' |
| 15768 |
'the operation can be overridden with the "__pos__()" special ' |
| 15769 |
'method.\n' |
| 15770 |
'\n' |
| 15771 |
'The unary "~" (invert) operator yields the bitwise inversion of ' |
| 15772 |
'its\n' |
| 15773 |
'integer argument. The bitwise inversion of "x" is defined as\n' |
| 15774 |
'"-(x+1)". It only applies to integral numbers or to custom ' |
| 15775 |
'objects\n' |
| 15776 |
'that override the "__invert__()" special method.\n' |
| 15777 |
'\n' |
| 15778 |
'In all three cases, if the argument does not have the proper type, ' |
| 15779 |
'a\n' |
| 15780 |
'"TypeError" exception is raised.\n', |
| 15781 |
'while': 'The "while" statement\n' |
| 15782 |
'*********************\n' |
| 15783 |
'\n' |
| 15784 |
'The "while" statement is used for repeated execution as long as an\n' |
| 15785 |
'expression is true:\n' |
| 15786 |
'\n' |
| 15787 |
' while_stmt ::= "while" assignment_expression ":" suite\n' |
| 15788 |
' ["else" ":" suite]\n' |
| 15789 |
'\n' |
| 15790 |
'This repeatedly tests the expression and, if it is true, executes ' |
| 15791 |
'the\n' |
| 15792 |
'first suite; if the expression is false (which may be the first ' |
| 15793 |
'time\n' |
| 15794 |
'it is tested) the suite of the "else" clause, if present, is ' |
| 15795 |
'executed\n' |
| 15796 |
'and the loop terminates.\n' |
| 15797 |
'\n' |
| 15798 |
'A "break" statement executed in the first suite terminates the ' |
| 15799 |
'loop\n' |
| 15800 |
'without executing the "else" clause’s suite. A "continue" ' |
| 15801 |
'statement\n' |
| 15802 |
'executed in the first suite skips the rest of the suite and goes ' |
| 15803 |
'back\n' |
| 15804 |
'to testing the expression.\n', |
| 15805 |
'with': 'The "with" statement\n' |
| 15806 |
'********************\n' |
| 15807 |
'\n' |
| 15808 |
'The "with" statement is used to wrap the execution of a block with\n' |
| 15809 |
'methods defined by a context manager (see section With Statement\n' |
| 15810 |
'Context Managers). This allows common "try"…"except"…"finally" ' |
| 15811 |
'usage\n' |
| 15812 |
'patterns to be encapsulated for convenient reuse.\n' |
| 15813 |
'\n' |
| 15814 |
' with_stmt ::= "with" ( "(" with_stmt_contents ","? ")" | ' |
| 15815 |
'with_stmt_contents ) ":" suite\n' |
| 15816 |
' with_stmt_contents ::= with_item ("," with_item)*\n' |
| 15817 |
' with_item ::= expression ["as" target]\n' |
| 15818 |
'\n' |
| 15819 |
'The execution of the "with" statement with one “item” proceeds as\n' |
| 15820 |
'follows:\n' |
| 15821 |
'\n' |
| 15822 |
'1. The context expression (the expression given in the "with_item") ' |
| 15823 |
'is\n' |
| 15824 |
' evaluated to obtain a context manager.\n' |
| 15825 |
'\n' |
| 15826 |
'2. The context manager’s "__enter__()" is loaded for later use.\n' |
| 15827 |
'\n' |
| 15828 |
'3. The context manager’s "__exit__()" is loaded for later use.\n' |
| 15829 |
'\n' |
| 15830 |
'4. The context manager’s "__enter__()" method is invoked.\n' |
| 15831 |
'\n' |
| 15832 |
'5. If a target was included in the "with" statement, the return ' |
| 15833 |
'value\n' |
| 15834 |
' from "__enter__()" is assigned to it.\n' |
| 15835 |
'\n' |
| 15836 |
' Note:\n' |
| 15837 |
'\n' |
| 15838 |
' The "with" statement guarantees that if the "__enter__()" ' |
| 15839 |
'method\n' |
| 15840 |
' returns without an error, then "__exit__()" will always be\n' |
| 15841 |
' called. Thus, if an error occurs during the assignment to the\n' |
| 15842 |
' target list, it will be treated the same as an error occurring\n' |
| 15843 |
' within the suite would be. See step 7 below.\n' |
| 15844 |
'\n' |
| 15845 |
'6. The suite is executed.\n' |
| 15846 |
'\n' |
| 15847 |
'7. The context manager’s "__exit__()" method is invoked. If an\n' |
| 15848 |
' exception caused the suite to be exited, its type, value, and\n' |
| 15849 |
' traceback are passed as arguments to "__exit__()". Otherwise, ' |
| 15850 |
'three\n' |
| 15851 |
' "None" arguments are supplied.\n' |
| 15852 |
'\n' |
| 15853 |
' If the suite was exited due to an exception, and the return ' |
| 15854 |
'value\n' |
| 15855 |
' from the "__exit__()" method was false, the exception is ' |
| 15856 |
'reraised.\n' |
| 15857 |
' If the return value was true, the exception is suppressed, and\n' |
| 15858 |
' execution continues with the statement following the "with"\n' |
| 15859 |
' statement.\n' |
| 15860 |
'\n' |
| 15861 |
' If the suite was exited for any reason other than an exception, ' |
| 15862 |
'the\n' |
| 15863 |
' return value from "__exit__()" is ignored, and execution ' |
| 15864 |
'proceeds\n' |
| 15865 |
' at the normal location for the kind of exit that was taken.\n' |
| 15866 |
'\n' |
| 15867 |
'The following code:\n' |
| 15868 |
'\n' |
| 15869 |
' with EXPRESSION as TARGET:\n' |
| 15870 |
' SUITE\n' |
| 15871 |
'\n' |
| 15872 |
'is semantically equivalent to:\n' |
| 15873 |
'\n' |
| 15874 |
' manager = (EXPRESSION)\n' |
| 15875 |
' enter = type(manager).__enter__\n' |
| 15876 |
' exit = type(manager).__exit__\n' |
| 15877 |
' value = enter(manager)\n' |
| 15878 |
' hit_except = False\n' |
| 15879 |
'\n' |
| 15880 |
' try:\n' |
| 15881 |
' TARGET = value\n' |
| 15882 |
' SUITE\n' |
| 15883 |
' except:\n' |
| 15884 |
' hit_except = True\n' |
| 15885 |
' if not exit(manager, *sys.exc_info()):\n' |
| 15886 |
' raise\n' |
| 15887 |
' finally:\n' |
| 15888 |
' if not hit_except:\n' |
| 15889 |
' exit(manager, None, None, None)\n' |
| 15890 |
'\n' |
| 15891 |
'With more than one item, the context managers are processed as if\n' |
| 15892 |
'multiple "with" statements were nested:\n' |
| 15893 |
'\n' |
| 15894 |
' with A() as a, B() as b:\n' |
| 15895 |
' SUITE\n' |
| 15896 |
'\n' |
| 15897 |
'is semantically equivalent to:\n' |
| 15898 |
'\n' |
| 15899 |
' with A() as a:\n' |
| 15900 |
' with B() as b:\n' |
| 15901 |
' SUITE\n' |
| 15902 |
'\n' |
| 15903 |
'You can also write multi-item context managers in multiple lines if\n' |
| 15904 |
'the items are surrounded by parentheses. For example:\n' |
| 15905 |
'\n' |
| 15906 |
' with (\n' |
| 15907 |
' A() as a,\n' |
| 15908 |
' B() as b,\n' |
| 15909 |
' ):\n' |
| 15910 |
' SUITE\n' |
| 15911 |
'\n' |
| 15912 |
'Changed in version 3.1: Support for multiple context expressions.\n' |
| 15913 |
'\n' |
| 15914 |
'Changed in version 3.10: Support for using grouping parentheses to\n' |
| 15915 |
'break the statement in multiple lines.\n' |
| 15916 |
'\n' |
| 15917 |
'See also:\n' |
| 15918 |
'\n' |
| 15919 |
' **PEP 343** - The “with” statement\n' |
| 15920 |
' The specification, background, and examples for the Python ' |
| 15921 |
'"with"\n' |
| 15922 |
' statement.\n', |
| 15923 |
'yield': 'The "yield" statement\n' |
| 15924 |
'*********************\n' |
| 15925 |
'\n' |
| 15926 |
' yield_stmt ::= yield_expression\n' |
| 15927 |
'\n' |
| 15928 |
'A "yield" statement is semantically equivalent to a yield ' |
| 15929 |
'expression.\n' |
| 15930 |
'The yield statement can be used to omit the parentheses that would\n' |
| 15931 |
'otherwise be required in the equivalent yield expression ' |
| 15932 |
'statement.\n' |
| 15933 |
'For example, the yield statements\n' |
| 15934 |
'\n' |
| 15935 |
' yield <expr>\n' |
| 15936 |
' yield from <expr>\n' |
| 15937 |
'\n' |
| 15938 |
'are equivalent to the yield expression statements\n' |
| 15939 |
'\n' |
| 15940 |
' (yield <expr>)\n' |
| 15941 |
' (yield from <expr>)\n' |
| 15942 |
'\n' |
| 15943 |
'Yield expressions and statements are only used when defining a\n' |
| 15944 |
'*generator* function, and are only used in the body of the ' |
| 15945 |
'generator\n' |
| 15946 |
'function. Using yield in a function definition is sufficient to ' |
| 15947 |
'cause\n' |
| 15948 |
'that definition to create a generator function instead of a normal\n' |
| 15949 |
'function.\n' |
| 15950 |
'\n' |
| 15951 |
'For full details of "yield" semantics, refer to the Yield ' |
| 15952 |
'expressions\n' |
| 15953 |
'section.\n'} |