Georg Brandl | 0f25cea | 2012-03-04 16:12:09 +0100 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Łukasz Langa | 439c93d | 2020-08-11 19:14:36 +0200 | [diff] [blame] | 2 | # Autogenerated by Sphinx on Tue Aug 11 19:08:56 2020 |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3 | topics = {'assert': 'The "assert" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4 | '**********************\n' |
| 5 | '\n' |
| 6 | 'Assert statements are a convenient way to insert debugging ' |
| 7 | 'assertions\n' |
| 8 | 'into a program:\n' |
| 9 | '\n' |
| 10 | ' assert_stmt ::= "assert" expression ["," expression]\n' |
| 11 | '\n' |
| 12 | 'The simple form, "assert expression", is equivalent to\n' |
| 13 | '\n' |
| 14 | ' if __debug__:\n' |
| 15 | ' if not expression: raise AssertionError\n' |
| 16 | '\n' |
| 17 | 'The extended form, "assert expression1, expression2", is ' |
| 18 | 'equivalent to\n' |
| 19 | '\n' |
| 20 | ' if __debug__:\n' |
| 21 | ' if not expression1: raise AssertionError(expression2)\n' |
| 22 | '\n' |
| 23 | 'These equivalences assume that "__debug__" and "AssertionError" ' |
| 24 | 'refer\n' |
| 25 | 'to the built-in variables with those names. In the current\n' |
| 26 | 'implementation, the built-in variable "__debug__" is "True" under\n' |
| 27 | 'normal circumstances, "False" when optimization is requested ' |
| 28 | '(command\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 29 | 'line option "-O"). The current code generator emits no code for ' |
| 30 | 'an\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 31 | 'assert statement when optimization is requested at compile time. ' |
| 32 | 'Note\n' |
| 33 | 'that it is unnecessary to include the source code for the ' |
| 34 | 'expression\n' |
| 35 | 'that failed in the error message; it will be displayed as part of ' |
| 36 | 'the\n' |
| 37 | 'stack trace.\n' |
| 38 | '\n' |
| 39 | 'Assignments to "__debug__" are illegal. The value for the ' |
| 40 | 'built-in\n' |
| 41 | 'variable is determined when the interpreter starts.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 42 | 'assignment': 'Assignment statements\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 43 | '*********************\n' |
| 44 | '\n' |
| 45 | 'Assignment statements are used to (re)bind names to values and ' |
| 46 | 'to\n' |
| 47 | 'modify attributes or items of mutable objects:\n' |
| 48 | '\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 49 | ' assignment_stmt ::= (target_list "=")+ (starred_expression ' |
| 50 | '| yield_expression)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 51 | ' target_list ::= target ("," target)* [","]\n' |
| 52 | ' target ::= identifier\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 53 | ' | "(" [target_list] ")"\n' |
| 54 | ' | "[" [target_list] "]"\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 55 | ' | attributeref\n' |
| 56 | ' | subscription\n' |
| 57 | ' | slicing\n' |
| 58 | ' | "*" target\n' |
| 59 | '\n' |
| 60 | '(See section Primaries for the syntax definitions for ' |
| 61 | '*attributeref*,\n' |
| 62 | '*subscription*, and *slicing*.)\n' |
| 63 | '\n' |
| 64 | 'An assignment statement evaluates the expression list ' |
| 65 | '(remember that\n' |
| 66 | 'this can be a single expression or a comma-separated list, the ' |
| 67 | 'latter\n' |
| 68 | 'yielding a tuple) and assigns the single resulting object to ' |
| 69 | 'each of\n' |
| 70 | 'the target lists, from left to right.\n' |
| 71 | '\n' |
| 72 | 'Assignment is defined recursively depending on the form of the ' |
| 73 | 'target\n' |
| 74 | '(list). When a target is part of a mutable object (an ' |
| 75 | 'attribute\n' |
| 76 | 'reference, subscription or slicing), the mutable object must\n' |
| 77 | 'ultimately perform the assignment and decide about its ' |
| 78 | 'validity, and\n' |
| 79 | 'may raise an exception if the assignment is unacceptable. The ' |
| 80 | 'rules\n' |
| 81 | 'observed by various types and the exceptions raised are given ' |
| 82 | 'with the\n' |
| 83 | 'definition of the object types (see section The standard type\n' |
| 84 | 'hierarchy).\n' |
| 85 | '\n' |
| 86 | 'Assignment of an object to a target list, optionally enclosed ' |
| 87 | 'in\n' |
| 88 | 'parentheses or square brackets, is recursively defined as ' |
| 89 | 'follows.\n' |
| 90 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 91 | '* If the target list is a single target with no trailing ' |
| 92 | 'comma,\n' |
| 93 | ' optionally in parentheses, the object is assigned to that ' |
| 94 | 'target.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 95 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 96 | '* Else: The object must be an iterable with the same number of ' |
| 97 | 'items\n' |
| 98 | ' as there are targets in the target list, and the items are ' |
| 99 | 'assigned,\n' |
| 100 | ' from left to right, to the corresponding targets.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 101 | '\n' |
| 102 | ' * If the target list contains one target prefixed with an\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 103 | ' asterisk, called a “starred” target: The object must be ' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 104 | 'an\n' |
| 105 | ' iterable with at least as many items as there are targets ' |
| 106 | 'in the\n' |
| 107 | ' target list, minus one. The first items of the iterable ' |
| 108 | 'are\n' |
| 109 | ' assigned, from left to right, to the targets before the ' |
| 110 | 'starred\n' |
| 111 | ' target. The final items of the iterable are assigned to ' |
| 112 | 'the\n' |
| 113 | ' targets after the starred target. A list of the remaining ' |
| 114 | 'items\n' |
| 115 | ' in the iterable is then assigned to the starred target ' |
| 116 | '(the list\n' |
| 117 | ' can be empty).\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 118 | '\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 119 | ' * Else: The object must be an iterable with the same number ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 120 | 'of\n' |
| 121 | ' items as there are targets in the target list, and the ' |
| 122 | 'items are\n' |
| 123 | ' assigned, from left to right, to the corresponding ' |
| 124 | 'targets.\n' |
| 125 | '\n' |
| 126 | 'Assignment of an object to a single target is recursively ' |
| 127 | 'defined as\n' |
| 128 | 'follows.\n' |
| 129 | '\n' |
| 130 | '* If the target is an identifier (name):\n' |
| 131 | '\n' |
| 132 | ' * If the name does not occur in a "global" or "nonlocal" ' |
| 133 | 'statement\n' |
| 134 | ' in the current code block: the name is bound to the object ' |
| 135 | 'in the\n' |
| 136 | ' current local namespace.\n' |
| 137 | '\n' |
| 138 | ' * Otherwise: the name is bound to the object in the global\n' |
| 139 | ' namespace or the outer namespace determined by ' |
| 140 | '"nonlocal",\n' |
| 141 | ' respectively.\n' |
| 142 | '\n' |
| 143 | ' The name is rebound if it was already bound. This may cause ' |
| 144 | 'the\n' |
| 145 | ' reference count for the object previously bound to the name ' |
| 146 | 'to reach\n' |
| 147 | ' zero, causing the object to be deallocated and its ' |
| 148 | 'destructor (if it\n' |
| 149 | ' has one) to be called.\n' |
| 150 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 151 | '* If the target is an attribute reference: The primary ' |
| 152 | 'expression in\n' |
| 153 | ' the reference is evaluated. It should yield an object with\n' |
| 154 | ' assignable attributes; if this is not the case, "TypeError" ' |
| 155 | 'is\n' |
| 156 | ' raised. That object is then asked to assign the assigned ' |
| 157 | 'object to\n' |
| 158 | ' the given attribute; if it cannot perform the assignment, it ' |
| 159 | 'raises\n' |
| 160 | ' an exception (usually but not necessarily ' |
| 161 | '"AttributeError").\n' |
| 162 | '\n' |
| 163 | ' Note: If the object is a class instance and the attribute ' |
| 164 | 'reference\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 165 | ' occurs on both sides of the assignment operator, the ' |
| 166 | 'right-hand side\n' |
| 167 | ' expression, "a.x" can access either an instance attribute or ' |
| 168 | '(if no\n' |
| 169 | ' instance attribute exists) a class attribute. The left-hand ' |
| 170 | 'side\n' |
| 171 | ' target "a.x" is always set as an instance attribute, ' |
| 172 | 'creating it if\n' |
| 173 | ' necessary. Thus, the two occurrences of "a.x" do not ' |
| 174 | 'necessarily\n' |
| 175 | ' refer to the same attribute: if the right-hand side ' |
| 176 | 'expression\n' |
| 177 | ' refers to a class attribute, the left-hand side creates a ' |
| 178 | 'new\n' |
| 179 | ' instance attribute as the target of the assignment:\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 180 | '\n' |
| 181 | ' class Cls:\n' |
| 182 | ' x = 3 # class variable\n' |
| 183 | ' inst = Cls()\n' |
| 184 | ' inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x ' |
| 185 | 'as 3\n' |
| 186 | '\n' |
| 187 | ' This description does not necessarily apply to descriptor\n' |
| 188 | ' attributes, such as properties created with "property()".\n' |
| 189 | '\n' |
| 190 | '* If the target is a subscription: The primary expression in ' |
| 191 | 'the\n' |
| 192 | ' reference is evaluated. It should yield either a mutable ' |
| 193 | 'sequence\n' |
| 194 | ' object (such as a list) or a mapping object (such as a ' |
| 195 | 'dictionary).\n' |
| 196 | ' Next, the subscript expression is evaluated.\n' |
| 197 | '\n' |
| 198 | ' If the primary is a mutable sequence object (such as a ' |
| 199 | 'list), the\n' |
| 200 | ' subscript must yield an integer. If it is negative, the ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 201 | 'sequence’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 202 | ' length is added to it. The resulting value must be a ' |
| 203 | 'nonnegative\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 204 | ' integer less than the sequence’s length, and the sequence is ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 205 | 'asked\n' |
| 206 | ' to assign the assigned object to its item with that index. ' |
| 207 | 'If the\n' |
| 208 | ' index is out of range, "IndexError" is raised (assignment to ' |
| 209 | 'a\n' |
| 210 | ' subscripted sequence cannot add new items to a list).\n' |
| 211 | '\n' |
| 212 | ' If the primary is a mapping object (such as a dictionary), ' |
| 213 | 'the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 214 | ' subscript must have a type compatible with the mapping’s key ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 215 | 'type,\n' |
| 216 | ' and the mapping is then asked to create a key/datum pair ' |
| 217 | 'which maps\n' |
| 218 | ' the subscript to the assigned object. This can either ' |
| 219 | 'replace an\n' |
| 220 | ' existing key/value pair with the same key value, or insert a ' |
| 221 | 'new\n' |
| 222 | ' key/value pair (if no key with the same value existed).\n' |
| 223 | '\n' |
| 224 | ' For user-defined objects, the "__setitem__()" method is ' |
| 225 | 'called with\n' |
| 226 | ' appropriate arguments.\n' |
| 227 | '\n' |
| 228 | '* If the target is a slicing: The primary expression in the\n' |
| 229 | ' reference is evaluated. It should yield a mutable sequence ' |
| 230 | 'object\n' |
| 231 | ' (such as a list). The assigned object should be a sequence ' |
| 232 | 'object\n' |
| 233 | ' of the same type. Next, the lower and upper bound ' |
| 234 | 'expressions are\n' |
| 235 | ' evaluated, insofar they are present; defaults are zero and ' |
| 236 | 'the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 237 | ' sequence’s length. The bounds should evaluate to integers. ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 238 | 'If\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 239 | ' either bound is negative, the sequence’s length is added to ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 240 | 'it. The\n' |
| 241 | ' resulting bounds are clipped to lie between zero and the ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 242 | 'sequence’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 243 | ' length, inclusive. Finally, the sequence object is asked to ' |
| 244 | 'replace\n' |
| 245 | ' the slice with the items of the assigned sequence. The ' |
| 246 | 'length of\n' |
| 247 | ' the slice may be different from the length of the assigned ' |
| 248 | 'sequence,\n' |
| 249 | ' thus changing the length of the target sequence, if the ' |
| 250 | 'target\n' |
| 251 | ' sequence allows it.\n' |
| 252 | '\n' |
| 253 | '**CPython implementation detail:** In the current ' |
| 254 | 'implementation, the\n' |
| 255 | 'syntax for targets is taken to be the same as for expressions, ' |
| 256 | 'and\n' |
| 257 | 'invalid syntax is rejected during the code generation phase, ' |
| 258 | 'causing\n' |
| 259 | 'less detailed error messages.\n' |
| 260 | '\n' |
| 261 | 'Although the definition of assignment implies that overlaps ' |
| 262 | 'between\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 263 | 'the left-hand side and the right-hand side are ‘simultaneous’ ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 264 | '(for\n' |
| 265 | 'example "a, b = b, a" swaps two variables), overlaps *within* ' |
| 266 | 'the\n' |
| 267 | 'collection of assigned-to variables occur left-to-right, ' |
| 268 | 'sometimes\n' |
| 269 | 'resulting in confusion. For instance, the following program ' |
| 270 | 'prints\n' |
| 271 | '"[0, 2]":\n' |
| 272 | '\n' |
| 273 | ' x = [0, 1]\n' |
| 274 | ' i = 0\n' |
| 275 | ' i, x[i] = 1, 2 # i is updated, then x[i] is ' |
| 276 | 'updated\n' |
| 277 | ' print(x)\n' |
| 278 | '\n' |
| 279 | 'See also:\n' |
| 280 | '\n' |
| 281 | ' **PEP 3132** - Extended Iterable Unpacking\n' |
| 282 | ' The specification for the "*target" feature.\n' |
| 283 | '\n' |
| 284 | '\n' |
| 285 | 'Augmented assignment statements\n' |
| 286 | '===============================\n' |
| 287 | '\n' |
| 288 | 'Augmented assignment is the combination, in a single ' |
| 289 | 'statement, of a\n' |
| 290 | 'binary operation and an assignment statement:\n' |
| 291 | '\n' |
| 292 | ' augmented_assignment_stmt ::= augtarget augop ' |
| 293 | '(expression_list | yield_expression)\n' |
| 294 | ' augtarget ::= identifier | attributeref | ' |
| 295 | 'subscription | slicing\n' |
| 296 | ' augop ::= "+=" | "-=" | "*=" | "@=" | ' |
| 297 | '"/=" | "//=" | "%=" | "**="\n' |
| 298 | ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n' |
| 299 | '\n' |
| 300 | '(See section Primaries for the syntax definitions of the last ' |
| 301 | 'three\n' |
| 302 | 'symbols.)\n' |
| 303 | '\n' |
| 304 | 'An augmented assignment evaluates the target (which, unlike ' |
| 305 | 'normal\n' |
| 306 | 'assignment statements, cannot be an unpacking) and the ' |
| 307 | 'expression\n' |
| 308 | 'list, performs the binary operation specific to the type of ' |
| 309 | 'assignment\n' |
| 310 | 'on the two operands, and assigns the result to the original ' |
| 311 | 'target.\n' |
| 312 | 'The target is only evaluated once.\n' |
| 313 | '\n' |
| 314 | 'An augmented assignment expression like "x += 1" can be ' |
| 315 | 'rewritten as\n' |
| 316 | '"x = x + 1" to achieve a similar, but not exactly equal ' |
| 317 | 'effect. In the\n' |
| 318 | 'augmented version, "x" is only evaluated once. Also, when ' |
| 319 | 'possible,\n' |
| 320 | 'the actual operation is performed *in-place*, meaning that ' |
| 321 | 'rather than\n' |
| 322 | 'creating a new object and assigning that to the target, the ' |
| 323 | 'old object\n' |
| 324 | 'is modified instead.\n' |
| 325 | '\n' |
| 326 | 'Unlike normal assignments, augmented assignments evaluate the ' |
| 327 | 'left-\n' |
| 328 | 'hand side *before* evaluating the right-hand side. For ' |
| 329 | 'example, "a[i]\n' |
| 330 | '+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and ' |
| 331 | 'performs\n' |
| 332 | 'the addition, and lastly, it writes the result back to ' |
| 333 | '"a[i]".\n' |
| 334 | '\n' |
| 335 | 'With the exception of assigning to tuples and multiple targets ' |
| 336 | 'in a\n' |
| 337 | 'single statement, the assignment done by augmented assignment\n' |
| 338 | 'statements is handled the same way as normal assignments. ' |
| 339 | 'Similarly,\n' |
| 340 | 'with the exception of the possible *in-place* behavior, the ' |
| 341 | 'binary\n' |
| 342 | 'operation performed by augmented assignment is the same as the ' |
| 343 | 'normal\n' |
| 344 | 'binary operations.\n' |
| 345 | '\n' |
| 346 | 'For targets which are attribute references, the same caveat ' |
| 347 | 'about\n' |
| 348 | 'class and instance attributes applies as for regular ' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 349 | 'assignments.\n' |
| 350 | '\n' |
| 351 | '\n' |
| 352 | 'Annotated assignment statements\n' |
| 353 | '===============================\n' |
| 354 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 355 | '*Annotation* assignment is the combination, in a single ' |
| 356 | 'statement, of\n' |
| 357 | 'a variable or attribute annotation and an optional assignment\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 358 | 'statement:\n' |
| 359 | '\n' |
Łukasz Langa | 3b5deb0 | 2019-06-04 19:44:34 +0200 | [diff] [blame] | 360 | ' annotated_assignment_stmt ::= augtarget ":" expression\n' |
| 361 | ' ["=" (starred_expression | ' |
| 362 | 'yield_expression)]\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 363 | '\n' |
| 364 | 'The difference from normal Assignment statements is that only ' |
| 365 | 'single\n' |
Łukasz Langa | 3b5deb0 | 2019-06-04 19:44:34 +0200 | [diff] [blame] | 366 | 'target is allowed.\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 367 | '\n' |
| 368 | 'For simple names as assignment targets, if in class or module ' |
| 369 | 'scope,\n' |
| 370 | 'the annotations are evaluated and stored in a special class or ' |
| 371 | 'module\n' |
| 372 | 'attribute "__annotations__" that is a dictionary mapping from ' |
| 373 | 'variable\n' |
| 374 | 'names (mangled if private) to evaluated annotations. This ' |
| 375 | 'attribute is\n' |
| 376 | 'writable and is automatically created at the start of class or ' |
| 377 | 'module\n' |
| 378 | 'body execution, if annotations are found statically.\n' |
| 379 | '\n' |
| 380 | 'For expressions as assignment targets, the annotations are ' |
| 381 | 'evaluated\n' |
| 382 | 'if in class or module scope, but not stored.\n' |
| 383 | '\n' |
| 384 | 'If a name is annotated in a function scope, then this name is ' |
| 385 | 'local\n' |
| 386 | 'for that scope. Annotations are never evaluated and stored in ' |
| 387 | 'function\n' |
| 388 | 'scopes.\n' |
| 389 | '\n' |
| 390 | 'If the right hand side is present, an annotated assignment ' |
| 391 | 'performs\n' |
| 392 | 'the actual assignment before evaluating annotations (where\n' |
| 393 | 'applicable). If the right hand side is not present for an ' |
| 394 | 'expression\n' |
| 395 | 'target, then the interpreter evaluates the target except for ' |
| 396 | 'the last\n' |
| 397 | '"__setitem__()" or "__setattr__()" call.\n' |
| 398 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 399 | 'See also:\n' |
| 400 | '\n' |
| 401 | ' **PEP 526** - Syntax for Variable Annotations\n' |
| 402 | ' The proposal that added syntax for annotating the types ' |
| 403 | 'of\n' |
| 404 | ' variables (including class variables and instance ' |
| 405 | 'variables),\n' |
| 406 | ' instead of expressing them through comments.\n' |
| 407 | '\n' |
| 408 | ' **PEP 484** - Type hints\n' |
| 409 | ' The proposal that added the "typing" module to provide a ' |
| 410 | 'standard\n' |
| 411 | ' syntax for type annotations that can be used in static ' |
| 412 | 'analysis\n' |
Łukasz Langa | 3b5deb0 | 2019-06-04 19:44:34 +0200 | [diff] [blame] | 413 | ' tools and IDEs.\n' |
| 414 | '\n' |
| 415 | 'Changed in version 3.8: Now annotated assignments allow same\n' |
| 416 | 'expressions in the right hand side as the regular ' |
| 417 | 'assignments.\n' |
| 418 | 'Previously, some expressions (like un-parenthesized tuple ' |
| 419 | 'expressions)\n' |
| 420 | 'caused a syntax error.\n', |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 421 | 'async': 'Coroutines\n' |
| 422 | '**********\n' |
| 423 | '\n' |
| 424 | 'New in version 3.5.\n' |
| 425 | '\n' |
| 426 | '\n' |
| 427 | 'Coroutine function definition\n' |
| 428 | '=============================\n' |
| 429 | '\n' |
| 430 | ' async_funcdef ::= [decorators] "async" "def" funcname "(" ' |
| 431 | '[parameter_list] ")"\n' |
| 432 | ' ["->" expression] ":" suite\n' |
| 433 | '\n' |
| 434 | 'Execution of Python coroutines can be suspended and resumed at ' |
| 435 | 'many\n' |
| 436 | 'points (see *coroutine*). Inside the body of a coroutine ' |
| 437 | 'function,\n' |
| 438 | '"await" and "async" identifiers become reserved keywords; "await"\n' |
| 439 | 'expressions, "async for" and "async with" can only be used in\n' |
| 440 | 'coroutine function bodies.\n' |
| 441 | '\n' |
| 442 | 'Functions defined with "async def" syntax are always coroutine\n' |
| 443 | 'functions, even if they do not contain "await" or "async" ' |
| 444 | 'keywords.\n' |
| 445 | '\n' |
| 446 | 'It is a "SyntaxError" to use a "yield from" expression inside the ' |
| 447 | 'body\n' |
| 448 | 'of a coroutine function.\n' |
| 449 | '\n' |
| 450 | 'An example of a coroutine function:\n' |
| 451 | '\n' |
| 452 | ' async def func(param1, param2):\n' |
| 453 | ' do_stuff()\n' |
| 454 | ' await some_coroutine()\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* is able to call asynchronous code in ' |
| 463 | 'its\n' |
| 464 | '*iter* implementation, and *asynchronous iterator* can call\n' |
| 465 | 'asynchronous code in its *next* method.\n' |
| 466 | '\n' |
| 467 | 'The "async for" statement allows convenient iteration over\n' |
| 468 | 'asynchronous iterators.\n' |
| 469 | '\n' |
| 470 | 'The following code:\n' |
| 471 | '\n' |
| 472 | ' async for TARGET in ITER:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 473 | ' SUITE\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 474 | ' else:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 475 | ' SUITE2\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 476 | '\n' |
| 477 | 'Is semantically equivalent to:\n' |
| 478 | '\n' |
| 479 | ' iter = (ITER)\n' |
| 480 | ' iter = type(iter).__aiter__(iter)\n' |
| 481 | ' running = True\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 482 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 483 | ' while running:\n' |
| 484 | ' try:\n' |
| 485 | ' TARGET = await type(iter).__anext__(iter)\n' |
| 486 | ' except StopAsyncIteration:\n' |
| 487 | ' running = False\n' |
| 488 | ' else:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 489 | ' SUITE\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 490 | ' else:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 491 | ' SUITE2\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 492 | '\n' |
| 493 | 'See also "__aiter__()" and "__anext__()" for details.\n' |
| 494 | '\n' |
| 495 | 'It is a "SyntaxError" to use an "async for" statement outside the ' |
| 496 | 'body\n' |
| 497 | 'of a coroutine function.\n' |
| 498 | '\n' |
| 499 | '\n' |
| 500 | 'The "async with" statement\n' |
| 501 | '==========================\n' |
| 502 | '\n' |
| 503 | ' async_with_stmt ::= "async" with_stmt\n' |
| 504 | '\n' |
| 505 | 'An *asynchronous context manager* is a *context manager* that is ' |
| 506 | 'able\n' |
| 507 | 'to suspend execution in its *enter* and *exit* methods.\n' |
| 508 | '\n' |
| 509 | 'The following code:\n' |
| 510 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 511 | ' async with EXPRESSION as TARGET:\n' |
| 512 | ' SUITE\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 513 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 514 | 'is semantically equivalent to:\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 515 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 516 | ' manager = (EXPRESSION)\n' |
| 517 | ' aenter = type(manager).__aenter__\n' |
| 518 | ' aexit = type(manager).__aexit__\n' |
| 519 | ' value = await aenter(manager)\n' |
| 520 | ' hit_except = False\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 521 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 522 | ' try:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 523 | ' TARGET = value\n' |
| 524 | ' SUITE\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 525 | ' except:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 526 | ' hit_except = True\n' |
| 527 | ' if not await aexit(manager, *sys.exc_info()):\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 528 | ' raise\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 529 | ' finally:\n' |
| 530 | ' if not hit_except:\n' |
| 531 | ' await aexit(manager, None, None, None)\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 532 | '\n' |
| 533 | 'See also "__aenter__()" and "__aexit__()" for details.\n' |
| 534 | '\n' |
| 535 | 'It is a "SyntaxError" to use an "async with" statement outside the\n' |
| 536 | 'body of a coroutine function.\n' |
| 537 | '\n' |
| 538 | 'See also:\n' |
| 539 | '\n' |
| 540 | ' **PEP 492** - Coroutines with async and await syntax\n' |
| 541 | ' The proposal that made coroutines a proper standalone concept ' |
| 542 | 'in\n' |
| 543 | ' Python, and added supporting syntax.\n' |
| 544 | '\n' |
| 545 | '-[ Footnotes ]-\n' |
| 546 | '\n' |
| 547 | '[1] The exception is propagated to the invocation stack unless\n' |
| 548 | ' there is a "finally" clause which happens to raise another\n' |
| 549 | ' exception. That new exception causes the old one to be lost.\n' |
| 550 | '\n' |
| 551 | '[2] A string literal appearing as the first statement in the\n' |
| 552 | ' function body is transformed into the function’s "__doc__"\n' |
| 553 | ' attribute and therefore the function’s *docstring*.\n' |
| 554 | '\n' |
| 555 | '[3] A string literal appearing as the first statement in the class\n' |
| 556 | ' body is transformed into the namespace’s "__doc__" item and\n' |
| 557 | ' therefore the class’s *docstring*.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 558 | 'atom-identifiers': 'Identifiers (Names)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 559 | '*******************\n' |
| 560 | '\n' |
| 561 | 'An identifier occurring as an atom is a name. See ' |
| 562 | 'section Identifiers\n' |
| 563 | 'and keywords for lexical definition and section Naming ' |
| 564 | 'and binding for\n' |
| 565 | 'documentation of naming and binding.\n' |
| 566 | '\n' |
| 567 | 'When the name is bound to an object, evaluation of the ' |
| 568 | 'atom yields\n' |
| 569 | 'that object. When a name is not bound, an attempt to ' |
| 570 | 'evaluate it\n' |
| 571 | 'raises a "NameError" exception.\n' |
| 572 | '\n' |
| 573 | '**Private name mangling:** When an identifier that ' |
| 574 | 'textually occurs in\n' |
| 575 | 'a class definition begins with two or more underscore ' |
| 576 | 'characters and\n' |
| 577 | 'does not end in two or more underscores, it is ' |
| 578 | 'considered a *private\n' |
| 579 | 'name* of that class. Private names are transformed to a ' |
| 580 | 'longer form\n' |
| 581 | 'before code is generated for them. The transformation ' |
| 582 | 'inserts the\n' |
| 583 | 'class name, with leading underscores removed and a ' |
| 584 | 'single underscore\n' |
| 585 | 'inserted, in front of the name. For example, the ' |
| 586 | 'identifier "__spam"\n' |
| 587 | 'occurring in a class named "Ham" will be transformed to ' |
| 588 | '"_Ham__spam".\n' |
| 589 | 'This transformation is independent of the syntactical ' |
| 590 | 'context in which\n' |
| 591 | 'the identifier is used. If the transformed name is ' |
| 592 | 'extremely long\n' |
| 593 | '(longer than 255 characters), implementation defined ' |
| 594 | 'truncation may\n' |
| 595 | 'happen. If the class name consists only of underscores, ' |
| 596 | 'no\n' |
| 597 | 'transformation is done.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 598 | 'atom-literals': 'Literals\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 599 | '********\n' |
| 600 | '\n' |
| 601 | 'Python supports string and bytes literals and various ' |
| 602 | 'numeric\n' |
| 603 | 'literals:\n' |
| 604 | '\n' |
| 605 | ' literal ::= stringliteral | bytesliteral\n' |
| 606 | ' | integer | floatnumber | imagnumber\n' |
| 607 | '\n' |
| 608 | 'Evaluation of a literal yields an object of the given type ' |
| 609 | '(string,\n' |
| 610 | 'bytes, integer, floating point number, complex number) with ' |
| 611 | 'the given\n' |
| 612 | 'value. The value may be approximated in the case of ' |
| 613 | 'floating point\n' |
| 614 | 'and imaginary (complex) literals. See section Literals for ' |
| 615 | 'details.\n' |
| 616 | '\n' |
| 617 | 'All literals correspond to immutable data types, and hence ' |
| 618 | 'the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 619 | 'object’s identity is less important than its value. ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 620 | 'Multiple\n' |
| 621 | 'evaluations of literals with the same value (either the ' |
| 622 | 'same\n' |
| 623 | 'occurrence in the program text or a different occurrence) ' |
| 624 | 'may obtain\n' |
| 625 | 'the same object or a different object with the same ' |
| 626 | 'value.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 627 | 'attribute-access': 'Customizing attribute access\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 628 | '****************************\n' |
| 629 | '\n' |
| 630 | 'The following methods can be defined to customize the ' |
| 631 | 'meaning of\n' |
| 632 | 'attribute access (use of, assignment to, or deletion of ' |
| 633 | '"x.name") for\n' |
| 634 | 'class instances.\n' |
| 635 | '\n' |
| 636 | 'object.__getattr__(self, name)\n' |
| 637 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 638 | ' Called when the default attribute access fails with ' |
| 639 | 'an\n' |
| 640 | ' "AttributeError" (either "__getattribute__()" raises ' |
| 641 | 'an\n' |
| 642 | ' "AttributeError" because *name* is not an instance ' |
| 643 | 'attribute or an\n' |
| 644 | ' attribute in the class tree for "self"; or ' |
| 645 | '"__get__()" of a *name*\n' |
| 646 | ' property raises "AttributeError"). This method ' |
| 647 | 'should either\n' |
| 648 | ' return the (computed) attribute value or raise an ' |
| 649 | '"AttributeError"\n' |
| 650 | ' exception.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 651 | '\n' |
| 652 | ' Note that if the attribute is found through the ' |
| 653 | 'normal mechanism,\n' |
| 654 | ' "__getattr__()" is not called. (This is an ' |
| 655 | 'intentional asymmetry\n' |
| 656 | ' between "__getattr__()" and "__setattr__()".) This is ' |
| 657 | 'done both for\n' |
| 658 | ' efficiency reasons and because otherwise ' |
| 659 | '"__getattr__()" would have\n' |
| 660 | ' no way to access other attributes of the instance. ' |
| 661 | 'Note that at\n' |
| 662 | ' least for instance variables, you can fake total ' |
| 663 | 'control by not\n' |
| 664 | ' inserting any values in the instance attribute ' |
| 665 | 'dictionary (but\n' |
| 666 | ' instead inserting them in another object). See the\n' |
| 667 | ' "__getattribute__()" method below for a way to ' |
| 668 | 'actually get total\n' |
| 669 | ' control over attribute access.\n' |
| 670 | '\n' |
| 671 | 'object.__getattribute__(self, name)\n' |
| 672 | '\n' |
| 673 | ' Called unconditionally to implement attribute ' |
| 674 | 'accesses for\n' |
| 675 | ' instances of the class. If the class also defines ' |
| 676 | '"__getattr__()",\n' |
| 677 | ' the latter will not be called unless ' |
| 678 | '"__getattribute__()" either\n' |
| 679 | ' calls it explicitly or raises an "AttributeError". ' |
| 680 | 'This method\n' |
| 681 | ' should return the (computed) attribute value or raise ' |
| 682 | 'an\n' |
| 683 | ' "AttributeError" exception. In order to avoid ' |
| 684 | 'infinite recursion in\n' |
| 685 | ' this method, its implementation should always call ' |
| 686 | 'the base class\n' |
| 687 | ' method with the same name to access any attributes it ' |
| 688 | 'needs, for\n' |
| 689 | ' example, "object.__getattribute__(self, name)".\n' |
| 690 | '\n' |
| 691 | ' Note: This method may still be bypassed when looking ' |
| 692 | 'up special\n' |
| 693 | ' methods as the result of implicit invocation via ' |
| 694 | 'language syntax\n' |
| 695 | ' or built-in functions. See Special method lookup.\n' |
| 696 | '\n' |
| 697 | 'object.__setattr__(self, name, value)\n' |
| 698 | '\n' |
| 699 | ' Called when an attribute assignment is attempted. ' |
| 700 | 'This is called\n' |
| 701 | ' instead of the normal mechanism (i.e. store the value ' |
| 702 | 'in the\n' |
| 703 | ' instance dictionary). *name* is the attribute name, ' |
| 704 | '*value* is the\n' |
| 705 | ' value to be assigned to it.\n' |
| 706 | '\n' |
| 707 | ' If "__setattr__()" wants to assign to an instance ' |
| 708 | 'attribute, it\n' |
| 709 | ' should call the base class method with the same name, ' |
| 710 | 'for example,\n' |
| 711 | ' "object.__setattr__(self, name, value)".\n' |
| 712 | '\n' |
| 713 | 'object.__delattr__(self, name)\n' |
| 714 | '\n' |
| 715 | ' Like "__setattr__()" but for attribute deletion ' |
| 716 | 'instead of\n' |
| 717 | ' assignment. This should only be implemented if "del ' |
| 718 | 'obj.name" is\n' |
| 719 | ' meaningful for the object.\n' |
| 720 | '\n' |
| 721 | 'object.__dir__(self)\n' |
| 722 | '\n' |
| 723 | ' Called when "dir()" is called on the object. A ' |
| 724 | 'sequence must be\n' |
| 725 | ' returned. "dir()" converts the returned sequence to a ' |
| 726 | 'list and\n' |
| 727 | ' sorts it.\n' |
| 728 | '\n' |
| 729 | '\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 730 | 'Customizing module attribute access\n' |
| 731 | '===================================\n' |
| 732 | '\n' |
| 733 | 'Special names "__getattr__" and "__dir__" can be also ' |
| 734 | 'used to\n' |
| 735 | 'customize access to module attributes. The "__getattr__" ' |
| 736 | 'function at\n' |
| 737 | 'the module level should accept one argument which is the ' |
| 738 | 'name of an\n' |
| 739 | 'attribute and return the computed value or raise an ' |
| 740 | '"AttributeError".\n' |
| 741 | 'If an attribute is not found on a module object through ' |
| 742 | 'the normal\n' |
| 743 | 'lookup, i.e. "object.__getattribute__()", then ' |
| 744 | '"__getattr__" is\n' |
| 745 | 'searched in the module "__dict__" before raising an ' |
| 746 | '"AttributeError".\n' |
| 747 | 'If found, it is called with the attribute name and the ' |
| 748 | 'result is\n' |
| 749 | 'returned.\n' |
| 750 | '\n' |
| 751 | 'The "__dir__" function should accept no arguments, and ' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 752 | 'return a\n' |
| 753 | 'sequence of strings that represents the names accessible ' |
| 754 | 'on module. If\n' |
| 755 | 'present, this function overrides the standard "dir()" ' |
| 756 | 'search on a\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 757 | 'module.\n' |
| 758 | '\n' |
| 759 | 'For a more fine grained customization of the module ' |
| 760 | 'behavior (setting\n' |
| 761 | 'attributes, properties, etc.), one can set the ' |
| 762 | '"__class__" attribute\n' |
| 763 | 'of a module object to a subclass of "types.ModuleType". ' |
| 764 | 'For example:\n' |
| 765 | '\n' |
| 766 | ' import sys\n' |
| 767 | ' from types import ModuleType\n' |
| 768 | '\n' |
| 769 | ' class VerboseModule(ModuleType):\n' |
| 770 | ' def __repr__(self):\n' |
| 771 | " return f'Verbose {self.__name__}'\n" |
| 772 | '\n' |
| 773 | ' def __setattr__(self, attr, value):\n' |
| 774 | " print(f'Setting {attr}...')\n" |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 775 | ' super().__setattr__(attr, value)\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 776 | '\n' |
| 777 | ' sys.modules[__name__].__class__ = VerboseModule\n' |
| 778 | '\n' |
| 779 | 'Note: Defining module "__getattr__" and setting module ' |
| 780 | '"__class__"\n' |
| 781 | ' only affect lookups made using the attribute access ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 782 | 'syntax –\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 783 | ' directly accessing the module globals (whether by code ' |
| 784 | 'within the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 785 | ' module, or via a reference to the module’s globals ' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 786 | 'dictionary) is\n' |
| 787 | ' unaffected.\n' |
| 788 | '\n' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 789 | 'Changed in version 3.5: "__class__" module attribute is ' |
| 790 | 'now writable.\n' |
| 791 | '\n' |
| 792 | 'New in version 3.7: "__getattr__" and "__dir__" module ' |
| 793 | 'attributes.\n' |
| 794 | '\n' |
| 795 | 'See also:\n' |
| 796 | '\n' |
| 797 | ' **PEP 562** - Module __getattr__ and __dir__\n' |
| 798 | ' Describes the "__getattr__" and "__dir__" functions ' |
| 799 | 'on modules.\n' |
| 800 | '\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 801 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 802 | 'Implementing Descriptors\n' |
| 803 | '========================\n' |
| 804 | '\n' |
| 805 | 'The following methods only apply when an instance of the ' |
| 806 | 'class\n' |
| 807 | 'containing the method (a so-called *descriptor* class) ' |
| 808 | 'appears in an\n' |
| 809 | '*owner* class (the descriptor must be in either the ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 810 | 'owner’s class\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 811 | 'dictionary or in the class dictionary for one of its ' |
| 812 | 'parents). In the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 813 | 'examples below, “the attribute” refers to the attribute ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 814 | 'whose name is\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 815 | 'the key of the property in the owner class’ "__dict__".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 816 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 817 | 'object.__get__(self, instance, owner=None)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 818 | '\n' |
| 819 | ' Called to get the attribute of the owner class (class ' |
| 820 | 'attribute\n' |
| 821 | ' access) or of an instance of that class (instance ' |
| 822 | 'attribute\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 823 | ' access). The optional *owner* argument is the owner ' |
| 824 | 'class, while\n' |
| 825 | ' *instance* is the instance that the attribute was ' |
| 826 | 'accessed through,\n' |
| 827 | ' or "None" when the attribute is accessed through the ' |
| 828 | '*owner*.\n' |
| 829 | '\n' |
| 830 | ' This method should return the computed attribute ' |
| 831 | 'value or raise an\n' |
| 832 | ' "AttributeError" exception.\n' |
| 833 | '\n' |
| 834 | ' **PEP 252** specifies that "__get__()" is callable ' |
| 835 | 'with one or two\n' |
| 836 | ' arguments. Python’s own built-in descriptors support ' |
| 837 | 'this\n' |
| 838 | ' specification; however, it is likely that some ' |
| 839 | 'third-party tools\n' |
| 840 | ' have descriptors that require both arguments. ' |
| 841 | 'Python’s own\n' |
| 842 | ' "__getattribute__()" implementation always passes in ' |
| 843 | 'both arguments\n' |
| 844 | ' whether they are required or not.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 845 | '\n' |
| 846 | 'object.__set__(self, instance, value)\n' |
| 847 | '\n' |
| 848 | ' Called to set the attribute on an instance *instance* ' |
| 849 | 'of the owner\n' |
| 850 | ' class to a new value, *value*.\n' |
| 851 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 852 | ' Note, adding "__set__()" or "__delete__()" changes ' |
| 853 | 'the kind of\n' |
| 854 | ' descriptor to a “data descriptor”. See Invoking ' |
| 855 | 'Descriptors for\n' |
| 856 | ' more details.\n' |
| 857 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 858 | 'object.__delete__(self, instance)\n' |
| 859 | '\n' |
| 860 | ' Called to delete the attribute on an instance ' |
| 861 | '*instance* of the\n' |
| 862 | ' owner class.\n' |
| 863 | '\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 864 | 'object.__set_name__(self, owner, name)\n' |
| 865 | '\n' |
| 866 | ' Called at the time the owning class *owner* is ' |
| 867 | 'created. The\n' |
| 868 | ' descriptor has been assigned to *name*.\n' |
| 869 | '\n' |
Łukasz Langa | 6202d85 | 2019-12-18 22:09:19 +0100 | [diff] [blame] | 870 | ' Note: "__set_name__()" is only called implicitly as ' |
| 871 | 'part of the\n' |
| 872 | ' "type" constructor, so it will need to be called ' |
| 873 | 'explicitly with\n' |
| 874 | ' the appropriate parameters when a descriptor is ' |
| 875 | 'added to a class\n' |
| 876 | ' after initial creation:\n' |
| 877 | '\n' |
| 878 | ' class A:\n' |
| 879 | ' pass\n' |
| 880 | ' descr = custom_descriptor()\n' |
| 881 | ' A.attr = descr\n' |
| 882 | " descr.__set_name__(A, 'attr')\n" |
| 883 | '\n' |
| 884 | ' See Creating the class object for more details.\n' |
| 885 | '\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 886 | ' New in version 3.6.\n' |
| 887 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 888 | 'The attribute "__objclass__" is interpreted by the ' |
| 889 | '"inspect" module as\n' |
| 890 | 'specifying the class where this object was defined ' |
| 891 | '(setting this\n' |
| 892 | 'appropriately can assist in runtime introspection of ' |
| 893 | 'dynamic class\n' |
| 894 | 'attributes). For callables, it may indicate that an ' |
| 895 | 'instance of the\n' |
| 896 | 'given type (or a subclass) is expected or required as ' |
| 897 | 'the first\n' |
| 898 | 'positional argument (for example, CPython sets this ' |
| 899 | 'attribute for\n' |
| 900 | 'unbound methods that are implemented in C).\n' |
| 901 | '\n' |
| 902 | '\n' |
| 903 | 'Invoking Descriptors\n' |
| 904 | '====================\n' |
| 905 | '\n' |
| 906 | 'In general, a descriptor is an object attribute with ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 907 | '“binding\n' |
| 908 | 'behavior”, one whose attribute access has been ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 909 | 'overridden by methods\n' |
| 910 | 'in the descriptor protocol: "__get__()", "__set__()", ' |
| 911 | 'and\n' |
| 912 | '"__delete__()". If any of those methods are defined for ' |
| 913 | 'an object, it\n' |
| 914 | 'is said to be a descriptor.\n' |
| 915 | '\n' |
| 916 | 'The default behavior for attribute access is to get, ' |
| 917 | 'set, or delete\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 918 | 'the attribute from an object’s dictionary. For instance, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 919 | '"a.x" has a\n' |
| 920 | 'lookup chain starting with "a.__dict__[\'x\']", then\n' |
| 921 | '"type(a).__dict__[\'x\']", and continuing through the ' |
| 922 | 'base classes of\n' |
| 923 | '"type(a)" excluding metaclasses.\n' |
| 924 | '\n' |
| 925 | 'However, if the looked-up value is an object defining ' |
| 926 | 'one of the\n' |
| 927 | 'descriptor methods, then Python may override the default ' |
| 928 | 'behavior and\n' |
| 929 | 'invoke the descriptor method instead. Where this occurs ' |
| 930 | 'in the\n' |
| 931 | 'precedence chain depends on which descriptor methods ' |
| 932 | 'were defined and\n' |
| 933 | 'how they were called.\n' |
| 934 | '\n' |
| 935 | 'The starting point for descriptor invocation is a ' |
| 936 | 'binding, "a.x". How\n' |
| 937 | 'the arguments are assembled depends on "a":\n' |
| 938 | '\n' |
| 939 | 'Direct Call\n' |
| 940 | ' The simplest and least common call is when user code ' |
| 941 | 'directly\n' |
| 942 | ' invokes a descriptor method: "x.__get__(a)".\n' |
| 943 | '\n' |
| 944 | 'Instance Binding\n' |
| 945 | ' If binding to an object instance, "a.x" is ' |
| 946 | 'transformed into the\n' |
| 947 | ' call: "type(a).__dict__[\'x\'].__get__(a, type(a))".\n' |
| 948 | '\n' |
| 949 | 'Class Binding\n' |
| 950 | ' If binding to a class, "A.x" is transformed into the ' |
| 951 | 'call:\n' |
| 952 | ' "A.__dict__[\'x\'].__get__(None, A)".\n' |
| 953 | '\n' |
| 954 | 'Super Binding\n' |
| 955 | ' If "a" is an instance of "super", then the binding ' |
| 956 | '"super(B,\n' |
| 957 | ' obj).m()" searches "obj.__class__.__mro__" for the ' |
| 958 | 'base class "A"\n' |
| 959 | ' immediately preceding "B" and then invokes the ' |
| 960 | 'descriptor with the\n' |
| 961 | ' call: "A.__dict__[\'m\'].__get__(obj, ' |
| 962 | 'obj.__class__)".\n' |
| 963 | '\n' |
| 964 | 'For instance bindings, the precedence of descriptor ' |
| 965 | 'invocation depends\n' |
| 966 | 'on the which descriptor methods are defined. A ' |
| 967 | 'descriptor can define\n' |
| 968 | 'any combination of "__get__()", "__set__()" and ' |
| 969 | '"__delete__()". If it\n' |
| 970 | 'does not define "__get__()", then accessing the ' |
| 971 | 'attribute will return\n' |
| 972 | 'the descriptor object itself unless there is a value in ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 973 | 'the object’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 974 | 'instance dictionary. If the descriptor defines ' |
| 975 | '"__set__()" and/or\n' |
| 976 | '"__delete__()", it is a data descriptor; if it defines ' |
| 977 | 'neither, it is\n' |
| 978 | 'a non-data descriptor. Normally, data descriptors ' |
| 979 | 'define both\n' |
| 980 | '"__get__()" and "__set__()", while non-data descriptors ' |
| 981 | 'have just the\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 982 | '"__get__()" method. Data descriptors with "__get__()" ' |
| 983 | 'and "__set__()"\n' |
| 984 | '(and/or "__delete__()") defined always override a ' |
| 985 | 'redefinition in an\n' |
| 986 | 'instance dictionary. In contrast, non-data descriptors ' |
| 987 | 'can be\n' |
| 988 | 'overridden by instances.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 989 | '\n' |
| 990 | 'Python methods (including "staticmethod()" and ' |
| 991 | '"classmethod()") are\n' |
| 992 | 'implemented as non-data descriptors. Accordingly, ' |
| 993 | 'instances can\n' |
| 994 | 'redefine and override methods. This allows individual ' |
| 995 | 'instances to\n' |
| 996 | 'acquire behaviors that differ from other instances of ' |
| 997 | 'the same class.\n' |
| 998 | '\n' |
| 999 | 'The "property()" function is implemented as a data ' |
| 1000 | 'descriptor.\n' |
| 1001 | 'Accordingly, instances cannot override the behavior of a ' |
| 1002 | 'property.\n' |
| 1003 | '\n' |
| 1004 | '\n' |
| 1005 | '__slots__\n' |
| 1006 | '=========\n' |
| 1007 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1008 | '*__slots__* allow us to explicitly declare data members ' |
| 1009 | '(like\n' |
| 1010 | 'properties) and deny the creation of *__dict__* and ' |
| 1011 | '*__weakref__*\n' |
| 1012 | '(unless explicitly declared in *__slots__* or available ' |
| 1013 | 'in a parent.)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1014 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1015 | 'The space saved over using *__dict__* can be ' |
Łukasz Langa | 23f4589 | 2019-02-25 13:08:32 +0100 | [diff] [blame] | 1016 | 'significant. Attribute\n' |
| 1017 | 'lookup speed can be significantly improved as well.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1018 | '\n' |
| 1019 | 'object.__slots__\n' |
| 1020 | '\n' |
| 1021 | ' This class variable can be assigned a string, ' |
| 1022 | 'iterable, or sequence\n' |
| 1023 | ' of strings with variable names used by instances. ' |
| 1024 | '*__slots__*\n' |
| 1025 | ' reserves space for the declared variables and ' |
| 1026 | 'prevents the\n' |
| 1027 | ' automatic creation of *__dict__* and *__weakref__* ' |
| 1028 | 'for each\n' |
| 1029 | ' instance.\n' |
| 1030 | '\n' |
| 1031 | '\n' |
| 1032 | 'Notes on using *__slots__*\n' |
| 1033 | '--------------------------\n' |
| 1034 | '\n' |
| 1035 | '* When inheriting from a class without *__slots__*, the ' |
| 1036 | '*__dict__*\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1037 | ' and *__weakref__* attribute of the instances will ' |
| 1038 | 'always be\n' |
| 1039 | ' accessible.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1040 | '\n' |
| 1041 | '* Without a *__dict__* variable, instances cannot be ' |
| 1042 | 'assigned new\n' |
| 1043 | ' variables not listed in the *__slots__* definition. ' |
| 1044 | 'Attempts to\n' |
| 1045 | ' assign to an unlisted variable name raises ' |
| 1046 | '"AttributeError". If\n' |
| 1047 | ' dynamic assignment of new variables is desired, then ' |
| 1048 | 'add\n' |
| 1049 | ' "\'__dict__\'" to the sequence of strings in the ' |
| 1050 | '*__slots__*\n' |
| 1051 | ' declaration.\n' |
| 1052 | '\n' |
| 1053 | '* Without a *__weakref__* variable for each instance, ' |
| 1054 | 'classes\n' |
| 1055 | ' defining *__slots__* do not support weak references to ' |
| 1056 | 'its\n' |
| 1057 | ' instances. If weak reference support is needed, then ' |
| 1058 | 'add\n' |
| 1059 | ' "\'__weakref__\'" to the sequence of strings in the ' |
| 1060 | '*__slots__*\n' |
| 1061 | ' declaration.\n' |
| 1062 | '\n' |
| 1063 | '* *__slots__* are implemented at the class level by ' |
| 1064 | 'creating\n' |
| 1065 | ' descriptors (Implementing Descriptors) for each ' |
| 1066 | 'variable name. As a\n' |
| 1067 | ' result, class attributes cannot be used to set default ' |
| 1068 | 'values for\n' |
| 1069 | ' instance variables defined by *__slots__*; otherwise, ' |
| 1070 | 'the class\n' |
| 1071 | ' attribute would overwrite the descriptor assignment.\n' |
| 1072 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1073 | '* The action of a *__slots__* declaration is not limited ' |
| 1074 | 'to the\n' |
| 1075 | ' class where it is defined. *__slots__* declared in ' |
| 1076 | 'parents are\n' |
| 1077 | ' available in child classes. However, child subclasses ' |
| 1078 | 'will get a\n' |
| 1079 | ' *__dict__* and *__weakref__* unless they also define ' |
| 1080 | '*__slots__*\n' |
| 1081 | ' (which should only contain names of any *additional* ' |
| 1082 | 'slots).\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1083 | '\n' |
| 1084 | '* If a class defines a slot also defined in a base ' |
| 1085 | 'class, the\n' |
| 1086 | ' instance variable defined by the base class slot is ' |
| 1087 | 'inaccessible\n' |
| 1088 | ' (except by retrieving its descriptor directly from the ' |
| 1089 | 'base class).\n' |
| 1090 | ' This renders the meaning of the program undefined. In ' |
| 1091 | 'the future, a\n' |
| 1092 | ' check may be added to prevent this.\n' |
| 1093 | '\n' |
| 1094 | '* Nonempty *__slots__* does not work for classes derived ' |
| 1095 | 'from\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1096 | ' “variable-length” built-in types such as "int", ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1097 | '"bytes" and "tuple".\n' |
| 1098 | '\n' |
| 1099 | '* Any non-string iterable may be assigned to ' |
| 1100 | '*__slots__*. Mappings\n' |
| 1101 | ' may also be used; however, in the future, special ' |
| 1102 | 'meaning may be\n' |
| 1103 | ' assigned to the values corresponding to each key.\n' |
| 1104 | '\n' |
| 1105 | '* *__class__* assignment works only if both classes have ' |
| 1106 | 'the same\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1107 | ' *__slots__*.\n' |
| 1108 | '\n' |
| 1109 | '* Multiple inheritance with multiple slotted parent ' |
| 1110 | 'classes can be\n' |
| 1111 | ' used, but only one parent is allowed to have ' |
| 1112 | 'attributes created by\n' |
| 1113 | ' slots (the other bases must have empty slot layouts) - ' |
| 1114 | 'violations\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 1115 | ' raise "TypeError".\n' |
| 1116 | '\n' |
| 1117 | '* If an iterator is used for *__slots__* then a ' |
| 1118 | 'descriptor is\n' |
| 1119 | ' created for each of the iterator’s values. However, ' |
| 1120 | 'the *__slots__*\n' |
| 1121 | ' attribute will be an empty iterator.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1122 | 'attribute-references': 'Attribute references\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1123 | '********************\n' |
| 1124 | '\n' |
| 1125 | 'An attribute reference is a primary followed by a ' |
| 1126 | 'period and a name:\n' |
| 1127 | '\n' |
| 1128 | ' attributeref ::= primary "." identifier\n' |
| 1129 | '\n' |
| 1130 | 'The primary must evaluate to an object of a type ' |
| 1131 | 'that supports\n' |
| 1132 | 'attribute references, which most objects do. This ' |
| 1133 | 'object is then\n' |
| 1134 | 'asked to produce the attribute whose name is the ' |
| 1135 | 'identifier. This\n' |
| 1136 | 'production can be customized by overriding the ' |
| 1137 | '"__getattr__()" method.\n' |
| 1138 | 'If this attribute is not available, the exception ' |
| 1139 | '"AttributeError" is\n' |
| 1140 | 'raised. Otherwise, the type and value of the object ' |
| 1141 | 'produced is\n' |
| 1142 | 'determined by the object. Multiple evaluations of ' |
| 1143 | 'the same attribute\n' |
| 1144 | 'reference may yield different objects.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1145 | 'augassign': 'Augmented assignment statements\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1146 | '*******************************\n' |
| 1147 | '\n' |
| 1148 | 'Augmented assignment is the combination, in a single statement, ' |
| 1149 | 'of a\n' |
| 1150 | 'binary operation and an assignment statement:\n' |
| 1151 | '\n' |
| 1152 | ' augmented_assignment_stmt ::= augtarget augop ' |
| 1153 | '(expression_list | yield_expression)\n' |
| 1154 | ' augtarget ::= identifier | attributeref | ' |
| 1155 | 'subscription | slicing\n' |
| 1156 | ' augop ::= "+=" | "-=" | "*=" | "@=" | ' |
| 1157 | '"/=" | "//=" | "%=" | "**="\n' |
| 1158 | ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n' |
| 1159 | '\n' |
| 1160 | '(See section Primaries for the syntax definitions of the last ' |
| 1161 | 'three\n' |
| 1162 | 'symbols.)\n' |
| 1163 | '\n' |
| 1164 | 'An augmented assignment evaluates the target (which, unlike ' |
| 1165 | 'normal\n' |
| 1166 | 'assignment statements, cannot be an unpacking) and the ' |
| 1167 | 'expression\n' |
| 1168 | 'list, performs the binary operation specific to the type of ' |
| 1169 | 'assignment\n' |
| 1170 | 'on the two operands, and assigns the result to the original ' |
| 1171 | 'target.\n' |
| 1172 | 'The target is only evaluated once.\n' |
| 1173 | '\n' |
| 1174 | 'An augmented assignment expression like "x += 1" can be ' |
| 1175 | 'rewritten as\n' |
| 1176 | '"x = x + 1" to achieve a similar, but not exactly equal effect. ' |
| 1177 | 'In the\n' |
| 1178 | 'augmented version, "x" is only evaluated once. Also, when ' |
| 1179 | 'possible,\n' |
| 1180 | 'the actual operation is performed *in-place*, meaning that ' |
| 1181 | 'rather than\n' |
| 1182 | 'creating a new object and assigning that to the target, the old ' |
| 1183 | 'object\n' |
| 1184 | 'is modified instead.\n' |
| 1185 | '\n' |
| 1186 | 'Unlike normal assignments, augmented assignments evaluate the ' |
| 1187 | 'left-\n' |
| 1188 | 'hand side *before* evaluating the right-hand side. For ' |
| 1189 | 'example, "a[i]\n' |
| 1190 | '+= f(x)" first looks-up "a[i]", then it evaluates "f(x)" and ' |
| 1191 | 'performs\n' |
| 1192 | 'the addition, and lastly, it writes the result back to "a[i]".\n' |
| 1193 | '\n' |
| 1194 | 'With the exception of assigning to tuples and multiple targets ' |
| 1195 | 'in a\n' |
| 1196 | 'single statement, the assignment done by augmented assignment\n' |
| 1197 | 'statements is handled the same way as normal assignments. ' |
| 1198 | 'Similarly,\n' |
| 1199 | 'with the exception of the possible *in-place* behavior, the ' |
| 1200 | 'binary\n' |
| 1201 | 'operation performed by augmented assignment is the same as the ' |
| 1202 | 'normal\n' |
| 1203 | 'binary operations.\n' |
| 1204 | '\n' |
| 1205 | 'For targets which are attribute references, the same caveat ' |
| 1206 | 'about\n' |
| 1207 | 'class and instance attributes applies as for regular ' |
| 1208 | 'assignments.\n', |
Ned Deily | c730223 | 2017-10-16 23:41:55 -0400 | [diff] [blame] | 1209 | 'await': 'Await expression\n' |
| 1210 | '****************\n' |
| 1211 | '\n' |
| 1212 | 'Suspend the execution of *coroutine* on an *awaitable* object. Can\n' |
| 1213 | 'only be used inside a *coroutine function*.\n' |
| 1214 | '\n' |
| 1215 | ' await_expr ::= "await" primary\n' |
| 1216 | '\n' |
| 1217 | 'New in version 3.5.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1218 | 'binary': 'Binary arithmetic operations\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1219 | '****************************\n' |
| 1220 | '\n' |
| 1221 | 'The binary arithmetic operations have the conventional priority\n' |
| 1222 | 'levels. Note that some of these operations also apply to certain ' |
| 1223 | 'non-\n' |
| 1224 | 'numeric types. Apart from the power operator, there are only two\n' |
| 1225 | 'levels, one for multiplicative operators and one for additive\n' |
| 1226 | 'operators:\n' |
| 1227 | '\n' |
| 1228 | ' m_expr ::= u_expr | m_expr "*" u_expr | m_expr "@" m_expr |\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1229 | ' m_expr "//" u_expr | m_expr "/" u_expr |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1230 | ' m_expr "%" u_expr\n' |
| 1231 | ' a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr\n' |
| 1232 | '\n' |
| 1233 | 'The "*" (multiplication) operator yields the product of its ' |
| 1234 | 'arguments.\n' |
| 1235 | 'The arguments must either both be numbers, or one argument must be ' |
| 1236 | 'an\n' |
| 1237 | 'integer and the other must be a sequence. In the former case, the\n' |
| 1238 | 'numbers are converted to a common type and then multiplied ' |
| 1239 | 'together.\n' |
| 1240 | 'In the latter case, sequence repetition is performed; a negative\n' |
| 1241 | 'repetition factor yields an empty sequence.\n' |
| 1242 | '\n' |
| 1243 | 'The "@" (at) operator is intended to be used for matrix\n' |
| 1244 | 'multiplication. No builtin Python types implement this operator.\n' |
| 1245 | '\n' |
| 1246 | 'New in version 3.5.\n' |
| 1247 | '\n' |
| 1248 | 'The "/" (division) and "//" (floor division) operators yield the\n' |
| 1249 | 'quotient of their arguments. The numeric arguments are first\n' |
| 1250 | 'converted to a common type. Division of integers yields a float, ' |
| 1251 | 'while\n' |
| 1252 | 'floor division of integers results in an integer; the result is ' |
| 1253 | 'that\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1254 | 'of mathematical division with the ‘floor’ function applied to the\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1255 | 'result. Division by zero raises the "ZeroDivisionError" ' |
| 1256 | 'exception.\n' |
| 1257 | '\n' |
| 1258 | 'The "%" (modulo) operator yields the remainder from the division ' |
| 1259 | 'of\n' |
| 1260 | 'the first argument by the second. The numeric arguments are ' |
| 1261 | 'first\n' |
| 1262 | 'converted to a common type. A zero right argument raises the\n' |
| 1263 | '"ZeroDivisionError" exception. The arguments may be floating ' |
| 1264 | 'point\n' |
| 1265 | 'numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals ' |
| 1266 | '"4*0.7 +\n' |
| 1267 | '0.34".) The modulo operator always yields a result with the same ' |
| 1268 | 'sign\n' |
| 1269 | 'as its second operand (or zero); the absolute value of the result ' |
| 1270 | 'is\n' |
| 1271 | 'strictly smaller than the absolute value of the second operand ' |
| 1272 | '[1].\n' |
| 1273 | '\n' |
| 1274 | 'The floor division and modulo operators are connected by the ' |
| 1275 | 'following\n' |
| 1276 | 'identity: "x == (x//y)*y + (x%y)". Floor division and modulo are ' |
| 1277 | 'also\n' |
| 1278 | 'connected with the built-in function "divmod()": "divmod(x, y) ==\n' |
| 1279 | '(x//y, x%y)". [2].\n' |
| 1280 | '\n' |
| 1281 | 'In addition to performing the modulo operation on numbers, the ' |
| 1282 | '"%"\n' |
| 1283 | 'operator is also overloaded by string objects to perform ' |
| 1284 | 'old-style\n' |
| 1285 | 'string formatting (also known as interpolation). The syntax for\n' |
| 1286 | 'string formatting is described in the Python Library Reference,\n' |
| 1287 | 'section printf-style String Formatting.\n' |
| 1288 | '\n' |
| 1289 | 'The floor division operator, the modulo operator, and the ' |
| 1290 | '"divmod()"\n' |
| 1291 | 'function are not defined for complex numbers. Instead, convert to ' |
| 1292 | 'a\n' |
| 1293 | 'floating point number using the "abs()" function if appropriate.\n' |
| 1294 | '\n' |
| 1295 | 'The "+" (addition) operator yields the sum of its arguments. The\n' |
| 1296 | 'arguments must either both be numbers or both be sequences of the ' |
| 1297 | 'same\n' |
| 1298 | 'type. In the former case, the numbers are converted to a common ' |
| 1299 | 'type\n' |
| 1300 | 'and then added together. In the latter case, the sequences are\n' |
| 1301 | 'concatenated.\n' |
| 1302 | '\n' |
| 1303 | 'The "-" (subtraction) operator yields the difference of its ' |
| 1304 | 'arguments.\n' |
| 1305 | 'The numeric arguments are first converted to a common type.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1306 | 'bitwise': 'Binary bitwise operations\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1307 | '*************************\n' |
| 1308 | '\n' |
| 1309 | 'Each of the three bitwise operations has a different priority ' |
| 1310 | 'level:\n' |
| 1311 | '\n' |
| 1312 | ' and_expr ::= shift_expr | and_expr "&" shift_expr\n' |
| 1313 | ' xor_expr ::= and_expr | xor_expr "^" and_expr\n' |
| 1314 | ' or_expr ::= xor_expr | or_expr "|" xor_expr\n' |
| 1315 | '\n' |
| 1316 | 'The "&" operator yields the bitwise AND of its arguments, which ' |
| 1317 | 'must\n' |
| 1318 | 'be integers.\n' |
| 1319 | '\n' |
| 1320 | 'The "^" operator yields the bitwise XOR (exclusive OR) of its\n' |
| 1321 | 'arguments, which must be integers.\n' |
| 1322 | '\n' |
| 1323 | 'The "|" operator yields the bitwise (inclusive) OR of its ' |
| 1324 | 'arguments,\n' |
| 1325 | 'which must be integers.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1326 | 'bltin-code-objects': 'Code Objects\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1327 | '************\n' |
| 1328 | '\n' |
| 1329 | 'Code objects are used by the implementation to ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1330 | 'represent “pseudo-\n' |
| 1331 | 'compiled” executable Python code such as a function ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1332 | 'body. They differ\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1333 | 'from function objects because they don’t contain a ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1334 | 'reference to their\n' |
| 1335 | 'global execution environment. Code objects are ' |
| 1336 | 'returned by the built-\n' |
| 1337 | 'in "compile()" function and can be extracted from ' |
| 1338 | 'function objects\n' |
| 1339 | 'through their "__code__" attribute. See also the ' |
| 1340 | '"code" module.\n' |
| 1341 | '\n' |
| 1342 | 'A code object can be executed or evaluated by passing ' |
| 1343 | 'it (instead of a\n' |
| 1344 | 'source string) to the "exec()" or "eval()" built-in ' |
| 1345 | 'functions.\n' |
| 1346 | '\n' |
| 1347 | 'See The standard type hierarchy for more ' |
| 1348 | 'information.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1349 | 'bltin-ellipsis-object': 'The Ellipsis Object\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1350 | '*******************\n' |
| 1351 | '\n' |
| 1352 | 'This object is commonly used by slicing (see ' |
| 1353 | 'Slicings). It supports\n' |
| 1354 | 'no special operations. There is exactly one ' |
| 1355 | 'ellipsis object, named\n' |
| 1356 | '"Ellipsis" (a built-in name). "type(Ellipsis)()" ' |
| 1357 | 'produces the\n' |
| 1358 | '"Ellipsis" singleton.\n' |
| 1359 | '\n' |
| 1360 | 'It is written as "Ellipsis" or "...".\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1361 | 'bltin-null-object': 'The Null Object\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1362 | '***************\n' |
| 1363 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1364 | 'This object is returned by functions that don’t ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1365 | 'explicitly return a\n' |
| 1366 | 'value. It supports no special operations. There is ' |
| 1367 | 'exactly one null\n' |
| 1368 | 'object, named "None" (a built-in name). "type(None)()" ' |
| 1369 | 'produces the\n' |
| 1370 | 'same singleton.\n' |
| 1371 | '\n' |
| 1372 | 'It is written as "None".\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1373 | 'bltin-type-objects': 'Type Objects\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1374 | '************\n' |
| 1375 | '\n' |
| 1376 | 'Type objects represent the various object types. An ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1377 | 'object’s type is\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1378 | 'accessed by the built-in function "type()". There are ' |
| 1379 | 'no special\n' |
| 1380 | 'operations on types. The standard module "types" ' |
| 1381 | 'defines names for\n' |
| 1382 | 'all standard built-in types.\n' |
| 1383 | '\n' |
| 1384 | 'Types are written like this: "<class \'int\'>".\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1385 | 'booleans': 'Boolean operations\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1386 | '******************\n' |
| 1387 | '\n' |
| 1388 | ' or_test ::= and_test | or_test "or" and_test\n' |
| 1389 | ' and_test ::= not_test | and_test "and" not_test\n' |
| 1390 | ' not_test ::= comparison | "not" not_test\n' |
| 1391 | '\n' |
| 1392 | 'In the context of Boolean operations, and also when expressions ' |
| 1393 | 'are\n' |
| 1394 | 'used by control flow statements, the following values are ' |
| 1395 | 'interpreted\n' |
| 1396 | 'as false: "False", "None", numeric zero of all types, and empty\n' |
| 1397 | 'strings and containers (including strings, tuples, lists,\n' |
| 1398 | 'dictionaries, sets and frozensets). All other values are ' |
| 1399 | 'interpreted\n' |
| 1400 | 'as true. User-defined objects can customize their truth value ' |
| 1401 | 'by\n' |
| 1402 | 'providing a "__bool__()" method.\n' |
| 1403 | '\n' |
| 1404 | 'The operator "not" yields "True" if its argument is false, ' |
| 1405 | '"False"\n' |
| 1406 | 'otherwise.\n' |
| 1407 | '\n' |
| 1408 | 'The expression "x and y" first evaluates *x*; if *x* is false, ' |
| 1409 | 'its\n' |
| 1410 | 'value is returned; otherwise, *y* is evaluated and the resulting ' |
| 1411 | 'value\n' |
| 1412 | 'is returned.\n' |
| 1413 | '\n' |
| 1414 | 'The expression "x or y" first evaluates *x*; if *x* is true, its ' |
| 1415 | 'value\n' |
| 1416 | 'is returned; otherwise, *y* is evaluated and the resulting value ' |
| 1417 | 'is\n' |
| 1418 | 'returned.\n' |
| 1419 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1420 | 'Note that neither "and" nor "or" restrict the value and type ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1421 | 'they\n' |
| 1422 | 'return to "False" and "True", but rather return the last ' |
| 1423 | 'evaluated\n' |
| 1424 | 'argument. This is sometimes useful, e.g., if "s" is a string ' |
| 1425 | 'that\n' |
| 1426 | 'should be replaced by a default value if it is empty, the ' |
| 1427 | 'expression\n' |
| 1428 | '"s or \'foo\'" yields the desired value. Because "not" has to ' |
| 1429 | 'create a\n' |
| 1430 | 'new value, it returns a boolean value regardless of the type of ' |
| 1431 | 'its\n' |
| 1432 | 'argument (for example, "not \'foo\'" produces "False" rather ' |
| 1433 | 'than "\'\'".)\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1434 | 'break': 'The "break" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1435 | '*********************\n' |
| 1436 | '\n' |
| 1437 | ' break_stmt ::= "break"\n' |
| 1438 | '\n' |
| 1439 | '"break" may only occur syntactically nested in a "for" or "while"\n' |
| 1440 | 'loop, but not nested in a function or class definition within that\n' |
| 1441 | 'loop.\n' |
| 1442 | '\n' |
| 1443 | 'It terminates the nearest enclosing loop, skipping the optional ' |
| 1444 | '"else"\n' |
| 1445 | 'clause if the loop has one.\n' |
| 1446 | '\n' |
| 1447 | 'If a "for" loop is terminated by "break", the loop control target\n' |
| 1448 | 'keeps its current value.\n' |
| 1449 | '\n' |
| 1450 | 'When "break" passes control out of a "try" statement with a ' |
| 1451 | '"finally"\n' |
| 1452 | 'clause, that "finally" clause is executed before really leaving ' |
| 1453 | 'the\n' |
| 1454 | 'loop.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1455 | 'callable-types': 'Emulating callable objects\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1456 | '**************************\n' |
| 1457 | '\n' |
| 1458 | 'object.__call__(self[, args...])\n' |
| 1459 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1460 | ' Called when the instance is “called” as a function; if ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1461 | 'this method\n' |
| 1462 | ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n' |
| 1463 | ' "x.__call__(arg1, arg2, ...)".\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1464 | 'calls': 'Calls\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1465 | '*****\n' |
| 1466 | '\n' |
| 1467 | 'A call calls a callable object (e.g., a *function*) with a ' |
| 1468 | 'possibly\n' |
| 1469 | 'empty series of *arguments*:\n' |
| 1470 | '\n' |
| 1471 | ' call ::= primary "(" [argument_list [","] | ' |
| 1472 | 'comprehension] ")"\n' |
| 1473 | ' argument_list ::= positional_arguments ["," ' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 1474 | 'starred_and_keywords]\n' |
| 1475 | ' ["," keywords_arguments]\n' |
| 1476 | ' | starred_and_keywords ["," ' |
| 1477 | 'keywords_arguments]\n' |
| 1478 | ' | keywords_arguments\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 1479 | ' positional_arguments ::= positional_item ("," positional_item)*\n' |
| 1480 | ' positional_item ::= assignment_expression | "*" expression\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 1481 | ' starred_and_keywords ::= ("*" expression | keyword_item)\n' |
| 1482 | ' ("," "*" expression | "," ' |
| 1483 | 'keyword_item)*\n' |
| 1484 | ' keywords_arguments ::= (keyword_item | "**" expression)\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1485 | ' ("," keyword_item | "," "**" ' |
| 1486 | 'expression)*\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1487 | ' keyword_item ::= identifier "=" expression\n' |
| 1488 | '\n' |
| 1489 | 'An optional trailing comma may be present after the positional and\n' |
| 1490 | 'keyword arguments but does not affect the semantics.\n' |
| 1491 | '\n' |
| 1492 | 'The primary must evaluate to a callable object (user-defined\n' |
| 1493 | 'functions, built-in functions, methods of built-in objects, class\n' |
| 1494 | 'objects, methods of class instances, and all objects having a\n' |
| 1495 | '"__call__()" method are callable). All argument expressions are\n' |
| 1496 | 'evaluated before the call is attempted. Please refer to section\n' |
| 1497 | 'Function definitions for the syntax of formal *parameter* lists.\n' |
| 1498 | '\n' |
| 1499 | 'If keyword arguments are present, they are first converted to\n' |
| 1500 | 'positional arguments, as follows. First, a list of unfilled slots ' |
| 1501 | 'is\n' |
| 1502 | 'created for the formal parameters. If there are N positional\n' |
| 1503 | 'arguments, they are placed in the first N slots. Next, for each\n' |
| 1504 | 'keyword argument, the identifier is used to determine the\n' |
| 1505 | 'corresponding slot (if the identifier is the same as the first ' |
| 1506 | 'formal\n' |
| 1507 | 'parameter name, the first slot is used, and so on). If the slot ' |
| 1508 | 'is\n' |
| 1509 | 'already filled, a "TypeError" exception is raised. Otherwise, the\n' |
| 1510 | 'value of the argument is placed in the slot, filling it (even if ' |
| 1511 | 'the\n' |
| 1512 | 'expression is "None", it fills the slot). When all arguments have\n' |
| 1513 | 'been processed, the slots that are still unfilled are filled with ' |
| 1514 | 'the\n' |
| 1515 | 'corresponding default value from the function definition. ' |
| 1516 | '(Default\n' |
| 1517 | 'values are calculated, once, when the function is defined; thus, a\n' |
| 1518 | 'mutable object such as a list or dictionary used as default value ' |
| 1519 | 'will\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1520 | 'be shared by all calls that don’t specify an argument value for ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1521 | 'the\n' |
| 1522 | 'corresponding slot; this should usually be avoided.) If there are ' |
| 1523 | 'any\n' |
| 1524 | 'unfilled slots for which no default value is specified, a ' |
| 1525 | '"TypeError"\n' |
| 1526 | 'exception is raised. Otherwise, the list of filled slots is used ' |
| 1527 | 'as\n' |
| 1528 | 'the argument list for the call.\n' |
| 1529 | '\n' |
| 1530 | '**CPython implementation detail:** An implementation may provide\n' |
| 1531 | 'built-in functions whose positional parameters do not have names, ' |
| 1532 | 'even\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1533 | 'if they are ‘named’ for the purpose of documentation, and which\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1534 | 'therefore cannot be supplied by keyword. In CPython, this is the ' |
| 1535 | 'case\n' |
| 1536 | 'for functions implemented in C that use "PyArg_ParseTuple()" to ' |
| 1537 | 'parse\n' |
| 1538 | 'their arguments.\n' |
| 1539 | '\n' |
| 1540 | 'If there are more positional arguments than there are formal ' |
| 1541 | 'parameter\n' |
| 1542 | 'slots, a "TypeError" exception is raised, unless a formal ' |
| 1543 | 'parameter\n' |
| 1544 | 'using the syntax "*identifier" is present; in this case, that ' |
| 1545 | 'formal\n' |
| 1546 | 'parameter receives a tuple containing the excess positional ' |
| 1547 | 'arguments\n' |
| 1548 | '(or an empty tuple if there were no excess positional arguments).\n' |
| 1549 | '\n' |
| 1550 | 'If any keyword argument does not correspond to a formal parameter\n' |
| 1551 | 'name, a "TypeError" exception is raised, unless a formal parameter\n' |
| 1552 | 'using the syntax "**identifier" is present; in this case, that ' |
| 1553 | 'formal\n' |
| 1554 | 'parameter receives a dictionary containing the excess keyword\n' |
| 1555 | 'arguments (using the keywords as keys and the argument values as\n' |
| 1556 | 'corresponding values), or a (new) empty dictionary if there were ' |
| 1557 | 'no\n' |
| 1558 | 'excess keyword arguments.\n' |
| 1559 | '\n' |
| 1560 | 'If the syntax "*expression" appears in the function call, ' |
| 1561 | '"expression"\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 1562 | 'must evaluate to an *iterable*. Elements from these iterables are\n' |
| 1563 | 'treated as if they were additional positional arguments. For the ' |
| 1564 | 'call\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1565 | '"f(x1, x2, *y, x3, x4)", if *y* evaluates to a sequence *y1*, …, ' |
| 1566 | '*yM*,\n' |
| 1567 | 'this is equivalent to a call with M+4 positional arguments *x1*, ' |
| 1568 | '*x2*,\n' |
| 1569 | '*y1*, …, *yM*, *x3*, *x4*.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1570 | '\n' |
| 1571 | 'A consequence of this is that although the "*expression" syntax ' |
| 1572 | 'may\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 1573 | 'appear *after* explicit keyword arguments, it is processed ' |
| 1574 | '*before*\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1575 | 'the keyword arguments (and any "**expression" arguments – see ' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 1576 | 'below).\n' |
| 1577 | 'So:\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1578 | '\n' |
| 1579 | ' >>> def f(a, b):\n' |
| 1580 | ' ... print(a, b)\n' |
| 1581 | ' ...\n' |
| 1582 | ' >>> f(b=1, *(2,))\n' |
| 1583 | ' 2 1\n' |
| 1584 | ' >>> f(a=1, *(2,))\n' |
| 1585 | ' Traceback (most recent call last):\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1586 | ' File "<stdin>", line 1, in <module>\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1587 | " TypeError: f() got multiple values for keyword argument 'a'\n" |
| 1588 | ' >>> f(1, *(2,))\n' |
| 1589 | ' 1 2\n' |
| 1590 | '\n' |
| 1591 | 'It is unusual for both keyword arguments and the "*expression" ' |
| 1592 | 'syntax\n' |
| 1593 | 'to be used in the same call, so in practice this confusion does ' |
| 1594 | 'not\n' |
| 1595 | 'arise.\n' |
| 1596 | '\n' |
| 1597 | 'If the syntax "**expression" appears in the function call,\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 1598 | '"expression" must evaluate to a *mapping*, the contents of which ' |
| 1599 | 'are\n' |
| 1600 | 'treated as additional keyword arguments. If a keyword is already\n' |
| 1601 | 'present (as an explicit keyword argument, or from another ' |
| 1602 | 'unpacking),\n' |
| 1603 | 'a "TypeError" exception is raised.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1604 | '\n' |
| 1605 | 'Formal parameters using the syntax "*identifier" or "**identifier"\n' |
| 1606 | 'cannot be used as positional argument slots or as keyword argument\n' |
| 1607 | 'names.\n' |
| 1608 | '\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 1609 | 'Changed in version 3.5: Function calls accept any number of "*" ' |
| 1610 | 'and\n' |
| 1611 | '"**" unpackings, positional arguments may follow iterable ' |
| 1612 | 'unpackings\n' |
| 1613 | '("*"), and keyword arguments may follow dictionary unpackings ' |
| 1614 | '("**").\n' |
| 1615 | 'Originally proposed by **PEP 448**.\n' |
| 1616 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1617 | 'A call always returns some value, possibly "None", unless it raises ' |
| 1618 | 'an\n' |
| 1619 | 'exception. How this value is computed depends on the type of the\n' |
| 1620 | 'callable object.\n' |
| 1621 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1622 | 'If it is—\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1623 | '\n' |
| 1624 | 'a user-defined function:\n' |
| 1625 | ' The code block for the function is executed, passing it the\n' |
| 1626 | ' argument list. The first thing the code block will do is bind ' |
| 1627 | 'the\n' |
| 1628 | ' formal parameters to the arguments; this is described in ' |
| 1629 | 'section\n' |
| 1630 | ' Function definitions. When the code block executes a "return"\n' |
| 1631 | ' statement, this specifies the return value of the function ' |
| 1632 | 'call.\n' |
| 1633 | '\n' |
| 1634 | 'a built-in function or method:\n' |
| 1635 | ' The result is up to the interpreter; see Built-in Functions for ' |
| 1636 | 'the\n' |
| 1637 | ' descriptions of built-in functions and methods.\n' |
| 1638 | '\n' |
| 1639 | 'a class object:\n' |
| 1640 | ' A new instance of that class is returned.\n' |
| 1641 | '\n' |
| 1642 | 'a class instance method:\n' |
| 1643 | ' The corresponding user-defined function is called, with an ' |
| 1644 | 'argument\n' |
| 1645 | ' list that is one longer than the argument list of the call: the\n' |
| 1646 | ' instance becomes the first argument.\n' |
| 1647 | '\n' |
| 1648 | 'a class instance:\n' |
| 1649 | ' The class must define a "__call__()" method; the effect is then ' |
| 1650 | 'the\n' |
| 1651 | ' same as if that method was called.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1652 | 'class': 'Class definitions\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1653 | '*****************\n' |
| 1654 | '\n' |
| 1655 | 'A class definition defines a class object (see section The ' |
| 1656 | 'standard\n' |
| 1657 | 'type hierarchy):\n' |
| 1658 | '\n' |
| 1659 | ' classdef ::= [decorators] "class" classname [inheritance] ":" ' |
| 1660 | 'suite\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 1661 | ' inheritance ::= "(" [argument_list] ")"\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1662 | ' classname ::= identifier\n' |
| 1663 | '\n' |
| 1664 | 'A class definition is an executable statement. The inheritance ' |
| 1665 | 'list\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 1666 | 'usually gives a list of base classes (see Metaclasses for more\n' |
| 1667 | 'advanced uses), so each item in the list should evaluate to a ' |
| 1668 | 'class\n' |
| 1669 | 'object which allows subclassing. Classes without an inheritance ' |
| 1670 | 'list\n' |
| 1671 | 'inherit, by default, from the base class "object"; hence,\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1672 | '\n' |
| 1673 | ' class Foo:\n' |
| 1674 | ' pass\n' |
| 1675 | '\n' |
| 1676 | 'is equivalent to\n' |
| 1677 | '\n' |
| 1678 | ' class Foo(object):\n' |
| 1679 | ' pass\n' |
| 1680 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1681 | 'The class’s suite is then executed in a new execution frame (see\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1682 | 'Naming and binding), using a newly created local namespace and the\n' |
| 1683 | 'original global namespace. (Usually, the suite contains mostly\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1684 | 'function definitions.) When the class’s suite finishes execution, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1685 | 'its\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1686 | 'execution frame is discarded but its local namespace is saved. [3] ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1687 | 'A\n' |
| 1688 | 'class object is then created using the inheritance list for the ' |
| 1689 | 'base\n' |
| 1690 | 'classes and the saved local namespace for the attribute ' |
| 1691 | 'dictionary.\n' |
| 1692 | 'The class name is bound to this class object in the original local\n' |
| 1693 | 'namespace.\n' |
| 1694 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 1695 | 'The order in which attributes are defined in the class body is\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1696 | 'preserved in the new class’s "__dict__". Note that this is ' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 1697 | 'reliable\n' |
| 1698 | 'only right after the class is created and only for classes that ' |
| 1699 | 'were\n' |
| 1700 | 'defined using the definition syntax.\n' |
| 1701 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1702 | 'Class creation can be customized heavily using metaclasses.\n' |
| 1703 | '\n' |
| 1704 | 'Classes can also be decorated: just like when decorating ' |
| 1705 | 'functions,\n' |
| 1706 | '\n' |
| 1707 | ' @f1(arg)\n' |
| 1708 | ' @f2\n' |
| 1709 | ' class Foo: pass\n' |
| 1710 | '\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 1711 | 'is roughly equivalent to\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1712 | '\n' |
| 1713 | ' class Foo: pass\n' |
| 1714 | ' Foo = f1(arg)(f2(Foo))\n' |
| 1715 | '\n' |
| 1716 | 'The evaluation rules for the decorator expressions are the same as ' |
| 1717 | 'for\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 1718 | 'function decorators. The result is then bound to the class name.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1719 | '\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 1720 | 'Changed in version 3.9: Classes may be decorated with any valid\n' |
| 1721 | '"assignment_expression". Previously, the grammar was much more\n' |
| 1722 | 'restrictive; see **PEP 614** for details.\n' |
| 1723 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1724 | '**Programmer’s note:** Variables defined in the class definition ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1725 | 'are\n' |
| 1726 | 'class attributes; they are shared by instances. Instance ' |
| 1727 | 'attributes\n' |
| 1728 | 'can be set in a method with "self.name = value". Both class and\n' |
| 1729 | 'instance attributes are accessible through the notation ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1730 | '“"self.name"”,\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1731 | 'and an instance attribute hides a class attribute with the same ' |
| 1732 | 'name\n' |
| 1733 | 'when accessed in this way. Class attributes can be used as ' |
| 1734 | 'defaults\n' |
| 1735 | 'for instance attributes, but using mutable values there can lead ' |
| 1736 | 'to\n' |
| 1737 | 'unexpected results. Descriptors can be used to create instance\n' |
| 1738 | 'variables with different implementation details.\n' |
| 1739 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1740 | 'See also:\n' |
| 1741 | '\n' |
| 1742 | ' **PEP 3115** - Metaclasses in Python 3000\n' |
| 1743 | ' The proposal that changed the declaration of metaclasses to ' |
| 1744 | 'the\n' |
| 1745 | ' current syntax, and the semantics for how classes with\n' |
| 1746 | ' metaclasses are constructed.\n' |
| 1747 | '\n' |
| 1748 | ' **PEP 3129** - Class Decorators\n' |
| 1749 | ' The proposal that added class decorators. Function and ' |
| 1750 | 'method\n' |
| 1751 | ' decorators were introduced in **PEP 318**.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1752 | 'comparisons': 'Comparisons\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1753 | '***********\n' |
| 1754 | '\n' |
| 1755 | 'Unlike C, all comparison operations in Python have the same ' |
| 1756 | 'priority,\n' |
| 1757 | 'which is lower than that of any arithmetic, shifting or ' |
| 1758 | 'bitwise\n' |
| 1759 | 'operation. Also unlike C, expressions like "a < b < c" have ' |
| 1760 | 'the\n' |
| 1761 | 'interpretation that is conventional in mathematics:\n' |
| 1762 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1763 | ' comparison ::= or_expr (comp_operator or_expr)*\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1764 | ' comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "!="\n' |
| 1765 | ' | "is" ["not"] | ["not"] "in"\n' |
| 1766 | '\n' |
| 1767 | 'Comparisons yield boolean values: "True" or "False".\n' |
| 1768 | '\n' |
| 1769 | 'Comparisons can be chained arbitrarily, e.g., "x < y <= z" ' |
| 1770 | 'is\n' |
| 1771 | 'equivalent to "x < y and y <= z", except that "y" is ' |
| 1772 | 'evaluated only\n' |
| 1773 | 'once (but in both cases "z" is not evaluated at all when "x < ' |
| 1774 | 'y" is\n' |
| 1775 | 'found to be false).\n' |
| 1776 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1777 | 'Formally, if *a*, *b*, *c*, …, *y*, *z* are expressions and ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1778 | '*op1*,\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1779 | '*op2*, …, *opN* are comparison operators, then "a op1 b op2 c ' |
| 1780 | '... y\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1781 | 'opN z" is equivalent to "a op1 b and b op2 c and ... y opN ' |
| 1782 | 'z", except\n' |
| 1783 | 'that each expression is evaluated at most once.\n' |
| 1784 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1785 | 'Note that "a op1 b op2 c" doesn’t imply any kind of ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1786 | 'comparison between\n' |
| 1787 | '*a* and *c*, so that, e.g., "x < y > z" is perfectly legal ' |
| 1788 | '(though\n' |
| 1789 | 'perhaps not pretty).\n' |
| 1790 | '\n' |
| 1791 | '\n' |
| 1792 | 'Value comparisons\n' |
| 1793 | '=================\n' |
| 1794 | '\n' |
| 1795 | 'The operators "<", ">", "==", ">=", "<=", and "!=" compare ' |
| 1796 | 'the values\n' |
| 1797 | 'of two objects. The objects do not need to have the same ' |
| 1798 | 'type.\n' |
| 1799 | '\n' |
| 1800 | 'Chapter Objects, values and types states that objects have a ' |
| 1801 | 'value (in\n' |
| 1802 | 'addition to type and identity). The value of an object is a ' |
| 1803 | 'rather\n' |
| 1804 | 'abstract notion in Python: For example, there is no canonical ' |
| 1805 | 'access\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1806 | 'method for an object’s value. Also, there is no requirement ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1807 | 'that the\n' |
| 1808 | 'value of an object should be constructed in a particular way, ' |
| 1809 | 'e.g.\n' |
| 1810 | 'comprised of all its data attributes. Comparison operators ' |
| 1811 | 'implement a\n' |
| 1812 | 'particular notion of what the value of an object is. One can ' |
| 1813 | 'think of\n' |
| 1814 | 'them as defining the value of an object indirectly, by means ' |
| 1815 | 'of their\n' |
| 1816 | 'comparison implementation.\n' |
| 1817 | '\n' |
| 1818 | 'Because all types are (direct or indirect) subtypes of ' |
| 1819 | '"object", they\n' |
| 1820 | 'inherit the default comparison behavior from "object". Types ' |
| 1821 | 'can\n' |
| 1822 | 'customize their comparison behavior by implementing *rich ' |
| 1823 | 'comparison\n' |
| 1824 | 'methods* like "__lt__()", described in Basic customization.\n' |
| 1825 | '\n' |
| 1826 | 'The default behavior for equality comparison ("==" and "!=") ' |
| 1827 | 'is based\n' |
| 1828 | 'on the identity of the objects. Hence, equality comparison ' |
| 1829 | 'of\n' |
| 1830 | 'instances with the same identity results in equality, and ' |
| 1831 | 'equality\n' |
| 1832 | 'comparison of instances with different identities results in\n' |
| 1833 | 'inequality. A motivation for this default behavior is the ' |
| 1834 | 'desire that\n' |
| 1835 | 'all objects should be reflexive (i.e. "x is y" implies "x == ' |
| 1836 | 'y").\n' |
| 1837 | '\n' |
| 1838 | 'A default order comparison ("<", ">", "<=", and ">=") is not ' |
| 1839 | 'provided;\n' |
| 1840 | 'an attempt raises "TypeError". A motivation for this default ' |
| 1841 | 'behavior\n' |
| 1842 | 'is the lack of a similar invariant as for equality.\n' |
| 1843 | '\n' |
| 1844 | 'The behavior of the default equality comparison, that ' |
| 1845 | 'instances with\n' |
| 1846 | 'different identities are always unequal, may be in contrast ' |
| 1847 | 'to what\n' |
| 1848 | 'types will need that have a sensible definition of object ' |
| 1849 | 'value and\n' |
| 1850 | 'value-based equality. Such types will need to customize ' |
| 1851 | 'their\n' |
| 1852 | 'comparison behavior, and in fact, a number of built-in types ' |
| 1853 | 'have done\n' |
| 1854 | 'that.\n' |
| 1855 | '\n' |
| 1856 | 'The following list describes the comparison behavior of the ' |
| 1857 | 'most\n' |
| 1858 | 'important built-in types.\n' |
| 1859 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1860 | '* Numbers of built-in numeric types (Numeric Types — int, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1861 | 'float,\n' |
| 1862 | ' complex) and of the standard library types ' |
| 1863 | '"fractions.Fraction" and\n' |
| 1864 | ' "decimal.Decimal" can be compared within and across their ' |
| 1865 | 'types,\n' |
| 1866 | ' with the restriction that complex numbers do not support ' |
| 1867 | 'order\n' |
| 1868 | ' comparison. Within the limits of the types involved, they ' |
| 1869 | 'compare\n' |
| 1870 | ' mathematically (algorithmically) correct without loss of ' |
| 1871 | 'precision.\n' |
| 1872 | '\n' |
| 1873 | ' The not-a-number values "float(\'NaN\')" and ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 1874 | '"decimal.Decimal(\'NaN\')"\n' |
| 1875 | ' are special. Any ordered comparison of a number to a ' |
| 1876 | 'not-a-number\n' |
| 1877 | ' value is false. A counter-intuitive implication is that ' |
| 1878 | 'not-a-number\n' |
| 1879 | ' values are not equal to themselves. For example, if "x =\n' |
Łukasz Langa | bc1c8af | 2020-04-27 22:44:04 +0200 | [diff] [blame] | 1880 | ' float(\'NaN\')", "3 < x", "x < 3" and "x == x" are all ' |
| 1881 | 'false, while "x\n' |
| 1882 | ' != x" is true. This behavior is compliant with IEEE 754.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1883 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 1884 | '* "None" and "NotImplemented" are singletons. **PEP 8** ' |
| 1885 | 'advises\n' |
| 1886 | ' that comparisons for singletons should always be done with ' |
| 1887 | '"is" or\n' |
| 1888 | ' "is not", never the equality operators.\n' |
| 1889 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1890 | '* Binary sequences (instances of "bytes" or "bytearray") can ' |
| 1891 | 'be\n' |
| 1892 | ' compared within and across their types. They compare\n' |
| 1893 | ' lexicographically using the numeric values of their ' |
| 1894 | 'elements.\n' |
| 1895 | '\n' |
| 1896 | '* Strings (instances of "str") compare lexicographically ' |
| 1897 | 'using the\n' |
| 1898 | ' numerical Unicode code points (the result of the built-in ' |
| 1899 | 'function\n' |
| 1900 | ' "ord()") of their characters. [3]\n' |
| 1901 | '\n' |
| 1902 | ' Strings and binary sequences cannot be directly compared.\n' |
| 1903 | '\n' |
| 1904 | '* Sequences (instances of "tuple", "list", or "range") can ' |
| 1905 | 'be\n' |
| 1906 | ' compared only within each of their types, with the ' |
| 1907 | 'restriction that\n' |
| 1908 | ' ranges do not support order comparison. Equality ' |
| 1909 | 'comparison across\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1910 | ' these types results in inequality, and ordering comparison ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1911 | 'across\n' |
| 1912 | ' these types raises "TypeError".\n' |
| 1913 | '\n' |
| 1914 | ' Sequences compare lexicographically using comparison of\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 1915 | ' corresponding elements. The built-in containers typically ' |
| 1916 | 'assume\n' |
| 1917 | ' identical objects are equal to themselves. That lets them ' |
| 1918 | 'bypass\n' |
| 1919 | ' equality tests for identical objects to improve performance ' |
| 1920 | 'and to\n' |
| 1921 | ' maintain their internal invariants.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1922 | '\n' |
| 1923 | ' Lexicographical comparison between built-in collections ' |
| 1924 | 'works as\n' |
| 1925 | ' follows:\n' |
| 1926 | '\n' |
| 1927 | ' * For two collections to compare equal, they must be of the ' |
| 1928 | 'same\n' |
| 1929 | ' type, have the same length, and each pair of ' |
| 1930 | 'corresponding\n' |
| 1931 | ' elements must compare equal (for example, "[1,2] == ' |
| 1932 | '(1,2)" is\n' |
| 1933 | ' false because the type is not the same).\n' |
| 1934 | '\n' |
| 1935 | ' * Collections that support order comparison are ordered the ' |
| 1936 | 'same\n' |
| 1937 | ' as their first unequal elements (for example, "[1,2,x] <= ' |
| 1938 | '[1,2,y]"\n' |
| 1939 | ' has the same value as "x <= y"). If a corresponding ' |
| 1940 | 'element does\n' |
| 1941 | ' not exist, the shorter collection is ordered first (for ' |
| 1942 | 'example,\n' |
| 1943 | ' "[1,2] < [1,2,3]" is true).\n' |
| 1944 | '\n' |
| 1945 | '* Mappings (instances of "dict") compare equal if and only if ' |
| 1946 | 'they\n' |
| 1947 | ' have equal *(key, value)* pairs. Equality comparison of the ' |
| 1948 | 'keys and\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 1949 | ' values enforces reflexivity.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 1950 | '\n' |
| 1951 | ' Order comparisons ("<", ">", "<=", and ">=") raise ' |
| 1952 | '"TypeError".\n' |
| 1953 | '\n' |
| 1954 | '* Sets (instances of "set" or "frozenset") can be compared ' |
| 1955 | 'within\n' |
| 1956 | ' and across their types.\n' |
| 1957 | '\n' |
| 1958 | ' They define order comparison operators to mean subset and ' |
| 1959 | 'superset\n' |
| 1960 | ' tests. Those relations do not define total orderings (for ' |
| 1961 | 'example,\n' |
| 1962 | ' the two sets "{1,2}" and "{2,3}" are not equal, nor subsets ' |
| 1963 | 'of one\n' |
| 1964 | ' another, nor supersets of one another). Accordingly, sets ' |
| 1965 | 'are not\n' |
| 1966 | ' appropriate arguments for functions which depend on total ' |
| 1967 | 'ordering\n' |
| 1968 | ' (for example, "min()", "max()", and "sorted()" produce ' |
| 1969 | 'undefined\n' |
| 1970 | ' results given a list of sets as inputs).\n' |
| 1971 | '\n' |
| 1972 | ' Comparison of sets enforces reflexivity of its elements.\n' |
| 1973 | '\n' |
| 1974 | '* Most other built-in types have no comparison methods ' |
| 1975 | 'implemented,\n' |
| 1976 | ' so they inherit the default comparison behavior.\n' |
| 1977 | '\n' |
| 1978 | 'User-defined classes that customize their comparison behavior ' |
| 1979 | 'should\n' |
| 1980 | 'follow some consistency rules, if possible:\n' |
| 1981 | '\n' |
| 1982 | '* Equality comparison should be reflexive. In other words, ' |
| 1983 | 'identical\n' |
| 1984 | ' objects should compare equal:\n' |
| 1985 | '\n' |
| 1986 | ' "x is y" implies "x == y"\n' |
| 1987 | '\n' |
| 1988 | '* Comparison should be symmetric. In other words, the ' |
| 1989 | 'following\n' |
| 1990 | ' expressions should have the same result:\n' |
| 1991 | '\n' |
| 1992 | ' "x == y" and "y == x"\n' |
| 1993 | '\n' |
| 1994 | ' "x != y" and "y != x"\n' |
| 1995 | '\n' |
| 1996 | ' "x < y" and "y > x"\n' |
| 1997 | '\n' |
| 1998 | ' "x <= y" and "y >= x"\n' |
| 1999 | '\n' |
| 2000 | '* Comparison should be transitive. The following ' |
| 2001 | '(non-exhaustive)\n' |
| 2002 | ' examples illustrate that:\n' |
| 2003 | '\n' |
| 2004 | ' "x > y and y > z" implies "x > z"\n' |
| 2005 | '\n' |
| 2006 | ' "x < y and y <= z" implies "x < z"\n' |
| 2007 | '\n' |
| 2008 | '* Inverse comparison should result in the boolean negation. ' |
| 2009 | 'In other\n' |
| 2010 | ' words, the following expressions should have the same ' |
| 2011 | 'result:\n' |
| 2012 | '\n' |
| 2013 | ' "x == y" and "not x != y"\n' |
| 2014 | '\n' |
| 2015 | ' "x < y" and "not x >= y" (for total ordering)\n' |
| 2016 | '\n' |
| 2017 | ' "x > y" and "not x <= y" (for total ordering)\n' |
| 2018 | '\n' |
| 2019 | ' The last two expressions apply to totally ordered ' |
| 2020 | 'collections (e.g.\n' |
| 2021 | ' to sequences, but not to sets or mappings). See also the\n' |
| 2022 | ' "total_ordering()" decorator.\n' |
| 2023 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 2024 | '* The "hash()" result should be consistent with equality. ' |
| 2025 | 'Objects\n' |
| 2026 | ' that are equal should either have the same hash value, or ' |
| 2027 | 'be marked\n' |
| 2028 | ' as unhashable.\n' |
| 2029 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2030 | 'Python does not enforce these consistency rules. In fact, ' |
| 2031 | 'the\n' |
| 2032 | 'not-a-number values are an example for not following these ' |
| 2033 | 'rules.\n' |
| 2034 | '\n' |
| 2035 | '\n' |
| 2036 | 'Membership test operations\n' |
| 2037 | '==========================\n' |
| 2038 | '\n' |
| 2039 | 'The operators "in" and "not in" test for membership. "x in ' |
| 2040 | 's"\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 2041 | 'evaluates to "True" if *x* is a member of *s*, and "False" ' |
| 2042 | 'otherwise.\n' |
| 2043 | '"x not in s" returns the negation of "x in s". All built-in ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2044 | 'sequences\n' |
| 2045 | 'and set types support this as well as dictionary, for which ' |
| 2046 | '"in" tests\n' |
| 2047 | 'whether the dictionary has a given key. For container types ' |
| 2048 | 'such as\n' |
| 2049 | 'list, tuple, set, frozenset, dict, or collections.deque, the\n' |
| 2050 | 'expression "x in y" is equivalent to "any(x is e or x == e ' |
| 2051 | 'for e in\n' |
| 2052 | 'y)".\n' |
| 2053 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 2054 | 'For the string and bytes types, "x in y" is "True" if and ' |
| 2055 | 'only if *x*\n' |
| 2056 | 'is a substring of *y*. An equivalent test is "y.find(x) != ' |
| 2057 | '-1".\n' |
| 2058 | 'Empty strings are always considered to be a substring of any ' |
| 2059 | 'other\n' |
| 2060 | 'string, so """ in "abc"" will return "True".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2061 | '\n' |
| 2062 | 'For user-defined classes which define the "__contains__()" ' |
| 2063 | 'method, "x\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 2064 | 'in y" returns "True" if "y.__contains__(x)" returns a true ' |
| 2065 | 'value, and\n' |
| 2066 | '"False" otherwise.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2067 | '\n' |
| 2068 | 'For user-defined classes which do not define "__contains__()" ' |
| 2069 | 'but do\n' |
Łukasz Langa | 3b5deb0 | 2019-06-04 19:44:34 +0200 | [diff] [blame] | 2070 | 'define "__iter__()", "x in y" is "True" if some value "z", ' |
| 2071 | 'for which\n' |
| 2072 | 'the expression "x is z or x == z" is true, is produced while ' |
| 2073 | 'iterating\n' |
| 2074 | 'over "y". If an exception is raised during the iteration, it ' |
| 2075 | 'is as if\n' |
| 2076 | '"in" raised that exception.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2077 | '\n' |
| 2078 | 'Lastly, the old-style iteration protocol is tried: if a class ' |
| 2079 | 'defines\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 2080 | '"__getitem__()", "x in y" is "True" if and only if there is a ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2081 | 'non-\n' |
Łukasz Langa | 3b5deb0 | 2019-06-04 19:44:34 +0200 | [diff] [blame] | 2082 | 'negative integer index *i* such that "x is y[i] or x == ' |
| 2083 | 'y[i]", and no\n' |
| 2084 | 'lower integer index raises the "IndexError" exception. (If ' |
| 2085 | 'any other\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2086 | 'exception is raised, it is as if "in" raised that ' |
| 2087 | 'exception).\n' |
| 2088 | '\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 2089 | 'The operator "not in" is defined to have the inverse truth ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2090 | 'value of\n' |
| 2091 | '"in".\n' |
| 2092 | '\n' |
| 2093 | '\n' |
| 2094 | 'Identity comparisons\n' |
| 2095 | '====================\n' |
| 2096 | '\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 2097 | 'The operators "is" and "is not" test for an object’s ' |
| 2098 | 'identity: "x is\n' |
| 2099 | 'y" is true if and only if *x* and *y* are the same object. ' |
| 2100 | 'An\n' |
| 2101 | 'Object’s identity is determined using the "id()" function. ' |
| 2102 | '"x is not\n' |
| 2103 | 'y" yields the inverse truth value. [4]\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 2104 | 'compound': 'Compound statements\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2105 | '*******************\n' |
| 2106 | '\n' |
| 2107 | 'Compound statements contain (groups of) other statements; they ' |
| 2108 | 'affect\n' |
| 2109 | 'or control the execution of those other statements in some way. ' |
| 2110 | 'In\n' |
| 2111 | 'general, compound statements span multiple lines, although in ' |
| 2112 | 'simple\n' |
| 2113 | 'incarnations a whole compound statement may be contained in one ' |
| 2114 | 'line.\n' |
| 2115 | '\n' |
| 2116 | 'The "if", "while" and "for" statements implement traditional ' |
| 2117 | 'control\n' |
| 2118 | 'flow constructs. "try" specifies exception handlers and/or ' |
| 2119 | 'cleanup\n' |
| 2120 | 'code for a group of statements, while the "with" statement ' |
| 2121 | 'allows the\n' |
| 2122 | 'execution of initialization and finalization code around a block ' |
| 2123 | 'of\n' |
| 2124 | 'code. Function and class definitions are also syntactically ' |
| 2125 | 'compound\n' |
| 2126 | 'statements.\n' |
| 2127 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2128 | 'A compound statement consists of one or more ‘clauses.’ A ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2129 | 'clause\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2130 | 'consists of a header and a ‘suite.’ The clause headers of a\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2131 | 'particular compound statement are all at the same indentation ' |
| 2132 | 'level.\n' |
| 2133 | 'Each clause header begins with a uniquely identifying keyword ' |
| 2134 | 'and ends\n' |
| 2135 | 'with a colon. A suite is a group of statements controlled by a\n' |
| 2136 | 'clause. A suite can be one or more semicolon-separated simple\n' |
| 2137 | 'statements on the same line as the header, following the ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2138 | 'header’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2139 | 'colon, or it can be one or more indented statements on ' |
| 2140 | 'subsequent\n' |
| 2141 | 'lines. Only the latter form of a suite can contain nested ' |
| 2142 | 'compound\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2143 | 'statements; the following is illegal, mostly because it wouldn’t ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2144 | 'be\n' |
| 2145 | 'clear to which "if" clause a following "else" clause would ' |
| 2146 | 'belong:\n' |
| 2147 | '\n' |
| 2148 | ' if test1: if test2: print(x)\n' |
| 2149 | '\n' |
| 2150 | 'Also note that the semicolon binds tighter than the colon in ' |
| 2151 | 'this\n' |
| 2152 | 'context, so that in the following example, either all or none of ' |
| 2153 | 'the\n' |
| 2154 | '"print()" calls are executed:\n' |
| 2155 | '\n' |
| 2156 | ' if x < y < z: print(x); print(y); print(z)\n' |
| 2157 | '\n' |
| 2158 | 'Summarizing:\n' |
| 2159 | '\n' |
| 2160 | ' compound_stmt ::= if_stmt\n' |
| 2161 | ' | while_stmt\n' |
| 2162 | ' | for_stmt\n' |
| 2163 | ' | try_stmt\n' |
| 2164 | ' | with_stmt\n' |
| 2165 | ' | funcdef\n' |
| 2166 | ' | classdef\n' |
| 2167 | ' | async_with_stmt\n' |
| 2168 | ' | async_for_stmt\n' |
| 2169 | ' | async_funcdef\n' |
| 2170 | ' suite ::= stmt_list NEWLINE | NEWLINE INDENT ' |
| 2171 | 'statement+ DEDENT\n' |
| 2172 | ' statement ::= stmt_list NEWLINE | compound_stmt\n' |
| 2173 | ' stmt_list ::= simple_stmt (";" simple_stmt)* [";"]\n' |
| 2174 | '\n' |
| 2175 | 'Note that statements always end in a "NEWLINE" possibly followed ' |
| 2176 | 'by a\n' |
| 2177 | '"DEDENT". Also note that optional continuation clauses always ' |
| 2178 | 'begin\n' |
| 2179 | 'with a keyword that cannot start a statement, thus there are no\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2180 | 'ambiguities (the ‘dangling "else"’ problem is solved in Python ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2181 | 'by\n' |
| 2182 | 'requiring nested "if" statements to be indented).\n' |
| 2183 | '\n' |
| 2184 | 'The formatting of the grammar rules in the following sections ' |
| 2185 | 'places\n' |
| 2186 | 'each clause on a separate line for clarity.\n' |
| 2187 | '\n' |
| 2188 | '\n' |
| 2189 | 'The "if" statement\n' |
| 2190 | '==================\n' |
| 2191 | '\n' |
| 2192 | 'The "if" statement is used for conditional execution:\n' |
| 2193 | '\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 2194 | ' if_stmt ::= "if" assignment_expression ":" suite\n' |
| 2195 | ' ("elif" assignment_expression ":" suite)*\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2196 | ' ["else" ":" suite]\n' |
| 2197 | '\n' |
| 2198 | 'It selects exactly one of the suites by evaluating the ' |
| 2199 | 'expressions one\n' |
| 2200 | 'by one until one is found to be true (see section Boolean ' |
| 2201 | 'operations\n' |
| 2202 | 'for the definition of true and false); then that suite is ' |
| 2203 | 'executed\n' |
| 2204 | '(and no other part of the "if" statement is executed or ' |
| 2205 | 'evaluated).\n' |
| 2206 | 'If all expressions are false, the suite of the "else" clause, ' |
| 2207 | 'if\n' |
| 2208 | 'present, is executed.\n' |
| 2209 | '\n' |
| 2210 | '\n' |
| 2211 | 'The "while" statement\n' |
| 2212 | '=====================\n' |
| 2213 | '\n' |
| 2214 | 'The "while" statement is used for repeated execution as long as ' |
| 2215 | 'an\n' |
| 2216 | 'expression is true:\n' |
| 2217 | '\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 2218 | ' while_stmt ::= "while" assignment_expression ":" suite\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2219 | ' ["else" ":" suite]\n' |
| 2220 | '\n' |
| 2221 | 'This repeatedly tests the expression and, if it is true, ' |
| 2222 | 'executes the\n' |
| 2223 | 'first suite; if the expression is false (which may be the first ' |
| 2224 | 'time\n' |
| 2225 | 'it is tested) the suite of the "else" clause, if present, is ' |
| 2226 | 'executed\n' |
| 2227 | 'and the loop terminates.\n' |
| 2228 | '\n' |
| 2229 | 'A "break" statement executed in the first suite terminates the ' |
| 2230 | 'loop\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2231 | 'without executing the "else" clause’s suite. A "continue" ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2232 | 'statement\n' |
| 2233 | 'executed in the first suite skips the rest of the suite and goes ' |
| 2234 | 'back\n' |
| 2235 | 'to testing the expression.\n' |
| 2236 | '\n' |
| 2237 | '\n' |
| 2238 | 'The "for" statement\n' |
| 2239 | '===================\n' |
| 2240 | '\n' |
| 2241 | 'The "for" statement is used to iterate over the elements of a ' |
| 2242 | 'sequence\n' |
| 2243 | '(such as a string, tuple or list) or other iterable object:\n' |
| 2244 | '\n' |
| 2245 | ' for_stmt ::= "for" target_list "in" expression_list ":" ' |
| 2246 | 'suite\n' |
| 2247 | ' ["else" ":" suite]\n' |
| 2248 | '\n' |
| 2249 | 'The expression list is evaluated once; it should yield an ' |
| 2250 | 'iterable\n' |
| 2251 | 'object. An iterator is created for the result of the\n' |
| 2252 | '"expression_list". The suite is then executed once for each ' |
| 2253 | 'item\n' |
| 2254 | 'provided by the iterator, in the order returned by the ' |
| 2255 | 'iterator. Each\n' |
| 2256 | 'item in turn is assigned to the target list using the standard ' |
| 2257 | 'rules\n' |
| 2258 | 'for assignments (see Assignment statements), and then the suite ' |
| 2259 | 'is\n' |
| 2260 | 'executed. When the items are exhausted (which is immediately ' |
| 2261 | 'when the\n' |
| 2262 | 'sequence is empty or an iterator raises a "StopIteration" ' |
| 2263 | 'exception),\n' |
| 2264 | 'the suite in the "else" clause, if present, is executed, and the ' |
| 2265 | 'loop\n' |
| 2266 | 'terminates.\n' |
| 2267 | '\n' |
| 2268 | 'A "break" statement executed in the first suite terminates the ' |
| 2269 | 'loop\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2270 | 'without executing the "else" clause’s suite. A "continue" ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2271 | 'statement\n' |
| 2272 | 'executed in the first suite skips the rest of the suite and ' |
| 2273 | 'continues\n' |
| 2274 | 'with the next item, or with the "else" clause if there is no ' |
| 2275 | 'next\n' |
| 2276 | 'item.\n' |
| 2277 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2278 | 'The for-loop makes assignments to the variables in the target ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2279 | 'list.\n' |
| 2280 | 'This overwrites all previous assignments to those variables ' |
| 2281 | 'including\n' |
| 2282 | 'those made in the suite of the for-loop:\n' |
| 2283 | '\n' |
| 2284 | ' for i in range(10):\n' |
| 2285 | ' print(i)\n' |
| 2286 | ' i = 5 # this will not affect the for-loop\n' |
| 2287 | ' # because i will be overwritten with ' |
| 2288 | 'the next\n' |
| 2289 | ' # index in the range\n' |
| 2290 | '\n' |
| 2291 | 'Names in the target list are not deleted when the loop is ' |
| 2292 | 'finished,\n' |
| 2293 | 'but if the sequence is empty, they will not have been assigned ' |
| 2294 | 'to at\n' |
| 2295 | 'all by the loop. Hint: the built-in function "range()" returns ' |
| 2296 | 'an\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2297 | 'iterator of integers suitable to emulate the effect of Pascal’s ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2298 | '"for i\n' |
| 2299 | ':= a to b do"; e.g., "list(range(3))" returns the list "[0, 1, ' |
| 2300 | '2]".\n' |
| 2301 | '\n' |
| 2302 | 'Note: There is a subtlety when the sequence is being modified by ' |
| 2303 | 'the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2304 | ' loop (this can only occur for mutable sequences, e.g. lists). ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2305 | 'An\n' |
| 2306 | ' internal counter is used to keep track of which item is used ' |
| 2307 | 'next,\n' |
| 2308 | ' and this is incremented on each iteration. When this counter ' |
| 2309 | 'has\n' |
| 2310 | ' reached the length of the sequence the loop terminates. This ' |
| 2311 | 'means\n' |
| 2312 | ' that if the suite deletes the current (or a previous) item ' |
| 2313 | 'from the\n' |
| 2314 | ' sequence, the next item will be skipped (since it gets the ' |
| 2315 | 'index of\n' |
| 2316 | ' the current item which has already been treated). Likewise, ' |
| 2317 | 'if the\n' |
| 2318 | ' suite inserts an item in the sequence before the current item, ' |
| 2319 | 'the\n' |
| 2320 | ' current item will be treated again the next time through the ' |
| 2321 | 'loop.\n' |
| 2322 | ' This can lead to nasty bugs that can be avoided by making a\n' |
| 2323 | ' temporary copy using a slice of the whole sequence, e.g.,\n' |
| 2324 | '\n' |
| 2325 | ' for x in a[:]:\n' |
| 2326 | ' if x < 0: a.remove(x)\n' |
| 2327 | '\n' |
| 2328 | '\n' |
| 2329 | 'The "try" statement\n' |
| 2330 | '===================\n' |
| 2331 | '\n' |
| 2332 | 'The "try" statement specifies exception handlers and/or cleanup ' |
| 2333 | 'code\n' |
| 2334 | 'for a group of statements:\n' |
| 2335 | '\n' |
| 2336 | ' try_stmt ::= try1_stmt | try2_stmt\n' |
| 2337 | ' try1_stmt ::= "try" ":" suite\n' |
| 2338 | ' ("except" [expression ["as" identifier]] ":" ' |
| 2339 | 'suite)+\n' |
| 2340 | ' ["else" ":" suite]\n' |
| 2341 | ' ["finally" ":" suite]\n' |
| 2342 | ' try2_stmt ::= "try" ":" suite\n' |
| 2343 | ' "finally" ":" suite\n' |
| 2344 | '\n' |
| 2345 | 'The "except" clause(s) specify one or more exception handlers. ' |
| 2346 | 'When no\n' |
| 2347 | 'exception occurs in the "try" clause, no exception handler is\n' |
| 2348 | 'executed. When an exception occurs in the "try" suite, a search ' |
| 2349 | 'for an\n' |
| 2350 | 'exception handler is started. This search inspects the except ' |
| 2351 | 'clauses\n' |
| 2352 | 'in turn until one is found that matches the exception. An ' |
| 2353 | 'expression-\n' |
| 2354 | 'less except clause, if present, must be last; it matches any\n' |
| 2355 | 'exception. For an except clause with an expression, that ' |
| 2356 | 'expression\n' |
| 2357 | 'is evaluated, and the clause matches the exception if the ' |
| 2358 | 'resulting\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2359 | 'object is “compatible” with the exception. An object is ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2360 | 'compatible\n' |
| 2361 | 'with an exception if it is the class or a base class of the ' |
| 2362 | 'exception\n' |
| 2363 | 'object or a tuple containing an item compatible with the ' |
| 2364 | 'exception.\n' |
| 2365 | '\n' |
| 2366 | 'If no except clause matches the exception, the search for an ' |
| 2367 | 'exception\n' |
| 2368 | 'handler continues in the surrounding code and on the invocation ' |
| 2369 | 'stack.\n' |
| 2370 | '[1]\n' |
| 2371 | '\n' |
| 2372 | 'If the evaluation of an expression in the header of an except ' |
| 2373 | 'clause\n' |
| 2374 | 'raises an exception, the original search for a handler is ' |
| 2375 | 'canceled and\n' |
| 2376 | 'a search starts for the new exception in the surrounding code ' |
| 2377 | 'and on\n' |
| 2378 | 'the call stack (it is treated as if the entire "try" statement ' |
| 2379 | 'raised\n' |
| 2380 | 'the exception).\n' |
| 2381 | '\n' |
| 2382 | 'When a matching except clause is found, the exception is ' |
| 2383 | 'assigned to\n' |
| 2384 | 'the target specified after the "as" keyword in that except ' |
| 2385 | 'clause, if\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2386 | 'present, and the except clause’s suite is executed. All except\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2387 | 'clauses must have an executable block. When the end of this ' |
| 2388 | 'block is\n' |
| 2389 | 'reached, execution continues normally after the entire try ' |
| 2390 | 'statement.\n' |
| 2391 | '(This means that if two nested handlers exist for the same ' |
| 2392 | 'exception,\n' |
| 2393 | 'and the exception occurs in the try clause of the inner handler, ' |
| 2394 | 'the\n' |
| 2395 | 'outer handler will not handle the exception.)\n' |
| 2396 | '\n' |
| 2397 | 'When an exception has been assigned using "as target", it is ' |
| 2398 | 'cleared\n' |
| 2399 | 'at the end of the except clause. This is as if\n' |
| 2400 | '\n' |
| 2401 | ' except E as N:\n' |
| 2402 | ' foo\n' |
| 2403 | '\n' |
| 2404 | 'was translated to\n' |
| 2405 | '\n' |
| 2406 | ' except E as N:\n' |
| 2407 | ' try:\n' |
| 2408 | ' foo\n' |
| 2409 | ' finally:\n' |
| 2410 | ' del N\n' |
| 2411 | '\n' |
| 2412 | 'This means the exception must be assigned to a different name to ' |
| 2413 | 'be\n' |
| 2414 | 'able to refer to it after the except clause. Exceptions are ' |
| 2415 | 'cleared\n' |
| 2416 | 'because with the traceback attached to them, they form a ' |
| 2417 | 'reference\n' |
| 2418 | 'cycle with the stack frame, keeping all locals in that frame ' |
| 2419 | 'alive\n' |
| 2420 | 'until the next garbage collection occurs.\n' |
| 2421 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2422 | 'Before an except clause’s suite is executed, details about the\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2423 | 'exception are stored in the "sys" module and can be accessed ' |
| 2424 | 'via\n' |
| 2425 | '"sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting ' |
| 2426 | 'of the\n' |
| 2427 | 'exception class, the exception instance and a traceback object ' |
| 2428 | '(see\n' |
| 2429 | 'section The standard type hierarchy) identifying the point in ' |
| 2430 | 'the\n' |
| 2431 | 'program where the exception occurred. "sys.exc_info()" values ' |
| 2432 | 'are\n' |
| 2433 | 'restored to their previous values (before the call) when ' |
| 2434 | 'returning\n' |
| 2435 | 'from a function that handled an exception.\n' |
| 2436 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2437 | 'The optional "else" clause is executed if the control flow ' |
| 2438 | 'leaves the\n' |
| 2439 | '"try" suite, no exception was raised, and no "return", ' |
| 2440 | '"continue", or\n' |
| 2441 | '"break" statement was executed. Exceptions in the "else" clause ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2442 | 'are\n' |
| 2443 | 'not handled by the preceding "except" clauses.\n' |
| 2444 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2445 | 'If "finally" is present, it specifies a ‘cleanup’ handler. The ' |
| 2446 | '"try"\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2447 | 'clause is executed, including any "except" and "else" clauses. ' |
| 2448 | 'If an\n' |
| 2449 | 'exception occurs in any of the clauses and is not handled, the\n' |
| 2450 | 'exception is temporarily saved. The "finally" clause is ' |
| 2451 | 'executed. If\n' |
| 2452 | 'there is a saved exception it is re-raised at the end of the ' |
| 2453 | '"finally"\n' |
| 2454 | 'clause. If the "finally" clause raises another exception, the ' |
| 2455 | 'saved\n' |
| 2456 | 'exception is set as the context of the new exception. If the ' |
| 2457 | '"finally"\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2458 | 'clause executes a "return", "break" or "continue" statement, the ' |
| 2459 | 'saved\n' |
| 2460 | 'exception is discarded:\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2461 | '\n' |
| 2462 | ' >>> def f():\n' |
| 2463 | ' ... try:\n' |
| 2464 | ' ... 1/0\n' |
| 2465 | ' ... finally:\n' |
| 2466 | ' ... return 42\n' |
| 2467 | ' ...\n' |
| 2468 | ' >>> f()\n' |
| 2469 | ' 42\n' |
| 2470 | '\n' |
| 2471 | 'The exception information is not available to the program ' |
| 2472 | 'during\n' |
| 2473 | 'execution of the "finally" clause.\n' |
| 2474 | '\n' |
| 2475 | 'When a "return", "break" or "continue" statement is executed in ' |
| 2476 | 'the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2477 | '"try" suite of a "try"…"finally" statement, the "finally" clause ' |
| 2478 | 'is\n' |
| 2479 | 'also executed ‘on the way out.’\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2480 | '\n' |
| 2481 | 'The return value of a function is determined by the last ' |
| 2482 | '"return"\n' |
| 2483 | 'statement executed. Since the "finally" clause always executes, ' |
| 2484 | 'a\n' |
| 2485 | '"return" statement executed in the "finally" clause will always ' |
| 2486 | 'be the\n' |
| 2487 | 'last one executed:\n' |
| 2488 | '\n' |
| 2489 | ' >>> def foo():\n' |
| 2490 | ' ... try:\n' |
| 2491 | " ... return 'try'\n" |
| 2492 | ' ... finally:\n' |
| 2493 | " ... return 'finally'\n" |
| 2494 | ' ...\n' |
| 2495 | ' >>> foo()\n' |
| 2496 | " 'finally'\n" |
| 2497 | '\n' |
| 2498 | 'Additional information on exceptions can be found in section\n' |
| 2499 | 'Exceptions, and information on using the "raise" statement to ' |
| 2500 | 'generate\n' |
| 2501 | 'exceptions may be found in section The raise statement.\n' |
| 2502 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2503 | 'Changed in version 3.8: Prior to Python 3.8, a "continue" ' |
| 2504 | 'statement\n' |
| 2505 | 'was illegal in the "finally" clause due to a problem with the\n' |
| 2506 | 'implementation.\n' |
| 2507 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2508 | '\n' |
| 2509 | 'The "with" statement\n' |
| 2510 | '====================\n' |
| 2511 | '\n' |
| 2512 | 'The "with" statement is used to wrap the execution of a block ' |
| 2513 | 'with\n' |
| 2514 | 'methods defined by a context manager (see section With ' |
| 2515 | 'Statement\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2516 | 'Context Managers). This allows common "try"…"except"…"finally" ' |
| 2517 | 'usage\n' |
| 2518 | 'patterns to be encapsulated for convenient reuse.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2519 | '\n' |
| 2520 | ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n' |
| 2521 | ' with_item ::= expression ["as" target]\n' |
| 2522 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2523 | 'The execution of the "with" statement with one “item” proceeds ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2524 | 'as\n' |
| 2525 | 'follows:\n' |
| 2526 | '\n' |
| 2527 | '1. The context expression (the expression given in the ' |
| 2528 | '"with_item")\n' |
| 2529 | ' is evaluated to obtain a context manager.\n' |
| 2530 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 2531 | '2. The context manager’s "__enter__()" is loaded for later use.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2532 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 2533 | '3. The context manager’s "__exit__()" is loaded for later use.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2534 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 2535 | '4. The context manager’s "__enter__()" method is invoked.\n' |
| 2536 | '\n' |
| 2537 | '5. If a target was included in the "with" statement, the return\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2538 | ' value from "__enter__()" is assigned to it.\n' |
| 2539 | '\n' |
| 2540 | ' Note: The "with" statement guarantees that if the ' |
| 2541 | '"__enter__()"\n' |
| 2542 | ' method returns without an error, then "__exit__()" will ' |
| 2543 | 'always be\n' |
| 2544 | ' called. Thus, if an error occurs during the assignment to ' |
| 2545 | 'the\n' |
| 2546 | ' target list, it will be treated the same as an error ' |
| 2547 | 'occurring\n' |
| 2548 | ' within the suite would be. See step 6 below.\n' |
| 2549 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 2550 | '6. The suite is executed.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2551 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 2552 | '7. The context manager’s "__exit__()" method is invoked. If an\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2553 | ' exception caused the suite to be exited, its type, value, ' |
| 2554 | 'and\n' |
| 2555 | ' traceback are passed as arguments to "__exit__()". Otherwise, ' |
| 2556 | 'three\n' |
| 2557 | ' "None" arguments are supplied.\n' |
| 2558 | '\n' |
| 2559 | ' If the suite was exited due to an exception, and the return ' |
| 2560 | 'value\n' |
| 2561 | ' from the "__exit__()" method was false, the exception is ' |
| 2562 | 'reraised.\n' |
| 2563 | ' If the return value was true, the exception is suppressed, ' |
| 2564 | 'and\n' |
| 2565 | ' execution continues with the statement following the "with"\n' |
| 2566 | ' statement.\n' |
| 2567 | '\n' |
| 2568 | ' If the suite was exited for any reason other than an ' |
| 2569 | 'exception, the\n' |
| 2570 | ' return value from "__exit__()" is ignored, and execution ' |
| 2571 | 'proceeds\n' |
| 2572 | ' at the normal location for the kind of exit that was taken.\n' |
| 2573 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 2574 | 'The following code:\n' |
| 2575 | '\n' |
| 2576 | ' with EXPRESSION as TARGET:\n' |
| 2577 | ' SUITE\n' |
| 2578 | '\n' |
| 2579 | 'is semantically equivalent to:\n' |
| 2580 | '\n' |
| 2581 | ' manager = (EXPRESSION)\n' |
| 2582 | ' enter = type(manager).__enter__\n' |
| 2583 | ' exit = type(manager).__exit__\n' |
| 2584 | ' value = enter(manager)\n' |
| 2585 | ' hit_except = False\n' |
| 2586 | '\n' |
| 2587 | ' try:\n' |
| 2588 | ' TARGET = value\n' |
| 2589 | ' SUITE\n' |
| 2590 | ' except:\n' |
| 2591 | ' hit_except = True\n' |
| 2592 | ' if not exit(manager, *sys.exc_info()):\n' |
| 2593 | ' raise\n' |
| 2594 | ' finally:\n' |
| 2595 | ' if not hit_except:\n' |
| 2596 | ' exit(manager, None, None, None)\n' |
| 2597 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2598 | 'With more than one item, the context managers are processed as ' |
| 2599 | 'if\n' |
| 2600 | 'multiple "with" statements were nested:\n' |
| 2601 | '\n' |
| 2602 | ' with A() as a, B() as b:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 2603 | ' SUITE\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2604 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 2605 | 'is semantically equivalent to:\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2606 | '\n' |
| 2607 | ' with A() as a:\n' |
| 2608 | ' with B() as b:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 2609 | ' SUITE\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2610 | '\n' |
| 2611 | 'Changed in version 3.1: Support for multiple context ' |
| 2612 | 'expressions.\n' |
| 2613 | '\n' |
| 2614 | 'See also:\n' |
| 2615 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2616 | ' **PEP 343** - The “with” statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2617 | ' The specification, background, and examples for the Python ' |
| 2618 | '"with"\n' |
| 2619 | ' statement.\n' |
| 2620 | '\n' |
| 2621 | '\n' |
| 2622 | 'Function definitions\n' |
| 2623 | '====================\n' |
| 2624 | '\n' |
| 2625 | 'A function definition defines a user-defined function object ' |
| 2626 | '(see\n' |
| 2627 | 'section The standard type hierarchy):\n' |
| 2628 | '\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 2629 | ' funcdef ::= [decorators] "def" funcname "(" ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2630 | '[parameter_list] ")"\n' |
| 2631 | ' ["->" expression] ":" suite\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 2632 | ' decorators ::= decorator+\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 2633 | ' decorator ::= "@" assignment_expression ' |
| 2634 | 'NEWLINE\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 2635 | ' dotted_name ::= identifier ("." identifier)*\n' |
| 2636 | ' parameter_list ::= defparameter ("," ' |
| 2637 | 'defparameter)* "," "/" ["," [parameter_list_no_posonly]]\n' |
| 2638 | ' | parameter_list_no_posonly\n' |
| 2639 | ' parameter_list_no_posonly ::= defparameter ("," ' |
| 2640 | 'defparameter)* ["," [parameter_list_starargs]]\n' |
| 2641 | ' | parameter_list_starargs\n' |
| 2642 | ' parameter_list_starargs ::= "*" [parameter] ("," ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2643 | 'defparameter)* ["," ["**" parameter [","]]]\n' |
| 2644 | ' | "**" parameter [","]\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 2645 | ' parameter ::= identifier [":" expression]\n' |
| 2646 | ' defparameter ::= parameter ["=" expression]\n' |
| 2647 | ' funcname ::= identifier\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2648 | '\n' |
| 2649 | 'A function definition is an executable statement. Its execution ' |
| 2650 | 'binds\n' |
| 2651 | 'the function name in the current local namespace to a function ' |
| 2652 | 'object\n' |
| 2653 | '(a wrapper around the executable code for the function). This\n' |
| 2654 | 'function object contains a reference to the current global ' |
| 2655 | 'namespace\n' |
| 2656 | 'as the global namespace to be used when the function is called.\n' |
| 2657 | '\n' |
| 2658 | 'The function definition does not execute the function body; this ' |
| 2659 | 'gets\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2660 | 'executed only when the function is called. [2]\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2661 | '\n' |
| 2662 | 'A function definition may be wrapped by one or more *decorator*\n' |
| 2663 | 'expressions. Decorator expressions are evaluated when the ' |
| 2664 | 'function is\n' |
| 2665 | 'defined, in the scope that contains the function definition. ' |
| 2666 | 'The\n' |
| 2667 | 'result must be a callable, which is invoked with the function ' |
| 2668 | 'object\n' |
| 2669 | 'as the only argument. The returned value is bound to the ' |
| 2670 | 'function name\n' |
| 2671 | 'instead of the function object. Multiple decorators are applied ' |
| 2672 | 'in\n' |
| 2673 | 'nested fashion. For example, the following code\n' |
| 2674 | '\n' |
| 2675 | ' @f1(arg)\n' |
| 2676 | ' @f2\n' |
| 2677 | ' def func(): pass\n' |
| 2678 | '\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 2679 | 'is roughly equivalent to\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2680 | '\n' |
| 2681 | ' def func(): pass\n' |
| 2682 | ' func = f1(arg)(f2(func))\n' |
| 2683 | '\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 2684 | 'except that the original function is not temporarily bound to ' |
| 2685 | 'the name\n' |
| 2686 | '"func".\n' |
| 2687 | '\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 2688 | 'Changed in version 3.9: Functions may be decorated with any ' |
| 2689 | 'valid\n' |
| 2690 | '"assignment_expression". Previously, the grammar was much more\n' |
| 2691 | 'restrictive; see **PEP 614** for details.\n' |
| 2692 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2693 | 'When one or more *parameters* have the form *parameter* "="\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2694 | '*expression*, the function is said to have “default parameter ' |
| 2695 | 'values.”\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2696 | 'For a parameter with a default value, the corresponding ' |
| 2697 | '*argument* may\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2698 | 'be omitted from a call, in which case the parameter’s default ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2699 | 'value is\n' |
| 2700 | 'substituted. If a parameter has a default value, all following\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2701 | 'parameters up until the “"*"” must also have a default value — ' |
| 2702 | 'this is\n' |
| 2703 | 'a syntactic restriction that is not expressed by the grammar.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2704 | '\n' |
| 2705 | '**Default parameter values are evaluated from left to right when ' |
| 2706 | 'the\n' |
| 2707 | 'function definition is executed.** This means that the ' |
| 2708 | 'expression is\n' |
| 2709 | 'evaluated once, when the function is defined, and that the same ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2710 | '“pre-\n' |
| 2711 | 'computed” value is used for each call. This is especially ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2712 | 'important\n' |
| 2713 | 'to understand when a default parameter is a mutable object, such ' |
| 2714 | 'as a\n' |
| 2715 | 'list or a dictionary: if the function modifies the object (e.g. ' |
| 2716 | 'by\n' |
| 2717 | 'appending an item to a list), the default value is in effect ' |
| 2718 | 'modified.\n' |
| 2719 | 'This is generally not what was intended. A way around this is ' |
| 2720 | 'to use\n' |
| 2721 | '"None" as the default, and explicitly test for it in the body of ' |
| 2722 | 'the\n' |
| 2723 | 'function, e.g.:\n' |
| 2724 | '\n' |
| 2725 | ' def whats_on_the_telly(penguin=None):\n' |
| 2726 | ' if penguin is None:\n' |
| 2727 | ' penguin = []\n' |
| 2728 | ' penguin.append("property of the zoo")\n' |
| 2729 | ' return penguin\n' |
| 2730 | '\n' |
| 2731 | 'Function call semantics are described in more detail in section ' |
| 2732 | 'Calls.\n' |
| 2733 | 'A function call always assigns values to all parameters ' |
| 2734 | 'mentioned in\n' |
| 2735 | 'the parameter list, either from position arguments, from ' |
| 2736 | 'keyword\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2737 | 'arguments, or from default values. If the form “"*identifier"” ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2738 | 'is\n' |
| 2739 | 'present, it is initialized to a tuple receiving any excess ' |
| 2740 | 'positional\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 2741 | 'parameters, defaulting to the empty tuple. If the form\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2742 | '“"**identifier"” is present, it is initialized to a new ordered\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 2743 | 'mapping receiving any excess keyword arguments, defaulting to a ' |
| 2744 | 'new\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2745 | 'empty mapping of the same type. Parameters after “"*"” or\n' |
| 2746 | '“"*identifier"” are keyword-only parameters and may only be ' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 2747 | 'passed\n' |
| 2748 | 'used keyword arguments.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2749 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2750 | 'Parameters may have an *annotation* of the form “": ' |
| 2751 | 'expression"”\n' |
| 2752 | 'following the parameter name. Any parameter may have an ' |
| 2753 | 'annotation,\n' |
| 2754 | 'even those of the form "*identifier" or "**identifier". ' |
| 2755 | 'Functions may\n' |
| 2756 | 'have “return” annotation of the form “"-> expression"” after ' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 2757 | 'the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2758 | 'parameter list. These annotations can be any valid Python ' |
| 2759 | 'expression.\n' |
| 2760 | 'The presence of annotations does not change the semantics of a\n' |
| 2761 | 'function. The annotation values are available as values of a\n' |
| 2762 | 'dictionary keyed by the parameters’ names in the ' |
| 2763 | '"__annotations__"\n' |
| 2764 | 'attribute of the function object. If the "annotations" import ' |
| 2765 | 'from\n' |
| 2766 | '"__future__" is used, annotations are preserved as strings at ' |
| 2767 | 'runtime\n' |
| 2768 | 'which enables postponed evaluation. Otherwise, they are ' |
| 2769 | 'evaluated\n' |
| 2770 | 'when the function definition is executed. In this case ' |
| 2771 | 'annotations\n' |
| 2772 | 'may be evaluated in a different order than they appear in the ' |
| 2773 | 'source\n' |
| 2774 | 'code.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2775 | '\n' |
| 2776 | 'It is also possible to create anonymous functions (functions not ' |
| 2777 | 'bound\n' |
| 2778 | 'to a name), for immediate use in expressions. This uses lambda\n' |
| 2779 | 'expressions, described in section Lambdas. Note that the ' |
| 2780 | 'lambda\n' |
| 2781 | 'expression is merely a shorthand for a simplified function ' |
| 2782 | 'definition;\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2783 | 'a function defined in a “"def"” statement can be passed around ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2784 | 'or\n' |
| 2785 | 'assigned to another name just like a function defined by a ' |
| 2786 | 'lambda\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2787 | 'expression. The “"def"” form is actually more powerful since ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2788 | 'it\n' |
| 2789 | 'allows the execution of multiple statements and annotations.\n' |
| 2790 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2791 | '**Programmer’s note:** Functions are first-class objects. A ' |
| 2792 | '“"def"”\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2793 | 'statement executed inside a function definition defines a local\n' |
| 2794 | 'function that can be returned or passed around. Free variables ' |
| 2795 | 'used\n' |
| 2796 | 'in the nested function can access the local variables of the ' |
| 2797 | 'function\n' |
| 2798 | 'containing the def. See section Naming and binding for ' |
| 2799 | 'details.\n' |
| 2800 | '\n' |
| 2801 | 'See also:\n' |
| 2802 | '\n' |
| 2803 | ' **PEP 3107** - Function Annotations\n' |
| 2804 | ' The original specification for function annotations.\n' |
| 2805 | '\n' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 2806 | ' **PEP 484** - Type Hints\n' |
| 2807 | ' Definition of a standard meaning for annotations: type ' |
| 2808 | 'hints.\n' |
| 2809 | '\n' |
| 2810 | ' **PEP 526** - Syntax for Variable Annotations\n' |
| 2811 | ' Ability to type hint variable declarations, including ' |
| 2812 | 'class\n' |
| 2813 | ' variables and instance variables\n' |
| 2814 | '\n' |
| 2815 | ' **PEP 563** - Postponed Evaluation of Annotations\n' |
| 2816 | ' Support for forward references within annotations by ' |
| 2817 | 'preserving\n' |
| 2818 | ' annotations in a string form at runtime instead of eager\n' |
| 2819 | ' evaluation.\n' |
| 2820 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2821 | '\n' |
| 2822 | 'Class definitions\n' |
| 2823 | '=================\n' |
| 2824 | '\n' |
| 2825 | 'A class definition defines a class object (see section The ' |
| 2826 | 'standard\n' |
| 2827 | 'type hierarchy):\n' |
| 2828 | '\n' |
| 2829 | ' classdef ::= [decorators] "class" classname [inheritance] ' |
| 2830 | '":" suite\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 2831 | ' inheritance ::= "(" [argument_list] ")"\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2832 | ' classname ::= identifier\n' |
| 2833 | '\n' |
| 2834 | 'A class definition is an executable statement. The inheritance ' |
| 2835 | 'list\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 2836 | 'usually gives a list of base classes (see Metaclasses for more\n' |
| 2837 | 'advanced uses), so each item in the list should evaluate to a ' |
| 2838 | 'class\n' |
| 2839 | 'object which allows subclassing. Classes without an inheritance ' |
| 2840 | 'list\n' |
| 2841 | 'inherit, by default, from the base class "object"; hence,\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2842 | '\n' |
| 2843 | ' class Foo:\n' |
| 2844 | ' pass\n' |
| 2845 | '\n' |
| 2846 | 'is equivalent to\n' |
| 2847 | '\n' |
| 2848 | ' class Foo(object):\n' |
| 2849 | ' pass\n' |
| 2850 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2851 | 'The class’s suite is then executed in a new execution frame ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2852 | '(see\n' |
| 2853 | 'Naming and binding), using a newly created local namespace and ' |
| 2854 | 'the\n' |
| 2855 | 'original global namespace. (Usually, the suite contains mostly\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2856 | 'function definitions.) When the class’s suite finishes ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2857 | 'execution, its\n' |
| 2858 | 'execution frame is discarded but its local namespace is saved. ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2859 | '[3] A\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2860 | 'class object is then created using the inheritance list for the ' |
| 2861 | 'base\n' |
| 2862 | 'classes and the saved local namespace for the attribute ' |
| 2863 | 'dictionary.\n' |
| 2864 | 'The class name is bound to this class object in the original ' |
| 2865 | 'local\n' |
| 2866 | 'namespace.\n' |
| 2867 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 2868 | 'The order in which attributes are defined in the class body is\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2869 | 'preserved in the new class’s "__dict__". Note that this is ' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 2870 | 'reliable\n' |
| 2871 | 'only right after the class is created and only for classes that ' |
| 2872 | 'were\n' |
| 2873 | 'defined using the definition syntax.\n' |
| 2874 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2875 | 'Class creation can be customized heavily using metaclasses.\n' |
| 2876 | '\n' |
| 2877 | 'Classes can also be decorated: just like when decorating ' |
| 2878 | 'functions,\n' |
| 2879 | '\n' |
| 2880 | ' @f1(arg)\n' |
| 2881 | ' @f2\n' |
| 2882 | ' class Foo: pass\n' |
| 2883 | '\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 2884 | 'is roughly equivalent to\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2885 | '\n' |
| 2886 | ' class Foo: pass\n' |
| 2887 | ' Foo = f1(arg)(f2(Foo))\n' |
| 2888 | '\n' |
| 2889 | 'The evaluation rules for the decorator expressions are the same ' |
| 2890 | 'as for\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 2891 | 'function decorators. The result is then bound to the class ' |
| 2892 | 'name.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2893 | '\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 2894 | 'Changed in version 3.9: Classes may be decorated with any valid\n' |
| 2895 | '"assignment_expression". Previously, the grammar was much more\n' |
| 2896 | 'restrictive; see **PEP 614** for details.\n' |
| 2897 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2898 | '**Programmer’s note:** Variables defined in the class definition ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2899 | 'are\n' |
| 2900 | 'class attributes; they are shared by instances. Instance ' |
| 2901 | 'attributes\n' |
| 2902 | 'can be set in a method with "self.name = value". Both class ' |
| 2903 | 'and\n' |
| 2904 | 'instance attributes are accessible through the notation ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2905 | '“"self.name"”,\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2906 | 'and an instance attribute hides a class attribute with the same ' |
| 2907 | 'name\n' |
| 2908 | 'when accessed in this way. Class attributes can be used as ' |
| 2909 | 'defaults\n' |
| 2910 | 'for instance attributes, but using mutable values there can lead ' |
| 2911 | 'to\n' |
| 2912 | 'unexpected results. Descriptors can be used to create instance\n' |
| 2913 | 'variables with different implementation details.\n' |
| 2914 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2915 | 'See also:\n' |
| 2916 | '\n' |
| 2917 | ' **PEP 3115** - Metaclasses in Python 3000\n' |
| 2918 | ' The proposal that changed the declaration of metaclasses to ' |
| 2919 | 'the\n' |
| 2920 | ' current syntax, and the semantics for how classes with\n' |
| 2921 | ' metaclasses are constructed.\n' |
| 2922 | '\n' |
| 2923 | ' **PEP 3129** - Class Decorators\n' |
| 2924 | ' The proposal that added class decorators. Function and ' |
| 2925 | 'method\n' |
| 2926 | ' decorators were introduced in **PEP 318**.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2927 | '\n' |
| 2928 | '\n' |
| 2929 | 'Coroutines\n' |
| 2930 | '==========\n' |
| 2931 | '\n' |
| 2932 | 'New in version 3.5.\n' |
| 2933 | '\n' |
| 2934 | '\n' |
| 2935 | 'Coroutine function definition\n' |
| 2936 | '-----------------------------\n' |
| 2937 | '\n' |
| 2938 | ' async_funcdef ::= [decorators] "async" "def" funcname "(" ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2939 | '[parameter_list] ")"\n' |
| 2940 | ' ["->" expression] ":" suite\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2941 | '\n' |
| 2942 | 'Execution of Python coroutines can be suspended and resumed at ' |
| 2943 | 'many\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2944 | 'points (see *coroutine*). Inside the body of a coroutine ' |
| 2945 | 'function,\n' |
| 2946 | '"await" and "async" identifiers become reserved keywords; ' |
| 2947 | '"await"\n' |
| 2948 | 'expressions, "async for" and "async with" can only be used in\n' |
| 2949 | 'coroutine function bodies.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2950 | '\n' |
| 2951 | 'Functions defined with "async def" syntax are always coroutine\n' |
| 2952 | 'functions, even if they do not contain "await" or "async" ' |
| 2953 | 'keywords.\n' |
| 2954 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 2955 | 'It is a "SyntaxError" to use a "yield from" expression inside ' |
| 2956 | 'the body\n' |
| 2957 | 'of a coroutine function.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2958 | '\n' |
| 2959 | 'An example of a coroutine function:\n' |
| 2960 | '\n' |
| 2961 | ' async def func(param1, param2):\n' |
| 2962 | ' do_stuff()\n' |
| 2963 | ' await some_coroutine()\n' |
| 2964 | '\n' |
| 2965 | '\n' |
| 2966 | 'The "async for" statement\n' |
| 2967 | '-------------------------\n' |
| 2968 | '\n' |
| 2969 | ' async_for_stmt ::= "async" for_stmt\n' |
| 2970 | '\n' |
| 2971 | 'An *asynchronous iterable* is able to call asynchronous code in ' |
| 2972 | 'its\n' |
| 2973 | '*iter* implementation, and *asynchronous iterator* can call\n' |
| 2974 | 'asynchronous code in its *next* method.\n' |
| 2975 | '\n' |
| 2976 | 'The "async for" statement allows convenient iteration over\n' |
| 2977 | 'asynchronous iterators.\n' |
| 2978 | '\n' |
| 2979 | 'The following code:\n' |
| 2980 | '\n' |
| 2981 | ' async for TARGET in ITER:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 2982 | ' SUITE\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2983 | ' else:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 2984 | ' SUITE2\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2985 | '\n' |
| 2986 | 'Is semantically equivalent to:\n' |
| 2987 | '\n' |
| 2988 | ' iter = (ITER)\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 2989 | ' iter = type(iter).__aiter__(iter)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2990 | ' running = True\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 2991 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2992 | ' while running:\n' |
| 2993 | ' try:\n' |
| 2994 | ' TARGET = await type(iter).__anext__(iter)\n' |
| 2995 | ' except StopAsyncIteration:\n' |
| 2996 | ' running = False\n' |
| 2997 | ' else:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 2998 | ' SUITE\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 2999 | ' else:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 3000 | ' SUITE2\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3001 | '\n' |
| 3002 | 'See also "__aiter__()" and "__anext__()" for details.\n' |
| 3003 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3004 | 'It is a "SyntaxError" to use an "async for" statement outside ' |
| 3005 | 'the body\n' |
| 3006 | 'of a coroutine function.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3007 | '\n' |
| 3008 | '\n' |
| 3009 | 'The "async with" statement\n' |
| 3010 | '--------------------------\n' |
| 3011 | '\n' |
| 3012 | ' async_with_stmt ::= "async" with_stmt\n' |
| 3013 | '\n' |
| 3014 | 'An *asynchronous context manager* is a *context manager* that is ' |
| 3015 | 'able\n' |
| 3016 | 'to suspend execution in its *enter* and *exit* methods.\n' |
| 3017 | '\n' |
| 3018 | 'The following code:\n' |
| 3019 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 3020 | ' async with EXPRESSION as TARGET:\n' |
| 3021 | ' SUITE\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3022 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 3023 | 'is semantically equivalent to:\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3024 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 3025 | ' manager = (EXPRESSION)\n' |
| 3026 | ' aenter = type(manager).__aenter__\n' |
| 3027 | ' aexit = type(manager).__aexit__\n' |
| 3028 | ' value = await aenter(manager)\n' |
| 3029 | ' hit_except = False\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3030 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3031 | ' try:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 3032 | ' TARGET = value\n' |
| 3033 | ' SUITE\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3034 | ' except:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 3035 | ' hit_except = True\n' |
| 3036 | ' if not await aexit(manager, *sys.exc_info()):\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3037 | ' raise\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 3038 | ' finally:\n' |
| 3039 | ' if not hit_except:\n' |
| 3040 | ' await aexit(manager, None, None, None)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3041 | '\n' |
| 3042 | 'See also "__aenter__()" and "__aexit__()" for details.\n' |
| 3043 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3044 | 'It is a "SyntaxError" to use an "async with" statement outside ' |
| 3045 | 'the\n' |
| 3046 | 'body of a coroutine function.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3047 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3048 | 'See also:\n' |
| 3049 | '\n' |
| 3050 | ' **PEP 492** - Coroutines with async and await syntax\n' |
| 3051 | ' The proposal that made coroutines a proper standalone ' |
| 3052 | 'concept in\n' |
| 3053 | ' Python, and added supporting syntax.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3054 | '\n' |
| 3055 | '-[ Footnotes ]-\n' |
| 3056 | '\n' |
| 3057 | '[1] The exception is propagated to the invocation stack unless\n' |
| 3058 | ' there is a "finally" clause which happens to raise another\n' |
| 3059 | ' exception. That new exception causes the old one to be ' |
| 3060 | 'lost.\n' |
| 3061 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3062 | '[2] A string literal appearing as the first statement in the\n' |
| 3063 | ' function body is transformed into the function’s "__doc__"\n' |
| 3064 | ' attribute and therefore the function’s *docstring*.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3065 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3066 | '[3] A string literal appearing as the first statement in the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3067 | 'class\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3068 | ' body is transformed into the namespace’s "__doc__" item and\n' |
| 3069 | ' therefore the class’s *docstring*.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3070 | 'context-managers': 'With Statement Context Managers\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3071 | '*******************************\n' |
| 3072 | '\n' |
| 3073 | 'A *context manager* is an object that defines the ' |
| 3074 | 'runtime context to\n' |
| 3075 | 'be established when executing a "with" statement. The ' |
| 3076 | 'context manager\n' |
| 3077 | 'handles the entry into, and the exit from, the desired ' |
| 3078 | 'runtime context\n' |
| 3079 | 'for the execution of the block of code. Context ' |
| 3080 | 'managers are normally\n' |
| 3081 | 'invoked using the "with" statement (described in section ' |
| 3082 | 'The with\n' |
| 3083 | 'statement), but can also be used by directly invoking ' |
| 3084 | 'their methods.\n' |
| 3085 | '\n' |
| 3086 | 'Typical uses of context managers include saving and ' |
| 3087 | 'restoring various\n' |
| 3088 | 'kinds of global state, locking and unlocking resources, ' |
| 3089 | 'closing opened\n' |
| 3090 | 'files, etc.\n' |
| 3091 | '\n' |
| 3092 | 'For more information on context managers, see Context ' |
| 3093 | 'Manager Types.\n' |
| 3094 | '\n' |
| 3095 | 'object.__enter__(self)\n' |
| 3096 | '\n' |
| 3097 | ' Enter the runtime context related to this object. The ' |
| 3098 | '"with"\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3099 | ' statement will bind this method’s return value to the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3100 | 'target(s)\n' |
| 3101 | ' specified in the "as" clause of the statement, if ' |
| 3102 | 'any.\n' |
| 3103 | '\n' |
| 3104 | 'object.__exit__(self, exc_type, exc_value, traceback)\n' |
| 3105 | '\n' |
| 3106 | ' Exit the runtime context related to this object. The ' |
| 3107 | 'parameters\n' |
| 3108 | ' describe the exception that caused the context to be ' |
| 3109 | 'exited. If the\n' |
| 3110 | ' context was exited without an exception, all three ' |
| 3111 | 'arguments will\n' |
| 3112 | ' be "None".\n' |
| 3113 | '\n' |
| 3114 | ' If an exception is supplied, and the method wishes to ' |
| 3115 | 'suppress the\n' |
| 3116 | ' exception (i.e., prevent it from being propagated), ' |
| 3117 | 'it should\n' |
| 3118 | ' return a true value. Otherwise, the exception will be ' |
| 3119 | 'processed\n' |
| 3120 | ' normally upon exit from this method.\n' |
| 3121 | '\n' |
| 3122 | ' Note that "__exit__()" methods should not reraise the ' |
| 3123 | 'passed-in\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3124 | ' exception; this is the caller’s responsibility.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3125 | '\n' |
| 3126 | 'See also:\n' |
| 3127 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3128 | ' **PEP 343** - The “with” statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3129 | ' The specification, background, and examples for the ' |
| 3130 | 'Python "with"\n' |
| 3131 | ' statement.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3132 | 'continue': 'The "continue" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3133 | '************************\n' |
| 3134 | '\n' |
| 3135 | ' continue_stmt ::= "continue"\n' |
| 3136 | '\n' |
| 3137 | '"continue" may only occur syntactically nested in a "for" or ' |
| 3138 | '"while"\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3139 | 'loop, but not nested in a function or class definition within ' |
| 3140 | 'that\n' |
| 3141 | 'loop. It continues with the next cycle of the nearest enclosing ' |
| 3142 | 'loop.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3143 | '\n' |
| 3144 | 'When "continue" passes control out of a "try" statement with a\n' |
| 3145 | '"finally" clause, that "finally" clause is executed before ' |
| 3146 | 'really\n' |
| 3147 | 'starting the next loop cycle.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3148 | 'conversions': 'Arithmetic conversions\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3149 | '**********************\n' |
| 3150 | '\n' |
| 3151 | 'When a description of an arithmetic operator below uses the ' |
| 3152 | 'phrase\n' |
Łukasz Langa | bc1c8af | 2020-04-27 22:44:04 +0200 | [diff] [blame] | 3153 | '“the numeric arguments are converted to a common type”, this ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3154 | 'means\n' |
| 3155 | 'that the operator implementation for built-in types works as ' |
| 3156 | 'follows:\n' |
| 3157 | '\n' |
| 3158 | '* If either argument is a complex number, the other is ' |
| 3159 | 'converted to\n' |
| 3160 | ' complex;\n' |
| 3161 | '\n' |
| 3162 | '* otherwise, if either argument is a floating point number, ' |
| 3163 | 'the\n' |
| 3164 | ' other is converted to floating point;\n' |
| 3165 | '\n' |
| 3166 | '* otherwise, both must be integers and no conversion is ' |
| 3167 | 'necessary.\n' |
| 3168 | '\n' |
| 3169 | 'Some additional rules apply for certain operators (e.g., a ' |
| 3170 | 'string as a\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3171 | 'left argument to the ‘%’ operator). Extensions must define ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3172 | 'their own\n' |
| 3173 | 'conversion behavior.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3174 | 'customization': 'Basic customization\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3175 | '*******************\n' |
| 3176 | '\n' |
| 3177 | 'object.__new__(cls[, ...])\n' |
| 3178 | '\n' |
| 3179 | ' Called to create a new instance of class *cls*. ' |
| 3180 | '"__new__()" is a\n' |
| 3181 | ' static method (special-cased so you need not declare it ' |
| 3182 | 'as such)\n' |
| 3183 | ' that takes the class of which an instance was requested ' |
| 3184 | 'as its\n' |
| 3185 | ' first argument. The remaining arguments are those ' |
| 3186 | 'passed to the\n' |
| 3187 | ' object constructor expression (the call to the class). ' |
| 3188 | 'The return\n' |
| 3189 | ' value of "__new__()" should be the new object instance ' |
| 3190 | '(usually an\n' |
| 3191 | ' instance of *cls*).\n' |
| 3192 | '\n' |
| 3193 | ' Typical implementations create a new instance of the ' |
| 3194 | 'class by\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3195 | ' invoking the superclass’s "__new__()" method using\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3196 | ' "super().__new__(cls[, ...])" with appropriate arguments ' |
| 3197 | 'and then\n' |
| 3198 | ' modifying the newly-created instance as necessary before ' |
| 3199 | 'returning\n' |
| 3200 | ' it.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3201 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 3202 | ' If "__new__()" is invoked during object construction and ' |
| 3203 | 'it returns\n' |
| 3204 | ' an instance or subclass of *cls*, then the new ' |
| 3205 | 'instance’s\n' |
| 3206 | ' "__init__()" method will be invoked like ' |
| 3207 | '"__init__(self[, ...])",\n' |
| 3208 | ' where *self* is the new instance and the remaining ' |
| 3209 | 'arguments are\n' |
| 3210 | ' the same as were passed to the object constructor.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3211 | '\n' |
| 3212 | ' If "__new__()" does not return an instance of *cls*, ' |
| 3213 | 'then the new\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3214 | ' instance’s "__init__()" method will not be invoked.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3215 | '\n' |
| 3216 | ' "__new__()" is intended mainly to allow subclasses of ' |
| 3217 | 'immutable\n' |
| 3218 | ' types (like int, str, or tuple) to customize instance ' |
| 3219 | 'creation. It\n' |
| 3220 | ' is also commonly overridden in custom metaclasses in ' |
| 3221 | 'order to\n' |
| 3222 | ' customize class creation.\n' |
| 3223 | '\n' |
| 3224 | 'object.__init__(self[, ...])\n' |
| 3225 | '\n' |
| 3226 | ' Called after the instance has been created (by ' |
| 3227 | '"__new__()"), but\n' |
| 3228 | ' before it is returned to the caller. The arguments are ' |
| 3229 | 'those\n' |
| 3230 | ' passed to the class constructor expression. If a base ' |
| 3231 | 'class has an\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3232 | ' "__init__()" method, the derived class’s "__init__()" ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3233 | 'method, if\n' |
| 3234 | ' any, must explicitly call it to ensure proper ' |
| 3235 | 'initialization of the\n' |
| 3236 | ' base class part of the instance; for example:\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3237 | ' "super().__init__([args...])".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3238 | '\n' |
| 3239 | ' Because "__new__()" and "__init__()" work together in ' |
| 3240 | 'constructing\n' |
| 3241 | ' objects ("__new__()" to create it, and "__init__()" to ' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 3242 | 'customize\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3243 | ' it), no non-"None" value may be returned by ' |
| 3244 | '"__init__()"; doing so\n' |
| 3245 | ' will cause a "TypeError" to be raised at runtime.\n' |
| 3246 | '\n' |
| 3247 | 'object.__del__(self)\n' |
| 3248 | '\n' |
| 3249 | ' Called when the instance is about to be destroyed. This ' |
| 3250 | 'is also\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 3251 | ' called a finalizer or (improperly) a destructor. If a ' |
| 3252 | 'base class\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3253 | ' has a "__del__()" method, the derived class’s ' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 3254 | '"__del__()" method,\n' |
| 3255 | ' if any, must explicitly call it to ensure proper ' |
| 3256 | 'deletion of the\n' |
| 3257 | ' base class part of the instance.\n' |
| 3258 | '\n' |
| 3259 | ' It is possible (though not recommended!) for the ' |
| 3260 | '"__del__()" method\n' |
| 3261 | ' to postpone destruction of the instance by creating a ' |
| 3262 | 'new reference\n' |
| 3263 | ' to it. This is called object *resurrection*. It is\n' |
| 3264 | ' implementation-dependent whether "__del__()" is called a ' |
| 3265 | 'second\n' |
| 3266 | ' time when a resurrected object is about to be destroyed; ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3267 | 'the\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 3268 | ' current *CPython* implementation only calls it once.\n' |
| 3269 | '\n' |
| 3270 | ' It is not guaranteed that "__del__()" methods are called ' |
| 3271 | 'for\n' |
| 3272 | ' objects that still exist when the interpreter exits.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3273 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3274 | ' Note: "del x" doesn’t directly call "x.__del__()" — the ' |
| 3275 | 'former\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3276 | ' decrements the reference count for "x" by one, and the ' |
| 3277 | 'latter is\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3278 | ' only called when "x"’s reference count reaches zero.\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 3279 | '\n' |
| 3280 | ' **CPython implementation detail:** It is possible for a ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3281 | 'reference\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 3282 | ' cycle to prevent the reference count of an object from ' |
| 3283 | 'going to\n' |
| 3284 | ' zero. In this case, the cycle will be later detected ' |
| 3285 | 'and deleted\n' |
| 3286 | ' by the *cyclic garbage collector*. A common cause of ' |
| 3287 | 'reference\n' |
| 3288 | ' cycles is when an exception has been caught in a local ' |
| 3289 | 'variable.\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3290 | ' The frame’s locals then reference the exception, which ' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 3291 | 'references\n' |
| 3292 | ' its own traceback, which references the locals of all ' |
| 3293 | 'frames caught\n' |
| 3294 | ' in the traceback.\n' |
| 3295 | '\n' |
| 3296 | ' See also: Documentation for the "gc" module.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3297 | '\n' |
| 3298 | ' Warning: Due to the precarious circumstances under ' |
| 3299 | 'which\n' |
| 3300 | ' "__del__()" methods are invoked, exceptions that occur ' |
| 3301 | 'during\n' |
| 3302 | ' their execution are ignored, and a warning is printed ' |
| 3303 | 'to\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 3304 | ' "sys.stderr" instead. In particular:\n' |
| 3305 | '\n' |
| 3306 | ' * "__del__()" can be invoked when arbitrary code is ' |
| 3307 | 'being\n' |
| 3308 | ' executed, including from any arbitrary thread. If ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3309 | '"__del__()"\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 3310 | ' needs to take a lock or invoke any other blocking ' |
| 3311 | 'resource, it\n' |
| 3312 | ' may deadlock as the resource may already be taken by ' |
| 3313 | 'the code\n' |
| 3314 | ' that gets interrupted to execute "__del__()".\n' |
| 3315 | '\n' |
| 3316 | ' * "__del__()" can be executed during interpreter ' |
| 3317 | 'shutdown. As\n' |
| 3318 | ' a consequence, the global variables it needs to ' |
| 3319 | 'access\n' |
| 3320 | ' (including other modules) may already have been ' |
| 3321 | 'deleted or set\n' |
| 3322 | ' to "None". Python guarantees that globals whose name ' |
| 3323 | 'begins\n' |
| 3324 | ' with a single underscore are deleted from their ' |
| 3325 | 'module before\n' |
| 3326 | ' other globals are deleted; if no other references to ' |
| 3327 | 'such\n' |
| 3328 | ' globals exist, this may help in assuring that ' |
| 3329 | 'imported modules\n' |
| 3330 | ' are still available at the time when the "__del__()" ' |
| 3331 | 'method is\n' |
| 3332 | ' called.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3333 | '\n' |
| 3334 | 'object.__repr__(self)\n' |
| 3335 | '\n' |
| 3336 | ' Called by the "repr()" built-in function to compute the ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3337 | '“official”\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3338 | ' string representation of an object. If at all possible, ' |
| 3339 | 'this\n' |
| 3340 | ' should look like a valid Python expression that could be ' |
| 3341 | 'used to\n' |
| 3342 | ' recreate an object with the same value (given an ' |
| 3343 | 'appropriate\n' |
| 3344 | ' environment). If this is not possible, a string of the ' |
| 3345 | 'form\n' |
| 3346 | ' "<...some useful description...>" should be returned. ' |
| 3347 | 'The return\n' |
| 3348 | ' value must be a string object. If a class defines ' |
| 3349 | '"__repr__()" but\n' |
| 3350 | ' not "__str__()", then "__repr__()" is also used when an ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3351 | '“informal”\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3352 | ' string representation of instances of that class is ' |
| 3353 | 'required.\n' |
| 3354 | '\n' |
| 3355 | ' This is typically used for debugging, so it is important ' |
| 3356 | 'that the\n' |
| 3357 | ' representation is information-rich and unambiguous.\n' |
| 3358 | '\n' |
| 3359 | 'object.__str__(self)\n' |
| 3360 | '\n' |
| 3361 | ' Called by "str(object)" and the built-in functions ' |
| 3362 | '"format()" and\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3363 | ' "print()" to compute the “informal” or nicely printable ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3364 | 'string\n' |
| 3365 | ' representation of an object. The return value must be a ' |
| 3366 | 'string\n' |
| 3367 | ' object.\n' |
| 3368 | '\n' |
| 3369 | ' This method differs from "object.__repr__()" in that ' |
| 3370 | 'there is no\n' |
| 3371 | ' expectation that "__str__()" return a valid Python ' |
| 3372 | 'expression: a\n' |
| 3373 | ' more convenient or concise representation can be used.\n' |
| 3374 | '\n' |
| 3375 | ' The default implementation defined by the built-in type ' |
| 3376 | '"object"\n' |
| 3377 | ' calls "object.__repr__()".\n' |
| 3378 | '\n' |
| 3379 | 'object.__bytes__(self)\n' |
| 3380 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3381 | ' Called by bytes to compute a byte-string representation ' |
| 3382 | 'of an\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3383 | ' object. This should return a "bytes" object.\n' |
| 3384 | '\n' |
| 3385 | 'object.__format__(self, format_spec)\n' |
| 3386 | '\n' |
| 3387 | ' Called by the "format()" built-in function, and by ' |
| 3388 | 'extension,\n' |
| 3389 | ' evaluation of formatted string literals and the ' |
| 3390 | '"str.format()"\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3391 | ' method, to produce a “formatted” string representation ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3392 | 'of an\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 3393 | ' object. The *format_spec* argument is a string that ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3394 | 'contains a\n' |
| 3395 | ' description of the formatting options desired. The ' |
| 3396 | 'interpretation\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 3397 | ' of the *format_spec* argument is up to the type ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3398 | 'implementing\n' |
| 3399 | ' "__format__()", however most classes will either ' |
| 3400 | 'delegate\n' |
| 3401 | ' formatting to one of the built-in types, or use a ' |
| 3402 | 'similar\n' |
| 3403 | ' formatting option syntax.\n' |
| 3404 | '\n' |
| 3405 | ' See Format Specification Mini-Language for a description ' |
| 3406 | 'of the\n' |
| 3407 | ' standard formatting syntax.\n' |
| 3408 | '\n' |
| 3409 | ' The return value must be a string object.\n' |
| 3410 | '\n' |
| 3411 | ' Changed in version 3.4: The __format__ method of ' |
| 3412 | '"object" itself\n' |
| 3413 | ' raises a "TypeError" if passed any non-empty string.\n' |
| 3414 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3415 | ' Changed in version 3.7: "object.__format__(x, \'\')" is ' |
| 3416 | 'now\n' |
Łukasz Langa | bc1c8af | 2020-04-27 22:44:04 +0200 | [diff] [blame] | 3417 | ' equivalent to "str(x)" rather than "format(str(x), ' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3418 | '\'\')".\n' |
| 3419 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3420 | 'object.__lt__(self, other)\n' |
| 3421 | 'object.__le__(self, other)\n' |
| 3422 | 'object.__eq__(self, other)\n' |
| 3423 | 'object.__ne__(self, other)\n' |
| 3424 | 'object.__gt__(self, other)\n' |
| 3425 | 'object.__ge__(self, other)\n' |
| 3426 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3427 | ' These are the so-called “rich comparison” methods. The\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3428 | ' correspondence between operator symbols and method names ' |
| 3429 | 'is as\n' |
| 3430 | ' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls ' |
| 3431 | '"x.__le__(y)",\n' |
| 3432 | ' "x==y" calls "x.__eq__(y)", "x!=y" calls "x.__ne__(y)", ' |
| 3433 | '"x>y" calls\n' |
| 3434 | ' "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".\n' |
| 3435 | '\n' |
| 3436 | ' A rich comparison method may return the singleton ' |
| 3437 | '"NotImplemented"\n' |
| 3438 | ' if it does not implement the operation for a given pair ' |
| 3439 | 'of\n' |
| 3440 | ' arguments. By convention, "False" and "True" are ' |
| 3441 | 'returned for a\n' |
| 3442 | ' successful comparison. However, these methods can return ' |
| 3443 | 'any value,\n' |
| 3444 | ' so if the comparison operator is used in a Boolean ' |
| 3445 | 'context (e.g.,\n' |
| 3446 | ' in the condition of an "if" statement), Python will call ' |
| 3447 | '"bool()"\n' |
| 3448 | ' on the value to determine if the result is true or ' |
| 3449 | 'false.\n' |
| 3450 | '\n' |
| 3451 | ' By default, "__ne__()" delegates to "__eq__()" and ' |
| 3452 | 'inverts the\n' |
| 3453 | ' result unless it is "NotImplemented". There are no ' |
| 3454 | 'other implied\n' |
| 3455 | ' relationships among the comparison operators, for ' |
| 3456 | 'example, the\n' |
| 3457 | ' truth of "(x<y or x==y)" does not imply "x<=y". To ' |
| 3458 | 'automatically\n' |
| 3459 | ' generate ordering operations from a single root ' |
| 3460 | 'operation, see\n' |
| 3461 | ' "functools.total_ordering()".\n' |
| 3462 | '\n' |
| 3463 | ' See the paragraph on "__hash__()" for some important ' |
| 3464 | 'notes on\n' |
| 3465 | ' creating *hashable* objects which support custom ' |
| 3466 | 'comparison\n' |
| 3467 | ' operations and are usable as dictionary keys.\n' |
| 3468 | '\n' |
| 3469 | ' There are no swapped-argument versions of these methods ' |
| 3470 | '(to be used\n' |
| 3471 | ' when the left argument does not support the operation ' |
| 3472 | 'but the right\n' |
| 3473 | ' argument does); rather, "__lt__()" and "__gt__()" are ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3474 | 'each other’s\n' |
| 3475 | ' reflection, "__le__()" and "__ge__()" are each other’s ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3476 | 'reflection,\n' |
| 3477 | ' and "__eq__()" and "__ne__()" are their own reflection. ' |
| 3478 | 'If the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3479 | ' operands are of different types, and right operand’s ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3480 | 'type is a\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3481 | ' direct or indirect subclass of the left operand’s type, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3482 | 'the\n' |
| 3483 | ' reflected method of the right operand has priority, ' |
| 3484 | 'otherwise the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3485 | ' left operand’s method has priority. Virtual subclassing ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3486 | 'is not\n' |
| 3487 | ' considered.\n' |
| 3488 | '\n' |
| 3489 | 'object.__hash__(self)\n' |
| 3490 | '\n' |
| 3491 | ' Called by built-in function "hash()" and for operations ' |
| 3492 | 'on members\n' |
| 3493 | ' of hashed collections including "set", "frozenset", and ' |
| 3494 | '"dict".\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3495 | ' "__hash__()" should return an integer. The only required ' |
| 3496 | 'property\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3497 | ' is that objects which compare equal have the same hash ' |
| 3498 | 'value; it is\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3499 | ' advised to mix together the hash values of the ' |
| 3500 | 'components of the\n' |
| 3501 | ' object that also play a part in comparison of objects by ' |
| 3502 | 'packing\n' |
| 3503 | ' them into a tuple and hashing the tuple. Example:\n' |
| 3504 | '\n' |
| 3505 | ' def __hash__(self):\n' |
| 3506 | ' return hash((self.name, self.nick, self.color))\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3507 | '\n' |
| 3508 | ' Note: "hash()" truncates the value returned from an ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3509 | 'object’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3510 | ' custom "__hash__()" method to the size of a ' |
| 3511 | '"Py_ssize_t". This\n' |
| 3512 | ' is typically 8 bytes on 64-bit builds and 4 bytes on ' |
| 3513 | '32-bit\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3514 | ' builds. If an object’s "__hash__()" must ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3515 | 'interoperate on builds\n' |
| 3516 | ' of different bit sizes, be sure to check the width on ' |
| 3517 | 'all\n' |
| 3518 | ' supported builds. An easy way to do this is with ' |
| 3519 | '"python -c\n' |
| 3520 | ' "import sys; print(sys.hash_info.width)"".\n' |
| 3521 | '\n' |
| 3522 | ' If a class does not define an "__eq__()" method it ' |
| 3523 | 'should not\n' |
| 3524 | ' define a "__hash__()" operation either; if it defines ' |
| 3525 | '"__eq__()"\n' |
| 3526 | ' but not "__hash__()", its instances will not be usable ' |
| 3527 | 'as items in\n' |
| 3528 | ' hashable collections. If a class defines mutable ' |
| 3529 | 'objects and\n' |
| 3530 | ' implements an "__eq__()" method, it should not ' |
| 3531 | 'implement\n' |
| 3532 | ' "__hash__()", since the implementation of hashable ' |
| 3533 | 'collections\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3534 | ' requires that a key’s hash value is immutable (if the ' |
| 3535 | 'object’s hash\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3536 | ' value changes, it will be in the wrong hash bucket).\n' |
| 3537 | '\n' |
| 3538 | ' User-defined classes have "__eq__()" and "__hash__()" ' |
| 3539 | 'methods by\n' |
| 3540 | ' default; with them, all objects compare unequal (except ' |
| 3541 | 'with\n' |
| 3542 | ' themselves) and "x.__hash__()" returns an appropriate ' |
| 3543 | 'value such\n' |
| 3544 | ' that "x == y" implies both that "x is y" and "hash(x) == ' |
| 3545 | 'hash(y)".\n' |
| 3546 | '\n' |
| 3547 | ' A class that overrides "__eq__()" and does not define ' |
| 3548 | '"__hash__()"\n' |
| 3549 | ' will have its "__hash__()" implicitly set to "None". ' |
| 3550 | 'When the\n' |
| 3551 | ' "__hash__()" method of a class is "None", instances of ' |
| 3552 | 'the class\n' |
| 3553 | ' will raise an appropriate "TypeError" when a program ' |
| 3554 | 'attempts to\n' |
| 3555 | ' retrieve their hash value, and will also be correctly ' |
| 3556 | 'identified as\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3557 | ' unhashable when checking "isinstance(obj,\n' |
| 3558 | ' collections.abc.Hashable)".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3559 | '\n' |
| 3560 | ' If a class that overrides "__eq__()" needs to retain ' |
| 3561 | 'the\n' |
| 3562 | ' implementation of "__hash__()" from a parent class, the ' |
| 3563 | 'interpreter\n' |
| 3564 | ' must be told this explicitly by setting "__hash__ =\n' |
| 3565 | ' <ParentClass>.__hash__".\n' |
| 3566 | '\n' |
| 3567 | ' If a class that does not override "__eq__()" wishes to ' |
| 3568 | 'suppress\n' |
| 3569 | ' hash support, it should include "__hash__ = None" in the ' |
| 3570 | 'class\n' |
| 3571 | ' definition. A class which defines its own "__hash__()" ' |
| 3572 | 'that\n' |
| 3573 | ' explicitly raises a "TypeError" would be incorrectly ' |
| 3574 | 'identified as\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3575 | ' hashable by an "isinstance(obj, ' |
| 3576 | 'collections.abc.Hashable)" call.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3577 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 3578 | ' Note: By default, the "__hash__()" values of str and ' |
| 3579 | 'bytes\n' |
| 3580 | ' objects are “salted” with an unpredictable random ' |
| 3581 | 'value.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3582 | ' Although they remain constant within an individual ' |
| 3583 | 'Python\n' |
| 3584 | ' process, they are not predictable between repeated ' |
| 3585 | 'invocations of\n' |
| 3586 | ' Python.This is intended to provide protection against ' |
| 3587 | 'a denial-\n' |
| 3588 | ' of-service caused by carefully-chosen inputs that ' |
| 3589 | 'exploit the\n' |
| 3590 | ' worst case performance of a dict insertion, O(n^2) ' |
| 3591 | 'complexity.\n' |
| 3592 | ' See ' |
| 3593 | 'http://www.ocert.org/advisories/ocert-2011-003.html for\n' |
| 3594 | ' details.Changing hash values affects the iteration ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3595 | 'order of sets.\n' |
| 3596 | ' Python has never made guarantees about this ordering ' |
| 3597 | '(and it\n' |
| 3598 | ' typically varies between 32-bit and 64-bit builds).See ' |
| 3599 | 'also\n' |
| 3600 | ' "PYTHONHASHSEED".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3601 | '\n' |
| 3602 | ' Changed in version 3.3: Hash randomization is enabled by ' |
| 3603 | 'default.\n' |
| 3604 | '\n' |
| 3605 | 'object.__bool__(self)\n' |
| 3606 | '\n' |
| 3607 | ' Called to implement truth value testing and the built-in ' |
| 3608 | 'operation\n' |
| 3609 | ' "bool()"; should return "False" or "True". When this ' |
| 3610 | 'method is not\n' |
| 3611 | ' defined, "__len__()" is called, if it is defined, and ' |
| 3612 | 'the object is\n' |
| 3613 | ' considered true if its result is nonzero. If a class ' |
| 3614 | 'defines\n' |
| 3615 | ' neither "__len__()" nor "__bool__()", all its instances ' |
| 3616 | 'are\n' |
| 3617 | ' considered true.\n', |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3618 | 'debugger': '"pdb" — The Python Debugger\n' |
| 3619 | '***************************\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3620 | '\n' |
| 3621 | '**Source code:** Lib/pdb.py\n' |
| 3622 | '\n' |
| 3623 | '======================================================================\n' |
| 3624 | '\n' |
| 3625 | 'The module "pdb" defines an interactive source code debugger ' |
| 3626 | 'for\n' |
| 3627 | 'Python programs. It supports setting (conditional) breakpoints ' |
| 3628 | 'and\n' |
| 3629 | 'single stepping at the source line level, inspection of stack ' |
| 3630 | 'frames,\n' |
| 3631 | 'source code listing, and evaluation of arbitrary Python code in ' |
| 3632 | 'the\n' |
| 3633 | 'context of any stack frame. It also supports post-mortem ' |
| 3634 | 'debugging\n' |
| 3635 | 'and can be called under program control.\n' |
| 3636 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3637 | 'The debugger is extensible – it is actually defined as the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3638 | 'class\n' |
| 3639 | '"Pdb". This is currently undocumented but easily understood by ' |
| 3640 | 'reading\n' |
| 3641 | 'the source. The extension interface uses the modules "bdb" and ' |
| 3642 | '"cmd".\n' |
| 3643 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3644 | 'The debugger’s prompt is "(Pdb)". Typical usage to run a program ' |
| 3645 | 'under\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3646 | 'control of the debugger is:\n' |
| 3647 | '\n' |
| 3648 | ' >>> import pdb\n' |
| 3649 | ' >>> import mymodule\n' |
| 3650 | " >>> pdb.run('mymodule.test()')\n" |
| 3651 | ' > <string>(0)?()\n' |
| 3652 | ' (Pdb) continue\n' |
| 3653 | ' > <string>(1)?()\n' |
| 3654 | ' (Pdb) continue\n' |
| 3655 | " NameError: 'spam'\n" |
| 3656 | ' > <string>(1)?()\n' |
| 3657 | ' (Pdb)\n' |
| 3658 | '\n' |
| 3659 | 'Changed in version 3.3: Tab-completion via the "readline" module ' |
| 3660 | 'is\n' |
| 3661 | 'available for commands and command arguments, e.g. the current ' |
| 3662 | 'global\n' |
| 3663 | 'and local names are offered as arguments of the "p" command.\n' |
| 3664 | '\n' |
| 3665 | '"pdb.py" can also be invoked as a script to debug other ' |
| 3666 | 'scripts. For\n' |
| 3667 | 'example:\n' |
| 3668 | '\n' |
| 3669 | ' python3 -m pdb myscript.py\n' |
| 3670 | '\n' |
| 3671 | 'When invoked as a script, pdb will automatically enter ' |
| 3672 | 'post-mortem\n' |
| 3673 | 'debugging if the program being debugged exits abnormally. After ' |
| 3674 | 'post-\n' |
| 3675 | 'mortem debugging (or after normal exit of the program), pdb ' |
| 3676 | 'will\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3677 | 'restart the program. Automatic restarting preserves pdb’s state ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3678 | '(such\n' |
| 3679 | 'as breakpoints) and in most cases is more useful than quitting ' |
| 3680 | 'the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3681 | 'debugger upon program’s exit.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3682 | '\n' |
| 3683 | 'New in version 3.2: "pdb.py" now accepts a "-c" option that ' |
| 3684 | 'executes\n' |
| 3685 | 'commands as if given in a ".pdbrc" file, see Debugger Commands.\n' |
| 3686 | '\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 3687 | 'New in version 3.7: "pdb.py" now accepts a "-m" option that ' |
| 3688 | 'execute\n' |
| 3689 | 'modules similar to the way "python3 -m" does. As with a script, ' |
| 3690 | 'the\n' |
| 3691 | 'debugger will pause execution just before the first line of the\n' |
| 3692 | 'module.\n' |
| 3693 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3694 | 'The typical usage to break into the debugger from a running ' |
| 3695 | 'program is\n' |
| 3696 | 'to insert\n' |
| 3697 | '\n' |
| 3698 | ' import pdb; pdb.set_trace()\n' |
| 3699 | '\n' |
| 3700 | 'at the location you want to break into the debugger. You can ' |
| 3701 | 'then\n' |
| 3702 | 'step through the code following this statement, and continue ' |
| 3703 | 'running\n' |
| 3704 | 'without the debugger using the "continue" command.\n' |
| 3705 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3706 | 'New in version 3.7: The built-in "breakpoint()", when called ' |
| 3707 | 'with\n' |
| 3708 | 'defaults, can be used instead of "import pdb; pdb.set_trace()".\n' |
| 3709 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3710 | 'The typical usage to inspect a crashed program is:\n' |
| 3711 | '\n' |
| 3712 | ' >>> import pdb\n' |
| 3713 | ' >>> import mymodule\n' |
| 3714 | ' >>> mymodule.test()\n' |
| 3715 | ' Traceback (most recent call last):\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 3716 | ' File "<stdin>", line 1, in <module>\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3717 | ' File "./mymodule.py", line 4, in test\n' |
| 3718 | ' test2()\n' |
| 3719 | ' File "./mymodule.py", line 3, in test2\n' |
| 3720 | ' print(spam)\n' |
| 3721 | ' NameError: spam\n' |
| 3722 | ' >>> pdb.pm()\n' |
| 3723 | ' > ./mymodule.py(3)test2()\n' |
| 3724 | ' -> print(spam)\n' |
| 3725 | ' (Pdb)\n' |
| 3726 | '\n' |
| 3727 | 'The module defines the following functions; each enters the ' |
| 3728 | 'debugger\n' |
| 3729 | 'in a slightly different way:\n' |
| 3730 | '\n' |
| 3731 | 'pdb.run(statement, globals=None, locals=None)\n' |
| 3732 | '\n' |
| 3733 | ' Execute the *statement* (given as a string or a code object) ' |
| 3734 | 'under\n' |
| 3735 | ' debugger control. The debugger prompt appears before any ' |
| 3736 | 'code is\n' |
| 3737 | ' executed; you can set breakpoints and type "continue", or you ' |
| 3738 | 'can\n' |
| 3739 | ' step through the statement using "step" or "next" (all these\n' |
| 3740 | ' commands are explained below). The optional *globals* and ' |
| 3741 | '*locals*\n' |
| 3742 | ' arguments specify the environment in which the code is ' |
| 3743 | 'executed; by\n' |
| 3744 | ' default the dictionary of the module "__main__" is used. ' |
| 3745 | '(See the\n' |
| 3746 | ' explanation of the built-in "exec()" or "eval()" functions.)\n' |
| 3747 | '\n' |
| 3748 | 'pdb.runeval(expression, globals=None, locals=None)\n' |
| 3749 | '\n' |
| 3750 | ' Evaluate the *expression* (given as a string or a code ' |
| 3751 | 'object)\n' |
| 3752 | ' under debugger control. When "runeval()" returns, it returns ' |
| 3753 | 'the\n' |
| 3754 | ' value of the expression. Otherwise this function is similar ' |
| 3755 | 'to\n' |
| 3756 | ' "run()".\n' |
| 3757 | '\n' |
| 3758 | 'pdb.runcall(function, *args, **kwds)\n' |
| 3759 | '\n' |
| 3760 | ' Call the *function* (a function or method object, not a ' |
| 3761 | 'string)\n' |
| 3762 | ' with the given arguments. When "runcall()" returns, it ' |
| 3763 | 'returns\n' |
| 3764 | ' whatever the function call returned. The debugger prompt ' |
| 3765 | 'appears\n' |
| 3766 | ' as soon as the function is entered.\n' |
| 3767 | '\n' |
Ned Deily | c730223 | 2017-10-16 23:41:55 -0400 | [diff] [blame] | 3768 | 'pdb.set_trace(*, header=None)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3769 | '\n' |
| 3770 | ' Enter the debugger at the calling stack frame. This is ' |
| 3771 | 'useful to\n' |
| 3772 | ' hard-code a breakpoint at a given point in a program, even if ' |
| 3773 | 'the\n' |
| 3774 | ' code is not otherwise being debugged (e.g. when an assertion\n' |
Ned Deily | 3f9a728 | 2017-12-05 03:17:33 -0500 | [diff] [blame] | 3775 | ' fails). If given, *header* is printed to the console just ' |
Ned Deily | c730223 | 2017-10-16 23:41:55 -0400 | [diff] [blame] | 3776 | 'before\n' |
| 3777 | ' debugging begins.\n' |
| 3778 | '\n' |
Ned Deily | 3f9a728 | 2017-12-05 03:17:33 -0500 | [diff] [blame] | 3779 | ' Changed in version 3.7: The keyword-only argument *header*.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3780 | '\n' |
| 3781 | 'pdb.post_mortem(traceback=None)\n' |
| 3782 | '\n' |
| 3783 | ' Enter post-mortem debugging of the given *traceback* object. ' |
| 3784 | 'If no\n' |
| 3785 | ' *traceback* is given, it uses the one of the exception that ' |
| 3786 | 'is\n' |
| 3787 | ' currently being handled (an exception must be being handled ' |
| 3788 | 'if the\n' |
| 3789 | ' default is to be used).\n' |
| 3790 | '\n' |
| 3791 | 'pdb.pm()\n' |
| 3792 | '\n' |
| 3793 | ' Enter post-mortem debugging of the traceback found in\n' |
| 3794 | ' "sys.last_traceback".\n' |
| 3795 | '\n' |
| 3796 | 'The "run*" functions and "set_trace()" are aliases for ' |
| 3797 | 'instantiating\n' |
| 3798 | 'the "Pdb" class and calling the method of the same name. If you ' |
| 3799 | 'want\n' |
| 3800 | 'to access further features, you have to do this yourself:\n' |
| 3801 | '\n' |
| 3802 | "class pdb.Pdb(completekey='tab', stdin=None, stdout=None, " |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 3803 | 'skip=None, nosigint=False, readrc=True)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3804 | '\n' |
| 3805 | ' "Pdb" is the debugger class.\n' |
| 3806 | '\n' |
| 3807 | ' The *completekey*, *stdin* and *stdout* arguments are passed ' |
| 3808 | 'to the\n' |
| 3809 | ' underlying "cmd.Cmd" class; see the description there.\n' |
| 3810 | '\n' |
| 3811 | ' The *skip* argument, if given, must be an iterable of ' |
| 3812 | 'glob-style\n' |
| 3813 | ' module name patterns. The debugger will not step into frames ' |
| 3814 | 'that\n' |
| 3815 | ' originate in a module that matches one of these patterns. ' |
| 3816 | '[1]\n' |
| 3817 | '\n' |
| 3818 | ' By default, Pdb sets a handler for the SIGINT signal (which ' |
| 3819 | 'is sent\n' |
| 3820 | ' when the user presses "Ctrl-C" on the console) when you give ' |
| 3821 | 'a\n' |
| 3822 | ' "continue" command. This allows you to break into the ' |
| 3823 | 'debugger\n' |
| 3824 | ' again by pressing "Ctrl-C". If you want Pdb not to touch ' |
| 3825 | 'the\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 3826 | ' SIGINT handler, set *nosigint* to true.\n' |
| 3827 | '\n' |
| 3828 | ' The *readrc* argument defaults to true and controls whether ' |
| 3829 | 'Pdb\n' |
| 3830 | ' will load .pdbrc files from the filesystem.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3831 | '\n' |
| 3832 | ' Example call to enable tracing with *skip*:\n' |
| 3833 | '\n' |
| 3834 | " import pdb; pdb.Pdb(skip=['django.*']).set_trace()\n" |
| 3835 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 3836 | ' Raises an auditing event "pdb.Pdb" with no arguments.\n' |
| 3837 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3838 | ' New in version 3.1: The *skip* argument.\n' |
| 3839 | '\n' |
| 3840 | ' New in version 3.2: The *nosigint* argument. Previously, a ' |
| 3841 | 'SIGINT\n' |
| 3842 | ' handler was never set by Pdb.\n' |
| 3843 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 3844 | ' Changed in version 3.6: The *readrc* argument.\n' |
| 3845 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3846 | ' run(statement, globals=None, locals=None)\n' |
| 3847 | ' runeval(expression, globals=None, locals=None)\n' |
| 3848 | ' runcall(function, *args, **kwds)\n' |
| 3849 | ' set_trace()\n' |
| 3850 | '\n' |
| 3851 | ' See the documentation for the functions explained above.\n' |
| 3852 | '\n' |
| 3853 | '\n' |
| 3854 | 'Debugger Commands\n' |
| 3855 | '=================\n' |
| 3856 | '\n' |
| 3857 | 'The commands recognized by the debugger are listed below. Most\n' |
| 3858 | 'commands can be abbreviated to one or two letters as indicated; ' |
| 3859 | 'e.g.\n' |
| 3860 | '"h(elp)" means that either "h" or "help" can be used to enter ' |
| 3861 | 'the help\n' |
| 3862 | 'command (but not "he" or "hel", nor "H" or "Help" or "HELP").\n' |
| 3863 | 'Arguments to commands must be separated by whitespace (spaces ' |
| 3864 | 'or\n' |
| 3865 | 'tabs). Optional arguments are enclosed in square brackets ' |
| 3866 | '("[]") in\n' |
| 3867 | 'the command syntax; the square brackets must not be typed.\n' |
| 3868 | 'Alternatives in the command syntax are separated by a vertical ' |
| 3869 | 'bar\n' |
| 3870 | '("|").\n' |
| 3871 | '\n' |
| 3872 | 'Entering a blank line repeats the last command entered. ' |
| 3873 | 'Exception: if\n' |
| 3874 | 'the last command was a "list" command, the next 11 lines are ' |
| 3875 | 'listed.\n' |
| 3876 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3877 | 'Commands that the debugger doesn’t recognize are assumed to be ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3878 | 'Python\n' |
| 3879 | 'statements and are executed in the context of the program being\n' |
| 3880 | 'debugged. Python statements can also be prefixed with an ' |
| 3881 | 'exclamation\n' |
| 3882 | 'point ("!"). This is a powerful way to inspect the program ' |
| 3883 | 'being\n' |
| 3884 | 'debugged; it is even possible to change a variable or call a ' |
| 3885 | 'function.\n' |
| 3886 | 'When an exception occurs in such a statement, the exception name ' |
| 3887 | 'is\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3888 | 'printed but the debugger’s state is not changed.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3889 | '\n' |
| 3890 | 'The debugger supports aliases. Aliases can have parameters ' |
| 3891 | 'which\n' |
| 3892 | 'allows one a certain level of adaptability to the context under\n' |
| 3893 | 'examination.\n' |
| 3894 | '\n' |
| 3895 | 'Multiple commands may be entered on a single line, separated by ' |
| 3896 | '";;".\n' |
| 3897 | '(A single ";" is not used as it is the separator for multiple ' |
| 3898 | 'commands\n' |
| 3899 | 'in a line that is passed to the Python parser.) No intelligence ' |
| 3900 | 'is\n' |
| 3901 | 'applied to separating the commands; the input is split at the ' |
| 3902 | 'first\n' |
| 3903 | '";;" pair, even if it is in the middle of a quoted string.\n' |
| 3904 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3905 | 'If a file ".pdbrc" exists in the user’s home directory or in ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3906 | 'the\n' |
| 3907 | 'current directory, it is read in and executed as if it had been ' |
| 3908 | 'typed\n' |
| 3909 | 'at the debugger prompt. This is particularly useful for ' |
| 3910 | 'aliases. If\n' |
| 3911 | 'both files exist, the one in the home directory is read first ' |
| 3912 | 'and\n' |
| 3913 | 'aliases defined there can be overridden by the local file.\n' |
| 3914 | '\n' |
| 3915 | 'Changed in version 3.2: ".pdbrc" can now contain commands that\n' |
| 3916 | 'continue debugging, such as "continue" or "next". Previously, ' |
| 3917 | 'these\n' |
| 3918 | 'commands had no effect.\n' |
| 3919 | '\n' |
| 3920 | 'h(elp) [command]\n' |
| 3921 | '\n' |
| 3922 | ' Without argument, print the list of available commands. With ' |
| 3923 | 'a\n' |
| 3924 | ' *command* as argument, print help about that command. "help ' |
| 3925 | 'pdb"\n' |
| 3926 | ' displays the full documentation (the docstring of the "pdb"\n' |
| 3927 | ' module). Since the *command* argument must be an identifier, ' |
| 3928 | '"help\n' |
| 3929 | ' exec" must be entered to get help on the "!" command.\n' |
| 3930 | '\n' |
| 3931 | 'w(here)\n' |
| 3932 | '\n' |
| 3933 | ' Print a stack trace, with the most recent frame at the ' |
| 3934 | 'bottom. An\n' |
| 3935 | ' arrow indicates the current frame, which determines the ' |
| 3936 | 'context of\n' |
| 3937 | ' most commands.\n' |
| 3938 | '\n' |
| 3939 | 'd(own) [count]\n' |
| 3940 | '\n' |
| 3941 | ' Move the current frame *count* (default one) levels down in ' |
| 3942 | 'the\n' |
| 3943 | ' stack trace (to a newer frame).\n' |
| 3944 | '\n' |
| 3945 | 'u(p) [count]\n' |
| 3946 | '\n' |
| 3947 | ' Move the current frame *count* (default one) levels up in the ' |
| 3948 | 'stack\n' |
| 3949 | ' trace (to an older frame).\n' |
| 3950 | '\n' |
| 3951 | 'b(reak) [([filename:]lineno | function) [, condition]]\n' |
| 3952 | '\n' |
| 3953 | ' With a *lineno* argument, set a break there in the current ' |
| 3954 | 'file.\n' |
| 3955 | ' With a *function* argument, set a break at the first ' |
| 3956 | 'executable\n' |
| 3957 | ' statement within that function. The line number may be ' |
| 3958 | 'prefixed\n' |
| 3959 | ' with a filename and a colon, to specify a breakpoint in ' |
| 3960 | 'another\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 3961 | ' file (probably one that hasn’t been loaded yet). The file ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3962 | 'is\n' |
| 3963 | ' searched on "sys.path". Note that each breakpoint is ' |
| 3964 | 'assigned a\n' |
| 3965 | ' number to which all the other breakpoint commands refer.\n' |
| 3966 | '\n' |
| 3967 | ' If a second argument is present, it is an expression which ' |
| 3968 | 'must\n' |
| 3969 | ' evaluate to true before the breakpoint is honored.\n' |
| 3970 | '\n' |
| 3971 | ' Without argument, list all breaks, including for each ' |
| 3972 | 'breakpoint,\n' |
| 3973 | ' the number of times that breakpoint has been hit, the ' |
| 3974 | 'current\n' |
| 3975 | ' ignore count, and the associated condition if any.\n' |
| 3976 | '\n' |
| 3977 | 'tbreak [([filename:]lineno | function) [, condition]]\n' |
| 3978 | '\n' |
| 3979 | ' Temporary breakpoint, which is removed automatically when it ' |
| 3980 | 'is\n' |
| 3981 | ' first hit. The arguments are the same as for "break".\n' |
| 3982 | '\n' |
Brandt Bucher | a0ed99b | 2019-11-11 12:47:48 -0800 | [diff] [blame] | 3983 | 'cl(ear) [filename:lineno | bpnumber ...]\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3984 | '\n' |
| 3985 | ' With a *filename:lineno* argument, clear all the breakpoints ' |
| 3986 | 'at\n' |
| 3987 | ' this line. With a space separated list of breakpoint numbers, ' |
| 3988 | 'clear\n' |
| 3989 | ' those breakpoints. Without argument, clear all breaks (but ' |
| 3990 | 'first\n' |
| 3991 | ' ask confirmation).\n' |
| 3992 | '\n' |
Brandt Bucher | a0ed99b | 2019-11-11 12:47:48 -0800 | [diff] [blame] | 3993 | 'disable [bpnumber ...]\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 3994 | '\n' |
| 3995 | ' Disable the breakpoints given as a space separated list of\n' |
| 3996 | ' breakpoint numbers. Disabling a breakpoint means it cannot ' |
| 3997 | 'cause\n' |
| 3998 | ' the program to stop execution, but unlike clearing a ' |
| 3999 | 'breakpoint, it\n' |
| 4000 | ' remains in the list of breakpoints and can be (re-)enabled.\n' |
| 4001 | '\n' |
Brandt Bucher | a0ed99b | 2019-11-11 12:47:48 -0800 | [diff] [blame] | 4002 | 'enable [bpnumber ...]\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4003 | '\n' |
| 4004 | ' Enable the breakpoints specified.\n' |
| 4005 | '\n' |
| 4006 | 'ignore bpnumber [count]\n' |
| 4007 | '\n' |
| 4008 | ' Set the ignore count for the given breakpoint number. If ' |
| 4009 | 'count is\n' |
| 4010 | ' omitted, the ignore count is set to 0. A breakpoint becomes ' |
| 4011 | 'active\n' |
| 4012 | ' when the ignore count is zero. When non-zero, the count is\n' |
| 4013 | ' decremented each time the breakpoint is reached and the ' |
| 4014 | 'breakpoint\n' |
| 4015 | ' is not disabled and any associated condition evaluates to ' |
| 4016 | 'true.\n' |
| 4017 | '\n' |
| 4018 | 'condition bpnumber [condition]\n' |
| 4019 | '\n' |
| 4020 | ' Set a new *condition* for the breakpoint, an expression which ' |
| 4021 | 'must\n' |
| 4022 | ' evaluate to true before the breakpoint is honored. If ' |
| 4023 | '*condition*\n' |
| 4024 | ' is absent, any existing condition is removed; i.e., the ' |
| 4025 | 'breakpoint\n' |
| 4026 | ' is made unconditional.\n' |
| 4027 | '\n' |
| 4028 | 'commands [bpnumber]\n' |
| 4029 | '\n' |
| 4030 | ' Specify a list of commands for breakpoint number *bpnumber*. ' |
| 4031 | 'The\n' |
| 4032 | ' commands themselves appear on the following lines. Type a ' |
| 4033 | 'line\n' |
| 4034 | ' containing just "end" to terminate the commands. An example:\n' |
| 4035 | '\n' |
| 4036 | ' (Pdb) commands 1\n' |
| 4037 | ' (com) p some_variable\n' |
| 4038 | ' (com) end\n' |
| 4039 | ' (Pdb)\n' |
| 4040 | '\n' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 4041 | ' To remove all commands from a breakpoint, type "commands" ' |
| 4042 | 'and\n' |
| 4043 | ' follow it immediately with "end"; that is, give no commands.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4044 | '\n' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 4045 | ' With no *bpnumber* argument, "commands" refers to the last\n' |
| 4046 | ' breakpoint set.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4047 | '\n' |
| 4048 | ' You can use breakpoint commands to start your program up ' |
| 4049 | 'again.\n' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 4050 | ' Simply use the "continue" command, or "step", or any other ' |
| 4051 | 'command\n' |
| 4052 | ' that resumes execution.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4053 | '\n' |
| 4054 | ' Specifying any command resuming execution (currently ' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 4055 | '"continue",\n' |
| 4056 | ' "step", "next", "return", "jump", "quit" and their ' |
| 4057 | 'abbreviations)\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4058 | ' terminates the command list (as if that command was ' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 4059 | 'immediately\n' |
| 4060 | ' followed by end). This is because any time you resume ' |
| 4061 | 'execution\n' |
| 4062 | ' (even with a simple next or step), you may encounter another\n' |
| 4063 | ' breakpoint—which could have its own command list, leading to\n' |
| 4064 | ' ambiguities about which list to execute.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4065 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4066 | ' If you use the ‘silent’ command in the command list, the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4067 | 'usual\n' |
| 4068 | ' message about stopping at a breakpoint is not printed. This ' |
| 4069 | 'may be\n' |
| 4070 | ' desirable for breakpoints that are to print a specific ' |
| 4071 | 'message and\n' |
| 4072 | ' then continue. If none of the other commands print anything, ' |
| 4073 | 'you\n' |
| 4074 | ' see no sign that the breakpoint was reached.\n' |
| 4075 | '\n' |
| 4076 | 's(tep)\n' |
| 4077 | '\n' |
| 4078 | ' Execute the current line, stop at the first possible ' |
| 4079 | 'occasion\n' |
| 4080 | ' (either in a function that is called or on the next line in ' |
| 4081 | 'the\n' |
| 4082 | ' current function).\n' |
| 4083 | '\n' |
| 4084 | 'n(ext)\n' |
| 4085 | '\n' |
| 4086 | ' Continue execution until the next line in the current ' |
| 4087 | 'function is\n' |
| 4088 | ' reached or it returns. (The difference between "next" and ' |
| 4089 | '"step"\n' |
| 4090 | ' is that "step" stops inside a called function, while "next"\n' |
| 4091 | ' executes called functions at (nearly) full speed, only ' |
| 4092 | 'stopping at\n' |
| 4093 | ' the next line in the current function.)\n' |
| 4094 | '\n' |
| 4095 | 'unt(il) [lineno]\n' |
| 4096 | '\n' |
| 4097 | ' Without argument, continue execution until the line with a ' |
| 4098 | 'number\n' |
| 4099 | ' greater than the current one is reached.\n' |
| 4100 | '\n' |
| 4101 | ' With a line number, continue execution until a line with a ' |
| 4102 | 'number\n' |
| 4103 | ' greater or equal to that is reached. In both cases, also ' |
| 4104 | 'stop when\n' |
| 4105 | ' the current frame returns.\n' |
| 4106 | '\n' |
| 4107 | ' Changed in version 3.2: Allow giving an explicit line ' |
| 4108 | 'number.\n' |
| 4109 | '\n' |
| 4110 | 'r(eturn)\n' |
| 4111 | '\n' |
| 4112 | ' Continue execution until the current function returns.\n' |
| 4113 | '\n' |
| 4114 | 'c(ont(inue))\n' |
| 4115 | '\n' |
| 4116 | ' Continue execution, only stop when a breakpoint is ' |
| 4117 | 'encountered.\n' |
| 4118 | '\n' |
| 4119 | 'j(ump) lineno\n' |
| 4120 | '\n' |
| 4121 | ' Set the next line that will be executed. Only available in ' |
| 4122 | 'the\n' |
| 4123 | ' bottom-most frame. This lets you jump back and execute code ' |
| 4124 | 'again,\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4125 | ' or jump forward to skip code that you don’t want to run.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4126 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4127 | ' It should be noted that not all jumps are allowed – for ' |
| 4128 | 'instance it\n' |
| 4129 | ' is not possible to jump into the middle of a "for" loop or ' |
| 4130 | 'out of a\n' |
| 4131 | ' "finally" clause.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4132 | '\n' |
| 4133 | 'l(ist) [first[, last]]\n' |
| 4134 | '\n' |
| 4135 | ' List source code for the current file. Without arguments, ' |
| 4136 | 'list 11\n' |
| 4137 | ' lines around the current line or continue the previous ' |
| 4138 | 'listing.\n' |
| 4139 | ' With "." as argument, list 11 lines around the current line. ' |
| 4140 | 'With\n' |
| 4141 | ' one argument, list 11 lines around at that line. With two\n' |
| 4142 | ' arguments, list the given range; if the second argument is ' |
| 4143 | 'less\n' |
| 4144 | ' than the first, it is interpreted as a count.\n' |
| 4145 | '\n' |
| 4146 | ' The current line in the current frame is indicated by "->". ' |
| 4147 | 'If an\n' |
| 4148 | ' exception is being debugged, the line where the exception ' |
| 4149 | 'was\n' |
| 4150 | ' originally raised or propagated is indicated by ">>", if it ' |
| 4151 | 'differs\n' |
| 4152 | ' from the current line.\n' |
| 4153 | '\n' |
| 4154 | ' New in version 3.2: The ">>" marker.\n' |
| 4155 | '\n' |
| 4156 | 'll | longlist\n' |
| 4157 | '\n' |
| 4158 | ' List all source code for the current function or frame.\n' |
| 4159 | ' Interesting lines are marked as for "list".\n' |
| 4160 | '\n' |
| 4161 | ' New in version 3.2.\n' |
| 4162 | '\n' |
| 4163 | 'a(rgs)\n' |
| 4164 | '\n' |
| 4165 | ' Print the argument list of the current function.\n' |
| 4166 | '\n' |
| 4167 | 'p expression\n' |
| 4168 | '\n' |
| 4169 | ' Evaluate the *expression* in the current context and print ' |
| 4170 | 'its\n' |
| 4171 | ' value.\n' |
| 4172 | '\n' |
| 4173 | ' Note: "print()" can also be used, but is not a debugger ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4174 | 'command —\n' |
| 4175 | ' this executes the Python "print()" function.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4176 | '\n' |
| 4177 | 'pp expression\n' |
| 4178 | '\n' |
| 4179 | ' Like the "p" command, except the value of the expression is ' |
| 4180 | 'pretty-\n' |
| 4181 | ' printed using the "pprint" module.\n' |
| 4182 | '\n' |
| 4183 | 'whatis expression\n' |
| 4184 | '\n' |
| 4185 | ' Print the type of the *expression*.\n' |
| 4186 | '\n' |
| 4187 | 'source expression\n' |
| 4188 | '\n' |
| 4189 | ' Try to get source code for the given object and display it.\n' |
| 4190 | '\n' |
| 4191 | ' New in version 3.2.\n' |
| 4192 | '\n' |
| 4193 | 'display [expression]\n' |
| 4194 | '\n' |
| 4195 | ' Display the value of the expression if it changed, each time\n' |
| 4196 | ' execution stops in the current frame.\n' |
| 4197 | '\n' |
| 4198 | ' Without expression, list all display expressions for the ' |
| 4199 | 'current\n' |
| 4200 | ' frame.\n' |
| 4201 | '\n' |
| 4202 | ' New in version 3.2.\n' |
| 4203 | '\n' |
| 4204 | 'undisplay [expression]\n' |
| 4205 | '\n' |
| 4206 | ' Do not display the expression any more in the current frame.\n' |
| 4207 | ' Without expression, clear all display expressions for the ' |
| 4208 | 'current\n' |
| 4209 | ' frame.\n' |
| 4210 | '\n' |
| 4211 | ' New in version 3.2.\n' |
| 4212 | '\n' |
| 4213 | 'interact\n' |
| 4214 | '\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 4215 | ' Start an interactive interpreter (using the "code" module) ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4216 | 'whose\n' |
| 4217 | ' global namespace contains all the (global and local) names ' |
| 4218 | 'found in\n' |
| 4219 | ' the current scope.\n' |
| 4220 | '\n' |
| 4221 | ' New in version 3.2.\n' |
| 4222 | '\n' |
| 4223 | 'alias [name [command]]\n' |
| 4224 | '\n' |
| 4225 | ' Create an alias called *name* that executes *command*. The ' |
| 4226 | 'command\n' |
| 4227 | ' must *not* be enclosed in quotes. Replaceable parameters can ' |
| 4228 | 'be\n' |
| 4229 | ' indicated by "%1", "%2", and so on, while "%*" is replaced by ' |
| 4230 | 'all\n' |
| 4231 | ' the parameters. If no command is given, the current alias ' |
| 4232 | 'for\n' |
| 4233 | ' *name* is shown. If no arguments are given, all aliases are ' |
| 4234 | 'listed.\n' |
| 4235 | '\n' |
| 4236 | ' Aliases may be nested and can contain anything that can be ' |
| 4237 | 'legally\n' |
| 4238 | ' typed at the pdb prompt. Note that internal pdb commands ' |
| 4239 | '*can* be\n' |
| 4240 | ' overridden by aliases. Such a command is then hidden until ' |
| 4241 | 'the\n' |
| 4242 | ' alias is removed. Aliasing is recursively applied to the ' |
| 4243 | 'first\n' |
| 4244 | ' word of the command line; all other words in the line are ' |
| 4245 | 'left\n' |
| 4246 | ' alone.\n' |
| 4247 | '\n' |
| 4248 | ' As an example, here are two useful aliases (especially when ' |
| 4249 | 'placed\n' |
| 4250 | ' in the ".pdbrc" file):\n' |
| 4251 | '\n' |
| 4252 | ' # Print instance variables (usage "pi classInst")\n' |
| 4253 | ' alias pi for k in %1.__dict__.keys(): ' |
| 4254 | 'print("%1.",k,"=",%1.__dict__[k])\n' |
| 4255 | ' # Print instance variables in self\n' |
| 4256 | ' alias ps pi self\n' |
| 4257 | '\n' |
| 4258 | 'unalias name\n' |
| 4259 | '\n' |
| 4260 | ' Delete the specified alias.\n' |
| 4261 | '\n' |
| 4262 | '! statement\n' |
| 4263 | '\n' |
| 4264 | ' Execute the (one-line) *statement* in the context of the ' |
| 4265 | 'current\n' |
| 4266 | ' stack frame. The exclamation point can be omitted unless the ' |
| 4267 | 'first\n' |
| 4268 | ' word of the statement resembles a debugger command. To set ' |
| 4269 | 'a\n' |
| 4270 | ' global variable, you can prefix the assignment command with ' |
| 4271 | 'a\n' |
| 4272 | ' "global" statement on the same line, e.g.:\n' |
| 4273 | '\n' |
| 4274 | " (Pdb) global list_options; list_options = ['-l']\n" |
| 4275 | ' (Pdb)\n' |
| 4276 | '\n' |
| 4277 | 'run [args ...]\n' |
| 4278 | 'restart [args ...]\n' |
| 4279 | '\n' |
| 4280 | ' Restart the debugged Python program. If an argument is ' |
| 4281 | 'supplied,\n' |
| 4282 | ' it is split with "shlex" and the result is used as the new\n' |
| 4283 | ' "sys.argv". History, breakpoints, actions and debugger ' |
| 4284 | 'options are\n' |
| 4285 | ' preserved. "restart" is an alias for "run".\n' |
| 4286 | '\n' |
| 4287 | 'q(uit)\n' |
| 4288 | '\n' |
| 4289 | ' Quit from the debugger. The program being executed is ' |
| 4290 | 'aborted.\n' |
| 4291 | '\n' |
Łukasz Langa | 6202d85 | 2019-12-18 22:09:19 +0100 | [diff] [blame] | 4292 | 'debug code\n' |
| 4293 | '\n' |
| 4294 | ' Enter a recursive debugger that steps through the code ' |
| 4295 | 'argument\n' |
| 4296 | ' (which is an arbitrary expression or statement to be executed ' |
| 4297 | 'in\n' |
| 4298 | ' the current environment).\n' |
| 4299 | '\n' |
| 4300 | 'retval\n' |
Łukasz Langa | 69dec9c | 2020-07-02 19:57:45 +0200 | [diff] [blame] | 4301 | '\n' |
| 4302 | ' Print the return value for the last return of a function.\n' |
Łukasz Langa | 6202d85 | 2019-12-18 22:09:19 +0100 | [diff] [blame] | 4303 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4304 | '-[ Footnotes ]-\n' |
| 4305 | '\n' |
| 4306 | '[1] Whether a frame is considered to originate in a certain ' |
| 4307 | 'module\n' |
| 4308 | ' is determined by the "__name__" in the frame globals.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 4309 | 'del': 'The "del" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4310 | '*******************\n' |
| 4311 | '\n' |
| 4312 | ' del_stmt ::= "del" target_list\n' |
| 4313 | '\n' |
| 4314 | 'Deletion is recursively defined very similar to the way assignment ' |
| 4315 | 'is\n' |
| 4316 | 'defined. Rather than spelling it out in full details, here are some\n' |
| 4317 | 'hints.\n' |
| 4318 | '\n' |
| 4319 | 'Deletion of a target list recursively deletes each target, from left\n' |
| 4320 | 'to right.\n' |
| 4321 | '\n' |
| 4322 | 'Deletion of a name removes the binding of that name from the local ' |
| 4323 | 'or\n' |
| 4324 | 'global namespace, depending on whether the name occurs in a "global"\n' |
| 4325 | 'statement in the same code block. If the name is unbound, a\n' |
| 4326 | '"NameError" exception will be raised.\n' |
| 4327 | '\n' |
| 4328 | 'Deletion of attribute references, subscriptions and slicings is ' |
| 4329 | 'passed\n' |
| 4330 | 'to the primary object involved; deletion of a slicing is in general\n' |
| 4331 | 'equivalent to assignment of an empty slice of the right type (but ' |
| 4332 | 'even\n' |
| 4333 | 'this is determined by the sliced object).\n' |
| 4334 | '\n' |
| 4335 | 'Changed in version 3.2: Previously it was illegal to delete a name\n' |
| 4336 | 'from the local namespace if it occurs as a free variable in a nested\n' |
| 4337 | 'block.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 4338 | 'dict': 'Dictionary displays\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4339 | '*******************\n' |
| 4340 | '\n' |
| 4341 | 'A dictionary display is a possibly empty series of key/datum pairs\n' |
| 4342 | 'enclosed in curly braces:\n' |
| 4343 | '\n' |
| 4344 | ' dict_display ::= "{" [key_datum_list | dict_comprehension] ' |
| 4345 | '"}"\n' |
| 4346 | ' key_datum_list ::= key_datum ("," key_datum)* [","]\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 4347 | ' key_datum ::= expression ":" expression | "**" or_expr\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4348 | ' dict_comprehension ::= expression ":" expression comp_for\n' |
| 4349 | '\n' |
| 4350 | 'A dictionary display yields a new dictionary object.\n' |
| 4351 | '\n' |
| 4352 | 'If a comma-separated sequence of key/datum pairs is given, they are\n' |
| 4353 | 'evaluated from left to right to define the entries of the ' |
| 4354 | 'dictionary:\n' |
| 4355 | 'each key object is used as a key into the dictionary to store the\n' |
| 4356 | 'corresponding datum. This means that you can specify the same key\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4357 | 'multiple times in the key/datum list, and the final dictionary’s ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4358 | 'value\n' |
| 4359 | 'for that key will be the last one given.\n' |
| 4360 | '\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 4361 | 'A double asterisk "**" denotes *dictionary unpacking*. Its operand\n' |
| 4362 | 'must be a *mapping*. Each mapping item is added to the new\n' |
| 4363 | 'dictionary. Later values replace values already set by earlier\n' |
| 4364 | 'key/datum pairs and earlier dictionary unpackings.\n' |
| 4365 | '\n' |
| 4366 | 'New in version 3.5: Unpacking into dictionary displays, originally\n' |
| 4367 | 'proposed by **PEP 448**.\n' |
| 4368 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4369 | 'A dict comprehension, in contrast to list and set comprehensions,\n' |
| 4370 | 'needs two expressions separated with a colon followed by the usual\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4371 | '“for” and “if” clauses. When the comprehension is run, the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4372 | 'resulting\n' |
| 4373 | 'key and value elements are inserted in the new dictionary in the ' |
| 4374 | 'order\n' |
| 4375 | 'they are produced.\n' |
| 4376 | '\n' |
| 4377 | 'Restrictions on the types of the key values are listed earlier in\n' |
| 4378 | 'section The standard type hierarchy. (To summarize, the key type\n' |
| 4379 | 'should be *hashable*, which excludes all mutable objects.) Clashes\n' |
| 4380 | 'between duplicate keys are not detected; the last datum (textually\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 4381 | 'rightmost in the display) stored for a given key value prevails.\n' |
| 4382 | '\n' |
| 4383 | 'Changed in version 3.8: Prior to Python 3.8, in dict ' |
| 4384 | 'comprehensions,\n' |
| 4385 | 'the evaluation order of key and value was not well-defined. In\n' |
| 4386 | 'CPython, the value was evaluated before the key. Starting with ' |
| 4387 | '3.8,\n' |
| 4388 | 'the key is evaluated before the value, as proposed by **PEP 572**.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 4389 | 'dynamic-features': 'Interaction with dynamic features\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4390 | '*********************************\n' |
| 4391 | '\n' |
| 4392 | 'Name resolution of free variables occurs at runtime, not ' |
| 4393 | 'at compile\n' |
| 4394 | 'time. This means that the following code will print 42:\n' |
| 4395 | '\n' |
| 4396 | ' i = 10\n' |
| 4397 | ' def f():\n' |
| 4398 | ' print(i)\n' |
| 4399 | ' i = 42\n' |
| 4400 | ' f()\n' |
| 4401 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4402 | 'The "eval()" and "exec()" functions do not have access ' |
| 4403 | 'to the full\n' |
| 4404 | 'environment for resolving names. Names may be resolved ' |
| 4405 | 'in the local\n' |
| 4406 | 'and global namespaces of the caller. Free variables are ' |
| 4407 | 'not resolved\n' |
| 4408 | 'in the nearest enclosing namespace, but in the global ' |
| 4409 | 'namespace. [1]\n' |
| 4410 | 'The "exec()" and "eval()" functions have optional ' |
| 4411 | 'arguments to\n' |
| 4412 | 'override the global and local namespace. If only one ' |
| 4413 | 'namespace is\n' |
| 4414 | 'specified, it is used for both.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 4415 | 'else': 'The "if" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4416 | '******************\n' |
| 4417 | '\n' |
| 4418 | 'The "if" statement is used for conditional execution:\n' |
| 4419 | '\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 4420 | ' if_stmt ::= "if" assignment_expression ":" suite\n' |
| 4421 | ' ("elif" assignment_expression ":" suite)*\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4422 | ' ["else" ":" suite]\n' |
| 4423 | '\n' |
| 4424 | 'It selects exactly one of the suites by evaluating the expressions ' |
| 4425 | 'one\n' |
| 4426 | 'by one until one is found to be true (see section Boolean ' |
| 4427 | 'operations\n' |
| 4428 | 'for the definition of true and false); then that suite is executed\n' |
| 4429 | '(and no other part of the "if" statement is executed or evaluated).\n' |
| 4430 | 'If all expressions are false, the suite of the "else" clause, if\n' |
| 4431 | 'present, is executed.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 4432 | 'exceptions': 'Exceptions\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4433 | '**********\n' |
| 4434 | '\n' |
| 4435 | 'Exceptions are a means of breaking out of the normal flow of ' |
| 4436 | 'control\n' |
| 4437 | 'of a code block in order to handle errors or other ' |
| 4438 | 'exceptional\n' |
| 4439 | 'conditions. An exception is *raised* at the point where the ' |
| 4440 | 'error is\n' |
| 4441 | 'detected; it may be *handled* by the surrounding code block or ' |
| 4442 | 'by any\n' |
| 4443 | 'code block that directly or indirectly invoked the code block ' |
| 4444 | 'where\n' |
| 4445 | 'the error occurred.\n' |
| 4446 | '\n' |
| 4447 | 'The Python interpreter raises an exception when it detects a ' |
| 4448 | 'run-time\n' |
| 4449 | 'error (such as division by zero). A Python program can also\n' |
| 4450 | 'explicitly raise an exception with the "raise" statement. ' |
| 4451 | 'Exception\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4452 | 'handlers are specified with the "try" … "except" statement. ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4453 | 'The\n' |
| 4454 | '"finally" clause of such a statement can be used to specify ' |
| 4455 | 'cleanup\n' |
| 4456 | 'code which does not handle the exception, but is executed ' |
| 4457 | 'whether an\n' |
| 4458 | 'exception occurred or not in the preceding code.\n' |
| 4459 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4460 | 'Python uses the “termination” model of error handling: an ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4461 | 'exception\n' |
| 4462 | 'handler can find out what happened and continue execution at ' |
| 4463 | 'an outer\n' |
| 4464 | 'level, but it cannot repair the cause of the error and retry ' |
| 4465 | 'the\n' |
| 4466 | 'failing operation (except by re-entering the offending piece ' |
| 4467 | 'of code\n' |
| 4468 | 'from the top).\n' |
| 4469 | '\n' |
| 4470 | 'When an exception is not handled at all, the interpreter ' |
| 4471 | 'terminates\n' |
| 4472 | 'execution of the program, or returns to its interactive main ' |
| 4473 | 'loop. In\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 4474 | 'either case, it prints a stack traceback, except when the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4475 | 'exception is\n' |
| 4476 | '"SystemExit".\n' |
| 4477 | '\n' |
| 4478 | 'Exceptions are identified by class instances. The "except" ' |
| 4479 | 'clause is\n' |
| 4480 | 'selected depending on the class of the instance: it must ' |
| 4481 | 'reference the\n' |
| 4482 | 'class of the instance or a base class thereof. The instance ' |
| 4483 | 'can be\n' |
| 4484 | 'received by the handler and can carry additional information ' |
| 4485 | 'about the\n' |
| 4486 | 'exceptional condition.\n' |
| 4487 | '\n' |
| 4488 | 'Note: Exception messages are not part of the Python API. ' |
| 4489 | 'Their\n' |
| 4490 | ' contents may change from one version of Python to the next ' |
| 4491 | 'without\n' |
| 4492 | ' warning and should not be relied on by code which will run ' |
| 4493 | 'under\n' |
| 4494 | ' multiple versions of the interpreter.\n' |
| 4495 | '\n' |
| 4496 | 'See also the description of the "try" statement in section The ' |
| 4497 | 'try\n' |
| 4498 | 'statement and "raise" statement in section The raise ' |
| 4499 | 'statement.\n' |
| 4500 | '\n' |
| 4501 | '-[ Footnotes ]-\n' |
| 4502 | '\n' |
| 4503 | '[1] This limitation occurs because the code that is executed ' |
| 4504 | 'by\n' |
| 4505 | ' these operations is not available at the time the module ' |
| 4506 | 'is\n' |
| 4507 | ' compiled.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 4508 | 'execmodel': 'Execution model\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4509 | '***************\n' |
| 4510 | '\n' |
| 4511 | '\n' |
| 4512 | 'Structure of a program\n' |
| 4513 | '======================\n' |
| 4514 | '\n' |
| 4515 | 'A Python program is constructed from code blocks. A *block* is ' |
| 4516 | 'a piece\n' |
| 4517 | 'of Python program text that is executed as a unit. The ' |
| 4518 | 'following are\n' |
| 4519 | 'blocks: a module, a function body, and a class definition. ' |
| 4520 | 'Each\n' |
| 4521 | 'command typed interactively is a block. A script file (a file ' |
| 4522 | 'given\n' |
| 4523 | 'as standard input to the interpreter or specified as a command ' |
| 4524 | 'line\n' |
| 4525 | 'argument to the interpreter) is a code block. A script command ' |
| 4526 | '(a\n' |
| 4527 | 'command specified on the interpreter command line with the ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4528 | '"-c"\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 4529 | 'option) is a code block. A module run as a top level script (as ' |
| 4530 | 'module\n' |
| 4531 | '"__main__") from the command line using a "-m" argument is also ' |
| 4532 | 'a code\n' |
| 4533 | 'block. The string argument passed to the built-in functions ' |
| 4534 | '"eval()"\n' |
| 4535 | 'and "exec()" is a code block.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4536 | '\n' |
| 4537 | 'A code block is executed in an *execution frame*. A frame ' |
| 4538 | 'contains\n' |
| 4539 | 'some administrative information (used for debugging) and ' |
| 4540 | 'determines\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4541 | 'where and how execution continues after the code block’s ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4542 | 'execution has\n' |
| 4543 | 'completed.\n' |
| 4544 | '\n' |
| 4545 | '\n' |
| 4546 | 'Naming and binding\n' |
| 4547 | '==================\n' |
| 4548 | '\n' |
| 4549 | '\n' |
| 4550 | 'Binding of names\n' |
| 4551 | '----------------\n' |
| 4552 | '\n' |
| 4553 | '*Names* refer to objects. Names are introduced by name ' |
| 4554 | 'binding\n' |
| 4555 | 'operations.\n' |
| 4556 | '\n' |
| 4557 | 'The following constructs bind names: formal parameters to ' |
| 4558 | 'functions,\n' |
| 4559 | '"import" statements, class and function definitions (these bind ' |
| 4560 | 'the\n' |
| 4561 | 'class or function name in the defining block), and targets that ' |
| 4562 | 'are\n' |
| 4563 | 'identifiers if occurring in an assignment, "for" loop header, ' |
| 4564 | 'or after\n' |
| 4565 | '"as" in a "with" statement or "except" clause. The "import" ' |
| 4566 | 'statement\n' |
| 4567 | 'of the form "from ... import *" binds all names defined in the\n' |
| 4568 | 'imported module, except those beginning with an underscore. ' |
| 4569 | 'This form\n' |
| 4570 | 'may only be used at the module level.\n' |
| 4571 | '\n' |
| 4572 | 'A target occurring in a "del" statement is also considered ' |
| 4573 | 'bound for\n' |
| 4574 | 'this purpose (though the actual semantics are to unbind the ' |
| 4575 | 'name).\n' |
| 4576 | '\n' |
| 4577 | 'Each assignment or import statement occurs within a block ' |
| 4578 | 'defined by a\n' |
| 4579 | 'class or function definition or at the module level (the ' |
| 4580 | 'top-level\n' |
| 4581 | 'code block).\n' |
| 4582 | '\n' |
| 4583 | 'If a name is bound in a block, it is a local variable of that ' |
| 4584 | 'block,\n' |
| 4585 | 'unless declared as "nonlocal" or "global". If a name is bound ' |
| 4586 | 'at the\n' |
| 4587 | 'module level, it is a global variable. (The variables of the ' |
| 4588 | 'module\n' |
| 4589 | 'code block are local and global.) If a variable is used in a ' |
| 4590 | 'code\n' |
| 4591 | 'block but not defined there, it is a *free variable*.\n' |
| 4592 | '\n' |
| 4593 | 'Each occurrence of a name in the program text refers to the ' |
| 4594 | '*binding*\n' |
| 4595 | 'of that name established by the following name resolution ' |
| 4596 | 'rules.\n' |
| 4597 | '\n' |
| 4598 | '\n' |
| 4599 | 'Resolution of names\n' |
| 4600 | '-------------------\n' |
| 4601 | '\n' |
| 4602 | 'A *scope* defines the visibility of a name within a block. If ' |
| 4603 | 'a local\n' |
| 4604 | 'variable is defined in a block, its scope includes that block. ' |
| 4605 | 'If the\n' |
| 4606 | 'definition occurs in a function block, the scope extends to any ' |
| 4607 | 'blocks\n' |
| 4608 | 'contained within the defining one, unless a contained block ' |
| 4609 | 'introduces\n' |
| 4610 | 'a different binding for the name.\n' |
| 4611 | '\n' |
| 4612 | 'When a name is used in a code block, it is resolved using the ' |
| 4613 | 'nearest\n' |
| 4614 | 'enclosing scope. The set of all such scopes visible to a code ' |
| 4615 | 'block\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4616 | 'is called the block’s *environment*.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4617 | '\n' |
| 4618 | 'When a name is not found at all, a "NameError" exception is ' |
| 4619 | 'raised. If\n' |
| 4620 | 'the current scope is a function scope, and the name refers to a ' |
| 4621 | 'local\n' |
| 4622 | 'variable that has not yet been bound to a value at the point ' |
| 4623 | 'where the\n' |
| 4624 | 'name is used, an "UnboundLocalError" exception is raised.\n' |
| 4625 | '"UnboundLocalError" is a subclass of "NameError".\n' |
| 4626 | '\n' |
| 4627 | 'If a name binding operation occurs anywhere within a code ' |
| 4628 | 'block, all\n' |
| 4629 | 'uses of the name within the block are treated as references to ' |
| 4630 | 'the\n' |
| 4631 | 'current block. This can lead to errors when a name is used ' |
| 4632 | 'within a\n' |
| 4633 | 'block before it is bound. This rule is subtle. Python lacks\n' |
| 4634 | 'declarations and allows name binding operations to occur ' |
| 4635 | 'anywhere\n' |
| 4636 | 'within a code block. The local variables of a code block can ' |
| 4637 | 'be\n' |
| 4638 | 'determined by scanning the entire text of the block for name ' |
| 4639 | 'binding\n' |
| 4640 | 'operations.\n' |
| 4641 | '\n' |
| 4642 | 'If the "global" statement occurs within a block, all uses of ' |
| 4643 | 'the name\n' |
| 4644 | 'specified in the statement refer to the binding of that name in ' |
| 4645 | 'the\n' |
| 4646 | 'top-level namespace. Names are resolved in the top-level ' |
| 4647 | 'namespace by\n' |
| 4648 | 'searching the global namespace, i.e. the namespace of the ' |
| 4649 | 'module\n' |
| 4650 | 'containing the code block, and the builtins namespace, the ' |
| 4651 | 'namespace\n' |
| 4652 | 'of the module "builtins". The global namespace is searched ' |
| 4653 | 'first. If\n' |
| 4654 | 'the name is not found there, the builtins namespace is ' |
| 4655 | 'searched. The\n' |
| 4656 | '"global" statement must precede all uses of the name.\n' |
| 4657 | '\n' |
| 4658 | 'The "global" statement has the same scope as a name binding ' |
| 4659 | 'operation\n' |
| 4660 | 'in the same block. If the nearest enclosing scope for a free ' |
| 4661 | 'variable\n' |
| 4662 | 'contains a global statement, the free variable is treated as a ' |
| 4663 | 'global.\n' |
| 4664 | '\n' |
| 4665 | 'The "nonlocal" statement causes corresponding names to refer ' |
| 4666 | 'to\n' |
| 4667 | 'previously bound variables in the nearest enclosing function ' |
| 4668 | 'scope.\n' |
| 4669 | '"SyntaxError" is raised at compile time if the given name does ' |
| 4670 | 'not\n' |
| 4671 | 'exist in any enclosing function scope.\n' |
| 4672 | '\n' |
| 4673 | 'The namespace for a module is automatically created the first ' |
| 4674 | 'time a\n' |
| 4675 | 'module is imported. The main module for a script is always ' |
| 4676 | 'called\n' |
| 4677 | '"__main__".\n' |
| 4678 | '\n' |
| 4679 | 'Class definition blocks and arguments to "exec()" and "eval()" ' |
| 4680 | 'are\n' |
| 4681 | 'special in the context of name resolution. A class definition ' |
| 4682 | 'is an\n' |
| 4683 | 'executable statement that may use and define names. These ' |
| 4684 | 'references\n' |
| 4685 | 'follow the normal rules for name resolution with an exception ' |
| 4686 | 'that\n' |
| 4687 | 'unbound local variables are looked up in the global namespace. ' |
| 4688 | 'The\n' |
| 4689 | 'namespace of the class definition becomes the attribute ' |
| 4690 | 'dictionary of\n' |
| 4691 | 'the class. The scope of names defined in a class block is ' |
| 4692 | 'limited to\n' |
| 4693 | 'the class block; it does not extend to the code blocks of ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4694 | 'methods –\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4695 | 'this includes comprehensions and generator expressions since ' |
| 4696 | 'they are\n' |
| 4697 | 'implemented using a function scope. This means that the ' |
| 4698 | 'following\n' |
| 4699 | 'will fail:\n' |
| 4700 | '\n' |
| 4701 | ' class A:\n' |
| 4702 | ' a = 42\n' |
| 4703 | ' b = list(a + i for i in range(10))\n' |
| 4704 | '\n' |
| 4705 | '\n' |
| 4706 | 'Builtins and restricted execution\n' |
| 4707 | '---------------------------------\n' |
| 4708 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 4709 | '**CPython implementation detail:** Users should not touch\n' |
| 4710 | '"__builtins__"; it is strictly an implementation detail. ' |
| 4711 | 'Users\n' |
| 4712 | 'wanting to override values in the builtins namespace should ' |
| 4713 | '"import"\n' |
| 4714 | 'the "builtins" module and modify its attributes appropriately.\n' |
| 4715 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4716 | 'The builtins namespace associated with the execution of a code ' |
| 4717 | 'block\n' |
| 4718 | 'is actually found by looking up the name "__builtins__" in its ' |
| 4719 | 'global\n' |
| 4720 | 'namespace; this should be a dictionary or a module (in the ' |
| 4721 | 'latter case\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4722 | 'the module’s dictionary is used). By default, when in the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4723 | '"__main__"\n' |
| 4724 | 'module, "__builtins__" is the built-in module "builtins"; when ' |
| 4725 | 'in any\n' |
| 4726 | 'other module, "__builtins__" is an alias for the dictionary of ' |
| 4727 | 'the\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 4728 | '"builtins" module itself.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4729 | '\n' |
| 4730 | '\n' |
| 4731 | 'Interaction with dynamic features\n' |
| 4732 | '---------------------------------\n' |
| 4733 | '\n' |
| 4734 | 'Name resolution of free variables occurs at runtime, not at ' |
| 4735 | 'compile\n' |
| 4736 | 'time. This means that the following code will print 42:\n' |
| 4737 | '\n' |
| 4738 | ' i = 10\n' |
| 4739 | ' def f():\n' |
| 4740 | ' print(i)\n' |
| 4741 | ' i = 42\n' |
| 4742 | ' f()\n' |
| 4743 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4744 | 'The "eval()" and "exec()" functions do not have access to the ' |
| 4745 | 'full\n' |
| 4746 | 'environment for resolving names. Names may be resolved in the ' |
| 4747 | 'local\n' |
| 4748 | 'and global namespaces of the caller. Free variables are not ' |
| 4749 | 'resolved\n' |
| 4750 | 'in the nearest enclosing namespace, but in the global ' |
| 4751 | 'namespace. [1]\n' |
| 4752 | 'The "exec()" and "eval()" functions have optional arguments to\n' |
| 4753 | 'override the global and local namespace. If only one namespace ' |
| 4754 | 'is\n' |
| 4755 | 'specified, it is used for both.\n' |
| 4756 | '\n' |
| 4757 | '\n' |
| 4758 | 'Exceptions\n' |
| 4759 | '==========\n' |
| 4760 | '\n' |
| 4761 | 'Exceptions are a means of breaking out of the normal flow of ' |
| 4762 | 'control\n' |
| 4763 | 'of a code block in order to handle errors or other exceptional\n' |
| 4764 | 'conditions. An exception is *raised* at the point where the ' |
| 4765 | 'error is\n' |
| 4766 | 'detected; it may be *handled* by the surrounding code block or ' |
| 4767 | 'by any\n' |
| 4768 | 'code block that directly or indirectly invoked the code block ' |
| 4769 | 'where\n' |
| 4770 | 'the error occurred.\n' |
| 4771 | '\n' |
| 4772 | 'The Python interpreter raises an exception when it detects a ' |
| 4773 | 'run-time\n' |
| 4774 | 'error (such as division by zero). A Python program can also\n' |
| 4775 | 'explicitly raise an exception with the "raise" statement. ' |
| 4776 | 'Exception\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4777 | 'handlers are specified with the "try" … "except" statement. ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4778 | 'The\n' |
| 4779 | '"finally" clause of such a statement can be used to specify ' |
| 4780 | 'cleanup\n' |
| 4781 | 'code which does not handle the exception, but is executed ' |
| 4782 | 'whether an\n' |
| 4783 | 'exception occurred or not in the preceding code.\n' |
| 4784 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4785 | 'Python uses the “termination” model of error handling: an ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4786 | 'exception\n' |
| 4787 | 'handler can find out what happened and continue execution at an ' |
| 4788 | 'outer\n' |
| 4789 | 'level, but it cannot repair the cause of the error and retry ' |
| 4790 | 'the\n' |
| 4791 | 'failing operation (except by re-entering the offending piece of ' |
| 4792 | 'code\n' |
| 4793 | 'from the top).\n' |
| 4794 | '\n' |
| 4795 | 'When an exception is not handled at all, the interpreter ' |
| 4796 | 'terminates\n' |
| 4797 | 'execution of the program, or returns to its interactive main ' |
| 4798 | 'loop. In\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 4799 | 'either case, it prints a stack traceback, except when the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4800 | 'exception is\n' |
| 4801 | '"SystemExit".\n' |
| 4802 | '\n' |
| 4803 | 'Exceptions are identified by class instances. The "except" ' |
| 4804 | 'clause is\n' |
| 4805 | 'selected depending on the class of the instance: it must ' |
| 4806 | 'reference the\n' |
| 4807 | 'class of the instance or a base class thereof. The instance ' |
| 4808 | 'can be\n' |
| 4809 | 'received by the handler and can carry additional information ' |
| 4810 | 'about the\n' |
| 4811 | 'exceptional condition.\n' |
| 4812 | '\n' |
| 4813 | 'Note: Exception messages are not part of the Python API. ' |
| 4814 | 'Their\n' |
| 4815 | ' contents may change from one version of Python to the next ' |
| 4816 | 'without\n' |
| 4817 | ' warning and should not be relied on by code which will run ' |
| 4818 | 'under\n' |
| 4819 | ' multiple versions of the interpreter.\n' |
| 4820 | '\n' |
| 4821 | 'See also the description of the "try" statement in section The ' |
| 4822 | 'try\n' |
| 4823 | 'statement and "raise" statement in section The raise ' |
| 4824 | 'statement.\n' |
| 4825 | '\n' |
| 4826 | '-[ Footnotes ]-\n' |
| 4827 | '\n' |
| 4828 | '[1] This limitation occurs because the code that is executed ' |
| 4829 | 'by\n' |
| 4830 | ' these operations is not available at the time the module ' |
| 4831 | 'is\n' |
| 4832 | ' compiled.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 4833 | 'exprlists': 'Expression lists\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4834 | '****************\n' |
| 4835 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4836 | ' expression_list ::= expression ("," expression)* [","]\n' |
| 4837 | ' starred_list ::= starred_item ("," starred_item)* ' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 4838 | '[","]\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4839 | ' starred_expression ::= expression | (starred_item ",")* ' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 4840 | '[starred_item]\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 4841 | ' starred_item ::= assignment_expression | "*" or_expr\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4842 | '\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 4843 | 'Except when part of a list or set display, an expression list\n' |
| 4844 | 'containing at least one comma yields a tuple. The length of ' |
| 4845 | 'the tuple\n' |
| 4846 | 'is the number of expressions in the list. The expressions are\n' |
| 4847 | 'evaluated from left to right.\n' |
| 4848 | '\n' |
| 4849 | 'An asterisk "*" denotes *iterable unpacking*. Its operand must ' |
| 4850 | 'be an\n' |
| 4851 | '*iterable*. The iterable is expanded into a sequence of items, ' |
| 4852 | 'which\n' |
| 4853 | 'are included in the new tuple, list, or set, at the site of ' |
| 4854 | 'the\n' |
| 4855 | 'unpacking.\n' |
| 4856 | '\n' |
| 4857 | 'New in version 3.5: Iterable unpacking in expression lists, ' |
| 4858 | 'originally\n' |
| 4859 | 'proposed by **PEP 448**.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4860 | '\n' |
| 4861 | 'The trailing comma is required only to create a single tuple ' |
| 4862 | '(a.k.a. a\n' |
| 4863 | '*singleton*); it is optional in all other cases. A single ' |
| 4864 | 'expression\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4865 | 'without a trailing comma doesn’t create a tuple, but rather ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4866 | 'yields the\n' |
| 4867 | 'value of that expression. (To create an empty tuple, use an ' |
| 4868 | 'empty pair\n' |
| 4869 | 'of parentheses: "()".)\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 4870 | 'floating': 'Floating point literals\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4871 | '***********************\n' |
| 4872 | '\n' |
| 4873 | 'Floating point literals are described by the following lexical\n' |
| 4874 | 'definitions:\n' |
| 4875 | '\n' |
| 4876 | ' floatnumber ::= pointfloat | exponentfloat\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 4877 | ' pointfloat ::= [digitpart] fraction | digitpart "."\n' |
| 4878 | ' exponentfloat ::= (digitpart | pointfloat) exponent\n' |
| 4879 | ' digitpart ::= digit (["_"] digit)*\n' |
| 4880 | ' fraction ::= "." digitpart\n' |
| 4881 | ' exponent ::= ("e" | "E") ["+" | "-"] digitpart\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4882 | '\n' |
| 4883 | 'Note that the integer and exponent parts are always interpreted ' |
| 4884 | 'using\n' |
| 4885 | 'radix 10. For example, "077e010" is legal, and denotes the same ' |
| 4886 | 'number\n' |
| 4887 | 'as "77e10". The allowed range of floating point literals is\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 4888 | 'implementation-dependent. As in integer literals, underscores ' |
| 4889 | 'are\n' |
| 4890 | 'supported for digit grouping.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4891 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 4892 | 'Some examples of floating point literals:\n' |
| 4893 | '\n' |
| 4894 | ' 3.14 10. .001 1e100 3.14e-10 0e0 ' |
| 4895 | '3.14_15_93\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4896 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 4897 | 'Changed in version 3.6: Underscores are now allowed for ' |
| 4898 | 'grouping\n' |
| 4899 | 'purposes in literals.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 4900 | 'for': 'The "for" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4901 | '*******************\n' |
| 4902 | '\n' |
| 4903 | 'The "for" statement is used to iterate over the elements of a ' |
| 4904 | 'sequence\n' |
| 4905 | '(such as a string, tuple or list) or other iterable object:\n' |
| 4906 | '\n' |
| 4907 | ' for_stmt ::= "for" target_list "in" expression_list ":" suite\n' |
| 4908 | ' ["else" ":" suite]\n' |
| 4909 | '\n' |
| 4910 | 'The expression list is evaluated once; it should yield an iterable\n' |
| 4911 | 'object. An iterator is created for the result of the\n' |
| 4912 | '"expression_list". The suite is then executed once for each item\n' |
| 4913 | 'provided by the iterator, in the order returned by the iterator. ' |
| 4914 | 'Each\n' |
| 4915 | 'item in turn is assigned to the target list using the standard rules\n' |
| 4916 | 'for assignments (see Assignment statements), and then the suite is\n' |
| 4917 | 'executed. When the items are exhausted (which is immediately when ' |
| 4918 | 'the\n' |
| 4919 | 'sequence is empty or an iterator raises a "StopIteration" ' |
| 4920 | 'exception),\n' |
| 4921 | 'the suite in the "else" clause, if present, is executed, and the ' |
| 4922 | 'loop\n' |
| 4923 | 'terminates.\n' |
| 4924 | '\n' |
| 4925 | 'A "break" statement executed in the first suite terminates the loop\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4926 | 'without executing the "else" clause’s suite. A "continue" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4927 | 'executed in the first suite skips the rest of the suite and ' |
| 4928 | 'continues\n' |
| 4929 | 'with the next item, or with the "else" clause if there is no next\n' |
| 4930 | 'item.\n' |
| 4931 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4932 | 'The for-loop makes assignments to the variables in the target list.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4933 | 'This overwrites all previous assignments to those variables ' |
| 4934 | 'including\n' |
| 4935 | 'those made in the suite of the for-loop:\n' |
| 4936 | '\n' |
| 4937 | ' for i in range(10):\n' |
| 4938 | ' print(i)\n' |
| 4939 | ' i = 5 # this will not affect the for-loop\n' |
| 4940 | ' # because i will be overwritten with the ' |
| 4941 | 'next\n' |
| 4942 | ' # index in the range\n' |
| 4943 | '\n' |
| 4944 | 'Names in the target list are not deleted when the loop is finished,\n' |
| 4945 | 'but if the sequence is empty, they will not have been assigned to at\n' |
| 4946 | 'all by the loop. Hint: the built-in function "range()" returns an\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4947 | 'iterator of integers suitable to emulate the effect of Pascal’s "for ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4948 | 'i\n' |
| 4949 | ':= a to b do"; e.g., "list(range(3))" returns the list "[0, 1, 2]".\n' |
| 4950 | '\n' |
| 4951 | 'Note: There is a subtlety when the sequence is being modified by the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4952 | ' loop (this can only occur for mutable sequences, e.g. lists). An\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4953 | ' internal counter is used to keep track of which item is used next,\n' |
| 4954 | ' and this is incremented on each iteration. When this counter has\n' |
| 4955 | ' reached the length of the sequence the loop terminates. This ' |
| 4956 | 'means\n' |
| 4957 | ' that if the suite deletes the current (or a previous) item from ' |
| 4958 | 'the\n' |
| 4959 | ' sequence, the next item will be skipped (since it gets the index ' |
| 4960 | 'of\n' |
| 4961 | ' the current item which has already been treated). Likewise, if ' |
| 4962 | 'the\n' |
| 4963 | ' suite inserts an item in the sequence before the current item, the\n' |
| 4964 | ' current item will be treated again the next time through the loop.\n' |
| 4965 | ' This can lead to nasty bugs that can be avoided by making a\n' |
| 4966 | ' temporary copy using a slice of the whole sequence, e.g.,\n' |
| 4967 | '\n' |
| 4968 | ' for x in a[:]:\n' |
| 4969 | ' if x < 0: a.remove(x)\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 4970 | 'formatstrings': 'Format String Syntax\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4971 | '********************\n' |
| 4972 | '\n' |
| 4973 | 'The "str.format()" method and the "Formatter" class share ' |
| 4974 | 'the same\n' |
| 4975 | 'syntax for format strings (although in the case of ' |
| 4976 | '"Formatter",\n' |
| 4977 | 'subclasses can define their own format string syntax). The ' |
| 4978 | 'syntax is\n' |
| 4979 | 'related to that of formatted string literals, but there ' |
| 4980 | 'are\n' |
| 4981 | 'differences.\n' |
| 4982 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4983 | 'Format strings contain “replacement fields” surrounded by ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 4984 | 'curly braces\n' |
| 4985 | '"{}". Anything that is not contained in braces is ' |
| 4986 | 'considered literal\n' |
| 4987 | 'text, which is copied unchanged to the output. If you need ' |
| 4988 | 'to include\n' |
| 4989 | 'a brace character in the literal text, it can be escaped by ' |
| 4990 | 'doubling:\n' |
| 4991 | '"{{" and "}}".\n' |
| 4992 | '\n' |
| 4993 | 'The grammar for a replacement field is as follows:\n' |
| 4994 | '\n' |
| 4995 | ' replacement_field ::= "{" [field_name] ["!" ' |
| 4996 | 'conversion] [":" format_spec] "}"\n' |
| 4997 | ' field_name ::= arg_name ("." attribute_name | ' |
| 4998 | '"[" element_index "]")*\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 4999 | ' arg_name ::= [identifier | digit+]\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5000 | ' attribute_name ::= identifier\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5001 | ' element_index ::= digit+ | index_string\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5002 | ' index_string ::= <any source character except ' |
| 5003 | '"]"> +\n' |
| 5004 | ' conversion ::= "r" | "s" | "a"\n' |
| 5005 | ' format_spec ::= <described in the next ' |
| 5006 | 'section>\n' |
| 5007 | '\n' |
| 5008 | 'In less formal terms, the replacement field can start with ' |
| 5009 | 'a\n' |
| 5010 | '*field_name* that specifies the object whose value is to be ' |
| 5011 | 'formatted\n' |
| 5012 | 'and inserted into the output instead of the replacement ' |
| 5013 | 'field. The\n' |
| 5014 | '*field_name* is optionally followed by a *conversion* ' |
| 5015 | 'field, which is\n' |
| 5016 | 'preceded by an exclamation point "\'!\'", and a ' |
| 5017 | '*format_spec*, which is\n' |
| 5018 | 'preceded by a colon "\':\'". These specify a non-default ' |
| 5019 | 'format for the\n' |
| 5020 | 'replacement value.\n' |
| 5021 | '\n' |
| 5022 | 'See also the Format Specification Mini-Language section.\n' |
| 5023 | '\n' |
| 5024 | 'The *field_name* itself begins with an *arg_name* that is ' |
| 5025 | 'either a\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5026 | 'number or a keyword. If it’s a number, it refers to a ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5027 | 'positional\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5028 | 'argument, and if it’s a keyword, it refers to a named ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5029 | 'keyword\n' |
| 5030 | 'argument. If the numerical arg_names in a format string ' |
| 5031 | 'are 0, 1, 2,\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5032 | '… in sequence, they can all be omitted (not just some) and ' |
| 5033 | 'the numbers\n' |
| 5034 | '0, 1, 2, … will be automatically inserted in that order. ' |
| 5035 | 'Because\n' |
| 5036 | '*arg_name* is not quote-delimited, it is not possible to ' |
| 5037 | 'specify\n' |
| 5038 | 'arbitrary dictionary keys (e.g., the strings "\'10\'" or ' |
| 5039 | '"\':-]\'") within\n' |
| 5040 | 'a format string. The *arg_name* can be followed by any ' |
| 5041 | 'number of index\n' |
| 5042 | 'or attribute expressions. An expression of the form ' |
| 5043 | '"\'.name\'" selects\n' |
| 5044 | 'the named attribute using "getattr()", while an expression ' |
| 5045 | 'of the form\n' |
| 5046 | '"\'[index]\'" does an index lookup using "__getitem__()".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5047 | '\n' |
| 5048 | 'Changed in version 3.1: The positional argument specifiers ' |
| 5049 | 'can be\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5050 | 'omitted for "str.format()", so "\'{} {}\'.format(a, b)" is ' |
| 5051 | 'equivalent to\n' |
| 5052 | '"\'{0} {1}\'.format(a, b)".\n' |
| 5053 | '\n' |
| 5054 | 'Changed in version 3.4: The positional argument specifiers ' |
| 5055 | 'can be\n' |
| 5056 | 'omitted for "Formatter".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5057 | '\n' |
| 5058 | 'Some simple format string examples:\n' |
| 5059 | '\n' |
| 5060 | ' "First, thou shalt count to {0}" # References first ' |
| 5061 | 'positional argument\n' |
| 5062 | ' "Bring me a {}" # Implicitly ' |
| 5063 | 'references the first positional argument\n' |
| 5064 | ' "From {} to {}" # Same as "From {0} to ' |
| 5065 | '{1}"\n' |
| 5066 | ' "My quest is {name}" # References keyword ' |
| 5067 | "argument 'name'\n" |
| 5068 | ' "Weight in tons {0.weight}" # \'weight\' attribute ' |
| 5069 | 'of first positional arg\n' |
| 5070 | ' "Units destroyed: {players[0]}" # First element of ' |
| 5071 | "keyword argument 'players'.\n" |
| 5072 | '\n' |
| 5073 | 'The *conversion* field causes a type coercion before ' |
| 5074 | 'formatting.\n' |
| 5075 | 'Normally, the job of formatting a value is done by the ' |
| 5076 | '"__format__()"\n' |
| 5077 | 'method of the value itself. However, in some cases it is ' |
| 5078 | 'desirable to\n' |
| 5079 | 'force a type to be formatted as a string, overriding its ' |
| 5080 | 'own\n' |
| 5081 | 'definition of formatting. By converting the value to a ' |
| 5082 | 'string before\n' |
| 5083 | 'calling "__format__()", the normal formatting logic is ' |
| 5084 | 'bypassed.\n' |
| 5085 | '\n' |
| 5086 | 'Three conversion flags are currently supported: "\'!s\'" ' |
| 5087 | 'which calls\n' |
| 5088 | '"str()" on the value, "\'!r\'" which calls "repr()" and ' |
| 5089 | '"\'!a\'" which\n' |
| 5090 | 'calls "ascii()".\n' |
| 5091 | '\n' |
| 5092 | 'Some examples:\n' |
| 5093 | '\n' |
| 5094 | ' "Harold\'s a clever {0!s}" # Calls str() on the ' |
| 5095 | 'argument first\n' |
| 5096 | ' "Bring out the holy {name!r}" # Calls repr() on the ' |
| 5097 | 'argument first\n' |
| 5098 | ' "More {!a}" # Calls ascii() on the ' |
| 5099 | 'argument first\n' |
| 5100 | '\n' |
| 5101 | 'The *format_spec* field contains a specification of how the ' |
| 5102 | 'value\n' |
| 5103 | 'should be presented, including such details as field width, ' |
| 5104 | 'alignment,\n' |
| 5105 | 'padding, decimal precision and so on. Each value type can ' |
| 5106 | 'define its\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5107 | 'own “formatting mini-language” or interpretation of the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5108 | '*format_spec*.\n' |
| 5109 | '\n' |
| 5110 | 'Most built-in types support a common formatting ' |
| 5111 | 'mini-language, which\n' |
| 5112 | 'is described in the next section.\n' |
| 5113 | '\n' |
| 5114 | 'A *format_spec* field can also include nested replacement ' |
| 5115 | 'fields\n' |
| 5116 | 'within it. These nested replacement fields may contain a ' |
| 5117 | 'field name,\n' |
| 5118 | 'conversion flag and format specification, but deeper ' |
| 5119 | 'nesting is not\n' |
| 5120 | 'allowed. The replacement fields within the format_spec ' |
| 5121 | 'are\n' |
| 5122 | 'substituted before the *format_spec* string is interpreted. ' |
| 5123 | 'This\n' |
| 5124 | 'allows the formatting of a value to be dynamically ' |
| 5125 | 'specified.\n' |
| 5126 | '\n' |
| 5127 | 'See the Format examples section for some examples.\n' |
| 5128 | '\n' |
| 5129 | '\n' |
| 5130 | 'Format Specification Mini-Language\n' |
| 5131 | '==================================\n' |
| 5132 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5133 | '“Format specifications” are used within replacement fields ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5134 | 'contained\n' |
| 5135 | 'within a format string to define how individual values are ' |
| 5136 | 'presented\n' |
| 5137 | '(see Format String Syntax and Formatted string literals). ' |
| 5138 | 'They can\n' |
| 5139 | 'also be passed directly to the built-in "format()" ' |
| 5140 | 'function. Each\n' |
| 5141 | 'formattable type may define how the format specification is ' |
| 5142 | 'to be\n' |
| 5143 | 'interpreted.\n' |
| 5144 | '\n' |
| 5145 | 'Most built-in types implement the following options for ' |
| 5146 | 'format\n' |
| 5147 | 'specifications, although some of the formatting options are ' |
| 5148 | 'only\n' |
| 5149 | 'supported by the numeric types.\n' |
| 5150 | '\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 5151 | 'A general convention is that an empty format specification ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5152 | 'produces\n' |
| 5153 | 'the same result as if you had called "str()" on the value. ' |
| 5154 | 'A non-empty\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 5155 | 'format specification typically modifies the result.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5156 | '\n' |
| 5157 | 'The general form of a *standard format specifier* is:\n' |
| 5158 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 5159 | ' format_spec ::= ' |
| 5160 | '[[fill]align][sign][#][0][width][grouping_option][.precision][type]\n' |
| 5161 | ' fill ::= <any character>\n' |
| 5162 | ' align ::= "<" | ">" | "=" | "^"\n' |
| 5163 | ' sign ::= "+" | "-" | " "\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5164 | ' width ::= digit+\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 5165 | ' grouping_option ::= "_" | ","\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5166 | ' precision ::= digit+\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 5167 | ' type ::= "b" | "c" | "d" | "e" | "E" | "f" | ' |
| 5168 | '"F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5169 | '\n' |
| 5170 | 'If a valid *align* value is specified, it can be preceded ' |
| 5171 | 'by a *fill*\n' |
| 5172 | 'character that can be any character and defaults to a space ' |
| 5173 | 'if\n' |
| 5174 | 'omitted. It is not possible to use a literal curly brace ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5175 | '(“"{"” or\n' |
| 5176 | '“"}"”) as the *fill* character in a formatted string ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5177 | 'literal or when\n' |
| 5178 | 'using the "str.format()" method. However, it is possible ' |
| 5179 | 'to insert a\n' |
| 5180 | 'curly brace with a nested replacement field. This ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5181 | 'limitation doesn’t\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5182 | 'affect the "format()" function.\n' |
| 5183 | '\n' |
| 5184 | 'The meaning of the various alignment options is as ' |
| 5185 | 'follows:\n' |
| 5186 | '\n' |
| 5187 | ' ' |
| 5188 | '+-----------+------------------------------------------------------------+\n' |
| 5189 | ' | Option | ' |
| 5190 | 'Meaning ' |
| 5191 | '|\n' |
| 5192 | ' ' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 5193 | '|===========|============================================================|\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5194 | ' | "\'<\'" | Forces the field to be left-aligned ' |
| 5195 | 'within the available |\n' |
| 5196 | ' | | space (this is the default for most ' |
| 5197 | 'objects). |\n' |
| 5198 | ' ' |
| 5199 | '+-----------+------------------------------------------------------------+\n' |
| 5200 | ' | "\'>\'" | Forces the field to be right-aligned ' |
| 5201 | 'within the available |\n' |
| 5202 | ' | | space (this is the default for ' |
| 5203 | 'numbers). |\n' |
| 5204 | ' ' |
| 5205 | '+-----------+------------------------------------------------------------+\n' |
| 5206 | ' | "\'=\'" | Forces the padding to be placed after ' |
| 5207 | 'the sign (if any) |\n' |
| 5208 | ' | | but before the digits. This is used for ' |
| 5209 | 'printing fields |\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5210 | ' | | in the form ‘+000000120’. This alignment ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5211 | 'option is only |\n' |
| 5212 | ' | | valid for numeric types. It becomes the ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5213 | 'default when ‘0’ |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5214 | ' | | immediately precedes the field ' |
| 5215 | 'width. |\n' |
| 5216 | ' ' |
| 5217 | '+-----------+------------------------------------------------------------+\n' |
| 5218 | ' | "\'^\'" | Forces the field to be centered within ' |
| 5219 | 'the available |\n' |
| 5220 | ' | | ' |
| 5221 | 'space. ' |
| 5222 | '|\n' |
| 5223 | ' ' |
| 5224 | '+-----------+------------------------------------------------------------+\n' |
| 5225 | '\n' |
| 5226 | 'Note that unless a minimum field width is defined, the ' |
| 5227 | 'field width\n' |
| 5228 | 'will always be the same size as the data to fill it, so ' |
| 5229 | 'that the\n' |
| 5230 | 'alignment option has no meaning in this case.\n' |
| 5231 | '\n' |
| 5232 | 'The *sign* option is only valid for number types, and can ' |
| 5233 | 'be one of\n' |
| 5234 | 'the following:\n' |
| 5235 | '\n' |
| 5236 | ' ' |
| 5237 | '+-----------+------------------------------------------------------------+\n' |
| 5238 | ' | Option | ' |
| 5239 | 'Meaning ' |
| 5240 | '|\n' |
| 5241 | ' ' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 5242 | '|===========|============================================================|\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5243 | ' | "\'+\'" | indicates that a sign should be used for ' |
| 5244 | 'both positive as |\n' |
| 5245 | ' | | well as negative ' |
| 5246 | 'numbers. |\n' |
| 5247 | ' ' |
| 5248 | '+-----------+------------------------------------------------------------+\n' |
| 5249 | ' | "\'-\'" | indicates that a sign should be used ' |
| 5250 | 'only for negative |\n' |
| 5251 | ' | | numbers (this is the default ' |
| 5252 | 'behavior). |\n' |
| 5253 | ' ' |
| 5254 | '+-----------+------------------------------------------------------------+\n' |
| 5255 | ' | space | indicates that a leading space should be ' |
| 5256 | 'used on positive |\n' |
| 5257 | ' | | numbers, and a minus sign on negative ' |
| 5258 | 'numbers. |\n' |
| 5259 | ' ' |
| 5260 | '+-----------+------------------------------------------------------------+\n' |
| 5261 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5262 | 'The "\'#\'" option causes the “alternate form” to be used ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5263 | 'for the\n' |
| 5264 | 'conversion. The alternate form is defined differently for ' |
| 5265 | 'different\n' |
| 5266 | 'types. This option is only valid for integer, float, ' |
| 5267 | 'complex and\n' |
| 5268 | 'Decimal types. For integers, when binary, octal, or ' |
| 5269 | 'hexadecimal output\n' |
| 5270 | 'is used, this option adds the prefix respective "\'0b\'", ' |
| 5271 | '"\'0o\'", or\n' |
| 5272 | '"\'0x\'" to the output value. For floats, complex and ' |
| 5273 | 'Decimal the\n' |
| 5274 | 'alternate form causes the result of the conversion to ' |
| 5275 | 'always contain a\n' |
| 5276 | 'decimal-point character, even if no digits follow it. ' |
| 5277 | 'Normally, a\n' |
| 5278 | 'decimal-point character appears in the result of these ' |
| 5279 | 'conversions\n' |
| 5280 | 'only if a digit follows it. In addition, for "\'g\'" and ' |
| 5281 | '"\'G\'"\n' |
| 5282 | 'conversions, trailing zeros are not removed from the ' |
| 5283 | 'result.\n' |
| 5284 | '\n' |
| 5285 | 'The "\',\'" option signals the use of a comma for a ' |
| 5286 | 'thousands separator.\n' |
| 5287 | 'For a locale aware separator, use the "\'n\'" integer ' |
| 5288 | 'presentation type\n' |
| 5289 | 'instead.\n' |
| 5290 | '\n' |
| 5291 | 'Changed in version 3.1: Added the "\',\'" option (see also ' |
| 5292 | '**PEP 378**).\n' |
| 5293 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 5294 | 'The "\'_\'" option signals the use of an underscore for a ' |
| 5295 | 'thousands\n' |
| 5296 | 'separator for floating point presentation types and for ' |
| 5297 | 'integer\n' |
| 5298 | 'presentation type "\'d\'". For integer presentation types ' |
| 5299 | '"\'b\'", "\'o\'",\n' |
| 5300 | '"\'x\'", and "\'X\'", underscores will be inserted every 4 ' |
| 5301 | 'digits. For\n' |
| 5302 | 'other presentation types, specifying this option is an ' |
| 5303 | 'error.\n' |
| 5304 | '\n' |
| 5305 | 'Changed in version 3.6: Added the "\'_\'" option (see also ' |
| 5306 | '**PEP 515**).\n' |
| 5307 | '\n' |
Łukasz Langa | 6e02691 | 2020-02-25 13:21:47 +0100 | [diff] [blame] | 5308 | '*width* is a decimal integer defining the minimum total ' |
| 5309 | 'field width,\n' |
| 5310 | 'including any prefixes, separators, and other formatting ' |
| 5311 | 'characters.\n' |
| 5312 | 'If not specified, then the field width will be determined ' |
| 5313 | 'by the\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5314 | 'content.\n' |
| 5315 | '\n' |
| 5316 | 'When no explicit alignment is given, preceding the *width* ' |
| 5317 | 'field by a\n' |
| 5318 | 'zero ("\'0\'") character enables sign-aware zero-padding ' |
| 5319 | 'for numeric\n' |
| 5320 | 'types. This is equivalent to a *fill* character of "\'0\'" ' |
| 5321 | 'with an\n' |
| 5322 | '*alignment* type of "\'=\'".\n' |
| 5323 | '\n' |
| 5324 | 'The *precision* is a decimal number indicating how many ' |
| 5325 | 'digits should\n' |
| 5326 | 'be displayed after the decimal point for a floating point ' |
| 5327 | 'value\n' |
| 5328 | 'formatted with "\'f\'" and "\'F\'", or before and after the ' |
| 5329 | 'decimal point\n' |
| 5330 | 'for a floating point value formatted with "\'g\'" or ' |
| 5331 | '"\'G\'". For non-\n' |
| 5332 | 'number types the field indicates the maximum field size - ' |
| 5333 | 'in other\n' |
| 5334 | 'words, how many characters will be used from the field ' |
| 5335 | 'content. The\n' |
| 5336 | '*precision* is not allowed for integer values.\n' |
| 5337 | '\n' |
| 5338 | 'Finally, the *type* determines how the data should be ' |
| 5339 | 'presented.\n' |
| 5340 | '\n' |
| 5341 | 'The available string presentation types are:\n' |
| 5342 | '\n' |
| 5343 | ' ' |
| 5344 | '+-----------+------------------------------------------------------------+\n' |
| 5345 | ' | Type | ' |
| 5346 | 'Meaning ' |
| 5347 | '|\n' |
| 5348 | ' ' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 5349 | '|===========|============================================================|\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5350 | ' | "\'s\'" | String format. This is the default type ' |
| 5351 | 'for strings and |\n' |
| 5352 | ' | | may be ' |
| 5353 | 'omitted. |\n' |
| 5354 | ' ' |
| 5355 | '+-----------+------------------------------------------------------------+\n' |
| 5356 | ' | None | The same as ' |
| 5357 | '"\'s\'". |\n' |
| 5358 | ' ' |
| 5359 | '+-----------+------------------------------------------------------------+\n' |
| 5360 | '\n' |
| 5361 | 'The available integer presentation types are:\n' |
| 5362 | '\n' |
| 5363 | ' ' |
| 5364 | '+-----------+------------------------------------------------------------+\n' |
| 5365 | ' | Type | ' |
| 5366 | 'Meaning ' |
| 5367 | '|\n' |
| 5368 | ' ' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 5369 | '|===========|============================================================|\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5370 | ' | "\'b\'" | Binary format. Outputs the number in ' |
| 5371 | 'base 2. |\n' |
| 5372 | ' ' |
| 5373 | '+-----------+------------------------------------------------------------+\n' |
| 5374 | ' | "\'c\'" | Character. Converts the integer to the ' |
| 5375 | 'corresponding |\n' |
| 5376 | ' | | unicode character before ' |
| 5377 | 'printing. |\n' |
| 5378 | ' ' |
| 5379 | '+-----------+------------------------------------------------------------+\n' |
| 5380 | ' | "\'d\'" | Decimal Integer. Outputs the number in ' |
| 5381 | 'base 10. |\n' |
| 5382 | ' ' |
| 5383 | '+-----------+------------------------------------------------------------+\n' |
| 5384 | ' | "\'o\'" | Octal format. Outputs the number in base ' |
| 5385 | '8. |\n' |
| 5386 | ' ' |
| 5387 | '+-----------+------------------------------------------------------------+\n' |
| 5388 | ' | "\'x\'" | Hex format. Outputs the number in base ' |
| 5389 | '16, using lower- |\n' |
| 5390 | ' | | case letters for the digits above ' |
| 5391 | '9. |\n' |
| 5392 | ' ' |
| 5393 | '+-----------+------------------------------------------------------------+\n' |
| 5394 | ' | "\'X\'" | Hex format. Outputs the number in base ' |
| 5395 | '16, using upper- |\n' |
| 5396 | ' | | case letters for the digits above ' |
| 5397 | '9. |\n' |
| 5398 | ' ' |
| 5399 | '+-----------+------------------------------------------------------------+\n' |
| 5400 | ' | "\'n\'" | Number. This is the same as "\'d\'", ' |
| 5401 | 'except that it uses the |\n' |
| 5402 | ' | | current locale setting to insert the ' |
| 5403 | 'appropriate number |\n' |
| 5404 | ' | | separator ' |
| 5405 | 'characters. |\n' |
| 5406 | ' ' |
| 5407 | '+-----------+------------------------------------------------------------+\n' |
| 5408 | ' | None | The same as ' |
| 5409 | '"\'d\'". |\n' |
| 5410 | ' ' |
| 5411 | '+-----------+------------------------------------------------------------+\n' |
| 5412 | '\n' |
| 5413 | 'In addition to the above presentation types, integers can ' |
| 5414 | 'be formatted\n' |
| 5415 | 'with the floating point presentation types listed below ' |
| 5416 | '(except "\'n\'"\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 5417 | 'and "None"). When doing so, "float()" is used to convert ' |
| 5418 | 'the integer\n' |
| 5419 | 'to a floating point number before formatting.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5420 | '\n' |
| 5421 | 'The available presentation types for floating point and ' |
| 5422 | 'decimal values\n' |
| 5423 | 'are:\n' |
| 5424 | '\n' |
| 5425 | ' ' |
| 5426 | '+-----------+------------------------------------------------------------+\n' |
| 5427 | ' | Type | ' |
| 5428 | 'Meaning ' |
| 5429 | '|\n' |
| 5430 | ' ' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 5431 | '|===========|============================================================|\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5432 | ' | "\'e\'" | Exponent notation. Prints the number in ' |
| 5433 | 'scientific |\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5434 | ' | | notation using the letter ‘e’ to indicate ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5435 | 'the exponent. |\n' |
| 5436 | ' | | The default precision is ' |
| 5437 | '"6". |\n' |
| 5438 | ' ' |
| 5439 | '+-----------+------------------------------------------------------------+\n' |
| 5440 | ' | "\'E\'" | Exponent notation. Same as "\'e\'" ' |
| 5441 | 'except it uses an upper |\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5442 | ' | | case ‘E’ as the separator ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5443 | 'character. |\n' |
| 5444 | ' ' |
| 5445 | '+-----------+------------------------------------------------------------+\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5446 | ' | "\'f\'" | Fixed-point notation. Displays the ' |
| 5447 | 'number as a fixed-point |\n' |
| 5448 | ' | | number. The default precision is ' |
| 5449 | '"6". |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5450 | ' ' |
| 5451 | '+-----------+------------------------------------------------------------+\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5452 | ' | "\'F\'" | Fixed-point notation. Same as "\'f\'", ' |
| 5453 | 'but converts "nan" to |\n' |
| 5454 | ' | | "NAN" and "inf" to ' |
| 5455 | '"INF". |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5456 | ' ' |
| 5457 | '+-----------+------------------------------------------------------------+\n' |
| 5458 | ' | "\'g\'" | General format. For a given precision ' |
| 5459 | '"p >= 1", this |\n' |
| 5460 | ' | | rounds the number to "p" significant ' |
| 5461 | 'digits and then |\n' |
| 5462 | ' | | formats the result in either fixed-point ' |
| 5463 | 'format or in |\n' |
| 5464 | ' | | scientific notation, depending on its ' |
| 5465 | 'magnitude. The |\n' |
| 5466 | ' | | precise rules are as follows: suppose that ' |
| 5467 | 'the result |\n' |
| 5468 | ' | | formatted with presentation type "\'e\'" ' |
| 5469 | 'and precision "p-1" |\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 5470 | ' | | would have exponent "exp". Then, if "m <= ' |
| 5471 | 'exp < p", where |\n' |
| 5472 | ' | | "m" is -4 for floats and -6 for ' |
| 5473 | '"Decimals", the number is |\n' |
| 5474 | ' | | formatted with presentation type "\'f\'" ' |
| 5475 | 'and precision |\n' |
| 5476 | ' | | "p-1-exp". Otherwise, the number is ' |
| 5477 | 'formatted with |\n' |
| 5478 | ' | | presentation type "\'e\'" and precision ' |
| 5479 | '"p-1". In both cases |\n' |
| 5480 | ' | | insignificant trailing zeros are removed ' |
| 5481 | 'from the |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5482 | ' | | significand, and the decimal point is also ' |
| 5483 | 'removed if |\n' |
| 5484 | ' | | there are no remaining digits following ' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 5485 | 'it, unless the |\n' |
| 5486 | ' | | "\'#\'" option is used. Positive and ' |
| 5487 | 'negative infinity, |\n' |
| 5488 | ' | | positive and negative zero, and nans, are ' |
| 5489 | 'formatted as |\n' |
| 5490 | ' | | "inf", "-inf", "0", "-0" and "nan" ' |
| 5491 | 'respectively, |\n' |
| 5492 | ' | | regardless of the precision. A precision ' |
| 5493 | 'of "0" is |\n' |
| 5494 | ' | | treated as equivalent to a precision of ' |
| 5495 | '"1". The default |\n' |
| 5496 | ' | | precision is ' |
| 5497 | '"6". |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5498 | ' ' |
| 5499 | '+-----------+------------------------------------------------------------+\n' |
| 5500 | ' | "\'G\'" | General format. Same as "\'g\'" except ' |
| 5501 | 'switches to "\'E\'" if |\n' |
| 5502 | ' | | the number gets too large. The ' |
| 5503 | 'representations of infinity |\n' |
| 5504 | ' | | and NaN are uppercased, ' |
| 5505 | 'too. |\n' |
| 5506 | ' ' |
| 5507 | '+-----------+------------------------------------------------------------+\n' |
| 5508 | ' | "\'n\'" | Number. This is the same as "\'g\'", ' |
| 5509 | 'except that it uses the |\n' |
| 5510 | ' | | current locale setting to insert the ' |
| 5511 | 'appropriate number |\n' |
| 5512 | ' | | separator ' |
| 5513 | 'characters. |\n' |
| 5514 | ' ' |
| 5515 | '+-----------+------------------------------------------------------------+\n' |
| 5516 | ' | "\'%\'" | Percentage. Multiplies the number by 100 ' |
| 5517 | 'and displays in |\n' |
| 5518 | ' | | fixed ("\'f\'") format, followed by a ' |
| 5519 | 'percent sign. |\n' |
| 5520 | ' ' |
| 5521 | '+-----------+------------------------------------------------------------+\n' |
| 5522 | ' | None | Similar to "\'g\'", except that ' |
| 5523 | 'fixed-point notation, when |\n' |
| 5524 | ' | | used, has at least one digit past the ' |
| 5525 | 'decimal point. The |\n' |
| 5526 | ' | | default precision is as high as needed to ' |
| 5527 | 'represent the |\n' |
| 5528 | ' | | particular value. The overall effect is to ' |
| 5529 | 'match the |\n' |
| 5530 | ' | | output of "str()" as altered by the other ' |
| 5531 | 'format |\n' |
| 5532 | ' | | ' |
| 5533 | 'modifiers. ' |
| 5534 | '|\n' |
| 5535 | ' ' |
| 5536 | '+-----------+------------------------------------------------------------+\n' |
| 5537 | '\n' |
| 5538 | '\n' |
| 5539 | 'Format examples\n' |
| 5540 | '===============\n' |
| 5541 | '\n' |
| 5542 | 'This section contains examples of the "str.format()" syntax ' |
| 5543 | 'and\n' |
| 5544 | 'comparison with the old "%"-formatting.\n' |
| 5545 | '\n' |
| 5546 | 'In most of the cases the syntax is similar to the old ' |
| 5547 | '"%"-formatting,\n' |
| 5548 | 'with the addition of the "{}" and with ":" used instead of ' |
| 5549 | '"%". For\n' |
| 5550 | 'example, "\'%03.2f\'" can be translated to "\'{:03.2f}\'".\n' |
| 5551 | '\n' |
| 5552 | 'The new format syntax also supports new and different ' |
| 5553 | 'options, shown\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5554 | 'in the following examples.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5555 | '\n' |
| 5556 | 'Accessing arguments by position:\n' |
| 5557 | '\n' |
| 5558 | " >>> '{0}, {1}, {2}'.format('a', 'b', 'c')\n" |
| 5559 | " 'a, b, c'\n" |
| 5560 | " >>> '{}, {}, {}'.format('a', 'b', 'c') # 3.1+ only\n" |
| 5561 | " 'a, b, c'\n" |
| 5562 | " >>> '{2}, {1}, {0}'.format('a', 'b', 'c')\n" |
| 5563 | " 'c, b, a'\n" |
| 5564 | " >>> '{2}, {1}, {0}'.format(*'abc') # unpacking " |
| 5565 | 'argument sequence\n' |
| 5566 | " 'c, b, a'\n" |
| 5567 | " >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' " |
| 5568 | 'indices can be repeated\n' |
| 5569 | " 'abracadabra'\n" |
| 5570 | '\n' |
| 5571 | 'Accessing arguments by name:\n' |
| 5572 | '\n' |
| 5573 | " >>> 'Coordinates: {latitude}, " |
| 5574 | "{longitude}'.format(latitude='37.24N', " |
| 5575 | "longitude='-115.81W')\n" |
| 5576 | " 'Coordinates: 37.24N, -115.81W'\n" |
| 5577 | " >>> coord = {'latitude': '37.24N', 'longitude': " |
| 5578 | "'-115.81W'}\n" |
| 5579 | " >>> 'Coordinates: {latitude}, " |
| 5580 | "{longitude}'.format(**coord)\n" |
| 5581 | " 'Coordinates: 37.24N, -115.81W'\n" |
| 5582 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5583 | 'Accessing arguments’ attributes:\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5584 | '\n' |
| 5585 | ' >>> c = 3-5j\n' |
| 5586 | " >>> ('The complex number {0} is formed from the real " |
| 5587 | "part {0.real} '\n" |
| 5588 | " ... 'and the imaginary part {0.imag}.').format(c)\n" |
| 5589 | " 'The complex number (3-5j) is formed from the real part " |
| 5590 | "3.0 and the imaginary part -5.0.'\n" |
| 5591 | ' >>> class Point:\n' |
| 5592 | ' ... def __init__(self, x, y):\n' |
| 5593 | ' ... self.x, self.y = x, y\n' |
| 5594 | ' ... def __str__(self):\n' |
| 5595 | " ... return 'Point({self.x}, " |
| 5596 | "{self.y})'.format(self=self)\n" |
| 5597 | ' ...\n' |
| 5598 | ' >>> str(Point(4, 2))\n' |
| 5599 | " 'Point(4, 2)'\n" |
| 5600 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5601 | 'Accessing arguments’ items:\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5602 | '\n' |
| 5603 | ' >>> coord = (3, 5)\n' |
| 5604 | " >>> 'X: {0[0]}; Y: {0[1]}'.format(coord)\n" |
| 5605 | " 'X: 3; Y: 5'\n" |
| 5606 | '\n' |
| 5607 | 'Replacing "%s" and "%r":\n' |
| 5608 | '\n' |
| 5609 | ' >>> "repr() shows quotes: {!r}; str() doesn\'t: ' |
| 5610 | '{!s}".format(\'test1\', \'test2\')\n' |
| 5611 | ' "repr() shows quotes: \'test1\'; str() doesn\'t: test2"\n' |
| 5612 | '\n' |
| 5613 | 'Aligning the text and specifying a width:\n' |
| 5614 | '\n' |
| 5615 | " >>> '{:<30}'.format('left aligned')\n" |
| 5616 | " 'left aligned '\n" |
| 5617 | " >>> '{:>30}'.format('right aligned')\n" |
| 5618 | " ' right aligned'\n" |
| 5619 | " >>> '{:^30}'.format('centered')\n" |
| 5620 | " ' centered '\n" |
| 5621 | " >>> '{:*^30}'.format('centered') # use '*' as a fill " |
| 5622 | 'char\n' |
| 5623 | " '***********centered***********'\n" |
| 5624 | '\n' |
| 5625 | 'Replacing "%+f", "%-f", and "% f" and specifying a sign:\n' |
| 5626 | '\n' |
| 5627 | " >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it " |
| 5628 | 'always\n' |
| 5629 | " '+3.140000; -3.140000'\n" |
| 5630 | " >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space " |
| 5631 | 'for positive numbers\n' |
| 5632 | " ' 3.140000; -3.140000'\n" |
| 5633 | " >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the " |
| 5634 | "minus -- same as '{:f}; {:f}'\n" |
| 5635 | " '3.140000; -3.140000'\n" |
| 5636 | '\n' |
| 5637 | 'Replacing "%x" and "%o" and converting the value to ' |
| 5638 | 'different bases:\n' |
| 5639 | '\n' |
| 5640 | ' >>> # format also supports binary numbers\n' |
| 5641 | ' >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: ' |
| 5642 | '{0:b}".format(42)\n' |
| 5643 | " 'int: 42; hex: 2a; oct: 52; bin: 101010'\n" |
| 5644 | ' >>> # with 0x, 0o, or 0b as prefix:\n' |
| 5645 | ' >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: ' |
| 5646 | '{0:#b}".format(42)\n' |
| 5647 | " 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'\n" |
| 5648 | '\n' |
| 5649 | 'Using the comma as a thousands separator:\n' |
| 5650 | '\n' |
| 5651 | " >>> '{:,}'.format(1234567890)\n" |
| 5652 | " '1,234,567,890'\n" |
| 5653 | '\n' |
| 5654 | 'Expressing a percentage:\n' |
| 5655 | '\n' |
| 5656 | ' >>> points = 19\n' |
| 5657 | ' >>> total = 22\n' |
| 5658 | " >>> 'Correct answers: {:.2%}'.format(points/total)\n" |
| 5659 | " 'Correct answers: 86.36%'\n" |
| 5660 | '\n' |
| 5661 | 'Using type-specific formatting:\n' |
| 5662 | '\n' |
| 5663 | ' >>> import datetime\n' |
| 5664 | ' >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)\n' |
| 5665 | " >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)\n" |
| 5666 | " '2010-07-04 12:15:58'\n" |
| 5667 | '\n' |
| 5668 | 'Nesting arguments and more complex examples:\n' |
| 5669 | '\n' |
| 5670 | " >>> for align, text in zip('<^>', ['left', 'center', " |
| 5671 | "'right']):\n" |
| 5672 | " ... '{0:{fill}{align}16}'.format(text, fill=align, " |
| 5673 | 'align=align)\n' |
| 5674 | ' ...\n' |
| 5675 | " 'left<<<<<<<<<<<<'\n" |
| 5676 | " '^^^^^center^^^^^'\n" |
| 5677 | " '>>>>>>>>>>>right'\n" |
| 5678 | ' >>>\n' |
| 5679 | ' >>> octets = [192, 168, 0, 1]\n' |
| 5680 | " >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)\n" |
| 5681 | " 'C0A80001'\n" |
| 5682 | ' >>> int(_, 16)\n' |
| 5683 | ' 3232235521\n' |
| 5684 | ' >>>\n' |
| 5685 | ' >>> width = 5\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5686 | ' >>> for num in range(5,12): \n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5687 | " ... for base in 'dXob':\n" |
| 5688 | " ... print('{0:{width}{base}}'.format(num, " |
| 5689 | "base=base, width=width), end=' ')\n" |
| 5690 | ' ... print()\n' |
| 5691 | ' ...\n' |
| 5692 | ' 5 5 5 101\n' |
| 5693 | ' 6 6 6 110\n' |
| 5694 | ' 7 7 7 111\n' |
| 5695 | ' 8 8 10 1000\n' |
| 5696 | ' 9 9 11 1001\n' |
| 5697 | ' 10 A 12 1010\n' |
| 5698 | ' 11 B 13 1011\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 5699 | 'function': 'Function definitions\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5700 | '********************\n' |
| 5701 | '\n' |
| 5702 | 'A function definition defines a user-defined function object ' |
| 5703 | '(see\n' |
| 5704 | 'section The standard type hierarchy):\n' |
| 5705 | '\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 5706 | ' funcdef ::= [decorators] "def" funcname "(" ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5707 | '[parameter_list] ")"\n' |
| 5708 | ' ["->" expression] ":" suite\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 5709 | ' decorators ::= decorator+\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 5710 | ' decorator ::= "@" assignment_expression ' |
| 5711 | 'NEWLINE\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 5712 | ' dotted_name ::= identifier ("." identifier)*\n' |
| 5713 | ' parameter_list ::= defparameter ("," ' |
| 5714 | 'defparameter)* "," "/" ["," [parameter_list_no_posonly]]\n' |
| 5715 | ' | parameter_list_no_posonly\n' |
| 5716 | ' parameter_list_no_posonly ::= defparameter ("," ' |
| 5717 | 'defparameter)* ["," [parameter_list_starargs]]\n' |
| 5718 | ' | parameter_list_starargs\n' |
| 5719 | ' parameter_list_starargs ::= "*" [parameter] ("," ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5720 | 'defparameter)* ["," ["**" parameter [","]]]\n' |
| 5721 | ' | "**" parameter [","]\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 5722 | ' parameter ::= identifier [":" expression]\n' |
| 5723 | ' defparameter ::= parameter ["=" expression]\n' |
| 5724 | ' funcname ::= identifier\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5725 | '\n' |
| 5726 | 'A function definition is an executable statement. Its execution ' |
| 5727 | 'binds\n' |
| 5728 | 'the function name in the current local namespace to a function ' |
| 5729 | 'object\n' |
| 5730 | '(a wrapper around the executable code for the function). This\n' |
| 5731 | 'function object contains a reference to the current global ' |
| 5732 | 'namespace\n' |
| 5733 | 'as the global namespace to be used when the function is called.\n' |
| 5734 | '\n' |
| 5735 | 'The function definition does not execute the function body; this ' |
| 5736 | 'gets\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5737 | 'executed only when the function is called. [2]\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5738 | '\n' |
| 5739 | 'A function definition may be wrapped by one or more *decorator*\n' |
| 5740 | 'expressions. Decorator expressions are evaluated when the ' |
| 5741 | 'function is\n' |
| 5742 | 'defined, in the scope that contains the function definition. ' |
| 5743 | 'The\n' |
| 5744 | 'result must be a callable, which is invoked with the function ' |
| 5745 | 'object\n' |
| 5746 | 'as the only argument. The returned value is bound to the ' |
| 5747 | 'function name\n' |
| 5748 | 'instead of the function object. Multiple decorators are applied ' |
| 5749 | 'in\n' |
| 5750 | 'nested fashion. For example, the following code\n' |
| 5751 | '\n' |
| 5752 | ' @f1(arg)\n' |
| 5753 | ' @f2\n' |
| 5754 | ' def func(): pass\n' |
| 5755 | '\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 5756 | 'is roughly equivalent to\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5757 | '\n' |
| 5758 | ' def func(): pass\n' |
| 5759 | ' func = f1(arg)(f2(func))\n' |
| 5760 | '\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 5761 | 'except that the original function is not temporarily bound to ' |
| 5762 | 'the name\n' |
| 5763 | '"func".\n' |
| 5764 | '\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 5765 | 'Changed in version 3.9: Functions may be decorated with any ' |
| 5766 | 'valid\n' |
| 5767 | '"assignment_expression". Previously, the grammar was much more\n' |
| 5768 | 'restrictive; see **PEP 614** for details.\n' |
| 5769 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5770 | 'When one or more *parameters* have the form *parameter* "="\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5771 | '*expression*, the function is said to have “default parameter ' |
| 5772 | 'values.”\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5773 | 'For a parameter with a default value, the corresponding ' |
| 5774 | '*argument* may\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5775 | 'be omitted from a call, in which case the parameter’s default ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5776 | 'value is\n' |
| 5777 | 'substituted. If a parameter has a default value, all following\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5778 | 'parameters up until the “"*"” must also have a default value — ' |
| 5779 | 'this is\n' |
| 5780 | 'a syntactic restriction that is not expressed by the grammar.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5781 | '\n' |
| 5782 | '**Default parameter values are evaluated from left to right when ' |
| 5783 | 'the\n' |
| 5784 | 'function definition is executed.** This means that the ' |
| 5785 | 'expression is\n' |
| 5786 | 'evaluated once, when the function is defined, and that the same ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5787 | '“pre-\n' |
| 5788 | 'computed” value is used for each call. This is especially ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5789 | 'important\n' |
| 5790 | 'to understand when a default parameter is a mutable object, such ' |
| 5791 | 'as a\n' |
| 5792 | 'list or a dictionary: if the function modifies the object (e.g. ' |
| 5793 | 'by\n' |
| 5794 | 'appending an item to a list), the default value is in effect ' |
| 5795 | 'modified.\n' |
| 5796 | 'This is generally not what was intended. A way around this is ' |
| 5797 | 'to use\n' |
| 5798 | '"None" as the default, and explicitly test for it in the body of ' |
| 5799 | 'the\n' |
| 5800 | 'function, e.g.:\n' |
| 5801 | '\n' |
| 5802 | ' def whats_on_the_telly(penguin=None):\n' |
| 5803 | ' if penguin is None:\n' |
| 5804 | ' penguin = []\n' |
| 5805 | ' penguin.append("property of the zoo")\n' |
| 5806 | ' return penguin\n' |
| 5807 | '\n' |
| 5808 | 'Function call semantics are described in more detail in section ' |
| 5809 | 'Calls.\n' |
| 5810 | 'A function call always assigns values to all parameters ' |
| 5811 | 'mentioned in\n' |
| 5812 | 'the parameter list, either from position arguments, from ' |
| 5813 | 'keyword\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5814 | 'arguments, or from default values. If the form “"*identifier"” ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5815 | 'is\n' |
| 5816 | 'present, it is initialized to a tuple receiving any excess ' |
| 5817 | 'positional\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 5818 | 'parameters, defaulting to the empty tuple. If the form\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5819 | '“"**identifier"” is present, it is initialized to a new ordered\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 5820 | 'mapping receiving any excess keyword arguments, defaulting to a ' |
| 5821 | 'new\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5822 | 'empty mapping of the same type. Parameters after “"*"” or\n' |
| 5823 | '“"*identifier"” are keyword-only parameters and may only be ' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 5824 | 'passed\n' |
| 5825 | 'used keyword arguments.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5826 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5827 | 'Parameters may have an *annotation* of the form “": ' |
| 5828 | 'expression"”\n' |
| 5829 | 'following the parameter name. Any parameter may have an ' |
| 5830 | 'annotation,\n' |
| 5831 | 'even those of the form "*identifier" or "**identifier". ' |
| 5832 | 'Functions may\n' |
| 5833 | 'have “return” annotation of the form “"-> expression"” after ' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 5834 | 'the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5835 | 'parameter list. These annotations can be any valid Python ' |
| 5836 | 'expression.\n' |
| 5837 | 'The presence of annotations does not change the semantics of a\n' |
| 5838 | 'function. The annotation values are available as values of a\n' |
| 5839 | 'dictionary keyed by the parameters’ names in the ' |
| 5840 | '"__annotations__"\n' |
| 5841 | 'attribute of the function object. If the "annotations" import ' |
| 5842 | 'from\n' |
| 5843 | '"__future__" is used, annotations are preserved as strings at ' |
| 5844 | 'runtime\n' |
| 5845 | 'which enables postponed evaluation. Otherwise, they are ' |
| 5846 | 'evaluated\n' |
| 5847 | 'when the function definition is executed. In this case ' |
| 5848 | 'annotations\n' |
| 5849 | 'may be evaluated in a different order than they appear in the ' |
| 5850 | 'source\n' |
| 5851 | 'code.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5852 | '\n' |
| 5853 | 'It is also possible to create anonymous functions (functions not ' |
| 5854 | 'bound\n' |
| 5855 | 'to a name), for immediate use in expressions. This uses lambda\n' |
| 5856 | 'expressions, described in section Lambdas. Note that the ' |
| 5857 | 'lambda\n' |
| 5858 | 'expression is merely a shorthand for a simplified function ' |
| 5859 | 'definition;\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5860 | 'a function defined in a “"def"” statement can be passed around ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5861 | 'or\n' |
| 5862 | 'assigned to another name just like a function defined by a ' |
| 5863 | 'lambda\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5864 | 'expression. The “"def"” form is actually more powerful since ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5865 | 'it\n' |
| 5866 | 'allows the execution of multiple statements and annotations.\n' |
| 5867 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5868 | '**Programmer’s note:** Functions are first-class objects. A ' |
| 5869 | '“"def"”\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5870 | 'statement executed inside a function definition defines a local\n' |
| 5871 | 'function that can be returned or passed around. Free variables ' |
| 5872 | 'used\n' |
| 5873 | 'in the nested function can access the local variables of the ' |
| 5874 | 'function\n' |
| 5875 | 'containing the def. See section Naming and binding for ' |
| 5876 | 'details.\n' |
| 5877 | '\n' |
| 5878 | 'See also:\n' |
| 5879 | '\n' |
| 5880 | ' **PEP 3107** - Function Annotations\n' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 5881 | ' The original specification for function annotations.\n' |
| 5882 | '\n' |
| 5883 | ' **PEP 484** - Type Hints\n' |
| 5884 | ' Definition of a standard meaning for annotations: type ' |
| 5885 | 'hints.\n' |
| 5886 | '\n' |
| 5887 | ' **PEP 526** - Syntax for Variable Annotations\n' |
| 5888 | ' Ability to type hint variable declarations, including ' |
| 5889 | 'class\n' |
| 5890 | ' variables and instance variables\n' |
| 5891 | '\n' |
| 5892 | ' **PEP 563** - Postponed Evaluation of Annotations\n' |
| 5893 | ' Support for forward references within annotations by ' |
| 5894 | 'preserving\n' |
| 5895 | ' annotations in a string form at runtime instead of eager\n' |
| 5896 | ' evaluation.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 5897 | 'global': 'The "global" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5898 | '**********************\n' |
| 5899 | '\n' |
| 5900 | ' global_stmt ::= "global" identifier ("," identifier)*\n' |
| 5901 | '\n' |
| 5902 | 'The "global" statement is a declaration which holds for the ' |
| 5903 | 'entire\n' |
| 5904 | 'current code block. It means that the listed identifiers are to ' |
| 5905 | 'be\n' |
| 5906 | 'interpreted as globals. It would be impossible to assign to a ' |
| 5907 | 'global\n' |
| 5908 | 'variable without "global", although free variables may refer to\n' |
| 5909 | 'globals without being declared global.\n' |
| 5910 | '\n' |
| 5911 | 'Names listed in a "global" statement must not be used in the same ' |
| 5912 | 'code\n' |
| 5913 | 'block textually preceding that "global" statement.\n' |
| 5914 | '\n' |
| 5915 | 'Names listed in a "global" statement must not be defined as ' |
| 5916 | 'formal\n' |
| 5917 | 'parameters or in a "for" loop control target, "class" definition,\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 5918 | 'function definition, "import" statement, or variable annotation.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5919 | '\n' |
| 5920 | '**CPython implementation detail:** The current implementation does ' |
| 5921 | 'not\n' |
Ned Deily | c730223 | 2017-10-16 23:41:55 -0400 | [diff] [blame] | 5922 | 'enforce some of these restrictions, but programs should not abuse ' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 5923 | 'this\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5924 | 'freedom, as future implementations may enforce them or silently ' |
| 5925 | 'change\n' |
| 5926 | 'the meaning of the program.\n' |
| 5927 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5928 | '**Programmer’s note:** "global" is a directive to the parser. It\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5929 | 'applies only to code parsed at the same time as the "global"\n' |
| 5930 | 'statement. In particular, a "global" statement contained in a ' |
| 5931 | 'string\n' |
| 5932 | 'or code object supplied to the built-in "exec()" function does ' |
| 5933 | 'not\n' |
| 5934 | 'affect the code block *containing* the function call, and code\n' |
| 5935 | 'contained in such a string is unaffected by "global" statements in ' |
| 5936 | 'the\n' |
| 5937 | 'code containing the function call. The same applies to the ' |
| 5938 | '"eval()"\n' |
| 5939 | 'and "compile()" functions.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 5940 | 'id-classes': 'Reserved classes of identifiers\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5941 | '*******************************\n' |
| 5942 | '\n' |
| 5943 | 'Certain classes of identifiers (besides keywords) have ' |
| 5944 | 'special\n' |
| 5945 | 'meanings. These classes are identified by the patterns of ' |
| 5946 | 'leading and\n' |
| 5947 | 'trailing underscore characters:\n' |
| 5948 | '\n' |
| 5949 | '"_*"\n' |
| 5950 | ' Not imported by "from module import *". The special ' |
| 5951 | 'identifier "_"\n' |
| 5952 | ' is used in the interactive interpreter to store the result ' |
| 5953 | 'of the\n' |
| 5954 | ' last evaluation; it is stored in the "builtins" module. ' |
| 5955 | 'When not\n' |
| 5956 | ' in interactive mode, "_" has no special meaning and is not ' |
| 5957 | 'defined.\n' |
| 5958 | ' See section The import statement.\n' |
| 5959 | '\n' |
| 5960 | ' Note: The name "_" is often used in conjunction with\n' |
| 5961 | ' internationalization; refer to the documentation for the\n' |
| 5962 | ' "gettext" module for more information on this ' |
| 5963 | 'convention.\n' |
| 5964 | '\n' |
| 5965 | '"__*__"\n' |
Łukasz Langa | bc1c8af | 2020-04-27 22:44:04 +0200 | [diff] [blame] | 5966 | ' System-defined names, informally known as “dunder” names. ' |
| 5967 | 'These\n' |
| 5968 | ' names are defined by the interpreter and its ' |
| 5969 | 'implementation\n' |
| 5970 | ' (including the standard library). Current system names are\n' |
| 5971 | ' discussed in the Special method names section and ' |
| 5972 | 'elsewhere. More\n' |
| 5973 | ' will likely be defined in future versions of Python. *Any* ' |
| 5974 | 'use of\n' |
| 5975 | ' "__*__" names, in any context, that does not follow ' |
| 5976 | 'explicitly\n' |
| 5977 | ' documented use, is subject to breakage without warning.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5978 | '\n' |
| 5979 | '"__*"\n' |
| 5980 | ' Class-private names. Names in this category, when used ' |
| 5981 | 'within the\n' |
| 5982 | ' context of a class definition, are re-written to use a ' |
| 5983 | 'mangled form\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 5984 | ' to help avoid name clashes between “private” attributes of ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5985 | 'base and\n' |
| 5986 | ' derived classes. See section Identifiers (Names).\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 5987 | 'identifiers': 'Identifiers and keywords\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 5988 | '************************\n' |
| 5989 | '\n' |
| 5990 | 'Identifiers (also referred to as *names*) are described by ' |
| 5991 | 'the\n' |
| 5992 | 'following lexical definitions.\n' |
| 5993 | '\n' |
| 5994 | 'The syntax of identifiers in Python is based on the Unicode ' |
| 5995 | 'standard\n' |
| 5996 | 'annex UAX-31, with elaboration and changes as defined below; ' |
| 5997 | 'see also\n' |
| 5998 | '**PEP 3131** for further details.\n' |
| 5999 | '\n' |
| 6000 | 'Within the ASCII range (U+0001..U+007F), the valid characters ' |
| 6001 | 'for\n' |
| 6002 | 'identifiers are the same as in Python 2.x: the uppercase and ' |
| 6003 | 'lowercase\n' |
| 6004 | 'letters "A" through "Z", the underscore "_" and, except for ' |
| 6005 | 'the first\n' |
| 6006 | 'character, the digits "0" through "9".\n' |
| 6007 | '\n' |
| 6008 | 'Python 3.0 introduces additional characters from outside the ' |
| 6009 | 'ASCII\n' |
| 6010 | 'range (see **PEP 3131**). For these characters, the ' |
| 6011 | 'classification\n' |
| 6012 | 'uses the version of the Unicode Character Database as ' |
| 6013 | 'included in the\n' |
| 6014 | '"unicodedata" module.\n' |
| 6015 | '\n' |
| 6016 | 'Identifiers are unlimited in length. Case is significant.\n' |
| 6017 | '\n' |
| 6018 | ' identifier ::= xid_start xid_continue*\n' |
| 6019 | ' id_start ::= <all characters in general categories Lu, ' |
| 6020 | 'Ll, Lt, Lm, Lo, Nl, the underscore, and characters with the ' |
| 6021 | 'Other_ID_Start property>\n' |
| 6022 | ' id_continue ::= <all characters in id_start, plus ' |
| 6023 | 'characters in the categories Mn, Mc, Nd, Pc and others with ' |
| 6024 | 'the Other_ID_Continue property>\n' |
| 6025 | ' xid_start ::= <all characters in id_start whose NFKC ' |
| 6026 | 'normalization is in "id_start xid_continue*">\n' |
| 6027 | ' xid_continue ::= <all characters in id_continue whose NFKC ' |
| 6028 | 'normalization is in "id_continue*">\n' |
| 6029 | '\n' |
| 6030 | 'The Unicode category codes mentioned above stand for:\n' |
| 6031 | '\n' |
| 6032 | '* *Lu* - uppercase letters\n' |
| 6033 | '\n' |
| 6034 | '* *Ll* - lowercase letters\n' |
| 6035 | '\n' |
| 6036 | '* *Lt* - titlecase letters\n' |
| 6037 | '\n' |
| 6038 | '* *Lm* - modifier letters\n' |
| 6039 | '\n' |
| 6040 | '* *Lo* - other letters\n' |
| 6041 | '\n' |
| 6042 | '* *Nl* - letter numbers\n' |
| 6043 | '\n' |
| 6044 | '* *Mn* - nonspacing marks\n' |
| 6045 | '\n' |
| 6046 | '* *Mc* - spacing combining marks\n' |
| 6047 | '\n' |
| 6048 | '* *Nd* - decimal numbers\n' |
| 6049 | '\n' |
| 6050 | '* *Pc* - connector punctuations\n' |
| 6051 | '\n' |
| 6052 | '* *Other_ID_Start* - explicit list of characters in ' |
| 6053 | 'PropList.txt to\n' |
| 6054 | ' support backwards compatibility\n' |
| 6055 | '\n' |
| 6056 | '* *Other_ID_Continue* - likewise\n' |
| 6057 | '\n' |
| 6058 | 'All identifiers are converted into the normal form NFKC while ' |
| 6059 | 'parsing;\n' |
| 6060 | 'comparison of identifiers is based on NFKC.\n' |
| 6061 | '\n' |
| 6062 | 'A non-normative HTML file listing all valid identifier ' |
| 6063 | 'characters for\n' |
Łukasz Langa | 0b1a1c8 | 2020-06-08 20:41:34 +0200 | [diff] [blame] | 6064 | 'Unicode 4.1 can be found at\n' |
| 6065 | 'https://www.unicode.org/Public/13.0.0/ucd/DerivedCoreProperties.txt\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6066 | '\n' |
| 6067 | '\n' |
| 6068 | 'Keywords\n' |
| 6069 | '========\n' |
| 6070 | '\n' |
| 6071 | 'The following identifiers are used as reserved words, or ' |
| 6072 | '*keywords* of\n' |
| 6073 | 'the language, and cannot be used as ordinary identifiers. ' |
| 6074 | 'They must\n' |
| 6075 | 'be spelled exactly as written here:\n' |
| 6076 | '\n' |
Ned Deily | 3f9a728 | 2017-12-05 03:17:33 -0500 | [diff] [blame] | 6077 | ' False await else import pass\n' |
| 6078 | ' None break except in raise\n' |
| 6079 | ' True class finally is return\n' |
| 6080 | ' and continue for lambda try\n' |
| 6081 | ' as def from nonlocal while\n' |
| 6082 | ' assert del global not with\n' |
| 6083 | ' async elif if or yield\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6084 | '\n' |
| 6085 | '\n' |
| 6086 | 'Reserved classes of identifiers\n' |
| 6087 | '===============================\n' |
| 6088 | '\n' |
| 6089 | 'Certain classes of identifiers (besides keywords) have ' |
| 6090 | 'special\n' |
| 6091 | 'meanings. These classes are identified by the patterns of ' |
| 6092 | 'leading and\n' |
| 6093 | 'trailing underscore characters:\n' |
| 6094 | '\n' |
| 6095 | '"_*"\n' |
| 6096 | ' Not imported by "from module import *". The special ' |
| 6097 | 'identifier "_"\n' |
| 6098 | ' is used in the interactive interpreter to store the result ' |
| 6099 | 'of the\n' |
| 6100 | ' last evaluation; it is stored in the "builtins" module. ' |
| 6101 | 'When not\n' |
| 6102 | ' in interactive mode, "_" has no special meaning and is not ' |
| 6103 | 'defined.\n' |
| 6104 | ' See section The import statement.\n' |
| 6105 | '\n' |
| 6106 | ' Note: The name "_" is often used in conjunction with\n' |
| 6107 | ' internationalization; refer to the documentation for ' |
| 6108 | 'the\n' |
| 6109 | ' "gettext" module for more information on this ' |
| 6110 | 'convention.\n' |
| 6111 | '\n' |
| 6112 | '"__*__"\n' |
Łukasz Langa | bc1c8af | 2020-04-27 22:44:04 +0200 | [diff] [blame] | 6113 | ' System-defined names, informally known as “dunder” names. ' |
| 6114 | 'These\n' |
| 6115 | ' names are defined by the interpreter and its ' |
| 6116 | 'implementation\n' |
| 6117 | ' (including the standard library). Current system names ' |
| 6118 | 'are\n' |
| 6119 | ' discussed in the Special method names section and ' |
| 6120 | 'elsewhere. More\n' |
| 6121 | ' will likely be defined in future versions of Python. ' |
| 6122 | '*Any* use of\n' |
| 6123 | ' "__*__" names, in any context, that does not follow ' |
| 6124 | 'explicitly\n' |
| 6125 | ' documented use, is subject to breakage without warning.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6126 | '\n' |
| 6127 | '"__*"\n' |
| 6128 | ' Class-private names. Names in this category, when used ' |
| 6129 | 'within the\n' |
| 6130 | ' context of a class definition, are re-written to use a ' |
| 6131 | 'mangled form\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6132 | ' to help avoid name clashes between “private” attributes of ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6133 | 'base and\n' |
| 6134 | ' derived classes. See section Identifiers (Names).\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6135 | 'if': 'The "if" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6136 | '******************\n' |
| 6137 | '\n' |
| 6138 | 'The "if" statement is used for conditional execution:\n' |
| 6139 | '\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 6140 | ' if_stmt ::= "if" assignment_expression ":" suite\n' |
| 6141 | ' ("elif" assignment_expression ":" suite)*\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6142 | ' ["else" ":" suite]\n' |
| 6143 | '\n' |
| 6144 | 'It selects exactly one of the suites by evaluating the expressions ' |
| 6145 | 'one\n' |
| 6146 | 'by one until one is found to be true (see section Boolean operations\n' |
| 6147 | 'for the definition of true and false); then that suite is executed\n' |
| 6148 | '(and no other part of the "if" statement is executed or evaluated).\n' |
| 6149 | 'If all expressions are false, the suite of the "else" clause, if\n' |
| 6150 | 'present, is executed.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6151 | 'imaginary': 'Imaginary literals\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6152 | '******************\n' |
| 6153 | '\n' |
| 6154 | 'Imaginary literals are described by the following lexical ' |
| 6155 | 'definitions:\n' |
| 6156 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 6157 | ' imagnumber ::= (floatnumber | digitpart) ("j" | "J")\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6158 | '\n' |
| 6159 | 'An imaginary literal yields a complex number with a real part ' |
| 6160 | 'of 0.0.\n' |
| 6161 | 'Complex numbers are represented as a pair of floating point ' |
| 6162 | 'numbers\n' |
| 6163 | 'and have the same restrictions on their range. To create a ' |
| 6164 | 'complex\n' |
| 6165 | 'number with a nonzero real part, add a floating point number to ' |
| 6166 | 'it,\n' |
| 6167 | 'e.g., "(3+4j)". Some examples of imaginary literals:\n' |
| 6168 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 6169 | ' 3.14j 10.j 10j .001j 1e100j 3.14e-10j ' |
| 6170 | '3.14_15_93j\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6171 | 'import': 'The "import" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6172 | '**********************\n' |
| 6173 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6174 | ' import_stmt ::= "import" module ["as" identifier] ("," ' |
| 6175 | 'module ["as" identifier])*\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6176 | ' | "from" relative_module "import" identifier ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6177 | '["as" identifier]\n' |
| 6178 | ' ("," identifier ["as" identifier])*\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6179 | ' | "from" relative_module "import" "(" ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6180 | 'identifier ["as" identifier]\n' |
| 6181 | ' ("," identifier ["as" identifier])* [","] ")"\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6182 | ' | "from" module "import" "*"\n' |
| 6183 | ' module ::= (identifier ".")* identifier\n' |
| 6184 | ' relative_module ::= "."* module | "."+\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6185 | '\n' |
| 6186 | 'The basic import statement (no "from" clause) is executed in two\n' |
| 6187 | 'steps:\n' |
| 6188 | '\n' |
| 6189 | '1. find a module, loading and initializing it if necessary\n' |
| 6190 | '\n' |
| 6191 | '2. define a name or names in the local namespace for the scope\n' |
| 6192 | ' where the "import" statement occurs.\n' |
| 6193 | '\n' |
| 6194 | 'When the statement contains multiple clauses (separated by commas) ' |
| 6195 | 'the\n' |
| 6196 | 'two steps are carried out separately for each clause, just as ' |
| 6197 | 'though\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 6198 | 'the clauses had been separated out into individual import ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6199 | 'statements.\n' |
| 6200 | '\n' |
| 6201 | 'The details of the first step, finding and loading modules are\n' |
| 6202 | 'described in greater detail in the section on the import system, ' |
| 6203 | 'which\n' |
| 6204 | 'also describes the various types of packages and modules that can ' |
| 6205 | 'be\n' |
| 6206 | 'imported, as well as all the hooks that can be used to customize ' |
| 6207 | 'the\n' |
| 6208 | 'import system. Note that failures in this step may indicate ' |
| 6209 | 'either\n' |
| 6210 | 'that the module could not be located, *or* that an error occurred\n' |
| 6211 | 'while initializing the module, which includes execution of the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6212 | 'module’s code.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6213 | '\n' |
| 6214 | 'If the requested module is retrieved successfully, it will be ' |
| 6215 | 'made\n' |
| 6216 | 'available in the local namespace in one of three ways:\n' |
| 6217 | '\n' |
| 6218 | '* If the module name is followed by "as", then the name following\n' |
| 6219 | ' "as" is bound directly to the imported module.\n' |
| 6220 | '\n' |
| 6221 | '* If no other name is specified, and the module being imported is ' |
| 6222 | 'a\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6223 | ' top level module, the module’s name is bound in the local ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6224 | 'namespace\n' |
| 6225 | ' as a reference to the imported module\n' |
| 6226 | '\n' |
| 6227 | '* If the module being imported is *not* a top level module, then ' |
| 6228 | 'the\n' |
| 6229 | ' name of the top level package that contains the module is bound ' |
| 6230 | 'in\n' |
| 6231 | ' the local namespace as a reference to the top level package. ' |
| 6232 | 'The\n' |
| 6233 | ' imported module must be accessed using its full qualified name\n' |
| 6234 | ' rather than directly\n' |
| 6235 | '\n' |
| 6236 | 'The "from" form uses a slightly more complex process:\n' |
| 6237 | '\n' |
| 6238 | '1. find the module specified in the "from" clause, loading and\n' |
| 6239 | ' initializing it if necessary;\n' |
| 6240 | '\n' |
| 6241 | '2. for each of the identifiers specified in the "import" clauses:\n' |
| 6242 | '\n' |
| 6243 | ' 1. check if the imported module has an attribute by that name\n' |
| 6244 | '\n' |
| 6245 | ' 2. if not, attempt to import a submodule with that name and ' |
| 6246 | 'then\n' |
| 6247 | ' check the imported module again for that attribute\n' |
| 6248 | '\n' |
| 6249 | ' 3. if the attribute is not found, "ImportError" is raised.\n' |
| 6250 | '\n' |
| 6251 | ' 4. otherwise, a reference to that value is stored in the local\n' |
| 6252 | ' namespace, using the name in the "as" clause if it is ' |
| 6253 | 'present,\n' |
| 6254 | ' otherwise using the attribute name\n' |
| 6255 | '\n' |
| 6256 | 'Examples:\n' |
| 6257 | '\n' |
| 6258 | ' import foo # foo imported and bound locally\n' |
| 6259 | ' import foo.bar.baz # foo.bar.baz imported, foo bound ' |
| 6260 | 'locally\n' |
| 6261 | ' import foo.bar.baz as fbb # foo.bar.baz imported and bound as ' |
| 6262 | 'fbb\n' |
| 6263 | ' from foo.bar import baz # foo.bar.baz imported and bound as ' |
| 6264 | 'baz\n' |
| 6265 | ' from foo import attr # foo imported and foo.attr bound as ' |
| 6266 | 'attr\n' |
| 6267 | '\n' |
| 6268 | 'If the list of identifiers is replaced by a star ("\'*\'"), all ' |
| 6269 | 'public\n' |
| 6270 | 'names defined in the module are bound in the local namespace for ' |
| 6271 | 'the\n' |
| 6272 | 'scope where the "import" statement occurs.\n' |
| 6273 | '\n' |
| 6274 | 'The *public names* defined by a module are determined by checking ' |
| 6275 | 'the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6276 | 'module’s namespace for a variable named "__all__"; if defined, it ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6277 | 'must\n' |
| 6278 | 'be a sequence of strings which are names defined or imported by ' |
| 6279 | 'that\n' |
| 6280 | 'module. The names given in "__all__" are all considered public ' |
| 6281 | 'and\n' |
| 6282 | 'are required to exist. If "__all__" is not defined, the set of ' |
| 6283 | 'public\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6284 | 'names includes all names found in the module’s namespace which do ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6285 | 'not\n' |
| 6286 | 'begin with an underscore character ("\'_\'"). "__all__" should ' |
| 6287 | 'contain\n' |
| 6288 | 'the entire public API. It is intended to avoid accidentally ' |
| 6289 | 'exporting\n' |
| 6290 | 'items that are not part of the API (such as library modules which ' |
| 6291 | 'were\n' |
| 6292 | 'imported and used within the module).\n' |
| 6293 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6294 | 'The wild card form of import — "from module import *" — is only\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6295 | 'allowed at the module level. Attempting to use it in class or\n' |
| 6296 | 'function definitions will raise a "SyntaxError".\n' |
| 6297 | '\n' |
| 6298 | 'When specifying what module to import you do not have to specify ' |
| 6299 | 'the\n' |
| 6300 | 'absolute name of the module. When a module or package is ' |
| 6301 | 'contained\n' |
| 6302 | 'within another package it is possible to make a relative import ' |
| 6303 | 'within\n' |
| 6304 | 'the same top package without having to mention the package name. ' |
| 6305 | 'By\n' |
| 6306 | 'using leading dots in the specified module or package after "from" ' |
| 6307 | 'you\n' |
| 6308 | 'can specify how high to traverse up the current package hierarchy\n' |
| 6309 | 'without specifying exact names. One leading dot means the current\n' |
| 6310 | 'package where the module making the import exists. Two dots means ' |
| 6311 | 'up\n' |
| 6312 | 'one package level. Three dots is up two levels, etc. So if you ' |
| 6313 | 'execute\n' |
| 6314 | '"from . import mod" from a module in the "pkg" package then you ' |
| 6315 | 'will\n' |
| 6316 | 'end up importing "pkg.mod". If you execute "from ..subpkg2 import ' |
| 6317 | 'mod"\n' |
| 6318 | 'from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 6319 | 'specification for relative imports is contained in the Package\n' |
| 6320 | 'Relative Imports section.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6321 | '\n' |
| 6322 | '"importlib.import_module()" is provided to support applications ' |
| 6323 | 'that\n' |
| 6324 | 'determine dynamically the modules to be loaded.\n' |
| 6325 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 6326 | 'Raises an auditing event "import" with arguments "module", ' |
| 6327 | '"filename",\n' |
| 6328 | '"sys.path", "sys.meta_path", "sys.path_hooks".\n' |
| 6329 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6330 | '\n' |
| 6331 | 'Future statements\n' |
| 6332 | '=================\n' |
| 6333 | '\n' |
| 6334 | 'A *future statement* is a directive to the compiler that a ' |
| 6335 | 'particular\n' |
| 6336 | 'module should be compiled using syntax or semantics that will be\n' |
| 6337 | 'available in a specified future release of Python where the ' |
| 6338 | 'feature\n' |
| 6339 | 'becomes standard.\n' |
| 6340 | '\n' |
| 6341 | 'The future statement is intended to ease migration to future ' |
| 6342 | 'versions\n' |
| 6343 | 'of Python that introduce incompatible changes to the language. ' |
| 6344 | 'It\n' |
| 6345 | 'allows use of the new features on a per-module basis before the\n' |
| 6346 | 'release in which the feature becomes standard.\n' |
| 6347 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6348 | ' future_stmt ::= "from" "__future__" "import" feature ["as" ' |
| 6349 | 'identifier]\n' |
| 6350 | ' ("," feature ["as" identifier])*\n' |
| 6351 | ' | "from" "__future__" "import" "(" feature ' |
| 6352 | '["as" identifier]\n' |
| 6353 | ' ("," feature ["as" identifier])* [","] ")"\n' |
| 6354 | ' feature ::= identifier\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6355 | '\n' |
| 6356 | 'A future statement must appear near the top of the module. The ' |
| 6357 | 'only\n' |
| 6358 | 'lines that can appear before a future statement are:\n' |
| 6359 | '\n' |
| 6360 | '* the module docstring (if any),\n' |
| 6361 | '\n' |
| 6362 | '* comments,\n' |
| 6363 | '\n' |
| 6364 | '* blank lines, and\n' |
| 6365 | '\n' |
| 6366 | '* other future statements.\n' |
| 6367 | '\n' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 6368 | 'The only feature in Python 3.7 that requires using the future\n' |
| 6369 | 'statement is "annotations".\n' |
| 6370 | '\n' |
| 6371 | 'All historical features enabled by the future statement are still\n' |
| 6372 | 'recognized by Python 3. The list includes "absolute_import",\n' |
| 6373 | '"division", "generators", "generator_stop", "unicode_literals",\n' |
| 6374 | '"print_function", "nested_scopes" and "with_statement". They are ' |
| 6375 | 'all\n' |
| 6376 | 'redundant because they are always enabled, and only kept for ' |
| 6377 | 'backwards\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6378 | 'compatibility.\n' |
| 6379 | '\n' |
| 6380 | 'A future statement is recognized and treated specially at compile\n' |
| 6381 | 'time: Changes to the semantics of core constructs are often\n' |
| 6382 | 'implemented by generating different code. It may even be the ' |
| 6383 | 'case\n' |
| 6384 | 'that a new feature introduces new incompatible syntax (such as a ' |
| 6385 | 'new\n' |
| 6386 | 'reserved word), in which case the compiler may need to parse the\n' |
| 6387 | 'module differently. Such decisions cannot be pushed off until\n' |
| 6388 | 'runtime.\n' |
| 6389 | '\n' |
| 6390 | 'For any given release, the compiler knows which feature names ' |
| 6391 | 'have\n' |
| 6392 | 'been defined, and raises a compile-time error if a future ' |
| 6393 | 'statement\n' |
| 6394 | 'contains a feature not known to it.\n' |
| 6395 | '\n' |
| 6396 | 'The direct runtime semantics are the same as for any import ' |
| 6397 | 'statement:\n' |
| 6398 | 'there is a standard module "__future__", described later, and it ' |
| 6399 | 'will\n' |
| 6400 | 'be imported in the usual way at the time the future statement is\n' |
| 6401 | 'executed.\n' |
| 6402 | '\n' |
| 6403 | 'The interesting runtime semantics depend on the specific feature\n' |
| 6404 | 'enabled by the future statement.\n' |
| 6405 | '\n' |
| 6406 | 'Note that there is nothing special about the statement:\n' |
| 6407 | '\n' |
| 6408 | ' import __future__ [as name]\n' |
| 6409 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6410 | 'That is not a future statement; it’s an ordinary import statement ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6411 | 'with\n' |
| 6412 | 'no special semantics or syntax restrictions.\n' |
| 6413 | '\n' |
| 6414 | 'Code compiled by calls to the built-in functions "exec()" and\n' |
| 6415 | '"compile()" that occur in a module "M" containing a future ' |
| 6416 | 'statement\n' |
| 6417 | 'will, by default, use the new syntax or semantics associated with ' |
| 6418 | 'the\n' |
| 6419 | 'future statement. This can be controlled by optional arguments ' |
| 6420 | 'to\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6421 | '"compile()" — see the documentation of that function for details.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6422 | '\n' |
| 6423 | 'A future statement typed at an interactive interpreter prompt ' |
| 6424 | 'will\n' |
| 6425 | 'take effect for the rest of the interpreter session. If an\n' |
| 6426 | 'interpreter is started with the "-i" option, is passed a script ' |
| 6427 | 'name\n' |
| 6428 | 'to execute, and the script includes a future statement, it will be ' |
| 6429 | 'in\n' |
| 6430 | 'effect in the interactive session started after the script is\n' |
| 6431 | 'executed.\n' |
| 6432 | '\n' |
| 6433 | 'See also:\n' |
| 6434 | '\n' |
| 6435 | ' **PEP 236** - Back to the __future__\n' |
| 6436 | ' The original proposal for the __future__ mechanism.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6437 | 'in': 'Membership test operations\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6438 | '**************************\n' |
| 6439 | '\n' |
| 6440 | 'The operators "in" and "not in" test for membership. "x in s"\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6441 | 'evaluates to "True" if *x* is a member of *s*, and "False" otherwise.\n' |
| 6442 | '"x not in s" returns the negation of "x in s". All built-in ' |
| 6443 | 'sequences\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6444 | 'and set types support this as well as dictionary, for which "in" ' |
| 6445 | 'tests\n' |
| 6446 | 'whether the dictionary has a given key. For container types such as\n' |
| 6447 | 'list, tuple, set, frozenset, dict, or collections.deque, the\n' |
| 6448 | 'expression "x in y" is equivalent to "any(x is e or x == e for e in\n' |
| 6449 | 'y)".\n' |
| 6450 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6451 | 'For the string and bytes types, "x in y" is "True" if and only if *x*\n' |
| 6452 | 'is a substring of *y*. An equivalent test is "y.find(x) != -1".\n' |
| 6453 | 'Empty strings are always considered to be a substring of any other\n' |
| 6454 | 'string, so """ in "abc"" will return "True".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6455 | '\n' |
| 6456 | 'For user-defined classes which define the "__contains__()" method, "x\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6457 | 'in y" returns "True" if "y.__contains__(x)" returns a true value, and\n' |
| 6458 | '"False" otherwise.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6459 | '\n' |
| 6460 | 'For user-defined classes which do not define "__contains__()" but do\n' |
Łukasz Langa | 3b5deb0 | 2019-06-04 19:44:34 +0200 | [diff] [blame] | 6461 | 'define "__iter__()", "x in y" is "True" if some value "z", for which\n' |
| 6462 | 'the expression "x is z or x == z" is true, is produced while ' |
| 6463 | 'iterating\n' |
| 6464 | 'over "y". If an exception is raised during the iteration, it is as if\n' |
| 6465 | '"in" raised that exception.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6466 | '\n' |
| 6467 | 'Lastly, the old-style iteration protocol is tried: if a class defines\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6468 | '"__getitem__()", "x in y" is "True" if and only if there is a non-\n' |
Łukasz Langa | 3b5deb0 | 2019-06-04 19:44:34 +0200 | [diff] [blame] | 6469 | 'negative integer index *i* such that "x is y[i] or x == y[i]", and no\n' |
| 6470 | 'lower integer index raises the "IndexError" exception. (If any other\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6471 | 'exception is raised, it is as if "in" raised that exception).\n' |
| 6472 | '\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 6473 | 'The operator "not in" is defined to have the inverse truth value of\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6474 | '"in".\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6475 | 'integers': 'Integer literals\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6476 | '****************\n' |
| 6477 | '\n' |
| 6478 | 'Integer literals are described by the following lexical ' |
| 6479 | 'definitions:\n' |
| 6480 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 6481 | ' integer ::= decinteger | bininteger | octinteger | ' |
| 6482 | 'hexinteger\n' |
| 6483 | ' decinteger ::= nonzerodigit (["_"] digit)* | "0"+ (["_"] ' |
| 6484 | '"0")*\n' |
| 6485 | ' bininteger ::= "0" ("b" | "B") (["_"] bindigit)+\n' |
| 6486 | ' octinteger ::= "0" ("o" | "O") (["_"] octdigit)+\n' |
| 6487 | ' hexinteger ::= "0" ("x" | "X") (["_"] hexdigit)+\n' |
| 6488 | ' nonzerodigit ::= "1"..."9"\n' |
| 6489 | ' digit ::= "0"..."9"\n' |
| 6490 | ' bindigit ::= "0" | "1"\n' |
| 6491 | ' octdigit ::= "0"..."7"\n' |
| 6492 | ' hexdigit ::= digit | "a"..."f" | "A"..."F"\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6493 | '\n' |
| 6494 | 'There is no limit for the length of integer literals apart from ' |
| 6495 | 'what\n' |
| 6496 | 'can be stored in available memory.\n' |
| 6497 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 6498 | 'Underscores are ignored for determining the numeric value of ' |
| 6499 | 'the\n' |
| 6500 | 'literal. They can be used to group digits for enhanced ' |
| 6501 | 'readability.\n' |
| 6502 | 'One underscore can occur between digits, and after base ' |
| 6503 | 'specifiers\n' |
| 6504 | 'like "0x".\n' |
| 6505 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6506 | 'Note that leading zeros in a non-zero decimal number are not ' |
| 6507 | 'allowed.\n' |
| 6508 | 'This is for disambiguation with C-style octal literals, which ' |
| 6509 | 'Python\n' |
| 6510 | 'used before version 3.0.\n' |
| 6511 | '\n' |
| 6512 | 'Some examples of integer literals:\n' |
| 6513 | '\n' |
| 6514 | ' 7 2147483647 0o177 0b100110111\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 6515 | ' 3 79228162514264337593543950336 0o377 0xdeadbeef\n' |
| 6516 | ' 100_000_000_000 0b_1110_0101\n' |
| 6517 | '\n' |
| 6518 | 'Changed in version 3.6: Underscores are now allowed for ' |
| 6519 | 'grouping\n' |
| 6520 | 'purposes in literals.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6521 | 'lambda': 'Lambdas\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6522 | '*******\n' |
| 6523 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6524 | ' lambda_expr ::= "lambda" [parameter_list] ":" ' |
| 6525 | 'expression\n' |
| 6526 | ' lambda_expr_nocond ::= "lambda" [parameter_list] ":" ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6527 | 'expression_nocond\n' |
| 6528 | '\n' |
| 6529 | 'Lambda expressions (sometimes called lambda forms) are used to ' |
| 6530 | 'create\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6531 | 'anonymous functions. The expression "lambda parameters: ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6532 | 'expression"\n' |
| 6533 | 'yields a function object. The unnamed object behaves like a ' |
| 6534 | 'function\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 6535 | 'object defined with:\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6536 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6537 | ' def <lambda>(parameters):\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6538 | ' return expression\n' |
| 6539 | '\n' |
| 6540 | 'See section Function definitions for the syntax of parameter ' |
| 6541 | 'lists.\n' |
| 6542 | 'Note that functions created with lambda expressions cannot ' |
| 6543 | 'contain\n' |
| 6544 | 'statements or annotations.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6545 | 'lists': 'List displays\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6546 | '*************\n' |
| 6547 | '\n' |
| 6548 | 'A list display is a possibly empty series of expressions enclosed ' |
| 6549 | 'in\n' |
| 6550 | 'square brackets:\n' |
| 6551 | '\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 6552 | ' list_display ::= "[" [starred_list | comprehension] "]"\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6553 | '\n' |
| 6554 | 'A list display yields a new list object, the contents being ' |
| 6555 | 'specified\n' |
| 6556 | 'by either a list of expressions or a comprehension. When a comma-\n' |
| 6557 | 'separated list of expressions is supplied, its elements are ' |
| 6558 | 'evaluated\n' |
| 6559 | 'from left to right and placed into the list object in that order.\n' |
| 6560 | 'When a comprehension is supplied, the list is constructed from the\n' |
| 6561 | 'elements resulting from the comprehension.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6562 | 'naming': 'Naming and binding\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6563 | '******************\n' |
| 6564 | '\n' |
| 6565 | '\n' |
| 6566 | 'Binding of names\n' |
| 6567 | '================\n' |
| 6568 | '\n' |
| 6569 | '*Names* refer to objects. Names are introduced by name binding\n' |
| 6570 | 'operations.\n' |
| 6571 | '\n' |
| 6572 | 'The following constructs bind names: formal parameters to ' |
| 6573 | 'functions,\n' |
| 6574 | '"import" statements, class and function definitions (these bind ' |
| 6575 | 'the\n' |
| 6576 | 'class or function name in the defining block), and targets that ' |
| 6577 | 'are\n' |
| 6578 | 'identifiers if occurring in an assignment, "for" loop header, or ' |
| 6579 | 'after\n' |
| 6580 | '"as" in a "with" statement or "except" clause. The "import" ' |
| 6581 | 'statement\n' |
| 6582 | 'of the form "from ... import *" binds all names defined in the\n' |
| 6583 | 'imported module, except those beginning with an underscore. This ' |
| 6584 | 'form\n' |
| 6585 | 'may only be used at the module level.\n' |
| 6586 | '\n' |
| 6587 | 'A target occurring in a "del" statement is also considered bound ' |
| 6588 | 'for\n' |
| 6589 | 'this purpose (though the actual semantics are to unbind the ' |
| 6590 | 'name).\n' |
| 6591 | '\n' |
| 6592 | 'Each assignment or import statement occurs within a block defined ' |
| 6593 | 'by a\n' |
| 6594 | 'class or function definition or at the module level (the ' |
| 6595 | 'top-level\n' |
| 6596 | 'code block).\n' |
| 6597 | '\n' |
| 6598 | 'If a name is bound in a block, it is a local variable of that ' |
| 6599 | 'block,\n' |
| 6600 | 'unless declared as "nonlocal" or "global". If a name is bound at ' |
| 6601 | 'the\n' |
| 6602 | 'module level, it is a global variable. (The variables of the ' |
| 6603 | 'module\n' |
| 6604 | 'code block are local and global.) If a variable is used in a ' |
| 6605 | 'code\n' |
| 6606 | 'block but not defined there, it is a *free variable*.\n' |
| 6607 | '\n' |
| 6608 | 'Each occurrence of a name in the program text refers to the ' |
| 6609 | '*binding*\n' |
| 6610 | 'of that name established by the following name resolution rules.\n' |
| 6611 | '\n' |
| 6612 | '\n' |
| 6613 | 'Resolution of names\n' |
| 6614 | '===================\n' |
| 6615 | '\n' |
| 6616 | 'A *scope* defines the visibility of a name within a block. If a ' |
| 6617 | 'local\n' |
| 6618 | 'variable is defined in a block, its scope includes that block. If ' |
| 6619 | 'the\n' |
| 6620 | 'definition occurs in a function block, the scope extends to any ' |
| 6621 | 'blocks\n' |
| 6622 | 'contained within the defining one, unless a contained block ' |
| 6623 | 'introduces\n' |
| 6624 | 'a different binding for the name.\n' |
| 6625 | '\n' |
| 6626 | 'When a name is used in a code block, it is resolved using the ' |
| 6627 | 'nearest\n' |
| 6628 | 'enclosing scope. The set of all such scopes visible to a code ' |
| 6629 | 'block\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6630 | 'is called the block’s *environment*.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6631 | '\n' |
| 6632 | 'When a name is not found at all, a "NameError" exception is ' |
| 6633 | 'raised. If\n' |
| 6634 | 'the current scope is a function scope, and the name refers to a ' |
| 6635 | 'local\n' |
| 6636 | 'variable that has not yet been bound to a value at the point where ' |
| 6637 | 'the\n' |
| 6638 | 'name is used, an "UnboundLocalError" exception is raised.\n' |
| 6639 | '"UnboundLocalError" is a subclass of "NameError".\n' |
| 6640 | '\n' |
| 6641 | 'If a name binding operation occurs anywhere within a code block, ' |
| 6642 | 'all\n' |
| 6643 | 'uses of the name within the block are treated as references to ' |
| 6644 | 'the\n' |
| 6645 | 'current block. This can lead to errors when a name is used within ' |
| 6646 | 'a\n' |
| 6647 | 'block before it is bound. This rule is subtle. Python lacks\n' |
| 6648 | 'declarations and allows name binding operations to occur anywhere\n' |
| 6649 | 'within a code block. The local variables of a code block can be\n' |
| 6650 | 'determined by scanning the entire text of the block for name ' |
| 6651 | 'binding\n' |
| 6652 | 'operations.\n' |
| 6653 | '\n' |
| 6654 | 'If the "global" statement occurs within a block, all uses of the ' |
| 6655 | 'name\n' |
| 6656 | 'specified in the statement refer to the binding of that name in ' |
| 6657 | 'the\n' |
| 6658 | 'top-level namespace. Names are resolved in the top-level ' |
| 6659 | 'namespace by\n' |
| 6660 | 'searching the global namespace, i.e. the namespace of the module\n' |
| 6661 | 'containing the code block, and the builtins namespace, the ' |
| 6662 | 'namespace\n' |
| 6663 | 'of the module "builtins". The global namespace is searched ' |
| 6664 | 'first. If\n' |
| 6665 | 'the name is not found there, the builtins namespace is searched. ' |
| 6666 | 'The\n' |
| 6667 | '"global" statement must precede all uses of the name.\n' |
| 6668 | '\n' |
| 6669 | 'The "global" statement has the same scope as a name binding ' |
| 6670 | 'operation\n' |
| 6671 | 'in the same block. If the nearest enclosing scope for a free ' |
| 6672 | 'variable\n' |
| 6673 | 'contains a global statement, the free variable is treated as a ' |
| 6674 | 'global.\n' |
| 6675 | '\n' |
| 6676 | 'The "nonlocal" statement causes corresponding names to refer to\n' |
| 6677 | 'previously bound variables in the nearest enclosing function ' |
| 6678 | 'scope.\n' |
| 6679 | '"SyntaxError" is raised at compile time if the given name does ' |
| 6680 | 'not\n' |
| 6681 | 'exist in any enclosing function scope.\n' |
| 6682 | '\n' |
| 6683 | 'The namespace for a module is automatically created the first time ' |
| 6684 | 'a\n' |
| 6685 | 'module is imported. The main module for a script is always ' |
| 6686 | 'called\n' |
| 6687 | '"__main__".\n' |
| 6688 | '\n' |
| 6689 | 'Class definition blocks and arguments to "exec()" and "eval()" ' |
| 6690 | 'are\n' |
| 6691 | 'special in the context of name resolution. A class definition is ' |
| 6692 | 'an\n' |
| 6693 | 'executable statement that may use and define names. These ' |
| 6694 | 'references\n' |
| 6695 | 'follow the normal rules for name resolution with an exception ' |
| 6696 | 'that\n' |
| 6697 | 'unbound local variables are looked up in the global namespace. ' |
| 6698 | 'The\n' |
| 6699 | 'namespace of the class definition becomes the attribute dictionary ' |
| 6700 | 'of\n' |
| 6701 | 'the class. The scope of names defined in a class block is limited ' |
| 6702 | 'to\n' |
| 6703 | 'the class block; it does not extend to the code blocks of methods ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6704 | '–\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6705 | 'this includes comprehensions and generator expressions since they ' |
| 6706 | 'are\n' |
| 6707 | 'implemented using a function scope. This means that the ' |
| 6708 | 'following\n' |
| 6709 | 'will fail:\n' |
| 6710 | '\n' |
| 6711 | ' class A:\n' |
| 6712 | ' a = 42\n' |
| 6713 | ' b = list(a + i for i in range(10))\n' |
| 6714 | '\n' |
| 6715 | '\n' |
| 6716 | 'Builtins and restricted execution\n' |
| 6717 | '=================================\n' |
| 6718 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6719 | '**CPython implementation detail:** Users should not touch\n' |
| 6720 | '"__builtins__"; it is strictly an implementation detail. Users\n' |
| 6721 | 'wanting to override values in the builtins namespace should ' |
| 6722 | '"import"\n' |
| 6723 | 'the "builtins" module and modify its attributes appropriately.\n' |
| 6724 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6725 | 'The builtins namespace associated with the execution of a code ' |
| 6726 | 'block\n' |
| 6727 | 'is actually found by looking up the name "__builtins__" in its ' |
| 6728 | 'global\n' |
| 6729 | 'namespace; this should be a dictionary or a module (in the latter ' |
| 6730 | 'case\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6731 | 'the module’s dictionary is used). By default, when in the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6732 | '"__main__"\n' |
| 6733 | 'module, "__builtins__" is the built-in module "builtins"; when in ' |
| 6734 | 'any\n' |
| 6735 | 'other module, "__builtins__" is an alias for the dictionary of ' |
| 6736 | 'the\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6737 | '"builtins" module itself.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6738 | '\n' |
| 6739 | '\n' |
| 6740 | 'Interaction with dynamic features\n' |
| 6741 | '=================================\n' |
| 6742 | '\n' |
| 6743 | 'Name resolution of free variables occurs at runtime, not at ' |
| 6744 | 'compile\n' |
| 6745 | 'time. This means that the following code will print 42:\n' |
| 6746 | '\n' |
| 6747 | ' i = 10\n' |
| 6748 | ' def f():\n' |
| 6749 | ' print(i)\n' |
| 6750 | ' i = 42\n' |
| 6751 | ' f()\n' |
| 6752 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6753 | 'The "eval()" and "exec()" functions do not have access to the ' |
| 6754 | 'full\n' |
| 6755 | 'environment for resolving names. Names may be resolved in the ' |
| 6756 | 'local\n' |
| 6757 | 'and global namespaces of the caller. Free variables are not ' |
| 6758 | 'resolved\n' |
| 6759 | 'in the nearest enclosing namespace, but in the global namespace. ' |
| 6760 | '[1]\n' |
| 6761 | 'The "exec()" and "eval()" functions have optional arguments to\n' |
| 6762 | 'override the global and local namespace. If only one namespace ' |
| 6763 | 'is\n' |
| 6764 | 'specified, it is used for both.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6765 | 'nonlocal': 'The "nonlocal" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6766 | '************************\n' |
| 6767 | '\n' |
| 6768 | ' nonlocal_stmt ::= "nonlocal" identifier ("," identifier)*\n' |
| 6769 | '\n' |
| 6770 | 'The "nonlocal" statement causes the listed identifiers to refer ' |
| 6771 | 'to\n' |
| 6772 | 'previously bound variables in the nearest enclosing scope ' |
| 6773 | 'excluding\n' |
| 6774 | 'globals. This is important because the default behavior for ' |
| 6775 | 'binding is\n' |
| 6776 | 'to search the local namespace first. The statement allows\n' |
| 6777 | 'encapsulated code to rebind variables outside of the local ' |
| 6778 | 'scope\n' |
| 6779 | 'besides the global (module) scope.\n' |
| 6780 | '\n' |
| 6781 | 'Names listed in a "nonlocal" statement, unlike those listed in ' |
| 6782 | 'a\n' |
| 6783 | '"global" statement, must refer to pre-existing bindings in an\n' |
| 6784 | 'enclosing scope (the scope in which a new binding should be ' |
| 6785 | 'created\n' |
| 6786 | 'cannot be determined unambiguously).\n' |
| 6787 | '\n' |
| 6788 | 'Names listed in a "nonlocal" statement must not collide with ' |
| 6789 | 'pre-\n' |
| 6790 | 'existing bindings in the local scope.\n' |
| 6791 | '\n' |
| 6792 | 'See also:\n' |
| 6793 | '\n' |
| 6794 | ' **PEP 3104** - Access to Names in Outer Scopes\n' |
| 6795 | ' The specification for the "nonlocal" statement.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6796 | 'numbers': 'Numeric literals\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6797 | '****************\n' |
| 6798 | '\n' |
| 6799 | 'There are three types of numeric literals: integers, floating ' |
| 6800 | 'point\n' |
| 6801 | 'numbers, and imaginary numbers. There are no complex literals\n' |
| 6802 | '(complex numbers can be formed by adding a real number and an\n' |
| 6803 | 'imaginary number).\n' |
| 6804 | '\n' |
| 6805 | 'Note that numeric literals do not include a sign; a phrase like ' |
| 6806 | '"-1"\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6807 | 'is actually an expression composed of the unary operator ‘"-"‘ ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6808 | 'and the\n' |
| 6809 | 'literal "1".\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 6810 | 'numeric-types': 'Emulating numeric types\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6811 | '***********************\n' |
| 6812 | '\n' |
| 6813 | 'The following methods can be defined to emulate numeric ' |
| 6814 | 'objects.\n' |
| 6815 | 'Methods corresponding to operations that are not supported ' |
| 6816 | 'by the\n' |
| 6817 | 'particular kind of number implemented (e.g., bitwise ' |
| 6818 | 'operations for\n' |
| 6819 | 'non-integral numbers) should be left undefined.\n' |
| 6820 | '\n' |
| 6821 | 'object.__add__(self, other)\n' |
| 6822 | 'object.__sub__(self, other)\n' |
| 6823 | 'object.__mul__(self, other)\n' |
| 6824 | 'object.__matmul__(self, other)\n' |
| 6825 | 'object.__truediv__(self, other)\n' |
| 6826 | 'object.__floordiv__(self, other)\n' |
| 6827 | 'object.__mod__(self, other)\n' |
| 6828 | 'object.__divmod__(self, other)\n' |
| 6829 | 'object.__pow__(self, other[, modulo])\n' |
| 6830 | 'object.__lshift__(self, other)\n' |
| 6831 | 'object.__rshift__(self, other)\n' |
| 6832 | 'object.__and__(self, other)\n' |
| 6833 | 'object.__xor__(self, other)\n' |
| 6834 | 'object.__or__(self, other)\n' |
| 6835 | '\n' |
| 6836 | ' These methods are called to implement the binary ' |
| 6837 | 'arithmetic\n' |
| 6838 | ' operations ("+", "-", "*", "@", "/", "//", "%", ' |
| 6839 | '"divmod()",\n' |
| 6840 | ' "pow()", "**", "<<", ">>", "&", "^", "|"). For ' |
| 6841 | 'instance, to\n' |
| 6842 | ' evaluate the expression "x + y", where *x* is an ' |
| 6843 | 'instance of a\n' |
| 6844 | ' class that has an "__add__()" method, "x.__add__(y)" is ' |
| 6845 | 'called.\n' |
| 6846 | ' The "__divmod__()" method should be the equivalent to ' |
| 6847 | 'using\n' |
| 6848 | ' "__floordiv__()" and "__mod__()"; it should not be ' |
| 6849 | 'related to\n' |
| 6850 | ' "__truediv__()". Note that "__pow__()" should be ' |
| 6851 | 'defined to accept\n' |
| 6852 | ' an optional third argument if the ternary version of the ' |
| 6853 | 'built-in\n' |
| 6854 | ' "pow()" function is to be supported.\n' |
| 6855 | '\n' |
| 6856 | ' If one of those methods does not support the operation ' |
| 6857 | 'with the\n' |
| 6858 | ' supplied arguments, it should return "NotImplemented".\n' |
| 6859 | '\n' |
| 6860 | 'object.__radd__(self, other)\n' |
| 6861 | 'object.__rsub__(self, other)\n' |
| 6862 | 'object.__rmul__(self, other)\n' |
| 6863 | 'object.__rmatmul__(self, other)\n' |
| 6864 | 'object.__rtruediv__(self, other)\n' |
| 6865 | 'object.__rfloordiv__(self, other)\n' |
| 6866 | 'object.__rmod__(self, other)\n' |
| 6867 | 'object.__rdivmod__(self, other)\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 6868 | 'object.__rpow__(self, other[, modulo])\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6869 | 'object.__rlshift__(self, other)\n' |
| 6870 | 'object.__rrshift__(self, other)\n' |
| 6871 | 'object.__rand__(self, other)\n' |
| 6872 | 'object.__rxor__(self, other)\n' |
| 6873 | 'object.__ror__(self, other)\n' |
| 6874 | '\n' |
| 6875 | ' These methods are called to implement the binary ' |
| 6876 | 'arithmetic\n' |
| 6877 | ' operations ("+", "-", "*", "@", "/", "//", "%", ' |
| 6878 | '"divmod()",\n' |
| 6879 | ' "pow()", "**", "<<", ">>", "&", "^", "|") with reflected ' |
| 6880 | '(swapped)\n' |
| 6881 | ' operands. These functions are only called if the left ' |
| 6882 | 'operand does\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 6883 | ' not support the corresponding operation [3] and the ' |
| 6884 | 'operands are of\n' |
| 6885 | ' different types. [4] For instance, to evaluate the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6886 | 'expression "x -\n' |
| 6887 | ' y", where *y* is an instance of a class that has an ' |
| 6888 | '"__rsub__()"\n' |
| 6889 | ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" ' |
| 6890 | 'returns\n' |
| 6891 | ' *NotImplemented*.\n' |
| 6892 | '\n' |
| 6893 | ' Note that ternary "pow()" will not try calling ' |
| 6894 | '"__rpow__()" (the\n' |
| 6895 | ' coercion rules would become too complicated).\n' |
| 6896 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6897 | ' Note: If the right operand’s type is a subclass of the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6898 | 'left\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6899 | ' operand’s type and that subclass provides the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6900 | 'reflected method\n' |
| 6901 | ' for the operation, this method will be called before ' |
| 6902 | 'the left\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6903 | ' operand’s non-reflected method. This behavior allows ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6904 | 'subclasses\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6905 | ' to override their ancestors’ operations.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6906 | '\n' |
| 6907 | 'object.__iadd__(self, other)\n' |
| 6908 | 'object.__isub__(self, other)\n' |
| 6909 | 'object.__imul__(self, other)\n' |
| 6910 | 'object.__imatmul__(self, other)\n' |
| 6911 | 'object.__itruediv__(self, other)\n' |
| 6912 | 'object.__ifloordiv__(self, other)\n' |
| 6913 | 'object.__imod__(self, other)\n' |
| 6914 | 'object.__ipow__(self, other[, modulo])\n' |
| 6915 | 'object.__ilshift__(self, other)\n' |
| 6916 | 'object.__irshift__(self, other)\n' |
| 6917 | 'object.__iand__(self, other)\n' |
| 6918 | 'object.__ixor__(self, other)\n' |
| 6919 | 'object.__ior__(self, other)\n' |
| 6920 | '\n' |
| 6921 | ' These methods are called to implement the augmented ' |
| 6922 | 'arithmetic\n' |
| 6923 | ' assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", ' |
| 6924 | '"**=",\n' |
| 6925 | ' "<<=", ">>=", "&=", "^=", "|="). These methods should ' |
| 6926 | 'attempt to\n' |
| 6927 | ' do the operation in-place (modifying *self*) and return ' |
| 6928 | 'the result\n' |
| 6929 | ' (which could be, but does not have to be, *self*). If a ' |
| 6930 | 'specific\n' |
| 6931 | ' method is not defined, the augmented assignment falls ' |
| 6932 | 'back to the\n' |
| 6933 | ' normal methods. For instance, if *x* is an instance of ' |
| 6934 | 'a class\n' |
| 6935 | ' with an "__iadd__()" method, "x += y" is equivalent to ' |
| 6936 | '"x =\n' |
| 6937 | ' x.__iadd__(y)" . Otherwise, "x.__add__(y)" and ' |
| 6938 | '"y.__radd__(x)" are\n' |
| 6939 | ' considered, as with the evaluation of "x + y". In ' |
| 6940 | 'certain\n' |
| 6941 | ' situations, augmented assignment can result in ' |
| 6942 | 'unexpected errors\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6943 | ' (see Why does a_tuple[i] += [‘item’] raise an exception ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6944 | 'when the\n' |
| 6945 | ' addition works?), but this behavior is in fact part of ' |
| 6946 | 'the data\n' |
| 6947 | ' model.\n' |
| 6948 | '\n' |
| 6949 | 'object.__neg__(self)\n' |
| 6950 | 'object.__pos__(self)\n' |
| 6951 | 'object.__abs__(self)\n' |
| 6952 | 'object.__invert__(self)\n' |
| 6953 | '\n' |
| 6954 | ' Called to implement the unary arithmetic operations ' |
| 6955 | '("-", "+",\n' |
| 6956 | ' "abs()" and "~").\n' |
| 6957 | '\n' |
| 6958 | 'object.__complex__(self)\n' |
| 6959 | 'object.__int__(self)\n' |
| 6960 | 'object.__float__(self)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6961 | '\n' |
| 6962 | ' Called to implement the built-in functions "complex()", ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6963 | '"int()" and\n' |
| 6964 | ' "float()". Should return a value of the appropriate ' |
| 6965 | 'type.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 6966 | '\n' |
| 6967 | 'object.__index__(self)\n' |
| 6968 | '\n' |
| 6969 | ' Called to implement "operator.index()", and whenever ' |
| 6970 | 'Python needs\n' |
| 6971 | ' to losslessly convert the numeric object to an integer ' |
| 6972 | 'object (such\n' |
| 6973 | ' as in slicing, or in the built-in "bin()", "hex()" and ' |
| 6974 | '"oct()"\n' |
| 6975 | ' functions). Presence of this method indicates that the ' |
| 6976 | 'numeric\n' |
| 6977 | ' object is an integer type. Must return an integer.\n' |
| 6978 | '\n' |
Łukasz Langa | 3b5deb0 | 2019-06-04 19:44:34 +0200 | [diff] [blame] | 6979 | ' If "__int__()", "__float__()" and "__complex__()" are ' |
| 6980 | 'not defined\n' |
| 6981 | ' then corresponding built-in functions "int()", "float()" ' |
| 6982 | 'and\n' |
| 6983 | ' "complex()" fall back to "__index__()".\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 6984 | '\n' |
| 6985 | 'object.__round__(self[, ndigits])\n' |
| 6986 | 'object.__trunc__(self)\n' |
| 6987 | 'object.__floor__(self)\n' |
| 6988 | 'object.__ceil__(self)\n' |
| 6989 | '\n' |
| 6990 | ' Called to implement the built-in function "round()" and ' |
| 6991 | '"math"\n' |
| 6992 | ' functions "trunc()", "floor()" and "ceil()". Unless ' |
| 6993 | '*ndigits* is\n' |
| 6994 | ' passed to "__round__()" all these methods should return ' |
| 6995 | 'the value\n' |
| 6996 | ' of the object truncated to an "Integral" (typically an ' |
| 6997 | '"int").\n' |
| 6998 | '\n' |
| 6999 | ' If "__int__()" is not defined then the built-in function ' |
| 7000 | '"int()"\n' |
| 7001 | ' falls back to "__trunc__()".\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7002 | 'objects': 'Objects, values and types\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7003 | '*************************\n' |
| 7004 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7005 | '*Objects* are Python’s abstraction for data. All data in a ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7006 | 'Python\n' |
| 7007 | 'program is represented by objects or by relations between ' |
| 7008 | 'objects. (In\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7009 | 'a sense, and in conformance to Von Neumann’s model of a “stored\n' |
Łukasz Langa | bc1c8af | 2020-04-27 22:44:04 +0200 | [diff] [blame] | 7010 | 'program computer”, code is also represented by objects.)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7011 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7012 | 'Every object has an identity, a type and a value. An object’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7013 | '*identity* never changes once it has been created; you may think ' |
| 7014 | 'of it\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7015 | 'as the object’s address in memory. The ‘"is"’ operator compares ' |
| 7016 | 'the\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7017 | 'identity of two objects; the "id()" function returns an integer\n' |
| 7018 | 'representing its identity.\n' |
| 7019 | '\n' |
| 7020 | '**CPython implementation detail:** For CPython, "id(x)" is the ' |
| 7021 | 'memory\n' |
| 7022 | 'address where "x" is stored.\n' |
| 7023 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7024 | 'An object’s type determines the operations that the object ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7025 | 'supports\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7026 | '(e.g., “does it have a length?”) and also defines the possible ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7027 | 'values\n' |
| 7028 | 'for objects of that type. The "type()" function returns an ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7029 | 'object’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7030 | 'type (which is an object itself). Like its identity, an ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7031 | 'object’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7032 | '*type* is also unchangeable. [1]\n' |
| 7033 | '\n' |
| 7034 | 'The *value* of some objects can change. Objects whose value can\n' |
| 7035 | 'change are said to be *mutable*; objects whose value is ' |
| 7036 | 'unchangeable\n' |
| 7037 | 'once they are created are called *immutable*. (The value of an\n' |
| 7038 | 'immutable container object that contains a reference to a ' |
| 7039 | 'mutable\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7040 | 'object can change when the latter’s value is changed; however ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7041 | 'the\n' |
| 7042 | 'container is still considered immutable, because the collection ' |
| 7043 | 'of\n' |
| 7044 | 'objects it contains cannot be changed. So, immutability is not\n' |
| 7045 | 'strictly the same as having an unchangeable value, it is more ' |
| 7046 | 'subtle.)\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7047 | 'An object’s mutability is determined by its type; for instance,\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7048 | 'numbers, strings and tuples are immutable, while dictionaries ' |
| 7049 | 'and\n' |
| 7050 | 'lists are mutable.\n' |
| 7051 | '\n' |
| 7052 | 'Objects are never explicitly destroyed; however, when they ' |
| 7053 | 'become\n' |
| 7054 | 'unreachable they may be garbage-collected. An implementation is\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7055 | 'allowed to postpone garbage collection or omit it altogether — it ' |
| 7056 | 'is a\n' |
| 7057 | 'matter of implementation quality how garbage collection is\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7058 | 'implemented, as long as no objects are collected that are still\n' |
| 7059 | 'reachable.\n' |
| 7060 | '\n' |
| 7061 | '**CPython implementation detail:** CPython currently uses a ' |
| 7062 | 'reference-\n' |
| 7063 | 'counting scheme with (optional) delayed detection of cyclically ' |
| 7064 | 'linked\n' |
| 7065 | 'garbage, which collects most objects as soon as they become\n' |
| 7066 | 'unreachable, but is not guaranteed to collect garbage containing\n' |
| 7067 | 'circular references. See the documentation of the "gc" module ' |
| 7068 | 'for\n' |
| 7069 | 'information on controlling the collection of cyclic garbage. ' |
| 7070 | 'Other\n' |
| 7071 | 'implementations act differently and CPython may change. Do not ' |
| 7072 | 'depend\n' |
| 7073 | 'on immediate finalization of objects when they become unreachable ' |
| 7074 | '(so\n' |
| 7075 | 'you should always close files explicitly).\n' |
| 7076 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7077 | 'Note that the use of the implementation’s tracing or debugging\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7078 | 'facilities may keep objects alive that would normally be ' |
| 7079 | 'collectable.\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7080 | 'Also note that catching an exception with a ‘"try"…"except"’ ' |
| 7081 | 'statement\n' |
| 7082 | 'may keep objects alive.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7083 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7084 | 'Some objects contain references to “external” resources such as ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7085 | 'open\n' |
| 7086 | 'files or windows. It is understood that these resources are ' |
| 7087 | 'freed\n' |
| 7088 | 'when the object is garbage-collected, but since garbage ' |
| 7089 | 'collection is\n' |
| 7090 | 'not guaranteed to happen, such objects also provide an explicit ' |
| 7091 | 'way to\n' |
| 7092 | 'release the external resource, usually a "close()" method. ' |
| 7093 | 'Programs\n' |
| 7094 | 'are strongly recommended to explicitly close such objects. The\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7095 | '‘"try"…"finally"’ statement and the ‘"with"’ statement provide\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7096 | 'convenient ways to do this.\n' |
| 7097 | '\n' |
| 7098 | 'Some objects contain references to other objects; these are ' |
| 7099 | 'called\n' |
| 7100 | '*containers*. Examples of containers are tuples, lists and\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7101 | 'dictionaries. The references are part of a container’s value. ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7102 | 'In\n' |
| 7103 | 'most cases, when we talk about the value of a container, we imply ' |
| 7104 | 'the\n' |
| 7105 | 'values, not the identities of the contained objects; however, ' |
| 7106 | 'when we\n' |
| 7107 | 'talk about the mutability of a container, only the identities of ' |
| 7108 | 'the\n' |
| 7109 | 'immediately contained objects are implied. So, if an immutable\n' |
| 7110 | 'container (like a tuple) contains a reference to a mutable ' |
| 7111 | 'object, its\n' |
| 7112 | 'value changes if that mutable object is changed.\n' |
| 7113 | '\n' |
| 7114 | 'Types affect almost all aspects of object behavior. Even the\n' |
| 7115 | 'importance of object identity is affected in some sense: for ' |
| 7116 | 'immutable\n' |
| 7117 | 'types, operations that compute new values may actually return a\n' |
| 7118 | 'reference to any existing object with the same type and value, ' |
| 7119 | 'while\n' |
| 7120 | 'for mutable objects this is not allowed. E.g., after "a = 1; b = ' |
| 7121 | '1",\n' |
| 7122 | '"a" and "b" may or may not refer to the same object with the ' |
| 7123 | 'value\n' |
| 7124 | 'one, depending on the implementation, but after "c = []; d = []", ' |
| 7125 | '"c"\n' |
| 7126 | 'and "d" are guaranteed to refer to two different, unique, newly\n' |
| 7127 | 'created empty lists. (Note that "c = d = []" assigns the same ' |
| 7128 | 'object\n' |
| 7129 | 'to both "c" and "d".)\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7130 | 'operator-summary': 'Operator precedence\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7131 | '*******************\n' |
| 7132 | '\n' |
| 7133 | 'The following table summarizes the operator precedence ' |
| 7134 | 'in Python, from\n' |
| 7135 | 'lowest precedence (least binding) to highest precedence ' |
| 7136 | '(most\n' |
| 7137 | 'binding). Operators in the same box have the same ' |
| 7138 | 'precedence. Unless\n' |
| 7139 | 'the syntax is explicitly given, operators are binary. ' |
| 7140 | 'Operators in\n' |
| 7141 | 'the same box group left to right (except for ' |
| 7142 | 'exponentiation, which\n' |
| 7143 | 'groups from right to left).\n' |
| 7144 | '\n' |
| 7145 | 'Note that comparisons, membership tests, and identity ' |
| 7146 | 'tests, all have\n' |
| 7147 | 'the same precedence and have a left-to-right chaining ' |
| 7148 | 'feature as\n' |
| 7149 | 'described in the Comparisons section.\n' |
| 7150 | '\n' |
| 7151 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7152 | '| Operator | ' |
| 7153 | 'Description |\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 7154 | '|=================================================|=======================================|\n' |
| 7155 | '| ":=" | ' |
| 7156 | 'Assignment expression |\n' |
| 7157 | '+-------------------------------------------------+---------------------------------------+\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7158 | '| "lambda" | ' |
| 7159 | 'Lambda expression |\n' |
| 7160 | '+-------------------------------------------------+---------------------------------------+\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7161 | '| "if" – "else" | ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7162 | 'Conditional expression |\n' |
| 7163 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7164 | '| "or" | ' |
| 7165 | 'Boolean OR |\n' |
| 7166 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7167 | '| "and" | ' |
| 7168 | 'Boolean AND |\n' |
| 7169 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7170 | '| "not" "x" | ' |
| 7171 | 'Boolean NOT |\n' |
| 7172 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7173 | '| "in", "not in", "is", "is not", "<", "<=", ">", | ' |
| 7174 | 'Comparisons, including membership |\n' |
| 7175 | '| ">=", "!=", "==" | ' |
| 7176 | 'tests and identity tests |\n' |
| 7177 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7178 | '| "|" | ' |
| 7179 | 'Bitwise OR |\n' |
| 7180 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7181 | '| "^" | ' |
| 7182 | 'Bitwise XOR |\n' |
| 7183 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7184 | '| "&" | ' |
| 7185 | 'Bitwise AND |\n' |
| 7186 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7187 | '| "<<", ">>" | ' |
| 7188 | 'Shifts |\n' |
| 7189 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7190 | '| "+", "-" | ' |
| 7191 | 'Addition and subtraction |\n' |
| 7192 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7193 | '| "*", "@", "/", "//", "%" | ' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7194 | 'Multiplication, matrix |\n' |
| 7195 | '| | ' |
| 7196 | 'multiplication, division, floor |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7197 | '| | ' |
| 7198 | 'division, remainder [5] |\n' |
| 7199 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7200 | '| "+x", "-x", "~x" | ' |
| 7201 | 'Positive, negative, bitwise NOT |\n' |
| 7202 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7203 | '| "**" | ' |
| 7204 | 'Exponentiation [6] |\n' |
| 7205 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7206 | '| "await" "x" | ' |
| 7207 | 'Await expression |\n' |
| 7208 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7209 | '| "x[index]", "x[index:index]", | ' |
| 7210 | 'Subscription, slicing, call, |\n' |
| 7211 | '| "x(arguments...)", "x.attribute" | ' |
| 7212 | 'attribute reference |\n' |
| 7213 | '+-------------------------------------------------+---------------------------------------+\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 7214 | '| "(expressions...)", "[expressions...]", "{key: | ' |
| 7215 | 'Binding or parenthesized expression, |\n' |
| 7216 | '| value...}", "{expressions...}" | list ' |
| 7217 | 'display, dictionary display, set |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7218 | '| | ' |
| 7219 | 'display |\n' |
| 7220 | '+-------------------------------------------------+---------------------------------------+\n' |
| 7221 | '\n' |
| 7222 | '-[ Footnotes ]-\n' |
| 7223 | '\n' |
| 7224 | '[1] While "abs(x%y) < abs(y)" is true mathematically, ' |
| 7225 | 'for floats\n' |
| 7226 | ' it may not be true numerically due to roundoff. For ' |
| 7227 | 'example, and\n' |
| 7228 | ' assuming a platform on which a Python float is an ' |
| 7229 | 'IEEE 754 double-\n' |
| 7230 | ' precision number, in order that "-1e-100 % 1e100" ' |
| 7231 | 'have the same\n' |
| 7232 | ' sign as "1e100", the computed result is "-1e-100 + ' |
| 7233 | '1e100", which\n' |
| 7234 | ' is numerically exactly equal to "1e100". The ' |
| 7235 | 'function\n' |
| 7236 | ' "math.fmod()" returns a result whose sign matches ' |
| 7237 | 'the sign of the\n' |
| 7238 | ' first argument instead, and so returns "-1e-100" in ' |
| 7239 | 'this case.\n' |
| 7240 | ' Which approach is more appropriate depends on the ' |
| 7241 | 'application.\n' |
| 7242 | '\n' |
| 7243 | '[2] If x is very close to an exact integer multiple of ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7244 | 'y, it’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7245 | ' possible for "x//y" to be one larger than ' |
| 7246 | '"(x-x%y)//y" due to\n' |
| 7247 | ' rounding. In such cases, Python returns the latter ' |
| 7248 | 'result, in\n' |
| 7249 | ' order to preserve that "divmod(x,y)[0] * y + x % y" ' |
| 7250 | 'be very close\n' |
| 7251 | ' to "x".\n' |
| 7252 | '\n' |
| 7253 | '[3] The Unicode standard distinguishes between *code ' |
| 7254 | 'points* (e.g.\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7255 | ' U+0041) and *abstract characters* (e.g. “LATIN ' |
| 7256 | 'CAPITAL LETTER A”).\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7257 | ' While most abstract characters in Unicode are only ' |
| 7258 | 'represented\n' |
| 7259 | ' using one code point, there is a number of abstract ' |
| 7260 | 'characters\n' |
| 7261 | ' that can in addition be represented using a sequence ' |
| 7262 | 'of more than\n' |
| 7263 | ' one code point. For example, the abstract character ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7264 | '“LATIN\n' |
| 7265 | ' CAPITAL LETTER C WITH CEDILLA” can be represented as ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7266 | 'a single\n' |
| 7267 | ' *precomposed character* at code position U+00C7, or ' |
| 7268 | 'as a sequence\n' |
| 7269 | ' of a *base character* at code position U+0043 (LATIN ' |
| 7270 | 'CAPITAL\n' |
| 7271 | ' LETTER C), followed by a *combining character* at ' |
| 7272 | 'code position\n' |
| 7273 | ' U+0327 (COMBINING CEDILLA).\n' |
| 7274 | '\n' |
| 7275 | ' The comparison operators on strings compare at the ' |
| 7276 | 'level of\n' |
| 7277 | ' Unicode code points. This may be counter-intuitive ' |
| 7278 | 'to humans. For\n' |
| 7279 | ' example, ""\\u00C7" == "\\u0043\\u0327"" is "False", ' |
| 7280 | 'even though both\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7281 | ' strings represent the same abstract character “LATIN ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7282 | 'CAPITAL\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7283 | ' LETTER C WITH CEDILLA”.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7284 | '\n' |
| 7285 | ' To compare strings at the level of abstract ' |
| 7286 | 'characters (that is,\n' |
| 7287 | ' in a way intuitive to humans), use ' |
| 7288 | '"unicodedata.normalize()".\n' |
| 7289 | '\n' |
| 7290 | '[4] Due to automatic garbage-collection, free lists, and ' |
| 7291 | 'the\n' |
| 7292 | ' dynamic nature of descriptors, you may notice ' |
| 7293 | 'seemingly unusual\n' |
| 7294 | ' behaviour in certain uses of the "is" operator, like ' |
| 7295 | 'those\n' |
| 7296 | ' involving comparisons between instance methods, or ' |
| 7297 | 'constants.\n' |
| 7298 | ' Check their documentation for more info.\n' |
| 7299 | '\n' |
| 7300 | '[5] The "%" operator is also used for string formatting; ' |
| 7301 | 'the same\n' |
| 7302 | ' precedence applies.\n' |
| 7303 | '\n' |
| 7304 | '[6] The power operator "**" binds less tightly than an ' |
| 7305 | 'arithmetic\n' |
| 7306 | ' or bitwise unary operator on its right, that is, ' |
| 7307 | '"2**-1" is "0.5".\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7308 | 'pass': 'The "pass" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7309 | '********************\n' |
| 7310 | '\n' |
| 7311 | ' pass_stmt ::= "pass"\n' |
| 7312 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7313 | '"pass" is a null operation — when it is executed, nothing happens. ' |
| 7314 | 'It\n' |
| 7315 | 'is useful as a placeholder when a statement is required ' |
| 7316 | 'syntactically,\n' |
| 7317 | 'but no code needs to be executed, for example:\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7318 | '\n' |
| 7319 | ' def f(arg): pass # a function that does nothing (yet)\n' |
| 7320 | '\n' |
| 7321 | ' class C: pass # a class with no methods (yet)\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7322 | 'power': 'The power operator\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7323 | '******************\n' |
| 7324 | '\n' |
| 7325 | 'The power operator binds more tightly than unary operators on its\n' |
| 7326 | 'left; it binds less tightly than unary operators on its right. ' |
| 7327 | 'The\n' |
| 7328 | 'syntax is:\n' |
| 7329 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7330 | ' power ::= (await_expr | primary) ["**" u_expr]\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7331 | '\n' |
| 7332 | 'Thus, in an unparenthesized sequence of power and unary operators, ' |
| 7333 | 'the\n' |
| 7334 | 'operators are evaluated from right to left (this does not ' |
| 7335 | 'constrain\n' |
| 7336 | 'the evaluation order for the operands): "-1**2" results in "-1".\n' |
| 7337 | '\n' |
| 7338 | 'The power operator has the same semantics as the built-in "pow()"\n' |
| 7339 | 'function, when called with two arguments: it yields its left ' |
| 7340 | 'argument\n' |
| 7341 | 'raised to the power of its right argument. The numeric arguments ' |
| 7342 | 'are\n' |
| 7343 | 'first converted to a common type, and the result is of that type.\n' |
| 7344 | '\n' |
| 7345 | 'For int operands, the result has the same type as the operands ' |
| 7346 | 'unless\n' |
| 7347 | 'the second argument is negative; in that case, all arguments are\n' |
| 7348 | 'converted to float and a float result is delivered. For example,\n' |
| 7349 | '"10**2" returns "100", but "10**-2" returns "0.01".\n' |
| 7350 | '\n' |
| 7351 | 'Raising "0.0" to a negative power results in a ' |
| 7352 | '"ZeroDivisionError".\n' |
| 7353 | 'Raising a negative number to a fractional power results in a ' |
| 7354 | '"complex"\n' |
| 7355 | 'number. (In earlier versions it raised a "ValueError".)\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7356 | 'raise': 'The "raise" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7357 | '*********************\n' |
| 7358 | '\n' |
| 7359 | ' raise_stmt ::= "raise" [expression ["from" expression]]\n' |
| 7360 | '\n' |
| 7361 | 'If no expressions are present, "raise" re-raises the last ' |
| 7362 | 'exception\n' |
| 7363 | 'that was active in the current scope. If no exception is active ' |
| 7364 | 'in\n' |
| 7365 | 'the current scope, a "RuntimeError" exception is raised indicating\n' |
| 7366 | 'that this is an error.\n' |
| 7367 | '\n' |
| 7368 | 'Otherwise, "raise" evaluates the first expression as the exception\n' |
| 7369 | 'object. It must be either a subclass or an instance of\n' |
| 7370 | '"BaseException". If it is a class, the exception instance will be\n' |
| 7371 | 'obtained when needed by instantiating the class with no arguments.\n' |
| 7372 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7373 | 'The *type* of the exception is the exception instance’s class, the\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7374 | '*value* is the instance itself.\n' |
| 7375 | '\n' |
| 7376 | 'A traceback object is normally created automatically when an ' |
| 7377 | 'exception\n' |
| 7378 | 'is raised and attached to it as the "__traceback__" attribute, ' |
| 7379 | 'which\n' |
| 7380 | 'is writable. You can create an exception and set your own traceback ' |
| 7381 | 'in\n' |
| 7382 | 'one step using the "with_traceback()" exception method (which ' |
| 7383 | 'returns\n' |
| 7384 | 'the same exception instance, with its traceback set to its ' |
| 7385 | 'argument),\n' |
| 7386 | 'like so:\n' |
| 7387 | '\n' |
| 7388 | ' raise Exception("foo occurred").with_traceback(tracebackobj)\n' |
| 7389 | '\n' |
| 7390 | 'The "from" clause is used for exception chaining: if given, the ' |
| 7391 | 'second\n' |
| 7392 | '*expression* must be another exception class or instance, which ' |
| 7393 | 'will\n' |
| 7394 | 'then be attached to the raised exception as the "__cause__" ' |
| 7395 | 'attribute\n' |
| 7396 | '(which is writable). If the raised exception is not handled, both\n' |
| 7397 | 'exceptions will be printed:\n' |
| 7398 | '\n' |
| 7399 | ' >>> try:\n' |
| 7400 | ' ... print(1 / 0)\n' |
| 7401 | ' ... except Exception as exc:\n' |
| 7402 | ' ... raise RuntimeError("Something bad happened") from exc\n' |
| 7403 | ' ...\n' |
| 7404 | ' Traceback (most recent call last):\n' |
| 7405 | ' File "<stdin>", line 2, in <module>\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7406 | ' ZeroDivisionError: division by zero\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7407 | '\n' |
| 7408 | ' The above exception was the direct cause of the following ' |
| 7409 | 'exception:\n' |
| 7410 | '\n' |
| 7411 | ' Traceback (most recent call last):\n' |
| 7412 | ' File "<stdin>", line 4, in <module>\n' |
| 7413 | ' RuntimeError: Something bad happened\n' |
| 7414 | '\n' |
| 7415 | 'A similar mechanism works implicitly if an exception is raised ' |
| 7416 | 'inside\n' |
| 7417 | 'an exception handler or a "finally" clause: the previous exception ' |
| 7418 | 'is\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7419 | 'then attached as the new exception’s "__context__" attribute:\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7420 | '\n' |
| 7421 | ' >>> try:\n' |
| 7422 | ' ... print(1 / 0)\n' |
| 7423 | ' ... except:\n' |
| 7424 | ' ... raise RuntimeError("Something bad happened")\n' |
| 7425 | ' ...\n' |
| 7426 | ' Traceback (most recent call last):\n' |
| 7427 | ' File "<stdin>", line 2, in <module>\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7428 | ' ZeroDivisionError: division by zero\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7429 | '\n' |
| 7430 | ' During handling of the above exception, another exception ' |
| 7431 | 'occurred:\n' |
| 7432 | '\n' |
| 7433 | ' Traceback (most recent call last):\n' |
| 7434 | ' File "<stdin>", line 4, in <module>\n' |
| 7435 | ' RuntimeError: Something bad happened\n' |
| 7436 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7437 | 'Exception chaining can be explicitly suppressed by specifying ' |
| 7438 | '"None"\n' |
| 7439 | 'in the "from" clause:\n' |
| 7440 | '\n' |
| 7441 | ' >>> try:\n' |
| 7442 | ' ... print(1 / 0)\n' |
| 7443 | ' ... except:\n' |
| 7444 | ' ... raise RuntimeError("Something bad happened") from None\n' |
| 7445 | ' ...\n' |
| 7446 | ' Traceback (most recent call last):\n' |
| 7447 | ' File "<stdin>", line 4, in <module>\n' |
| 7448 | ' RuntimeError: Something bad happened\n' |
| 7449 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7450 | 'Additional information on exceptions can be found in section\n' |
| 7451 | 'Exceptions, and information about handling exceptions is in ' |
| 7452 | 'section\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7453 | 'The try statement.\n' |
| 7454 | '\n' |
| 7455 | 'Changed in version 3.3: "None" is now permitted as "Y" in "raise X\n' |
| 7456 | 'from Y".\n' |
| 7457 | '\n' |
| 7458 | 'New in version 3.3: The "__suppress_context__" attribute to ' |
| 7459 | 'suppress\n' |
| 7460 | 'automatic display of the exception context.\n', |
| 7461 | 'return': 'The "return" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7462 | '**********************\n' |
| 7463 | '\n' |
| 7464 | ' return_stmt ::= "return" [expression_list]\n' |
| 7465 | '\n' |
| 7466 | '"return" may only occur syntactically nested in a function ' |
| 7467 | 'definition,\n' |
| 7468 | 'not within a nested class definition.\n' |
| 7469 | '\n' |
| 7470 | 'If an expression list is present, it is evaluated, else "None" is\n' |
| 7471 | 'substituted.\n' |
| 7472 | '\n' |
| 7473 | '"return" leaves the current function call with the expression list ' |
| 7474 | '(or\n' |
| 7475 | '"None") as return value.\n' |
| 7476 | '\n' |
| 7477 | 'When "return" passes control out of a "try" statement with a ' |
| 7478 | '"finally"\n' |
| 7479 | 'clause, that "finally" clause is executed before really leaving ' |
| 7480 | 'the\n' |
| 7481 | 'function.\n' |
| 7482 | '\n' |
| 7483 | 'In a generator function, the "return" statement indicates that ' |
| 7484 | 'the\n' |
| 7485 | 'generator is done and will cause "StopIteration" to be raised. ' |
| 7486 | 'The\n' |
| 7487 | 'returned value (if any) is used as an argument to construct\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7488 | '"StopIteration" and becomes the "StopIteration.value" attribute.\n' |
| 7489 | '\n' |
| 7490 | 'In an asynchronous generator function, an empty "return" ' |
| 7491 | 'statement\n' |
| 7492 | 'indicates that the asynchronous generator is done and will cause\n' |
| 7493 | '"StopAsyncIteration" to be raised. A non-empty "return" statement ' |
| 7494 | 'is\n' |
| 7495 | 'a syntax error in an asynchronous generator function.\n', |
| 7496 | 'sequence-types': 'Emulating container types\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7497 | '*************************\n' |
| 7498 | '\n' |
| 7499 | 'The following methods can be defined to implement ' |
| 7500 | 'container objects.\n' |
| 7501 | 'Containers usually are sequences (such as lists or tuples) ' |
| 7502 | 'or mappings\n' |
| 7503 | '(like dictionaries), but can represent other containers as ' |
| 7504 | 'well. The\n' |
| 7505 | 'first set of methods is used either to emulate a sequence ' |
| 7506 | 'or to\n' |
| 7507 | 'emulate a mapping; the difference is that for a sequence, ' |
| 7508 | 'the\n' |
| 7509 | 'allowable keys should be the integers *k* for which "0 <= ' |
| 7510 | 'k < N" where\n' |
| 7511 | '*N* is the length of the sequence, or slice objects, which ' |
| 7512 | 'define a\n' |
| 7513 | 'range of items. It is also recommended that mappings ' |
| 7514 | 'provide the\n' |
| 7515 | 'methods "keys()", "values()", "items()", "get()", ' |
| 7516 | '"clear()",\n' |
| 7517 | '"setdefault()", "pop()", "popitem()", "copy()", and ' |
| 7518 | '"update()"\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7519 | 'behaving similar to those for Python’s standard dictionary ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7520 | 'objects.\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7521 | 'The "collections.abc" module provides a "MutableMapping" ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7522 | 'abstract base\n' |
| 7523 | 'class to help create those methods from a base set of ' |
| 7524 | '"__getitem__()",\n' |
| 7525 | '"__setitem__()", "__delitem__()", and "keys()". Mutable ' |
| 7526 | 'sequences\n' |
| 7527 | 'should provide methods "append()", "count()", "index()", ' |
| 7528 | '"extend()",\n' |
| 7529 | '"insert()", "pop()", "remove()", "reverse()" and "sort()", ' |
| 7530 | 'like Python\n' |
| 7531 | 'standard list objects. Finally, sequence types should ' |
| 7532 | 'implement\n' |
| 7533 | 'addition (meaning concatenation) and multiplication ' |
| 7534 | '(meaning\n' |
| 7535 | 'repetition) by defining the methods "__add__()", ' |
| 7536 | '"__radd__()",\n' |
| 7537 | '"__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" ' |
| 7538 | 'described\n' |
| 7539 | 'below; they should not define other numerical operators. ' |
| 7540 | 'It is\n' |
| 7541 | 'recommended that both mappings and sequences implement ' |
| 7542 | 'the\n' |
| 7543 | '"__contains__()" method to allow efficient use of the "in" ' |
| 7544 | 'operator;\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7545 | 'for mappings, "in" should search the mapping’s keys; for ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7546 | 'sequences, it\n' |
| 7547 | 'should search through the values. It is further ' |
| 7548 | 'recommended that both\n' |
| 7549 | 'mappings and sequences implement the "__iter__()" method ' |
| 7550 | 'to allow\n' |
| 7551 | 'efficient iteration through the container; for mappings, ' |
| 7552 | '"__iter__()"\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 7553 | 'should iterate through the object’s keys; for sequences, ' |
| 7554 | 'it should\n' |
| 7555 | 'iterate through the values.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7556 | '\n' |
| 7557 | 'object.__len__(self)\n' |
| 7558 | '\n' |
| 7559 | ' Called to implement the built-in function "len()". ' |
| 7560 | 'Should return\n' |
| 7561 | ' the length of the object, an integer ">=" 0. Also, an ' |
| 7562 | 'object that\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7563 | ' doesn’t define a "__bool__()" method and whose ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7564 | '"__len__()" method\n' |
| 7565 | ' returns zero is considered to be false in a Boolean ' |
| 7566 | 'context.\n' |
| 7567 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7568 | ' **CPython implementation detail:** In CPython, the ' |
| 7569 | 'length is\n' |
| 7570 | ' required to be at most "sys.maxsize". If the length is ' |
| 7571 | 'larger than\n' |
| 7572 | ' "sys.maxsize" some features (such as "len()") may ' |
| 7573 | 'raise\n' |
| 7574 | ' "OverflowError". To prevent raising "OverflowError" by ' |
| 7575 | 'truth value\n' |
| 7576 | ' testing, an object must define a "__bool__()" method.\n' |
| 7577 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7578 | 'object.__length_hint__(self)\n' |
| 7579 | '\n' |
| 7580 | ' Called to implement "operator.length_hint()". Should ' |
| 7581 | 'return an\n' |
| 7582 | ' estimated length for the object (which may be greater ' |
| 7583 | 'or less than\n' |
| 7584 | ' the actual length). The length must be an integer ">=" ' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 7585 | '0. The\n' |
| 7586 | ' return value may also be "NotImplemented", which is ' |
| 7587 | 'treated the\n' |
| 7588 | ' same as if the "__length_hint__" method didn’t exist at ' |
| 7589 | 'all. This\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7590 | ' method is purely an optimization and is never required ' |
| 7591 | 'for\n' |
| 7592 | ' correctness.\n' |
| 7593 | '\n' |
| 7594 | ' New in version 3.4.\n' |
| 7595 | '\n' |
| 7596 | 'Note: Slicing is done exclusively with the following three ' |
| 7597 | 'methods.\n' |
| 7598 | ' A call like\n' |
| 7599 | '\n' |
| 7600 | ' a[1:2] = b\n' |
| 7601 | '\n' |
| 7602 | ' is translated to\n' |
| 7603 | '\n' |
| 7604 | ' a[slice(1, 2, None)] = b\n' |
| 7605 | '\n' |
| 7606 | ' and so forth. Missing slice items are always filled in ' |
| 7607 | 'with "None".\n' |
| 7608 | '\n' |
| 7609 | 'object.__getitem__(self, key)\n' |
| 7610 | '\n' |
| 7611 | ' Called to implement evaluation of "self[key]". For ' |
| 7612 | 'sequence types,\n' |
| 7613 | ' the accepted keys should be integers and slice ' |
| 7614 | 'objects. Note that\n' |
| 7615 | ' the special interpretation of negative indexes (if the ' |
| 7616 | 'class wishes\n' |
| 7617 | ' to emulate a sequence type) is up to the ' |
| 7618 | '"__getitem__()" method. If\n' |
| 7619 | ' *key* is of an inappropriate type, "TypeError" may be ' |
| 7620 | 'raised; if of\n' |
| 7621 | ' a value outside the set of indexes for the sequence ' |
| 7622 | '(after any\n' |
| 7623 | ' special interpretation of negative values), ' |
| 7624 | '"IndexError" should be\n' |
| 7625 | ' raised. For mapping types, if *key* is missing (not in ' |
| 7626 | 'the\n' |
| 7627 | ' container), "KeyError" should be raised.\n' |
| 7628 | '\n' |
| 7629 | ' Note: "for" loops expect that an "IndexError" will be ' |
| 7630 | 'raised for\n' |
| 7631 | ' illegal indexes to allow proper detection of the end ' |
| 7632 | 'of the\n' |
| 7633 | ' sequence.\n' |
| 7634 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7635 | 'object.__setitem__(self, key, value)\n' |
| 7636 | '\n' |
| 7637 | ' Called to implement assignment to "self[key]". Same ' |
| 7638 | 'note as for\n' |
| 7639 | ' "__getitem__()". This should only be implemented for ' |
| 7640 | 'mappings if\n' |
| 7641 | ' the objects support changes to the values for keys, or ' |
| 7642 | 'if new keys\n' |
| 7643 | ' can be added, or for sequences if elements can be ' |
| 7644 | 'replaced. The\n' |
| 7645 | ' same exceptions should be raised for improper *key* ' |
| 7646 | 'values as for\n' |
| 7647 | ' the "__getitem__()" method.\n' |
| 7648 | '\n' |
| 7649 | 'object.__delitem__(self, key)\n' |
| 7650 | '\n' |
| 7651 | ' Called to implement deletion of "self[key]". Same note ' |
| 7652 | 'as for\n' |
| 7653 | ' "__getitem__()". This should only be implemented for ' |
| 7654 | 'mappings if\n' |
| 7655 | ' the objects support removal of keys, or for sequences ' |
| 7656 | 'if elements\n' |
| 7657 | ' can be removed from the sequence. The same exceptions ' |
| 7658 | 'should be\n' |
| 7659 | ' raised for improper *key* values as for the ' |
| 7660 | '"__getitem__()" method.\n' |
| 7661 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7662 | 'object.__missing__(self, key)\n' |
| 7663 | '\n' |
| 7664 | ' Called by "dict"."__getitem__()" to implement ' |
| 7665 | '"self[key]" for dict\n' |
| 7666 | ' subclasses when key is not in the dictionary.\n' |
| 7667 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7668 | 'object.__iter__(self)\n' |
| 7669 | '\n' |
| 7670 | ' This method is called when an iterator is required for ' |
| 7671 | 'a container.\n' |
| 7672 | ' This method should return a new iterator object that ' |
| 7673 | 'can iterate\n' |
| 7674 | ' over all the objects in the container. For mappings, ' |
| 7675 | 'it should\n' |
| 7676 | ' iterate over the keys of the container.\n' |
| 7677 | '\n' |
| 7678 | ' Iterator objects also need to implement this method; ' |
| 7679 | 'they are\n' |
| 7680 | ' required to return themselves. For more information on ' |
| 7681 | 'iterator\n' |
| 7682 | ' objects, see Iterator Types.\n' |
| 7683 | '\n' |
| 7684 | 'object.__reversed__(self)\n' |
| 7685 | '\n' |
| 7686 | ' Called (if present) by the "reversed()" built-in to ' |
| 7687 | 'implement\n' |
| 7688 | ' reverse iteration. It should return a new iterator ' |
| 7689 | 'object that\n' |
| 7690 | ' iterates over all the objects in the container in ' |
| 7691 | 'reverse order.\n' |
| 7692 | '\n' |
| 7693 | ' If the "__reversed__()" method is not provided, the ' |
| 7694 | '"reversed()"\n' |
| 7695 | ' built-in will fall back to using the sequence protocol ' |
| 7696 | '("__len__()"\n' |
| 7697 | ' and "__getitem__()"). Objects that support the ' |
| 7698 | 'sequence protocol\n' |
| 7699 | ' should only provide "__reversed__()" if they can ' |
| 7700 | 'provide an\n' |
| 7701 | ' implementation that is more efficient than the one ' |
| 7702 | 'provided by\n' |
| 7703 | ' "reversed()".\n' |
| 7704 | '\n' |
| 7705 | 'The membership test operators ("in" and "not in") are ' |
| 7706 | 'normally\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 7707 | 'implemented as an iteration through a container. However, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7708 | 'container\n' |
| 7709 | 'objects can supply the following special method with a ' |
| 7710 | 'more efficient\n' |
| 7711 | 'implementation, which also does not require the object be ' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 7712 | 'iterable.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7713 | '\n' |
| 7714 | 'object.__contains__(self, item)\n' |
| 7715 | '\n' |
| 7716 | ' Called to implement membership test operators. Should ' |
| 7717 | 'return true\n' |
| 7718 | ' if *item* is in *self*, false otherwise. For mapping ' |
| 7719 | 'objects, this\n' |
| 7720 | ' should consider the keys of the mapping rather than the ' |
| 7721 | 'values or\n' |
| 7722 | ' the key-item pairs.\n' |
| 7723 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7724 | ' For objects that don’t define "__contains__()", the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7725 | 'membership test\n' |
| 7726 | ' first tries iteration via "__iter__()", then the old ' |
| 7727 | 'sequence\n' |
| 7728 | ' iteration protocol via "__getitem__()", see this ' |
| 7729 | 'section in the\n' |
| 7730 | ' language reference.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7731 | 'shifting': 'Shifting operations\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7732 | '*******************\n' |
| 7733 | '\n' |
| 7734 | 'The shifting operations have lower priority than the arithmetic\n' |
| 7735 | 'operations:\n' |
| 7736 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7737 | ' shift_expr ::= a_expr | shift_expr ("<<" | ">>") a_expr\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7738 | '\n' |
| 7739 | 'These operators accept integers as arguments. They shift the ' |
| 7740 | 'first\n' |
| 7741 | 'argument to the left or right by the number of bits given by ' |
| 7742 | 'the\n' |
| 7743 | 'second argument.\n' |
| 7744 | '\n' |
| 7745 | 'A right shift by *n* bits is defined as floor division by ' |
| 7746 | '"pow(2,n)".\n' |
| 7747 | 'A left shift by *n* bits is defined as multiplication with ' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7748 | '"pow(2,n)".\n', |
| 7749 | 'slicings': 'Slicings\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7750 | '********\n' |
| 7751 | '\n' |
| 7752 | 'A slicing selects a range of items in a sequence object (e.g., ' |
| 7753 | 'a\n' |
| 7754 | 'string, tuple or list). Slicings may be used as expressions or ' |
| 7755 | 'as\n' |
| 7756 | 'targets in assignment or "del" statements. The syntax for a ' |
| 7757 | 'slicing:\n' |
| 7758 | '\n' |
| 7759 | ' slicing ::= primary "[" slice_list "]"\n' |
| 7760 | ' slice_list ::= slice_item ("," slice_item)* [","]\n' |
| 7761 | ' slice_item ::= expression | proper_slice\n' |
| 7762 | ' proper_slice ::= [lower_bound] ":" [upper_bound] [ ":" ' |
| 7763 | '[stride] ]\n' |
| 7764 | ' lower_bound ::= expression\n' |
| 7765 | ' upper_bound ::= expression\n' |
| 7766 | ' stride ::= expression\n' |
| 7767 | '\n' |
| 7768 | 'There is ambiguity in the formal syntax here: anything that ' |
| 7769 | 'looks like\n' |
| 7770 | 'an expression list also looks like a slice list, so any ' |
| 7771 | 'subscription\n' |
| 7772 | 'can be interpreted as a slicing. Rather than further ' |
| 7773 | 'complicating the\n' |
| 7774 | 'syntax, this is disambiguated by defining that in this case the\n' |
| 7775 | 'interpretation as a subscription takes priority over the\n' |
| 7776 | 'interpretation as a slicing (this is the case if the slice list\n' |
| 7777 | 'contains no proper slice).\n' |
| 7778 | '\n' |
| 7779 | 'The semantics for a slicing are as follows. The primary is ' |
| 7780 | 'indexed\n' |
| 7781 | '(using the same "__getitem__()" method as normal subscription) ' |
| 7782 | 'with a\n' |
| 7783 | 'key that is constructed from the slice list, as follows. If the ' |
| 7784 | 'slice\n' |
| 7785 | 'list contains at least one comma, the key is a tuple containing ' |
| 7786 | 'the\n' |
| 7787 | 'conversion of the slice items; otherwise, the conversion of the ' |
| 7788 | 'lone\n' |
| 7789 | 'slice item is the key. The conversion of a slice item that is ' |
| 7790 | 'an\n' |
| 7791 | 'expression is that expression. The conversion of a proper slice ' |
| 7792 | 'is a\n' |
| 7793 | 'slice object (see section The standard type hierarchy) whose ' |
| 7794 | '"start",\n' |
| 7795 | '"stop" and "step" attributes are the values of the expressions ' |
| 7796 | 'given\n' |
| 7797 | 'as lower bound, upper bound and stride, respectively, ' |
| 7798 | 'substituting\n' |
| 7799 | '"None" for missing expressions.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7800 | 'specialattrs': 'Special Attributes\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7801 | '******************\n' |
| 7802 | '\n' |
| 7803 | 'The implementation adds a few special read-only attributes ' |
| 7804 | 'to several\n' |
| 7805 | 'object types, where they are relevant. Some of these are ' |
| 7806 | 'not reported\n' |
| 7807 | 'by the "dir()" built-in function.\n' |
| 7808 | '\n' |
| 7809 | 'object.__dict__\n' |
| 7810 | '\n' |
| 7811 | ' A dictionary or other mapping object used to store an ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7812 | 'object’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7813 | ' (writable) attributes.\n' |
| 7814 | '\n' |
| 7815 | 'instance.__class__\n' |
| 7816 | '\n' |
| 7817 | ' The class to which a class instance belongs.\n' |
| 7818 | '\n' |
| 7819 | 'class.__bases__\n' |
| 7820 | '\n' |
| 7821 | ' The tuple of base classes of a class object.\n' |
| 7822 | '\n' |
Ned Deily | aa843d2 | 2016-07-11 15:32:48 -0400 | [diff] [blame] | 7823 | 'definition.__name__\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7824 | '\n' |
Ned Deily | aa843d2 | 2016-07-11 15:32:48 -0400 | [diff] [blame] | 7825 | ' The name of the class, function, method, descriptor, or ' |
| 7826 | 'generator\n' |
| 7827 | ' instance.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7828 | '\n' |
Ned Deily | aa843d2 | 2016-07-11 15:32:48 -0400 | [diff] [blame] | 7829 | 'definition.__qualname__\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7830 | '\n' |
Ned Deily | aa843d2 | 2016-07-11 15:32:48 -0400 | [diff] [blame] | 7831 | ' The *qualified name* of the class, function, method, ' |
| 7832 | 'descriptor, or\n' |
| 7833 | ' generator instance.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7834 | '\n' |
| 7835 | ' New in version 3.3.\n' |
| 7836 | '\n' |
| 7837 | 'class.__mro__\n' |
| 7838 | '\n' |
| 7839 | ' This attribute is a tuple of classes that are considered ' |
| 7840 | 'when\n' |
| 7841 | ' looking for base classes during method resolution.\n' |
| 7842 | '\n' |
| 7843 | 'class.mro()\n' |
| 7844 | '\n' |
| 7845 | ' This method can be overridden by a metaclass to customize ' |
| 7846 | 'the\n' |
| 7847 | ' method resolution order for its instances. It is called ' |
| 7848 | 'at class\n' |
| 7849 | ' instantiation, and its result is stored in "__mro__".\n' |
| 7850 | '\n' |
| 7851 | 'class.__subclasses__()\n' |
| 7852 | '\n' |
| 7853 | ' Each class keeps a list of weak references to its ' |
| 7854 | 'immediate\n' |
| 7855 | ' subclasses. This method returns a list of all those ' |
| 7856 | 'references\n' |
| 7857 | ' still alive. Example:\n' |
| 7858 | '\n' |
| 7859 | ' >>> int.__subclasses__()\n' |
| 7860 | " [<class 'bool'>]\n" |
| 7861 | '\n' |
| 7862 | '-[ Footnotes ]-\n' |
| 7863 | '\n' |
| 7864 | '[1] Additional information on these special methods may be ' |
| 7865 | 'found\n' |
| 7866 | ' in the Python Reference Manual (Basic customization).\n' |
| 7867 | '\n' |
| 7868 | '[2] As a consequence, the list "[1, 2]" is considered equal ' |
| 7869 | 'to\n' |
| 7870 | ' "[1.0, 2.0]", and similarly for tuples.\n' |
| 7871 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7872 | '[3] They must have since the parser can’t tell the type of ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7873 | 'the\n' |
| 7874 | ' operands.\n' |
| 7875 | '\n' |
| 7876 | '[4] Cased characters are those with general category ' |
| 7877 | 'property\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7878 | ' being one of “Lu” (Letter, uppercase), “Ll” (Letter, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7879 | 'lowercase),\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7880 | ' or “Lt” (Letter, titlecase).\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7881 | '\n' |
| 7882 | '[5] To format only a tuple you should therefore provide a\n' |
| 7883 | ' singleton tuple whose only element is the tuple to be ' |
| 7884 | 'formatted.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7885 | 'specialnames': 'Special method names\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7886 | '********************\n' |
| 7887 | '\n' |
| 7888 | 'A class can implement certain operations that are invoked by ' |
| 7889 | 'special\n' |
| 7890 | 'syntax (such as arithmetic operations or subscripting and ' |
| 7891 | 'slicing) by\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7892 | 'defining methods with special names. This is Python’s ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7893 | 'approach to\n' |
| 7894 | '*operator overloading*, allowing classes to define their own ' |
| 7895 | 'behavior\n' |
| 7896 | 'with respect to language operators. For instance, if a ' |
| 7897 | 'class defines\n' |
| 7898 | 'a method named "__getitem__()", and "x" is an instance of ' |
| 7899 | 'this class,\n' |
| 7900 | 'then "x[i]" is roughly equivalent to "type(x).__getitem__(x, ' |
| 7901 | 'i)".\n' |
| 7902 | 'Except where mentioned, attempts to execute an operation ' |
| 7903 | 'raise an\n' |
| 7904 | 'exception when no appropriate method is defined (typically\n' |
| 7905 | '"AttributeError" or "TypeError").\n' |
| 7906 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 7907 | 'Setting a special method to "None" indicates that the ' |
| 7908 | 'corresponding\n' |
| 7909 | 'operation is not available. For example, if a class sets ' |
| 7910 | '"__iter__()"\n' |
| 7911 | 'to "None", the class is not iterable, so calling "iter()" on ' |
| 7912 | 'its\n' |
| 7913 | 'instances will raise a "TypeError" (without falling back to\n' |
| 7914 | '"__getitem__()"). [2]\n' |
| 7915 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7916 | 'When implementing a class that emulates any built-in type, ' |
| 7917 | 'it is\n' |
| 7918 | 'important that the emulation only be implemented to the ' |
| 7919 | 'degree that it\n' |
| 7920 | 'makes sense for the object being modelled. For example, ' |
| 7921 | 'some\n' |
| 7922 | 'sequences may work well with retrieval of individual ' |
| 7923 | 'elements, but\n' |
| 7924 | 'extracting a slice may not make sense. (One example of this ' |
| 7925 | 'is the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7926 | '"NodeList" interface in the W3C’s Document Object Model.)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7927 | '\n' |
| 7928 | '\n' |
| 7929 | 'Basic customization\n' |
| 7930 | '===================\n' |
| 7931 | '\n' |
| 7932 | 'object.__new__(cls[, ...])\n' |
| 7933 | '\n' |
| 7934 | ' Called to create a new instance of class *cls*. ' |
| 7935 | '"__new__()" is a\n' |
| 7936 | ' static method (special-cased so you need not declare it ' |
| 7937 | 'as such)\n' |
| 7938 | ' that takes the class of which an instance was requested ' |
| 7939 | 'as its\n' |
| 7940 | ' first argument. The remaining arguments are those passed ' |
| 7941 | 'to the\n' |
| 7942 | ' object constructor expression (the call to the class). ' |
| 7943 | 'The return\n' |
| 7944 | ' value of "__new__()" should be the new object instance ' |
| 7945 | '(usually an\n' |
| 7946 | ' instance of *cls*).\n' |
| 7947 | '\n' |
| 7948 | ' Typical implementations create a new instance of the ' |
| 7949 | 'class by\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7950 | ' invoking the superclass’s "__new__()" method using\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7951 | ' "super().__new__(cls[, ...])" with appropriate arguments ' |
| 7952 | 'and then\n' |
| 7953 | ' modifying the newly-created instance as necessary before ' |
| 7954 | 'returning\n' |
| 7955 | ' it.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7956 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 7957 | ' If "__new__()" is invoked during object construction and ' |
| 7958 | 'it returns\n' |
| 7959 | ' an instance or subclass of *cls*, then the new ' |
| 7960 | 'instance’s\n' |
| 7961 | ' "__init__()" method will be invoked like "__init__(self[, ' |
| 7962 | '...])",\n' |
| 7963 | ' where *self* is the new instance and the remaining ' |
| 7964 | 'arguments are\n' |
| 7965 | ' the same as were passed to the object constructor.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7966 | '\n' |
| 7967 | ' If "__new__()" does not return an instance of *cls*, then ' |
| 7968 | 'the new\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7969 | ' instance’s "__init__()" method will not be invoked.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7970 | '\n' |
| 7971 | ' "__new__()" is intended mainly to allow subclasses of ' |
| 7972 | 'immutable\n' |
| 7973 | ' types (like int, str, or tuple) to customize instance ' |
| 7974 | 'creation. It\n' |
| 7975 | ' is also commonly overridden in custom metaclasses in ' |
| 7976 | 'order to\n' |
| 7977 | ' customize class creation.\n' |
| 7978 | '\n' |
| 7979 | 'object.__init__(self[, ...])\n' |
| 7980 | '\n' |
| 7981 | ' Called after the instance has been created (by ' |
| 7982 | '"__new__()"), but\n' |
| 7983 | ' before it is returned to the caller. The arguments are ' |
| 7984 | 'those\n' |
| 7985 | ' passed to the class constructor expression. If a base ' |
| 7986 | 'class has an\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 7987 | ' "__init__()" method, the derived class’s "__init__()" ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7988 | 'method, if\n' |
| 7989 | ' any, must explicitly call it to ensure proper ' |
| 7990 | 'initialization of the\n' |
| 7991 | ' base class part of the instance; for example:\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 7992 | ' "super().__init__([args...])".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7993 | '\n' |
| 7994 | ' Because "__new__()" and "__init__()" work together in ' |
| 7995 | 'constructing\n' |
| 7996 | ' objects ("__new__()" to create it, and "__init__()" to ' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 7997 | 'customize\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 7998 | ' it), no non-"None" value may be returned by "__init__()"; ' |
| 7999 | 'doing so\n' |
| 8000 | ' will cause a "TypeError" to be raised at runtime.\n' |
| 8001 | '\n' |
| 8002 | 'object.__del__(self)\n' |
| 8003 | '\n' |
| 8004 | ' Called when the instance is about to be destroyed. This ' |
| 8005 | 'is also\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8006 | ' called a finalizer or (improperly) a destructor. If a ' |
| 8007 | 'base class\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8008 | ' has a "__del__()" method, the derived class’s "__del__()" ' |
| 8009 | 'method,\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8010 | ' if any, must explicitly call it to ensure proper deletion ' |
| 8011 | 'of the\n' |
| 8012 | ' base class part of the instance.\n' |
| 8013 | '\n' |
| 8014 | ' It is possible (though not recommended!) for the ' |
| 8015 | '"__del__()" method\n' |
| 8016 | ' to postpone destruction of the instance by creating a new ' |
| 8017 | 'reference\n' |
| 8018 | ' to it. This is called object *resurrection*. It is\n' |
| 8019 | ' implementation-dependent whether "__del__()" is called a ' |
| 8020 | 'second\n' |
| 8021 | ' time when a resurrected object is about to be destroyed; ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8022 | 'the\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8023 | ' current *CPython* implementation only calls it once.\n' |
| 8024 | '\n' |
| 8025 | ' It is not guaranteed that "__del__()" methods are called ' |
| 8026 | 'for\n' |
| 8027 | ' objects that still exist when the interpreter exits.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8028 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8029 | ' Note: "del x" doesn’t directly call "x.__del__()" — the ' |
| 8030 | 'former\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8031 | ' decrements the reference count for "x" by one, and the ' |
| 8032 | 'latter is\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8033 | ' only called when "x"’s reference count reaches zero.\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8034 | '\n' |
| 8035 | ' **CPython implementation detail:** It is possible for a ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8036 | 'reference\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8037 | ' cycle to prevent the reference count of an object from ' |
| 8038 | 'going to\n' |
| 8039 | ' zero. In this case, the cycle will be later detected and ' |
| 8040 | 'deleted\n' |
| 8041 | ' by the *cyclic garbage collector*. A common cause of ' |
| 8042 | 'reference\n' |
| 8043 | ' cycles is when an exception has been caught in a local ' |
| 8044 | 'variable.\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8045 | ' The frame’s locals then reference the exception, which ' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8046 | 'references\n' |
| 8047 | ' its own traceback, which references the locals of all ' |
| 8048 | 'frames caught\n' |
| 8049 | ' in the traceback.\n' |
| 8050 | '\n' |
| 8051 | ' See also: Documentation for the "gc" module.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8052 | '\n' |
| 8053 | ' Warning: Due to the precarious circumstances under which\n' |
| 8054 | ' "__del__()" methods are invoked, exceptions that occur ' |
| 8055 | 'during\n' |
| 8056 | ' their execution are ignored, and a warning is printed ' |
| 8057 | 'to\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8058 | ' "sys.stderr" instead. In particular:\n' |
| 8059 | '\n' |
| 8060 | ' * "__del__()" can be invoked when arbitrary code is ' |
| 8061 | 'being\n' |
| 8062 | ' executed, including from any arbitrary thread. If ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8063 | '"__del__()"\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8064 | ' needs to take a lock or invoke any other blocking ' |
| 8065 | 'resource, it\n' |
| 8066 | ' may deadlock as the resource may already be taken by ' |
| 8067 | 'the code\n' |
| 8068 | ' that gets interrupted to execute "__del__()".\n' |
| 8069 | '\n' |
| 8070 | ' * "__del__()" can be executed during interpreter ' |
| 8071 | 'shutdown. As\n' |
| 8072 | ' a consequence, the global variables it needs to ' |
| 8073 | 'access\n' |
| 8074 | ' (including other modules) may already have been ' |
| 8075 | 'deleted or set\n' |
| 8076 | ' to "None". Python guarantees that globals whose name ' |
| 8077 | 'begins\n' |
| 8078 | ' with a single underscore are deleted from their ' |
| 8079 | 'module before\n' |
| 8080 | ' other globals are deleted; if no other references to ' |
| 8081 | 'such\n' |
| 8082 | ' globals exist, this may help in assuring that ' |
| 8083 | 'imported modules\n' |
| 8084 | ' are still available at the time when the "__del__()" ' |
| 8085 | 'method is\n' |
| 8086 | ' called.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8087 | '\n' |
| 8088 | 'object.__repr__(self)\n' |
| 8089 | '\n' |
| 8090 | ' Called by the "repr()" built-in function to compute the ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8091 | '“official”\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8092 | ' string representation of an object. If at all possible, ' |
| 8093 | 'this\n' |
| 8094 | ' should look like a valid Python expression that could be ' |
| 8095 | 'used to\n' |
| 8096 | ' recreate an object with the same value (given an ' |
| 8097 | 'appropriate\n' |
| 8098 | ' environment). If this is not possible, a string of the ' |
| 8099 | 'form\n' |
| 8100 | ' "<...some useful description...>" should be returned. The ' |
| 8101 | 'return\n' |
| 8102 | ' value must be a string object. If a class defines ' |
| 8103 | '"__repr__()" but\n' |
| 8104 | ' not "__str__()", then "__repr__()" is also used when an ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8105 | '“informal”\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8106 | ' string representation of instances of that class is ' |
| 8107 | 'required.\n' |
| 8108 | '\n' |
| 8109 | ' This is typically used for debugging, so it is important ' |
| 8110 | 'that the\n' |
| 8111 | ' representation is information-rich and unambiguous.\n' |
| 8112 | '\n' |
| 8113 | 'object.__str__(self)\n' |
| 8114 | '\n' |
| 8115 | ' Called by "str(object)" and the built-in functions ' |
| 8116 | '"format()" and\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8117 | ' "print()" to compute the “informal” or nicely printable ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8118 | 'string\n' |
| 8119 | ' representation of an object. The return value must be a ' |
| 8120 | 'string\n' |
| 8121 | ' object.\n' |
| 8122 | '\n' |
| 8123 | ' This method differs from "object.__repr__()" in that ' |
| 8124 | 'there is no\n' |
| 8125 | ' expectation that "__str__()" return a valid Python ' |
| 8126 | 'expression: a\n' |
| 8127 | ' more convenient or concise representation can be used.\n' |
| 8128 | '\n' |
| 8129 | ' The default implementation defined by the built-in type ' |
| 8130 | '"object"\n' |
| 8131 | ' calls "object.__repr__()".\n' |
| 8132 | '\n' |
| 8133 | 'object.__bytes__(self)\n' |
| 8134 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 8135 | ' Called by bytes to compute a byte-string representation ' |
| 8136 | 'of an\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8137 | ' object. This should return a "bytes" object.\n' |
| 8138 | '\n' |
| 8139 | 'object.__format__(self, format_spec)\n' |
| 8140 | '\n' |
| 8141 | ' Called by the "format()" built-in function, and by ' |
| 8142 | 'extension,\n' |
| 8143 | ' evaluation of formatted string literals and the ' |
| 8144 | '"str.format()"\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8145 | ' method, to produce a “formatted” string representation of ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8146 | 'an\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 8147 | ' object. The *format_spec* argument is a string that ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8148 | 'contains a\n' |
| 8149 | ' description of the formatting options desired. The ' |
| 8150 | 'interpretation\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 8151 | ' of the *format_spec* argument is up to the type ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8152 | 'implementing\n' |
| 8153 | ' "__format__()", however most classes will either ' |
| 8154 | 'delegate\n' |
| 8155 | ' formatting to one of the built-in types, or use a ' |
| 8156 | 'similar\n' |
| 8157 | ' formatting option syntax.\n' |
| 8158 | '\n' |
| 8159 | ' See Format Specification Mini-Language for a description ' |
| 8160 | 'of the\n' |
| 8161 | ' standard formatting syntax.\n' |
| 8162 | '\n' |
| 8163 | ' The return value must be a string object.\n' |
| 8164 | '\n' |
| 8165 | ' Changed in version 3.4: The __format__ method of "object" ' |
| 8166 | 'itself\n' |
| 8167 | ' raises a "TypeError" if passed any non-empty string.\n' |
| 8168 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 8169 | ' Changed in version 3.7: "object.__format__(x, \'\')" is ' |
| 8170 | 'now\n' |
Łukasz Langa | bc1c8af | 2020-04-27 22:44:04 +0200 | [diff] [blame] | 8171 | ' equivalent to "str(x)" rather than "format(str(x), ' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 8172 | '\'\')".\n' |
| 8173 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8174 | 'object.__lt__(self, other)\n' |
| 8175 | 'object.__le__(self, other)\n' |
| 8176 | 'object.__eq__(self, other)\n' |
| 8177 | 'object.__ne__(self, other)\n' |
| 8178 | 'object.__gt__(self, other)\n' |
| 8179 | 'object.__ge__(self, other)\n' |
| 8180 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8181 | ' These are the so-called “rich comparison” methods. The\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8182 | ' correspondence between operator symbols and method names ' |
| 8183 | 'is as\n' |
| 8184 | ' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls ' |
| 8185 | '"x.__le__(y)",\n' |
| 8186 | ' "x==y" calls "x.__eq__(y)", "x!=y" calls "x.__ne__(y)", ' |
| 8187 | '"x>y" calls\n' |
| 8188 | ' "x.__gt__(y)", and "x>=y" calls "x.__ge__(y)".\n' |
| 8189 | '\n' |
| 8190 | ' A rich comparison method may return the singleton ' |
| 8191 | '"NotImplemented"\n' |
| 8192 | ' if it does not implement the operation for a given pair ' |
| 8193 | 'of\n' |
| 8194 | ' arguments. By convention, "False" and "True" are returned ' |
| 8195 | 'for a\n' |
| 8196 | ' successful comparison. However, these methods can return ' |
| 8197 | 'any value,\n' |
| 8198 | ' so if the comparison operator is used in a Boolean ' |
| 8199 | 'context (e.g.,\n' |
| 8200 | ' in the condition of an "if" statement), Python will call ' |
| 8201 | '"bool()"\n' |
| 8202 | ' on the value to determine if the result is true or ' |
| 8203 | 'false.\n' |
| 8204 | '\n' |
| 8205 | ' By default, "__ne__()" delegates to "__eq__()" and ' |
| 8206 | 'inverts the\n' |
| 8207 | ' result unless it is "NotImplemented". There are no other ' |
| 8208 | 'implied\n' |
| 8209 | ' relationships among the comparison operators, for ' |
| 8210 | 'example, the\n' |
| 8211 | ' truth of "(x<y or x==y)" does not imply "x<=y". To ' |
| 8212 | 'automatically\n' |
| 8213 | ' generate ordering operations from a single root ' |
| 8214 | 'operation, see\n' |
| 8215 | ' "functools.total_ordering()".\n' |
| 8216 | '\n' |
| 8217 | ' See the paragraph on "__hash__()" for some important ' |
| 8218 | 'notes on\n' |
| 8219 | ' creating *hashable* objects which support custom ' |
| 8220 | 'comparison\n' |
| 8221 | ' operations and are usable as dictionary keys.\n' |
| 8222 | '\n' |
| 8223 | ' There are no swapped-argument versions of these methods ' |
| 8224 | '(to be used\n' |
| 8225 | ' when the left argument does not support the operation but ' |
| 8226 | 'the right\n' |
| 8227 | ' argument does); rather, "__lt__()" and "__gt__()" are ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8228 | 'each other’s\n' |
| 8229 | ' reflection, "__le__()" and "__ge__()" are each other’s ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8230 | 'reflection,\n' |
| 8231 | ' and "__eq__()" and "__ne__()" are their own reflection. ' |
| 8232 | 'If the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8233 | ' operands are of different types, and right operand’s type ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8234 | 'is a\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8235 | ' direct or indirect subclass of the left operand’s type, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8236 | 'the\n' |
| 8237 | ' reflected method of the right operand has priority, ' |
| 8238 | 'otherwise the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8239 | ' left operand’s method has priority. Virtual subclassing ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8240 | 'is not\n' |
| 8241 | ' considered.\n' |
| 8242 | '\n' |
| 8243 | 'object.__hash__(self)\n' |
| 8244 | '\n' |
| 8245 | ' Called by built-in function "hash()" and for operations ' |
| 8246 | 'on members\n' |
| 8247 | ' of hashed collections including "set", "frozenset", and ' |
| 8248 | '"dict".\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 8249 | ' "__hash__()" should return an integer. The only required ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8250 | 'property\n' |
| 8251 | ' is that objects which compare equal have the same hash ' |
| 8252 | 'value; it is\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 8253 | ' advised to mix together the hash values of the components ' |
| 8254 | 'of the\n' |
| 8255 | ' object that also play a part in comparison of objects by ' |
| 8256 | 'packing\n' |
| 8257 | ' them into a tuple and hashing the tuple. Example:\n' |
| 8258 | '\n' |
| 8259 | ' def __hash__(self):\n' |
| 8260 | ' return hash((self.name, self.nick, self.color))\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8261 | '\n' |
| 8262 | ' Note: "hash()" truncates the value returned from an ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8263 | 'object’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8264 | ' custom "__hash__()" method to the size of a ' |
| 8265 | '"Py_ssize_t". This\n' |
| 8266 | ' is typically 8 bytes on 64-bit builds and 4 bytes on ' |
| 8267 | '32-bit\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8268 | ' builds. If an object’s "__hash__()" must interoperate ' |
| 8269 | 'on builds\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8270 | ' of different bit sizes, be sure to check the width on ' |
| 8271 | 'all\n' |
| 8272 | ' supported builds. An easy way to do this is with ' |
| 8273 | '"python -c\n' |
| 8274 | ' "import sys; print(sys.hash_info.width)"".\n' |
| 8275 | '\n' |
| 8276 | ' If a class does not define an "__eq__()" method it should ' |
| 8277 | 'not\n' |
| 8278 | ' define a "__hash__()" operation either; if it defines ' |
| 8279 | '"__eq__()"\n' |
| 8280 | ' but not "__hash__()", its instances will not be usable as ' |
| 8281 | 'items in\n' |
| 8282 | ' hashable collections. If a class defines mutable objects ' |
| 8283 | 'and\n' |
| 8284 | ' implements an "__eq__()" method, it should not implement\n' |
| 8285 | ' "__hash__()", since the implementation of hashable ' |
| 8286 | 'collections\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8287 | ' requires that a key’s hash value is immutable (if the ' |
| 8288 | 'object’s hash\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8289 | ' value changes, it will be in the wrong hash bucket).\n' |
| 8290 | '\n' |
| 8291 | ' User-defined classes have "__eq__()" and "__hash__()" ' |
| 8292 | 'methods by\n' |
| 8293 | ' default; with them, all objects compare unequal (except ' |
| 8294 | 'with\n' |
| 8295 | ' themselves) and "x.__hash__()" returns an appropriate ' |
| 8296 | 'value such\n' |
| 8297 | ' that "x == y" implies both that "x is y" and "hash(x) == ' |
| 8298 | 'hash(y)".\n' |
| 8299 | '\n' |
| 8300 | ' A class that overrides "__eq__()" and does not define ' |
| 8301 | '"__hash__()"\n' |
| 8302 | ' will have its "__hash__()" implicitly set to "None". ' |
| 8303 | 'When the\n' |
| 8304 | ' "__hash__()" method of a class is "None", instances of ' |
| 8305 | 'the class\n' |
| 8306 | ' will raise an appropriate "TypeError" when a program ' |
| 8307 | 'attempts to\n' |
| 8308 | ' retrieve their hash value, and will also be correctly ' |
| 8309 | 'identified as\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 8310 | ' unhashable when checking "isinstance(obj,\n' |
| 8311 | ' collections.abc.Hashable)".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8312 | '\n' |
| 8313 | ' If a class that overrides "__eq__()" needs to retain the\n' |
| 8314 | ' implementation of "__hash__()" from a parent class, the ' |
| 8315 | 'interpreter\n' |
| 8316 | ' must be told this explicitly by setting "__hash__ =\n' |
| 8317 | ' <ParentClass>.__hash__".\n' |
| 8318 | '\n' |
| 8319 | ' If a class that does not override "__eq__()" wishes to ' |
| 8320 | 'suppress\n' |
| 8321 | ' hash support, it should include "__hash__ = None" in the ' |
| 8322 | 'class\n' |
| 8323 | ' definition. A class which defines its own "__hash__()" ' |
| 8324 | 'that\n' |
| 8325 | ' explicitly raises a "TypeError" would be incorrectly ' |
| 8326 | 'identified as\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 8327 | ' hashable by an "isinstance(obj, ' |
| 8328 | 'collections.abc.Hashable)" call.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8329 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 8330 | ' Note: By default, the "__hash__()" values of str and ' |
| 8331 | 'bytes\n' |
| 8332 | ' objects are “salted” with an unpredictable random ' |
| 8333 | 'value.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8334 | ' Although they remain constant within an individual ' |
| 8335 | 'Python\n' |
| 8336 | ' process, they are not predictable between repeated ' |
| 8337 | 'invocations of\n' |
| 8338 | ' Python.This is intended to provide protection against a ' |
| 8339 | 'denial-\n' |
| 8340 | ' of-service caused by carefully-chosen inputs that ' |
| 8341 | 'exploit the\n' |
| 8342 | ' worst case performance of a dict insertion, O(n^2) ' |
| 8343 | 'complexity.\n' |
| 8344 | ' See http://www.ocert.org/advisories/ocert-2011-003.html ' |
| 8345 | 'for\n' |
| 8346 | ' details.Changing hash values affects the iteration ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8347 | 'order of sets.\n' |
| 8348 | ' Python has never made guarantees about this ordering ' |
| 8349 | '(and it\n' |
| 8350 | ' typically varies between 32-bit and 64-bit builds).See ' |
| 8351 | 'also\n' |
| 8352 | ' "PYTHONHASHSEED".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8353 | '\n' |
| 8354 | ' Changed in version 3.3: Hash randomization is enabled by ' |
| 8355 | 'default.\n' |
| 8356 | '\n' |
| 8357 | 'object.__bool__(self)\n' |
| 8358 | '\n' |
| 8359 | ' Called to implement truth value testing and the built-in ' |
| 8360 | 'operation\n' |
| 8361 | ' "bool()"; should return "False" or "True". When this ' |
| 8362 | 'method is not\n' |
| 8363 | ' defined, "__len__()" is called, if it is defined, and the ' |
| 8364 | 'object is\n' |
| 8365 | ' considered true if its result is nonzero. If a class ' |
| 8366 | 'defines\n' |
| 8367 | ' neither "__len__()" nor "__bool__()", all its instances ' |
| 8368 | 'are\n' |
| 8369 | ' considered true.\n' |
| 8370 | '\n' |
| 8371 | '\n' |
| 8372 | 'Customizing attribute access\n' |
| 8373 | '============================\n' |
| 8374 | '\n' |
| 8375 | 'The following methods can be defined to customize the ' |
| 8376 | 'meaning of\n' |
| 8377 | 'attribute access (use of, assignment to, or deletion of ' |
| 8378 | '"x.name") for\n' |
| 8379 | 'class instances.\n' |
| 8380 | '\n' |
| 8381 | 'object.__getattr__(self, name)\n' |
| 8382 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8383 | ' Called when the default attribute access fails with an\n' |
| 8384 | ' "AttributeError" (either "__getattribute__()" raises an\n' |
| 8385 | ' "AttributeError" because *name* is not an instance ' |
| 8386 | 'attribute or an\n' |
| 8387 | ' attribute in the class tree for "self"; or "__get__()" of ' |
| 8388 | 'a *name*\n' |
| 8389 | ' property raises "AttributeError"). This method should ' |
| 8390 | 'either\n' |
| 8391 | ' return the (computed) attribute value or raise an ' |
| 8392 | '"AttributeError"\n' |
| 8393 | ' exception.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8394 | '\n' |
| 8395 | ' Note that if the attribute is found through the normal ' |
| 8396 | 'mechanism,\n' |
| 8397 | ' "__getattr__()" is not called. (This is an intentional ' |
| 8398 | 'asymmetry\n' |
| 8399 | ' between "__getattr__()" and "__setattr__()".) This is ' |
| 8400 | 'done both for\n' |
| 8401 | ' efficiency reasons and because otherwise "__getattr__()" ' |
| 8402 | 'would have\n' |
| 8403 | ' no way to access other attributes of the instance. Note ' |
| 8404 | 'that at\n' |
| 8405 | ' least for instance variables, you can fake total control ' |
| 8406 | 'by not\n' |
| 8407 | ' inserting any values in the instance attribute dictionary ' |
| 8408 | '(but\n' |
| 8409 | ' instead inserting them in another object). See the\n' |
| 8410 | ' "__getattribute__()" method below for a way to actually ' |
| 8411 | 'get total\n' |
| 8412 | ' control over attribute access.\n' |
| 8413 | '\n' |
| 8414 | 'object.__getattribute__(self, name)\n' |
| 8415 | '\n' |
| 8416 | ' Called unconditionally to implement attribute accesses ' |
| 8417 | 'for\n' |
| 8418 | ' instances of the class. If the class also defines ' |
| 8419 | '"__getattr__()",\n' |
| 8420 | ' the latter will not be called unless "__getattribute__()" ' |
| 8421 | 'either\n' |
| 8422 | ' calls it explicitly or raises an "AttributeError". This ' |
| 8423 | 'method\n' |
| 8424 | ' should return the (computed) attribute value or raise an\n' |
| 8425 | ' "AttributeError" exception. In order to avoid infinite ' |
| 8426 | 'recursion in\n' |
| 8427 | ' this method, its implementation should always call the ' |
| 8428 | 'base class\n' |
| 8429 | ' method with the same name to access any attributes it ' |
| 8430 | 'needs, for\n' |
| 8431 | ' example, "object.__getattribute__(self, name)".\n' |
| 8432 | '\n' |
| 8433 | ' Note: This method may still be bypassed when looking up ' |
| 8434 | 'special\n' |
| 8435 | ' methods as the result of implicit invocation via ' |
| 8436 | 'language syntax\n' |
| 8437 | ' or built-in functions. See Special method lookup.\n' |
| 8438 | '\n' |
| 8439 | 'object.__setattr__(self, name, value)\n' |
| 8440 | '\n' |
| 8441 | ' Called when an attribute assignment is attempted. This ' |
| 8442 | 'is called\n' |
| 8443 | ' instead of the normal mechanism (i.e. store the value in ' |
| 8444 | 'the\n' |
| 8445 | ' instance dictionary). *name* is the attribute name, ' |
| 8446 | '*value* is the\n' |
| 8447 | ' value to be assigned to it.\n' |
| 8448 | '\n' |
| 8449 | ' If "__setattr__()" wants to assign to an instance ' |
| 8450 | 'attribute, it\n' |
| 8451 | ' should call the base class method with the same name, for ' |
| 8452 | 'example,\n' |
| 8453 | ' "object.__setattr__(self, name, value)".\n' |
| 8454 | '\n' |
| 8455 | 'object.__delattr__(self, name)\n' |
| 8456 | '\n' |
| 8457 | ' Like "__setattr__()" but for attribute deletion instead ' |
| 8458 | 'of\n' |
| 8459 | ' assignment. This should only be implemented if "del ' |
| 8460 | 'obj.name" is\n' |
| 8461 | ' meaningful for the object.\n' |
| 8462 | '\n' |
| 8463 | 'object.__dir__(self)\n' |
| 8464 | '\n' |
| 8465 | ' Called when "dir()" is called on the object. A sequence ' |
| 8466 | 'must be\n' |
| 8467 | ' returned. "dir()" converts the returned sequence to a ' |
| 8468 | 'list and\n' |
| 8469 | ' sorts it.\n' |
| 8470 | '\n' |
| 8471 | '\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8472 | 'Customizing module attribute access\n' |
| 8473 | '-----------------------------------\n' |
| 8474 | '\n' |
| 8475 | 'Special names "__getattr__" and "__dir__" can be also used ' |
| 8476 | 'to\n' |
| 8477 | 'customize access to module attributes. The "__getattr__" ' |
| 8478 | 'function at\n' |
| 8479 | 'the module level should accept one argument which is the ' |
| 8480 | 'name of an\n' |
| 8481 | 'attribute and return the computed value or raise an ' |
| 8482 | '"AttributeError".\n' |
| 8483 | 'If an attribute is not found on a module object through the ' |
| 8484 | 'normal\n' |
| 8485 | 'lookup, i.e. "object.__getattribute__()", then "__getattr__" ' |
| 8486 | 'is\n' |
| 8487 | 'searched in the module "__dict__" before raising an ' |
| 8488 | '"AttributeError".\n' |
| 8489 | 'If found, it is called with the attribute name and the ' |
| 8490 | 'result is\n' |
| 8491 | 'returned.\n' |
| 8492 | '\n' |
| 8493 | 'The "__dir__" function should accept no arguments, and ' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 8494 | 'return a\n' |
| 8495 | 'sequence of strings that represents the names accessible on ' |
| 8496 | 'module. If\n' |
| 8497 | 'present, this function overrides the standard "dir()" search ' |
| 8498 | 'on a\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8499 | 'module.\n' |
| 8500 | '\n' |
| 8501 | 'For a more fine grained customization of the module behavior ' |
| 8502 | '(setting\n' |
| 8503 | 'attributes, properties, etc.), one can set the "__class__" ' |
| 8504 | 'attribute\n' |
| 8505 | 'of a module object to a subclass of "types.ModuleType". For ' |
| 8506 | 'example:\n' |
| 8507 | '\n' |
| 8508 | ' import sys\n' |
| 8509 | ' from types import ModuleType\n' |
| 8510 | '\n' |
| 8511 | ' class VerboseModule(ModuleType):\n' |
| 8512 | ' def __repr__(self):\n' |
| 8513 | " return f'Verbose {self.__name__}'\n" |
| 8514 | '\n' |
| 8515 | ' def __setattr__(self, attr, value):\n' |
| 8516 | " print(f'Setting {attr}...')\n" |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8517 | ' super().__setattr__(attr, value)\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8518 | '\n' |
| 8519 | ' sys.modules[__name__].__class__ = VerboseModule\n' |
| 8520 | '\n' |
| 8521 | 'Note: Defining module "__getattr__" and setting module ' |
| 8522 | '"__class__"\n' |
| 8523 | ' only affect lookups made using the attribute access syntax ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8524 | '–\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8525 | ' directly accessing the module globals (whether by code ' |
| 8526 | 'within the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8527 | ' module, or via a reference to the module’s globals ' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8528 | 'dictionary) is\n' |
| 8529 | ' unaffected.\n' |
| 8530 | '\n' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 8531 | 'Changed in version 3.5: "__class__" module attribute is now ' |
| 8532 | 'writable.\n' |
| 8533 | '\n' |
| 8534 | 'New in version 3.7: "__getattr__" and "__dir__" module ' |
| 8535 | 'attributes.\n' |
| 8536 | '\n' |
| 8537 | 'See also:\n' |
| 8538 | '\n' |
| 8539 | ' **PEP 562** - Module __getattr__ and __dir__\n' |
| 8540 | ' Describes the "__getattr__" and "__dir__" functions on ' |
| 8541 | 'modules.\n' |
| 8542 | '\n' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 8543 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8544 | 'Implementing Descriptors\n' |
| 8545 | '------------------------\n' |
| 8546 | '\n' |
| 8547 | 'The following methods only apply when an instance of the ' |
| 8548 | 'class\n' |
| 8549 | 'containing the method (a so-called *descriptor* class) ' |
| 8550 | 'appears in an\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8551 | '*owner* class (the descriptor must be in either the owner’s ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8552 | 'class\n' |
| 8553 | 'dictionary or in the class dictionary for one of its ' |
| 8554 | 'parents). In the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8555 | 'examples below, “the attribute” refers to the attribute ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8556 | 'whose name is\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8557 | 'the key of the property in the owner class’ "__dict__".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8558 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 8559 | 'object.__get__(self, instance, owner=None)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8560 | '\n' |
| 8561 | ' Called to get the attribute of the owner class (class ' |
| 8562 | 'attribute\n' |
| 8563 | ' access) or of an instance of that class (instance ' |
| 8564 | 'attribute\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 8565 | ' access). The optional *owner* argument is the owner ' |
| 8566 | 'class, while\n' |
| 8567 | ' *instance* is the instance that the attribute was ' |
| 8568 | 'accessed through,\n' |
| 8569 | ' or "None" when the attribute is accessed through the ' |
| 8570 | '*owner*.\n' |
| 8571 | '\n' |
| 8572 | ' This method should return the computed attribute value or ' |
| 8573 | 'raise an\n' |
| 8574 | ' "AttributeError" exception.\n' |
| 8575 | '\n' |
| 8576 | ' **PEP 252** specifies that "__get__()" is callable with ' |
| 8577 | 'one or two\n' |
| 8578 | ' arguments. Python’s own built-in descriptors support ' |
| 8579 | 'this\n' |
| 8580 | ' specification; however, it is likely that some ' |
| 8581 | 'third-party tools\n' |
| 8582 | ' have descriptors that require both arguments. Python’s ' |
| 8583 | 'own\n' |
| 8584 | ' "__getattribute__()" implementation always passes in both ' |
| 8585 | 'arguments\n' |
| 8586 | ' whether they are required or not.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8587 | '\n' |
| 8588 | 'object.__set__(self, instance, value)\n' |
| 8589 | '\n' |
| 8590 | ' Called to set the attribute on an instance *instance* of ' |
| 8591 | 'the owner\n' |
| 8592 | ' class to a new value, *value*.\n' |
| 8593 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 8594 | ' Note, adding "__set__()" or "__delete__()" changes the ' |
| 8595 | 'kind of\n' |
| 8596 | ' descriptor to a “data descriptor”. See Invoking ' |
| 8597 | 'Descriptors for\n' |
| 8598 | ' more details.\n' |
| 8599 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8600 | 'object.__delete__(self, instance)\n' |
| 8601 | '\n' |
| 8602 | ' Called to delete the attribute on an instance *instance* ' |
| 8603 | 'of the\n' |
| 8604 | ' owner class.\n' |
| 8605 | '\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 8606 | 'object.__set_name__(self, owner, name)\n' |
| 8607 | '\n' |
| 8608 | ' Called at the time the owning class *owner* is created. ' |
| 8609 | 'The\n' |
| 8610 | ' descriptor has been assigned to *name*.\n' |
| 8611 | '\n' |
Łukasz Langa | 6202d85 | 2019-12-18 22:09:19 +0100 | [diff] [blame] | 8612 | ' Note: "__set_name__()" is only called implicitly as part ' |
| 8613 | 'of the\n' |
| 8614 | ' "type" constructor, so it will need to be called ' |
| 8615 | 'explicitly with\n' |
| 8616 | ' the appropriate parameters when a descriptor is added ' |
| 8617 | 'to a class\n' |
| 8618 | ' after initial creation:\n' |
| 8619 | '\n' |
| 8620 | ' class A:\n' |
| 8621 | ' pass\n' |
| 8622 | ' descr = custom_descriptor()\n' |
| 8623 | ' A.attr = descr\n' |
| 8624 | " descr.__set_name__(A, 'attr')\n" |
| 8625 | '\n' |
| 8626 | ' See Creating the class object for more details.\n' |
| 8627 | '\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 8628 | ' New in version 3.6.\n' |
| 8629 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8630 | 'The attribute "__objclass__" is interpreted by the "inspect" ' |
| 8631 | 'module as\n' |
| 8632 | 'specifying the class where this object was defined (setting ' |
| 8633 | 'this\n' |
| 8634 | 'appropriately can assist in runtime introspection of dynamic ' |
| 8635 | 'class\n' |
| 8636 | 'attributes). For callables, it may indicate that an instance ' |
| 8637 | 'of the\n' |
| 8638 | 'given type (or a subclass) is expected or required as the ' |
| 8639 | 'first\n' |
| 8640 | 'positional argument (for example, CPython sets this ' |
| 8641 | 'attribute for\n' |
| 8642 | 'unbound methods that are implemented in C).\n' |
| 8643 | '\n' |
| 8644 | '\n' |
| 8645 | 'Invoking Descriptors\n' |
| 8646 | '--------------------\n' |
| 8647 | '\n' |
| 8648 | 'In general, a descriptor is an object attribute with ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8649 | '“binding\n' |
| 8650 | 'behavior”, one whose attribute access has been overridden by ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8651 | 'methods\n' |
| 8652 | 'in the descriptor protocol: "__get__()", "__set__()", and\n' |
| 8653 | '"__delete__()". If any of those methods are defined for an ' |
| 8654 | 'object, it\n' |
| 8655 | 'is said to be a descriptor.\n' |
| 8656 | '\n' |
| 8657 | 'The default behavior for attribute access is to get, set, or ' |
| 8658 | 'delete\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8659 | 'the attribute from an object’s dictionary. For instance, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8660 | '"a.x" has a\n' |
| 8661 | 'lookup chain starting with "a.__dict__[\'x\']", then\n' |
| 8662 | '"type(a).__dict__[\'x\']", and continuing through the base ' |
| 8663 | 'classes of\n' |
| 8664 | '"type(a)" excluding metaclasses.\n' |
| 8665 | '\n' |
| 8666 | 'However, if the looked-up value is an object defining one of ' |
| 8667 | 'the\n' |
| 8668 | 'descriptor methods, then Python may override the default ' |
| 8669 | 'behavior and\n' |
| 8670 | 'invoke the descriptor method instead. Where this occurs in ' |
| 8671 | 'the\n' |
| 8672 | 'precedence chain depends on which descriptor methods were ' |
| 8673 | 'defined and\n' |
| 8674 | 'how they were called.\n' |
| 8675 | '\n' |
| 8676 | 'The starting point for descriptor invocation is a binding, ' |
| 8677 | '"a.x". How\n' |
| 8678 | 'the arguments are assembled depends on "a":\n' |
| 8679 | '\n' |
| 8680 | 'Direct Call\n' |
| 8681 | ' The simplest and least common call is when user code ' |
| 8682 | 'directly\n' |
| 8683 | ' invokes a descriptor method: "x.__get__(a)".\n' |
| 8684 | '\n' |
| 8685 | 'Instance Binding\n' |
| 8686 | ' If binding to an object instance, "a.x" is transformed ' |
| 8687 | 'into the\n' |
| 8688 | ' call: "type(a).__dict__[\'x\'].__get__(a, type(a))".\n' |
| 8689 | '\n' |
| 8690 | 'Class Binding\n' |
| 8691 | ' If binding to a class, "A.x" is transformed into the ' |
| 8692 | 'call:\n' |
| 8693 | ' "A.__dict__[\'x\'].__get__(None, A)".\n' |
| 8694 | '\n' |
| 8695 | 'Super Binding\n' |
| 8696 | ' If "a" is an instance of "super", then the binding ' |
| 8697 | '"super(B,\n' |
| 8698 | ' obj).m()" searches "obj.__class__.__mro__" for the base ' |
| 8699 | 'class "A"\n' |
| 8700 | ' immediately preceding "B" and then invokes the descriptor ' |
| 8701 | 'with the\n' |
| 8702 | ' call: "A.__dict__[\'m\'].__get__(obj, obj.__class__)".\n' |
| 8703 | '\n' |
| 8704 | 'For instance bindings, the precedence of descriptor ' |
| 8705 | 'invocation depends\n' |
| 8706 | 'on the which descriptor methods are defined. A descriptor ' |
| 8707 | 'can define\n' |
| 8708 | 'any combination of "__get__()", "__set__()" and ' |
| 8709 | '"__delete__()". If it\n' |
| 8710 | 'does not define "__get__()", then accessing the attribute ' |
| 8711 | 'will return\n' |
| 8712 | 'the descriptor object itself unless there is a value in the ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8713 | 'object’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8714 | 'instance dictionary. If the descriptor defines "__set__()" ' |
| 8715 | 'and/or\n' |
| 8716 | '"__delete__()", it is a data descriptor; if it defines ' |
| 8717 | 'neither, it is\n' |
| 8718 | 'a non-data descriptor. Normally, data descriptors define ' |
| 8719 | 'both\n' |
| 8720 | '"__get__()" and "__set__()", while non-data descriptors have ' |
| 8721 | 'just the\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 8722 | '"__get__()" method. Data descriptors with "__get__()" and ' |
| 8723 | '"__set__()"\n' |
| 8724 | '(and/or "__delete__()") defined always override a ' |
| 8725 | 'redefinition in an\n' |
| 8726 | 'instance dictionary. In contrast, non-data descriptors can ' |
| 8727 | 'be\n' |
| 8728 | 'overridden by instances.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8729 | '\n' |
| 8730 | 'Python methods (including "staticmethod()" and ' |
| 8731 | '"classmethod()") are\n' |
| 8732 | 'implemented as non-data descriptors. Accordingly, instances ' |
| 8733 | 'can\n' |
| 8734 | 'redefine and override methods. This allows individual ' |
| 8735 | 'instances to\n' |
| 8736 | 'acquire behaviors that differ from other instances of the ' |
| 8737 | 'same class.\n' |
| 8738 | '\n' |
| 8739 | 'The "property()" function is implemented as a data ' |
| 8740 | 'descriptor.\n' |
| 8741 | 'Accordingly, instances cannot override the behavior of a ' |
| 8742 | 'property.\n' |
| 8743 | '\n' |
| 8744 | '\n' |
| 8745 | '__slots__\n' |
| 8746 | '---------\n' |
| 8747 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 8748 | '*__slots__* allow us to explicitly declare data members ' |
| 8749 | '(like\n' |
| 8750 | 'properties) and deny the creation of *__dict__* and ' |
| 8751 | '*__weakref__*\n' |
| 8752 | '(unless explicitly declared in *__slots__* or available in a ' |
| 8753 | 'parent.)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8754 | '\n' |
Łukasz Langa | 23f4589 | 2019-02-25 13:08:32 +0100 | [diff] [blame] | 8755 | 'The space saved over using *__dict__* can be significant. ' |
| 8756 | 'Attribute\n' |
| 8757 | 'lookup speed can be significantly improved as well.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8758 | '\n' |
| 8759 | 'object.__slots__\n' |
| 8760 | '\n' |
| 8761 | ' This class variable can be assigned a string, iterable, ' |
| 8762 | 'or sequence\n' |
| 8763 | ' of strings with variable names used by instances. ' |
| 8764 | '*__slots__*\n' |
| 8765 | ' reserves space for the declared variables and prevents ' |
| 8766 | 'the\n' |
| 8767 | ' automatic creation of *__dict__* and *__weakref__* for ' |
| 8768 | 'each\n' |
| 8769 | ' instance.\n' |
| 8770 | '\n' |
| 8771 | '\n' |
| 8772 | 'Notes on using *__slots__*\n' |
| 8773 | '~~~~~~~~~~~~~~~~~~~~~~~~~~\n' |
| 8774 | '\n' |
| 8775 | '* When inheriting from a class without *__slots__*, the ' |
| 8776 | '*__dict__*\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 8777 | ' and *__weakref__* attribute of the instances will always ' |
| 8778 | 'be\n' |
| 8779 | ' accessible.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8780 | '\n' |
| 8781 | '* Without a *__dict__* variable, instances cannot be ' |
| 8782 | 'assigned new\n' |
| 8783 | ' variables not listed in the *__slots__* definition. ' |
| 8784 | 'Attempts to\n' |
| 8785 | ' assign to an unlisted variable name raises ' |
| 8786 | '"AttributeError". If\n' |
| 8787 | ' dynamic assignment of new variables is desired, then add\n' |
| 8788 | ' "\'__dict__\'" to the sequence of strings in the ' |
| 8789 | '*__slots__*\n' |
| 8790 | ' declaration.\n' |
| 8791 | '\n' |
| 8792 | '* Without a *__weakref__* variable for each instance, ' |
| 8793 | 'classes\n' |
| 8794 | ' defining *__slots__* do not support weak references to ' |
| 8795 | 'its\n' |
| 8796 | ' instances. If weak reference support is needed, then add\n' |
| 8797 | ' "\'__weakref__\'" to the sequence of strings in the ' |
| 8798 | '*__slots__*\n' |
| 8799 | ' declaration.\n' |
| 8800 | '\n' |
| 8801 | '* *__slots__* are implemented at the class level by ' |
| 8802 | 'creating\n' |
| 8803 | ' descriptors (Implementing Descriptors) for each variable ' |
| 8804 | 'name. As a\n' |
| 8805 | ' result, class attributes cannot be used to set default ' |
| 8806 | 'values for\n' |
| 8807 | ' instance variables defined by *__slots__*; otherwise, the ' |
| 8808 | 'class\n' |
| 8809 | ' attribute would overwrite the descriptor assignment.\n' |
| 8810 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 8811 | '* The action of a *__slots__* declaration is not limited to ' |
| 8812 | 'the\n' |
| 8813 | ' class where it is defined. *__slots__* declared in ' |
| 8814 | 'parents are\n' |
| 8815 | ' available in child classes. However, child subclasses will ' |
| 8816 | 'get a\n' |
| 8817 | ' *__dict__* and *__weakref__* unless they also define ' |
| 8818 | '*__slots__*\n' |
| 8819 | ' (which should only contain names of any *additional* ' |
| 8820 | 'slots).\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8821 | '\n' |
| 8822 | '* If a class defines a slot also defined in a base class, ' |
| 8823 | 'the\n' |
| 8824 | ' instance variable defined by the base class slot is ' |
| 8825 | 'inaccessible\n' |
| 8826 | ' (except by retrieving its descriptor directly from the ' |
| 8827 | 'base class).\n' |
| 8828 | ' This renders the meaning of the program undefined. In the ' |
| 8829 | 'future, a\n' |
| 8830 | ' check may be added to prevent this.\n' |
| 8831 | '\n' |
| 8832 | '* Nonempty *__slots__* does not work for classes derived ' |
| 8833 | 'from\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8834 | ' “variable-length” built-in types such as "int", "bytes" ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8835 | 'and "tuple".\n' |
| 8836 | '\n' |
| 8837 | '* Any non-string iterable may be assigned to *__slots__*. ' |
| 8838 | 'Mappings\n' |
| 8839 | ' may also be used; however, in the future, special meaning ' |
| 8840 | 'may be\n' |
| 8841 | ' assigned to the values corresponding to each key.\n' |
| 8842 | '\n' |
| 8843 | '* *__class__* assignment works only if both classes have the ' |
| 8844 | 'same\n' |
| 8845 | ' *__slots__*.\n' |
| 8846 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 8847 | '* Multiple inheritance with multiple slotted parent classes ' |
| 8848 | 'can be\n' |
| 8849 | ' used, but only one parent is allowed to have attributes ' |
| 8850 | 'created by\n' |
| 8851 | ' slots (the other bases must have empty slot layouts) - ' |
| 8852 | 'violations\n' |
| 8853 | ' raise "TypeError".\n' |
| 8854 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 8855 | '* If an iterator is used for *__slots__* then a descriptor ' |
| 8856 | 'is\n' |
| 8857 | ' created for each of the iterator’s values. However, the ' |
| 8858 | '*__slots__*\n' |
| 8859 | ' attribute will be an empty iterator.\n' |
| 8860 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8861 | '\n' |
| 8862 | 'Customizing class creation\n' |
| 8863 | '==========================\n' |
| 8864 | '\n' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 8865 | 'Whenever a class inherits from another class, ' |
| 8866 | '*__init_subclass__* is\n' |
| 8867 | 'called on that class. This way, it is possible to write ' |
| 8868 | 'classes which\n' |
| 8869 | 'change the behavior of subclasses. This is closely related ' |
| 8870 | 'to class\n' |
| 8871 | 'decorators, but where class decorators only affect the ' |
| 8872 | 'specific class\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8873 | 'they’re applied to, "__init_subclass__" solely applies to ' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 8874 | 'future\n' |
| 8875 | 'subclasses of the class defining the method.\n' |
| 8876 | '\n' |
| 8877 | 'classmethod object.__init_subclass__(cls)\n' |
| 8878 | '\n' |
| 8879 | ' This method is called whenever the containing class is ' |
| 8880 | 'subclassed.\n' |
| 8881 | ' *cls* is then the new subclass. If defined as a normal ' |
| 8882 | 'instance\n' |
| 8883 | ' method, this method is implicitly converted to a class ' |
| 8884 | 'method.\n' |
| 8885 | '\n' |
| 8886 | ' Keyword arguments which are given to a new class are ' |
| 8887 | 'passed to the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8888 | ' parent’s class "__init_subclass__". For compatibility ' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 8889 | 'with other\n' |
| 8890 | ' classes using "__init_subclass__", one should take out ' |
| 8891 | 'the needed\n' |
| 8892 | ' keyword arguments and pass the others over to the base ' |
| 8893 | 'class, as\n' |
| 8894 | ' in:\n' |
| 8895 | '\n' |
| 8896 | ' class Philosopher:\n' |
Łukasz Langa | 3b5deb0 | 2019-06-04 19:44:34 +0200 | [diff] [blame] | 8897 | ' def __init_subclass__(cls, /, default_name, ' |
Ned Deily | 46b0a32 | 2016-08-15 16:12:59 -0400 | [diff] [blame] | 8898 | '**kwargs):\n' |
| 8899 | ' super().__init_subclass__(**kwargs)\n' |
| 8900 | ' cls.default_name = default_name\n' |
| 8901 | '\n' |
| 8902 | ' class AustralianPhilosopher(Philosopher, ' |
| 8903 | 'default_name="Bruce"):\n' |
| 8904 | ' pass\n' |
| 8905 | '\n' |
| 8906 | ' The default implementation "object.__init_subclass__" ' |
| 8907 | 'does nothing,\n' |
| 8908 | ' but raises an error if it is called with any arguments.\n' |
| 8909 | '\n' |
| 8910 | ' Note: The metaclass hint "metaclass" is consumed by the ' |
| 8911 | 'rest of\n' |
| 8912 | ' the type machinery, and is never passed to ' |
| 8913 | '"__init_subclass__"\n' |
| 8914 | ' implementations. The actual metaclass (rather than the ' |
| 8915 | 'explicit\n' |
| 8916 | ' hint) can be accessed as "type(cls)".\n' |
| 8917 | '\n' |
| 8918 | ' New in version 3.6.\n' |
| 8919 | '\n' |
| 8920 | '\n' |
| 8921 | 'Metaclasses\n' |
| 8922 | '-----------\n' |
| 8923 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8924 | 'By default, classes are constructed using "type()". The ' |
| 8925 | 'class body is\n' |
| 8926 | 'executed in a new namespace and the class name is bound ' |
| 8927 | 'locally to the\n' |
| 8928 | 'result of "type(name, bases, namespace)".\n' |
| 8929 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 8930 | 'The class creation process can be customized by passing the\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8931 | '"metaclass" keyword argument in the class definition line, ' |
| 8932 | 'or by\n' |
| 8933 | 'inheriting from an existing class that included such an ' |
| 8934 | 'argument. In\n' |
| 8935 | 'the following example, both "MyClass" and "MySubclass" are ' |
| 8936 | 'instances\n' |
| 8937 | 'of "Meta":\n' |
| 8938 | '\n' |
| 8939 | ' class Meta(type):\n' |
| 8940 | ' pass\n' |
| 8941 | '\n' |
| 8942 | ' class MyClass(metaclass=Meta):\n' |
| 8943 | ' pass\n' |
| 8944 | '\n' |
| 8945 | ' class MySubclass(MyClass):\n' |
| 8946 | ' pass\n' |
| 8947 | '\n' |
| 8948 | 'Any other keyword arguments that are specified in the class ' |
| 8949 | 'definition\n' |
| 8950 | 'are passed through to all metaclass operations described ' |
| 8951 | 'below.\n' |
| 8952 | '\n' |
| 8953 | 'When a class definition is executed, the following steps ' |
| 8954 | 'occur:\n' |
| 8955 | '\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 8956 | '* MRO entries are resolved;\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8957 | '\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 8958 | '* the appropriate metaclass is determined;\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8959 | '\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 8960 | '* the class namespace is prepared;\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8961 | '\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 8962 | '* the class body is executed;\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8963 | '\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 8964 | '* the class object is created.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8965 | '\n' |
| 8966 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 8967 | 'Resolving MRO entries\n' |
| 8968 | '---------------------\n' |
| 8969 | '\n' |
| 8970 | 'If a base that appears in class definition is not an ' |
| 8971 | 'instance of\n' |
| 8972 | '"type", then an "__mro_entries__" method is searched on it. ' |
| 8973 | 'If found,\n' |
| 8974 | 'it is called with the original bases tuple. This method must ' |
| 8975 | 'return a\n' |
| 8976 | 'tuple of classes that will be used instead of this base. The ' |
| 8977 | 'tuple may\n' |
| 8978 | 'be empty, in such case the original base is ignored.\n' |
| 8979 | '\n' |
| 8980 | 'See also: **PEP 560** - Core support for typing module and ' |
| 8981 | 'generic\n' |
| 8982 | ' types\n' |
| 8983 | '\n' |
| 8984 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8985 | 'Determining the appropriate metaclass\n' |
| 8986 | '-------------------------------------\n' |
| 8987 | '\n' |
| 8988 | 'The appropriate metaclass for a class definition is ' |
| 8989 | 'determined as\n' |
| 8990 | 'follows:\n' |
| 8991 | '\n' |
| 8992 | '* if no bases and no explicit metaclass are given, then ' |
| 8993 | '"type()" is\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 8994 | ' used;\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8995 | '\n' |
| 8996 | '* if an explicit metaclass is given and it is *not* an ' |
| 8997 | 'instance of\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 8998 | ' "type()", then it is used directly as the metaclass;\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 8999 | '\n' |
| 9000 | '* if an instance of "type()" is given as the explicit ' |
| 9001 | 'metaclass, or\n' |
| 9002 | ' bases are defined, then the most derived metaclass is ' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 9003 | 'used.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9004 | '\n' |
| 9005 | 'The most derived metaclass is selected from the explicitly ' |
| 9006 | 'specified\n' |
| 9007 | 'metaclass (if any) and the metaclasses (i.e. "type(cls)") of ' |
| 9008 | 'all\n' |
| 9009 | 'specified base classes. The most derived metaclass is one ' |
| 9010 | 'which is a\n' |
| 9011 | 'subtype of *all* of these candidate metaclasses. If none of ' |
| 9012 | 'the\n' |
| 9013 | 'candidate metaclasses meets that criterion, then the class ' |
| 9014 | 'definition\n' |
| 9015 | 'will fail with "TypeError".\n' |
| 9016 | '\n' |
| 9017 | '\n' |
| 9018 | 'Preparing the class namespace\n' |
| 9019 | '-----------------------------\n' |
| 9020 | '\n' |
| 9021 | 'Once the appropriate metaclass has been identified, then the ' |
| 9022 | 'class\n' |
| 9023 | 'namespace is prepared. If the metaclass has a "__prepare__" ' |
| 9024 | 'attribute,\n' |
| 9025 | 'it is called as "namespace = metaclass.__prepare__(name, ' |
| 9026 | 'bases,\n' |
| 9027 | '**kwds)" (where the additional keyword arguments, if any, ' |
| 9028 | 'come from\n' |
Łukasz Langa | 6e02691 | 2020-02-25 13:21:47 +0100 | [diff] [blame] | 9029 | 'the class definition). The "__prepare__" method should be ' |
| 9030 | 'implemented\n' |
| 9031 | 'as a "classmethod()". The namespace returned by ' |
| 9032 | '"__prepare__" is\n' |
| 9033 | 'passed in to "__new__", but when the final class object is ' |
| 9034 | 'created the\n' |
| 9035 | 'namespace is copied into a new "dict".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9036 | '\n' |
| 9037 | 'If the metaclass has no "__prepare__" attribute, then the ' |
| 9038 | 'class\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 9039 | 'namespace is initialised as an empty ordered mapping.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9040 | '\n' |
| 9041 | 'See also:\n' |
| 9042 | '\n' |
| 9043 | ' **PEP 3115** - Metaclasses in Python 3000\n' |
| 9044 | ' Introduced the "__prepare__" namespace hook\n' |
| 9045 | '\n' |
| 9046 | '\n' |
| 9047 | 'Executing the class body\n' |
| 9048 | '------------------------\n' |
| 9049 | '\n' |
| 9050 | 'The class body is executed (approximately) as "exec(body, ' |
| 9051 | 'globals(),\n' |
| 9052 | 'namespace)". The key difference from a normal call to ' |
| 9053 | '"exec()" is that\n' |
| 9054 | 'lexical scoping allows the class body (including any ' |
| 9055 | 'methods) to\n' |
| 9056 | 'reference names from the current and outer scopes when the ' |
| 9057 | 'class\n' |
| 9058 | 'definition occurs inside a function.\n' |
| 9059 | '\n' |
| 9060 | 'However, even when the class definition occurs inside the ' |
| 9061 | 'function,\n' |
| 9062 | 'methods defined inside the class still cannot see names ' |
| 9063 | 'defined at the\n' |
| 9064 | 'class scope. Class variables must be accessed through the ' |
| 9065 | 'first\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 9066 | 'parameter of instance or class methods, or through the ' |
| 9067 | 'implicit\n' |
| 9068 | 'lexically scoped "__class__" reference described in the next ' |
| 9069 | 'section.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9070 | '\n' |
| 9071 | '\n' |
| 9072 | 'Creating the class object\n' |
| 9073 | '-------------------------\n' |
| 9074 | '\n' |
| 9075 | 'Once the class namespace has been populated by executing the ' |
| 9076 | 'class\n' |
| 9077 | 'body, the class object is created by calling ' |
| 9078 | '"metaclass(name, bases,\n' |
| 9079 | 'namespace, **kwds)" (the additional keywords passed here are ' |
| 9080 | 'the same\n' |
| 9081 | 'as those passed to "__prepare__").\n' |
| 9082 | '\n' |
| 9083 | 'This class object is the one that will be referenced by the ' |
| 9084 | 'zero-\n' |
| 9085 | 'argument form of "super()". "__class__" is an implicit ' |
| 9086 | 'closure\n' |
| 9087 | 'reference created by the compiler if any methods in a class ' |
| 9088 | 'body refer\n' |
| 9089 | 'to either "__class__" or "super". This allows the zero ' |
| 9090 | 'argument form\n' |
| 9091 | 'of "super()" to correctly identify the class being defined ' |
| 9092 | 'based on\n' |
| 9093 | 'lexical scoping, while the class or instance that was used ' |
| 9094 | 'to make the\n' |
| 9095 | 'current call is identified based on the first argument ' |
| 9096 | 'passed to the\n' |
| 9097 | 'method.\n' |
| 9098 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 9099 | '**CPython implementation detail:** In CPython 3.6 and later, ' |
| 9100 | 'the\n' |
| 9101 | '"__class__" cell is passed to the metaclass as a ' |
| 9102 | '"__classcell__" entry\n' |
| 9103 | 'in the class namespace. If present, this must be propagated ' |
| 9104 | 'up to the\n' |
| 9105 | '"type.__new__" call in order for the class to be ' |
| 9106 | 'initialised\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9107 | 'correctly. Failing to do so will result in a "RuntimeError" ' |
| 9108 | 'in Python\n' |
| 9109 | '3.8.\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 9110 | '\n' |
| 9111 | 'When using the default metaclass "type", or any metaclass ' |
| 9112 | 'that\n' |
| 9113 | 'ultimately calls "type.__new__", the following additional\n' |
| 9114 | 'customisation steps are invoked after creating the class ' |
| 9115 | 'object:\n' |
| 9116 | '\n' |
| 9117 | '* first, "type.__new__" collects all of the descriptors in ' |
| 9118 | 'the class\n' |
| 9119 | ' namespace that define a "__set_name__()" method;\n' |
| 9120 | '\n' |
| 9121 | '* second, all of these "__set_name__" methods are called ' |
| 9122 | 'with the\n' |
| 9123 | ' class being defined and the assigned name of that ' |
| 9124 | 'particular\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 9125 | ' descriptor;\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 9126 | '\n' |
| 9127 | '* finally, the "__init_subclass__()" hook is called on the ' |
| 9128 | 'immediate\n' |
| 9129 | ' parent of the new class in its method resolution order.\n' |
| 9130 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9131 | 'After the class object is created, it is passed to the ' |
| 9132 | 'class\n' |
| 9133 | 'decorators included in the class definition (if any) and the ' |
| 9134 | 'resulting\n' |
| 9135 | 'object is bound in the local namespace as the defined ' |
| 9136 | 'class.\n' |
| 9137 | '\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 9138 | 'When a new class is created by "type.__new__", the object ' |
| 9139 | 'provided as\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 9140 | 'the namespace parameter is copied to a new ordered mapping ' |
| 9141 | 'and the\n' |
| 9142 | 'original object is discarded. The new copy is wrapped in a ' |
| 9143 | 'read-only\n' |
| 9144 | 'proxy, which becomes the "__dict__" attribute of the class ' |
| 9145 | 'object.\n' |
Ned Deily | 8b9173a | 2016-06-13 16:51:55 -0400 | [diff] [blame] | 9146 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9147 | 'See also:\n' |
| 9148 | '\n' |
| 9149 | ' **PEP 3135** - New super\n' |
| 9150 | ' Describes the implicit "__class__" closure reference\n' |
| 9151 | '\n' |
| 9152 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9153 | 'Uses for metaclasses\n' |
| 9154 | '--------------------\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9155 | '\n' |
| 9156 | 'The potential uses for metaclasses are boundless. Some ideas ' |
| 9157 | 'that have\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 9158 | 'been explored include enum, logging, interface checking, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9159 | 'automatic\n' |
| 9160 | 'delegation, automatic property creation, proxies, ' |
| 9161 | 'frameworks, and\n' |
| 9162 | 'automatic resource locking/synchronization.\n' |
| 9163 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9164 | '\n' |
| 9165 | 'Customizing instance and subclass checks\n' |
| 9166 | '========================================\n' |
| 9167 | '\n' |
| 9168 | 'The following methods are used to override the default ' |
| 9169 | 'behavior of the\n' |
| 9170 | '"isinstance()" and "issubclass()" built-in functions.\n' |
| 9171 | '\n' |
| 9172 | 'In particular, the metaclass "abc.ABCMeta" implements these ' |
| 9173 | 'methods in\n' |
| 9174 | 'order to allow the addition of Abstract Base Classes (ABCs) ' |
| 9175 | 'as\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9176 | '“virtual base classes” to any class or type (including ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9177 | 'built-in\n' |
| 9178 | 'types), including other ABCs.\n' |
| 9179 | '\n' |
| 9180 | 'class.__instancecheck__(self, instance)\n' |
| 9181 | '\n' |
| 9182 | ' Return true if *instance* should be considered a (direct ' |
| 9183 | 'or\n' |
| 9184 | ' indirect) instance of *class*. If defined, called to ' |
| 9185 | 'implement\n' |
| 9186 | ' "isinstance(instance, class)".\n' |
| 9187 | '\n' |
| 9188 | 'class.__subclasscheck__(self, subclass)\n' |
| 9189 | '\n' |
| 9190 | ' Return true if *subclass* should be considered a (direct ' |
| 9191 | 'or\n' |
| 9192 | ' indirect) subclass of *class*. If defined, called to ' |
| 9193 | 'implement\n' |
| 9194 | ' "issubclass(subclass, class)".\n' |
| 9195 | '\n' |
| 9196 | 'Note that these methods are looked up on the type ' |
| 9197 | '(metaclass) of a\n' |
| 9198 | 'class. They cannot be defined as class methods in the ' |
| 9199 | 'actual class.\n' |
| 9200 | 'This is consistent with the lookup of special methods that ' |
| 9201 | 'are called\n' |
| 9202 | 'on instances, only in this case the instance is itself a ' |
| 9203 | 'class.\n' |
| 9204 | '\n' |
| 9205 | 'See also:\n' |
| 9206 | '\n' |
| 9207 | ' **PEP 3119** - Introducing Abstract Base Classes\n' |
| 9208 | ' Includes the specification for customizing ' |
| 9209 | '"isinstance()" and\n' |
| 9210 | ' "issubclass()" behavior through "__instancecheck__()" ' |
| 9211 | 'and\n' |
| 9212 | ' "__subclasscheck__()", with motivation for this ' |
| 9213 | 'functionality in\n' |
| 9214 | ' the context of adding Abstract Base Classes (see the ' |
| 9215 | '"abc"\n' |
| 9216 | ' module) to the language.\n' |
| 9217 | '\n' |
| 9218 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9219 | 'Emulating generic types\n' |
| 9220 | '=======================\n' |
| 9221 | '\n' |
| 9222 | 'One can implement the generic class syntax as specified by ' |
| 9223 | '**PEP 484**\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 9224 | '(for example "List[int]") by defining a special method:\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9225 | '\n' |
| 9226 | 'classmethod object.__class_getitem__(cls, key)\n' |
| 9227 | '\n' |
| 9228 | ' Return an object representing the specialization of a ' |
| 9229 | 'generic class\n' |
| 9230 | ' by type arguments found in *key*.\n' |
| 9231 | '\n' |
| 9232 | 'This method is looked up on the class object itself, and ' |
| 9233 | 'when defined\n' |
| 9234 | 'in the class body, this method is implicitly a class ' |
| 9235 | 'method. Note,\n' |
| 9236 | 'this mechanism is primarily reserved for use with static ' |
| 9237 | 'type hints,\n' |
| 9238 | 'other usage is discouraged.\n' |
| 9239 | '\n' |
| 9240 | 'See also: **PEP 560** - Core support for typing module and ' |
| 9241 | 'generic\n' |
| 9242 | ' types\n' |
| 9243 | '\n' |
| 9244 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9245 | 'Emulating callable objects\n' |
| 9246 | '==========================\n' |
| 9247 | '\n' |
| 9248 | 'object.__call__(self[, args...])\n' |
| 9249 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9250 | ' Called when the instance is “called” as a function; if ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9251 | 'this method\n' |
| 9252 | ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n' |
| 9253 | ' "x.__call__(arg1, arg2, ...)".\n' |
| 9254 | '\n' |
| 9255 | '\n' |
| 9256 | 'Emulating container types\n' |
| 9257 | '=========================\n' |
| 9258 | '\n' |
| 9259 | 'The following methods can be defined to implement container ' |
| 9260 | 'objects.\n' |
| 9261 | 'Containers usually are sequences (such as lists or tuples) ' |
| 9262 | 'or mappings\n' |
| 9263 | '(like dictionaries), but can represent other containers as ' |
| 9264 | 'well. The\n' |
| 9265 | 'first set of methods is used either to emulate a sequence or ' |
| 9266 | 'to\n' |
| 9267 | 'emulate a mapping; the difference is that for a sequence, ' |
| 9268 | 'the\n' |
| 9269 | 'allowable keys should be the integers *k* for which "0 <= k ' |
| 9270 | '< N" where\n' |
| 9271 | '*N* is the length of the sequence, or slice objects, which ' |
| 9272 | 'define a\n' |
| 9273 | 'range of items. It is also recommended that mappings ' |
| 9274 | 'provide the\n' |
| 9275 | 'methods "keys()", "values()", "items()", "get()", ' |
| 9276 | '"clear()",\n' |
| 9277 | '"setdefault()", "pop()", "popitem()", "copy()", and ' |
| 9278 | '"update()"\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9279 | 'behaving similar to those for Python’s standard dictionary ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9280 | 'objects.\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 9281 | 'The "collections.abc" module provides a "MutableMapping" ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9282 | 'abstract base\n' |
| 9283 | 'class to help create those methods from a base set of ' |
| 9284 | '"__getitem__()",\n' |
| 9285 | '"__setitem__()", "__delitem__()", and "keys()". Mutable ' |
| 9286 | 'sequences\n' |
| 9287 | 'should provide methods "append()", "count()", "index()", ' |
| 9288 | '"extend()",\n' |
| 9289 | '"insert()", "pop()", "remove()", "reverse()" and "sort()", ' |
| 9290 | 'like Python\n' |
| 9291 | 'standard list objects. Finally, sequence types should ' |
| 9292 | 'implement\n' |
| 9293 | 'addition (meaning concatenation) and multiplication ' |
| 9294 | '(meaning\n' |
| 9295 | 'repetition) by defining the methods "__add__()", ' |
| 9296 | '"__radd__()",\n' |
| 9297 | '"__iadd__()", "__mul__()", "__rmul__()" and "__imul__()" ' |
| 9298 | 'described\n' |
| 9299 | 'below; they should not define other numerical operators. It ' |
| 9300 | 'is\n' |
| 9301 | 'recommended that both mappings and sequences implement the\n' |
| 9302 | '"__contains__()" method to allow efficient use of the "in" ' |
| 9303 | 'operator;\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9304 | 'for mappings, "in" should search the mapping’s keys; for ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9305 | 'sequences, it\n' |
| 9306 | 'should search through the values. It is further recommended ' |
| 9307 | 'that both\n' |
| 9308 | 'mappings and sequences implement the "__iter__()" method to ' |
| 9309 | 'allow\n' |
| 9310 | 'efficient iteration through the container; for mappings, ' |
| 9311 | '"__iter__()"\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 9312 | 'should iterate through the object’s keys; for sequences, it ' |
| 9313 | 'should\n' |
| 9314 | 'iterate through the values.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9315 | '\n' |
| 9316 | 'object.__len__(self)\n' |
| 9317 | '\n' |
| 9318 | ' Called to implement the built-in function "len()". ' |
| 9319 | 'Should return\n' |
| 9320 | ' the length of the object, an integer ">=" 0. Also, an ' |
| 9321 | 'object that\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9322 | ' doesn’t define a "__bool__()" method and whose ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9323 | '"__len__()" method\n' |
| 9324 | ' returns zero is considered to be false in a Boolean ' |
| 9325 | 'context.\n' |
| 9326 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 9327 | ' **CPython implementation detail:** In CPython, the length ' |
| 9328 | 'is\n' |
| 9329 | ' required to be at most "sys.maxsize". If the length is ' |
| 9330 | 'larger than\n' |
| 9331 | ' "sys.maxsize" some features (such as "len()") may raise\n' |
| 9332 | ' "OverflowError". To prevent raising "OverflowError" by ' |
| 9333 | 'truth value\n' |
| 9334 | ' testing, an object must define a "__bool__()" method.\n' |
| 9335 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9336 | 'object.__length_hint__(self)\n' |
| 9337 | '\n' |
| 9338 | ' Called to implement "operator.length_hint()". Should ' |
| 9339 | 'return an\n' |
| 9340 | ' estimated length for the object (which may be greater or ' |
| 9341 | 'less than\n' |
| 9342 | ' the actual length). The length must be an integer ">=" 0. ' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 9343 | 'The\n' |
| 9344 | ' return value may also be "NotImplemented", which is ' |
| 9345 | 'treated the\n' |
| 9346 | ' same as if the "__length_hint__" method didn’t exist at ' |
| 9347 | 'all. This\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9348 | ' method is purely an optimization and is never required ' |
| 9349 | 'for\n' |
| 9350 | ' correctness.\n' |
| 9351 | '\n' |
| 9352 | ' New in version 3.4.\n' |
| 9353 | '\n' |
| 9354 | 'Note: Slicing is done exclusively with the following three ' |
| 9355 | 'methods.\n' |
| 9356 | ' A call like\n' |
| 9357 | '\n' |
| 9358 | ' a[1:2] = b\n' |
| 9359 | '\n' |
| 9360 | ' is translated to\n' |
| 9361 | '\n' |
| 9362 | ' a[slice(1, 2, None)] = b\n' |
| 9363 | '\n' |
| 9364 | ' and so forth. Missing slice items are always filled in ' |
| 9365 | 'with "None".\n' |
| 9366 | '\n' |
| 9367 | 'object.__getitem__(self, key)\n' |
| 9368 | '\n' |
| 9369 | ' Called to implement evaluation of "self[key]". For ' |
| 9370 | 'sequence types,\n' |
| 9371 | ' the accepted keys should be integers and slice objects. ' |
| 9372 | 'Note that\n' |
| 9373 | ' the special interpretation of negative indexes (if the ' |
| 9374 | 'class wishes\n' |
| 9375 | ' to emulate a sequence type) is up to the "__getitem__()" ' |
| 9376 | 'method. If\n' |
| 9377 | ' *key* is of an inappropriate type, "TypeError" may be ' |
| 9378 | 'raised; if of\n' |
| 9379 | ' a value outside the set of indexes for the sequence ' |
| 9380 | '(after any\n' |
| 9381 | ' special interpretation of negative values), "IndexError" ' |
| 9382 | 'should be\n' |
| 9383 | ' raised. For mapping types, if *key* is missing (not in ' |
| 9384 | 'the\n' |
| 9385 | ' container), "KeyError" should be raised.\n' |
| 9386 | '\n' |
| 9387 | ' Note: "for" loops expect that an "IndexError" will be ' |
| 9388 | 'raised for\n' |
| 9389 | ' illegal indexes to allow proper detection of the end of ' |
| 9390 | 'the\n' |
| 9391 | ' sequence.\n' |
| 9392 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9393 | 'object.__setitem__(self, key, value)\n' |
| 9394 | '\n' |
| 9395 | ' Called to implement assignment to "self[key]". Same note ' |
| 9396 | 'as for\n' |
| 9397 | ' "__getitem__()". This should only be implemented for ' |
| 9398 | 'mappings if\n' |
| 9399 | ' the objects support changes to the values for keys, or if ' |
| 9400 | 'new keys\n' |
| 9401 | ' can be added, or for sequences if elements can be ' |
| 9402 | 'replaced. The\n' |
| 9403 | ' same exceptions should be raised for improper *key* ' |
| 9404 | 'values as for\n' |
| 9405 | ' the "__getitem__()" method.\n' |
| 9406 | '\n' |
| 9407 | 'object.__delitem__(self, key)\n' |
| 9408 | '\n' |
| 9409 | ' Called to implement deletion of "self[key]". Same note ' |
| 9410 | 'as for\n' |
| 9411 | ' "__getitem__()". This should only be implemented for ' |
| 9412 | 'mappings if\n' |
| 9413 | ' the objects support removal of keys, or for sequences if ' |
| 9414 | 'elements\n' |
| 9415 | ' can be removed from the sequence. The same exceptions ' |
| 9416 | 'should be\n' |
| 9417 | ' raised for improper *key* values as for the ' |
| 9418 | '"__getitem__()" method.\n' |
| 9419 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9420 | 'object.__missing__(self, key)\n' |
| 9421 | '\n' |
| 9422 | ' Called by "dict"."__getitem__()" to implement "self[key]" ' |
| 9423 | 'for dict\n' |
| 9424 | ' subclasses when key is not in the dictionary.\n' |
| 9425 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9426 | 'object.__iter__(self)\n' |
| 9427 | '\n' |
| 9428 | ' This method is called when an iterator is required for a ' |
| 9429 | 'container.\n' |
| 9430 | ' This method should return a new iterator object that can ' |
| 9431 | 'iterate\n' |
| 9432 | ' over all the objects in the container. For mappings, it ' |
| 9433 | 'should\n' |
| 9434 | ' iterate over the keys of the container.\n' |
| 9435 | '\n' |
| 9436 | ' Iterator objects also need to implement this method; they ' |
| 9437 | 'are\n' |
| 9438 | ' required to return themselves. For more information on ' |
| 9439 | 'iterator\n' |
| 9440 | ' objects, see Iterator Types.\n' |
| 9441 | '\n' |
| 9442 | 'object.__reversed__(self)\n' |
| 9443 | '\n' |
| 9444 | ' Called (if present) by the "reversed()" built-in to ' |
| 9445 | 'implement\n' |
| 9446 | ' reverse iteration. It should return a new iterator ' |
| 9447 | 'object that\n' |
| 9448 | ' iterates over all the objects in the container in reverse ' |
| 9449 | 'order.\n' |
| 9450 | '\n' |
| 9451 | ' If the "__reversed__()" method is not provided, the ' |
| 9452 | '"reversed()"\n' |
| 9453 | ' built-in will fall back to using the sequence protocol ' |
| 9454 | '("__len__()"\n' |
| 9455 | ' and "__getitem__()"). Objects that support the sequence ' |
| 9456 | 'protocol\n' |
| 9457 | ' should only provide "__reversed__()" if they can provide ' |
| 9458 | 'an\n' |
| 9459 | ' implementation that is more efficient than the one ' |
| 9460 | 'provided by\n' |
| 9461 | ' "reversed()".\n' |
| 9462 | '\n' |
| 9463 | 'The membership test operators ("in" and "not in") are ' |
| 9464 | 'normally\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 9465 | 'implemented as an iteration through a container. However, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9466 | 'container\n' |
| 9467 | 'objects can supply the following special method with a more ' |
| 9468 | 'efficient\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 9469 | 'implementation, which also does not require the object be ' |
| 9470 | 'iterable.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9471 | '\n' |
| 9472 | 'object.__contains__(self, item)\n' |
| 9473 | '\n' |
| 9474 | ' Called to implement membership test operators. Should ' |
| 9475 | 'return true\n' |
| 9476 | ' if *item* is in *self*, false otherwise. For mapping ' |
| 9477 | 'objects, this\n' |
| 9478 | ' should consider the keys of the mapping rather than the ' |
| 9479 | 'values or\n' |
| 9480 | ' the key-item pairs.\n' |
| 9481 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9482 | ' For objects that don’t define "__contains__()", the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9483 | 'membership test\n' |
| 9484 | ' first tries iteration via "__iter__()", then the old ' |
| 9485 | 'sequence\n' |
| 9486 | ' iteration protocol via "__getitem__()", see this section ' |
| 9487 | 'in the\n' |
| 9488 | ' language reference.\n' |
| 9489 | '\n' |
| 9490 | '\n' |
| 9491 | 'Emulating numeric types\n' |
| 9492 | '=======================\n' |
| 9493 | '\n' |
| 9494 | 'The following methods can be defined to emulate numeric ' |
| 9495 | 'objects.\n' |
| 9496 | 'Methods corresponding to operations that are not supported ' |
| 9497 | 'by the\n' |
| 9498 | 'particular kind of number implemented (e.g., bitwise ' |
| 9499 | 'operations for\n' |
| 9500 | 'non-integral numbers) should be left undefined.\n' |
| 9501 | '\n' |
| 9502 | 'object.__add__(self, other)\n' |
| 9503 | 'object.__sub__(self, other)\n' |
| 9504 | 'object.__mul__(self, other)\n' |
| 9505 | 'object.__matmul__(self, other)\n' |
| 9506 | 'object.__truediv__(self, other)\n' |
| 9507 | 'object.__floordiv__(self, other)\n' |
| 9508 | 'object.__mod__(self, other)\n' |
| 9509 | 'object.__divmod__(self, other)\n' |
| 9510 | 'object.__pow__(self, other[, modulo])\n' |
| 9511 | 'object.__lshift__(self, other)\n' |
| 9512 | 'object.__rshift__(self, other)\n' |
| 9513 | 'object.__and__(self, other)\n' |
| 9514 | 'object.__xor__(self, other)\n' |
| 9515 | 'object.__or__(self, other)\n' |
| 9516 | '\n' |
| 9517 | ' These methods are called to implement the binary ' |
| 9518 | 'arithmetic\n' |
| 9519 | ' operations ("+", "-", "*", "@", "/", "//", "%", ' |
| 9520 | '"divmod()",\n' |
| 9521 | ' "pow()", "**", "<<", ">>", "&", "^", "|"). For instance, ' |
| 9522 | 'to\n' |
| 9523 | ' evaluate the expression "x + y", where *x* is an instance ' |
| 9524 | 'of a\n' |
| 9525 | ' class that has an "__add__()" method, "x.__add__(y)" is ' |
| 9526 | 'called.\n' |
| 9527 | ' The "__divmod__()" method should be the equivalent to ' |
| 9528 | 'using\n' |
| 9529 | ' "__floordiv__()" and "__mod__()"; it should not be ' |
| 9530 | 'related to\n' |
| 9531 | ' "__truediv__()". Note that "__pow__()" should be defined ' |
| 9532 | 'to accept\n' |
| 9533 | ' an optional third argument if the ternary version of the ' |
| 9534 | 'built-in\n' |
| 9535 | ' "pow()" function is to be supported.\n' |
| 9536 | '\n' |
| 9537 | ' If one of those methods does not support the operation ' |
| 9538 | 'with the\n' |
| 9539 | ' supplied arguments, it should return "NotImplemented".\n' |
| 9540 | '\n' |
| 9541 | 'object.__radd__(self, other)\n' |
| 9542 | 'object.__rsub__(self, other)\n' |
| 9543 | 'object.__rmul__(self, other)\n' |
| 9544 | 'object.__rmatmul__(self, other)\n' |
| 9545 | 'object.__rtruediv__(self, other)\n' |
| 9546 | 'object.__rfloordiv__(self, other)\n' |
| 9547 | 'object.__rmod__(self, other)\n' |
| 9548 | 'object.__rdivmod__(self, other)\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 9549 | 'object.__rpow__(self, other[, modulo])\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9550 | 'object.__rlshift__(self, other)\n' |
| 9551 | 'object.__rrshift__(self, other)\n' |
| 9552 | 'object.__rand__(self, other)\n' |
| 9553 | 'object.__rxor__(self, other)\n' |
| 9554 | 'object.__ror__(self, other)\n' |
| 9555 | '\n' |
| 9556 | ' These methods are called to implement the binary ' |
| 9557 | 'arithmetic\n' |
| 9558 | ' operations ("+", "-", "*", "@", "/", "//", "%", ' |
| 9559 | '"divmod()",\n' |
| 9560 | ' "pow()", "**", "<<", ">>", "&", "^", "|") with reflected ' |
| 9561 | '(swapped)\n' |
| 9562 | ' operands. These functions are only called if the left ' |
| 9563 | 'operand does\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 9564 | ' not support the corresponding operation [3] and the ' |
| 9565 | 'operands are of\n' |
| 9566 | ' different types. [4] For instance, to evaluate the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9567 | 'expression "x -\n' |
| 9568 | ' y", where *y* is an instance of a class that has an ' |
| 9569 | '"__rsub__()"\n' |
| 9570 | ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" ' |
| 9571 | 'returns\n' |
| 9572 | ' *NotImplemented*.\n' |
| 9573 | '\n' |
| 9574 | ' Note that ternary "pow()" will not try calling ' |
| 9575 | '"__rpow__()" (the\n' |
| 9576 | ' coercion rules would become too complicated).\n' |
| 9577 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9578 | ' Note: If the right operand’s type is a subclass of the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9579 | 'left\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9580 | ' operand’s type and that subclass provides the reflected ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9581 | 'method\n' |
| 9582 | ' for the operation, this method will be called before ' |
| 9583 | 'the left\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9584 | ' operand’s non-reflected method. This behavior allows ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9585 | 'subclasses\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9586 | ' to override their ancestors’ operations.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9587 | '\n' |
| 9588 | 'object.__iadd__(self, other)\n' |
| 9589 | 'object.__isub__(self, other)\n' |
| 9590 | 'object.__imul__(self, other)\n' |
| 9591 | 'object.__imatmul__(self, other)\n' |
| 9592 | 'object.__itruediv__(self, other)\n' |
| 9593 | 'object.__ifloordiv__(self, other)\n' |
| 9594 | 'object.__imod__(self, other)\n' |
| 9595 | 'object.__ipow__(self, other[, modulo])\n' |
| 9596 | 'object.__ilshift__(self, other)\n' |
| 9597 | 'object.__irshift__(self, other)\n' |
| 9598 | 'object.__iand__(self, other)\n' |
| 9599 | 'object.__ixor__(self, other)\n' |
| 9600 | 'object.__ior__(self, other)\n' |
| 9601 | '\n' |
| 9602 | ' These methods are called to implement the augmented ' |
| 9603 | 'arithmetic\n' |
| 9604 | ' assignments ("+=", "-=", "*=", "@=", "/=", "//=", "%=", ' |
| 9605 | '"**=",\n' |
| 9606 | ' "<<=", ">>=", "&=", "^=", "|="). These methods should ' |
| 9607 | 'attempt to\n' |
| 9608 | ' do the operation in-place (modifying *self*) and return ' |
| 9609 | 'the result\n' |
| 9610 | ' (which could be, but does not have to be, *self*). If a ' |
| 9611 | 'specific\n' |
| 9612 | ' method is not defined, the augmented assignment falls ' |
| 9613 | 'back to the\n' |
| 9614 | ' normal methods. For instance, if *x* is an instance of a ' |
| 9615 | 'class\n' |
| 9616 | ' with an "__iadd__()" method, "x += y" is equivalent to "x ' |
| 9617 | '=\n' |
| 9618 | ' x.__iadd__(y)" . Otherwise, "x.__add__(y)" and ' |
| 9619 | '"y.__radd__(x)" are\n' |
| 9620 | ' considered, as with the evaluation of "x + y". In ' |
| 9621 | 'certain\n' |
| 9622 | ' situations, augmented assignment can result in unexpected ' |
| 9623 | 'errors\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9624 | ' (see Why does a_tuple[i] += [‘item’] raise an exception ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9625 | 'when the\n' |
| 9626 | ' addition works?), but this behavior is in fact part of ' |
| 9627 | 'the data\n' |
| 9628 | ' model.\n' |
| 9629 | '\n' |
| 9630 | 'object.__neg__(self)\n' |
| 9631 | 'object.__pos__(self)\n' |
| 9632 | 'object.__abs__(self)\n' |
| 9633 | 'object.__invert__(self)\n' |
| 9634 | '\n' |
| 9635 | ' Called to implement the unary arithmetic operations ("-", ' |
| 9636 | '"+",\n' |
| 9637 | ' "abs()" and "~").\n' |
| 9638 | '\n' |
| 9639 | 'object.__complex__(self)\n' |
| 9640 | 'object.__int__(self)\n' |
| 9641 | 'object.__float__(self)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9642 | '\n' |
| 9643 | ' Called to implement the built-in functions "complex()", ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9644 | '"int()" and\n' |
| 9645 | ' "float()". Should return a value of the appropriate ' |
| 9646 | 'type.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9647 | '\n' |
| 9648 | 'object.__index__(self)\n' |
| 9649 | '\n' |
| 9650 | ' Called to implement "operator.index()", and whenever ' |
| 9651 | 'Python needs\n' |
| 9652 | ' to losslessly convert the numeric object to an integer ' |
| 9653 | 'object (such\n' |
| 9654 | ' as in slicing, or in the built-in "bin()", "hex()" and ' |
| 9655 | '"oct()"\n' |
| 9656 | ' functions). Presence of this method indicates that the ' |
| 9657 | 'numeric\n' |
| 9658 | ' object is an integer type. Must return an integer.\n' |
| 9659 | '\n' |
Łukasz Langa | 3b5deb0 | 2019-06-04 19:44:34 +0200 | [diff] [blame] | 9660 | ' If "__int__()", "__float__()" and "__complex__()" are not ' |
| 9661 | 'defined\n' |
| 9662 | ' then corresponding built-in functions "int()", "float()" ' |
| 9663 | 'and\n' |
| 9664 | ' "complex()" fall back to "__index__()".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9665 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9666 | 'object.__round__(self[, ndigits])\n' |
| 9667 | 'object.__trunc__(self)\n' |
| 9668 | 'object.__floor__(self)\n' |
| 9669 | 'object.__ceil__(self)\n' |
| 9670 | '\n' |
| 9671 | ' Called to implement the built-in function "round()" and ' |
| 9672 | '"math"\n' |
| 9673 | ' functions "trunc()", "floor()" and "ceil()". Unless ' |
| 9674 | '*ndigits* is\n' |
| 9675 | ' passed to "__round__()" all these methods should return ' |
| 9676 | 'the value\n' |
| 9677 | ' of the object truncated to an "Integral" (typically an ' |
| 9678 | '"int").\n' |
| 9679 | '\n' |
| 9680 | ' If "__int__()" is not defined then the built-in function ' |
| 9681 | '"int()"\n' |
| 9682 | ' falls back to "__trunc__()".\n' |
| 9683 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9684 | '\n' |
| 9685 | 'With Statement Context Managers\n' |
| 9686 | '===============================\n' |
| 9687 | '\n' |
| 9688 | 'A *context manager* is an object that defines the runtime ' |
| 9689 | 'context to\n' |
| 9690 | 'be established when executing a "with" statement. The ' |
| 9691 | 'context manager\n' |
| 9692 | 'handles the entry into, and the exit from, the desired ' |
| 9693 | 'runtime context\n' |
| 9694 | 'for the execution of the block of code. Context managers ' |
| 9695 | 'are normally\n' |
| 9696 | 'invoked using the "with" statement (described in section The ' |
| 9697 | 'with\n' |
| 9698 | 'statement), but can also be used by directly invoking their ' |
| 9699 | 'methods.\n' |
| 9700 | '\n' |
| 9701 | 'Typical uses of context managers include saving and ' |
| 9702 | 'restoring various\n' |
| 9703 | 'kinds of global state, locking and unlocking resources, ' |
| 9704 | 'closing opened\n' |
| 9705 | 'files, etc.\n' |
| 9706 | '\n' |
| 9707 | 'For more information on context managers, see Context ' |
| 9708 | 'Manager Types.\n' |
| 9709 | '\n' |
| 9710 | 'object.__enter__(self)\n' |
| 9711 | '\n' |
| 9712 | ' Enter the runtime context related to this object. The ' |
| 9713 | '"with"\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9714 | ' statement will bind this method’s return value to the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9715 | 'target(s)\n' |
| 9716 | ' specified in the "as" clause of the statement, if any.\n' |
| 9717 | '\n' |
| 9718 | 'object.__exit__(self, exc_type, exc_value, traceback)\n' |
| 9719 | '\n' |
| 9720 | ' Exit the runtime context related to this object. The ' |
| 9721 | 'parameters\n' |
| 9722 | ' describe the exception that caused the context to be ' |
| 9723 | 'exited. If the\n' |
| 9724 | ' context was exited without an exception, all three ' |
| 9725 | 'arguments will\n' |
| 9726 | ' be "None".\n' |
| 9727 | '\n' |
| 9728 | ' If an exception is supplied, and the method wishes to ' |
| 9729 | 'suppress the\n' |
| 9730 | ' exception (i.e., prevent it from being propagated), it ' |
| 9731 | 'should\n' |
| 9732 | ' return a true value. Otherwise, the exception will be ' |
| 9733 | 'processed\n' |
| 9734 | ' normally upon exit from this method.\n' |
| 9735 | '\n' |
| 9736 | ' Note that "__exit__()" methods should not reraise the ' |
| 9737 | 'passed-in\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9738 | ' exception; this is the caller’s responsibility.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9739 | '\n' |
| 9740 | 'See also:\n' |
| 9741 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9742 | ' **PEP 343** - The “with” statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9743 | ' The specification, background, and examples for the ' |
| 9744 | 'Python "with"\n' |
| 9745 | ' statement.\n' |
| 9746 | '\n' |
| 9747 | '\n' |
| 9748 | 'Special method lookup\n' |
| 9749 | '=====================\n' |
| 9750 | '\n' |
| 9751 | 'For custom classes, implicit invocations of special methods ' |
| 9752 | 'are only\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9753 | 'guaranteed to work correctly if defined on an object’s type, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9754 | 'not in\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9755 | 'the object’s instance dictionary. That behaviour is the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9756 | 'reason why\n' |
| 9757 | 'the following code raises an exception:\n' |
| 9758 | '\n' |
| 9759 | ' >>> class C:\n' |
| 9760 | ' ... pass\n' |
| 9761 | ' ...\n' |
| 9762 | ' >>> c = C()\n' |
| 9763 | ' >>> c.__len__ = lambda: 5\n' |
| 9764 | ' >>> len(c)\n' |
| 9765 | ' Traceback (most recent call last):\n' |
| 9766 | ' File "<stdin>", line 1, in <module>\n' |
| 9767 | " TypeError: object of type 'C' has no len()\n" |
| 9768 | '\n' |
| 9769 | 'The rationale behind this behaviour lies with a number of ' |
| 9770 | 'special\n' |
| 9771 | 'methods such as "__hash__()" and "__repr__()" that are ' |
| 9772 | 'implemented by\n' |
| 9773 | 'all objects, including type objects. If the implicit lookup ' |
| 9774 | 'of these\n' |
| 9775 | 'methods used the conventional lookup process, they would ' |
| 9776 | 'fail when\n' |
| 9777 | 'invoked on the type object itself:\n' |
| 9778 | '\n' |
| 9779 | ' >>> 1 .__hash__() == hash(1)\n' |
| 9780 | ' True\n' |
| 9781 | ' >>> int.__hash__() == hash(int)\n' |
| 9782 | ' Traceback (most recent call last):\n' |
| 9783 | ' File "<stdin>", line 1, in <module>\n' |
| 9784 | " TypeError: descriptor '__hash__' of 'int' object needs an " |
| 9785 | 'argument\n' |
| 9786 | '\n' |
| 9787 | 'Incorrectly attempting to invoke an unbound method of a ' |
| 9788 | 'class in this\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9789 | 'way is sometimes referred to as ‘metaclass confusion’, and ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9790 | 'is avoided\n' |
| 9791 | 'by bypassing the instance when looking up special methods:\n' |
| 9792 | '\n' |
| 9793 | ' >>> type(1).__hash__(1) == hash(1)\n' |
| 9794 | ' True\n' |
| 9795 | ' >>> type(int).__hash__(int) == hash(int)\n' |
| 9796 | ' True\n' |
| 9797 | '\n' |
| 9798 | 'In addition to bypassing any instance attributes in the ' |
| 9799 | 'interest of\n' |
| 9800 | 'correctness, implicit special method lookup generally also ' |
| 9801 | 'bypasses\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 9802 | 'the "__getattribute__()" method even of the object’s ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9803 | 'metaclass:\n' |
| 9804 | '\n' |
| 9805 | ' >>> class Meta(type):\n' |
| 9806 | ' ... def __getattribute__(*args):\n' |
| 9807 | ' ... print("Metaclass getattribute invoked")\n' |
| 9808 | ' ... return type.__getattribute__(*args)\n' |
| 9809 | ' ...\n' |
| 9810 | ' >>> class C(object, metaclass=Meta):\n' |
| 9811 | ' ... def __len__(self):\n' |
| 9812 | ' ... return 10\n' |
| 9813 | ' ... def __getattribute__(*args):\n' |
| 9814 | ' ... print("Class getattribute invoked")\n' |
| 9815 | ' ... return object.__getattribute__(*args)\n' |
| 9816 | ' ...\n' |
| 9817 | ' >>> c = C()\n' |
| 9818 | ' >>> c.__len__() # Explicit lookup via ' |
| 9819 | 'instance\n' |
| 9820 | ' Class getattribute invoked\n' |
| 9821 | ' 10\n' |
| 9822 | ' >>> type(c).__len__(c) # Explicit lookup via ' |
| 9823 | 'type\n' |
| 9824 | ' Metaclass getattribute invoked\n' |
| 9825 | ' 10\n' |
| 9826 | ' >>> len(c) # Implicit lookup\n' |
| 9827 | ' 10\n' |
| 9828 | '\n' |
| 9829 | 'Bypassing the "__getattribute__()" machinery in this fashion ' |
| 9830 | 'provides\n' |
| 9831 | 'significant scope for speed optimisations within the ' |
| 9832 | 'interpreter, at\n' |
| 9833 | 'the cost of some flexibility in the handling of special ' |
| 9834 | 'methods (the\n' |
| 9835 | 'special method *must* be set on the class object itself in ' |
| 9836 | 'order to be\n' |
| 9837 | 'consistently invoked by the interpreter).\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 9838 | 'string-methods': 'String Methods\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9839 | '**************\n' |
| 9840 | '\n' |
| 9841 | 'Strings implement all of the common sequence operations, ' |
| 9842 | 'along with\n' |
| 9843 | 'the additional methods described below.\n' |
| 9844 | '\n' |
| 9845 | 'Strings also support two styles of string formatting, one ' |
| 9846 | 'providing a\n' |
| 9847 | 'large degree of flexibility and customization (see ' |
| 9848 | '"str.format()",\n' |
| 9849 | 'Format String Syntax and Custom String Formatting) and the ' |
| 9850 | 'other based\n' |
| 9851 | 'on C "printf" style formatting that handles a narrower ' |
| 9852 | 'range of types\n' |
| 9853 | 'and is slightly harder to use correctly, but is often ' |
| 9854 | 'faster for the\n' |
| 9855 | 'cases it can handle (printf-style String Formatting).\n' |
| 9856 | '\n' |
| 9857 | 'The Text Processing Services section of the standard ' |
| 9858 | 'library covers a\n' |
| 9859 | 'number of other modules that provide various text related ' |
| 9860 | 'utilities\n' |
| 9861 | '(including regular expression support in the "re" ' |
| 9862 | 'module).\n' |
| 9863 | '\n' |
| 9864 | 'str.capitalize()\n' |
| 9865 | '\n' |
| 9866 | ' Return a copy of the string with its first character ' |
| 9867 | 'capitalized\n' |
| 9868 | ' and the rest lowercased.\n' |
| 9869 | '\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 9870 | ' Changed in version 3.8: The first character is now put ' |
| 9871 | 'into\n' |
| 9872 | ' titlecase rather than uppercase. This means that ' |
| 9873 | 'characters like\n' |
| 9874 | ' digraphs will only have their first letter capitalized, ' |
| 9875 | 'instead of\n' |
| 9876 | ' the full character.\n' |
| 9877 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9878 | 'str.casefold()\n' |
| 9879 | '\n' |
| 9880 | ' Return a casefolded copy of the string. Casefolded ' |
| 9881 | 'strings may be\n' |
| 9882 | ' used for caseless matching.\n' |
| 9883 | '\n' |
| 9884 | ' Casefolding is similar to lowercasing but more ' |
| 9885 | 'aggressive because\n' |
| 9886 | ' it is intended to remove all case distinctions in a ' |
| 9887 | 'string. For\n' |
| 9888 | ' example, the German lowercase letter "\'ß\'" is ' |
| 9889 | 'equivalent to ""ss"".\n' |
| 9890 | ' Since it is already lowercase, "lower()" would do ' |
| 9891 | 'nothing to "\'ß\'";\n' |
| 9892 | ' "casefold()" converts it to ""ss"".\n' |
| 9893 | '\n' |
| 9894 | ' The casefolding algorithm is described in section 3.13 ' |
| 9895 | 'of the\n' |
| 9896 | ' Unicode Standard.\n' |
| 9897 | '\n' |
| 9898 | ' New in version 3.3.\n' |
| 9899 | '\n' |
| 9900 | 'str.center(width[, fillchar])\n' |
| 9901 | '\n' |
| 9902 | ' Return centered in a string of length *width*. Padding ' |
| 9903 | 'is done\n' |
| 9904 | ' using the specified *fillchar* (default is an ASCII ' |
| 9905 | 'space). The\n' |
| 9906 | ' original string is returned if *width* is less than or ' |
| 9907 | 'equal to\n' |
| 9908 | ' "len(s)".\n' |
| 9909 | '\n' |
| 9910 | 'str.count(sub[, start[, end]])\n' |
| 9911 | '\n' |
| 9912 | ' Return the number of non-overlapping occurrences of ' |
| 9913 | 'substring *sub*\n' |
| 9914 | ' in the range [*start*, *end*]. Optional arguments ' |
| 9915 | '*start* and\n' |
| 9916 | ' *end* are interpreted as in slice notation.\n' |
| 9917 | '\n' |
Łukasz Langa | bc1c8af | 2020-04-27 22:44:04 +0200 | [diff] [blame] | 9918 | 'str.removeprefix(prefix, /)\n' |
| 9919 | '\n' |
| 9920 | ' If the string starts with the *prefix* string, return\n' |
| 9921 | ' "string[len(prefix):]". Otherwise, return a copy of the ' |
| 9922 | 'original\n' |
| 9923 | ' string:\n' |
| 9924 | '\n' |
| 9925 | " >>> 'TestHook'.removeprefix('Test')\n" |
| 9926 | " 'Hook'\n" |
| 9927 | " >>> 'BaseTestCase'.removeprefix('Test')\n" |
| 9928 | " 'BaseTestCase'\n" |
| 9929 | '\n' |
| 9930 | ' New in version 3.9.\n' |
| 9931 | '\n' |
| 9932 | 'str.removesuffix(suffix, /)\n' |
| 9933 | '\n' |
| 9934 | ' If the string ends with the *suffix* string and that ' |
| 9935 | '*suffix* is\n' |
| 9936 | ' not empty, return "string[:-len(suffix)]". Otherwise, ' |
| 9937 | 'return a copy\n' |
| 9938 | ' of the original string:\n' |
| 9939 | '\n' |
| 9940 | " >>> 'MiscTests'.removesuffix('Tests')\n" |
| 9941 | " 'Misc'\n" |
| 9942 | " >>> 'TmpDirMixin'.removesuffix('Tests')\n" |
| 9943 | " 'TmpDirMixin'\n" |
| 9944 | '\n' |
| 9945 | ' New in version 3.9.\n' |
| 9946 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9947 | 'str.encode(encoding="utf-8", errors="strict")\n' |
| 9948 | '\n' |
| 9949 | ' Return an encoded version of the string as a bytes ' |
| 9950 | 'object. Default\n' |
| 9951 | ' encoding is "\'utf-8\'". *errors* may be given to set a ' |
| 9952 | 'different\n' |
| 9953 | ' error handling scheme. The default for *errors* is ' |
| 9954 | '"\'strict\'",\n' |
| 9955 | ' meaning that encoding errors raise a "UnicodeError". ' |
| 9956 | 'Other possible\n' |
| 9957 | ' values are "\'ignore\'", "\'replace\'", ' |
| 9958 | '"\'xmlcharrefreplace\'",\n' |
| 9959 | ' "\'backslashreplace\'" and any other name registered ' |
| 9960 | 'via\n' |
| 9961 | ' "codecs.register_error()", see section Error Handlers. ' |
| 9962 | 'For a list\n' |
| 9963 | ' of possible encodings, see section Standard Encodings.\n' |
| 9964 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 9965 | ' By default, the *errors* argument is not checked for ' |
| 9966 | 'best\n' |
| 9967 | ' performances, but only used at the first encoding ' |
| 9968 | 'error. Enable the\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 9969 | ' Python Development Mode, or use a debug build to check ' |
| 9970 | '*errors*.\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 9971 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9972 | ' Changed in version 3.1: Support for keyword arguments ' |
| 9973 | 'added.\n' |
| 9974 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 9975 | ' Changed in version 3.9: The *errors* is now checked in ' |
| 9976 | 'development\n' |
| 9977 | ' mode and in debug mode.\n' |
| 9978 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 9979 | 'str.endswith(suffix[, start[, end]])\n' |
| 9980 | '\n' |
| 9981 | ' Return "True" if the string ends with the specified ' |
| 9982 | '*suffix*,\n' |
| 9983 | ' otherwise return "False". *suffix* can also be a tuple ' |
| 9984 | 'of suffixes\n' |
| 9985 | ' to look for. With optional *start*, test beginning at ' |
| 9986 | 'that\n' |
| 9987 | ' position. With optional *end*, stop comparing at that ' |
| 9988 | 'position.\n' |
| 9989 | '\n' |
| 9990 | 'str.expandtabs(tabsize=8)\n' |
| 9991 | '\n' |
| 9992 | ' Return a copy of the string where all tab characters ' |
| 9993 | 'are replaced\n' |
| 9994 | ' by one or more spaces, depending on the current column ' |
| 9995 | 'and the\n' |
| 9996 | ' given tab size. Tab positions occur every *tabsize* ' |
| 9997 | 'characters\n' |
| 9998 | ' (default is 8, giving tab positions at columns 0, 8, 16 ' |
| 9999 | 'and so on).\n' |
| 10000 | ' To expand the string, the current column is set to zero ' |
| 10001 | 'and the\n' |
| 10002 | ' string is examined character by character. If the ' |
| 10003 | 'character is a\n' |
| 10004 | ' tab ("\\t"), one or more space characters are inserted ' |
| 10005 | 'in the result\n' |
| 10006 | ' until the current column is equal to the next tab ' |
| 10007 | 'position. (The\n' |
| 10008 | ' tab character itself is not copied.) If the character ' |
| 10009 | 'is a newline\n' |
| 10010 | ' ("\\n") or return ("\\r"), it is copied and the current ' |
| 10011 | 'column is\n' |
| 10012 | ' reset to zero. Any other character is copied unchanged ' |
| 10013 | 'and the\n' |
| 10014 | ' current column is incremented by one regardless of how ' |
| 10015 | 'the\n' |
| 10016 | ' character is represented when printed.\n' |
| 10017 | '\n' |
| 10018 | " >>> '01\\t012\\t0123\\t01234'.expandtabs()\n" |
| 10019 | " '01 012 0123 01234'\n" |
| 10020 | " >>> '01\\t012\\t0123\\t01234'.expandtabs(4)\n" |
| 10021 | " '01 012 0123 01234'\n" |
| 10022 | '\n' |
| 10023 | 'str.find(sub[, start[, end]])\n' |
| 10024 | '\n' |
| 10025 | ' Return the lowest index in the string where substring ' |
| 10026 | '*sub* is\n' |
| 10027 | ' found within the slice "s[start:end]". Optional ' |
| 10028 | 'arguments *start*\n' |
| 10029 | ' and *end* are interpreted as in slice notation. Return ' |
| 10030 | '"-1" if\n' |
| 10031 | ' *sub* is not found.\n' |
| 10032 | '\n' |
| 10033 | ' Note: The "find()" method should be used only if you ' |
| 10034 | 'need to know\n' |
| 10035 | ' the position of *sub*. To check if *sub* is a ' |
| 10036 | 'substring or not,\n' |
| 10037 | ' use the "in" operator:\n' |
| 10038 | '\n' |
| 10039 | " >>> 'Py' in 'Python'\n" |
| 10040 | ' True\n' |
| 10041 | '\n' |
| 10042 | 'str.format(*args, **kwargs)\n' |
| 10043 | '\n' |
| 10044 | ' Perform a string formatting operation. The string on ' |
| 10045 | 'which this\n' |
| 10046 | ' method is called can contain literal text or ' |
| 10047 | 'replacement fields\n' |
| 10048 | ' delimited by braces "{}". Each replacement field ' |
| 10049 | 'contains either\n' |
| 10050 | ' the numeric index of a positional argument, or the name ' |
| 10051 | 'of a\n' |
| 10052 | ' keyword argument. Returns a copy of the string where ' |
| 10053 | 'each\n' |
| 10054 | ' replacement field is replaced with the string value of ' |
| 10055 | 'the\n' |
| 10056 | ' corresponding argument.\n' |
| 10057 | '\n' |
| 10058 | ' >>> "The sum of 1 + 2 is {0}".format(1+2)\n' |
| 10059 | " 'The sum of 1 + 2 is 3'\n" |
| 10060 | '\n' |
| 10061 | ' See Format String Syntax for a description of the ' |
| 10062 | 'various\n' |
| 10063 | ' formatting options that can be specified in format ' |
| 10064 | 'strings.\n' |
| 10065 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 10066 | ' Note: When formatting a number ("int", "float", ' |
| 10067 | '"complex",\n' |
| 10068 | ' "decimal.Decimal" and subclasses) with the "n" type ' |
| 10069 | '(ex:\n' |
| 10070 | ' "\'{:n}\'.format(1234)"), the function temporarily ' |
| 10071 | 'sets the\n' |
| 10072 | ' "LC_CTYPE" locale to the "LC_NUMERIC" locale to ' |
| 10073 | 'decode\n' |
| 10074 | ' "decimal_point" and "thousands_sep" fields of ' |
| 10075 | '"localeconv()" if\n' |
| 10076 | ' they are non-ASCII or longer than 1 byte, and the ' |
| 10077 | '"LC_NUMERIC"\n' |
| 10078 | ' locale is different than the "LC_CTYPE" locale. This ' |
| 10079 | 'temporary\n' |
| 10080 | ' change affects other threads.\n' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 10081 | '\n' |
| 10082 | ' Changed in version 3.7: When formatting a number with ' |
| 10083 | 'the "n" type,\n' |
| 10084 | ' the function sets temporarily the "LC_CTYPE" locale to ' |
| 10085 | 'the\n' |
| 10086 | ' "LC_NUMERIC" locale in some cases.\n' |
| 10087 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10088 | 'str.format_map(mapping)\n' |
| 10089 | '\n' |
| 10090 | ' Similar to "str.format(**mapping)", except that ' |
| 10091 | '"mapping" is used\n' |
| 10092 | ' directly and not copied to a "dict". This is useful if ' |
| 10093 | 'for example\n' |
| 10094 | ' "mapping" is a dict subclass:\n' |
| 10095 | '\n' |
| 10096 | ' >>> class Default(dict):\n' |
| 10097 | ' ... def __missing__(self, key):\n' |
| 10098 | ' ... return key\n' |
| 10099 | ' ...\n' |
| 10100 | " >>> '{name} was born in " |
| 10101 | "{country}'.format_map(Default(name='Guido'))\n" |
| 10102 | " 'Guido was born in country'\n" |
| 10103 | '\n' |
| 10104 | ' New in version 3.2.\n' |
| 10105 | '\n' |
| 10106 | 'str.index(sub[, start[, end]])\n' |
| 10107 | '\n' |
| 10108 | ' Like "find()", but raise "ValueError" when the ' |
| 10109 | 'substring is not\n' |
| 10110 | ' found.\n' |
| 10111 | '\n' |
| 10112 | 'str.isalnum()\n' |
| 10113 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10114 | ' Return "True" if all characters in the string are ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10115 | 'alphanumeric and\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10116 | ' there is at least one character, "False" otherwise. A ' |
| 10117 | 'character\n' |
| 10118 | ' "c" is alphanumeric if one of the following returns ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10119 | '"True":\n' |
| 10120 | ' "c.isalpha()", "c.isdecimal()", "c.isdigit()", or ' |
| 10121 | '"c.isnumeric()".\n' |
| 10122 | '\n' |
| 10123 | 'str.isalpha()\n' |
| 10124 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10125 | ' Return "True" if all characters in the string are ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10126 | 'alphabetic and\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10127 | ' there is at least one character, "False" otherwise. ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10128 | 'Alphabetic\n' |
| 10129 | ' characters are those characters defined in the Unicode ' |
| 10130 | 'character\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 10131 | ' database as “Letter”, i.e., those with general category ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10132 | 'property\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 10133 | ' being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10134 | 'that this is\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 10135 | ' different from the “Alphabetic” property defined in the ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10136 | 'Unicode\n' |
| 10137 | ' Standard.\n' |
| 10138 | '\n' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 10139 | 'str.isascii()\n' |
| 10140 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10141 | ' Return "True" if the string is empty or all characters ' |
| 10142 | 'in the\n' |
| 10143 | ' string are ASCII, "False" otherwise. ASCII characters ' |
| 10144 | 'have code\n' |
| 10145 | ' points in the range U+0000-U+007F.\n' |
Ned Deily | 6e41cd9 | 2018-01-30 18:48:26 -0500 | [diff] [blame] | 10146 | '\n' |
| 10147 | ' New in version 3.7.\n' |
| 10148 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10149 | 'str.isdecimal()\n' |
| 10150 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10151 | ' Return "True" if all characters in the string are ' |
| 10152 | 'decimal\n' |
| 10153 | ' characters and there is at least one character, "False" ' |
| 10154 | 'otherwise.\n' |
| 10155 | ' Decimal characters are those that can be used to form ' |
| 10156 | 'numbers in\n' |
| 10157 | ' base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. ' |
| 10158 | 'Formally a decimal\n' |
| 10159 | ' character is a character in the Unicode General ' |
| 10160 | 'Category “Nd”.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10161 | '\n' |
| 10162 | 'str.isdigit()\n' |
| 10163 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10164 | ' Return "True" if all characters in the string are ' |
| 10165 | 'digits and there\n' |
| 10166 | ' is at least one character, "False" otherwise. Digits ' |
| 10167 | 'include\n' |
| 10168 | ' decimal characters and digits that need special ' |
| 10169 | 'handling, such as\n' |
| 10170 | ' the compatibility superscript digits. This covers ' |
| 10171 | 'digits which\n' |
| 10172 | ' cannot be used to form numbers in base 10, like the ' |
| 10173 | 'Kharosthi\n' |
| 10174 | ' numbers. Formally, a digit is a character that has the ' |
| 10175 | 'property\n' |
| 10176 | ' value Numeric_Type=Digit or Numeric_Type=Decimal.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10177 | '\n' |
| 10178 | 'str.isidentifier()\n' |
| 10179 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10180 | ' Return "True" if the string is a valid identifier ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10181 | 'according to the\n' |
| 10182 | ' language definition, section Identifiers and keywords.\n' |
| 10183 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 10184 | ' Call "keyword.iskeyword()" to test whether string "s" ' |
| 10185 | 'is a reserved\n' |
| 10186 | ' identifier, such as "def" and "class".\n' |
| 10187 | '\n' |
| 10188 | ' Example:\n' |
| 10189 | '\n' |
| 10190 | ' >>> from keyword import iskeyword\n' |
| 10191 | '\n' |
| 10192 | " >>> 'hello'.isidentifier(), iskeyword('hello')\n" |
| 10193 | ' True, False\n' |
| 10194 | " >>> 'def'.isidentifier(), iskeyword('def')\n" |
| 10195 | ' True, True\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10196 | '\n' |
| 10197 | 'str.islower()\n' |
| 10198 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10199 | ' Return "True" if all cased characters [4] in the string ' |
| 10200 | 'are\n' |
| 10201 | ' lowercase and there is at least one cased character, ' |
| 10202 | '"False"\n' |
| 10203 | ' otherwise.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10204 | '\n' |
| 10205 | 'str.isnumeric()\n' |
| 10206 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10207 | ' Return "True" if all characters in the string are ' |
| 10208 | 'numeric\n' |
| 10209 | ' characters, and there is at least one character, ' |
| 10210 | '"False" otherwise.\n' |
| 10211 | ' Numeric characters include digit characters, and all ' |
| 10212 | 'characters\n' |
| 10213 | ' that have the Unicode numeric value property, e.g. ' |
| 10214 | 'U+2155, VULGAR\n' |
| 10215 | ' FRACTION ONE FIFTH. Formally, numeric characters are ' |
| 10216 | 'those with\n' |
| 10217 | ' the property value Numeric_Type=Digit, ' |
| 10218 | 'Numeric_Type=Decimal or\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10219 | ' Numeric_Type=Numeric.\n' |
| 10220 | '\n' |
| 10221 | 'str.isprintable()\n' |
| 10222 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10223 | ' Return "True" if all characters in the string are ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10224 | 'printable or the\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10225 | ' string is empty, "False" otherwise. Nonprintable ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10226 | 'characters are\n' |
| 10227 | ' those characters defined in the Unicode character ' |
| 10228 | 'database as\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 10229 | ' “Other” or “Separator”, excepting the ASCII space ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10230 | '(0x20) which is\n' |
| 10231 | ' considered printable. (Note that printable characters ' |
| 10232 | 'in this\n' |
| 10233 | ' context are those which should not be escaped when ' |
| 10234 | '"repr()" is\n' |
| 10235 | ' invoked on a string. It has no bearing on the handling ' |
| 10236 | 'of strings\n' |
| 10237 | ' written to "sys.stdout" or "sys.stderr".)\n' |
| 10238 | '\n' |
| 10239 | 'str.isspace()\n' |
| 10240 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10241 | ' Return "True" if there are only whitespace characters ' |
| 10242 | 'in the string\n' |
| 10243 | ' and there is at least one character, "False" ' |
| 10244 | 'otherwise.\n' |
| 10245 | '\n' |
| 10246 | ' A character is *whitespace* if in the Unicode character ' |
| 10247 | 'database\n' |
| 10248 | ' (see "unicodedata"), either its general category is ' |
| 10249 | '"Zs"\n' |
| 10250 | ' (“Separator, space”), or its bidirectional class is one ' |
| 10251 | 'of "WS",\n' |
| 10252 | ' "B", or "S".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10253 | '\n' |
| 10254 | 'str.istitle()\n' |
| 10255 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10256 | ' Return "True" if the string is a titlecased string and ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10257 | 'there is at\n' |
| 10258 | ' least one character, for example uppercase characters ' |
| 10259 | 'may only\n' |
| 10260 | ' follow uncased characters and lowercase characters only ' |
| 10261 | 'cased ones.\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10262 | ' Return "False" otherwise.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10263 | '\n' |
| 10264 | 'str.isupper()\n' |
| 10265 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10266 | ' Return "True" if all cased characters [4] in the string ' |
| 10267 | 'are\n' |
| 10268 | ' uppercase and there is at least one cased character, ' |
| 10269 | '"False"\n' |
| 10270 | ' otherwise.\n' |
| 10271 | '\n' |
| 10272 | " >>> 'BANANA'.isupper()\n" |
| 10273 | ' True\n' |
| 10274 | " >>> 'banana'.isupper()\n" |
| 10275 | ' False\n' |
| 10276 | " >>> 'baNana'.isupper()\n" |
| 10277 | ' False\n' |
| 10278 | " >>> ' '.isupper()\n" |
| 10279 | ' False\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10280 | '\n' |
| 10281 | 'str.join(iterable)\n' |
| 10282 | '\n' |
| 10283 | ' Return a string which is the concatenation of the ' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 10284 | 'strings in\n' |
| 10285 | ' *iterable*. A "TypeError" will be raised if there are ' |
| 10286 | 'any non-\n' |
| 10287 | ' string values in *iterable*, including "bytes" ' |
| 10288 | 'objects. The\n' |
| 10289 | ' separator between elements is the string providing this ' |
| 10290 | 'method.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10291 | '\n' |
| 10292 | 'str.ljust(width[, fillchar])\n' |
| 10293 | '\n' |
| 10294 | ' Return the string left justified in a string of length ' |
| 10295 | '*width*.\n' |
| 10296 | ' Padding is done using the specified *fillchar* (default ' |
| 10297 | 'is an ASCII\n' |
| 10298 | ' space). The original string is returned if *width* is ' |
| 10299 | 'less than or\n' |
| 10300 | ' equal to "len(s)".\n' |
| 10301 | '\n' |
| 10302 | 'str.lower()\n' |
| 10303 | '\n' |
| 10304 | ' Return a copy of the string with all the cased ' |
| 10305 | 'characters [4]\n' |
| 10306 | ' converted to lowercase.\n' |
| 10307 | '\n' |
| 10308 | ' The lowercasing algorithm used is described in section ' |
| 10309 | '3.13 of the\n' |
| 10310 | ' Unicode Standard.\n' |
| 10311 | '\n' |
| 10312 | 'str.lstrip([chars])\n' |
| 10313 | '\n' |
| 10314 | ' Return a copy of the string with leading characters ' |
| 10315 | 'removed. The\n' |
| 10316 | ' *chars* argument is a string specifying the set of ' |
| 10317 | 'characters to be\n' |
| 10318 | ' removed. If omitted or "None", the *chars* argument ' |
| 10319 | 'defaults to\n' |
| 10320 | ' removing whitespace. The *chars* argument is not a ' |
| 10321 | 'prefix; rather,\n' |
| 10322 | ' all combinations of its values are stripped:\n' |
| 10323 | '\n' |
| 10324 | " >>> ' spacious '.lstrip()\n" |
| 10325 | " 'spacious '\n" |
| 10326 | " >>> 'www.example.com'.lstrip('cmowz.')\n" |
| 10327 | " 'example.com'\n" |
| 10328 | '\n' |
Łukasz Langa | bc1c8af | 2020-04-27 22:44:04 +0200 | [diff] [blame] | 10329 | ' See "str.removeprefix()" for a method that will remove ' |
| 10330 | 'a single\n' |
| 10331 | ' prefix string rather than all of a set of characters. ' |
| 10332 | 'For example:\n' |
| 10333 | '\n' |
| 10334 | " >>> 'Arthur: three!'.lstrip('Arthur: ')\n" |
| 10335 | " 'ee!'\n" |
| 10336 | " >>> 'Arthur: three!'.removeprefix('Arthur: ')\n" |
| 10337 | " 'three!'\n" |
| 10338 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10339 | 'static str.maketrans(x[, y[, z]])\n' |
| 10340 | '\n' |
| 10341 | ' This static method returns a translation table usable ' |
| 10342 | 'for\n' |
| 10343 | ' "str.translate()".\n' |
| 10344 | '\n' |
| 10345 | ' If there is only one argument, it must be a dictionary ' |
| 10346 | 'mapping\n' |
| 10347 | ' Unicode ordinals (integers) or characters (strings of ' |
| 10348 | 'length 1) to\n' |
| 10349 | ' Unicode ordinals, strings (of arbitrary lengths) or ' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 10350 | '"None".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10351 | ' Character keys will then be converted to ordinals.\n' |
| 10352 | '\n' |
| 10353 | ' If there are two arguments, they must be strings of ' |
| 10354 | 'equal length,\n' |
| 10355 | ' and in the resulting dictionary, each character in x ' |
| 10356 | 'will be mapped\n' |
| 10357 | ' to the character at the same position in y. If there ' |
| 10358 | 'is a third\n' |
| 10359 | ' argument, it must be a string, whose characters will be ' |
| 10360 | 'mapped to\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 10361 | ' "None" in the result.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10362 | '\n' |
| 10363 | 'str.partition(sep)\n' |
| 10364 | '\n' |
| 10365 | ' Split the string at the first occurrence of *sep*, and ' |
| 10366 | 'return a\n' |
| 10367 | ' 3-tuple containing the part before the separator, the ' |
| 10368 | 'separator\n' |
| 10369 | ' itself, and the part after the separator. If the ' |
| 10370 | 'separator is not\n' |
| 10371 | ' found, return a 3-tuple containing the string itself, ' |
| 10372 | 'followed by\n' |
| 10373 | ' two empty strings.\n' |
| 10374 | '\n' |
| 10375 | 'str.replace(old, new[, count])\n' |
| 10376 | '\n' |
| 10377 | ' Return a copy of the string with all occurrences of ' |
| 10378 | 'substring *old*\n' |
| 10379 | ' replaced by *new*. If the optional argument *count* is ' |
| 10380 | 'given, only\n' |
| 10381 | ' the first *count* occurrences are replaced.\n' |
| 10382 | '\n' |
| 10383 | 'str.rfind(sub[, start[, end]])\n' |
| 10384 | '\n' |
| 10385 | ' Return the highest index in the string where substring ' |
| 10386 | '*sub* is\n' |
| 10387 | ' found, such that *sub* is contained within ' |
| 10388 | '"s[start:end]".\n' |
| 10389 | ' Optional arguments *start* and *end* are interpreted as ' |
| 10390 | 'in slice\n' |
| 10391 | ' notation. Return "-1" on failure.\n' |
| 10392 | '\n' |
| 10393 | 'str.rindex(sub[, start[, end]])\n' |
| 10394 | '\n' |
| 10395 | ' Like "rfind()" but raises "ValueError" when the ' |
| 10396 | 'substring *sub* is\n' |
| 10397 | ' not found.\n' |
| 10398 | '\n' |
| 10399 | 'str.rjust(width[, fillchar])\n' |
| 10400 | '\n' |
| 10401 | ' Return the string right justified in a string of length ' |
| 10402 | '*width*.\n' |
| 10403 | ' Padding is done using the specified *fillchar* (default ' |
| 10404 | 'is an ASCII\n' |
| 10405 | ' space). The original string is returned if *width* is ' |
| 10406 | 'less than or\n' |
| 10407 | ' equal to "len(s)".\n' |
| 10408 | '\n' |
| 10409 | 'str.rpartition(sep)\n' |
| 10410 | '\n' |
| 10411 | ' Split the string at the last occurrence of *sep*, and ' |
| 10412 | 'return a\n' |
| 10413 | ' 3-tuple containing the part before the separator, the ' |
| 10414 | 'separator\n' |
| 10415 | ' itself, and the part after the separator. If the ' |
| 10416 | 'separator is not\n' |
| 10417 | ' found, return a 3-tuple containing two empty strings, ' |
| 10418 | 'followed by\n' |
| 10419 | ' the string itself.\n' |
| 10420 | '\n' |
| 10421 | 'str.rsplit(sep=None, maxsplit=-1)\n' |
| 10422 | '\n' |
| 10423 | ' Return a list of the words in the string, using *sep* ' |
| 10424 | 'as the\n' |
| 10425 | ' delimiter string. If *maxsplit* is given, at most ' |
| 10426 | '*maxsplit* splits\n' |
| 10427 | ' are done, the *rightmost* ones. If *sep* is not ' |
| 10428 | 'specified or\n' |
| 10429 | ' "None", any whitespace string is a separator. Except ' |
| 10430 | 'for splitting\n' |
| 10431 | ' from the right, "rsplit()" behaves like "split()" which ' |
| 10432 | 'is\n' |
| 10433 | ' described in detail below.\n' |
| 10434 | '\n' |
| 10435 | 'str.rstrip([chars])\n' |
| 10436 | '\n' |
| 10437 | ' Return a copy of the string with trailing characters ' |
| 10438 | 'removed. The\n' |
| 10439 | ' *chars* argument is a string specifying the set of ' |
| 10440 | 'characters to be\n' |
| 10441 | ' removed. If omitted or "None", the *chars* argument ' |
| 10442 | 'defaults to\n' |
| 10443 | ' removing whitespace. The *chars* argument is not a ' |
| 10444 | 'suffix; rather,\n' |
| 10445 | ' all combinations of its values are stripped:\n' |
| 10446 | '\n' |
| 10447 | " >>> ' spacious '.rstrip()\n" |
| 10448 | " ' spacious'\n" |
| 10449 | " >>> 'mississippi'.rstrip('ipz')\n" |
| 10450 | " 'mississ'\n" |
| 10451 | '\n' |
Łukasz Langa | bc1c8af | 2020-04-27 22:44:04 +0200 | [diff] [blame] | 10452 | ' See "str.removesuffix()" for a method that will remove ' |
| 10453 | 'a single\n' |
| 10454 | ' suffix string rather than all of a set of characters. ' |
| 10455 | 'For example:\n' |
| 10456 | '\n' |
| 10457 | " >>> 'Monty Python'.rstrip(' Python')\n" |
| 10458 | " 'M'\n" |
| 10459 | " >>> 'Monty Python'.removesuffix(' Python')\n" |
| 10460 | " 'Monty'\n" |
| 10461 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10462 | 'str.split(sep=None, maxsplit=-1)\n' |
| 10463 | '\n' |
| 10464 | ' Return a list of the words in the string, using *sep* ' |
| 10465 | 'as the\n' |
| 10466 | ' delimiter string. If *maxsplit* is given, at most ' |
| 10467 | '*maxsplit*\n' |
| 10468 | ' splits are done (thus, the list will have at most ' |
| 10469 | '"maxsplit+1"\n' |
| 10470 | ' elements). If *maxsplit* is not specified or "-1", ' |
| 10471 | 'then there is\n' |
| 10472 | ' no limit on the number of splits (all possible splits ' |
| 10473 | 'are made).\n' |
| 10474 | '\n' |
| 10475 | ' If *sep* is given, consecutive delimiters are not ' |
| 10476 | 'grouped together\n' |
| 10477 | ' and are deemed to delimit empty strings (for example,\n' |
| 10478 | ' "\'1,,2\'.split(\',\')" returns "[\'1\', \'\', ' |
| 10479 | '\'2\']"). The *sep* argument\n' |
| 10480 | ' may consist of multiple characters (for example,\n' |
| 10481 | ' "\'1<>2<>3\'.split(\'<>\')" returns "[\'1\', \'2\', ' |
| 10482 | '\'3\']"). Splitting an\n' |
| 10483 | ' empty string with a specified separator returns ' |
| 10484 | '"[\'\']".\n' |
| 10485 | '\n' |
| 10486 | ' For example:\n' |
| 10487 | '\n' |
| 10488 | " >>> '1,2,3'.split(',')\n" |
| 10489 | " ['1', '2', '3']\n" |
| 10490 | " >>> '1,2,3'.split(',', maxsplit=1)\n" |
| 10491 | " ['1', '2,3']\n" |
| 10492 | " >>> '1,2,,3,'.split(',')\n" |
| 10493 | " ['1', '2', '', '3', '']\n" |
| 10494 | '\n' |
| 10495 | ' If *sep* is not specified or is "None", a different ' |
| 10496 | 'splitting\n' |
| 10497 | ' algorithm is applied: runs of consecutive whitespace ' |
| 10498 | 'are regarded\n' |
| 10499 | ' as a single separator, and the result will contain no ' |
| 10500 | 'empty strings\n' |
| 10501 | ' at the start or end if the string has leading or ' |
| 10502 | 'trailing\n' |
| 10503 | ' whitespace. Consequently, splitting an empty string or ' |
| 10504 | 'a string\n' |
| 10505 | ' consisting of just whitespace with a "None" separator ' |
| 10506 | 'returns "[]".\n' |
| 10507 | '\n' |
| 10508 | ' For example:\n' |
| 10509 | '\n' |
| 10510 | " >>> '1 2 3'.split()\n" |
| 10511 | " ['1', '2', '3']\n" |
| 10512 | " >>> '1 2 3'.split(maxsplit=1)\n" |
| 10513 | " ['1', '2 3']\n" |
| 10514 | " >>> ' 1 2 3 '.split()\n" |
| 10515 | " ['1', '2', '3']\n" |
| 10516 | '\n' |
| 10517 | 'str.splitlines([keepends])\n' |
| 10518 | '\n' |
| 10519 | ' Return a list of the lines in the string, breaking at ' |
| 10520 | 'line\n' |
| 10521 | ' boundaries. Line breaks are not included in the ' |
| 10522 | 'resulting list\n' |
| 10523 | ' unless *keepends* is given and true.\n' |
| 10524 | '\n' |
| 10525 | ' This method splits on the following line boundaries. ' |
| 10526 | 'In\n' |
| 10527 | ' particular, the boundaries are a superset of *universal ' |
| 10528 | 'newlines*.\n' |
| 10529 | '\n' |
| 10530 | ' ' |
| 10531 | '+-------------------------+-------------------------------+\n' |
| 10532 | ' | Representation | ' |
| 10533 | 'Description |\n' |
| 10534 | ' ' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10535 | '|=========================|===============================|\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10536 | ' | "\\n" | Line ' |
| 10537 | 'Feed |\n' |
| 10538 | ' ' |
| 10539 | '+-------------------------+-------------------------------+\n' |
| 10540 | ' | "\\r" | Carriage ' |
| 10541 | 'Return |\n' |
| 10542 | ' ' |
| 10543 | '+-------------------------+-------------------------------+\n' |
| 10544 | ' | "\\r\\n" | Carriage Return + Line ' |
| 10545 | 'Feed |\n' |
| 10546 | ' ' |
| 10547 | '+-------------------------+-------------------------------+\n' |
| 10548 | ' | "\\v" or "\\x0b" | Line ' |
| 10549 | 'Tabulation |\n' |
| 10550 | ' ' |
| 10551 | '+-------------------------+-------------------------------+\n' |
| 10552 | ' | "\\f" or "\\x0c" | Form ' |
| 10553 | 'Feed |\n' |
| 10554 | ' ' |
| 10555 | '+-------------------------+-------------------------------+\n' |
| 10556 | ' | "\\x1c" | File ' |
| 10557 | 'Separator |\n' |
| 10558 | ' ' |
| 10559 | '+-------------------------+-------------------------------+\n' |
| 10560 | ' | "\\x1d" | Group ' |
| 10561 | 'Separator |\n' |
| 10562 | ' ' |
| 10563 | '+-------------------------+-------------------------------+\n' |
| 10564 | ' | "\\x1e" | Record ' |
| 10565 | 'Separator |\n' |
| 10566 | ' ' |
| 10567 | '+-------------------------+-------------------------------+\n' |
| 10568 | ' | "\\x85" | Next Line (C1 Control ' |
| 10569 | 'Code) |\n' |
| 10570 | ' ' |
| 10571 | '+-------------------------+-------------------------------+\n' |
| 10572 | ' | "\\u2028" | Line ' |
| 10573 | 'Separator |\n' |
| 10574 | ' ' |
| 10575 | '+-------------------------+-------------------------------+\n' |
| 10576 | ' | "\\u2029" | Paragraph ' |
| 10577 | 'Separator |\n' |
| 10578 | ' ' |
| 10579 | '+-------------------------+-------------------------------+\n' |
| 10580 | '\n' |
| 10581 | ' Changed in version 3.2: "\\v" and "\\f" added to list ' |
| 10582 | 'of line\n' |
| 10583 | ' boundaries.\n' |
| 10584 | '\n' |
| 10585 | ' For example:\n' |
| 10586 | '\n' |
| 10587 | " >>> 'ab c\\n\\nde fg\\rkl\\r\\n'.splitlines()\n" |
| 10588 | " ['ab c', '', 'de fg', 'kl']\n" |
| 10589 | " >>> 'ab c\\n\\nde " |
| 10590 | "fg\\rkl\\r\\n'.splitlines(keepends=True)\n" |
| 10591 | " ['ab c\\n', '\\n', 'de fg\\r', 'kl\\r\\n']\n" |
| 10592 | '\n' |
| 10593 | ' Unlike "split()" when a delimiter string *sep* is ' |
| 10594 | 'given, this\n' |
| 10595 | ' method returns an empty list for the empty string, and ' |
| 10596 | 'a terminal\n' |
| 10597 | ' line break does not result in an extra line:\n' |
| 10598 | '\n' |
| 10599 | ' >>> "".splitlines()\n' |
| 10600 | ' []\n' |
| 10601 | ' >>> "One line\\n".splitlines()\n' |
| 10602 | " ['One line']\n" |
| 10603 | '\n' |
| 10604 | ' For comparison, "split(\'\\n\')" gives:\n' |
| 10605 | '\n' |
| 10606 | " >>> ''.split('\\n')\n" |
| 10607 | " ['']\n" |
| 10608 | " >>> 'Two lines\\n'.split('\\n')\n" |
| 10609 | " ['Two lines', '']\n" |
| 10610 | '\n' |
| 10611 | 'str.startswith(prefix[, start[, end]])\n' |
| 10612 | '\n' |
| 10613 | ' Return "True" if string starts with the *prefix*, ' |
| 10614 | 'otherwise return\n' |
| 10615 | ' "False". *prefix* can also be a tuple of prefixes to ' |
| 10616 | 'look for.\n' |
| 10617 | ' With optional *start*, test string beginning at that ' |
| 10618 | 'position.\n' |
| 10619 | ' With optional *end*, stop comparing string at that ' |
| 10620 | 'position.\n' |
| 10621 | '\n' |
| 10622 | 'str.strip([chars])\n' |
| 10623 | '\n' |
| 10624 | ' Return a copy of the string with the leading and ' |
| 10625 | 'trailing\n' |
| 10626 | ' characters removed. The *chars* argument is a string ' |
| 10627 | 'specifying the\n' |
| 10628 | ' set of characters to be removed. If omitted or "None", ' |
| 10629 | 'the *chars*\n' |
| 10630 | ' argument defaults to removing whitespace. The *chars* ' |
| 10631 | 'argument is\n' |
| 10632 | ' not a prefix or suffix; rather, all combinations of its ' |
| 10633 | 'values are\n' |
| 10634 | ' stripped:\n' |
| 10635 | '\n' |
| 10636 | " >>> ' spacious '.strip()\n" |
| 10637 | " 'spacious'\n" |
| 10638 | " >>> 'www.example.com'.strip('cmowz.')\n" |
| 10639 | " 'example'\n" |
| 10640 | '\n' |
| 10641 | ' The outermost leading and trailing *chars* argument ' |
| 10642 | 'values are\n' |
| 10643 | ' stripped from the string. Characters are removed from ' |
| 10644 | 'the leading\n' |
| 10645 | ' end until reaching a string character that is not ' |
| 10646 | 'contained in the\n' |
| 10647 | ' set of characters in *chars*. A similar action takes ' |
| 10648 | 'place on the\n' |
| 10649 | ' trailing end. For example:\n' |
| 10650 | '\n' |
| 10651 | " >>> comment_string = '#....... Section 3.2.1 Issue " |
| 10652 | "#32 .......'\n" |
| 10653 | " >>> comment_string.strip('.#! ')\n" |
| 10654 | " 'Section 3.2.1 Issue #32'\n" |
| 10655 | '\n' |
| 10656 | 'str.swapcase()\n' |
| 10657 | '\n' |
| 10658 | ' Return a copy of the string with uppercase characters ' |
| 10659 | 'converted to\n' |
| 10660 | ' lowercase and vice versa. Note that it is not ' |
| 10661 | 'necessarily true that\n' |
| 10662 | ' "s.swapcase().swapcase() == s".\n' |
| 10663 | '\n' |
| 10664 | 'str.title()\n' |
| 10665 | '\n' |
| 10666 | ' Return a titlecased version of the string where words ' |
| 10667 | 'start with an\n' |
| 10668 | ' uppercase character and the remaining characters are ' |
| 10669 | 'lowercase.\n' |
| 10670 | '\n' |
| 10671 | ' For example:\n' |
| 10672 | '\n' |
| 10673 | " >>> 'Hello world'.title()\n" |
| 10674 | " 'Hello World'\n" |
| 10675 | '\n' |
| 10676 | ' The algorithm uses a simple language-independent ' |
| 10677 | 'definition of a\n' |
| 10678 | ' word as groups of consecutive letters. The definition ' |
| 10679 | 'works in\n' |
| 10680 | ' many contexts but it means that apostrophes in ' |
| 10681 | 'contractions and\n' |
| 10682 | ' possessives form word boundaries, which may not be the ' |
| 10683 | 'desired\n' |
| 10684 | ' result:\n' |
| 10685 | '\n' |
| 10686 | ' >>> "they\'re bill\'s friends from the UK".title()\n' |
| 10687 | ' "They\'Re Bill\'S Friends From The Uk"\n' |
| 10688 | '\n' |
| 10689 | ' A workaround for apostrophes can be constructed using ' |
| 10690 | 'regular\n' |
| 10691 | ' expressions:\n' |
| 10692 | '\n' |
| 10693 | ' >>> import re\n' |
| 10694 | ' >>> def titlecase(s):\n' |
| 10695 | ' ... return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n' |
| 10696 | ' ... lambda mo: ' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 10697 | 'mo.group(0).capitalize(),\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10698 | ' ... s)\n' |
| 10699 | ' ...\n' |
| 10700 | ' >>> titlecase("they\'re bill\'s friends.")\n' |
| 10701 | ' "They\'re Bill\'s Friends."\n' |
| 10702 | '\n' |
| 10703 | 'str.translate(table)\n' |
| 10704 | '\n' |
| 10705 | ' Return a copy of the string in which each character has ' |
| 10706 | 'been mapped\n' |
| 10707 | ' through the given translation table. The table must be ' |
| 10708 | 'an object\n' |
| 10709 | ' that implements indexing via "__getitem__()", typically ' |
| 10710 | 'a *mapping*\n' |
| 10711 | ' or *sequence*. When indexed by a Unicode ordinal (an ' |
| 10712 | 'integer), the\n' |
| 10713 | ' table object can do any of the following: return a ' |
| 10714 | 'Unicode ordinal\n' |
| 10715 | ' or a string, to map the character to one or more other ' |
| 10716 | 'characters;\n' |
| 10717 | ' return "None", to delete the character from the return ' |
| 10718 | 'string; or\n' |
| 10719 | ' raise a "LookupError" exception, to map the character ' |
| 10720 | 'to itself.\n' |
| 10721 | '\n' |
| 10722 | ' You can use "str.maketrans()" to create a translation ' |
| 10723 | 'map from\n' |
| 10724 | ' character-to-character mappings in different formats.\n' |
| 10725 | '\n' |
| 10726 | ' See also the "codecs" module for a more flexible ' |
| 10727 | 'approach to custom\n' |
| 10728 | ' character mappings.\n' |
| 10729 | '\n' |
| 10730 | 'str.upper()\n' |
| 10731 | '\n' |
| 10732 | ' Return a copy of the string with all the cased ' |
| 10733 | 'characters [4]\n' |
| 10734 | ' converted to uppercase. Note that ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 10735 | '"s.upper().isupper()" might be\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10736 | ' "False" if "s" contains uncased characters or if the ' |
| 10737 | 'Unicode\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 10738 | ' category of the resulting character(s) is not “Lu” ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10739 | '(Letter,\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 10740 | ' uppercase), but e.g. “Lt” (Letter, titlecase).\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10741 | '\n' |
| 10742 | ' The uppercasing algorithm used is described in section ' |
| 10743 | '3.13 of the\n' |
| 10744 | ' Unicode Standard.\n' |
| 10745 | '\n' |
| 10746 | 'str.zfill(width)\n' |
| 10747 | '\n' |
| 10748 | ' Return a copy of the string left filled with ASCII ' |
| 10749 | '"\'0\'" digits to\n' |
| 10750 | ' make a string of length *width*. A leading sign prefix\n' |
| 10751 | ' ("\'+\'"/"\'-\'") is handled by inserting the padding ' |
| 10752 | '*after* the sign\n' |
| 10753 | ' character rather than before. The original string is ' |
| 10754 | 'returned if\n' |
| 10755 | ' *width* is less than or equal to "len(s)".\n' |
| 10756 | '\n' |
| 10757 | ' For example:\n' |
| 10758 | '\n' |
| 10759 | ' >>> "42".zfill(5)\n' |
| 10760 | " '00042'\n" |
| 10761 | ' >>> "-42".zfill(5)\n' |
| 10762 | " '-0042'\n", |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 10763 | 'strings': 'String and Bytes literals\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10764 | '*************************\n' |
| 10765 | '\n' |
| 10766 | 'String literals are described by the following lexical ' |
| 10767 | 'definitions:\n' |
| 10768 | '\n' |
| 10769 | ' stringliteral ::= [stringprefix](shortstring | longstring)\n' |
| 10770 | ' stringprefix ::= "r" | "u" | "R" | "U" | "f" | "F"\n' |
| 10771 | ' | "fr" | "Fr" | "fR" | "FR" | "rf" | "rF" | ' |
| 10772 | '"Rf" | "RF"\n' |
| 10773 | ' shortstring ::= "\'" shortstringitem* "\'" | \'"\' ' |
| 10774 | 'shortstringitem* \'"\'\n' |
| 10775 | ' longstring ::= "\'\'\'" longstringitem* "\'\'\'" | ' |
| 10776 | '\'"""\' longstringitem* \'"""\'\n' |
| 10777 | ' shortstringitem ::= shortstringchar | stringescapeseq\n' |
| 10778 | ' longstringitem ::= longstringchar | stringescapeseq\n' |
| 10779 | ' shortstringchar ::= <any source character except "\\" or ' |
| 10780 | 'newline or the quote>\n' |
| 10781 | ' longstringchar ::= <any source character except "\\">\n' |
| 10782 | ' stringescapeseq ::= "\\" <any source character>\n' |
| 10783 | '\n' |
| 10784 | ' bytesliteral ::= bytesprefix(shortbytes | longbytes)\n' |
| 10785 | ' bytesprefix ::= "b" | "B" | "br" | "Br" | "bR" | "BR" | ' |
| 10786 | '"rb" | "rB" | "Rb" | "RB"\n' |
| 10787 | ' shortbytes ::= "\'" shortbytesitem* "\'" | \'"\' ' |
| 10788 | 'shortbytesitem* \'"\'\n' |
| 10789 | ' longbytes ::= "\'\'\'" longbytesitem* "\'\'\'" | \'"""\' ' |
| 10790 | 'longbytesitem* \'"""\'\n' |
| 10791 | ' shortbytesitem ::= shortbyteschar | bytesescapeseq\n' |
| 10792 | ' longbytesitem ::= longbyteschar | bytesescapeseq\n' |
| 10793 | ' shortbyteschar ::= <any ASCII character except "\\" or newline ' |
| 10794 | 'or the quote>\n' |
| 10795 | ' longbyteschar ::= <any ASCII character except "\\">\n' |
| 10796 | ' bytesescapeseq ::= "\\" <any ASCII character>\n' |
| 10797 | '\n' |
| 10798 | 'One syntactic restriction not indicated by these productions is ' |
| 10799 | 'that\n' |
| 10800 | 'whitespace is not allowed between the "stringprefix" or ' |
| 10801 | '"bytesprefix"\n' |
| 10802 | 'and the rest of the literal. The source character set is defined ' |
| 10803 | 'by\n' |
| 10804 | 'the encoding declaration; it is UTF-8 if no encoding declaration ' |
| 10805 | 'is\n' |
| 10806 | 'given in the source file; see section Encoding declarations.\n' |
| 10807 | '\n' |
| 10808 | 'In plain English: Both types of literals can be enclosed in ' |
| 10809 | 'matching\n' |
| 10810 | 'single quotes ("\'") or double quotes ("""). They can also be ' |
| 10811 | 'enclosed\n' |
| 10812 | 'in matching groups of three single or double quotes (these are\n' |
| 10813 | 'generally referred to as *triple-quoted strings*). The ' |
| 10814 | 'backslash\n' |
| 10815 | '("\\") character is used to escape characters that otherwise have ' |
| 10816 | 'a\n' |
| 10817 | 'special meaning, such as newline, backslash itself, or the quote\n' |
| 10818 | 'character.\n' |
| 10819 | '\n' |
| 10820 | 'Bytes literals are always prefixed with "\'b\'" or "\'B\'"; they ' |
| 10821 | 'produce\n' |
| 10822 | 'an instance of the "bytes" type instead of the "str" type. They ' |
| 10823 | 'may\n' |
| 10824 | 'only contain ASCII characters; bytes with a numeric value of 128 ' |
| 10825 | 'or\n' |
| 10826 | 'greater must be expressed with escapes.\n' |
| 10827 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10828 | 'Both string and bytes literals may optionally be prefixed with a\n' |
| 10829 | 'letter "\'r\'" or "\'R\'"; such strings are called *raw strings* ' |
| 10830 | 'and treat\n' |
| 10831 | 'backslashes as literal characters. As a result, in string ' |
| 10832 | 'literals,\n' |
| 10833 | '"\'\\U\'" and "\'\\u\'" escapes in raw strings are not treated ' |
| 10834 | 'specially.\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 10835 | 'Given that Python 2.x’s raw unicode literals behave differently ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10836 | 'than\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 10837 | 'Python 3.x’s the "\'ur\'" syntax is not supported.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10838 | '\n' |
| 10839 | 'New in version 3.3: The "\'rb\'" prefix of raw bytes literals has ' |
| 10840 | 'been\n' |
| 10841 | 'added as a synonym of "\'br\'".\n' |
| 10842 | '\n' |
| 10843 | 'New in version 3.3: Support for the unicode legacy literal\n' |
| 10844 | '("u\'value\'") was reintroduced to simplify the maintenance of ' |
| 10845 | 'dual\n' |
| 10846 | 'Python 2.x and 3.x codebases. See **PEP 414** for more ' |
| 10847 | 'information.\n' |
| 10848 | '\n' |
| 10849 | 'A string literal with "\'f\'" or "\'F\'" in its prefix is a ' |
| 10850 | '*formatted\n' |
| 10851 | 'string literal*; see Formatted string literals. The "\'f\'" may ' |
| 10852 | 'be\n' |
| 10853 | 'combined with "\'r\'", but not with "\'b\'" or "\'u\'", therefore ' |
| 10854 | 'raw\n' |
| 10855 | 'formatted strings are possible, but formatted bytes literals are ' |
| 10856 | 'not.\n' |
| 10857 | '\n' |
| 10858 | 'In triple-quoted literals, unescaped newlines and quotes are ' |
| 10859 | 'allowed\n' |
| 10860 | '(and are retained), except that three unescaped quotes in a row\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 10861 | 'terminate the literal. (A “quote” is the character used to open ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10862 | 'the\n' |
| 10863 | 'literal, i.e. either "\'" or """.)\n' |
| 10864 | '\n' |
| 10865 | 'Unless an "\'r\'" or "\'R\'" prefix is present, escape sequences ' |
| 10866 | 'in string\n' |
| 10867 | 'and bytes literals are interpreted according to rules similar to ' |
| 10868 | 'those\n' |
| 10869 | 'used by Standard C. The recognized escape sequences are:\n' |
| 10870 | '\n' |
| 10871 | '+-------------------+-----------------------------------+---------+\n' |
| 10872 | '| Escape Sequence | Meaning | Notes ' |
| 10873 | '|\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10874 | '|===================|===================================|=========|\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10875 | '| "\\newline" | Backslash and newline ignored ' |
| 10876 | '| |\n' |
| 10877 | '+-------------------+-----------------------------------+---------+\n' |
| 10878 | '| "\\\\" | Backslash ("\\") ' |
| 10879 | '| |\n' |
| 10880 | '+-------------------+-----------------------------------+---------+\n' |
| 10881 | '| "\\\'" | Single quote ("\'") ' |
| 10882 | '| |\n' |
| 10883 | '+-------------------+-----------------------------------+---------+\n' |
| 10884 | '| "\\"" | Double quote (""") ' |
| 10885 | '| |\n' |
| 10886 | '+-------------------+-----------------------------------+---------+\n' |
| 10887 | '| "\\a" | ASCII Bell (BEL) ' |
| 10888 | '| |\n' |
| 10889 | '+-------------------+-----------------------------------+---------+\n' |
| 10890 | '| "\\b" | ASCII Backspace (BS) ' |
| 10891 | '| |\n' |
| 10892 | '+-------------------+-----------------------------------+---------+\n' |
| 10893 | '| "\\f" | ASCII Formfeed (FF) ' |
| 10894 | '| |\n' |
| 10895 | '+-------------------+-----------------------------------+---------+\n' |
| 10896 | '| "\\n" | ASCII Linefeed (LF) ' |
| 10897 | '| |\n' |
| 10898 | '+-------------------+-----------------------------------+---------+\n' |
| 10899 | '| "\\r" | ASCII Carriage Return (CR) ' |
| 10900 | '| |\n' |
| 10901 | '+-------------------+-----------------------------------+---------+\n' |
| 10902 | '| "\\t" | ASCII Horizontal Tab (TAB) ' |
| 10903 | '| |\n' |
| 10904 | '+-------------------+-----------------------------------+---------+\n' |
| 10905 | '| "\\v" | ASCII Vertical Tab (VT) ' |
| 10906 | '| |\n' |
| 10907 | '+-------------------+-----------------------------------+---------+\n' |
| 10908 | '| "\\ooo" | Character with octal value *ooo* | ' |
| 10909 | '(1,3) |\n' |
| 10910 | '+-------------------+-----------------------------------+---------+\n' |
| 10911 | '| "\\xhh" | Character with hex value *hh* | ' |
| 10912 | '(2,3) |\n' |
| 10913 | '+-------------------+-----------------------------------+---------+\n' |
| 10914 | '\n' |
| 10915 | 'Escape sequences only recognized in string literals are:\n' |
| 10916 | '\n' |
| 10917 | '+-------------------+-----------------------------------+---------+\n' |
| 10918 | '| Escape Sequence | Meaning | Notes ' |
| 10919 | '|\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10920 | '|===================|===================================|=========|\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10921 | '| "\\N{name}" | Character named *name* in the | ' |
| 10922 | '(4) |\n' |
| 10923 | '| | Unicode database | ' |
| 10924 | '|\n' |
| 10925 | '+-------------------+-----------------------------------+---------+\n' |
| 10926 | '| "\\uxxxx" | Character with 16-bit hex value | ' |
| 10927 | '(5) |\n' |
| 10928 | '| | *xxxx* | ' |
| 10929 | '|\n' |
| 10930 | '+-------------------+-----------------------------------+---------+\n' |
| 10931 | '| "\\Uxxxxxxxx" | Character with 32-bit hex value | ' |
| 10932 | '(6) |\n' |
| 10933 | '| | *xxxxxxxx* | ' |
| 10934 | '|\n' |
| 10935 | '+-------------------+-----------------------------------+---------+\n' |
| 10936 | '\n' |
| 10937 | 'Notes:\n' |
| 10938 | '\n' |
| 10939 | '1. As in Standard C, up to three octal digits are accepted.\n' |
| 10940 | '\n' |
| 10941 | '2. Unlike in Standard C, exactly two hex digits are required.\n' |
| 10942 | '\n' |
| 10943 | '3. In a bytes literal, hexadecimal and octal escapes denote the\n' |
| 10944 | ' byte with the given value. In a string literal, these escapes\n' |
| 10945 | ' denote a Unicode character with the given value.\n' |
| 10946 | '\n' |
| 10947 | '4. Changed in version 3.3: Support for name aliases [1] has been\n' |
| 10948 | ' added.\n' |
| 10949 | '\n' |
| 10950 | '5. Exactly four hex digits are required.\n' |
| 10951 | '\n' |
| 10952 | '6. Any Unicode character can be encoded this way. Exactly eight\n' |
| 10953 | ' hex digits are required.\n' |
| 10954 | '\n' |
| 10955 | 'Unlike Standard C, all unrecognized escape sequences are left in ' |
| 10956 | 'the\n' |
| 10957 | 'string unchanged, i.e., *the backslash is left in the result*. ' |
| 10958 | '(This\n' |
| 10959 | 'behavior is useful when debugging: if an escape sequence is ' |
| 10960 | 'mistyped,\n' |
| 10961 | 'the resulting output is more easily recognized as broken.) It is ' |
| 10962 | 'also\n' |
| 10963 | 'important to note that the escape sequences only recognized in ' |
| 10964 | 'string\n' |
| 10965 | 'literals fall into the category of unrecognized escapes for ' |
| 10966 | 'bytes\n' |
| 10967 | 'literals.\n' |
| 10968 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 10969 | ' Changed in version 3.6: Unrecognized escape sequences produce ' |
| 10970 | 'a\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10971 | ' "DeprecationWarning". In a future Python version they will be ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 10972 | 'a\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 10973 | ' "SyntaxWarning" and eventually a "SyntaxError".\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 10974 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10975 | 'Even in a raw literal, quotes can be escaped with a backslash, ' |
| 10976 | 'but the\n' |
| 10977 | 'backslash remains in the result; for example, "r"\\""" is a ' |
| 10978 | 'valid\n' |
| 10979 | 'string literal consisting of two characters: a backslash and a ' |
| 10980 | 'double\n' |
| 10981 | 'quote; "r"\\"" is not a valid string literal (even a raw string ' |
| 10982 | 'cannot\n' |
| 10983 | 'end in an odd number of backslashes). Specifically, *a raw ' |
| 10984 | 'literal\n' |
| 10985 | 'cannot end in a single backslash* (since the backslash would ' |
| 10986 | 'escape\n' |
| 10987 | 'the following quote character). Note also that a single ' |
| 10988 | 'backslash\n' |
| 10989 | 'followed by a newline is interpreted as those two characters as ' |
| 10990 | 'part\n' |
| 10991 | 'of the literal, *not* as a line continuation.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 10992 | 'subscriptions': 'Subscriptions\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 10993 | '*************\n' |
| 10994 | '\n' |
| 10995 | 'A subscription selects an item of a sequence (string, tuple ' |
| 10996 | 'or list)\n' |
| 10997 | 'or mapping (dictionary) object:\n' |
| 10998 | '\n' |
| 10999 | ' subscription ::= primary "[" expression_list "]"\n' |
| 11000 | '\n' |
| 11001 | 'The primary must evaluate to an object that supports ' |
| 11002 | 'subscription\n' |
| 11003 | '(lists or dictionaries for example). User-defined objects ' |
| 11004 | 'can support\n' |
| 11005 | 'subscription by defining a "__getitem__()" method.\n' |
| 11006 | '\n' |
| 11007 | 'For built-in objects, there are two types of objects that ' |
| 11008 | 'support\n' |
| 11009 | 'subscription:\n' |
| 11010 | '\n' |
| 11011 | 'If the primary is a mapping, the expression list must ' |
| 11012 | 'evaluate to an\n' |
| 11013 | 'object whose value is one of the keys of the mapping, and ' |
| 11014 | 'the\n' |
| 11015 | 'subscription selects the value in the mapping that ' |
| 11016 | 'corresponds to that\n' |
| 11017 | 'key. (The expression list is a tuple except if it has ' |
| 11018 | 'exactly one\n' |
| 11019 | 'item.)\n' |
| 11020 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11021 | 'If the primary is a sequence, the expression list must ' |
| 11022 | 'evaluate to an\n' |
| 11023 | 'integer or a slice (as discussed in the following ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11024 | 'section).\n' |
| 11025 | '\n' |
| 11026 | 'The formal syntax makes no special provision for negative ' |
| 11027 | 'indices in\n' |
| 11028 | 'sequences; however, built-in sequences all provide a ' |
| 11029 | '"__getitem__()"\n' |
| 11030 | 'method that interprets negative indices by adding the ' |
| 11031 | 'length of the\n' |
| 11032 | 'sequence to the index (so that "x[-1]" selects the last ' |
| 11033 | 'item of "x").\n' |
| 11034 | 'The resulting value must be a nonnegative integer less than ' |
| 11035 | 'the number\n' |
| 11036 | 'of items in the sequence, and the subscription selects the ' |
| 11037 | 'item whose\n' |
| 11038 | 'index is that value (counting from zero). Since the support ' |
| 11039 | 'for\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11040 | 'negative indices and slicing occurs in the object’s ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11041 | '"__getitem__()"\n' |
| 11042 | 'method, subclasses overriding this method will need to ' |
| 11043 | 'explicitly add\n' |
| 11044 | 'that support.\n' |
| 11045 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11046 | 'A string’s items are characters. A character is not a ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11047 | 'separate data\n' |
| 11048 | 'type but a string of exactly one character.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11049 | 'truth': 'Truth Value Testing\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11050 | '*******************\n' |
| 11051 | '\n' |
| 11052 | 'Any object can be tested for truth value, for use in an "if" or\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11053 | '"while" condition or as operand of the Boolean operations below.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11054 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11055 | 'By default, an object is considered true unless its class defines\n' |
| 11056 | 'either a "__bool__()" method that returns "False" or a "__len__()"\n' |
| 11057 | 'method that returns zero, when called with the object. [1] Here ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11058 | 'are\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11059 | 'most of the built-in objects considered false:\n' |
| 11060 | '\n' |
| 11061 | '* constants defined to be false: "None" and "False".\n' |
| 11062 | '\n' |
| 11063 | '* zero of any numeric type: "0", "0.0", "0j", "Decimal(0)",\n' |
| 11064 | ' "Fraction(0, 1)"\n' |
| 11065 | '\n' |
| 11066 | '* empty sequences and collections: "\'\'", "()", "[]", "{}", ' |
| 11067 | '"set()",\n' |
| 11068 | ' "range(0)"\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11069 | '\n' |
| 11070 | 'Operations and built-in functions that have a Boolean result ' |
| 11071 | 'always\n' |
| 11072 | 'return "0" or "False" for false and "1" or "True" for true, unless\n' |
| 11073 | 'otherwise stated. (Important exception: the Boolean operations ' |
| 11074 | '"or"\n' |
| 11075 | 'and "and" always return one of their operands.)\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11076 | 'try': 'The "try" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11077 | '*******************\n' |
| 11078 | '\n' |
| 11079 | 'The "try" statement specifies exception handlers and/or cleanup code\n' |
| 11080 | 'for a group of statements:\n' |
| 11081 | '\n' |
| 11082 | ' try_stmt ::= try1_stmt | try2_stmt\n' |
| 11083 | ' try1_stmt ::= "try" ":" suite\n' |
| 11084 | ' ("except" [expression ["as" identifier]] ":" ' |
| 11085 | 'suite)+\n' |
| 11086 | ' ["else" ":" suite]\n' |
| 11087 | ' ["finally" ":" suite]\n' |
| 11088 | ' try2_stmt ::= "try" ":" suite\n' |
| 11089 | ' "finally" ":" suite\n' |
| 11090 | '\n' |
| 11091 | 'The "except" clause(s) specify one or more exception handlers. When ' |
| 11092 | 'no\n' |
| 11093 | 'exception occurs in the "try" clause, no exception handler is\n' |
| 11094 | 'executed. When an exception occurs in the "try" suite, a search for ' |
| 11095 | 'an\n' |
| 11096 | 'exception handler is started. This search inspects the except ' |
| 11097 | 'clauses\n' |
| 11098 | 'in turn until one is found that matches the exception. An ' |
| 11099 | 'expression-\n' |
| 11100 | 'less except clause, if present, must be last; it matches any\n' |
| 11101 | 'exception. For an except clause with an expression, that expression\n' |
| 11102 | 'is evaluated, and the clause matches the exception if the resulting\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11103 | 'object is “compatible” with the exception. An object is compatible\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11104 | 'with an exception if it is the class or a base class of the ' |
| 11105 | 'exception\n' |
| 11106 | 'object or a tuple containing an item compatible with the exception.\n' |
| 11107 | '\n' |
| 11108 | 'If no except clause matches the exception, the search for an ' |
| 11109 | 'exception\n' |
| 11110 | 'handler continues in the surrounding code and on the invocation ' |
| 11111 | 'stack.\n' |
| 11112 | '[1]\n' |
| 11113 | '\n' |
| 11114 | 'If the evaluation of an expression in the header of an except clause\n' |
| 11115 | 'raises an exception, the original search for a handler is canceled ' |
| 11116 | 'and\n' |
| 11117 | 'a search starts for the new exception in the surrounding code and on\n' |
| 11118 | 'the call stack (it is treated as if the entire "try" statement ' |
| 11119 | 'raised\n' |
| 11120 | 'the exception).\n' |
| 11121 | '\n' |
| 11122 | 'When a matching except clause is found, the exception is assigned to\n' |
| 11123 | 'the target specified after the "as" keyword in that except clause, ' |
| 11124 | 'if\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11125 | 'present, and the except clause’s suite is executed. All except\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11126 | 'clauses must have an executable block. When the end of this block ' |
| 11127 | 'is\n' |
| 11128 | 'reached, execution continues normally after the entire try ' |
| 11129 | 'statement.\n' |
| 11130 | '(This means that if two nested handlers exist for the same ' |
| 11131 | 'exception,\n' |
| 11132 | 'and the exception occurs in the try clause of the inner handler, the\n' |
| 11133 | 'outer handler will not handle the exception.)\n' |
| 11134 | '\n' |
| 11135 | 'When an exception has been assigned using "as target", it is cleared\n' |
| 11136 | 'at the end of the except clause. This is as if\n' |
| 11137 | '\n' |
| 11138 | ' except E as N:\n' |
| 11139 | ' foo\n' |
| 11140 | '\n' |
| 11141 | 'was translated to\n' |
| 11142 | '\n' |
| 11143 | ' except E as N:\n' |
| 11144 | ' try:\n' |
| 11145 | ' foo\n' |
| 11146 | ' finally:\n' |
| 11147 | ' del N\n' |
| 11148 | '\n' |
| 11149 | 'This means the exception must be assigned to a different name to be\n' |
| 11150 | 'able to refer to it after the except clause. Exceptions are cleared\n' |
| 11151 | 'because with the traceback attached to them, they form a reference\n' |
| 11152 | 'cycle with the stack frame, keeping all locals in that frame alive\n' |
| 11153 | 'until the next garbage collection occurs.\n' |
| 11154 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11155 | 'Before an except clause’s suite is executed, details about the\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11156 | 'exception are stored in the "sys" module and can be accessed via\n' |
| 11157 | '"sys.exc_info()". "sys.exc_info()" returns a 3-tuple consisting of ' |
| 11158 | 'the\n' |
| 11159 | 'exception class, the exception instance and a traceback object (see\n' |
| 11160 | 'section The standard type hierarchy) identifying the point in the\n' |
| 11161 | 'program where the exception occurred. "sys.exc_info()" values are\n' |
| 11162 | 'restored to their previous values (before the call) when returning\n' |
| 11163 | 'from a function that handled an exception.\n' |
| 11164 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11165 | 'The optional "else" clause is executed if the control flow leaves ' |
| 11166 | 'the\n' |
| 11167 | '"try" suite, no exception was raised, and no "return", "continue", ' |
| 11168 | 'or\n' |
| 11169 | '"break" statement was executed. Exceptions in the "else" clause are\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11170 | 'not handled by the preceding "except" clauses.\n' |
| 11171 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11172 | 'If "finally" is present, it specifies a ‘cleanup’ handler. The ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11173 | '"try"\n' |
| 11174 | 'clause is executed, including any "except" and "else" clauses. If ' |
| 11175 | 'an\n' |
| 11176 | 'exception occurs in any of the clauses and is not handled, the\n' |
| 11177 | 'exception is temporarily saved. The "finally" clause is executed. ' |
| 11178 | 'If\n' |
| 11179 | 'there is a saved exception it is re-raised at the end of the ' |
| 11180 | '"finally"\n' |
| 11181 | 'clause. If the "finally" clause raises another exception, the saved\n' |
| 11182 | 'exception is set as the context of the new exception. If the ' |
| 11183 | '"finally"\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11184 | 'clause executes a "return", "break" or "continue" statement, the ' |
| 11185 | 'saved\n' |
| 11186 | 'exception is discarded:\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11187 | '\n' |
| 11188 | ' >>> def f():\n' |
| 11189 | ' ... try:\n' |
| 11190 | ' ... 1/0\n' |
| 11191 | ' ... finally:\n' |
| 11192 | ' ... return 42\n' |
| 11193 | ' ...\n' |
| 11194 | ' >>> f()\n' |
| 11195 | ' 42\n' |
| 11196 | '\n' |
| 11197 | 'The exception information is not available to the program during\n' |
| 11198 | 'execution of the "finally" clause.\n' |
| 11199 | '\n' |
| 11200 | 'When a "return", "break" or "continue" statement is executed in the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11201 | '"try" suite of a "try"…"finally" statement, the "finally" clause is\n' |
| 11202 | 'also executed ‘on the way out.’\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11203 | '\n' |
| 11204 | 'The return value of a function is determined by the last "return"\n' |
| 11205 | 'statement executed. Since the "finally" clause always executes, a\n' |
| 11206 | '"return" statement executed in the "finally" clause will always be ' |
| 11207 | 'the\n' |
| 11208 | 'last one executed:\n' |
| 11209 | '\n' |
| 11210 | ' >>> def foo():\n' |
| 11211 | ' ... try:\n' |
| 11212 | " ... return 'try'\n" |
| 11213 | ' ... finally:\n' |
| 11214 | " ... return 'finally'\n" |
| 11215 | ' ...\n' |
| 11216 | ' >>> foo()\n' |
| 11217 | " 'finally'\n" |
| 11218 | '\n' |
| 11219 | 'Additional information on exceptions can be found in section\n' |
| 11220 | 'Exceptions, and information on using the "raise" statement to ' |
| 11221 | 'generate\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11222 | 'exceptions may be found in section The raise statement.\n' |
| 11223 | '\n' |
| 11224 | 'Changed in version 3.8: Prior to Python 3.8, a "continue" statement\n' |
| 11225 | 'was illegal in the "finally" clause due to a problem with the\n' |
| 11226 | 'implementation.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11227 | 'types': 'The standard type hierarchy\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11228 | '***************************\n' |
| 11229 | '\n' |
| 11230 | 'Below is a list of the types that are built into Python. ' |
| 11231 | 'Extension\n' |
| 11232 | 'modules (written in C, Java, or other languages, depending on the\n' |
| 11233 | 'implementation) can define additional types. Future versions of\n' |
| 11234 | 'Python may add types to the type hierarchy (e.g., rational ' |
| 11235 | 'numbers,\n' |
| 11236 | 'efficiently stored arrays of integers, etc.), although such ' |
| 11237 | 'additions\n' |
| 11238 | 'will often be provided via the standard library instead.\n' |
| 11239 | '\n' |
| 11240 | 'Some of the type descriptions below contain a paragraph listing\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11241 | '‘special attributes.’ These are attributes that provide access to ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11242 | 'the\n' |
| 11243 | 'implementation and are not intended for general use. Their ' |
| 11244 | 'definition\n' |
| 11245 | 'may change in the future.\n' |
| 11246 | '\n' |
| 11247 | 'None\n' |
| 11248 | ' This type has a single value. There is a single object with ' |
| 11249 | 'this\n' |
| 11250 | ' value. This object is accessed through the built-in name "None". ' |
| 11251 | 'It\n' |
| 11252 | ' is used to signify the absence of a value in many situations, ' |
| 11253 | 'e.g.,\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11254 | ' it is returned from functions that don’t explicitly return\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11255 | ' anything. Its truth value is false.\n' |
| 11256 | '\n' |
| 11257 | 'NotImplemented\n' |
| 11258 | ' This type has a single value. There is a single object with ' |
| 11259 | 'this\n' |
| 11260 | ' value. This object is accessed through the built-in name\n' |
| 11261 | ' "NotImplemented". Numeric methods and rich comparison methods\n' |
| 11262 | ' should return this value if they do not implement the operation ' |
| 11263 | 'for\n' |
| 11264 | ' the operands provided. (The interpreter will then try the\n' |
| 11265 | ' reflected operation, or some other fallback, depending on the\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 11266 | ' operator.) It should not be evaluated in a boolean context.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11267 | '\n' |
| 11268 | ' See Implementing the arithmetic operations for more details.\n' |
| 11269 | '\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 11270 | ' Changed in version 3.9: Evaluating "NotImplemented" in a ' |
| 11271 | 'boolean\n' |
| 11272 | ' context is deprecated. While it currently evaluates as true, it\n' |
| 11273 | ' will emit a "DeprecationWarning". It will raise a "TypeError" in ' |
| 11274 | 'a\n' |
| 11275 | ' future version of Python.\n' |
| 11276 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11277 | 'Ellipsis\n' |
| 11278 | ' This type has a single value. There is a single object with ' |
| 11279 | 'this\n' |
| 11280 | ' value. This object is accessed through the literal "..." or the\n' |
| 11281 | ' built-in name "Ellipsis". Its truth value is true.\n' |
| 11282 | '\n' |
| 11283 | '"numbers.Number"\n' |
| 11284 | ' These are created by numeric literals and returned as results ' |
| 11285 | 'by\n' |
| 11286 | ' arithmetic operators and arithmetic built-in functions. ' |
| 11287 | 'Numeric\n' |
| 11288 | ' objects are immutable; once created their value never changes.\n' |
| 11289 | ' Python numbers are of course strongly related to mathematical\n' |
| 11290 | ' numbers, but subject to the limitations of numerical ' |
| 11291 | 'representation\n' |
| 11292 | ' in computers.\n' |
| 11293 | '\n' |
| 11294 | ' Python distinguishes between integers, floating point numbers, ' |
| 11295 | 'and\n' |
| 11296 | ' complex numbers:\n' |
| 11297 | '\n' |
| 11298 | ' "numbers.Integral"\n' |
| 11299 | ' These represent elements from the mathematical set of ' |
| 11300 | 'integers\n' |
| 11301 | ' (positive and negative).\n' |
| 11302 | '\n' |
| 11303 | ' There are two types of integers:\n' |
| 11304 | '\n' |
| 11305 | ' Integers ("int")\n' |
| 11306 | '\n' |
| 11307 | ' These represent numbers in an unlimited range, subject to\n' |
| 11308 | ' available (virtual) memory only. For the purpose of ' |
| 11309 | 'shift\n' |
| 11310 | ' and mask operations, a binary representation is assumed, ' |
| 11311 | 'and\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11312 | ' negative numbers are represented in a variant of 2’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11313 | ' complement which gives the illusion of an infinite string ' |
| 11314 | 'of\n' |
| 11315 | ' sign bits extending to the left.\n' |
| 11316 | '\n' |
| 11317 | ' Booleans ("bool")\n' |
| 11318 | ' These represent the truth values False and True. The two\n' |
| 11319 | ' objects representing the values "False" and "True" are ' |
| 11320 | 'the\n' |
| 11321 | ' only Boolean objects. The Boolean type is a subtype of ' |
| 11322 | 'the\n' |
| 11323 | ' integer type, and Boolean values behave like the values 0 ' |
| 11324 | 'and\n' |
| 11325 | ' 1, respectively, in almost all contexts, the exception ' |
| 11326 | 'being\n' |
| 11327 | ' that when converted to a string, the strings ""False"" or\n' |
| 11328 | ' ""True"" are returned, respectively.\n' |
| 11329 | '\n' |
| 11330 | ' The rules for integer representation are intended to give ' |
| 11331 | 'the\n' |
| 11332 | ' most meaningful interpretation of shift and mask operations\n' |
| 11333 | ' involving negative integers.\n' |
| 11334 | '\n' |
| 11335 | ' "numbers.Real" ("float")\n' |
| 11336 | ' These represent machine-level double precision floating ' |
| 11337 | 'point\n' |
| 11338 | ' numbers. You are at the mercy of the underlying machine\n' |
| 11339 | ' architecture (and C or Java implementation) for the accepted\n' |
| 11340 | ' range and handling of overflow. Python does not support ' |
| 11341 | 'single-\n' |
| 11342 | ' precision floating point numbers; the savings in processor ' |
| 11343 | 'and\n' |
| 11344 | ' memory usage that are usually the reason for using these are\n' |
| 11345 | ' dwarfed by the overhead of using objects in Python, so there ' |
| 11346 | 'is\n' |
| 11347 | ' no reason to complicate the language with two kinds of ' |
| 11348 | 'floating\n' |
| 11349 | ' point numbers.\n' |
| 11350 | '\n' |
| 11351 | ' "numbers.Complex" ("complex")\n' |
| 11352 | ' These represent complex numbers as a pair of machine-level\n' |
| 11353 | ' double precision floating point numbers. The same caveats ' |
| 11354 | 'apply\n' |
| 11355 | ' as for floating point numbers. The real and imaginary parts ' |
| 11356 | 'of a\n' |
| 11357 | ' complex number "z" can be retrieved through the read-only\n' |
| 11358 | ' attributes "z.real" and "z.imag".\n' |
| 11359 | '\n' |
| 11360 | 'Sequences\n' |
| 11361 | ' These represent finite ordered sets indexed by non-negative\n' |
| 11362 | ' numbers. The built-in function "len()" returns the number of ' |
| 11363 | 'items\n' |
| 11364 | ' of a sequence. When the length of a sequence is *n*, the index ' |
| 11365 | 'set\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11366 | ' contains the numbers 0, 1, …, *n*-1. Item *i* of sequence *a* ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11367 | 'is\n' |
| 11368 | ' selected by "a[i]".\n' |
| 11369 | '\n' |
| 11370 | ' Sequences also support slicing: "a[i:j]" selects all items with\n' |
| 11371 | ' index *k* such that *i* "<=" *k* "<" *j*. When used as an\n' |
| 11372 | ' expression, a slice is a sequence of the same type. This ' |
| 11373 | 'implies\n' |
| 11374 | ' that the index set is renumbered so that it starts at 0.\n' |
| 11375 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11376 | ' Some sequences also support “extended slicing” with a third ' |
| 11377 | '“step”\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11378 | ' parameter: "a[i:j:k]" selects all items of *a* with index *x* ' |
| 11379 | 'where\n' |
| 11380 | ' "x = i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*.\n' |
| 11381 | '\n' |
| 11382 | ' Sequences are distinguished according to their mutability:\n' |
| 11383 | '\n' |
| 11384 | ' Immutable sequences\n' |
| 11385 | ' An object of an immutable sequence type cannot change once it ' |
| 11386 | 'is\n' |
| 11387 | ' created. (If the object contains references to other ' |
| 11388 | 'objects,\n' |
| 11389 | ' these other objects may be mutable and may be changed; ' |
| 11390 | 'however,\n' |
| 11391 | ' the collection of objects directly referenced by an ' |
| 11392 | 'immutable\n' |
| 11393 | ' object cannot change.)\n' |
| 11394 | '\n' |
| 11395 | ' The following types are immutable sequences:\n' |
| 11396 | '\n' |
| 11397 | ' Strings\n' |
| 11398 | ' A string is a sequence of values that represent Unicode ' |
| 11399 | 'code\n' |
| 11400 | ' points. All the code points in the range "U+0000 - ' |
| 11401 | 'U+10FFFF"\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11402 | ' can be represented in a string. Python doesn’t have a ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11403 | '"char"\n' |
| 11404 | ' type; instead, every code point in the string is ' |
| 11405 | 'represented\n' |
| 11406 | ' as a string object with length "1". The built-in ' |
| 11407 | 'function\n' |
| 11408 | ' "ord()" converts a code point from its string form to an\n' |
| 11409 | ' integer in the range "0 - 10FFFF"; "chr()" converts an\n' |
| 11410 | ' integer in the range "0 - 10FFFF" to the corresponding ' |
| 11411 | 'length\n' |
| 11412 | ' "1" string object. "str.encode()" can be used to convert ' |
| 11413 | 'a\n' |
| 11414 | ' "str" to "bytes" using the given text encoding, and\n' |
| 11415 | ' "bytes.decode()" can be used to achieve the opposite.\n' |
| 11416 | '\n' |
| 11417 | ' Tuples\n' |
| 11418 | ' The items of a tuple are arbitrary Python objects. Tuples ' |
| 11419 | 'of\n' |
| 11420 | ' two or more items are formed by comma-separated lists of\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11421 | ' expressions. A tuple of one item (a ‘singleton’) can be\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11422 | ' formed by affixing a comma to an expression (an expression ' |
| 11423 | 'by\n' |
| 11424 | ' itself does not create a tuple, since parentheses must be\n' |
| 11425 | ' usable for grouping of expressions). An empty tuple can ' |
| 11426 | 'be\n' |
| 11427 | ' formed by an empty pair of parentheses.\n' |
| 11428 | '\n' |
| 11429 | ' Bytes\n' |
| 11430 | ' A bytes object is an immutable array. The items are ' |
| 11431 | '8-bit\n' |
| 11432 | ' bytes, represented by integers in the range 0 <= x < 256.\n' |
| 11433 | ' Bytes literals (like "b\'abc\'") and the built-in ' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11434 | '"bytes()"\n' |
| 11435 | ' constructor can be used to create bytes objects. Also, ' |
| 11436 | 'bytes\n' |
| 11437 | ' objects can be decoded to strings via the "decode()" ' |
| 11438 | 'method.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11439 | '\n' |
| 11440 | ' Mutable sequences\n' |
| 11441 | ' Mutable sequences can be changed after they are created. ' |
| 11442 | 'The\n' |
| 11443 | ' subscription and slicing notations can be used as the target ' |
| 11444 | 'of\n' |
| 11445 | ' assignment and "del" (delete) statements.\n' |
| 11446 | '\n' |
| 11447 | ' There are currently two intrinsic mutable sequence types:\n' |
| 11448 | '\n' |
| 11449 | ' Lists\n' |
| 11450 | ' The items of a list are arbitrary Python objects. Lists ' |
| 11451 | 'are\n' |
| 11452 | ' formed by placing a comma-separated list of expressions ' |
| 11453 | 'in\n' |
| 11454 | ' square brackets. (Note that there are no special cases ' |
| 11455 | 'needed\n' |
| 11456 | ' to form lists of length 0 or 1.)\n' |
| 11457 | '\n' |
| 11458 | ' Byte Arrays\n' |
| 11459 | ' A bytearray object is a mutable array. They are created ' |
| 11460 | 'by\n' |
| 11461 | ' the built-in "bytearray()" constructor. Aside from being\n' |
| 11462 | ' mutable (and hence unhashable), byte arrays otherwise ' |
| 11463 | 'provide\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11464 | ' the same interface and functionality as immutable "bytes"\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11465 | ' objects.\n' |
| 11466 | '\n' |
| 11467 | ' The extension module "array" provides an additional example ' |
| 11468 | 'of a\n' |
| 11469 | ' mutable sequence type, as does the "collections" module.\n' |
| 11470 | '\n' |
| 11471 | 'Set types\n' |
| 11472 | ' These represent unordered, finite sets of unique, immutable\n' |
| 11473 | ' objects. As such, they cannot be indexed by any subscript. ' |
| 11474 | 'However,\n' |
| 11475 | ' they can be iterated over, and the built-in function "len()"\n' |
| 11476 | ' returns the number of items in a set. Common uses for sets are ' |
| 11477 | 'fast\n' |
| 11478 | ' membership testing, removing duplicates from a sequence, and\n' |
| 11479 | ' computing mathematical operations such as intersection, union,\n' |
| 11480 | ' difference, and symmetric difference.\n' |
| 11481 | '\n' |
| 11482 | ' For set elements, the same immutability rules apply as for\n' |
| 11483 | ' dictionary keys. Note that numeric types obey the normal rules ' |
| 11484 | 'for\n' |
| 11485 | ' numeric comparison: if two numbers compare equal (e.g., "1" and\n' |
| 11486 | ' "1.0"), only one of them can be contained in a set.\n' |
| 11487 | '\n' |
| 11488 | ' There are currently two intrinsic set types:\n' |
| 11489 | '\n' |
| 11490 | ' Sets\n' |
| 11491 | ' These represent a mutable set. They are created by the ' |
| 11492 | 'built-in\n' |
| 11493 | ' "set()" constructor and can be modified afterwards by ' |
| 11494 | 'several\n' |
| 11495 | ' methods, such as "add()".\n' |
| 11496 | '\n' |
| 11497 | ' Frozen sets\n' |
| 11498 | ' These represent an immutable set. They are created by the\n' |
| 11499 | ' built-in "frozenset()" constructor. As a frozenset is ' |
| 11500 | 'immutable\n' |
| 11501 | ' and *hashable*, it can be used again as an element of ' |
| 11502 | 'another\n' |
| 11503 | ' set, or as a dictionary key.\n' |
| 11504 | '\n' |
| 11505 | 'Mappings\n' |
| 11506 | ' These represent finite sets of objects indexed by arbitrary ' |
| 11507 | 'index\n' |
| 11508 | ' sets. The subscript notation "a[k]" selects the item indexed by ' |
| 11509 | '"k"\n' |
| 11510 | ' from the mapping "a"; this can be used in expressions and as ' |
| 11511 | 'the\n' |
| 11512 | ' target of assignments or "del" statements. The built-in ' |
| 11513 | 'function\n' |
| 11514 | ' "len()" returns the number of items in a mapping.\n' |
| 11515 | '\n' |
| 11516 | ' There is currently a single intrinsic mapping type:\n' |
| 11517 | '\n' |
| 11518 | ' Dictionaries\n' |
| 11519 | ' These represent finite sets of objects indexed by nearly\n' |
| 11520 | ' arbitrary values. The only types of values not acceptable ' |
| 11521 | 'as\n' |
| 11522 | ' keys are values containing lists or dictionaries or other\n' |
| 11523 | ' mutable types that are compared by value rather than by ' |
| 11524 | 'object\n' |
| 11525 | ' identity, the reason being that the efficient implementation ' |
| 11526 | 'of\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11527 | ' dictionaries requires a key’s hash value to remain constant.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11528 | ' Numeric types used for keys obey the normal rules for ' |
| 11529 | 'numeric\n' |
| 11530 | ' comparison: if two numbers compare equal (e.g., "1" and ' |
| 11531 | '"1.0")\n' |
| 11532 | ' then they can be used interchangeably to index the same\n' |
| 11533 | ' dictionary entry.\n' |
| 11534 | '\n' |
Łukasz Langa | bc1c8af | 2020-04-27 22:44:04 +0200 | [diff] [blame] | 11535 | ' Dictionaries preserve insertion order, meaning that keys will ' |
| 11536 | 'be\n' |
| 11537 | ' produced in the same order they were added sequentially over ' |
| 11538 | 'the\n' |
| 11539 | ' dictionary. Replacing an existing key does not change the ' |
| 11540 | 'order,\n' |
| 11541 | ' however removing a key and re-inserting it will add it to ' |
| 11542 | 'the\n' |
| 11543 | ' end instead of keeping its old place.\n' |
| 11544 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11545 | ' Dictionaries are mutable; they can be created by the "{...}"\n' |
| 11546 | ' notation (see section Dictionary displays).\n' |
| 11547 | '\n' |
| 11548 | ' The extension modules "dbm.ndbm" and "dbm.gnu" provide\n' |
| 11549 | ' additional examples of mapping types, as does the ' |
| 11550 | '"collections"\n' |
| 11551 | ' module.\n' |
| 11552 | '\n' |
Łukasz Langa | bc1c8af | 2020-04-27 22:44:04 +0200 | [diff] [blame] | 11553 | ' Changed in version 3.7: Dictionaries did not preserve ' |
| 11554 | 'insertion\n' |
| 11555 | ' order in versions of Python before 3.6. In CPython 3.6,\n' |
| 11556 | ' insertion order was preserved, but it was considered an\n' |
| 11557 | ' implementation detail at that time rather than a language\n' |
| 11558 | ' guarantee.\n' |
| 11559 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11560 | 'Callable types\n' |
| 11561 | ' These are the types to which the function call operation (see\n' |
| 11562 | ' section Calls) can be applied:\n' |
| 11563 | '\n' |
| 11564 | ' User-defined functions\n' |
| 11565 | ' A user-defined function object is created by a function\n' |
| 11566 | ' definition (see section Function definitions). It should be\n' |
| 11567 | ' called with an argument list containing the same number of ' |
| 11568 | 'items\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11569 | ' as the function’s formal parameter list.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11570 | '\n' |
| 11571 | ' Special attributes:\n' |
| 11572 | '\n' |
| 11573 | ' ' |
| 11574 | '+---------------------------+---------------------------------+-------------+\n' |
| 11575 | ' | Attribute | Meaning ' |
| 11576 | '| |\n' |
| 11577 | ' ' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 11578 | '|===========================|=================================|=============|\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11579 | ' | "__doc__" | The function’s documentation ' |
| 11580 | '| Writable |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11581 | ' | | string, or "None" if ' |
| 11582 | '| |\n' |
| 11583 | ' | | unavailable; not inherited by ' |
| 11584 | '| |\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 11585 | ' | | subclasses. ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11586 | '| |\n' |
| 11587 | ' ' |
| 11588 | '+---------------------------+---------------------------------+-------------+\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 11589 | ' | "__name__" | The function’s name. ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11590 | '| Writable |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11591 | ' ' |
| 11592 | '+---------------------------+---------------------------------+-------------+\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 11593 | ' | "__qualname__" | The function’s *qualified ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11594 | '| Writable |\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 11595 | ' | | name*. New in version 3.3. ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11596 | '| |\n' |
| 11597 | ' ' |
| 11598 | '+---------------------------+---------------------------------+-------------+\n' |
| 11599 | ' | "__module__" | The name of the module the ' |
| 11600 | '| Writable |\n' |
| 11601 | ' | | function was defined in, or ' |
| 11602 | '| |\n' |
| 11603 | ' | | "None" if unavailable. ' |
| 11604 | '| |\n' |
| 11605 | ' ' |
| 11606 | '+---------------------------+---------------------------------+-------------+\n' |
| 11607 | ' | "__defaults__" | A tuple containing default ' |
| 11608 | '| Writable |\n' |
| 11609 | ' | | argument values for those ' |
| 11610 | '| |\n' |
| 11611 | ' | | arguments that have defaults, ' |
| 11612 | '| |\n' |
| 11613 | ' | | or "None" if no arguments have ' |
| 11614 | '| |\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 11615 | ' | | a default value. ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11616 | '| |\n' |
| 11617 | ' ' |
| 11618 | '+---------------------------+---------------------------------+-------------+\n' |
| 11619 | ' | "__code__" | The code object representing ' |
| 11620 | '| Writable |\n' |
| 11621 | ' | | the compiled function body. ' |
| 11622 | '| |\n' |
| 11623 | ' ' |
| 11624 | '+---------------------------+---------------------------------+-------------+\n' |
| 11625 | ' | "__globals__" | A reference to the dictionary ' |
| 11626 | '| Read-only |\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11627 | ' | | that holds the function’s ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11628 | '| |\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11629 | ' | | global variables — the global ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11630 | '| |\n' |
| 11631 | ' | | namespace of the module in ' |
| 11632 | '| |\n' |
| 11633 | ' | | which the function was defined. ' |
| 11634 | '| |\n' |
| 11635 | ' ' |
| 11636 | '+---------------------------+---------------------------------+-------------+\n' |
| 11637 | ' | "__dict__" | The namespace supporting ' |
| 11638 | '| Writable |\n' |
| 11639 | ' | | arbitrary function attributes. ' |
| 11640 | '| |\n' |
| 11641 | ' ' |
| 11642 | '+---------------------------+---------------------------------+-------------+\n' |
| 11643 | ' | "__closure__" | "None" or a tuple of cells that ' |
| 11644 | '| Read-only |\n' |
| 11645 | ' | | contain bindings for the ' |
| 11646 | '| |\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11647 | ' | | function’s free variables. See ' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11648 | '| |\n' |
| 11649 | ' | | below for information on the ' |
| 11650 | '| |\n' |
| 11651 | ' | | "cell_contents" attribute. ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11652 | '| |\n' |
| 11653 | ' ' |
| 11654 | '+---------------------------+---------------------------------+-------------+\n' |
| 11655 | ' | "__annotations__" | A dict containing annotations ' |
| 11656 | '| Writable |\n' |
| 11657 | ' | | of parameters. The keys of the ' |
| 11658 | '| |\n' |
| 11659 | ' | | dict are the parameter names, ' |
| 11660 | '| |\n' |
| 11661 | ' | | and "\'return\'" for the ' |
| 11662 | 'return | |\n' |
| 11663 | ' | | annotation, if provided. ' |
| 11664 | '| |\n' |
| 11665 | ' ' |
| 11666 | '+---------------------------+---------------------------------+-------------+\n' |
| 11667 | ' | "__kwdefaults__" | A dict containing defaults for ' |
| 11668 | '| Writable |\n' |
| 11669 | ' | | keyword-only parameters. ' |
| 11670 | '| |\n' |
| 11671 | ' ' |
| 11672 | '+---------------------------+---------------------------------+-------------+\n' |
| 11673 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11674 | ' Most of the attributes labelled “Writable” check the type of ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11675 | 'the\n' |
| 11676 | ' assigned value.\n' |
| 11677 | '\n' |
| 11678 | ' Function objects also support getting and setting arbitrary\n' |
| 11679 | ' attributes, which can be used, for example, to attach ' |
| 11680 | 'metadata\n' |
| 11681 | ' to functions. Regular attribute dot-notation is used to get ' |
| 11682 | 'and\n' |
| 11683 | ' set such attributes. *Note that the current implementation ' |
| 11684 | 'only\n' |
| 11685 | ' supports function attributes on user-defined functions. ' |
| 11686 | 'Function\n' |
| 11687 | ' attributes on built-in functions may be supported in the\n' |
| 11688 | ' future.*\n' |
| 11689 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11690 | ' A cell object has the attribute "cell_contents". This can be\n' |
| 11691 | ' used to get the value of the cell, as well as set the value.\n' |
| 11692 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11693 | ' Additional information about a function’s definition can be\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11694 | ' retrieved from its code object; see the description of ' |
| 11695 | 'internal\n' |
Łukasz Langa | 23f4589 | 2019-02-25 13:08:32 +0100 | [diff] [blame] | 11696 | ' types below. The "cell" type can be accessed in the "types"\n' |
| 11697 | ' module.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11698 | '\n' |
| 11699 | ' Instance methods\n' |
| 11700 | ' An instance method object combines a class, a class instance ' |
| 11701 | 'and\n' |
| 11702 | ' any callable object (normally a user-defined function).\n' |
| 11703 | '\n' |
| 11704 | ' Special read-only attributes: "__self__" is the class ' |
| 11705 | 'instance\n' |
| 11706 | ' object, "__func__" is the function object; "__doc__" is the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11707 | ' method’s documentation (same as "__func__.__doc__"); ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11708 | '"__name__"\n' |
| 11709 | ' is the method name (same as "__func__.__name__"); ' |
| 11710 | '"__module__"\n' |
| 11711 | ' is the name of the module the method was defined in, or ' |
| 11712 | '"None"\n' |
| 11713 | ' if unavailable.\n' |
| 11714 | '\n' |
| 11715 | ' Methods also support accessing (but not setting) the ' |
| 11716 | 'arbitrary\n' |
| 11717 | ' function attributes on the underlying function object.\n' |
| 11718 | '\n' |
| 11719 | ' User-defined method objects may be created when getting an\n' |
| 11720 | ' attribute of a class (perhaps via an instance of that class), ' |
| 11721 | 'if\n' |
| 11722 | ' that attribute is a user-defined function object or a class\n' |
| 11723 | ' method object.\n' |
| 11724 | '\n' |
| 11725 | ' When an instance method object is created by retrieving a ' |
| 11726 | 'user-\n' |
| 11727 | ' defined function object from a class via one of its ' |
| 11728 | 'instances,\n' |
| 11729 | ' its "__self__" attribute is the instance, and the method ' |
| 11730 | 'object\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11731 | ' is said to be bound. The new method’s "__func__" attribute ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11732 | 'is\n' |
| 11733 | ' the original function object.\n' |
| 11734 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11735 | ' When an instance method object is created by retrieving a ' |
| 11736 | 'class\n' |
| 11737 | ' method object from a class or instance, its "__self__" ' |
| 11738 | 'attribute\n' |
| 11739 | ' is the class itself, and its "__func__" attribute is the\n' |
| 11740 | ' function object underlying the class method.\n' |
| 11741 | '\n' |
| 11742 | ' When an instance method object is called, the underlying\n' |
| 11743 | ' function ("__func__") is called, inserting the class ' |
| 11744 | 'instance\n' |
| 11745 | ' ("__self__") in front of the argument list. For instance, ' |
| 11746 | 'when\n' |
| 11747 | ' "C" is a class which contains a definition for a function ' |
| 11748 | '"f()",\n' |
| 11749 | ' and "x" is an instance of "C", calling "x.f(1)" is equivalent ' |
| 11750 | 'to\n' |
| 11751 | ' calling "C.f(x, 1)".\n' |
| 11752 | '\n' |
| 11753 | ' When an instance method object is derived from a class ' |
| 11754 | 'method\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11755 | ' object, the “class instance” stored in "__self__" will ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11756 | 'actually\n' |
| 11757 | ' be the class itself, so that calling either "x.f(1)" or ' |
| 11758 | '"C.f(1)"\n' |
| 11759 | ' is equivalent to calling "f(C,1)" where "f" is the ' |
| 11760 | 'underlying\n' |
| 11761 | ' function.\n' |
| 11762 | '\n' |
| 11763 | ' Note that the transformation from function object to ' |
| 11764 | 'instance\n' |
| 11765 | ' method object happens each time the attribute is retrieved ' |
| 11766 | 'from\n' |
| 11767 | ' the instance. In some cases, a fruitful optimization is to\n' |
| 11768 | ' assign the attribute to a local variable and call that local\n' |
| 11769 | ' variable. Also notice that this transformation only happens ' |
| 11770 | 'for\n' |
| 11771 | ' user-defined functions; other callable objects (and all non-\n' |
| 11772 | ' callable objects) are retrieved without transformation. It ' |
| 11773 | 'is\n' |
| 11774 | ' also important to note that user-defined functions which are\n' |
| 11775 | ' attributes of a class instance are not converted to bound\n' |
| 11776 | ' methods; this *only* happens when the function is an ' |
| 11777 | 'attribute\n' |
| 11778 | ' of the class.\n' |
| 11779 | '\n' |
| 11780 | ' Generator functions\n' |
| 11781 | ' A function or method which uses the "yield" statement (see\n' |
| 11782 | ' section The yield statement) is called a *generator ' |
| 11783 | 'function*.\n' |
| 11784 | ' Such a function, when called, always returns an iterator ' |
| 11785 | 'object\n' |
| 11786 | ' which can be used to execute the body of the function: ' |
| 11787 | 'calling\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11788 | ' the iterator’s "iterator.__next__()" method will cause the\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11789 | ' function to execute until it provides a value using the ' |
| 11790 | '"yield"\n' |
| 11791 | ' statement. When the function executes a "return" statement ' |
| 11792 | 'or\n' |
| 11793 | ' falls off the end, a "StopIteration" exception is raised and ' |
| 11794 | 'the\n' |
| 11795 | ' iterator will have reached the end of the set of values to ' |
| 11796 | 'be\n' |
| 11797 | ' returned.\n' |
| 11798 | '\n' |
| 11799 | ' Coroutine functions\n' |
| 11800 | ' A function or method which is defined using "async def" is\n' |
| 11801 | ' called a *coroutine function*. Such a function, when ' |
| 11802 | 'called,\n' |
| 11803 | ' returns a *coroutine* object. It may contain "await"\n' |
| 11804 | ' expressions, as well as "async with" and "async for" ' |
| 11805 | 'statements.\n' |
| 11806 | ' See also the Coroutine Objects section.\n' |
| 11807 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11808 | ' Asynchronous generator functions\n' |
| 11809 | ' A function or method which is defined using "async def" and\n' |
| 11810 | ' which uses the "yield" statement is called a *asynchronous\n' |
| 11811 | ' generator function*. Such a function, when called, returns ' |
| 11812 | 'an\n' |
| 11813 | ' asynchronous iterator object which can be used in an "async ' |
| 11814 | 'for"\n' |
| 11815 | ' statement to execute the body of the function.\n' |
| 11816 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11817 | ' Calling the asynchronous iterator’s "aiterator.__anext__()"\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11818 | ' method will return an *awaitable* which when awaited will\n' |
| 11819 | ' execute until it provides a value using the "yield" ' |
| 11820 | 'expression.\n' |
| 11821 | ' When the function executes an empty "return" statement or ' |
| 11822 | 'falls\n' |
| 11823 | ' off the end, a "StopAsyncIteration" exception is raised and ' |
| 11824 | 'the\n' |
| 11825 | ' asynchronous iterator will have reached the end of the set ' |
| 11826 | 'of\n' |
| 11827 | ' values to be yielded.\n' |
| 11828 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11829 | ' Built-in functions\n' |
| 11830 | ' A built-in function object is a wrapper around a C function.\n' |
| 11831 | ' Examples of built-in functions are "len()" and "math.sin()"\n' |
| 11832 | ' ("math" is a standard built-in module). The number and type ' |
| 11833 | 'of\n' |
| 11834 | ' the arguments are determined by the C function. Special ' |
| 11835 | 'read-\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11836 | ' only attributes: "__doc__" is the function’s documentation\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11837 | ' string, or "None" if unavailable; "__name__" is the ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11838 | 'function’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11839 | ' name; "__self__" is set to "None" (but see the next item);\n' |
| 11840 | ' "__module__" is the name of the module the function was ' |
| 11841 | 'defined\n' |
| 11842 | ' in or "None" if unavailable.\n' |
| 11843 | '\n' |
| 11844 | ' Built-in methods\n' |
| 11845 | ' This is really a different disguise of a built-in function, ' |
| 11846 | 'this\n' |
| 11847 | ' time containing an object passed to the C function as an\n' |
| 11848 | ' implicit extra argument. An example of a built-in method is\n' |
| 11849 | ' "alist.append()", assuming *alist* is a list object. In this\n' |
| 11850 | ' case, the special read-only attribute "__self__" is set to ' |
| 11851 | 'the\n' |
| 11852 | ' object denoted by *alist*.\n' |
| 11853 | '\n' |
| 11854 | ' Classes\n' |
| 11855 | ' Classes are callable. These objects normally act as ' |
| 11856 | 'factories\n' |
| 11857 | ' for new instances of themselves, but variations are possible ' |
| 11858 | 'for\n' |
| 11859 | ' class types that override "__new__()". The arguments of the\n' |
| 11860 | ' call are passed to "__new__()" and, in the typical case, to\n' |
| 11861 | ' "__init__()" to initialize the new instance.\n' |
| 11862 | '\n' |
| 11863 | ' Class Instances\n' |
| 11864 | ' Instances of arbitrary classes can be made callable by ' |
| 11865 | 'defining\n' |
| 11866 | ' a "__call__()" method in their class.\n' |
| 11867 | '\n' |
| 11868 | 'Modules\n' |
| 11869 | ' Modules are a basic organizational unit of Python code, and are\n' |
| 11870 | ' created by the import system as invoked either by the "import"\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11871 | ' statement, or by calling functions such as\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11872 | ' "importlib.import_module()" and built-in "__import__()". A ' |
| 11873 | 'module\n' |
| 11874 | ' object has a namespace implemented by a dictionary object (this ' |
| 11875 | 'is\n' |
| 11876 | ' the dictionary referenced by the "__globals__" attribute of\n' |
| 11877 | ' functions defined in the module). Attribute references are\n' |
| 11878 | ' translated to lookups in this dictionary, e.g., "m.x" is ' |
| 11879 | 'equivalent\n' |
| 11880 | ' to "m.__dict__["x"]". A module object does not contain the code\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11881 | ' object used to initialize the module (since it isn’t needed ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11882 | 'once\n' |
| 11883 | ' the initialization is done).\n' |
| 11884 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11885 | ' Attribute assignment updates the module’s namespace dictionary,\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11886 | ' e.g., "m.x = 1" is equivalent to "m.__dict__["x"] = 1".\n' |
| 11887 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11888 | ' Predefined (writable) attributes: "__name__" is the module’s ' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 11889 | 'name;\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11890 | ' "__doc__" is the module’s documentation string, or "None" if\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 11891 | ' unavailable; "__annotations__" (optional) is a dictionary\n' |
| 11892 | ' containing *variable annotations* collected during module body\n' |
| 11893 | ' execution; "__file__" is the pathname of the file from which ' |
| 11894 | 'the\n' |
| 11895 | ' module was loaded, if it was loaded from a file. The "__file__"\n' |
| 11896 | ' attribute may be missing for certain types of modules, such as ' |
| 11897 | 'C\n' |
| 11898 | ' modules that are statically linked into the interpreter; for\n' |
| 11899 | ' extension modules loaded dynamically from a shared library, it ' |
| 11900 | 'is\n' |
| 11901 | ' the pathname of the shared library file.\n' |
| 11902 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11903 | ' Special read-only attribute: "__dict__" is the module’s ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11904 | 'namespace\n' |
| 11905 | ' as a dictionary object.\n' |
| 11906 | '\n' |
| 11907 | ' **CPython implementation detail:** Because of the way CPython\n' |
| 11908 | ' clears module dictionaries, the module dictionary will be ' |
| 11909 | 'cleared\n' |
| 11910 | ' when the module falls out of scope even if the dictionary still ' |
| 11911 | 'has\n' |
| 11912 | ' live references. To avoid this, copy the dictionary or keep ' |
| 11913 | 'the\n' |
| 11914 | ' module around while using its dictionary directly.\n' |
| 11915 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11916 | 'Custom classes\n' |
| 11917 | ' Custom class types are typically created by class definitions ' |
| 11918 | '(see\n' |
| 11919 | ' section Class definitions). A class has a namespace implemented ' |
| 11920 | 'by\n' |
| 11921 | ' a dictionary object. Class attribute references are translated ' |
| 11922 | 'to\n' |
| 11923 | ' lookups in this dictionary, e.g., "C.x" is translated to\n' |
| 11924 | ' "C.__dict__["x"]" (although there are a number of hooks which ' |
| 11925 | 'allow\n' |
| 11926 | ' for other means of locating attributes). When the attribute name ' |
| 11927 | 'is\n' |
| 11928 | ' not found there, the attribute search continues in the base\n' |
| 11929 | ' classes. This search of the base classes uses the C3 method\n' |
| 11930 | ' resolution order which behaves correctly even in the presence ' |
| 11931 | 'of\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11932 | ' ‘diamond’ inheritance structures where there are multiple\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11933 | ' inheritance paths leading back to a common ancestor. Additional\n' |
| 11934 | ' details on the C3 MRO used by Python can be found in the\n' |
| 11935 | ' documentation accompanying the 2.3 release at\n' |
| 11936 | ' https://www.python.org/download/releases/2.3/mro/.\n' |
| 11937 | '\n' |
| 11938 | ' When a class attribute reference (for class "C", say) would ' |
| 11939 | 'yield a\n' |
| 11940 | ' class method object, it is transformed into an instance method\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11941 | ' object whose "__self__" attribute is "C". When it would yield ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11942 | 'a\n' |
| 11943 | ' static method object, it is transformed into the object wrapped ' |
| 11944 | 'by\n' |
| 11945 | ' the static method object. See section Implementing Descriptors ' |
| 11946 | 'for\n' |
| 11947 | ' another way in which attributes retrieved from a class may ' |
| 11948 | 'differ\n' |
| 11949 | ' from those actually contained in its "__dict__".\n' |
| 11950 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11951 | ' Class attribute assignments update the class’s dictionary, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11952 | 'never\n' |
| 11953 | ' the dictionary of a base class.\n' |
| 11954 | '\n' |
| 11955 | ' A class object can be called (see above) to yield a class ' |
| 11956 | 'instance\n' |
| 11957 | ' (see below).\n' |
| 11958 | '\n' |
| 11959 | ' Special attributes: "__name__" is the class name; "__module__" ' |
| 11960 | 'is\n' |
| 11961 | ' the module name in which the class was defined; "__dict__" is ' |
| 11962 | 'the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11963 | ' dictionary containing the class’s namespace; "__bases__" is a ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11964 | 'tuple\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11965 | ' containing the base classes, in the order of their occurrence ' |
| 11966 | 'in\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11967 | ' the base class list; "__doc__" is the class’s documentation ' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 11968 | 'string,\n' |
| 11969 | ' or "None" if undefined; "__annotations__" (optional) is a\n' |
| 11970 | ' dictionary containing *variable annotations* collected during ' |
| 11971 | 'class\n' |
| 11972 | ' body execution.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11973 | '\n' |
| 11974 | 'Class instances\n' |
| 11975 | ' A class instance is created by calling a class object (see ' |
| 11976 | 'above).\n' |
| 11977 | ' A class instance has a namespace implemented as a dictionary ' |
| 11978 | 'which\n' |
| 11979 | ' is the first place in which attribute references are searched.\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11980 | ' When an attribute is not found there, and the instance’s class ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11981 | 'has\n' |
| 11982 | ' an attribute by that name, the search continues with the class\n' |
| 11983 | ' attributes. If a class attribute is found that is a ' |
| 11984 | 'user-defined\n' |
| 11985 | ' function object, it is transformed into an instance method ' |
| 11986 | 'object\n' |
| 11987 | ' whose "__self__" attribute is the instance. Static method and\n' |
| 11988 | ' class method objects are also transformed; see above under\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11989 | ' “Classes”. See section Implementing Descriptors for another way ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11990 | 'in\n' |
| 11991 | ' which attributes of a class retrieved via its instances may ' |
| 11992 | 'differ\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11993 | ' from the objects actually stored in the class’s "__dict__". If ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11994 | 'no\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11995 | ' class attribute is found, and the object’s class has a\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 11996 | ' "__getattr__()" method, that is called to satisfy the lookup.\n' |
| 11997 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 11998 | ' Attribute assignments and deletions update the instance’s\n' |
| 11999 | ' dictionary, never a class’s dictionary. If the class has a\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12000 | ' "__setattr__()" or "__delattr__()" method, this is called ' |
| 12001 | 'instead\n' |
| 12002 | ' of updating the instance dictionary directly.\n' |
| 12003 | '\n' |
| 12004 | ' Class instances can pretend to be numbers, sequences, or ' |
| 12005 | 'mappings\n' |
| 12006 | ' if they have methods with certain special names. See section\n' |
| 12007 | ' Special method names.\n' |
| 12008 | '\n' |
| 12009 | ' Special attributes: "__dict__" is the attribute dictionary;\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12010 | ' "__class__" is the instance’s class.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12011 | '\n' |
| 12012 | 'I/O objects (also known as file objects)\n' |
| 12013 | ' A *file object* represents an open file. Various shortcuts are\n' |
| 12014 | ' available to create file objects: the "open()" built-in ' |
| 12015 | 'function,\n' |
| 12016 | ' and also "os.popen()", "os.fdopen()", and the "makefile()" ' |
| 12017 | 'method\n' |
| 12018 | ' of socket objects (and perhaps by other functions or methods\n' |
| 12019 | ' provided by extension modules).\n' |
| 12020 | '\n' |
| 12021 | ' The objects "sys.stdin", "sys.stdout" and "sys.stderr" are\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12022 | ' initialized to file objects corresponding to the interpreter’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12023 | ' standard input, output and error streams; they are all open in ' |
| 12024 | 'text\n' |
| 12025 | ' mode and therefore follow the interface defined by the\n' |
| 12026 | ' "io.TextIOBase" abstract class.\n' |
| 12027 | '\n' |
| 12028 | 'Internal types\n' |
| 12029 | ' A few types used internally by the interpreter are exposed to ' |
| 12030 | 'the\n' |
| 12031 | ' user. Their definitions may change with future versions of the\n' |
| 12032 | ' interpreter, but they are mentioned here for completeness.\n' |
| 12033 | '\n' |
| 12034 | ' Code objects\n' |
| 12035 | ' Code objects represent *byte-compiled* executable Python ' |
| 12036 | 'code,\n' |
| 12037 | ' or *bytecode*. The difference between a code object and a\n' |
| 12038 | ' function object is that the function object contains an ' |
| 12039 | 'explicit\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12040 | ' reference to the function’s globals (the module in which it ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12041 | 'was\n' |
| 12042 | ' defined), while a code object contains no context; also the\n' |
| 12043 | ' default argument values are stored in the function object, ' |
| 12044 | 'not\n' |
| 12045 | ' in the code object (because they represent values calculated ' |
| 12046 | 'at\n' |
| 12047 | ' run-time). Unlike function objects, code objects are ' |
| 12048 | 'immutable\n' |
| 12049 | ' and contain no references (directly or indirectly) to ' |
| 12050 | 'mutable\n' |
| 12051 | ' objects.\n' |
| 12052 | '\n' |
| 12053 | ' Special read-only attributes: "co_name" gives the function ' |
| 12054 | 'name;\n' |
Łukasz Langa | 3b5deb0 | 2019-06-04 19:44:34 +0200 | [diff] [blame] | 12055 | ' "co_argcount" is the total number of positional arguments\n' |
| 12056 | ' (including positional-only arguments and arguments with ' |
| 12057 | 'default\n' |
| 12058 | ' values); "co_posonlyargcount" is the number of ' |
| 12059 | 'positional-only\n' |
| 12060 | ' arguments (including arguments with default values);\n' |
| 12061 | ' "co_kwonlyargcount" is the number of keyword-only arguments\n' |
| 12062 | ' (including arguments with default values); "co_nlocals" is ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12063 | 'the\n' |
Łukasz Langa | 3b5deb0 | 2019-06-04 19:44:34 +0200 | [diff] [blame] | 12064 | ' number of local variables used by the function (including\n' |
| 12065 | ' arguments); "co_varnames" is a tuple containing the names of ' |
| 12066 | 'the\n' |
| 12067 | ' local variables (starting with the argument names);\n' |
| 12068 | ' "co_cellvars" is a tuple containing the names of local ' |
| 12069 | 'variables\n' |
| 12070 | ' that are referenced by nested functions; "co_freevars" is a\n' |
| 12071 | ' tuple containing the names of free variables; "co_code" is a\n' |
| 12072 | ' string representing the sequence of bytecode instructions;\n' |
| 12073 | ' "co_consts" is a tuple containing the literals used by the\n' |
| 12074 | ' bytecode; "co_names" is a tuple containing the names used by ' |
| 12075 | 'the\n' |
| 12076 | ' bytecode; "co_filename" is the filename from which the code ' |
| 12077 | 'was\n' |
| 12078 | ' compiled; "co_firstlineno" is the first line number of the\n' |
| 12079 | ' function; "co_lnotab" is a string encoding the mapping from\n' |
| 12080 | ' bytecode offsets to line numbers (for details see the source\n' |
| 12081 | ' code of the interpreter); "co_stacksize" is the required ' |
| 12082 | 'stack\n' |
Łukasz Langa | 6202d85 | 2019-12-18 22:09:19 +0100 | [diff] [blame] | 12083 | ' size; "co_flags" is an integer encoding a number of flags ' |
| 12084 | 'for\n' |
| 12085 | ' the interpreter.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12086 | '\n' |
| 12087 | ' The following flag bits are defined for "co_flags": bit ' |
| 12088 | '"0x04"\n' |
| 12089 | ' is set if the function uses the "*arguments" syntax to accept ' |
| 12090 | 'an\n' |
| 12091 | ' arbitrary number of positional arguments; bit "0x08" is set ' |
| 12092 | 'if\n' |
| 12093 | ' the function uses the "**keywords" syntax to accept ' |
| 12094 | 'arbitrary\n' |
| 12095 | ' keyword arguments; bit "0x20" is set if the function is a\n' |
| 12096 | ' generator.\n' |
| 12097 | '\n' |
| 12098 | ' Future feature declarations ("from __future__ import ' |
| 12099 | 'division")\n' |
| 12100 | ' also use bits in "co_flags" to indicate whether a code ' |
| 12101 | 'object\n' |
| 12102 | ' was compiled with a particular feature enabled: bit "0x2000" ' |
| 12103 | 'is\n' |
| 12104 | ' set if the function was compiled with future division ' |
| 12105 | 'enabled;\n' |
| 12106 | ' bits "0x10" and "0x1000" were used in earlier versions of\n' |
| 12107 | ' Python.\n' |
| 12108 | '\n' |
| 12109 | ' Other bits in "co_flags" are reserved for internal use.\n' |
| 12110 | '\n' |
| 12111 | ' If a code object represents a function, the first item in\n' |
| 12112 | ' "co_consts" is the documentation string of the function, or\n' |
| 12113 | ' "None" if undefined.\n' |
| 12114 | '\n' |
| 12115 | ' Frame objects\n' |
| 12116 | ' Frame objects represent execution frames. They may occur in\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12117 | ' traceback objects (see below), and are also passed to ' |
| 12118 | 'registered\n' |
| 12119 | ' trace functions.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12120 | '\n' |
| 12121 | ' Special read-only attributes: "f_back" is to the previous ' |
| 12122 | 'stack\n' |
| 12123 | ' frame (towards the caller), or "None" if this is the bottom\n' |
| 12124 | ' stack frame; "f_code" is the code object being executed in ' |
| 12125 | 'this\n' |
| 12126 | ' frame; "f_locals" is the dictionary used to look up local\n' |
| 12127 | ' variables; "f_globals" is used for global variables;\n' |
| 12128 | ' "f_builtins" is used for built-in (intrinsic) names; ' |
| 12129 | '"f_lasti"\n' |
| 12130 | ' gives the precise instruction (this is an index into the\n' |
| 12131 | ' bytecode string of the code object).\n' |
| 12132 | '\n' |
| 12133 | ' Special writable attributes: "f_trace", if not "None", is a\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 12134 | ' function called for various events during code execution ' |
| 12135 | '(this\n' |
| 12136 | ' is used by the debugger). Normally an event is triggered for\n' |
| 12137 | ' each new source line - this can be disabled by setting\n' |
| 12138 | ' "f_trace_lines" to "False".\n' |
| 12139 | '\n' |
| 12140 | ' Implementations *may* allow per-opcode events to be requested ' |
| 12141 | 'by\n' |
| 12142 | ' setting "f_trace_opcodes" to "True". Note that this may lead ' |
| 12143 | 'to\n' |
| 12144 | ' undefined interpreter behaviour if exceptions raised by the\n' |
| 12145 | ' trace function escape to the function being traced.\n' |
| 12146 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12147 | ' "f_lineno" is the current line number of the frame — writing ' |
| 12148 | 'to\n' |
| 12149 | ' this from within a trace function jumps to the given line ' |
| 12150 | '(only\n' |
| 12151 | ' for the bottom-most frame). A debugger can implement a Jump\n' |
| 12152 | ' command (aka Set Next Statement) by writing to f_lineno.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12153 | '\n' |
| 12154 | ' Frame objects support one method:\n' |
| 12155 | '\n' |
| 12156 | ' frame.clear()\n' |
| 12157 | '\n' |
| 12158 | ' This method clears all references to local variables held ' |
| 12159 | 'by\n' |
| 12160 | ' the frame. Also, if the frame belonged to a generator, ' |
| 12161 | 'the\n' |
| 12162 | ' generator is finalized. This helps break reference ' |
| 12163 | 'cycles\n' |
| 12164 | ' involving frame objects (for example when catching an\n' |
| 12165 | ' exception and storing its traceback for later use).\n' |
| 12166 | '\n' |
| 12167 | ' "RuntimeError" is raised if the frame is currently ' |
| 12168 | 'executing.\n' |
| 12169 | '\n' |
| 12170 | ' New in version 3.4.\n' |
| 12171 | '\n' |
| 12172 | ' Traceback objects\n' |
| 12173 | ' Traceback objects represent a stack trace of an exception. ' |
| 12174 | 'A\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12175 | ' traceback object is implicitly created when an exception ' |
| 12176 | 'occurs,\n' |
| 12177 | ' and may also be explicitly created by calling\n' |
| 12178 | ' "types.TracebackType".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12179 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12180 | ' For implicitly created tracebacks, when the search for an\n' |
| 12181 | ' exception handler unwinds the execution stack, at each ' |
| 12182 | 'unwound\n' |
| 12183 | ' level a traceback object is inserted in front of the current\n' |
| 12184 | ' traceback. When an exception handler is entered, the stack\n' |
| 12185 | ' trace is made available to the program. (See section The try\n' |
| 12186 | ' statement.) It is accessible as the third item of the tuple\n' |
| 12187 | ' returned by "sys.exc_info()", and as the "__traceback__"\n' |
| 12188 | ' attribute of the caught exception.\n' |
| 12189 | '\n' |
| 12190 | ' When the program contains no suitable handler, the stack ' |
| 12191 | 'trace\n' |
| 12192 | ' is written (nicely formatted) to the standard error stream; ' |
| 12193 | 'if\n' |
| 12194 | ' the interpreter is interactive, it is also made available to ' |
| 12195 | 'the\n' |
| 12196 | ' user as "sys.last_traceback".\n' |
| 12197 | '\n' |
| 12198 | ' For explicitly created tracebacks, it is up to the creator ' |
| 12199 | 'of\n' |
| 12200 | ' the traceback to determine how the "tb_next" attributes ' |
| 12201 | 'should\n' |
| 12202 | ' be linked to form a full stack trace.\n' |
| 12203 | '\n' |
| 12204 | ' Special read-only attributes: "tb_frame" points to the ' |
| 12205 | 'execution\n' |
| 12206 | ' frame of the current level; "tb_lineno" gives the line ' |
| 12207 | 'number\n' |
| 12208 | ' where the exception occurred; "tb_lasti" indicates the ' |
| 12209 | 'precise\n' |
| 12210 | ' instruction. The line number and last instruction in the\n' |
| 12211 | ' traceback may differ from the line number of its frame object ' |
| 12212 | 'if\n' |
| 12213 | ' the exception occurred in a "try" statement with no matching\n' |
| 12214 | ' except clause or with a finally clause.\n' |
| 12215 | '\n' |
| 12216 | ' Special writable attribute: "tb_next" is the next level in ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12217 | 'the\n' |
| 12218 | ' stack trace (towards the frame where the exception occurred), ' |
| 12219 | 'or\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12220 | ' "None" if there is no next level.\n' |
| 12221 | '\n' |
| 12222 | ' Changed in version 3.7: Traceback objects can now be ' |
| 12223 | 'explicitly\n' |
| 12224 | ' instantiated from Python code, and the "tb_next" attribute ' |
| 12225 | 'of\n' |
| 12226 | ' existing instances can be updated.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12227 | '\n' |
| 12228 | ' Slice objects\n' |
| 12229 | ' Slice objects are used to represent slices for ' |
| 12230 | '"__getitem__()"\n' |
| 12231 | ' methods. They are also created by the built-in "slice()"\n' |
| 12232 | ' function.\n' |
| 12233 | '\n' |
| 12234 | ' Special read-only attributes: "start" is the lower bound; ' |
| 12235 | '"stop"\n' |
| 12236 | ' is the upper bound; "step" is the step value; each is "None" ' |
| 12237 | 'if\n' |
| 12238 | ' omitted. These attributes can have any type.\n' |
| 12239 | '\n' |
| 12240 | ' Slice objects support one method:\n' |
| 12241 | '\n' |
| 12242 | ' slice.indices(self, length)\n' |
| 12243 | '\n' |
| 12244 | ' This method takes a single integer argument *length* and\n' |
| 12245 | ' computes information about the slice that the slice ' |
| 12246 | 'object\n' |
| 12247 | ' would describe if applied to a sequence of *length* ' |
| 12248 | 'items.\n' |
| 12249 | ' It returns a tuple of three integers; respectively these ' |
| 12250 | 'are\n' |
| 12251 | ' the *start* and *stop* indices and the *step* or stride\n' |
| 12252 | ' length of the slice. Missing or out-of-bounds indices are\n' |
| 12253 | ' handled in a manner consistent with regular slices.\n' |
| 12254 | '\n' |
| 12255 | ' Static method objects\n' |
| 12256 | ' Static method objects provide a way of defeating the\n' |
| 12257 | ' transformation of function objects to method objects ' |
| 12258 | 'described\n' |
| 12259 | ' above. A static method object is a wrapper around any other\n' |
| 12260 | ' object, usually a user-defined method object. When a static\n' |
| 12261 | ' method object is retrieved from a class or a class instance, ' |
| 12262 | 'the\n' |
| 12263 | ' object actually returned is the wrapped object, which is not\n' |
| 12264 | ' subject to any further transformation. Static method objects ' |
| 12265 | 'are\n' |
| 12266 | ' not themselves callable, although the objects they wrap ' |
| 12267 | 'usually\n' |
| 12268 | ' are. Static method objects are created by the built-in\n' |
| 12269 | ' "staticmethod()" constructor.\n' |
| 12270 | '\n' |
| 12271 | ' Class method objects\n' |
| 12272 | ' A class method object, like a static method object, is a ' |
| 12273 | 'wrapper\n' |
| 12274 | ' around another object that alters the way in which that ' |
| 12275 | 'object\n' |
| 12276 | ' is retrieved from classes and class instances. The behaviour ' |
| 12277 | 'of\n' |
| 12278 | ' class method objects upon such retrieval is described above,\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12279 | ' under “User-defined methods”. Class method objects are ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12280 | 'created\n' |
| 12281 | ' by the built-in "classmethod()" constructor.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 12282 | 'typesfunctions': 'Functions\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12283 | '*********\n' |
| 12284 | '\n' |
| 12285 | 'Function objects are created by function definitions. The ' |
| 12286 | 'only\n' |
| 12287 | 'operation on a function object is to call it: ' |
| 12288 | '"func(argument-list)".\n' |
| 12289 | '\n' |
| 12290 | 'There are really two flavors of function objects: built-in ' |
| 12291 | 'functions\n' |
| 12292 | 'and user-defined functions. Both support the same ' |
| 12293 | 'operation (to call\n' |
| 12294 | 'the function), but the implementation is different, hence ' |
| 12295 | 'the\n' |
| 12296 | 'different object types.\n' |
| 12297 | '\n' |
| 12298 | 'See Function definitions for more information.\n', |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12299 | 'typesmapping': 'Mapping Types — "dict"\n' |
| 12300 | '**********************\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12301 | '\n' |
| 12302 | 'A *mapping* object maps *hashable* values to arbitrary ' |
| 12303 | 'objects.\n' |
| 12304 | 'Mappings are mutable objects. There is currently only one ' |
| 12305 | 'standard\n' |
| 12306 | 'mapping type, the *dictionary*. (For other containers see ' |
| 12307 | 'the built-\n' |
| 12308 | 'in "list", "set", and "tuple" classes, and the "collections" ' |
| 12309 | 'module.)\n' |
| 12310 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12311 | 'A dictionary’s keys are *almost* arbitrary values. Values ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12312 | 'that are\n' |
| 12313 | 'not *hashable*, that is, values containing lists, ' |
| 12314 | 'dictionaries or\n' |
| 12315 | 'other mutable types (that are compared by value rather than ' |
| 12316 | 'by object\n' |
| 12317 | 'identity) may not be used as keys. Numeric types used for ' |
| 12318 | 'keys obey\n' |
| 12319 | 'the normal rules for numeric comparison: if two numbers ' |
| 12320 | 'compare equal\n' |
| 12321 | '(such as "1" and "1.0") then they can be used ' |
| 12322 | 'interchangeably to index\n' |
| 12323 | 'the same dictionary entry. (Note however, that since ' |
| 12324 | 'computers store\n' |
| 12325 | 'floating-point numbers as approximations it is usually ' |
| 12326 | 'unwise to use\n' |
| 12327 | 'them as dictionary keys.)\n' |
| 12328 | '\n' |
| 12329 | 'Dictionaries can be created by placing a comma-separated ' |
| 12330 | 'list of "key:\n' |
| 12331 | 'value" pairs within braces, for example: "{\'jack\': 4098, ' |
| 12332 | "'sjoerd':\n" |
| 12333 | '4127}" or "{4098: \'jack\', 4127: \'sjoerd\'}", or by the ' |
| 12334 | '"dict"\n' |
| 12335 | 'constructor.\n' |
| 12336 | '\n' |
| 12337 | 'class dict(**kwarg)\n' |
| 12338 | 'class dict(mapping, **kwarg)\n' |
| 12339 | 'class dict(iterable, **kwarg)\n' |
| 12340 | '\n' |
| 12341 | ' Return a new dictionary initialized from an optional ' |
| 12342 | 'positional\n' |
| 12343 | ' argument and a possibly empty set of keyword arguments.\n' |
| 12344 | '\n' |
| 12345 | ' If no positional argument is given, an empty dictionary ' |
| 12346 | 'is created.\n' |
| 12347 | ' If a positional argument is given and it is a mapping ' |
| 12348 | 'object, a\n' |
| 12349 | ' dictionary is created with the same key-value pairs as ' |
| 12350 | 'the mapping\n' |
| 12351 | ' object. Otherwise, the positional argument must be an ' |
| 12352 | '*iterable*\n' |
| 12353 | ' object. Each item in the iterable must itself be an ' |
| 12354 | 'iterable with\n' |
| 12355 | ' exactly two objects. The first object of each item ' |
| 12356 | 'becomes a key\n' |
| 12357 | ' in the new dictionary, and the second object the ' |
| 12358 | 'corresponding\n' |
| 12359 | ' value. If a key occurs more than once, the last value ' |
| 12360 | 'for that key\n' |
| 12361 | ' becomes the corresponding value in the new dictionary.\n' |
| 12362 | '\n' |
| 12363 | ' If keyword arguments are given, the keyword arguments and ' |
| 12364 | 'their\n' |
| 12365 | ' values are added to the dictionary created from the ' |
| 12366 | 'positional\n' |
| 12367 | ' argument. If a key being added is already present, the ' |
| 12368 | 'value from\n' |
| 12369 | ' the keyword argument replaces the value from the ' |
| 12370 | 'positional\n' |
| 12371 | ' argument.\n' |
| 12372 | '\n' |
| 12373 | ' To illustrate, the following examples all return a ' |
| 12374 | 'dictionary equal\n' |
| 12375 | ' to "{"one": 1, "two": 2, "three": 3}":\n' |
| 12376 | '\n' |
| 12377 | ' >>> a = dict(one=1, two=2, three=3)\n' |
| 12378 | " >>> b = {'one': 1, 'two': 2, 'three': 3}\n" |
| 12379 | " >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))\n" |
| 12380 | " >>> d = dict([('two', 2), ('one', 1), ('three', 3)])\n" |
| 12381 | " >>> e = dict({'three': 3, 'one': 1, 'two': 2})\n" |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 12382 | " >>> f = dict({'one': 1, 'three': 3}, two=2)\n" |
| 12383 | ' >>> a == b == c == d == e == f\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12384 | ' True\n' |
| 12385 | '\n' |
| 12386 | ' Providing keyword arguments as in the first example only ' |
| 12387 | 'works for\n' |
| 12388 | ' keys that are valid Python identifiers. Otherwise, any ' |
| 12389 | 'valid keys\n' |
| 12390 | ' can be used.\n' |
| 12391 | '\n' |
| 12392 | ' These are the operations that dictionaries support (and ' |
| 12393 | 'therefore,\n' |
| 12394 | ' custom mapping types should support too):\n' |
| 12395 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 12396 | ' list(d)\n' |
| 12397 | '\n' |
| 12398 | ' Return a list of all the keys used in the dictionary ' |
| 12399 | '*d*.\n' |
| 12400 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12401 | ' len(d)\n' |
| 12402 | '\n' |
| 12403 | ' Return the number of items in the dictionary *d*.\n' |
| 12404 | '\n' |
| 12405 | ' d[key]\n' |
| 12406 | '\n' |
| 12407 | ' Return the item of *d* with key *key*. Raises a ' |
| 12408 | '"KeyError" if\n' |
| 12409 | ' *key* is not in the map.\n' |
| 12410 | '\n' |
| 12411 | ' If a subclass of dict defines a method "__missing__()" ' |
| 12412 | 'and *key*\n' |
| 12413 | ' is not present, the "d[key]" operation calls that ' |
| 12414 | 'method with\n' |
| 12415 | ' the key *key* as argument. The "d[key]" operation ' |
| 12416 | 'then returns\n' |
| 12417 | ' or raises whatever is returned or raised by the\n' |
| 12418 | ' "__missing__(key)" call. No other operations or ' |
| 12419 | 'methods invoke\n' |
| 12420 | ' "__missing__()". If "__missing__()" is not defined, ' |
| 12421 | '"KeyError"\n' |
| 12422 | ' is raised. "__missing__()" must be a method; it cannot ' |
| 12423 | 'be an\n' |
| 12424 | ' instance variable:\n' |
| 12425 | '\n' |
| 12426 | ' >>> class Counter(dict):\n' |
| 12427 | ' ... def __missing__(self, key):\n' |
| 12428 | ' ... return 0\n' |
| 12429 | ' >>> c = Counter()\n' |
| 12430 | " >>> c['red']\n" |
| 12431 | ' 0\n' |
| 12432 | " >>> c['red'] += 1\n" |
| 12433 | " >>> c['red']\n" |
| 12434 | ' 1\n' |
| 12435 | '\n' |
| 12436 | ' The example above shows part of the implementation of\n' |
| 12437 | ' "collections.Counter". A different "__missing__" ' |
| 12438 | 'method is used\n' |
| 12439 | ' by "collections.defaultdict".\n' |
| 12440 | '\n' |
| 12441 | ' d[key] = value\n' |
| 12442 | '\n' |
| 12443 | ' Set "d[key]" to *value*.\n' |
| 12444 | '\n' |
| 12445 | ' del d[key]\n' |
| 12446 | '\n' |
| 12447 | ' Remove "d[key]" from *d*. Raises a "KeyError" if ' |
| 12448 | '*key* is not\n' |
| 12449 | ' in the map.\n' |
| 12450 | '\n' |
| 12451 | ' key in d\n' |
| 12452 | '\n' |
| 12453 | ' Return "True" if *d* has a key *key*, else "False".\n' |
| 12454 | '\n' |
| 12455 | ' key not in d\n' |
| 12456 | '\n' |
| 12457 | ' Equivalent to "not key in d".\n' |
| 12458 | '\n' |
| 12459 | ' iter(d)\n' |
| 12460 | '\n' |
| 12461 | ' Return an iterator over the keys of the dictionary. ' |
| 12462 | 'This is a\n' |
| 12463 | ' shortcut for "iter(d.keys())".\n' |
| 12464 | '\n' |
| 12465 | ' clear()\n' |
| 12466 | '\n' |
| 12467 | ' Remove all items from the dictionary.\n' |
| 12468 | '\n' |
| 12469 | ' copy()\n' |
| 12470 | '\n' |
| 12471 | ' Return a shallow copy of the dictionary.\n' |
| 12472 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12473 | ' classmethod fromkeys(iterable[, value])\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12474 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12475 | ' Create a new dictionary with keys from *iterable* and ' |
| 12476 | 'values set\n' |
| 12477 | ' to *value*.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12478 | '\n' |
| 12479 | ' "fromkeys()" is a class method that returns a new ' |
| 12480 | 'dictionary.\n' |
Łukasz Langa | c1004b8 | 2019-05-06 20:30:25 +0200 | [diff] [blame] | 12481 | ' *value* defaults to "None". All of the values refer ' |
| 12482 | 'to just a\n' |
| 12483 | ' single instance, so it generally doesn’t make sense ' |
| 12484 | 'for *value*\n' |
| 12485 | ' to be a mutable object such as an empty list. To get ' |
| 12486 | 'distinct\n' |
| 12487 | ' values, use a dict comprehension instead.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12488 | '\n' |
| 12489 | ' get(key[, default])\n' |
| 12490 | '\n' |
| 12491 | ' Return the value for *key* if *key* is in the ' |
| 12492 | 'dictionary, else\n' |
| 12493 | ' *default*. If *default* is not given, it defaults to ' |
| 12494 | '"None", so\n' |
| 12495 | ' that this method never raises a "KeyError".\n' |
| 12496 | '\n' |
| 12497 | ' items()\n' |
| 12498 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12499 | ' Return a new view of the dictionary’s items ("(key, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12500 | 'value)"\n' |
| 12501 | ' pairs). See the documentation of view objects.\n' |
| 12502 | '\n' |
| 12503 | ' keys()\n' |
| 12504 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12505 | ' Return a new view of the dictionary’s keys. See the\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12506 | ' documentation of view objects.\n' |
| 12507 | '\n' |
| 12508 | ' pop(key[, default])\n' |
| 12509 | '\n' |
| 12510 | ' If *key* is in the dictionary, remove it and return ' |
| 12511 | 'its value,\n' |
| 12512 | ' else return *default*. If *default* is not given and ' |
| 12513 | '*key* is\n' |
| 12514 | ' not in the dictionary, a "KeyError" is raised.\n' |
| 12515 | '\n' |
| 12516 | ' popitem()\n' |
| 12517 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12518 | ' Remove and return a "(key, value)" pair from the ' |
| 12519 | 'dictionary.\n' |
| 12520 | ' Pairs are returned in LIFO (last-in, first-out) ' |
| 12521 | 'order.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12522 | '\n' |
| 12523 | ' "popitem()" is useful to destructively iterate over a\n' |
| 12524 | ' dictionary, as often used in set algorithms. If the ' |
| 12525 | 'dictionary\n' |
| 12526 | ' is empty, calling "popitem()" raises a "KeyError".\n' |
| 12527 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12528 | ' Changed in version 3.7: LIFO order is now guaranteed. ' |
| 12529 | 'In prior\n' |
| 12530 | ' versions, "popitem()" would return an arbitrary ' |
| 12531 | 'key/value pair.\n' |
| 12532 | '\n' |
| 12533 | ' reversed(d)\n' |
| 12534 | '\n' |
| 12535 | ' Return a reverse iterator over the keys of the ' |
| 12536 | 'dictionary. This\n' |
| 12537 | ' is a shortcut for "reversed(d.keys())".\n' |
| 12538 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 12539 | ' New in version 3.8.\n' |
| 12540 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12541 | ' setdefault(key[, default])\n' |
| 12542 | '\n' |
| 12543 | ' If *key* is in the dictionary, return its value. If ' |
| 12544 | 'not, insert\n' |
| 12545 | ' *key* with a value of *default* and return *default*. ' |
| 12546 | '*default*\n' |
| 12547 | ' defaults to "None".\n' |
| 12548 | '\n' |
| 12549 | ' update([other])\n' |
| 12550 | '\n' |
| 12551 | ' Update the dictionary with the key/value pairs from ' |
| 12552 | '*other*,\n' |
| 12553 | ' overwriting existing keys. Return "None".\n' |
| 12554 | '\n' |
| 12555 | ' "update()" accepts either another dictionary object or ' |
| 12556 | 'an\n' |
| 12557 | ' iterable of key/value pairs (as tuples or other ' |
| 12558 | 'iterables of\n' |
| 12559 | ' length two). If keyword arguments are specified, the ' |
| 12560 | 'dictionary\n' |
| 12561 | ' is then updated with those key/value pairs: ' |
| 12562 | '"d.update(red=1,\n' |
| 12563 | ' blue=2)".\n' |
| 12564 | '\n' |
| 12565 | ' values()\n' |
| 12566 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12567 | ' Return a new view of the dictionary’s values. See ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12568 | 'the\n' |
| 12569 | ' documentation of view objects.\n' |
| 12570 | '\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 12571 | ' An equality comparison between one "dict.values()" ' |
| 12572 | 'view and\n' |
| 12573 | ' another will always return "False". This also applies ' |
| 12574 | 'when\n' |
| 12575 | ' comparing "dict.values()" to itself:\n' |
| 12576 | '\n' |
| 12577 | " >>> d = {'a': 1}\n" |
| 12578 | ' >>> d.values() == d.values()\n' |
| 12579 | ' False\n' |
| 12580 | '\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 12581 | ' d | other\n' |
| 12582 | '\n' |
| 12583 | ' Create a new dictionary with the merged keys and ' |
| 12584 | 'values of *d*\n' |
| 12585 | ' and *other*, which must both be dictionaries. The ' |
| 12586 | 'values of\n' |
| 12587 | ' *other* take priority when *d* and *other* share ' |
| 12588 | 'keys.\n' |
| 12589 | '\n' |
| 12590 | ' New in version 3.9.\n' |
| 12591 | '\n' |
| 12592 | ' d |= other\n' |
| 12593 | '\n' |
| 12594 | ' Update the dictionary *d* with keys and values from ' |
| 12595 | '*other*,\n' |
| 12596 | ' which may be either a *mapping* or an *iterable* of ' |
| 12597 | 'key/value\n' |
| 12598 | ' pairs. The values of *other* take priority when *d* ' |
| 12599 | 'and *other*\n' |
| 12600 | ' share keys.\n' |
| 12601 | '\n' |
| 12602 | ' New in version 3.9.\n' |
| 12603 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12604 | ' Dictionaries compare equal if and only if they have the ' |
| 12605 | 'same "(key,\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 12606 | ' value)" pairs (regardless of ordering). Order comparisons ' |
| 12607 | '(‘<’,\n' |
| 12608 | ' ‘<=’, ‘>=’, ‘>’) raise "TypeError".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12609 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12610 | ' Dictionaries preserve insertion order. Note that ' |
| 12611 | 'updating a key\n' |
| 12612 | ' does not affect the order. Keys added after deletion are ' |
| 12613 | 'inserted\n' |
| 12614 | ' at the end.\n' |
| 12615 | '\n' |
| 12616 | ' >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}\n' |
| 12617 | ' >>> d\n' |
| 12618 | " {'one': 1, 'two': 2, 'three': 3, 'four': 4}\n" |
| 12619 | ' >>> list(d)\n' |
| 12620 | " ['one', 'two', 'three', 'four']\n" |
| 12621 | ' >>> list(d.values())\n' |
| 12622 | ' [1, 2, 3, 4]\n' |
| 12623 | ' >>> d["one"] = 42\n' |
| 12624 | ' >>> d\n' |
| 12625 | " {'one': 42, 'two': 2, 'three': 3, 'four': 4}\n" |
| 12626 | ' >>> del d["two"]\n' |
| 12627 | ' >>> d["two"] = None\n' |
| 12628 | ' >>> d\n' |
| 12629 | " {'one': 42, 'three': 3, 'four': 4, 'two': None}\n" |
| 12630 | '\n' |
| 12631 | ' Changed in version 3.7: Dictionary order is guaranteed to ' |
| 12632 | 'be\n' |
| 12633 | ' insertion order. This behavior was an implementation ' |
| 12634 | 'detail of\n' |
| 12635 | ' CPython from 3.6.\n' |
| 12636 | '\n' |
| 12637 | ' Dictionaries and dictionary views are reversible.\n' |
| 12638 | '\n' |
| 12639 | ' >>> d = {"one": 1, "two": 2, "three": 3, "four": 4}\n' |
| 12640 | ' >>> d\n' |
| 12641 | " {'one': 1, 'two': 2, 'three': 3, 'four': 4}\n" |
| 12642 | ' >>> list(reversed(d))\n' |
| 12643 | " ['four', 'three', 'two', 'one']\n" |
| 12644 | ' >>> list(reversed(d.values()))\n' |
| 12645 | ' [4, 3, 2, 1]\n' |
| 12646 | ' >>> list(reversed(d.items()))\n' |
| 12647 | " [('four', 4), ('three', 3), ('two', 2), ('one', 1)]\n" |
| 12648 | '\n' |
| 12649 | ' Changed in version 3.8: Dictionaries are now reversible.\n' |
| 12650 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12651 | 'See also: "types.MappingProxyType" can be used to create a ' |
| 12652 | 'read-only\n' |
| 12653 | ' view of a "dict".\n' |
| 12654 | '\n' |
| 12655 | '\n' |
| 12656 | 'Dictionary view objects\n' |
| 12657 | '=======================\n' |
| 12658 | '\n' |
| 12659 | 'The objects returned by "dict.keys()", "dict.values()" and\n' |
| 12660 | '"dict.items()" are *view objects*. They provide a dynamic ' |
| 12661 | 'view on the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12662 | 'dictionary’s entries, which means that when the dictionary ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12663 | 'changes,\n' |
| 12664 | 'the view reflects these changes.\n' |
| 12665 | '\n' |
| 12666 | 'Dictionary views can be iterated over to yield their ' |
| 12667 | 'respective data,\n' |
| 12668 | 'and support membership tests:\n' |
| 12669 | '\n' |
| 12670 | 'len(dictview)\n' |
| 12671 | '\n' |
| 12672 | ' Return the number of entries in the dictionary.\n' |
| 12673 | '\n' |
| 12674 | 'iter(dictview)\n' |
| 12675 | '\n' |
| 12676 | ' Return an iterator over the keys, values or items ' |
| 12677 | '(represented as\n' |
| 12678 | ' tuples of "(key, value)") in the dictionary.\n' |
| 12679 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12680 | ' Keys and values are iterated over in insertion order. ' |
| 12681 | 'This allows\n' |
| 12682 | ' the creation of "(value, key)" pairs using "zip()": ' |
| 12683 | '"pairs =\n' |
| 12684 | ' zip(d.values(), d.keys())". Another way to create the ' |
| 12685 | 'same list is\n' |
| 12686 | ' "pairs = [(v, k) for (k, v) in d.items()]".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12687 | '\n' |
| 12688 | ' Iterating views while adding or deleting entries in the ' |
| 12689 | 'dictionary\n' |
| 12690 | ' may raise a "RuntimeError" or fail to iterate over all ' |
| 12691 | 'entries.\n' |
| 12692 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12693 | ' Changed in version 3.7: Dictionary order is guaranteed to ' |
| 12694 | 'be\n' |
| 12695 | ' insertion order.\n' |
| 12696 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12697 | 'x in dictview\n' |
| 12698 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12699 | ' Return "True" if *x* is in the underlying dictionary’s ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12700 | 'keys, values\n' |
| 12701 | ' or items (in the latter case, *x* should be a "(key, ' |
| 12702 | 'value)"\n' |
| 12703 | ' tuple).\n' |
| 12704 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12705 | 'reversed(dictview)\n' |
| 12706 | '\n' |
| 12707 | ' Return a reverse iterator over the keys, values or items ' |
| 12708 | 'of the\n' |
| 12709 | ' dictionary. The view will be iterated in reverse order of ' |
| 12710 | 'the\n' |
| 12711 | ' insertion.\n' |
| 12712 | '\n' |
| 12713 | ' Changed in version 3.8: Dictionary views are now ' |
| 12714 | 'reversible.\n' |
| 12715 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12716 | 'Keys views are set-like since their entries are unique and ' |
| 12717 | 'hashable.\n' |
| 12718 | 'If all values are hashable, so that "(key, value)" pairs are ' |
| 12719 | 'unique\n' |
| 12720 | 'and hashable, then the items view is also set-like. (Values ' |
| 12721 | 'views are\n' |
| 12722 | 'not treated as set-like since the entries are generally not ' |
| 12723 | 'unique.)\n' |
| 12724 | 'For set-like views, all of the operations defined for the ' |
| 12725 | 'abstract\n' |
| 12726 | 'base class "collections.abc.Set" are available (for example, ' |
| 12727 | '"==",\n' |
| 12728 | '"<", or "^").\n' |
| 12729 | '\n' |
| 12730 | 'An example of dictionary view usage:\n' |
| 12731 | '\n' |
| 12732 | " >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, " |
| 12733 | "'spam': 500}\n" |
| 12734 | ' >>> keys = dishes.keys()\n' |
| 12735 | ' >>> values = dishes.values()\n' |
| 12736 | '\n' |
| 12737 | ' >>> # iteration\n' |
| 12738 | ' >>> n = 0\n' |
| 12739 | ' >>> for val in values:\n' |
| 12740 | ' ... n += val\n' |
| 12741 | ' >>> print(n)\n' |
| 12742 | ' 504\n' |
| 12743 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12744 | ' >>> # keys and values are iterated over in the same order ' |
| 12745 | '(insertion order)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12746 | ' >>> list(keys)\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12747 | " ['eggs', 'sausage', 'bacon', 'spam']\n" |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12748 | ' >>> list(values)\n' |
| 12749 | ' [2, 1, 1, 500]\n' |
| 12750 | '\n' |
| 12751 | ' >>> # view objects are dynamic and reflect dict changes\n' |
| 12752 | " >>> del dishes['eggs']\n" |
| 12753 | " >>> del dishes['sausage']\n" |
| 12754 | ' >>> list(keys)\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12755 | " ['bacon', 'spam']\n" |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12756 | '\n' |
| 12757 | ' >>> # set operations\n' |
| 12758 | " >>> keys & {'eggs', 'bacon', 'salad'}\n" |
| 12759 | " {'bacon'}\n" |
| 12760 | " >>> keys ^ {'sausage', 'juice'}\n" |
| 12761 | " {'juice', 'sausage', 'bacon', 'spam'}\n", |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 12762 | 'typesmethods': 'Methods\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12763 | '*******\n' |
| 12764 | '\n' |
| 12765 | 'Methods are functions that are called using the attribute ' |
| 12766 | 'notation.\n' |
| 12767 | 'There are two flavors: built-in methods (such as "append()" ' |
| 12768 | 'on lists)\n' |
| 12769 | 'and class instance methods. Built-in methods are described ' |
| 12770 | 'with the\n' |
| 12771 | 'types that support them.\n' |
| 12772 | '\n' |
| 12773 | 'If you access a method (a function defined in a class ' |
| 12774 | 'namespace)\n' |
| 12775 | 'through an instance, you get a special object: a *bound ' |
| 12776 | 'method* (also\n' |
| 12777 | 'called *instance method*) object. When called, it will add ' |
| 12778 | 'the "self"\n' |
| 12779 | 'argument to the argument list. Bound methods have two ' |
| 12780 | 'special read-\n' |
| 12781 | 'only attributes: "m.__self__" is the object on which the ' |
| 12782 | 'method\n' |
| 12783 | 'operates, and "m.__func__" is the function implementing the ' |
| 12784 | 'method.\n' |
| 12785 | 'Calling "m(arg-1, arg-2, ..., arg-n)" is completely ' |
| 12786 | 'equivalent to\n' |
| 12787 | 'calling "m.__func__(m.__self__, arg-1, arg-2, ..., arg-n)".\n' |
| 12788 | '\n' |
| 12789 | 'Like function objects, bound method objects support getting ' |
| 12790 | 'arbitrary\n' |
| 12791 | 'attributes. However, since method attributes are actually ' |
| 12792 | 'stored on\n' |
| 12793 | 'the underlying function object ("meth.__func__"), setting ' |
| 12794 | 'method\n' |
| 12795 | 'attributes on bound methods is disallowed. Attempting to ' |
| 12796 | 'set an\n' |
| 12797 | 'attribute on a method results in an "AttributeError" being ' |
| 12798 | 'raised. In\n' |
| 12799 | 'order to set a method attribute, you need to explicitly set ' |
| 12800 | 'it on the\n' |
| 12801 | 'underlying function object:\n' |
| 12802 | '\n' |
| 12803 | ' >>> class C:\n' |
| 12804 | ' ... def method(self):\n' |
| 12805 | ' ... pass\n' |
| 12806 | ' ...\n' |
| 12807 | ' >>> c = C()\n' |
| 12808 | " >>> c.method.whoami = 'my name is method' # can't set on " |
| 12809 | 'the method\n' |
| 12810 | ' Traceback (most recent call last):\n' |
| 12811 | ' File "<stdin>", line 1, in <module>\n' |
| 12812 | " AttributeError: 'method' object has no attribute " |
| 12813 | "'whoami'\n" |
| 12814 | " >>> c.method.__func__.whoami = 'my name is method'\n" |
| 12815 | ' >>> c.method.whoami\n' |
| 12816 | " 'my name is method'\n" |
| 12817 | '\n' |
| 12818 | 'See The standard type hierarchy for more information.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 12819 | 'typesmodules': 'Modules\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12820 | '*******\n' |
| 12821 | '\n' |
| 12822 | 'The only special operation on a module is attribute access: ' |
| 12823 | '"m.name",\n' |
| 12824 | 'where *m* is a module and *name* accesses a name defined in ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12825 | '*m*’s\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12826 | 'symbol table. Module attributes can be assigned to. (Note ' |
| 12827 | 'that the\n' |
| 12828 | '"import" statement is not, strictly speaking, an operation ' |
| 12829 | 'on a module\n' |
| 12830 | 'object; "import foo" does not require a module object named ' |
| 12831 | '*foo* to\n' |
| 12832 | 'exist, rather it requires an (external) *definition* for a ' |
| 12833 | 'module\n' |
| 12834 | 'named *foo* somewhere.)\n' |
| 12835 | '\n' |
| 12836 | 'A special attribute of every module is "__dict__". This is ' |
| 12837 | 'the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12838 | 'dictionary containing the module’s symbol table. Modifying ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12839 | 'this\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12840 | 'dictionary will actually change the module’s symbol table, ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12841 | 'but direct\n' |
| 12842 | 'assignment to the "__dict__" attribute is not possible (you ' |
| 12843 | 'can write\n' |
| 12844 | '"m.__dict__[\'a\'] = 1", which defines "m.a" to be "1", but ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12845 | 'you can’t\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12846 | 'write "m.__dict__ = {}"). Modifying "__dict__" directly is ' |
| 12847 | 'not\n' |
| 12848 | 'recommended.\n' |
| 12849 | '\n' |
| 12850 | 'Modules built into the interpreter are written like this: ' |
| 12851 | '"<module\n' |
| 12852 | '\'sys\' (built-in)>". If loaded from a file, they are ' |
| 12853 | 'written as\n' |
| 12854 | '"<module \'os\' from ' |
| 12855 | '\'/usr/local/lib/pythonX.Y/os.pyc\'>".\n', |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 12856 | 'typesseq': 'Sequence Types — "list", "tuple", "range"\n' |
| 12857 | '*****************************************\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12858 | '\n' |
| 12859 | 'There are three basic sequence types: lists, tuples, and range\n' |
| 12860 | 'objects. Additional sequence types tailored for processing of ' |
| 12861 | 'binary\n' |
| 12862 | 'data and text strings are described in dedicated sections.\n' |
| 12863 | '\n' |
| 12864 | '\n' |
| 12865 | 'Common Sequence Operations\n' |
| 12866 | '==========================\n' |
| 12867 | '\n' |
| 12868 | 'The operations in the following table are supported by most ' |
| 12869 | 'sequence\n' |
| 12870 | 'types, both mutable and immutable. The ' |
| 12871 | '"collections.abc.Sequence" ABC\n' |
| 12872 | 'is provided to make it easier to correctly implement these ' |
| 12873 | 'operations\n' |
| 12874 | 'on custom sequence types.\n' |
| 12875 | '\n' |
| 12876 | 'This table lists the sequence operations sorted in ascending ' |
| 12877 | 'priority.\n' |
| 12878 | 'In the table, *s* and *t* are sequences of the same type, *n*, ' |
| 12879 | '*i*,\n' |
| 12880 | '*j* and *k* are integers and *x* is an arbitrary object that ' |
| 12881 | 'meets any\n' |
| 12882 | 'type and value restrictions imposed by *s*.\n' |
| 12883 | '\n' |
| 12884 | 'The "in" and "not in" operations have the same priorities as ' |
| 12885 | 'the\n' |
| 12886 | 'comparison operations. The "+" (concatenation) and "*" ' |
| 12887 | '(repetition)\n' |
| 12888 | 'operations have the same priority as the corresponding numeric\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 12889 | 'operations. [3]\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12890 | '\n' |
| 12891 | '+----------------------------+----------------------------------+------------+\n' |
| 12892 | '| Operation | Result ' |
| 12893 | '| Notes |\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 12894 | '|============================|==================================|============|\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 12895 | '| "x in s" | "True" if an item of *s* is ' |
| 12896 | '| (1) |\n' |
| 12897 | '| | equal to *x*, else "False" ' |
| 12898 | '| |\n' |
| 12899 | '+----------------------------+----------------------------------+------------+\n' |
| 12900 | '| "x not in s" | "False" if an item of *s* is ' |
| 12901 | '| (1) |\n' |
| 12902 | '| | equal to *x*, else "True" ' |
| 12903 | '| |\n' |
| 12904 | '+----------------------------+----------------------------------+------------+\n' |
| 12905 | '| "s + t" | the concatenation of *s* and *t* ' |
| 12906 | '| (6)(7) |\n' |
| 12907 | '+----------------------------+----------------------------------+------------+\n' |
| 12908 | '| "s * n" or "n * s" | equivalent to adding *s* to ' |
| 12909 | '| (2)(7) |\n' |
| 12910 | '| | itself *n* times ' |
| 12911 | '| |\n' |
| 12912 | '+----------------------------+----------------------------------+------------+\n' |
| 12913 | '| "s[i]" | *i*th item of *s*, origin 0 ' |
| 12914 | '| (3) |\n' |
| 12915 | '+----------------------------+----------------------------------+------------+\n' |
| 12916 | '| "s[i:j]" | slice of *s* from *i* to *j* ' |
| 12917 | '| (3)(4) |\n' |
| 12918 | '+----------------------------+----------------------------------+------------+\n' |
| 12919 | '| "s[i:j:k]" | slice of *s* from *i* to *j* ' |
| 12920 | '| (3)(5) |\n' |
| 12921 | '| | with step *k* ' |
| 12922 | '| |\n' |
| 12923 | '+----------------------------+----------------------------------+------------+\n' |
| 12924 | '| "len(s)" | length of *s* ' |
| 12925 | '| |\n' |
| 12926 | '+----------------------------+----------------------------------+------------+\n' |
| 12927 | '| "min(s)" | smallest item of *s* ' |
| 12928 | '| |\n' |
| 12929 | '+----------------------------+----------------------------------+------------+\n' |
| 12930 | '| "max(s)" | largest item of *s* ' |
| 12931 | '| |\n' |
| 12932 | '+----------------------------+----------------------------------+------------+\n' |
| 12933 | '| "s.index(x[, i[, j]])" | index of the first occurrence of ' |
| 12934 | '| (8) |\n' |
| 12935 | '| | *x* in *s* (at or after index ' |
| 12936 | '| |\n' |
| 12937 | '| | *i* and before index *j*) ' |
| 12938 | '| |\n' |
| 12939 | '+----------------------------+----------------------------------+------------+\n' |
| 12940 | '| "s.count(x)" | total number of occurrences of ' |
| 12941 | '| |\n' |
| 12942 | '| | *x* in *s* ' |
| 12943 | '| |\n' |
| 12944 | '+----------------------------+----------------------------------+------------+\n' |
| 12945 | '\n' |
| 12946 | 'Sequences of the same type also support comparisons. In ' |
| 12947 | 'particular,\n' |
| 12948 | 'tuples and lists are compared lexicographically by comparing\n' |
| 12949 | 'corresponding elements. This means that to compare equal, every\n' |
| 12950 | 'element must compare equal and the two sequences must be of the ' |
| 12951 | 'same\n' |
| 12952 | 'type and have the same length. (For full details see ' |
| 12953 | 'Comparisons in\n' |
| 12954 | 'the language reference.)\n' |
| 12955 | '\n' |
| 12956 | 'Notes:\n' |
| 12957 | '\n' |
| 12958 | '1. While the "in" and "not in" operations are used only for ' |
| 12959 | 'simple\n' |
| 12960 | ' containment testing in the general case, some specialised ' |
| 12961 | 'sequences\n' |
| 12962 | ' (such as "str", "bytes" and "bytearray") also use them for\n' |
| 12963 | ' subsequence testing:\n' |
| 12964 | '\n' |
| 12965 | ' >>> "gg" in "eggs"\n' |
| 12966 | ' True\n' |
| 12967 | '\n' |
| 12968 | '2. Values of *n* less than "0" are treated as "0" (which yields ' |
| 12969 | 'an\n' |
| 12970 | ' empty sequence of the same type as *s*). Note that items in ' |
| 12971 | 'the\n' |
| 12972 | ' sequence *s* are not copied; they are referenced multiple ' |
| 12973 | 'times.\n' |
| 12974 | ' This often haunts new Python programmers; consider:\n' |
| 12975 | '\n' |
| 12976 | ' >>> lists = [[]] * 3\n' |
| 12977 | ' >>> lists\n' |
| 12978 | ' [[], [], []]\n' |
| 12979 | ' >>> lists[0].append(3)\n' |
| 12980 | ' >>> lists\n' |
| 12981 | ' [[3], [3], [3]]\n' |
| 12982 | '\n' |
| 12983 | ' What has happened is that "[[]]" is a one-element list ' |
| 12984 | 'containing\n' |
| 12985 | ' an empty list, so all three elements of "[[]] * 3" are ' |
| 12986 | 'references\n' |
| 12987 | ' to this single empty list. Modifying any of the elements of\n' |
| 12988 | ' "lists" modifies this single list. You can create a list of\n' |
| 12989 | ' different lists this way:\n' |
| 12990 | '\n' |
| 12991 | ' >>> lists = [[] for i in range(3)]\n' |
| 12992 | ' >>> lists[0].append(3)\n' |
| 12993 | ' >>> lists[1].append(5)\n' |
| 12994 | ' >>> lists[2].append(7)\n' |
| 12995 | ' >>> lists\n' |
| 12996 | ' [[3], [5], [7]]\n' |
| 12997 | '\n' |
| 12998 | ' Further explanation is available in the FAQ entry How do I ' |
| 12999 | 'create a\n' |
| 13000 | ' multidimensional list?.\n' |
| 13001 | '\n' |
| 13002 | '3. If *i* or *j* is negative, the index is relative to the end ' |
| 13003 | 'of\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 13004 | ' sequence *s*: "len(s) + i" or "len(s) + j" is substituted. ' |
| 13005 | 'But\n' |
| 13006 | ' note that "-0" is still "0".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13007 | '\n' |
| 13008 | '4. The slice of *s* from *i* to *j* is defined as the sequence ' |
| 13009 | 'of\n' |
| 13010 | ' items with index *k* such that "i <= k < j". If *i* or *j* ' |
| 13011 | 'is\n' |
| 13012 | ' greater than "len(s)", use "len(s)". If *i* is omitted or ' |
| 13013 | '"None",\n' |
| 13014 | ' use "0". If *j* is omitted or "None", use "len(s)". If *i* ' |
| 13015 | 'is\n' |
| 13016 | ' greater than or equal to *j*, the slice is empty.\n' |
| 13017 | '\n' |
| 13018 | '5. The slice of *s* from *i* to *j* with step *k* is defined as ' |
| 13019 | 'the\n' |
| 13020 | ' sequence of items with index "x = i + n*k" such that "0 <= n ' |
| 13021 | '<\n' |
| 13022 | ' (j-i)/k". In other words, the indices are "i", "i+k", ' |
| 13023 | '"i+2*k",\n' |
| 13024 | ' "i+3*k" and so on, stopping when *j* is reached (but never\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 13025 | ' including *j*). When *k* is positive, *i* and *j* are ' |
| 13026 | 'reduced to\n' |
| 13027 | ' "len(s)" if they are greater. When *k* is negative, *i* and ' |
| 13028 | '*j* are\n' |
| 13029 | ' reduced to "len(s) - 1" if they are greater. If *i* or *j* ' |
| 13030 | 'are\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13031 | ' omitted or "None", they become “end” values (which end ' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 13032 | 'depends on\n' |
| 13033 | ' the sign of *k*). Note, *k* cannot be zero. If *k* is ' |
| 13034 | '"None", it\n' |
| 13035 | ' is treated like "1".\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13036 | '\n' |
| 13037 | '6. Concatenating immutable sequences always results in a new\n' |
| 13038 | ' object. This means that building up a sequence by repeated\n' |
| 13039 | ' concatenation will have a quadratic runtime cost in the ' |
| 13040 | 'total\n' |
| 13041 | ' sequence length. To get a linear runtime cost, you must ' |
| 13042 | 'switch to\n' |
| 13043 | ' one of the alternatives below:\n' |
| 13044 | '\n' |
| 13045 | ' * if concatenating "str" objects, you can build a list and ' |
| 13046 | 'use\n' |
| 13047 | ' "str.join()" at the end or else write to an "io.StringIO"\n' |
| 13048 | ' instance and retrieve its value when complete\n' |
| 13049 | '\n' |
| 13050 | ' * if concatenating "bytes" objects, you can similarly use\n' |
| 13051 | ' "bytes.join()" or "io.BytesIO", or you can do in-place\n' |
| 13052 | ' concatenation with a "bytearray" object. "bytearray" ' |
| 13053 | 'objects are\n' |
| 13054 | ' mutable and have an efficient overallocation mechanism\n' |
| 13055 | '\n' |
| 13056 | ' * if concatenating "tuple" objects, extend a "list" instead\n' |
| 13057 | '\n' |
| 13058 | ' * for other types, investigate the relevant class ' |
| 13059 | 'documentation\n' |
| 13060 | '\n' |
| 13061 | '7. Some sequence types (such as "range") only support item\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13062 | ' sequences that follow specific patterns, and hence don’t ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13063 | 'support\n' |
| 13064 | ' sequence concatenation or repetition.\n' |
| 13065 | '\n' |
| 13066 | '8. "index" raises "ValueError" when *x* is not found in *s*. ' |
Ned Deily | 3b43bfa | 2018-01-08 21:57:13 -0500 | [diff] [blame] | 13067 | 'Not\n' |
| 13068 | ' all implementations support passing the additional arguments ' |
| 13069 | '*i*\n' |
| 13070 | ' and *j*. These arguments allow efficient searching of ' |
| 13071 | 'subsections\n' |
| 13072 | ' of the sequence. Passing the extra arguments is roughly ' |
| 13073 | 'equivalent\n' |
| 13074 | ' to using "s[i:j].index(x)", only without copying any data and ' |
| 13075 | 'with\n' |
| 13076 | ' the returned index being relative to the start of the ' |
| 13077 | 'sequence\n' |
| 13078 | ' rather than the start of the slice.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13079 | '\n' |
| 13080 | '\n' |
| 13081 | 'Immutable Sequence Types\n' |
| 13082 | '========================\n' |
| 13083 | '\n' |
| 13084 | 'The only operation that immutable sequence types generally ' |
| 13085 | 'implement\n' |
| 13086 | 'that is not also implemented by mutable sequence types is ' |
| 13087 | 'support for\n' |
| 13088 | 'the "hash()" built-in.\n' |
| 13089 | '\n' |
| 13090 | 'This support allows immutable sequences, such as "tuple" ' |
| 13091 | 'instances, to\n' |
| 13092 | 'be used as "dict" keys and stored in "set" and "frozenset" ' |
| 13093 | 'instances.\n' |
| 13094 | '\n' |
| 13095 | 'Attempting to hash an immutable sequence that contains ' |
| 13096 | 'unhashable\n' |
| 13097 | 'values will result in "TypeError".\n' |
| 13098 | '\n' |
| 13099 | '\n' |
| 13100 | 'Mutable Sequence Types\n' |
| 13101 | '======================\n' |
| 13102 | '\n' |
| 13103 | 'The operations in the following table are defined on mutable ' |
| 13104 | 'sequence\n' |
| 13105 | 'types. The "collections.abc.MutableSequence" ABC is provided to ' |
| 13106 | 'make\n' |
| 13107 | 'it easier to correctly implement these operations on custom ' |
| 13108 | 'sequence\n' |
| 13109 | 'types.\n' |
| 13110 | '\n' |
| 13111 | 'In the table *s* is an instance of a mutable sequence type, *t* ' |
| 13112 | 'is any\n' |
| 13113 | 'iterable object and *x* is an arbitrary object that meets any ' |
| 13114 | 'type and\n' |
| 13115 | 'value restrictions imposed by *s* (for example, "bytearray" ' |
| 13116 | 'only\n' |
| 13117 | 'accepts integers that meet the value restriction "0 <= x <= ' |
| 13118 | '255").\n' |
| 13119 | '\n' |
| 13120 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13121 | '| Operation | ' |
| 13122 | 'Result | Notes |\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 13123 | '|================================|==================================|=======================|\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13124 | '| "s[i] = x" | item *i* of *s* is replaced ' |
| 13125 | 'by | |\n' |
| 13126 | '| | ' |
| 13127 | '*x* | |\n' |
| 13128 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13129 | '| "s[i:j] = t" | slice of *s* from *i* to *j* ' |
| 13130 | 'is | |\n' |
| 13131 | '| | replaced by the contents of ' |
| 13132 | 'the | |\n' |
| 13133 | '| | iterable ' |
| 13134 | '*t* | |\n' |
| 13135 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13136 | '| "del s[i:j]" | same as "s[i:j] = ' |
| 13137 | '[]" | |\n' |
| 13138 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13139 | '| "s[i:j:k] = t" | the elements of "s[i:j:k]" ' |
| 13140 | 'are | (1) |\n' |
| 13141 | '| | replaced by those of ' |
| 13142 | '*t* | |\n' |
| 13143 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13144 | '| "del s[i:j:k]" | removes the elements ' |
| 13145 | 'of | |\n' |
| 13146 | '| | "s[i:j:k]" from the ' |
| 13147 | 'list | |\n' |
| 13148 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13149 | '| "s.append(x)" | appends *x* to the end of ' |
| 13150 | 'the | |\n' |
| 13151 | '| | sequence (same ' |
| 13152 | 'as | |\n' |
| 13153 | '| | "s[len(s):len(s)] = ' |
| 13154 | '[x]") | |\n' |
| 13155 | '+--------------------------------+----------------------------------+-----------------------+\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13156 | '| "s.clear()" | removes all items from *s* ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13157 | '(same | (5) |\n' |
| 13158 | '| | as "del ' |
| 13159 | 's[:]") | |\n' |
| 13160 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13161 | '| "s.copy()" | creates a shallow copy of ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13162 | '*s* | (5) |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13163 | '| | (same as ' |
| 13164 | '"s[:]") | |\n' |
| 13165 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13166 | '| "s.extend(t)" or "s += t" | extends *s* with the contents ' |
| 13167 | 'of | |\n' |
| 13168 | '| | *t* (for the most part the ' |
| 13169 | 'same | |\n' |
| 13170 | '| | as "s[len(s):len(s)] = ' |
| 13171 | 't") | |\n' |
| 13172 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13173 | '| "s *= n" | updates *s* with its ' |
| 13174 | 'contents | (6) |\n' |
| 13175 | '| | repeated *n* ' |
| 13176 | 'times | |\n' |
| 13177 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13178 | '| "s.insert(i, x)" | inserts *x* into *s* at ' |
| 13179 | 'the | |\n' |
| 13180 | '| | index given by *i* (same ' |
| 13181 | 'as | |\n' |
| 13182 | '| | "s[i:i] = ' |
| 13183 | '[x]") | |\n' |
| 13184 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13185 | '| "s.pop([i])" | retrieves the item at *i* ' |
| 13186 | 'and | (2) |\n' |
| 13187 | '| | also removes it from ' |
| 13188 | '*s* | |\n' |
| 13189 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13190 | '| "s.remove(x)" | remove the first item from ' |
| 13191 | '*s* | (3) |\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 13192 | '| | where "s[i]" is equal to ' |
| 13193 | '*x* | |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13194 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13195 | '| "s.reverse()" | reverses the items of *s* ' |
| 13196 | 'in | (4) |\n' |
| 13197 | '| | ' |
| 13198 | 'place | |\n' |
| 13199 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13200 | '\n' |
| 13201 | 'Notes:\n' |
| 13202 | '\n' |
| 13203 | '1. *t* must have the same length as the slice it is replacing.\n' |
| 13204 | '\n' |
| 13205 | '2. The optional argument *i* defaults to "-1", so that by ' |
| 13206 | 'default\n' |
| 13207 | ' the last item is removed and returned.\n' |
| 13208 | '\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 13209 | '3. "remove()" raises "ValueError" when *x* is not found in *s*.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13210 | '\n' |
| 13211 | '4. The "reverse()" method modifies the sequence in place for\n' |
| 13212 | ' economy of space when reversing a large sequence. To remind ' |
| 13213 | 'users\n' |
| 13214 | ' that it operates by side effect, it does not return the ' |
| 13215 | 'reversed\n' |
| 13216 | ' sequence.\n' |
| 13217 | '\n' |
| 13218 | '5. "clear()" and "copy()" are included for consistency with the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13219 | ' interfaces of mutable containers that don’t support slicing\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 13220 | ' operations (such as "dict" and "set"). "copy()" is not part ' |
| 13221 | 'of the\n' |
| 13222 | ' "collections.abc.MutableSequence" ABC, but most concrete ' |
| 13223 | 'mutable\n' |
| 13224 | ' sequence classes provide it.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13225 | '\n' |
| 13226 | ' New in version 3.3: "clear()" and "copy()" methods.\n' |
| 13227 | '\n' |
| 13228 | '6. The value *n* is an integer, or an object implementing\n' |
| 13229 | ' "__index__()". Zero and negative values of *n* clear the ' |
| 13230 | 'sequence.\n' |
| 13231 | ' Items in the sequence are not copied; they are referenced ' |
| 13232 | 'multiple\n' |
| 13233 | ' times, as explained for "s * n" under Common Sequence ' |
| 13234 | 'Operations.\n' |
| 13235 | '\n' |
| 13236 | '\n' |
| 13237 | 'Lists\n' |
| 13238 | '=====\n' |
| 13239 | '\n' |
| 13240 | 'Lists are mutable sequences, typically used to store collections ' |
| 13241 | 'of\n' |
| 13242 | 'homogeneous items (where the precise degree of similarity will ' |
| 13243 | 'vary by\n' |
| 13244 | 'application).\n' |
| 13245 | '\n' |
| 13246 | 'class list([iterable])\n' |
| 13247 | '\n' |
| 13248 | ' Lists may be constructed in several ways:\n' |
| 13249 | '\n' |
| 13250 | ' * Using a pair of square brackets to denote the empty list: ' |
| 13251 | '"[]"\n' |
| 13252 | '\n' |
| 13253 | ' * Using square brackets, separating items with commas: ' |
| 13254 | '"[a]",\n' |
| 13255 | ' "[a, b, c]"\n' |
| 13256 | '\n' |
| 13257 | ' * Using a list comprehension: "[x for x in iterable]"\n' |
| 13258 | '\n' |
| 13259 | ' * Using the type constructor: "list()" or "list(iterable)"\n' |
| 13260 | '\n' |
| 13261 | ' The constructor builds a list whose items are the same and in ' |
| 13262 | 'the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13263 | ' same order as *iterable*’s items. *iterable* may be either ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13264 | 'a\n' |
| 13265 | ' sequence, a container that supports iteration, or an ' |
| 13266 | 'iterator\n' |
| 13267 | ' object. If *iterable* is already a list, a copy is made and\n' |
| 13268 | ' returned, similar to "iterable[:]". For example, ' |
| 13269 | '"list(\'abc\')"\n' |
| 13270 | ' returns "[\'a\', \'b\', \'c\']" and "list( (1, 2, 3) )" ' |
| 13271 | 'returns "[1, 2,\n' |
| 13272 | ' 3]". If no argument is given, the constructor creates a new ' |
| 13273 | 'empty\n' |
| 13274 | ' list, "[]".\n' |
| 13275 | '\n' |
| 13276 | ' Many other operations also produce lists, including the ' |
| 13277 | '"sorted()"\n' |
| 13278 | ' built-in.\n' |
| 13279 | '\n' |
| 13280 | ' Lists implement all of the common and mutable sequence ' |
| 13281 | 'operations.\n' |
| 13282 | ' Lists also provide the following additional method:\n' |
| 13283 | '\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 13284 | ' sort(*, key=None, reverse=False)\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13285 | '\n' |
| 13286 | ' This method sorts the list in place, using only "<" ' |
| 13287 | 'comparisons\n' |
| 13288 | ' between items. Exceptions are not suppressed - if any ' |
| 13289 | 'comparison\n' |
| 13290 | ' operations fail, the entire sort operation will fail (and ' |
| 13291 | 'the\n' |
| 13292 | ' list will likely be left in a partially modified state).\n' |
| 13293 | '\n' |
| 13294 | ' "sort()" accepts two arguments that can only be passed by\n' |
| 13295 | ' keyword (keyword-only arguments):\n' |
| 13296 | '\n' |
| 13297 | ' *key* specifies a function of one argument that is used ' |
| 13298 | 'to\n' |
| 13299 | ' extract a comparison key from each list element (for ' |
| 13300 | 'example,\n' |
| 13301 | ' "key=str.lower"). The key corresponding to each item in ' |
| 13302 | 'the list\n' |
| 13303 | ' is calculated once and then used for the entire sorting ' |
| 13304 | 'process.\n' |
| 13305 | ' The default value of "None" means that list items are ' |
| 13306 | 'sorted\n' |
| 13307 | ' directly without calculating a separate key value.\n' |
| 13308 | '\n' |
| 13309 | ' The "functools.cmp_to_key()" utility is available to ' |
| 13310 | 'convert a\n' |
| 13311 | ' 2.x style *cmp* function to a *key* function.\n' |
| 13312 | '\n' |
| 13313 | ' *reverse* is a boolean value. If set to "True", then the ' |
| 13314 | 'list\n' |
| 13315 | ' elements are sorted as if each comparison were reversed.\n' |
| 13316 | '\n' |
| 13317 | ' This method modifies the sequence in place for economy of ' |
| 13318 | 'space\n' |
| 13319 | ' when sorting a large sequence. To remind users that it ' |
| 13320 | 'operates\n' |
| 13321 | ' by side effect, it does not return the sorted sequence ' |
| 13322 | '(use\n' |
| 13323 | ' "sorted()" to explicitly request a new sorted list ' |
| 13324 | 'instance).\n' |
| 13325 | '\n' |
| 13326 | ' The "sort()" method is guaranteed to be stable. A sort ' |
| 13327 | 'is\n' |
| 13328 | ' stable if it guarantees not to change the relative order ' |
| 13329 | 'of\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13330 | ' elements that compare equal — this is helpful for sorting ' |
| 13331 | 'in\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13332 | ' multiple passes (for example, sort by department, then by ' |
| 13333 | 'salary\n' |
| 13334 | ' grade).\n' |
| 13335 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13336 | ' For sorting examples and a brief sorting tutorial, see ' |
| 13337 | 'Sorting\n' |
| 13338 | ' HOW TO.\n' |
| 13339 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13340 | ' **CPython implementation detail:** While a list is being ' |
| 13341 | 'sorted,\n' |
| 13342 | ' the effect of attempting to mutate, or even inspect, the ' |
| 13343 | 'list is\n' |
| 13344 | ' undefined. The C implementation of Python makes the list ' |
| 13345 | 'appear\n' |
| 13346 | ' empty for the duration, and raises "ValueError" if it can ' |
| 13347 | 'detect\n' |
| 13348 | ' that the list has been mutated during a sort.\n' |
| 13349 | '\n' |
| 13350 | '\n' |
| 13351 | 'Tuples\n' |
| 13352 | '======\n' |
| 13353 | '\n' |
| 13354 | 'Tuples are immutable sequences, typically used to store ' |
| 13355 | 'collections of\n' |
| 13356 | 'heterogeneous data (such as the 2-tuples produced by the ' |
| 13357 | '"enumerate()"\n' |
| 13358 | 'built-in). Tuples are also used for cases where an immutable ' |
| 13359 | 'sequence\n' |
| 13360 | 'of homogeneous data is needed (such as allowing storage in a ' |
| 13361 | '"set" or\n' |
| 13362 | '"dict" instance).\n' |
| 13363 | '\n' |
| 13364 | 'class tuple([iterable])\n' |
| 13365 | '\n' |
| 13366 | ' Tuples may be constructed in a number of ways:\n' |
| 13367 | '\n' |
| 13368 | ' * Using a pair of parentheses to denote the empty tuple: ' |
| 13369 | '"()"\n' |
| 13370 | '\n' |
| 13371 | ' * Using a trailing comma for a singleton tuple: "a," or ' |
| 13372 | '"(a,)"\n' |
| 13373 | '\n' |
| 13374 | ' * Separating items with commas: "a, b, c" or "(a, b, c)"\n' |
| 13375 | '\n' |
| 13376 | ' * Using the "tuple()" built-in: "tuple()" or ' |
| 13377 | '"tuple(iterable)"\n' |
| 13378 | '\n' |
| 13379 | ' The constructor builds a tuple whose items are the same and ' |
| 13380 | 'in the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13381 | ' same order as *iterable*’s items. *iterable* may be either ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13382 | 'a\n' |
| 13383 | ' sequence, a container that supports iteration, or an ' |
| 13384 | 'iterator\n' |
| 13385 | ' object. If *iterable* is already a tuple, it is returned\n' |
| 13386 | ' unchanged. For example, "tuple(\'abc\')" returns "(\'a\', ' |
| 13387 | '\'b\', \'c\')"\n' |
| 13388 | ' and "tuple( [1, 2, 3] )" returns "(1, 2, 3)". If no argument ' |
| 13389 | 'is\n' |
| 13390 | ' given, the constructor creates a new empty tuple, "()".\n' |
| 13391 | '\n' |
| 13392 | ' Note that it is actually the comma which makes a tuple, not ' |
| 13393 | 'the\n' |
| 13394 | ' parentheses. The parentheses are optional, except in the ' |
| 13395 | 'empty\n' |
| 13396 | ' tuple case, or when they are needed to avoid syntactic ' |
| 13397 | 'ambiguity.\n' |
| 13398 | ' For example, "f(a, b, c)" is a function call with three ' |
| 13399 | 'arguments,\n' |
| 13400 | ' while "f((a, b, c))" is a function call with a 3-tuple as the ' |
| 13401 | 'sole\n' |
| 13402 | ' argument.\n' |
| 13403 | '\n' |
| 13404 | ' Tuples implement all of the common sequence operations.\n' |
| 13405 | '\n' |
| 13406 | 'For heterogeneous collections of data where access by name is ' |
| 13407 | 'clearer\n' |
| 13408 | 'than access by index, "collections.namedtuple()" may be a more\n' |
| 13409 | 'appropriate choice than a simple tuple object.\n' |
| 13410 | '\n' |
| 13411 | '\n' |
| 13412 | 'Ranges\n' |
| 13413 | '======\n' |
| 13414 | '\n' |
| 13415 | 'The "range" type represents an immutable sequence of numbers and ' |
| 13416 | 'is\n' |
| 13417 | 'commonly used for looping a specific number of times in "for" ' |
| 13418 | 'loops.\n' |
| 13419 | '\n' |
| 13420 | 'class range(stop)\n' |
| 13421 | 'class range(start, stop[, step])\n' |
| 13422 | '\n' |
| 13423 | ' The arguments to the range constructor must be integers ' |
| 13424 | '(either\n' |
| 13425 | ' built-in "int" or any object that implements the "__index__"\n' |
| 13426 | ' special method). If the *step* argument is omitted, it ' |
| 13427 | 'defaults to\n' |
| 13428 | ' "1". If the *start* argument is omitted, it defaults to "0". ' |
| 13429 | 'If\n' |
| 13430 | ' *step* is zero, "ValueError" is raised.\n' |
| 13431 | '\n' |
| 13432 | ' For a positive *step*, the contents of a range "r" are ' |
| 13433 | 'determined\n' |
| 13434 | ' by the formula "r[i] = start + step*i" where "i >= 0" and ' |
| 13435 | '"r[i] <\n' |
| 13436 | ' stop".\n' |
| 13437 | '\n' |
| 13438 | ' For a negative *step*, the contents of the range are still\n' |
| 13439 | ' determined by the formula "r[i] = start + step*i", but the\n' |
| 13440 | ' constraints are "i >= 0" and "r[i] > stop".\n' |
| 13441 | '\n' |
| 13442 | ' A range object will be empty if "r[0]" does not meet the ' |
| 13443 | 'value\n' |
| 13444 | ' constraint. Ranges do support negative indices, but these ' |
| 13445 | 'are\n' |
| 13446 | ' interpreted as indexing from the end of the sequence ' |
| 13447 | 'determined by\n' |
| 13448 | ' the positive indices.\n' |
| 13449 | '\n' |
| 13450 | ' Ranges containing absolute values larger than "sys.maxsize" ' |
| 13451 | 'are\n' |
| 13452 | ' permitted but some features (such as "len()") may raise\n' |
| 13453 | ' "OverflowError".\n' |
| 13454 | '\n' |
| 13455 | ' Range examples:\n' |
| 13456 | '\n' |
| 13457 | ' >>> list(range(10))\n' |
| 13458 | ' [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n' |
| 13459 | ' >>> list(range(1, 11))\n' |
| 13460 | ' [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n' |
| 13461 | ' >>> list(range(0, 30, 5))\n' |
| 13462 | ' [0, 5, 10, 15, 20, 25]\n' |
| 13463 | ' >>> list(range(0, 10, 3))\n' |
| 13464 | ' [0, 3, 6, 9]\n' |
| 13465 | ' >>> list(range(0, -10, -1))\n' |
| 13466 | ' [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]\n' |
| 13467 | ' >>> list(range(0))\n' |
| 13468 | ' []\n' |
| 13469 | ' >>> list(range(1, 0))\n' |
| 13470 | ' []\n' |
| 13471 | '\n' |
| 13472 | ' Ranges implement all of the common sequence operations ' |
| 13473 | 'except\n' |
| 13474 | ' concatenation and repetition (due to the fact that range ' |
| 13475 | 'objects\n' |
| 13476 | ' can only represent sequences that follow a strict pattern ' |
| 13477 | 'and\n' |
| 13478 | ' repetition and concatenation will usually violate that ' |
| 13479 | 'pattern).\n' |
| 13480 | '\n' |
| 13481 | ' start\n' |
| 13482 | '\n' |
| 13483 | ' The value of the *start* parameter (or "0" if the ' |
| 13484 | 'parameter was\n' |
| 13485 | ' not supplied)\n' |
| 13486 | '\n' |
| 13487 | ' stop\n' |
| 13488 | '\n' |
| 13489 | ' The value of the *stop* parameter\n' |
| 13490 | '\n' |
| 13491 | ' step\n' |
| 13492 | '\n' |
| 13493 | ' The value of the *step* parameter (or "1" if the parameter ' |
| 13494 | 'was\n' |
| 13495 | ' not supplied)\n' |
| 13496 | '\n' |
| 13497 | 'The advantage of the "range" type over a regular "list" or ' |
| 13498 | '"tuple" is\n' |
| 13499 | 'that a "range" object will always take the same (small) amount ' |
| 13500 | 'of\n' |
| 13501 | 'memory, no matter the size of the range it represents (as it ' |
| 13502 | 'only\n' |
| 13503 | 'stores the "start", "stop" and "step" values, calculating ' |
| 13504 | 'individual\n' |
| 13505 | 'items and subranges as needed).\n' |
| 13506 | '\n' |
| 13507 | 'Range objects implement the "collections.abc.Sequence" ABC, and\n' |
| 13508 | 'provide features such as containment tests, element index ' |
| 13509 | 'lookup,\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13510 | 'slicing and support for negative indices (see Sequence Types — ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13511 | 'list,\n' |
| 13512 | 'tuple, range):\n' |
| 13513 | '\n' |
| 13514 | '>>> r = range(0, 20, 2)\n' |
| 13515 | '>>> r\n' |
| 13516 | 'range(0, 20, 2)\n' |
| 13517 | '>>> 11 in r\n' |
| 13518 | 'False\n' |
| 13519 | '>>> 10 in r\n' |
| 13520 | 'True\n' |
| 13521 | '>>> r.index(10)\n' |
| 13522 | '5\n' |
| 13523 | '>>> r[5]\n' |
| 13524 | '10\n' |
| 13525 | '>>> r[:5]\n' |
| 13526 | 'range(0, 10, 2)\n' |
| 13527 | '>>> r[-1]\n' |
| 13528 | '18\n' |
| 13529 | '\n' |
| 13530 | 'Testing range objects for equality with "==" and "!=" compares ' |
| 13531 | 'them as\n' |
| 13532 | 'sequences. That is, two range objects are considered equal if ' |
| 13533 | 'they\n' |
| 13534 | 'represent the same sequence of values. (Note that two range ' |
| 13535 | 'objects\n' |
| 13536 | 'that compare equal might have different "start", "stop" and ' |
| 13537 | '"step"\n' |
| 13538 | 'attributes, for example "range(0) == range(2, 1, 3)" or ' |
| 13539 | '"range(0, 3,\n' |
| 13540 | '2) == range(0, 4, 2)".)\n' |
| 13541 | '\n' |
| 13542 | 'Changed in version 3.2: Implement the Sequence ABC. Support ' |
| 13543 | 'slicing\n' |
| 13544 | 'and negative indices. Test "int" objects for membership in ' |
| 13545 | 'constant\n' |
| 13546 | 'time instead of iterating through all items.\n' |
| 13547 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13548 | 'Changed in version 3.3: Define ‘==’ and ‘!=’ to compare range ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13549 | 'objects\n' |
| 13550 | 'based on the sequence of values they define (instead of ' |
| 13551 | 'comparing\n' |
| 13552 | 'based on object identity).\n' |
| 13553 | '\n' |
Ned Deily | c934dde | 2016-09-12 10:48:44 -0400 | [diff] [blame] | 13554 | 'New in version 3.3: The "start", "stop" and "step" attributes.\n' |
| 13555 | '\n' |
| 13556 | 'See also:\n' |
| 13557 | '\n' |
| 13558 | ' * The linspace recipe shows how to implement a lazy version ' |
| 13559 | 'of\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13560 | ' range suitable for floating point applications.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 13561 | 'typesseq-mutable': 'Mutable Sequence Types\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13562 | '**********************\n' |
| 13563 | '\n' |
| 13564 | 'The operations in the following table are defined on ' |
| 13565 | 'mutable sequence\n' |
| 13566 | 'types. The "collections.abc.MutableSequence" ABC is ' |
| 13567 | 'provided to make\n' |
| 13568 | 'it easier to correctly implement these operations on ' |
| 13569 | 'custom sequence\n' |
| 13570 | 'types.\n' |
| 13571 | '\n' |
| 13572 | 'In the table *s* is an instance of a mutable sequence ' |
| 13573 | 'type, *t* is any\n' |
| 13574 | 'iterable object and *x* is an arbitrary object that ' |
| 13575 | 'meets any type and\n' |
| 13576 | 'value restrictions imposed by *s* (for example, ' |
| 13577 | '"bytearray" only\n' |
| 13578 | 'accepts integers that meet the value restriction "0 <= x ' |
| 13579 | '<= 255").\n' |
| 13580 | '\n' |
| 13581 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13582 | '| Operation | ' |
| 13583 | 'Result | Notes ' |
| 13584 | '|\n' |
Łukasz Langa | fd75708 | 2019-11-19 12:17:21 +0100 | [diff] [blame] | 13585 | '|================================|==================================|=======================|\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13586 | '| "s[i] = x" | item *i* of *s* is ' |
| 13587 | 'replaced by | |\n' |
| 13588 | '| | ' |
| 13589 | '*x* | ' |
| 13590 | '|\n' |
| 13591 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13592 | '| "s[i:j] = t" | slice of *s* from *i* ' |
| 13593 | 'to *j* is | |\n' |
| 13594 | '| | replaced by the ' |
| 13595 | 'contents of the | |\n' |
| 13596 | '| | iterable ' |
| 13597 | '*t* | |\n' |
| 13598 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13599 | '| "del s[i:j]" | same as "s[i:j] = ' |
| 13600 | '[]" | |\n' |
| 13601 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13602 | '| "s[i:j:k] = t" | the elements of ' |
| 13603 | '"s[i:j:k]" are | (1) |\n' |
| 13604 | '| | replaced by those of ' |
| 13605 | '*t* | |\n' |
| 13606 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13607 | '| "del s[i:j:k]" | removes the elements ' |
| 13608 | 'of | |\n' |
| 13609 | '| | "s[i:j:k]" from the ' |
| 13610 | 'list | |\n' |
| 13611 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13612 | '| "s.append(x)" | appends *x* to the ' |
| 13613 | 'end of the | |\n' |
| 13614 | '| | sequence (same ' |
| 13615 | 'as | |\n' |
| 13616 | '| | "s[len(s):len(s)] = ' |
| 13617 | '[x]") | |\n' |
| 13618 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13619 | '| "s.clear()" | removes all items ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13620 | 'from *s* (same | (5) |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13621 | '| | as "del ' |
| 13622 | 's[:]") | |\n' |
| 13623 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13624 | '| "s.copy()" | creates a shallow ' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13625 | 'copy of *s* | (5) |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13626 | '| | (same as ' |
| 13627 | '"s[:]") | |\n' |
| 13628 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13629 | '| "s.extend(t)" or "s += t" | extends *s* with the ' |
| 13630 | 'contents of | |\n' |
| 13631 | '| | *t* (for the most ' |
| 13632 | 'part the same | |\n' |
| 13633 | '| | as "s[len(s):len(s)] ' |
| 13634 | '= t") | |\n' |
| 13635 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13636 | '| "s *= n" | updates *s* with its ' |
| 13637 | 'contents | (6) |\n' |
| 13638 | '| | repeated *n* ' |
| 13639 | 'times | |\n' |
| 13640 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13641 | '| "s.insert(i, x)" | inserts *x* into *s* ' |
| 13642 | 'at the | |\n' |
| 13643 | '| | index given by *i* ' |
| 13644 | '(same as | |\n' |
| 13645 | '| | "s[i:i] = ' |
| 13646 | '[x]") | |\n' |
| 13647 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13648 | '| "s.pop([i])" | retrieves the item at ' |
| 13649 | '*i* and | (2) |\n' |
| 13650 | '| | also removes it from ' |
| 13651 | '*s* | |\n' |
| 13652 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13653 | '| "s.remove(x)" | remove the first item ' |
| 13654 | 'from *s* | (3) |\n' |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 13655 | '| | where "s[i]" is equal ' |
| 13656 | 'to *x* | |\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13657 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13658 | '| "s.reverse()" | reverses the items of ' |
| 13659 | '*s* in | (4) |\n' |
| 13660 | '| | ' |
| 13661 | 'place | ' |
| 13662 | '|\n' |
| 13663 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13664 | '\n' |
| 13665 | 'Notes:\n' |
| 13666 | '\n' |
| 13667 | '1. *t* must have the same length as the slice it is ' |
| 13668 | 'replacing.\n' |
| 13669 | '\n' |
| 13670 | '2. The optional argument *i* defaults to "-1", so that ' |
| 13671 | 'by default\n' |
| 13672 | ' the last item is removed and returned.\n' |
| 13673 | '\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 13674 | '3. "remove()" raises "ValueError" when *x* is not found ' |
| 13675 | 'in *s*.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13676 | '\n' |
| 13677 | '4. The "reverse()" method modifies the sequence in place ' |
| 13678 | 'for\n' |
| 13679 | ' economy of space when reversing a large sequence. To ' |
| 13680 | 'remind users\n' |
| 13681 | ' that it operates by side effect, it does not return ' |
| 13682 | 'the reversed\n' |
| 13683 | ' sequence.\n' |
| 13684 | '\n' |
| 13685 | '5. "clear()" and "copy()" are included for consistency ' |
| 13686 | 'with the\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13687 | ' interfaces of mutable containers that don’t support ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13688 | 'slicing\n' |
Pablo Galindo | 29cb21d | 2019-05-29 22:59:00 +0100 | [diff] [blame] | 13689 | ' operations (such as "dict" and "set"). "copy()" is ' |
| 13690 | 'not part of the\n' |
| 13691 | ' "collections.abc.MutableSequence" ABC, but most ' |
| 13692 | 'concrete mutable\n' |
| 13693 | ' sequence classes provide it.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13694 | '\n' |
| 13695 | ' New in version 3.3: "clear()" and "copy()" methods.\n' |
| 13696 | '\n' |
| 13697 | '6. The value *n* is an integer, or an object ' |
| 13698 | 'implementing\n' |
| 13699 | ' "__index__()". Zero and negative values of *n* clear ' |
| 13700 | 'the sequence.\n' |
| 13701 | ' Items in the sequence are not copied; they are ' |
| 13702 | 'referenced multiple\n' |
| 13703 | ' times, as explained for "s * n" under Common Sequence ' |
| 13704 | 'Operations.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 13705 | 'unary': 'Unary arithmetic and bitwise operations\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13706 | '***************************************\n' |
| 13707 | '\n' |
| 13708 | 'All unary arithmetic and bitwise operations have the same ' |
| 13709 | 'priority:\n' |
| 13710 | '\n' |
| 13711 | ' u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr\n' |
| 13712 | '\n' |
| 13713 | 'The unary "-" (minus) operator yields the negation of its numeric\n' |
| 13714 | 'argument.\n' |
| 13715 | '\n' |
| 13716 | 'The unary "+" (plus) operator yields its numeric argument ' |
| 13717 | 'unchanged.\n' |
| 13718 | '\n' |
| 13719 | 'The unary "~" (invert) operator yields the bitwise inversion of ' |
| 13720 | 'its\n' |
| 13721 | 'integer argument. The bitwise inversion of "x" is defined as\n' |
| 13722 | '"-(x+1)". It only applies to integral numbers.\n' |
| 13723 | '\n' |
| 13724 | 'In all three cases, if the argument does not have the proper type, ' |
| 13725 | 'a\n' |
| 13726 | '"TypeError" exception is raised.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 13727 | 'while': 'The "while" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13728 | '*********************\n' |
| 13729 | '\n' |
| 13730 | 'The "while" statement is used for repeated execution as long as an\n' |
| 13731 | 'expression is true:\n' |
| 13732 | '\n' |
Łukasz Langa | dcd4c4f | 2020-03-23 17:19:13 +0100 | [diff] [blame] | 13733 | ' while_stmt ::= "while" assignment_expression ":" suite\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13734 | ' ["else" ":" suite]\n' |
| 13735 | '\n' |
| 13736 | 'This repeatedly tests the expression and, if it is true, executes ' |
| 13737 | 'the\n' |
| 13738 | 'first suite; if the expression is false (which may be the first ' |
| 13739 | 'time\n' |
| 13740 | 'it is tested) the suite of the "else" clause, if present, is ' |
| 13741 | 'executed\n' |
| 13742 | 'and the loop terminates.\n' |
| 13743 | '\n' |
| 13744 | 'A "break" statement executed in the first suite terminates the ' |
| 13745 | 'loop\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13746 | 'without executing the "else" clause’s suite. A "continue" ' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13747 | 'statement\n' |
| 13748 | 'executed in the first suite skips the rest of the suite and goes ' |
| 13749 | 'back\n' |
| 13750 | 'to testing the expression.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 13751 | 'with': 'The "with" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13752 | '********************\n' |
| 13753 | '\n' |
| 13754 | 'The "with" statement is used to wrap the execution of a block with\n' |
| 13755 | 'methods defined by a context manager (see section With Statement\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13756 | 'Context Managers). This allows common "try"…"except"…"finally" ' |
| 13757 | 'usage\n' |
| 13758 | 'patterns to be encapsulated for convenient reuse.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13759 | '\n' |
| 13760 | ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n' |
| 13761 | ' with_item ::= expression ["as" target]\n' |
| 13762 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13763 | 'The execution of the "with" statement with one “item” proceeds as\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13764 | 'follows:\n' |
| 13765 | '\n' |
| 13766 | '1. The context expression (the expression given in the "with_item")\n' |
| 13767 | ' is evaluated to obtain a context manager.\n' |
| 13768 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 13769 | '2. The context manager’s "__enter__()" is loaded for later use.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13770 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 13771 | '3. The context manager’s "__exit__()" is loaded for later use.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13772 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 13773 | '4. The context manager’s "__enter__()" method is invoked.\n' |
| 13774 | '\n' |
| 13775 | '5. If a target was included in the "with" statement, the return\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13776 | ' value from "__enter__()" is assigned to it.\n' |
| 13777 | '\n' |
| 13778 | ' Note: The "with" statement guarantees that if the "__enter__()"\n' |
| 13779 | ' method returns without an error, then "__exit__()" will always ' |
| 13780 | 'be\n' |
| 13781 | ' called. Thus, if an error occurs during the assignment to the\n' |
| 13782 | ' target list, it will be treated the same as an error occurring\n' |
| 13783 | ' within the suite would be. See step 6 below.\n' |
| 13784 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 13785 | '6. The suite is executed.\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13786 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 13787 | '7. The context manager’s "__exit__()" method is invoked. If an\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13788 | ' exception caused the suite to be exited, its type, value, and\n' |
| 13789 | ' traceback are passed as arguments to "__exit__()". Otherwise, ' |
| 13790 | 'three\n' |
| 13791 | ' "None" arguments are supplied.\n' |
| 13792 | '\n' |
| 13793 | ' If the suite was exited due to an exception, and the return ' |
| 13794 | 'value\n' |
| 13795 | ' from the "__exit__()" method was false, the exception is ' |
| 13796 | 'reraised.\n' |
| 13797 | ' If the return value was true, the exception is suppressed, and\n' |
| 13798 | ' execution continues with the statement following the "with"\n' |
| 13799 | ' statement.\n' |
| 13800 | '\n' |
| 13801 | ' If the suite was exited for any reason other than an exception, ' |
| 13802 | 'the\n' |
| 13803 | ' return value from "__exit__()" is ignored, and execution ' |
| 13804 | 'proceeds\n' |
| 13805 | ' at the normal location for the kind of exit that was taken.\n' |
| 13806 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 13807 | 'The following code:\n' |
| 13808 | '\n' |
| 13809 | ' with EXPRESSION as TARGET:\n' |
| 13810 | ' SUITE\n' |
| 13811 | '\n' |
| 13812 | 'is semantically equivalent to:\n' |
| 13813 | '\n' |
| 13814 | ' manager = (EXPRESSION)\n' |
| 13815 | ' enter = type(manager).__enter__\n' |
| 13816 | ' exit = type(manager).__exit__\n' |
| 13817 | ' value = enter(manager)\n' |
| 13818 | ' hit_except = False\n' |
| 13819 | '\n' |
| 13820 | ' try:\n' |
| 13821 | ' TARGET = value\n' |
| 13822 | ' SUITE\n' |
| 13823 | ' except:\n' |
| 13824 | ' hit_except = True\n' |
| 13825 | ' if not exit(manager, *sys.exc_info()):\n' |
| 13826 | ' raise\n' |
| 13827 | ' finally:\n' |
| 13828 | ' if not hit_except:\n' |
| 13829 | ' exit(manager, None, None, None)\n' |
| 13830 | '\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13831 | 'With more than one item, the context managers are processed as if\n' |
| 13832 | 'multiple "with" statements were nested:\n' |
| 13833 | '\n' |
| 13834 | ' with A() as a, B() as b:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 13835 | ' SUITE\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13836 | '\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 13837 | 'is semantically equivalent to:\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13838 | '\n' |
| 13839 | ' with A() as a:\n' |
| 13840 | ' with B() as b:\n' |
Łukasz Langa | c33378d | 2020-01-24 22:05:07 +0100 | [diff] [blame] | 13841 | ' SUITE\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13842 | '\n' |
| 13843 | 'Changed in version 3.1: Support for multiple context expressions.\n' |
| 13844 | '\n' |
| 13845 | 'See also:\n' |
| 13846 | '\n' |
Łukasz Langa | aab0e57 | 2019-02-03 14:04:12 +0100 | [diff] [blame] | 13847 | ' **PEP 343** - The “with” statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13848 | ' The specification, background, and examples for the Python ' |
| 13849 | '"with"\n' |
| 13850 | ' statement.\n', |
Ned Deily | 450ceea | 2017-09-19 01:01:36 -0400 | [diff] [blame] | 13851 | 'yield': 'The "yield" statement\n' |
Ned Deily | cae752a | 2016-05-16 13:44:52 -0400 | [diff] [blame] | 13852 | '*********************\n' |
| 13853 | '\n' |
| 13854 | ' yield_stmt ::= yield_expression\n' |
| 13855 | '\n' |
| 13856 | 'A "yield" statement is semantically equivalent to a yield ' |
| 13857 | 'expression.\n' |
| 13858 | 'The yield statement can be used to omit the parentheses that would\n' |
| 13859 | 'otherwise be required in the equivalent yield expression ' |
| 13860 | 'statement.\n' |
| 13861 | 'For example, the yield statements\n' |
| 13862 | '\n' |
| 13863 | ' yield <expr>\n' |
| 13864 | ' yield from <expr>\n' |
| 13865 | '\n' |
| 13866 | 'are equivalent to the yield expression statements\n' |
| 13867 | '\n' |
| 13868 | ' (yield <expr>)\n' |
| 13869 | ' (yield from <expr>)\n' |
| 13870 | '\n' |
| 13871 | 'Yield expressions and statements are only used when defining a\n' |
| 13872 | '*generator* function, and are only used in the body of the ' |
| 13873 | 'generator\n' |
| 13874 | 'function. Using yield in a function definition is sufficient to ' |
| 13875 | 'cause\n' |
| 13876 | 'that definition to create a generator function instead of a normal\n' |
| 13877 | 'function.\n' |
| 13878 | '\n' |
| 13879 | 'For full details of "yield" semantics, refer to the Yield ' |
| 13880 | 'expressions\n' |
| 13881 | 'section.\n'} |