Benjamin Peterson | d58cec2 | 2014-11-25 18:25:06 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Benjamin Peterson | 241282b | 2015-11-21 13:35:41 -0800 | [diff] [blame] | 2 | # Autogenerated by Sphinx on Sat Nov 21 13:35:13 2015 |
| 3 | topics = {'assert': '\n' |
| 4 | 'The "assert" statement\n' |
| 5 | '**********************\n' |
| 6 | '\n' |
| 7 | 'Assert statements are a convenient way to insert debugging ' |
| 8 | 'assertions\n' |
| 9 | 'into a program:\n' |
| 10 | '\n' |
| 11 | ' assert_stmt ::= "assert" expression ["," expression]\n' |
| 12 | '\n' |
| 13 | 'The simple form, "assert expression", is equivalent to\n' |
| 14 | '\n' |
| 15 | ' if __debug__:\n' |
| 16 | ' if not expression: raise AssertionError\n' |
| 17 | '\n' |
| 18 | 'The extended form, "assert expression1, expression2", is ' |
| 19 | 'equivalent to\n' |
| 20 | '\n' |
| 21 | ' if __debug__:\n' |
| 22 | ' if not expression1: raise AssertionError(expression2)\n' |
| 23 | '\n' |
| 24 | 'These equivalences assume that "__debug__" and "AssertionError" ' |
| 25 | 'refer\n' |
| 26 | 'to the built-in variables with those names. In the current\n' |
| 27 | 'implementation, the built-in variable "__debug__" is "True" ' |
| 28 | 'under\n' |
| 29 | 'normal circumstances, "False" when optimization is requested ' |
| 30 | '(command\n' |
| 31 | 'line option -O). The current code generator emits no code for ' |
| 32 | 'an\n' |
| 33 | 'assert statement when optimization is requested at compile ' |
| 34 | 'time. Note\n' |
| 35 | 'that it is unnecessary to include the source code for the ' |
| 36 | 'expression\n' |
| 37 | 'that failed in the error message; it will be displayed as part ' |
| 38 | 'of the\n' |
| 39 | 'stack trace.\n' |
| 40 | '\n' |
| 41 | 'Assignments to "__debug__" are illegal. The value for the ' |
| 42 | 'built-in\n' |
| 43 | 'variable is determined when the interpreter starts.\n', |
| 44 | 'assignment': '\n' |
| 45 | 'Assignment statements\n' |
| 46 | '*********************\n' |
| 47 | '\n' |
| 48 | 'Assignment statements are used to (re)bind names to values ' |
| 49 | 'and to\n' |
| 50 | 'modify attributes or items of mutable objects:\n' |
| 51 | '\n' |
| 52 | ' assignment_stmt ::= (target_list "=")+ (expression_list | ' |
| 53 | 'yield_expression)\n' |
| 54 | ' target_list ::= target ("," target)* [","]\n' |
| 55 | ' target ::= identifier\n' |
| 56 | ' | "(" target_list ")"\n' |
| 57 | ' | "[" target_list "]"\n' |
| 58 | ' | attributeref\n' |
| 59 | ' | subscription\n' |
| 60 | ' | slicing\n' |
| 61 | '\n' |
| 62 | '(See section Primaries for the syntax definitions for the ' |
| 63 | 'last three\n' |
| 64 | 'symbols.)\n' |
| 65 | '\n' |
| 66 | 'An assignment statement evaluates the expression list ' |
| 67 | '(remember that\n' |
| 68 | 'this can be a single expression or a comma-separated list, ' |
| 69 | 'the latter\n' |
| 70 | 'yielding a tuple) and assigns the single resulting object to ' |
| 71 | 'each of\n' |
| 72 | 'the target lists, from left to right.\n' |
| 73 | '\n' |
| 74 | 'Assignment is defined recursively depending on the form of ' |
| 75 | 'the target\n' |
| 76 | '(list). When a target is part of a mutable object (an ' |
| 77 | 'attribute\n' |
| 78 | 'reference, subscription or slicing), the mutable object ' |
| 79 | 'must\n' |
| 80 | 'ultimately perform the assignment and decide about its ' |
| 81 | 'validity, and\n' |
| 82 | 'may raise an exception if the assignment is unacceptable. ' |
| 83 | 'The rules\n' |
| 84 | 'observed by various types and the exceptions raised are ' |
| 85 | 'given with the\n' |
| 86 | 'definition of the object types (see section The standard ' |
| 87 | 'type\n' |
| 88 | 'hierarchy).\n' |
| 89 | '\n' |
| 90 | 'Assignment of an object to a target list is recursively ' |
| 91 | 'defined as\n' |
| 92 | 'follows.\n' |
| 93 | '\n' |
| 94 | '* If the target list is a single target: The object is ' |
| 95 | 'assigned to\n' |
| 96 | ' that target.\n' |
| 97 | '\n' |
| 98 | '* If the target list is a comma-separated list of targets: ' |
| 99 | 'The\n' |
| 100 | ' object must be an iterable with the same number of items ' |
| 101 | 'as there\n' |
| 102 | ' are targets in the target list, and the items are ' |
| 103 | 'assigned, from\n' |
| 104 | ' left to right, to the corresponding targets.\n' |
| 105 | '\n' |
| 106 | 'Assignment of an object to a single target is recursively ' |
| 107 | 'defined as\n' |
| 108 | 'follows.\n' |
| 109 | '\n' |
| 110 | '* If the target is an identifier (name):\n' |
| 111 | '\n' |
| 112 | ' * If the name does not occur in a "global" statement in ' |
| 113 | 'the\n' |
| 114 | ' current code block: the name is bound to the object in ' |
| 115 | 'the current\n' |
| 116 | ' local namespace.\n' |
| 117 | '\n' |
| 118 | ' * Otherwise: the name is bound to the object in the ' |
| 119 | 'current global\n' |
| 120 | ' namespace.\n' |
| 121 | '\n' |
| 122 | ' The name is rebound if it was already bound. This may ' |
| 123 | 'cause the\n' |
| 124 | ' reference count for the object previously bound to the ' |
| 125 | 'name to reach\n' |
| 126 | ' zero, causing the object to be deallocated and its ' |
| 127 | 'destructor (if it\n' |
| 128 | ' has one) to be called.\n' |
| 129 | '\n' |
| 130 | '* If the target is a target list enclosed in parentheses or ' |
| 131 | 'in\n' |
| 132 | ' square brackets: The object must be an iterable with the ' |
| 133 | 'same number\n' |
| 134 | ' of items as there are targets in the target list, and its ' |
| 135 | 'items are\n' |
| 136 | ' assigned, from left to right, to the corresponding ' |
| 137 | 'targets.\n' |
| 138 | '\n' |
| 139 | '* If the target is an attribute reference: The primary ' |
| 140 | 'expression in\n' |
| 141 | ' the reference is evaluated. It should yield an object ' |
| 142 | 'with\n' |
| 143 | ' assignable attributes; if this is not the case, ' |
| 144 | '"TypeError" is\n' |
| 145 | ' raised. That object is then asked to assign the assigned ' |
| 146 | 'object to\n' |
| 147 | ' the given attribute; if it cannot perform the assignment, ' |
| 148 | 'it raises\n' |
| 149 | ' an exception (usually but not necessarily ' |
| 150 | '"AttributeError").\n' |
| 151 | '\n' |
| 152 | ' Note: If the object is a class instance and the attribute ' |
| 153 | 'reference\n' |
| 154 | ' occurs on both sides of the assignment operator, the RHS ' |
| 155 | 'expression,\n' |
| 156 | ' "a.x" can access either an instance attribute or (if no ' |
| 157 | 'instance\n' |
| 158 | ' attribute exists) a class attribute. The LHS target "a.x" ' |
| 159 | 'is always\n' |
| 160 | ' set as an instance attribute, creating it if necessary. ' |
| 161 | 'Thus, the\n' |
| 162 | ' two occurrences of "a.x" do not necessarily refer to the ' |
| 163 | 'same\n' |
| 164 | ' attribute: if the RHS expression refers to a class ' |
| 165 | 'attribute, the\n' |
| 166 | ' LHS creates a new instance attribute as the target of the\n' |
| 167 | ' assignment:\n' |
| 168 | '\n' |
| 169 | ' class Cls:\n' |
| 170 | ' x = 3 # class variable\n' |
| 171 | ' inst = Cls()\n' |
| 172 | ' inst.x = inst.x + 1 # writes inst.x as 4 leaving ' |
| 173 | 'Cls.x as 3\n' |
| 174 | '\n' |
| 175 | ' This description does not necessarily apply to descriptor\n' |
| 176 | ' attributes, such as properties created with "property()".\n' |
| 177 | '\n' |
| 178 | '* If the target is a subscription: The primary expression in ' |
| 179 | 'the\n' |
| 180 | ' reference is evaluated. It should yield either a mutable ' |
| 181 | 'sequence\n' |
| 182 | ' object (such as a list) or a mapping object (such as a ' |
| 183 | 'dictionary).\n' |
| 184 | ' Next, the subscript expression is evaluated.\n' |
| 185 | '\n' |
| 186 | ' If the primary is a mutable sequence object (such as a ' |
| 187 | 'list), the\n' |
| 188 | ' subscript must yield a plain integer. If it is negative, ' |
| 189 | 'the\n' |
| 190 | " sequence's length is added to it. The resulting value must " |
| 191 | 'be a\n' |
| 192 | " nonnegative integer less than the sequence's length, and " |
| 193 | 'the\n' |
| 194 | ' sequence is asked to assign the assigned object to its ' |
| 195 | 'item with\n' |
| 196 | ' that index. If the index is out of range, "IndexError" is ' |
| 197 | 'raised\n' |
| 198 | ' (assignment to a subscripted sequence cannot add new items ' |
| 199 | 'to a\n' |
| 200 | ' list).\n' |
| 201 | '\n' |
| 202 | ' If the primary is a mapping object (such as a dictionary), ' |
| 203 | 'the\n' |
| 204 | " subscript must have a type compatible with the mapping's " |
| 205 | 'key type,\n' |
| 206 | ' and the mapping is then asked to create a key/datum pair ' |
| 207 | 'which maps\n' |
| 208 | ' the subscript to the assigned object. This can either ' |
| 209 | 'replace an\n' |
| 210 | ' existing key/value pair with the same key value, or insert ' |
| 211 | 'a new\n' |
| 212 | ' key/value pair (if no key with the same value existed).\n' |
| 213 | '\n' |
| 214 | '* If the target is a slicing: The primary expression in the\n' |
| 215 | ' reference is evaluated. It should yield a mutable ' |
| 216 | 'sequence object\n' |
| 217 | ' (such as a list). The assigned object should be a ' |
| 218 | 'sequence object\n' |
| 219 | ' of the same type. Next, the lower and upper bound ' |
| 220 | 'expressions are\n' |
| 221 | ' evaluated, insofar they are present; defaults are zero and ' |
| 222 | 'the\n' |
| 223 | " sequence's length. The bounds should evaluate to (small) " |
| 224 | 'integers.\n' |
| 225 | " If either bound is negative, the sequence's length is " |
| 226 | 'added to it.\n' |
| 227 | ' The resulting bounds are clipped to lie between zero and ' |
| 228 | 'the\n' |
| 229 | " sequence's length, inclusive. Finally, the sequence " |
| 230 | 'object is asked\n' |
| 231 | ' to replace the slice with the items of the assigned ' |
| 232 | 'sequence. The\n' |
| 233 | ' length of the slice may be different from the length of ' |
| 234 | 'the assigned\n' |
| 235 | ' sequence, thus changing the length of the target sequence, ' |
| 236 | 'if the\n' |
| 237 | ' object allows it.\n' |
| 238 | '\n' |
| 239 | '**CPython implementation detail:** In the current ' |
| 240 | 'implementation, the\n' |
| 241 | 'syntax for targets is taken to be the same as for ' |
| 242 | 'expressions, and\n' |
| 243 | 'invalid syntax is rejected during the code generation phase, ' |
| 244 | 'causing\n' |
| 245 | 'less detailed error messages.\n' |
| 246 | '\n' |
| 247 | 'WARNING: Although the definition of assignment implies that ' |
| 248 | 'overlaps\n' |
| 249 | 'between the left-hand side and the right-hand side are ' |
| 250 | "'safe' (for\n" |
| 251 | 'example "a, b = b, a" swaps two variables), overlaps ' |
| 252 | '*within* the\n' |
| 253 | 'collection of assigned-to variables are not safe! For ' |
| 254 | 'instance, the\n' |
| 255 | 'following program prints "[0, 2]":\n' |
| 256 | '\n' |
| 257 | ' x = [0, 1]\n' |
| 258 | ' i = 0\n' |
| 259 | ' i, x[i] = 1, 2\n' |
| 260 | ' print x\n' |
| 261 | '\n' |
| 262 | '\n' |
| 263 | 'Augmented assignment statements\n' |
| 264 | '===============================\n' |
| 265 | '\n' |
| 266 | 'Augmented assignment is the combination, in a single ' |
| 267 | 'statement, of a\n' |
| 268 | 'binary operation and an assignment statement:\n' |
| 269 | '\n' |
| 270 | ' augmented_assignment_stmt ::= augtarget augop ' |
| 271 | '(expression_list | yield_expression)\n' |
| 272 | ' augtarget ::= identifier | attributeref | ' |
| 273 | 'subscription | slicing\n' |
| 274 | ' augop ::= "+=" | "-=" | "*=" | "/=" | ' |
| 275 | '"//=" | "%=" | "**="\n' |
| 276 | ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n' |
| 277 | '\n' |
| 278 | '(See section Primaries for the syntax definitions for the ' |
| 279 | 'last three\n' |
| 280 | 'symbols.)\n' |
| 281 | '\n' |
| 282 | 'An augmented assignment evaluates the target (which, unlike ' |
| 283 | 'normal\n' |
| 284 | 'assignment statements, cannot be an unpacking) and the ' |
| 285 | 'expression\n' |
| 286 | 'list, performs the binary operation specific to the type of ' |
| 287 | 'assignment\n' |
| 288 | 'on the two operands, and assigns the result to the original ' |
| 289 | 'target.\n' |
| 290 | 'The target is only evaluated once.\n' |
| 291 | '\n' |
| 292 | 'An augmented assignment expression like "x += 1" can be ' |
| 293 | 'rewritten as\n' |
| 294 | '"x = x + 1" to achieve a similar, but not exactly equal ' |
| 295 | 'effect. In the\n' |
| 296 | 'augmented version, "x" is only evaluated once. Also, when ' |
| 297 | 'possible,\n' |
| 298 | 'the actual operation is performed *in-place*, meaning that ' |
| 299 | 'rather than\n' |
| 300 | 'creating a new object and assigning that to the target, the ' |
| 301 | 'old object\n' |
| 302 | 'is modified instead.\n' |
| 303 | '\n' |
| 304 | 'With the exception of assigning to tuples and multiple ' |
| 305 | 'targets in a\n' |
| 306 | 'single statement, the assignment done by augmented ' |
| 307 | 'assignment\n' |
| 308 | 'statements is handled the same way as normal assignments. ' |
| 309 | 'Similarly,\n' |
| 310 | 'with the exception of the possible *in-place* behavior, the ' |
| 311 | 'binary\n' |
| 312 | 'operation performed by augmented assignment is the same as ' |
| 313 | 'the normal\n' |
| 314 | 'binary operations.\n' |
| 315 | '\n' |
| 316 | 'For targets which are attribute references, the same caveat ' |
| 317 | 'about\n' |
| 318 | 'class and instance attributes applies as for regular ' |
| 319 | 'assignments.\n', |
| 320 | 'atom-identifiers': '\n' |
| 321 | 'Identifiers (Names)\n' |
| 322 | '*******************\n' |
| 323 | '\n' |
| 324 | 'An identifier occurring as an atom is a name. See ' |
| 325 | 'section Identifiers\n' |
| 326 | 'and keywords for lexical definition and section Naming ' |
| 327 | 'and binding for\n' |
| 328 | 'documentation of naming and binding.\n' |
| 329 | '\n' |
| 330 | 'When the name is bound to an object, evaluation of the ' |
| 331 | 'atom yields\n' |
| 332 | 'that object. When a name is not bound, an attempt to ' |
| 333 | 'evaluate it\n' |
| 334 | 'raises a "NameError" exception.\n' |
| 335 | '\n' |
| 336 | '**Private name mangling:** When an identifier that ' |
| 337 | 'textually occurs in\n' |
| 338 | 'a class definition begins with two or more underscore ' |
| 339 | 'characters and\n' |
| 340 | 'does not end in two or more underscores, it is ' |
| 341 | 'considered a *private\n' |
| 342 | 'name* of that class. Private names are transformed to ' |
| 343 | 'a longer form\n' |
| 344 | 'before code is generated for them. The transformation ' |
| 345 | 'inserts the\n' |
| 346 | 'class name, with leading underscores removed and a ' |
| 347 | 'single underscore\n' |
| 348 | 'inserted, in front of the name. For example, the ' |
| 349 | 'identifier "__spam"\n' |
| 350 | 'occurring in a class named "Ham" will be transformed ' |
| 351 | 'to "_Ham__spam".\n' |
| 352 | 'This transformation is independent of the syntactical ' |
| 353 | 'context in which\n' |
| 354 | 'the identifier is used. If the transformed name is ' |
| 355 | 'extremely long\n' |
| 356 | '(longer than 255 characters), implementation defined ' |
| 357 | 'truncation may\n' |
| 358 | 'happen. If the class name consists only of ' |
| 359 | 'underscores, no\n' |
| 360 | 'transformation is done.\n', |
| 361 | 'atom-literals': '\n' |
| 362 | 'Literals\n' |
| 363 | '********\n' |
| 364 | '\n' |
| 365 | 'Python supports string literals and various numeric ' |
| 366 | 'literals:\n' |
| 367 | '\n' |
| 368 | ' literal ::= stringliteral | integer | longinteger\n' |
| 369 | ' | floatnumber | imagnumber\n' |
| 370 | '\n' |
| 371 | 'Evaluation of a literal yields an object of the given ' |
| 372 | 'type (string,\n' |
| 373 | 'integer, long integer, floating point number, complex ' |
| 374 | 'number) with the\n' |
| 375 | 'given value. The value may be approximated in the case ' |
| 376 | 'of floating\n' |
| 377 | 'point and imaginary (complex) literals. See section ' |
| 378 | 'Literals for\n' |
| 379 | 'details.\n' |
| 380 | '\n' |
| 381 | 'All literals correspond to immutable data types, and ' |
| 382 | 'hence the\n' |
| 383 | "object's identity is less important than its value. " |
| 384 | 'Multiple\n' |
| 385 | 'evaluations of literals with the same value (either the ' |
| 386 | 'same\n' |
| 387 | 'occurrence in the program text or a different occurrence) ' |
| 388 | 'may obtain\n' |
| 389 | 'the same object or a different object with the same ' |
| 390 | 'value.\n', |
| 391 | 'attribute-access': '\n' |
| 392 | 'Customizing attribute access\n' |
| 393 | '****************************\n' |
| 394 | '\n' |
| 395 | 'The following methods can be defined to customize the ' |
| 396 | 'meaning of\n' |
| 397 | 'attribute access (use of, assignment to, or deletion ' |
| 398 | 'of "x.name") for\n' |
| 399 | 'class instances.\n' |
| 400 | '\n' |
| 401 | 'object.__getattr__(self, name)\n' |
| 402 | '\n' |
| 403 | ' Called when an attribute lookup has not found the ' |
| 404 | 'attribute in the\n' |
| 405 | ' usual places (i.e. it is not an instance attribute ' |
| 406 | 'nor is it found\n' |
| 407 | ' in the class tree for "self"). "name" is the ' |
| 408 | 'attribute name. This\n' |
| 409 | ' method should return the (computed) attribute value ' |
| 410 | 'or raise an\n' |
| 411 | ' "AttributeError" exception.\n' |
| 412 | '\n' |
| 413 | ' Note that if the attribute is found through the ' |
| 414 | 'normal mechanism,\n' |
| 415 | ' "__getattr__()" is not called. (This is an ' |
| 416 | 'intentional asymmetry\n' |
| 417 | ' between "__getattr__()" and "__setattr__()".) This ' |
| 418 | 'is done both for\n' |
| 419 | ' efficiency reasons and because otherwise ' |
| 420 | '"__getattr__()" would have\n' |
| 421 | ' no way to access other attributes of the instance. ' |
| 422 | 'Note that at\n' |
| 423 | ' least for instance variables, you can fake total ' |
| 424 | 'control by not\n' |
| 425 | ' inserting any values in the instance attribute ' |
| 426 | 'dictionary (but\n' |
| 427 | ' instead inserting them in another object). See ' |
| 428 | 'the\n' |
| 429 | ' "__getattribute__()" method below for a way to ' |
| 430 | 'actually get total\n' |
| 431 | ' control in new-style classes.\n' |
| 432 | '\n' |
| 433 | 'object.__setattr__(self, name, value)\n' |
| 434 | '\n' |
| 435 | ' Called when an attribute assignment is attempted. ' |
| 436 | 'This is called\n' |
| 437 | ' instead of the normal mechanism (i.e. store the ' |
| 438 | 'value in the\n' |
| 439 | ' instance dictionary). *name* is the attribute ' |
| 440 | 'name, *value* is the\n' |
| 441 | ' value to be assigned to it.\n' |
| 442 | '\n' |
| 443 | ' If "__setattr__()" wants to assign to an instance ' |
| 444 | 'attribute, it\n' |
| 445 | ' should not simply execute "self.name = value" --- ' |
| 446 | 'this would cause\n' |
| 447 | ' a recursive call to itself. Instead, it should ' |
| 448 | 'insert the value in\n' |
| 449 | ' the dictionary of instance attributes, e.g., ' |
| 450 | '"self.__dict__[name] =\n' |
| 451 | ' value". For new-style classes, rather than ' |
| 452 | 'accessing the instance\n' |
| 453 | ' dictionary, it should call the base class method ' |
| 454 | 'with the same\n' |
| 455 | ' name, for example, "object.__setattr__(self, name, ' |
| 456 | 'value)".\n' |
| 457 | '\n' |
| 458 | 'object.__delattr__(self, name)\n' |
| 459 | '\n' |
| 460 | ' Like "__setattr__()" but for attribute deletion ' |
| 461 | 'instead of\n' |
| 462 | ' assignment. This should only be implemented if ' |
| 463 | '"del obj.name" is\n' |
| 464 | ' meaningful for the object.\n' |
| 465 | '\n' |
| 466 | '\n' |
| 467 | 'More attribute access for new-style classes\n' |
| 468 | '===========================================\n' |
| 469 | '\n' |
| 470 | 'The following methods only apply to new-style ' |
| 471 | 'classes.\n' |
| 472 | '\n' |
| 473 | 'object.__getattribute__(self, name)\n' |
| 474 | '\n' |
| 475 | ' Called unconditionally to implement attribute ' |
| 476 | 'accesses for\n' |
| 477 | ' instances of the class. If the class also defines ' |
| 478 | '"__getattr__()",\n' |
| 479 | ' the latter will not be called unless ' |
| 480 | '"__getattribute__()" either\n' |
| 481 | ' calls it explicitly or raises an "AttributeError". ' |
| 482 | 'This method\n' |
| 483 | ' should return the (computed) attribute value or ' |
| 484 | 'raise an\n' |
| 485 | ' "AttributeError" exception. In order to avoid ' |
| 486 | 'infinite recursion in\n' |
| 487 | ' this method, its implementation should always call ' |
| 488 | 'the base class\n' |
| 489 | ' method with the same name to access any attributes ' |
| 490 | 'it needs, for\n' |
| 491 | ' example, "object.__getattribute__(self, name)".\n' |
| 492 | '\n' |
| 493 | ' Note: This method may still be bypassed when ' |
| 494 | 'looking up special\n' |
| 495 | ' methods as the result of implicit invocation via ' |
| 496 | 'language syntax\n' |
| 497 | ' or built-in functions. See Special method lookup ' |
| 498 | 'for new-style\n' |
| 499 | ' classes.\n' |
| 500 | '\n' |
| 501 | '\n' |
| 502 | 'Implementing Descriptors\n' |
| 503 | '========================\n' |
| 504 | '\n' |
| 505 | 'The following methods only apply when an instance of ' |
| 506 | 'the class\n' |
| 507 | 'containing the method (a so-called *descriptor* class) ' |
| 508 | 'appears in an\n' |
| 509 | '*owner* class (the descriptor must be in either the ' |
| 510 | "owner's class\n" |
| 511 | 'dictionary or in the class dictionary for one of its ' |
| 512 | 'parents). In the\n' |
| 513 | 'examples below, "the attribute" refers to the ' |
| 514 | 'attribute whose name is\n' |
| 515 | "the key of the property in the owner class' " |
| 516 | '"__dict__".\n' |
| 517 | '\n' |
| 518 | 'object.__get__(self, instance, owner)\n' |
| 519 | '\n' |
| 520 | ' Called to get the attribute of the owner class ' |
| 521 | '(class attribute\n' |
| 522 | ' access) or of an instance of that class (instance ' |
| 523 | 'attribute\n' |
| 524 | ' access). *owner* is always the owner class, while ' |
| 525 | '*instance* is the\n' |
| 526 | ' instance that the attribute was accessed through, ' |
| 527 | 'or "None" when\n' |
| 528 | ' the attribute is accessed through the *owner*. ' |
| 529 | 'This method should\n' |
| 530 | ' return the (computed) attribute value or raise an ' |
| 531 | '"AttributeError"\n' |
| 532 | ' exception.\n' |
| 533 | '\n' |
| 534 | 'object.__set__(self, instance, value)\n' |
| 535 | '\n' |
| 536 | ' Called to set the attribute on an instance ' |
| 537 | '*instance* of the owner\n' |
| 538 | ' class to a new value, *value*.\n' |
| 539 | '\n' |
| 540 | 'object.__delete__(self, instance)\n' |
| 541 | '\n' |
| 542 | ' Called to delete the attribute on an instance ' |
| 543 | '*instance* of the\n' |
| 544 | ' owner class.\n' |
| 545 | '\n' |
| 546 | '\n' |
| 547 | 'Invoking Descriptors\n' |
| 548 | '====================\n' |
| 549 | '\n' |
| 550 | 'In general, a descriptor is an object attribute with ' |
| 551 | '"binding\n' |
| 552 | 'behavior", one whose attribute access has been ' |
| 553 | 'overridden by methods\n' |
| 554 | 'in the descriptor protocol: "__get__()", "__set__()", ' |
| 555 | 'and\n' |
| 556 | '"__delete__()". If any of those methods are defined ' |
| 557 | 'for an object, it\n' |
| 558 | 'is said to be a descriptor.\n' |
| 559 | '\n' |
| 560 | 'The default behavior for attribute access is to get, ' |
| 561 | 'set, or delete\n' |
| 562 | "the attribute from an object's dictionary. For " |
| 563 | 'instance, "a.x" has a\n' |
| 564 | 'lookup chain starting with "a.__dict__[\'x\']", then\n' |
| 565 | '"type(a).__dict__[\'x\']", and continuing through the ' |
| 566 | 'base classes of\n' |
| 567 | '"type(a)" excluding metaclasses.\n' |
| 568 | '\n' |
| 569 | 'However, if the looked-up value is an object defining ' |
| 570 | 'one of the\n' |
| 571 | 'descriptor methods, then Python may override the ' |
| 572 | 'default behavior and\n' |
| 573 | 'invoke the descriptor method instead. Where this ' |
| 574 | 'occurs in the\n' |
| 575 | 'precedence chain depends on which descriptor methods ' |
| 576 | 'were defined and\n' |
| 577 | 'how they were called. Note that descriptors are only ' |
| 578 | 'invoked for new\n' |
| 579 | 'style objects or classes (ones that subclass ' |
| 580 | '"object()" or "type()").\n' |
| 581 | '\n' |
| 582 | 'The starting point for descriptor invocation is a ' |
| 583 | 'binding, "a.x". How\n' |
| 584 | 'the arguments are assembled depends on "a":\n' |
| 585 | '\n' |
| 586 | 'Direct Call\n' |
| 587 | ' The simplest and least common call is when user ' |
| 588 | 'code directly\n' |
| 589 | ' invokes a descriptor method: "x.__get__(a)".\n' |
| 590 | '\n' |
| 591 | 'Instance Binding\n' |
| 592 | ' If binding to a new-style object instance, "a.x" is ' |
| 593 | 'transformed\n' |
| 594 | ' into the call: "type(a).__dict__[\'x\'].__get__(a, ' |
| 595 | 'type(a))".\n' |
| 596 | '\n' |
| 597 | 'Class Binding\n' |
| 598 | ' If binding to a new-style class, "A.x" is ' |
| 599 | 'transformed into the\n' |
| 600 | ' call: "A.__dict__[\'x\'].__get__(None, A)".\n' |
| 601 | '\n' |
| 602 | 'Super Binding\n' |
| 603 | ' If "a" is an instance of "super", then the binding ' |
| 604 | '"super(B,\n' |
| 605 | ' obj).m()" searches "obj.__class__.__mro__" for the ' |
| 606 | 'base class "A"\n' |
| 607 | ' immediately preceding "B" and then invokes the ' |
| 608 | 'descriptor with the\n' |
| 609 | ' call: "A.__dict__[\'m\'].__get__(obj, ' |
| 610 | 'obj.__class__)".\n' |
| 611 | '\n' |
| 612 | 'For instance bindings, the precedence of descriptor ' |
| 613 | 'invocation depends\n' |
| 614 | 'on the which descriptor methods are defined. A ' |
| 615 | 'descriptor can define\n' |
| 616 | 'any combination of "__get__()", "__set__()" and ' |
| 617 | '"__delete__()". If it\n' |
| 618 | 'does not define "__get__()", then accessing the ' |
| 619 | 'attribute will return\n' |
| 620 | 'the descriptor object itself unless there is a value ' |
| 621 | "in the object's\n" |
| 622 | 'instance dictionary. If the descriptor defines ' |
| 623 | '"__set__()" and/or\n' |
| 624 | '"__delete__()", it is a data descriptor; if it defines ' |
| 625 | 'neither, it is\n' |
| 626 | 'a non-data descriptor. Normally, data descriptors ' |
| 627 | 'define both\n' |
| 628 | '"__get__()" and "__set__()", while non-data ' |
| 629 | 'descriptors have just the\n' |
| 630 | '"__get__()" method. Data descriptors with "__set__()" ' |
| 631 | 'and "__get__()"\n' |
| 632 | 'defined always override a redefinition in an instance ' |
| 633 | 'dictionary. In\n' |
| 634 | 'contrast, non-data descriptors can be overridden by ' |
| 635 | 'instances.\n' |
| 636 | '\n' |
| 637 | 'Python methods (including "staticmethod()" and ' |
| 638 | '"classmethod()") are\n' |
| 639 | 'implemented as non-data descriptors. Accordingly, ' |
| 640 | 'instances can\n' |
| 641 | 'redefine and override methods. This allows individual ' |
| 642 | 'instances to\n' |
| 643 | 'acquire behaviors that differ from other instances of ' |
| 644 | 'the same class.\n' |
| 645 | '\n' |
| 646 | 'The "property()" function is implemented as a data ' |
| 647 | 'descriptor.\n' |
| 648 | 'Accordingly, instances cannot override the behavior of ' |
| 649 | 'a property.\n' |
| 650 | '\n' |
| 651 | '\n' |
| 652 | '__slots__\n' |
| 653 | '=========\n' |
| 654 | '\n' |
| 655 | 'By default, instances of both old and new-style ' |
| 656 | 'classes have a\n' |
| 657 | 'dictionary for attribute storage. This wastes space ' |
| 658 | 'for objects\n' |
| 659 | 'having very few instance variables. The space ' |
| 660 | 'consumption can become\n' |
| 661 | 'acute when creating large numbers of instances.\n' |
| 662 | '\n' |
| 663 | 'The default can be overridden by defining *__slots__* ' |
| 664 | 'in a new-style\n' |
| 665 | 'class definition. The *__slots__* declaration takes a ' |
| 666 | 'sequence of\n' |
| 667 | 'instance variables and reserves just enough space in ' |
| 668 | 'each instance to\n' |
| 669 | 'hold a value for each variable. Space is saved ' |
| 670 | 'because *__dict__* is\n' |
| 671 | 'not created for each instance.\n' |
| 672 | '\n' |
| 673 | '__slots__\n' |
| 674 | '\n' |
| 675 | ' This class variable can be assigned a string, ' |
| 676 | 'iterable, or sequence\n' |
| 677 | ' of strings with variable names used by instances. ' |
| 678 | 'If defined in a\n' |
| 679 | ' new-style class, *__slots__* reserves space for the ' |
| 680 | 'declared\n' |
| 681 | ' variables and prevents the automatic creation of ' |
| 682 | '*__dict__* and\n' |
| 683 | ' *__weakref__* for each instance.\n' |
| 684 | '\n' |
| 685 | ' New in version 2.2.\n' |
| 686 | '\n' |
| 687 | 'Notes on using *__slots__*\n' |
| 688 | '\n' |
| 689 | '* When inheriting from a class without *__slots__*, ' |
| 690 | 'the *__dict__*\n' |
| 691 | ' attribute of that class will always be accessible, ' |
| 692 | 'so a *__slots__*\n' |
| 693 | ' definition in the subclass is meaningless.\n' |
| 694 | '\n' |
| 695 | '* Without a *__dict__* variable, instances cannot be ' |
| 696 | 'assigned new\n' |
| 697 | ' variables not listed in the *__slots__* definition. ' |
| 698 | 'Attempts to\n' |
| 699 | ' assign to an unlisted variable name raises ' |
| 700 | '"AttributeError". If\n' |
| 701 | ' dynamic assignment of new variables is desired, then ' |
| 702 | 'add\n' |
| 703 | ' "\'__dict__\'" to the sequence of strings in the ' |
| 704 | '*__slots__*\n' |
| 705 | ' declaration.\n' |
| 706 | '\n' |
| 707 | ' Changed in version 2.3: Previously, adding ' |
| 708 | '"\'__dict__\'" to the\n' |
| 709 | ' *__slots__* declaration would not enable the ' |
| 710 | 'assignment of new\n' |
| 711 | ' attributes not specifically listed in the sequence ' |
| 712 | 'of instance\n' |
| 713 | ' variable names.\n' |
| 714 | '\n' |
| 715 | '* Without a *__weakref__* variable for each instance, ' |
| 716 | 'classes\n' |
| 717 | ' defining *__slots__* do not support weak references ' |
| 718 | 'to its\n' |
| 719 | ' instances. If weak reference support is needed, then ' |
| 720 | 'add\n' |
| 721 | ' "\'__weakref__\'" to the sequence of strings in the ' |
| 722 | '*__slots__*\n' |
| 723 | ' declaration.\n' |
| 724 | '\n' |
| 725 | ' Changed in version 2.3: Previously, adding ' |
| 726 | '"\'__weakref__\'" to the\n' |
| 727 | ' *__slots__* declaration would not enable support for ' |
| 728 | 'weak\n' |
| 729 | ' references.\n' |
| 730 | '\n' |
| 731 | '* *__slots__* are implemented at the class level by ' |
| 732 | 'creating\n' |
| 733 | ' descriptors (Implementing Descriptors) for each ' |
| 734 | 'variable name. As a\n' |
| 735 | ' result, class attributes cannot be used to set ' |
| 736 | 'default values for\n' |
| 737 | ' instance variables defined by *__slots__*; ' |
| 738 | 'otherwise, the class\n' |
| 739 | ' attribute would overwrite the descriptor ' |
| 740 | 'assignment.\n' |
| 741 | '\n' |
| 742 | '* The action of a *__slots__* declaration is limited ' |
| 743 | 'to the class\n' |
| 744 | ' where it is defined. As a result, subclasses will ' |
| 745 | 'have a *__dict__*\n' |
| 746 | ' unless they also define *__slots__* (which must only ' |
| 747 | 'contain names\n' |
| 748 | ' of any *additional* slots).\n' |
| 749 | '\n' |
| 750 | '* If a class defines a slot also defined in a base ' |
| 751 | 'class, the\n' |
| 752 | ' instance variable defined by the base class slot is ' |
| 753 | 'inaccessible\n' |
| 754 | ' (except by retrieving its descriptor directly from ' |
| 755 | 'the base class).\n' |
| 756 | ' This renders the meaning of the program undefined. ' |
| 757 | 'In the future, a\n' |
| 758 | ' check may be added to prevent this.\n' |
| 759 | '\n' |
| 760 | '* Nonempty *__slots__* does not work for classes ' |
| 761 | 'derived from\n' |
| 762 | ' "variable-length" built-in types such as "long", ' |
| 763 | '"str" and "tuple".\n' |
| 764 | '\n' |
| 765 | '* Any non-string iterable may be assigned to ' |
| 766 | '*__slots__*. Mappings\n' |
| 767 | ' may also be used; however, in the future, special ' |
| 768 | 'meaning may be\n' |
| 769 | ' assigned to the values corresponding to each key.\n' |
| 770 | '\n' |
| 771 | '* *__class__* assignment works only if both classes ' |
| 772 | 'have the same\n' |
| 773 | ' *__slots__*.\n' |
| 774 | '\n' |
| 775 | ' Changed in version 2.6: Previously, *__class__* ' |
| 776 | 'assignment raised an\n' |
| 777 | ' error if either new or old class had *__slots__*.\n', |
| 778 | 'attribute-references': '\n' |
| 779 | 'Attribute references\n' |
| 780 | '********************\n' |
| 781 | '\n' |
| 782 | 'An attribute reference is a primary followed by a ' |
| 783 | 'period and a name:\n' |
| 784 | '\n' |
| 785 | ' attributeref ::= primary "." identifier\n' |
| 786 | '\n' |
| 787 | 'The primary must evaluate to an object of a type ' |
| 788 | 'that supports\n' |
| 789 | 'attribute references, e.g., a module, list, or an ' |
| 790 | 'instance. This\n' |
| 791 | 'object is then asked to produce the attribute ' |
| 792 | 'whose name is the\n' |
| 793 | 'identifier. If this attribute is not available, ' |
| 794 | 'the exception\n' |
| 795 | '"AttributeError" is raised. Otherwise, the type ' |
| 796 | 'and value of the\n' |
| 797 | 'object produced is determined by the object. ' |
| 798 | 'Multiple evaluations of\n' |
| 799 | 'the same attribute reference may yield different ' |
| 800 | 'objects.\n', |
| 801 | 'augassign': '\n' |
| 802 | 'Augmented assignment statements\n' |
| 803 | '*******************************\n' |
| 804 | '\n' |
| 805 | 'Augmented assignment is the combination, in a single ' |
| 806 | 'statement, of a\n' |
| 807 | 'binary operation and an assignment statement:\n' |
| 808 | '\n' |
| 809 | ' augmented_assignment_stmt ::= augtarget augop ' |
| 810 | '(expression_list | yield_expression)\n' |
| 811 | ' augtarget ::= identifier | attributeref | ' |
| 812 | 'subscription | slicing\n' |
| 813 | ' augop ::= "+=" | "-=" | "*=" | "/=" | ' |
| 814 | '"//=" | "%=" | "**="\n' |
| 815 | ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n' |
| 816 | '\n' |
| 817 | '(See section Primaries for the syntax definitions for the ' |
| 818 | 'last three\n' |
| 819 | 'symbols.)\n' |
| 820 | '\n' |
| 821 | 'An augmented assignment evaluates the target (which, unlike ' |
| 822 | 'normal\n' |
| 823 | 'assignment statements, cannot be an unpacking) and the ' |
| 824 | 'expression\n' |
| 825 | 'list, performs the binary operation specific to the type of ' |
| 826 | 'assignment\n' |
| 827 | 'on the two operands, and assigns the result to the original ' |
| 828 | 'target.\n' |
| 829 | 'The target is only evaluated once.\n' |
| 830 | '\n' |
| 831 | 'An augmented assignment expression like "x += 1" can be ' |
| 832 | 'rewritten as\n' |
| 833 | '"x = x + 1" to achieve a similar, but not exactly equal ' |
| 834 | 'effect. In the\n' |
| 835 | 'augmented version, "x" is only evaluated once. Also, when ' |
| 836 | 'possible,\n' |
| 837 | 'the actual operation is performed *in-place*, meaning that ' |
| 838 | 'rather than\n' |
| 839 | 'creating a new object and assigning that to the target, the ' |
| 840 | 'old object\n' |
| 841 | 'is modified instead.\n' |
| 842 | '\n' |
| 843 | 'With the exception of assigning to tuples and multiple ' |
| 844 | 'targets in a\n' |
| 845 | 'single statement, the assignment done by augmented ' |
| 846 | 'assignment\n' |
| 847 | 'statements is handled the same way as normal assignments. ' |
| 848 | 'Similarly,\n' |
| 849 | 'with the exception of the possible *in-place* behavior, the ' |
| 850 | 'binary\n' |
| 851 | 'operation performed by augmented assignment is the same as ' |
| 852 | 'the normal\n' |
| 853 | 'binary operations.\n' |
| 854 | '\n' |
| 855 | 'For targets which are attribute references, the same caveat ' |
| 856 | 'about\n' |
| 857 | 'class and instance attributes applies as for regular ' |
| 858 | 'assignments.\n', |
| 859 | 'binary': '\n' |
| 860 | 'Binary arithmetic operations\n' |
| 861 | '****************************\n' |
| 862 | '\n' |
| 863 | 'The binary arithmetic operations have the conventional priority\n' |
| 864 | 'levels. Note that some of these operations also apply to ' |
| 865 | 'certain non-\n' |
| 866 | 'numeric types. Apart from the power operator, there are only ' |
| 867 | 'two\n' |
| 868 | 'levels, one for multiplicative operators and one for additive\n' |
| 869 | 'operators:\n' |
| 870 | '\n' |
| 871 | ' m_expr ::= u_expr | m_expr "*" u_expr | m_expr "//" u_expr | ' |
| 872 | 'm_expr "/" u_expr\n' |
| 873 | ' | m_expr "%" u_expr\n' |
| 874 | ' a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr\n' |
| 875 | '\n' |
| 876 | 'The "*" (multiplication) operator yields the product of its ' |
| 877 | 'arguments.\n' |
| 878 | 'The arguments must either both be numbers, or one argument must ' |
| 879 | 'be an\n' |
| 880 | 'integer (plain or long) and the other must be a sequence. In ' |
| 881 | 'the\n' |
| 882 | 'former case, the numbers are converted to a common type and ' |
| 883 | 'then\n' |
| 884 | 'multiplied together. In the latter case, sequence repetition ' |
| 885 | 'is\n' |
| 886 | 'performed; a negative repetition factor yields an empty ' |
| 887 | 'sequence.\n' |
| 888 | '\n' |
| 889 | 'The "/" (division) and "//" (floor division) operators yield ' |
| 890 | 'the\n' |
| 891 | 'quotient of their arguments. The numeric arguments are first\n' |
| 892 | 'converted to a common type. Plain or long integer division ' |
| 893 | 'yields an\n' |
| 894 | 'integer of the same type; the result is that of mathematical ' |
| 895 | 'division\n' |
| 896 | "with the 'floor' function applied to the result. Division by " |
| 897 | 'zero\n' |
| 898 | 'raises the "ZeroDivisionError" exception.\n' |
| 899 | '\n' |
| 900 | 'The "%" (modulo) operator yields the remainder from the division ' |
| 901 | 'of\n' |
| 902 | 'the first argument by the second. The numeric arguments are ' |
| 903 | 'first\n' |
| 904 | 'converted to a common type. A zero right argument raises the\n' |
| 905 | '"ZeroDivisionError" exception. The arguments may be floating ' |
| 906 | 'point\n' |
| 907 | 'numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals ' |
| 908 | '"4*0.7 +\n' |
| 909 | '0.34".) The modulo operator always yields a result with the ' |
| 910 | 'same sign\n' |
| 911 | 'as its second operand (or zero); the absolute value of the ' |
| 912 | 'result is\n' |
| 913 | 'strictly smaller than the absolute value of the second operand ' |
| 914 | '[2].\n' |
| 915 | '\n' |
| 916 | 'The integer division and modulo operators are connected by the\n' |
| 917 | 'following identity: "x == (x/y)*y + (x%y)". Integer division ' |
| 918 | 'and\n' |
| 919 | 'modulo are also connected with the built-in function ' |
| 920 | '"divmod()":\n' |
| 921 | '"divmod(x, y) == (x/y, x%y)". These identities don\'t hold for\n' |
| 922 | 'floating point numbers; there similar identities hold ' |
| 923 | 'approximately\n' |
| 924 | 'where "x/y" is replaced by "floor(x/y)" or "floor(x/y) - 1" ' |
| 925 | '[3].\n' |
| 926 | '\n' |
| 927 | 'In addition to performing the modulo operation on numbers, the ' |
| 928 | '"%"\n' |
| 929 | 'operator is also overloaded by string and unicode objects to ' |
| 930 | 'perform\n' |
| 931 | 'string formatting (also known as interpolation). The syntax for ' |
| 932 | 'string\n' |
| 933 | 'formatting is described in the Python Library Reference, ' |
| 934 | 'section\n' |
| 935 | 'String Formatting Operations.\n' |
| 936 | '\n' |
| 937 | 'Deprecated since version 2.3: The floor division operator, the ' |
| 938 | 'modulo\n' |
| 939 | 'operator, and the "divmod()" function are no longer defined for\n' |
| 940 | 'complex numbers. Instead, convert to a floating point number ' |
| 941 | 'using\n' |
| 942 | 'the "abs()" function if appropriate.\n' |
| 943 | '\n' |
| 944 | 'The "+" (addition) operator yields the sum of its arguments. ' |
| 945 | 'The\n' |
| 946 | 'arguments must either both be numbers or both sequences of the ' |
| 947 | 'same\n' |
| 948 | 'type. In the former case, the numbers are converted to a common ' |
| 949 | 'type\n' |
| 950 | 'and then added together. In the latter case, the sequences are\n' |
| 951 | 'concatenated.\n' |
| 952 | '\n' |
| 953 | 'The "-" (subtraction) operator yields the difference of its ' |
| 954 | 'arguments.\n' |
| 955 | 'The numeric arguments are first converted to a common type.\n', |
| 956 | 'bitwise': '\n' |
| 957 | 'Binary bitwise operations\n' |
| 958 | '*************************\n' |
| 959 | '\n' |
| 960 | 'Each of the three bitwise operations has a different priority ' |
| 961 | 'level:\n' |
| 962 | '\n' |
| 963 | ' and_expr ::= shift_expr | and_expr "&" shift_expr\n' |
| 964 | ' xor_expr ::= and_expr | xor_expr "^" and_expr\n' |
| 965 | ' or_expr ::= xor_expr | or_expr "|" xor_expr\n' |
| 966 | '\n' |
| 967 | 'The "&" operator yields the bitwise AND of its arguments, which ' |
| 968 | 'must\n' |
| 969 | 'be plain or long integers. The arguments are converted to a ' |
| 970 | 'common\n' |
| 971 | 'type.\n' |
| 972 | '\n' |
| 973 | 'The "^" operator yields the bitwise XOR (exclusive OR) of its\n' |
| 974 | 'arguments, which must be plain or long integers. The arguments ' |
| 975 | 'are\n' |
| 976 | 'converted to a common type.\n' |
| 977 | '\n' |
| 978 | 'The "|" operator yields the bitwise (inclusive) OR of its ' |
| 979 | 'arguments,\n' |
| 980 | 'which must be plain or long integers. The arguments are ' |
| 981 | 'converted to\n' |
| 982 | 'a common type.\n', |
| 983 | 'bltin-code-objects': '\n' |
| 984 | 'Code Objects\n' |
| 985 | '************\n' |
| 986 | '\n' |
| 987 | 'Code objects are used by the implementation to ' |
| 988 | 'represent "pseudo-\n' |
| 989 | 'compiled" executable Python code such as a function ' |
| 990 | 'body. They differ\n' |
| 991 | "from function objects because they don't contain a " |
| 992 | 'reference to their\n' |
| 993 | 'global execution environment. Code objects are ' |
| 994 | 'returned by the built-\n' |
| 995 | 'in "compile()" function and can be extracted from ' |
| 996 | 'function objects\n' |
| 997 | 'through their "func_code" attribute. See also the ' |
| 998 | '"code" module.\n' |
| 999 | '\n' |
| 1000 | 'A code object can be executed or evaluated by ' |
| 1001 | 'passing it (instead of a\n' |
| 1002 | 'source string) to the "exec" statement or the ' |
| 1003 | 'built-in "eval()"\n' |
| 1004 | 'function.\n' |
| 1005 | '\n' |
| 1006 | 'See The standard type hierarchy for more ' |
| 1007 | 'information.\n', |
| 1008 | 'bltin-ellipsis-object': '\n' |
| 1009 | 'The Ellipsis Object\n' |
| 1010 | '*******************\n' |
| 1011 | '\n' |
| 1012 | 'This object is used by extended slice notation ' |
| 1013 | '(see Slicings). It\n' |
| 1014 | 'supports no special operations. There is exactly ' |
| 1015 | 'one ellipsis object,\n' |
| 1016 | 'named "Ellipsis" (a built-in name).\n' |
| 1017 | '\n' |
| 1018 | 'It is written as "Ellipsis". When in a ' |
| 1019 | 'subscript, it can also be\n' |
| 1020 | 'written as "...", for example "seq[...]".\n', |
| 1021 | 'bltin-null-object': '\n' |
| 1022 | 'The Null Object\n' |
| 1023 | '***************\n' |
| 1024 | '\n' |
| 1025 | "This object is returned by functions that don't " |
| 1026 | 'explicitly return a\n' |
| 1027 | 'value. It supports no special operations. There is ' |
| 1028 | 'exactly one null\n' |
| 1029 | 'object, named "None" (a built-in name).\n' |
| 1030 | '\n' |
| 1031 | 'It is written as "None".\n', |
| 1032 | 'bltin-type-objects': '\n' |
| 1033 | 'Type Objects\n' |
| 1034 | '************\n' |
| 1035 | '\n' |
| 1036 | 'Type objects represent the various object types. An ' |
| 1037 | "object's type is\n" |
| 1038 | 'accessed by the built-in function "type()". There ' |
| 1039 | 'are no special\n' |
| 1040 | 'operations on types. The standard module "types" ' |
| 1041 | 'defines names for\n' |
| 1042 | 'all standard built-in types.\n' |
| 1043 | '\n' |
| 1044 | 'Types are written like this: "<type \'int\'>".\n', |
| 1045 | 'booleans': '\n' |
| 1046 | 'Boolean operations\n' |
| 1047 | '******************\n' |
| 1048 | '\n' |
| 1049 | ' or_test ::= and_test | or_test "or" and_test\n' |
| 1050 | ' and_test ::= not_test | and_test "and" not_test\n' |
| 1051 | ' not_test ::= comparison | "not" not_test\n' |
| 1052 | '\n' |
| 1053 | 'In the context of Boolean operations, and also when ' |
| 1054 | 'expressions are\n' |
| 1055 | 'used by control flow statements, the following values are ' |
| 1056 | 'interpreted\n' |
| 1057 | 'as false: "False", "None", numeric zero of all types, and ' |
| 1058 | 'empty\n' |
| 1059 | 'strings and containers (including strings, tuples, lists,\n' |
| 1060 | 'dictionaries, sets and frozensets). All other values are ' |
| 1061 | 'interpreted\n' |
| 1062 | 'as true. (See the "__nonzero__()" special method for a way to ' |
| 1063 | 'change\n' |
| 1064 | 'this.)\n' |
| 1065 | '\n' |
| 1066 | 'The operator "not" yields "True" if its argument is false, ' |
| 1067 | '"False"\n' |
| 1068 | 'otherwise.\n' |
| 1069 | '\n' |
| 1070 | 'The expression "x and y" first evaluates *x*; if *x* is false, ' |
| 1071 | 'its\n' |
| 1072 | 'value is returned; otherwise, *y* is evaluated and the ' |
| 1073 | 'resulting value\n' |
| 1074 | 'is returned.\n' |
| 1075 | '\n' |
| 1076 | 'The expression "x or y" first evaluates *x*; if *x* is true, ' |
| 1077 | 'its value\n' |
| 1078 | 'is returned; otherwise, *y* is evaluated and the resulting ' |
| 1079 | 'value is\n' |
| 1080 | 'returned.\n' |
| 1081 | '\n' |
| 1082 | '(Note that neither "and" nor "or" restrict the value and type ' |
| 1083 | 'they\n' |
| 1084 | 'return to "False" and "True", but rather return the last ' |
| 1085 | 'evaluated\n' |
| 1086 | 'argument. This is sometimes useful, e.g., if "s" is a string ' |
| 1087 | 'that\n' |
| 1088 | 'should be replaced by a default value if it is empty, the ' |
| 1089 | 'expression\n' |
| 1090 | '"s or \'foo\'" yields the desired value. Because "not" has to ' |
| 1091 | 'invent a\n' |
| 1092 | 'value anyway, it does not bother to return a value of the same ' |
| 1093 | 'type as\n' |
| 1094 | 'its argument, so e.g., "not \'foo\'" yields "False", not ' |
| 1095 | '"\'\'".)\n', |
| 1096 | 'break': '\n' |
| 1097 | 'The "break" statement\n' |
| 1098 | '*********************\n' |
| 1099 | '\n' |
| 1100 | ' break_stmt ::= "break"\n' |
| 1101 | '\n' |
| 1102 | '"break" may only occur syntactically nested in a "for" or ' |
| 1103 | '"while"\n' |
| 1104 | 'loop, but not nested in a function or class definition within ' |
| 1105 | 'that\n' |
| 1106 | 'loop.\n' |
| 1107 | '\n' |
| 1108 | 'It terminates the nearest enclosing loop, skipping the optional ' |
| 1109 | '"else"\n' |
| 1110 | 'clause if the loop has one.\n' |
| 1111 | '\n' |
| 1112 | 'If a "for" loop is terminated by "break", the loop control ' |
| 1113 | 'target\n' |
| 1114 | 'keeps its current value.\n' |
| 1115 | '\n' |
| 1116 | 'When "break" passes control out of a "try" statement with a ' |
| 1117 | '"finally"\n' |
| 1118 | 'clause, that "finally" clause is executed before really leaving ' |
| 1119 | 'the\n' |
| 1120 | 'loop.\n', |
| 1121 | 'callable-types': '\n' |
| 1122 | 'Emulating callable objects\n' |
| 1123 | '**************************\n' |
| 1124 | '\n' |
| 1125 | 'object.__call__(self[, args...])\n' |
| 1126 | '\n' |
| 1127 | ' Called when the instance is "called" as a function; ' |
| 1128 | 'if this method\n' |
| 1129 | ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n' |
| 1130 | ' "x.__call__(arg1, arg2, ...)".\n', |
| 1131 | 'calls': '\n' |
| 1132 | 'Calls\n' |
| 1133 | '*****\n' |
| 1134 | '\n' |
| 1135 | 'A call calls a callable object (e.g., a *function*) with a ' |
| 1136 | 'possibly\n' |
| 1137 | 'empty series of *arguments*:\n' |
| 1138 | '\n' |
| 1139 | ' call ::= primary "(" [argument_list [","]\n' |
| 1140 | ' | expression genexpr_for] ")"\n' |
| 1141 | ' argument_list ::= positional_arguments ["," ' |
| 1142 | 'keyword_arguments]\n' |
| 1143 | ' ["," "*" expression] ["," ' |
| 1144 | 'keyword_arguments]\n' |
| 1145 | ' ["," "**" expression]\n' |
| 1146 | ' | keyword_arguments ["," "*" expression]\n' |
| 1147 | ' ["," "**" expression]\n' |
| 1148 | ' | "*" expression ["," keyword_arguments] ' |
| 1149 | '["," "**" expression]\n' |
| 1150 | ' | "**" expression\n' |
| 1151 | ' positional_arguments ::= expression ("," expression)*\n' |
| 1152 | ' keyword_arguments ::= keyword_item ("," keyword_item)*\n' |
| 1153 | ' keyword_item ::= identifier "=" expression\n' |
| 1154 | '\n' |
| 1155 | 'A trailing comma may be present after the positional and keyword\n' |
| 1156 | 'arguments but does not affect the semantics.\n' |
| 1157 | '\n' |
| 1158 | 'The primary must evaluate to a callable object (user-defined\n' |
| 1159 | 'functions, built-in functions, methods of built-in objects, ' |
| 1160 | 'class\n' |
| 1161 | 'objects, methods of class instances, and certain class instances\n' |
| 1162 | 'themselves are callable; extensions may define additional ' |
| 1163 | 'callable\n' |
| 1164 | 'object types). All argument expressions are evaluated before the ' |
| 1165 | 'call\n' |
| 1166 | 'is attempted. Please refer to section Function definitions for ' |
| 1167 | 'the\n' |
| 1168 | 'syntax of formal *parameter* lists.\n' |
| 1169 | '\n' |
| 1170 | 'If keyword arguments are present, they are first converted to\n' |
| 1171 | 'positional arguments, as follows. First, a list of unfilled ' |
| 1172 | 'slots is\n' |
| 1173 | 'created for the formal parameters. If there are N positional\n' |
| 1174 | 'arguments, they are placed in the first N slots. Next, for each\n' |
| 1175 | 'keyword argument, the identifier is used to determine the\n' |
| 1176 | 'corresponding slot (if the identifier is the same as the first ' |
| 1177 | 'formal\n' |
| 1178 | 'parameter name, the first slot is used, and so on). If the slot ' |
| 1179 | 'is\n' |
| 1180 | 'already filled, a "TypeError" exception is raised. Otherwise, ' |
| 1181 | 'the\n' |
| 1182 | 'value of the argument is placed in the slot, filling it (even if ' |
| 1183 | 'the\n' |
| 1184 | 'expression is "None", it fills the slot). When all arguments ' |
| 1185 | 'have\n' |
| 1186 | 'been processed, the slots that are still unfilled are filled with ' |
| 1187 | 'the\n' |
| 1188 | 'corresponding default value from the function definition. ' |
| 1189 | '(Default\n' |
| 1190 | 'values are calculated, once, when the function is defined; thus, ' |
| 1191 | 'a\n' |
| 1192 | 'mutable object such as a list or dictionary used as default value ' |
| 1193 | 'will\n' |
| 1194 | "be shared by all calls that don't specify an argument value for " |
| 1195 | 'the\n' |
| 1196 | 'corresponding slot; this should usually be avoided.) If there ' |
| 1197 | 'are any\n' |
| 1198 | 'unfilled slots for which no default value is specified, a ' |
| 1199 | '"TypeError"\n' |
| 1200 | 'exception is raised. Otherwise, the list of filled slots is used ' |
| 1201 | 'as\n' |
| 1202 | 'the argument list for the call.\n' |
| 1203 | '\n' |
| 1204 | '**CPython implementation detail:** An implementation may provide\n' |
| 1205 | 'built-in functions whose positional parameters do not have names, ' |
| 1206 | 'even\n' |
| 1207 | "if they are 'named' for the purpose of documentation, and which\n" |
| 1208 | 'therefore cannot be supplied by keyword. In CPython, this is the ' |
| 1209 | 'case\n' |
| 1210 | 'for functions implemented in C that use "PyArg_ParseTuple()" to ' |
| 1211 | 'parse\n' |
| 1212 | 'their arguments.\n' |
| 1213 | '\n' |
| 1214 | 'If there are more positional arguments than there are formal ' |
| 1215 | 'parameter\n' |
| 1216 | 'slots, a "TypeError" exception is raised, unless a formal ' |
| 1217 | 'parameter\n' |
| 1218 | 'using the syntax "*identifier" is present; in this case, that ' |
| 1219 | 'formal\n' |
| 1220 | 'parameter receives a tuple containing the excess positional ' |
| 1221 | 'arguments\n' |
| 1222 | '(or an empty tuple if there were no excess positional ' |
| 1223 | 'arguments).\n' |
| 1224 | '\n' |
| 1225 | 'If any keyword argument does not correspond to a formal ' |
| 1226 | 'parameter\n' |
| 1227 | 'name, a "TypeError" exception is raised, unless a formal ' |
| 1228 | 'parameter\n' |
| 1229 | 'using the syntax "**identifier" is present; in this case, that ' |
| 1230 | 'formal\n' |
| 1231 | 'parameter receives a dictionary containing the excess keyword\n' |
| 1232 | 'arguments (using the keywords as keys and the argument values as\n' |
| 1233 | 'corresponding values), or a (new) empty dictionary if there were ' |
| 1234 | 'no\n' |
| 1235 | 'excess keyword arguments.\n' |
| 1236 | '\n' |
| 1237 | 'If the syntax "*expression" appears in the function call, ' |
| 1238 | '"expression"\n' |
| 1239 | 'must evaluate to an iterable. Elements from this iterable are ' |
| 1240 | 'treated\n' |
| 1241 | 'as if they were additional positional arguments; if there are\n' |
| 1242 | 'positional arguments *x1*, ..., *xN*, and "expression" evaluates ' |
| 1243 | 'to a\n' |
| 1244 | 'sequence *y1*, ..., *yM*, this is equivalent to a call with M+N\n' |
| 1245 | 'positional arguments *x1*, ..., *xN*, *y1*, ..., *yM*.\n' |
| 1246 | '\n' |
| 1247 | 'A consequence of this is that although the "*expression" syntax ' |
| 1248 | 'may\n' |
| 1249 | 'appear *after* some keyword arguments, it is processed *before* ' |
| 1250 | 'the\n' |
| 1251 | 'keyword arguments (and the "**expression" argument, if any -- ' |
| 1252 | 'see\n' |
| 1253 | 'below). So:\n' |
| 1254 | '\n' |
| 1255 | ' >>> def f(a, b):\n' |
| 1256 | ' ... print a, b\n' |
| 1257 | ' ...\n' |
| 1258 | ' >>> f(b=1, *(2,))\n' |
| 1259 | ' 2 1\n' |
| 1260 | ' >>> f(a=1, *(2,))\n' |
| 1261 | ' Traceback (most recent call last):\n' |
| 1262 | ' File "<stdin>", line 1, in ?\n' |
| 1263 | " TypeError: f() got multiple values for keyword argument 'a'\n" |
| 1264 | ' >>> f(1, *(2,))\n' |
| 1265 | ' 1 2\n' |
| 1266 | '\n' |
| 1267 | 'It is unusual for both keyword arguments and the "*expression" ' |
| 1268 | 'syntax\n' |
| 1269 | 'to be used in the same call, so in practice this confusion does ' |
| 1270 | 'not\n' |
| 1271 | 'arise.\n' |
| 1272 | '\n' |
| 1273 | 'If the syntax "**expression" appears in the function call,\n' |
| 1274 | '"expression" must evaluate to a mapping, the contents of which ' |
| 1275 | 'are\n' |
| 1276 | 'treated as additional keyword arguments. In the case of a ' |
| 1277 | 'keyword\n' |
| 1278 | 'appearing in both "expression" and as an explicit keyword ' |
| 1279 | 'argument, a\n' |
| 1280 | '"TypeError" exception is raised.\n' |
| 1281 | '\n' |
| 1282 | 'Formal parameters using the syntax "*identifier" or ' |
| 1283 | '"**identifier"\n' |
| 1284 | 'cannot be used as positional argument slots or as keyword ' |
| 1285 | 'argument\n' |
| 1286 | 'names. Formal parameters using the syntax "(sublist)" cannot be ' |
| 1287 | 'used\n' |
| 1288 | 'as keyword argument names; the outermost sublist corresponds to ' |
| 1289 | 'a\n' |
| 1290 | 'single unnamed argument slot, and the argument value is assigned ' |
| 1291 | 'to\n' |
| 1292 | 'the sublist using the usual tuple assignment rules after all ' |
| 1293 | 'other\n' |
| 1294 | 'parameter processing is done.\n' |
| 1295 | '\n' |
| 1296 | 'A call always returns some value, possibly "None", unless it ' |
| 1297 | 'raises an\n' |
| 1298 | 'exception. How this value is computed depends on the type of ' |
| 1299 | 'the\n' |
| 1300 | 'callable object.\n' |
| 1301 | '\n' |
| 1302 | 'If it is---\n' |
| 1303 | '\n' |
| 1304 | 'a user-defined function:\n' |
| 1305 | ' The code block for the function is executed, passing it the\n' |
| 1306 | ' argument list. The first thing the code block will do is bind ' |
| 1307 | 'the\n' |
| 1308 | ' formal parameters to the arguments; this is described in ' |
| 1309 | 'section\n' |
| 1310 | ' Function definitions. When the code block executes a ' |
| 1311 | '"return"\n' |
| 1312 | ' statement, this specifies the return value of the function ' |
| 1313 | 'call.\n' |
| 1314 | '\n' |
| 1315 | 'a built-in function or method:\n' |
| 1316 | ' The result is up to the interpreter; see Built-in Functions ' |
| 1317 | 'for the\n' |
| 1318 | ' descriptions of built-in functions and methods.\n' |
| 1319 | '\n' |
| 1320 | 'a class object:\n' |
| 1321 | ' A new instance of that class is returned.\n' |
| 1322 | '\n' |
| 1323 | 'a class instance method:\n' |
| 1324 | ' The corresponding user-defined function is called, with an ' |
| 1325 | 'argument\n' |
| 1326 | ' list that is one longer than the argument list of the call: ' |
| 1327 | 'the\n' |
| 1328 | ' instance becomes the first argument.\n' |
| 1329 | '\n' |
| 1330 | 'a class instance:\n' |
| 1331 | ' The class must define a "__call__()" method; the effect is ' |
| 1332 | 'then the\n' |
| 1333 | ' same as if that method was called.\n', |
| 1334 | 'class': '\n' |
| 1335 | 'Class definitions\n' |
| 1336 | '*****************\n' |
| 1337 | '\n' |
| 1338 | 'A class definition defines a class object (see section The ' |
| 1339 | 'standard\n' |
| 1340 | 'type hierarchy):\n' |
| 1341 | '\n' |
| 1342 | ' classdef ::= "class" classname [inheritance] ":" suite\n' |
| 1343 | ' inheritance ::= "(" [expression_list] ")"\n' |
| 1344 | ' classname ::= identifier\n' |
| 1345 | '\n' |
| 1346 | 'A class definition is an executable statement. It first ' |
| 1347 | 'evaluates the\n' |
| 1348 | 'inheritance list, if present. Each item in the inheritance list\n' |
| 1349 | 'should evaluate to a class object or class type which allows\n' |
| 1350 | "subclassing. The class's suite is then executed in a new " |
| 1351 | 'execution\n' |
| 1352 | 'frame (see section Naming and binding), using a newly created ' |
| 1353 | 'local\n' |
| 1354 | 'namespace and the original global namespace. (Usually, the suite\n' |
| 1355 | "contains only function definitions.) When the class's suite " |
| 1356 | 'finishes\n' |
| 1357 | 'execution, its execution frame is discarded but its local ' |
| 1358 | 'namespace is\n' |
| 1359 | 'saved. [4] A class object is then created using the inheritance ' |
| 1360 | 'list\n' |
| 1361 | 'for the base classes and the saved local namespace for the ' |
| 1362 | 'attribute\n' |
| 1363 | 'dictionary. The class name is bound to this class object in the\n' |
| 1364 | 'original local namespace.\n' |
| 1365 | '\n' |
| 1366 | "**Programmer's note:** Variables defined in the class definition " |
| 1367 | 'are\n' |
| 1368 | 'class variables; they are shared by all instances. To create ' |
| 1369 | 'instance\n' |
| 1370 | 'variables, they can be set in a method with "self.name = value". ' |
| 1371 | 'Both\n' |
| 1372 | 'class and instance variables are accessible through the notation\n' |
| 1373 | '""self.name"", and an instance variable hides a class variable ' |
| 1374 | 'with\n' |
| 1375 | 'the same name when accessed in this way. Class variables can be ' |
| 1376 | 'used\n' |
| 1377 | 'as defaults for instance variables, but using mutable values ' |
| 1378 | 'there can\n' |
| 1379 | 'lead to unexpected results. For *new-style class*es, descriptors ' |
| 1380 | 'can\n' |
| 1381 | 'be used to create instance variables with different ' |
| 1382 | 'implementation\n' |
| 1383 | 'details.\n' |
| 1384 | '\n' |
| 1385 | 'Class definitions, like function definitions, may be wrapped by ' |
| 1386 | 'one or\n' |
| 1387 | 'more *decorator* expressions. The evaluation rules for the ' |
| 1388 | 'decorator\n' |
| 1389 | 'expressions are the same as for functions. The result must be a ' |
| 1390 | 'class\n' |
| 1391 | 'object, which is then bound to the class name.\n' |
| 1392 | '\n' |
| 1393 | '-[ Footnotes ]-\n' |
| 1394 | '\n' |
| 1395 | '[1] The exception is propagated to the invocation stack unless\n' |
| 1396 | ' there is a "finally" clause which happens to raise another\n' |
| 1397 | ' exception. That new exception causes the old one to be lost.\n' |
| 1398 | '\n' |
| 1399 | '[2] Currently, control "flows off the end" except in the case of\n' |
| 1400 | ' an exception or the execution of a "return", "continue", or\n' |
| 1401 | ' "break" statement.\n' |
| 1402 | '\n' |
| 1403 | '[3] A string literal appearing as the first statement in the\n' |
| 1404 | ' function body is transformed into the function\'s "__doc__"\n' |
| 1405 | " attribute and therefore the function's *docstring*.\n" |
| 1406 | '\n' |
| 1407 | '[4] A string literal appearing as the first statement in the ' |
| 1408 | 'class\n' |
| 1409 | ' body is transformed into the namespace\'s "__doc__" item and\n' |
| 1410 | " therefore the class's *docstring*.\n", |
| 1411 | 'comparisons': '\n' |
| 1412 | 'Comparisons\n' |
| 1413 | '***********\n' |
| 1414 | '\n' |
| 1415 | 'Unlike C, all comparison operations in Python have the same ' |
| 1416 | 'priority,\n' |
| 1417 | 'which is lower than that of any arithmetic, shifting or ' |
| 1418 | 'bitwise\n' |
| 1419 | 'operation. Also unlike C, expressions like "a < b < c" ' |
| 1420 | 'have the\n' |
| 1421 | 'interpretation that is conventional in mathematics:\n' |
| 1422 | '\n' |
| 1423 | ' comparison ::= or_expr ( comp_operator or_expr )*\n' |
| 1424 | ' comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" ' |
| 1425 | '| "!="\n' |
| 1426 | ' | "is" ["not"] | ["not"] "in"\n' |
| 1427 | '\n' |
| 1428 | 'Comparisons yield boolean values: "True" or "False".\n' |
| 1429 | '\n' |
| 1430 | 'Comparisons can be chained arbitrarily, e.g., "x < y <= z" ' |
| 1431 | 'is\n' |
| 1432 | 'equivalent to "x < y and y <= z", except that "y" is ' |
| 1433 | 'evaluated only\n' |
| 1434 | 'once (but in both cases "z" is not evaluated at all when "x ' |
| 1435 | '< y" is\n' |
| 1436 | 'found to be false).\n' |
| 1437 | '\n' |
| 1438 | 'Formally, if *a*, *b*, *c*, ..., *y*, *z* are expressions ' |
| 1439 | 'and *op1*,\n' |
| 1440 | '*op2*, ..., *opN* are comparison operators, then "a op1 b ' |
| 1441 | 'op2 c ... y\n' |
| 1442 | 'opN z" is equivalent to "a op1 b and b op2 c and ... y opN ' |
| 1443 | 'z", except\n' |
| 1444 | 'that each expression is evaluated at most once.\n' |
| 1445 | '\n' |
| 1446 | 'Note that "a op1 b op2 c" doesn\'t imply any kind of ' |
| 1447 | 'comparison between\n' |
| 1448 | '*a* and *c*, so that, e.g., "x < y > z" is perfectly legal ' |
| 1449 | '(though\n' |
| 1450 | 'perhaps not pretty).\n' |
| 1451 | '\n' |
| 1452 | 'The forms "<>" and "!=" are equivalent; for consistency ' |
| 1453 | 'with C, "!="\n' |
| 1454 | 'is preferred; where "!=" is mentioned below "<>" is also ' |
| 1455 | 'accepted.\n' |
| 1456 | 'The "<>" spelling is considered obsolescent.\n' |
| 1457 | '\n' |
| 1458 | 'The operators "<", ">", "==", ">=", "<=", and "!=" compare ' |
| 1459 | 'the values\n' |
| 1460 | 'of two objects. The objects need not have the same type. ' |
| 1461 | 'If both are\n' |
| 1462 | 'numbers, they are converted to a common type. Otherwise, ' |
| 1463 | 'objects of\n' |
| 1464 | 'different types *always* compare unequal, and are ordered ' |
| 1465 | 'consistently\n' |
| 1466 | 'but arbitrarily. You can control comparison behavior of ' |
| 1467 | 'objects of\n' |
| 1468 | 'non-built-in types by defining a "__cmp__" method or rich ' |
| 1469 | 'comparison\n' |
| 1470 | 'methods like "__gt__", described in section Special method ' |
| 1471 | 'names.\n' |
| 1472 | '\n' |
| 1473 | '(This unusual definition of comparison was used to simplify ' |
| 1474 | 'the\n' |
| 1475 | 'definition of operations like sorting and the "in" and "not ' |
| 1476 | 'in"\n' |
| 1477 | 'operators. In the future, the comparison rules for objects ' |
| 1478 | 'of\n' |
| 1479 | 'different types are likely to change.)\n' |
| 1480 | '\n' |
| 1481 | 'Comparison of objects of the same type depends on the ' |
| 1482 | 'type:\n' |
| 1483 | '\n' |
| 1484 | '* Numbers are compared arithmetically.\n' |
| 1485 | '\n' |
| 1486 | '* Strings are compared lexicographically using the numeric\n' |
| 1487 | ' equivalents (the result of the built-in function "ord()") ' |
| 1488 | 'of their\n' |
| 1489 | ' characters. Unicode and 8-bit strings are fully ' |
| 1490 | 'interoperable in\n' |
| 1491 | ' this behavior. [4]\n' |
| 1492 | '\n' |
| 1493 | '* Tuples and lists are compared lexicographically using ' |
| 1494 | 'comparison\n' |
| 1495 | ' of corresponding elements. This means that to compare ' |
| 1496 | 'equal, each\n' |
| 1497 | ' element must compare equal and the two sequences must be ' |
| 1498 | 'of the same\n' |
| 1499 | ' type and have the same length.\n' |
| 1500 | '\n' |
| 1501 | ' If not equal, the sequences are ordered the same as their ' |
| 1502 | 'first\n' |
| 1503 | ' differing elements. For example, "cmp([1,2,x], [1,2,y])" ' |
| 1504 | 'returns\n' |
| 1505 | ' the same as "cmp(x,y)". If the corresponding element ' |
| 1506 | 'does not\n' |
| 1507 | ' exist, the shorter sequence is ordered first (for ' |
| 1508 | 'example, "[1,2] <\n' |
| 1509 | ' [1,2,3]").\n' |
| 1510 | '\n' |
| 1511 | '* Mappings (dictionaries) compare equal if and only if ' |
| 1512 | 'their sorted\n' |
| 1513 | ' (key, value) lists compare equal. [5] Outcomes other than ' |
| 1514 | 'equality\n' |
| 1515 | ' are resolved consistently, but are not otherwise defined. ' |
| 1516 | '[6]\n' |
| 1517 | '\n' |
| 1518 | '* Most other objects of built-in types compare unequal ' |
| 1519 | 'unless they\n' |
| 1520 | ' are the same object; the choice whether one object is ' |
| 1521 | 'considered\n' |
| 1522 | ' smaller or larger than another one is made arbitrarily ' |
| 1523 | 'but\n' |
| 1524 | ' consistently within one execution of a program.\n' |
| 1525 | '\n' |
| 1526 | 'The operators "in" and "not in" test for collection ' |
| 1527 | 'membership. "x in\n' |
| 1528 | 's" evaluates to true if *x* is a member of the collection ' |
| 1529 | '*s*, and\n' |
| 1530 | 'false otherwise. "x not in s" returns the negation of "x ' |
| 1531 | 'in s". The\n' |
| 1532 | 'collection membership test has traditionally been bound to ' |
| 1533 | 'sequences;\n' |
| 1534 | 'an object is a member of a collection if the collection is ' |
| 1535 | 'a sequence\n' |
| 1536 | 'and contains an element equal to that object. However, it ' |
| 1537 | 'make sense\n' |
| 1538 | 'for many other object types to support membership tests ' |
| 1539 | 'without being\n' |
| 1540 | 'a sequence. In particular, dictionaries (for keys) and ' |
| 1541 | 'sets support\n' |
| 1542 | 'membership testing.\n' |
| 1543 | '\n' |
| 1544 | 'For the list and tuple types, "x in y" is true if and only ' |
| 1545 | 'if there\n' |
| 1546 | 'exists an index *i* such that "x == y[i]" is true.\n' |
| 1547 | '\n' |
| 1548 | 'For the Unicode and string types, "x in y" is true if and ' |
| 1549 | 'only if *x*\n' |
| 1550 | 'is a substring of *y*. An equivalent test is "y.find(x) != ' |
| 1551 | '-1".\n' |
| 1552 | 'Note, *x* and *y* need not be the same type; consequently, ' |
| 1553 | '"u\'ab\' in\n' |
| 1554 | '\'abc\'" will return "True". Empty strings are always ' |
| 1555 | 'considered to be a\n' |
| 1556 | 'substring of any other string, so """ in "abc"" will return ' |
| 1557 | '"True".\n' |
| 1558 | '\n' |
| 1559 | 'Changed in version 2.3: Previously, *x* was required to be ' |
| 1560 | 'a string of\n' |
| 1561 | 'length "1".\n' |
| 1562 | '\n' |
| 1563 | 'For user-defined classes which define the "__contains__()" ' |
| 1564 | 'method, "x\n' |
| 1565 | 'in y" is true if and only if "y.__contains__(x)" is true.\n' |
| 1566 | '\n' |
| 1567 | 'For user-defined classes which do not define ' |
| 1568 | '"__contains__()" but do\n' |
| 1569 | 'define "__iter__()", "x in y" is true if some value "z" ' |
| 1570 | 'with "x == z"\n' |
| 1571 | 'is produced while iterating over "y". If an exception is ' |
| 1572 | 'raised\n' |
| 1573 | 'during the iteration, it is as if "in" raised that ' |
| 1574 | 'exception.\n' |
| 1575 | '\n' |
| 1576 | 'Lastly, the old-style iteration protocol is tried: if a ' |
| 1577 | 'class defines\n' |
| 1578 | '"__getitem__()", "x in y" is true if and only if there is a ' |
| 1579 | 'non-\n' |
| 1580 | 'negative integer index *i* such that "x == y[i]", and all ' |
| 1581 | 'lower\n' |
| 1582 | 'integer indices do not raise "IndexError" exception. (If ' |
| 1583 | 'any other\n' |
| 1584 | 'exception is raised, it is as if "in" raised that ' |
| 1585 | 'exception).\n' |
| 1586 | '\n' |
| 1587 | 'The operator "not in" is defined to have the inverse true ' |
| 1588 | 'value of\n' |
| 1589 | '"in".\n' |
| 1590 | '\n' |
| 1591 | 'The operators "is" and "is not" test for object identity: ' |
| 1592 | '"x is y" is\n' |
| 1593 | 'true if and only if *x* and *y* are the same object. "x is ' |
| 1594 | 'not y"\n' |
| 1595 | 'yields the inverse truth value. [7]\n', |
| 1596 | 'compound': '\n' |
| 1597 | 'Compound statements\n' |
| 1598 | '*******************\n' |
| 1599 | '\n' |
| 1600 | 'Compound statements contain (groups of) other statements; they ' |
| 1601 | 'affect\n' |
| 1602 | 'or control the execution of those other statements in some ' |
| 1603 | 'way. In\n' |
| 1604 | 'general, compound statements span multiple lines, although in ' |
| 1605 | 'simple\n' |
| 1606 | 'incarnations a whole compound statement may be contained in ' |
| 1607 | 'one line.\n' |
| 1608 | '\n' |
| 1609 | 'The "if", "while" and "for" statements implement traditional ' |
| 1610 | 'control\n' |
| 1611 | 'flow constructs. "try" specifies exception handlers and/or ' |
| 1612 | 'cleanup\n' |
| 1613 | 'code for a group of statements. Function and class ' |
| 1614 | 'definitions are\n' |
| 1615 | 'also syntactically compound statements.\n' |
| 1616 | '\n' |
| 1617 | "Compound statements consist of one or more 'clauses.' A " |
| 1618 | 'clause\n' |
| 1619 | "consists of a header and a 'suite.' The clause headers of a\n" |
| 1620 | 'particular compound statement are all at the same indentation ' |
| 1621 | 'level.\n' |
| 1622 | 'Each clause header begins with a uniquely identifying keyword ' |
| 1623 | 'and ends\n' |
| 1624 | 'with a colon. A suite is a group of statements controlled by ' |
| 1625 | 'a\n' |
| 1626 | 'clause. A suite can be one or more semicolon-separated ' |
| 1627 | 'simple\n' |
| 1628 | 'statements on the same line as the header, following the ' |
| 1629 | "header's\n" |
| 1630 | 'colon, or it can be one or more indented statements on ' |
| 1631 | 'subsequent\n' |
| 1632 | 'lines. Only the latter form of suite can contain nested ' |
| 1633 | 'compound\n' |
| 1634 | 'statements; the following is illegal, mostly because it ' |
| 1635 | "wouldn't be\n" |
| 1636 | 'clear to which "if" clause a following "else" clause would ' |
| 1637 | 'belong:\n' |
| 1638 | '\n' |
| 1639 | ' if test1: if test2: print x\n' |
| 1640 | '\n' |
| 1641 | 'Also note that the semicolon binds tighter than the colon in ' |
| 1642 | 'this\n' |
| 1643 | 'context, so that in the following example, either all or none ' |
| 1644 | 'of the\n' |
| 1645 | '"print" statements are executed:\n' |
| 1646 | '\n' |
| 1647 | ' if x < y < z: print x; print y; print z\n' |
| 1648 | '\n' |
| 1649 | 'Summarizing:\n' |
| 1650 | '\n' |
| 1651 | ' compound_stmt ::= if_stmt\n' |
| 1652 | ' | while_stmt\n' |
| 1653 | ' | for_stmt\n' |
| 1654 | ' | try_stmt\n' |
| 1655 | ' | with_stmt\n' |
| 1656 | ' | funcdef\n' |
| 1657 | ' | classdef\n' |
| 1658 | ' | decorated\n' |
| 1659 | ' suite ::= stmt_list NEWLINE | NEWLINE INDENT ' |
| 1660 | 'statement+ DEDENT\n' |
| 1661 | ' statement ::= stmt_list NEWLINE | compound_stmt\n' |
| 1662 | ' stmt_list ::= simple_stmt (";" simple_stmt)* [";"]\n' |
| 1663 | '\n' |
| 1664 | 'Note that statements always end in a "NEWLINE" possibly ' |
| 1665 | 'followed by a\n' |
| 1666 | '"DEDENT". Also note that optional continuation clauses always ' |
| 1667 | 'begin\n' |
| 1668 | 'with a keyword that cannot start a statement, thus there are ' |
| 1669 | 'no\n' |
| 1670 | 'ambiguities (the \'dangling "else"\' problem is solved in ' |
| 1671 | 'Python by\n' |
| 1672 | 'requiring nested "if" statements to be indented).\n' |
| 1673 | '\n' |
| 1674 | 'The formatting of the grammar rules in the following sections ' |
| 1675 | 'places\n' |
| 1676 | 'each clause on a separate line for clarity.\n' |
| 1677 | '\n' |
| 1678 | '\n' |
| 1679 | 'The "if" statement\n' |
| 1680 | '==================\n' |
| 1681 | '\n' |
| 1682 | 'The "if" statement is used for conditional execution:\n' |
| 1683 | '\n' |
| 1684 | ' if_stmt ::= "if" expression ":" suite\n' |
| 1685 | ' ( "elif" expression ":" suite )*\n' |
| 1686 | ' ["else" ":" suite]\n' |
| 1687 | '\n' |
| 1688 | 'It selects exactly one of the suites by evaluating the ' |
| 1689 | 'expressions one\n' |
| 1690 | 'by one until one is found to be true (see section Boolean ' |
| 1691 | 'operations\n' |
| 1692 | 'for the definition of true and false); then that suite is ' |
| 1693 | 'executed\n' |
| 1694 | '(and no other part of the "if" statement is executed or ' |
| 1695 | 'evaluated).\n' |
| 1696 | 'If all expressions are false, the suite of the "else" clause, ' |
| 1697 | 'if\n' |
| 1698 | 'present, is executed.\n' |
| 1699 | '\n' |
| 1700 | '\n' |
| 1701 | 'The "while" statement\n' |
| 1702 | '=====================\n' |
| 1703 | '\n' |
| 1704 | 'The "while" statement is used for repeated execution as long ' |
| 1705 | 'as an\n' |
| 1706 | 'expression is true:\n' |
| 1707 | '\n' |
| 1708 | ' while_stmt ::= "while" expression ":" suite\n' |
| 1709 | ' ["else" ":" suite]\n' |
| 1710 | '\n' |
| 1711 | 'This repeatedly tests the expression and, if it is true, ' |
| 1712 | 'executes the\n' |
| 1713 | 'first suite; if the expression is false (which may be the ' |
| 1714 | 'first time\n' |
| 1715 | 'it is tested) the suite of the "else" clause, if present, is ' |
| 1716 | 'executed\n' |
| 1717 | 'and the loop terminates.\n' |
| 1718 | '\n' |
| 1719 | 'A "break" statement executed in the first suite terminates the ' |
| 1720 | 'loop\n' |
| 1721 | 'without executing the "else" clause\'s suite. A "continue" ' |
| 1722 | 'statement\n' |
| 1723 | 'executed in the first suite skips the rest of the suite and ' |
| 1724 | 'goes back\n' |
| 1725 | 'to testing the expression.\n' |
| 1726 | '\n' |
| 1727 | '\n' |
| 1728 | 'The "for" statement\n' |
| 1729 | '===================\n' |
| 1730 | '\n' |
| 1731 | 'The "for" statement is used to iterate over the elements of a ' |
| 1732 | 'sequence\n' |
| 1733 | '(such as a string, tuple or list) or other iterable object:\n' |
| 1734 | '\n' |
| 1735 | ' for_stmt ::= "for" target_list "in" expression_list ":" ' |
| 1736 | 'suite\n' |
| 1737 | ' ["else" ":" suite]\n' |
| 1738 | '\n' |
| 1739 | 'The expression list is evaluated once; it should yield an ' |
| 1740 | 'iterable\n' |
| 1741 | 'object. An iterator is created for the result of the\n' |
| 1742 | '"expression_list". The suite is then executed once for each ' |
| 1743 | 'item\n' |
| 1744 | 'provided by the iterator, in the order of ascending indices. ' |
| 1745 | 'Each\n' |
| 1746 | 'item in turn is assigned to the target list using the standard ' |
| 1747 | 'rules\n' |
| 1748 | 'for assignments, and then the suite is executed. When the ' |
| 1749 | 'items are\n' |
| 1750 | 'exhausted (which is immediately when the sequence is empty), ' |
| 1751 | 'the suite\n' |
| 1752 | 'in the "else" clause, if present, is executed, and the loop\n' |
| 1753 | 'terminates.\n' |
| 1754 | '\n' |
| 1755 | 'A "break" statement executed in the first suite terminates the ' |
| 1756 | 'loop\n' |
| 1757 | 'without executing the "else" clause\'s suite. A "continue" ' |
| 1758 | 'statement\n' |
| 1759 | 'executed in the first suite skips the rest of the suite and ' |
| 1760 | 'continues\n' |
| 1761 | 'with the next item, or with the "else" clause if there was no ' |
| 1762 | 'next\n' |
| 1763 | 'item.\n' |
| 1764 | '\n' |
| 1765 | 'The suite may assign to the variable(s) in the target list; ' |
| 1766 | 'this does\n' |
| 1767 | 'not affect the next item assigned to it.\n' |
| 1768 | '\n' |
| 1769 | 'The target list is not deleted when the loop is finished, but ' |
| 1770 | 'if the\n' |
| 1771 | 'sequence is empty, it will not have been assigned to at all by ' |
| 1772 | 'the\n' |
| 1773 | 'loop. Hint: the built-in function "range()" returns a ' |
| 1774 | 'sequence of\n' |
| 1775 | 'integers suitable to emulate the effect of Pascal\'s "for i := ' |
| 1776 | 'a to b\n' |
| 1777 | 'do"; e.g., "range(3)" returns the list "[0, 1, 2]".\n' |
| 1778 | '\n' |
| 1779 | 'Note: There is a subtlety when the sequence is being modified ' |
| 1780 | 'by the\n' |
| 1781 | ' loop (this can only occur for mutable sequences, i.e. ' |
| 1782 | 'lists). An\n' |
| 1783 | ' internal counter is used to keep track of which item is used ' |
| 1784 | 'next,\n' |
| 1785 | ' and this is incremented on each iteration. When this ' |
| 1786 | 'counter has\n' |
| 1787 | ' reached the length of the sequence the loop terminates. ' |
| 1788 | 'This means\n' |
| 1789 | ' that if the suite deletes the current (or a previous) item ' |
| 1790 | 'from the\n' |
| 1791 | ' sequence, the next item will be skipped (since it gets the ' |
| 1792 | 'index of\n' |
| 1793 | ' the current item which has already been treated). Likewise, ' |
| 1794 | 'if the\n' |
| 1795 | ' suite inserts an item in the sequence before the current ' |
| 1796 | 'item, the\n' |
| 1797 | ' current item will be treated again the next time through the ' |
| 1798 | 'loop.\n' |
| 1799 | ' This can lead to nasty bugs that can be avoided by making a\n' |
| 1800 | ' temporary copy using a slice of the whole sequence, e.g.,\n' |
| 1801 | '\n' |
| 1802 | ' for x in a[:]:\n' |
| 1803 | ' if x < 0: a.remove(x)\n' |
| 1804 | '\n' |
| 1805 | '\n' |
| 1806 | 'The "try" statement\n' |
| 1807 | '===================\n' |
| 1808 | '\n' |
| 1809 | 'The "try" statement specifies exception handlers and/or ' |
| 1810 | 'cleanup code\n' |
| 1811 | 'for a group of statements:\n' |
| 1812 | '\n' |
| 1813 | ' try_stmt ::= try1_stmt | try2_stmt\n' |
| 1814 | ' try1_stmt ::= "try" ":" suite\n' |
| 1815 | ' ("except" [expression [("as" | ",") ' |
| 1816 | 'identifier]] ":" suite)+\n' |
| 1817 | ' ["else" ":" suite]\n' |
| 1818 | ' ["finally" ":" suite]\n' |
| 1819 | ' try2_stmt ::= "try" ":" suite\n' |
| 1820 | ' "finally" ":" suite\n' |
| 1821 | '\n' |
| 1822 | 'Changed in version 2.5: In previous versions of Python,\n' |
| 1823 | '"try"..."except"..."finally" did not work. "try"..."except" ' |
| 1824 | 'had to be\n' |
| 1825 | 'nested in "try"..."finally".\n' |
| 1826 | '\n' |
| 1827 | 'The "except" clause(s) specify one or more exception handlers. ' |
| 1828 | 'When no\n' |
| 1829 | 'exception occurs in the "try" clause, no exception handler is\n' |
| 1830 | 'executed. When an exception occurs in the "try" suite, a ' |
| 1831 | 'search for an\n' |
| 1832 | 'exception handler is started. This search inspects the except ' |
| 1833 | 'clauses\n' |
| 1834 | 'in turn until one is found that matches the exception. An ' |
| 1835 | 'expression-\n' |
| 1836 | 'less except clause, if present, must be last; it matches any\n' |
| 1837 | 'exception. For an except clause with an expression, that ' |
| 1838 | 'expression\n' |
| 1839 | 'is evaluated, and the clause matches the exception if the ' |
| 1840 | 'resulting\n' |
| 1841 | 'object is "compatible" with the exception. An object is ' |
| 1842 | 'compatible\n' |
| 1843 | 'with an exception if it is the class or a base class of the ' |
| 1844 | 'exception\n' |
| 1845 | 'object, or a tuple containing an item compatible with the ' |
| 1846 | 'exception.\n' |
| 1847 | '\n' |
| 1848 | 'If no except clause matches the exception, the search for an ' |
| 1849 | 'exception\n' |
| 1850 | 'handler continues in the surrounding code and on the ' |
| 1851 | 'invocation stack.\n' |
| 1852 | '[1]\n' |
| 1853 | '\n' |
| 1854 | 'If the evaluation of an expression in the header of an except ' |
| 1855 | 'clause\n' |
| 1856 | 'raises an exception, the original search for a handler is ' |
| 1857 | 'canceled and\n' |
| 1858 | 'a search starts for the new exception in the surrounding code ' |
| 1859 | 'and on\n' |
| 1860 | 'the call stack (it is treated as if the entire "try" statement ' |
| 1861 | 'raised\n' |
| 1862 | 'the exception).\n' |
| 1863 | '\n' |
| 1864 | 'When a matching except clause is found, the exception is ' |
| 1865 | 'assigned to\n' |
| 1866 | 'the target specified in that except clause, if present, and ' |
| 1867 | 'the except\n' |
| 1868 | "clause's suite is executed. All except clauses must have an\n" |
| 1869 | 'executable block. When the end of this block is reached, ' |
| 1870 | 'execution\n' |
| 1871 | 'continues normally after the entire try statement. (This ' |
| 1872 | 'means that\n' |
| 1873 | 'if two nested handlers exist for the same exception, and the ' |
| 1874 | 'exception\n' |
| 1875 | 'occurs in the try clause of the inner handler, the outer ' |
| 1876 | 'handler will\n' |
| 1877 | 'not handle the exception.)\n' |
| 1878 | '\n' |
| 1879 | "Before an except clause's suite is executed, details about " |
| 1880 | 'the\n' |
| 1881 | 'exception are assigned to three variables in the "sys" ' |
| 1882 | 'module:\n' |
| 1883 | '"sys.exc_type" receives the object identifying the exception;\n' |
| 1884 | '"sys.exc_value" receives the exception\'s parameter;\n' |
| 1885 | '"sys.exc_traceback" receives a traceback object (see section ' |
| 1886 | 'The\n' |
| 1887 | 'standard type hierarchy) identifying the point in the program ' |
| 1888 | 'where\n' |
| 1889 | 'the exception occurred. These details are also available ' |
| 1890 | 'through the\n' |
| 1891 | '"sys.exc_info()" function, which returns a tuple "(exc_type,\n' |
| 1892 | 'exc_value, exc_traceback)". Use of the corresponding ' |
| 1893 | 'variables is\n' |
| 1894 | 'deprecated in favor of this function, since their use is ' |
| 1895 | 'unsafe in a\n' |
| 1896 | 'threaded program. As of Python 1.5, the variables are ' |
| 1897 | 'restored to\n' |
| 1898 | 'their previous values (before the call) when returning from a ' |
| 1899 | 'function\n' |
| 1900 | 'that handled an exception.\n' |
| 1901 | '\n' |
| 1902 | 'The optional "else" clause is executed if and when control ' |
| 1903 | 'flows off\n' |
| 1904 | 'the end of the "try" clause. [2] Exceptions in the "else" ' |
| 1905 | 'clause are\n' |
| 1906 | 'not handled by the preceding "except" clauses.\n' |
| 1907 | '\n' |
| 1908 | 'If "finally" is present, it specifies a \'cleanup\' handler. ' |
| 1909 | 'The "try"\n' |
| 1910 | 'clause is executed, including any "except" and "else" ' |
| 1911 | 'clauses. If an\n' |
| 1912 | 'exception occurs in any of the clauses and is not handled, ' |
| 1913 | 'the\n' |
| 1914 | 'exception is temporarily saved. The "finally" clause is ' |
| 1915 | 'executed. If\n' |
| 1916 | 'there is a saved exception, it is re-raised at the end of the\n' |
| 1917 | '"finally" clause. If the "finally" clause raises another ' |
| 1918 | 'exception or\n' |
| 1919 | 'executes a "return" or "break" statement, the saved exception ' |
| 1920 | 'is\n' |
| 1921 | 'discarded:\n' |
| 1922 | '\n' |
| 1923 | ' >>> def f():\n' |
| 1924 | ' ... try:\n' |
| 1925 | ' ... 1/0\n' |
| 1926 | ' ... finally:\n' |
| 1927 | ' ... return 42\n' |
| 1928 | ' ...\n' |
| 1929 | ' >>> f()\n' |
| 1930 | ' 42\n' |
| 1931 | '\n' |
| 1932 | 'The exception information is not available to the program ' |
| 1933 | 'during\n' |
| 1934 | 'execution of the "finally" clause.\n' |
| 1935 | '\n' |
| 1936 | 'When a "return", "break" or "continue" statement is executed ' |
| 1937 | 'in the\n' |
| 1938 | '"try" suite of a "try"..."finally" statement, the "finally" ' |
| 1939 | 'clause is\n' |
| 1940 | 'also executed \'on the way out.\' A "continue" statement is ' |
| 1941 | 'illegal in\n' |
| 1942 | 'the "finally" clause. (The reason is a problem with the ' |
| 1943 | 'current\n' |
| 1944 | 'implementation --- this restriction may be lifted in the ' |
| 1945 | 'future).\n' |
| 1946 | '\n' |
| 1947 | 'The return value of a function is determined by the last ' |
| 1948 | '"return"\n' |
| 1949 | 'statement executed. Since the "finally" clause always ' |
| 1950 | 'executes, a\n' |
| 1951 | '"return" statement executed in the "finally" clause will ' |
| 1952 | 'always be the\n' |
| 1953 | 'last one executed:\n' |
| 1954 | '\n' |
| 1955 | ' >>> def foo():\n' |
| 1956 | ' ... try:\n' |
| 1957 | " ... return 'try'\n" |
| 1958 | ' ... finally:\n' |
| 1959 | " ... return 'finally'\n" |
| 1960 | ' ...\n' |
| 1961 | ' >>> foo()\n' |
| 1962 | " 'finally'\n" |
| 1963 | '\n' |
| 1964 | 'Additional information on exceptions can be found in section\n' |
| 1965 | 'Exceptions, and information on using the "raise" statement to ' |
| 1966 | 'generate\n' |
| 1967 | 'exceptions may be found in section The raise statement.\n' |
| 1968 | '\n' |
| 1969 | '\n' |
| 1970 | 'The "with" statement\n' |
| 1971 | '====================\n' |
| 1972 | '\n' |
| 1973 | 'New in version 2.5.\n' |
| 1974 | '\n' |
| 1975 | 'The "with" statement is used to wrap the execution of a block ' |
| 1976 | 'with\n' |
| 1977 | 'methods defined by a context manager (see section With ' |
| 1978 | 'Statement\n' |
| 1979 | 'Context Managers). This allows common ' |
| 1980 | '"try"..."except"..."finally"\n' |
| 1981 | 'usage patterns to be encapsulated for convenient reuse.\n' |
| 1982 | '\n' |
| 1983 | ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n' |
| 1984 | ' with_item ::= expression ["as" target]\n' |
| 1985 | '\n' |
| 1986 | 'The execution of the "with" statement with one "item" proceeds ' |
| 1987 | 'as\n' |
| 1988 | 'follows:\n' |
| 1989 | '\n' |
| 1990 | '1. The context expression (the expression given in the ' |
| 1991 | '"with_item")\n' |
| 1992 | ' is evaluated to obtain a context manager.\n' |
| 1993 | '\n' |
| 1994 | '2. The context manager\'s "__exit__()" is loaded for later ' |
| 1995 | 'use.\n' |
| 1996 | '\n' |
| 1997 | '3. The context manager\'s "__enter__()" method is invoked.\n' |
| 1998 | '\n' |
| 1999 | '4. If a target was included in the "with" statement, the ' |
| 2000 | 'return\n' |
| 2001 | ' value from "__enter__()" is assigned to it.\n' |
| 2002 | '\n' |
| 2003 | ' Note: The "with" statement guarantees that if the ' |
| 2004 | '"__enter__()"\n' |
| 2005 | ' method returns without an error, then "__exit__()" will ' |
| 2006 | 'always be\n' |
| 2007 | ' called. Thus, if an error occurs during the assignment to ' |
| 2008 | 'the\n' |
| 2009 | ' target list, it will be treated the same as an error ' |
| 2010 | 'occurring\n' |
| 2011 | ' within the suite would be. See step 6 below.\n' |
| 2012 | '\n' |
| 2013 | '5. The suite is executed.\n' |
| 2014 | '\n' |
| 2015 | '6. The context manager\'s "__exit__()" method is invoked. If ' |
| 2016 | 'an\n' |
| 2017 | ' exception caused the suite to be exited, its type, value, ' |
| 2018 | 'and\n' |
| 2019 | ' traceback are passed as arguments to "__exit__()". ' |
| 2020 | 'Otherwise, three\n' |
| 2021 | ' "None" arguments are supplied.\n' |
| 2022 | '\n' |
| 2023 | ' If the suite was exited due to an exception, and the return ' |
| 2024 | 'value\n' |
| 2025 | ' from the "__exit__()" method was false, the exception is ' |
| 2026 | 'reraised.\n' |
| 2027 | ' If the return value was true, the exception is suppressed, ' |
| 2028 | 'and\n' |
| 2029 | ' execution continues with the statement following the ' |
| 2030 | '"with"\n' |
| 2031 | ' statement.\n' |
| 2032 | '\n' |
| 2033 | ' If the suite was exited for any reason other than an ' |
| 2034 | 'exception, the\n' |
| 2035 | ' return value from "__exit__()" is ignored, and execution ' |
| 2036 | 'proceeds\n' |
| 2037 | ' at the normal location for the kind of exit that was ' |
| 2038 | 'taken.\n' |
| 2039 | '\n' |
| 2040 | 'With more than one item, the context managers are processed as ' |
| 2041 | 'if\n' |
| 2042 | 'multiple "with" statements were nested:\n' |
| 2043 | '\n' |
| 2044 | ' with A() as a, B() as b:\n' |
| 2045 | ' suite\n' |
| 2046 | '\n' |
| 2047 | 'is equivalent to\n' |
| 2048 | '\n' |
| 2049 | ' with A() as a:\n' |
| 2050 | ' with B() as b:\n' |
| 2051 | ' suite\n' |
| 2052 | '\n' |
| 2053 | 'Note: In Python 2.5, the "with" statement is only allowed when ' |
| 2054 | 'the\n' |
| 2055 | ' "with_statement" feature has been enabled. It is always ' |
| 2056 | 'enabled in\n' |
| 2057 | ' Python 2.6.\n' |
| 2058 | '\n' |
| 2059 | 'Changed in version 2.7: Support for multiple context ' |
| 2060 | 'expressions.\n' |
| 2061 | '\n' |
| 2062 | 'See also: **PEP 0343** - The "with" statement\n' |
| 2063 | '\n' |
| 2064 | ' The specification, background, and examples for the ' |
| 2065 | 'Python "with"\n' |
| 2066 | ' statement.\n' |
| 2067 | '\n' |
| 2068 | '\n' |
| 2069 | 'Function definitions\n' |
| 2070 | '====================\n' |
| 2071 | '\n' |
| 2072 | 'A function definition defines a user-defined function object ' |
| 2073 | '(see\n' |
| 2074 | 'section The standard type hierarchy):\n' |
| 2075 | '\n' |
| 2076 | ' decorated ::= decorators (classdef | funcdef)\n' |
| 2077 | ' decorators ::= decorator+\n' |
| 2078 | ' decorator ::= "@" dotted_name ["(" [argument_list ' |
| 2079 | '[","]] ")"] NEWLINE\n' |
| 2080 | ' funcdef ::= "def" funcname "(" [parameter_list] ")" ' |
| 2081 | '":" suite\n' |
| 2082 | ' dotted_name ::= identifier ("." identifier)*\n' |
| 2083 | ' parameter_list ::= (defparameter ",")*\n' |
| 2084 | ' ( "*" identifier ["," "**" identifier]\n' |
| 2085 | ' | "**" identifier\n' |
| 2086 | ' | defparameter [","] )\n' |
| 2087 | ' defparameter ::= parameter ["=" expression]\n' |
| 2088 | ' sublist ::= parameter ("," parameter)* [","]\n' |
| 2089 | ' parameter ::= identifier | "(" sublist ")"\n' |
| 2090 | ' funcname ::= identifier\n' |
| 2091 | '\n' |
| 2092 | 'A function definition is an executable statement. Its ' |
| 2093 | 'execution binds\n' |
| 2094 | 'the function name in the current local namespace to a function ' |
| 2095 | 'object\n' |
| 2096 | '(a wrapper around the executable code for the function). ' |
| 2097 | 'This\n' |
| 2098 | 'function object contains a reference to the current global ' |
| 2099 | 'namespace\n' |
| 2100 | 'as the global namespace to be used when the function is ' |
| 2101 | 'called.\n' |
| 2102 | '\n' |
| 2103 | 'The function definition does not execute the function body; ' |
| 2104 | 'this gets\n' |
| 2105 | 'executed only when the function is called. [3]\n' |
| 2106 | '\n' |
| 2107 | 'A function definition may be wrapped by one or more ' |
| 2108 | '*decorator*\n' |
| 2109 | 'expressions. Decorator expressions are evaluated when the ' |
| 2110 | 'function is\n' |
| 2111 | 'defined, in the scope that contains the function definition. ' |
| 2112 | 'The\n' |
| 2113 | 'result must be a callable, which is invoked with the function ' |
| 2114 | 'object\n' |
| 2115 | 'as the only argument. The returned value is bound to the ' |
| 2116 | 'function name\n' |
| 2117 | 'instead of the function object. Multiple decorators are ' |
| 2118 | 'applied in\n' |
| 2119 | 'nested fashion. For example, the following code:\n' |
| 2120 | '\n' |
| 2121 | ' @f1(arg)\n' |
| 2122 | ' @f2\n' |
| 2123 | ' def func(): pass\n' |
| 2124 | '\n' |
| 2125 | 'is equivalent to:\n' |
| 2126 | '\n' |
| 2127 | ' def func(): pass\n' |
| 2128 | ' func = f1(arg)(f2(func))\n' |
| 2129 | '\n' |
| 2130 | 'When one or more top-level *parameters* have the form ' |
| 2131 | '*parameter* "="\n' |
| 2132 | '*expression*, the function is said to have "default parameter ' |
| 2133 | 'values."\n' |
| 2134 | 'For a parameter with a default value, the corresponding ' |
| 2135 | '*argument* may\n' |
| 2136 | "be omitted from a call, in which case the parameter's default " |
| 2137 | 'value is\n' |
| 2138 | 'substituted. If a parameter has a default value, all ' |
| 2139 | 'following\n' |
| 2140 | 'parameters must also have a default value --- this is a ' |
| 2141 | 'syntactic\n' |
| 2142 | 'restriction that is not expressed by the grammar.\n' |
| 2143 | '\n' |
| 2144 | '**Default parameter values are evaluated when the function ' |
| 2145 | 'definition\n' |
| 2146 | 'is executed.** This means that the expression is evaluated ' |
| 2147 | 'once, when\n' |
| 2148 | 'the function is defined, and that the same "pre-computed" ' |
| 2149 | 'value is\n' |
| 2150 | 'used for each call. This is especially important to ' |
| 2151 | 'understand when a\n' |
| 2152 | 'default parameter is a mutable object, such as a list or a ' |
| 2153 | 'dictionary:\n' |
| 2154 | 'if the function modifies the object (e.g. by appending an item ' |
| 2155 | 'to a\n' |
| 2156 | 'list), the default value is in effect modified. This is ' |
| 2157 | 'generally not\n' |
| 2158 | 'what was intended. A way around this is to use "None" as ' |
| 2159 | 'the\n' |
| 2160 | 'default, and explicitly test for it in the body of the ' |
| 2161 | 'function, e.g.:\n' |
| 2162 | '\n' |
| 2163 | ' def whats_on_the_telly(penguin=None):\n' |
| 2164 | ' if penguin is None:\n' |
| 2165 | ' penguin = []\n' |
| 2166 | ' penguin.append("property of the zoo")\n' |
| 2167 | ' return penguin\n' |
| 2168 | '\n' |
| 2169 | 'Function call semantics are described in more detail in ' |
| 2170 | 'section Calls.\n' |
| 2171 | 'A function call always assigns values to all parameters ' |
| 2172 | 'mentioned in\n' |
| 2173 | 'the parameter list, either from position arguments, from ' |
| 2174 | 'keyword\n' |
| 2175 | 'arguments, or from default values. If the form ' |
| 2176 | '""*identifier"" is\n' |
| 2177 | 'present, it is initialized to a tuple receiving any excess ' |
| 2178 | 'positional\n' |
| 2179 | 'parameters, defaulting to the empty tuple. If the form\n' |
| 2180 | '""**identifier"" is present, it is initialized to a new ' |
| 2181 | 'dictionary\n' |
| 2182 | 'receiving any excess keyword arguments, defaulting to a new ' |
| 2183 | 'empty\n' |
| 2184 | 'dictionary.\n' |
| 2185 | '\n' |
| 2186 | 'It is also possible to create anonymous functions (functions ' |
| 2187 | 'not bound\n' |
| 2188 | 'to a name), for immediate use in expressions. This uses ' |
| 2189 | 'lambda\n' |
| 2190 | 'expressions, described in section Lambdas. Note that the ' |
| 2191 | 'lambda\n' |
| 2192 | 'expression is merely a shorthand for a simplified function ' |
| 2193 | 'definition;\n' |
| 2194 | 'a function defined in a ""def"" statement can be passed around ' |
| 2195 | 'or\n' |
| 2196 | 'assigned to another name just like a function defined by a ' |
| 2197 | 'lambda\n' |
| 2198 | 'expression. The ""def"" form is actually more powerful since ' |
| 2199 | 'it\n' |
| 2200 | 'allows the execution of multiple statements.\n' |
| 2201 | '\n' |
| 2202 | "**Programmer's note:** Functions are first-class objects. A " |
| 2203 | '""def""\n' |
| 2204 | 'form executed inside a function definition defines a local ' |
| 2205 | 'function\n' |
| 2206 | 'that can be returned or passed around. Free variables used in ' |
| 2207 | 'the\n' |
| 2208 | 'nested function can access the local variables of the ' |
| 2209 | 'function\n' |
| 2210 | 'containing the def. See section Naming and binding for ' |
| 2211 | 'details.\n' |
| 2212 | '\n' |
| 2213 | '\n' |
| 2214 | 'Class definitions\n' |
| 2215 | '=================\n' |
| 2216 | '\n' |
| 2217 | 'A class definition defines a class object (see section The ' |
| 2218 | 'standard\n' |
| 2219 | 'type hierarchy):\n' |
| 2220 | '\n' |
| 2221 | ' classdef ::= "class" classname [inheritance] ":" suite\n' |
| 2222 | ' inheritance ::= "(" [expression_list] ")"\n' |
| 2223 | ' classname ::= identifier\n' |
| 2224 | '\n' |
| 2225 | 'A class definition is an executable statement. It first ' |
| 2226 | 'evaluates the\n' |
| 2227 | 'inheritance list, if present. Each item in the inheritance ' |
| 2228 | 'list\n' |
| 2229 | 'should evaluate to a class object or class type which allows\n' |
| 2230 | "subclassing. The class's suite is then executed in a new " |
| 2231 | 'execution\n' |
| 2232 | 'frame (see section Naming and binding), using a newly created ' |
| 2233 | 'local\n' |
| 2234 | 'namespace and the original global namespace. (Usually, the ' |
| 2235 | 'suite\n' |
| 2236 | "contains only function definitions.) When the class's suite " |
| 2237 | 'finishes\n' |
| 2238 | 'execution, its execution frame is discarded but its local ' |
| 2239 | 'namespace is\n' |
| 2240 | 'saved. [4] A class object is then created using the ' |
| 2241 | 'inheritance list\n' |
| 2242 | 'for the base classes and the saved local namespace for the ' |
| 2243 | 'attribute\n' |
| 2244 | 'dictionary. The class name is bound to this class object in ' |
| 2245 | 'the\n' |
| 2246 | 'original local namespace.\n' |
| 2247 | '\n' |
| 2248 | "**Programmer's note:** Variables defined in the class " |
| 2249 | 'definition are\n' |
| 2250 | 'class variables; they are shared by all instances. To create ' |
| 2251 | 'instance\n' |
| 2252 | 'variables, they can be set in a method with "self.name = ' |
| 2253 | 'value". Both\n' |
| 2254 | 'class and instance variables are accessible through the ' |
| 2255 | 'notation\n' |
| 2256 | '""self.name"", and an instance variable hides a class variable ' |
| 2257 | 'with\n' |
| 2258 | 'the same name when accessed in this way. Class variables can ' |
| 2259 | 'be used\n' |
| 2260 | 'as defaults for instance variables, but using mutable values ' |
| 2261 | 'there can\n' |
| 2262 | 'lead to unexpected results. For *new-style class*es, ' |
| 2263 | 'descriptors can\n' |
| 2264 | 'be used to create instance variables with different ' |
| 2265 | 'implementation\n' |
| 2266 | 'details.\n' |
| 2267 | '\n' |
| 2268 | 'Class definitions, like function definitions, may be wrapped ' |
| 2269 | 'by one or\n' |
| 2270 | 'more *decorator* expressions. The evaluation rules for the ' |
| 2271 | 'decorator\n' |
| 2272 | 'expressions are the same as for functions. The result must be ' |
| 2273 | 'a class\n' |
| 2274 | 'object, which is then bound to the class name.\n' |
| 2275 | '\n' |
| 2276 | '-[ Footnotes ]-\n' |
| 2277 | '\n' |
| 2278 | '[1] The exception is propagated to the invocation stack ' |
| 2279 | 'unless\n' |
| 2280 | ' there is a "finally" clause which happens to raise ' |
| 2281 | 'another\n' |
| 2282 | ' exception. That new exception causes the old one to be ' |
| 2283 | 'lost.\n' |
| 2284 | '\n' |
| 2285 | '[2] Currently, control "flows off the end" except in the case ' |
| 2286 | 'of\n' |
| 2287 | ' an exception or the execution of a "return", "continue", ' |
| 2288 | 'or\n' |
| 2289 | ' "break" statement.\n' |
| 2290 | '\n' |
| 2291 | '[3] A string literal appearing as the first statement in the\n' |
| 2292 | " function body is transformed into the function's " |
| 2293 | '"__doc__"\n' |
| 2294 | " attribute and therefore the function's *docstring*.\n" |
| 2295 | '\n' |
| 2296 | '[4] A string literal appearing as the first statement in the ' |
| 2297 | 'class\n' |
| 2298 | ' body is transformed into the namespace\'s "__doc__" item ' |
| 2299 | 'and\n' |
| 2300 | " therefore the class's *docstring*.\n", |
| 2301 | 'context-managers': '\n' |
| 2302 | 'With Statement Context Managers\n' |
| 2303 | '*******************************\n' |
| 2304 | '\n' |
| 2305 | 'New in version 2.5.\n' |
| 2306 | '\n' |
| 2307 | 'A *context manager* is an object that defines the ' |
| 2308 | 'runtime context to\n' |
| 2309 | 'be established when executing a "with" statement. The ' |
| 2310 | 'context manager\n' |
| 2311 | 'handles the entry into, and the exit from, the desired ' |
| 2312 | 'runtime context\n' |
| 2313 | 'for the execution of the block of code. Context ' |
| 2314 | 'managers are normally\n' |
| 2315 | 'invoked using the "with" statement (described in ' |
| 2316 | 'section The with\n' |
| 2317 | 'statement), but can also be used by directly invoking ' |
| 2318 | 'their methods.\n' |
| 2319 | '\n' |
| 2320 | 'Typical uses of context managers include saving and ' |
| 2321 | 'restoring various\n' |
| 2322 | 'kinds of global state, locking and unlocking ' |
| 2323 | 'resources, closing opened\n' |
| 2324 | 'files, etc.\n' |
| 2325 | '\n' |
| 2326 | 'For more information on context managers, see Context ' |
| 2327 | 'Manager Types.\n' |
| 2328 | '\n' |
| 2329 | 'object.__enter__(self)\n' |
| 2330 | '\n' |
| 2331 | ' Enter the runtime context related to this object. ' |
| 2332 | 'The "with"\n' |
| 2333 | " statement will bind this method's return value to " |
| 2334 | 'the target(s)\n' |
| 2335 | ' specified in the "as" clause of the statement, if ' |
| 2336 | 'any.\n' |
| 2337 | '\n' |
| 2338 | 'object.__exit__(self, exc_type, exc_value, traceback)\n' |
| 2339 | '\n' |
| 2340 | ' Exit the runtime context related to this object. ' |
| 2341 | 'The parameters\n' |
| 2342 | ' describe the exception that caused the context to ' |
| 2343 | 'be exited. If the\n' |
| 2344 | ' context was exited without an exception, all three ' |
| 2345 | 'arguments will\n' |
| 2346 | ' be "None".\n' |
| 2347 | '\n' |
| 2348 | ' If an exception is supplied, and the method wishes ' |
| 2349 | 'to suppress the\n' |
| 2350 | ' exception (i.e., prevent it from being propagated), ' |
| 2351 | 'it should\n' |
| 2352 | ' return a true value. Otherwise, the exception will ' |
| 2353 | 'be processed\n' |
| 2354 | ' normally upon exit from this method.\n' |
| 2355 | '\n' |
| 2356 | ' Note that "__exit__()" methods should not reraise ' |
| 2357 | 'the passed-in\n' |
| 2358 | " exception; this is the caller's responsibility.\n" |
| 2359 | '\n' |
| 2360 | 'See also: **PEP 0343** - The "with" statement\n' |
| 2361 | '\n' |
| 2362 | ' The specification, background, and examples for ' |
| 2363 | 'the Python "with"\n' |
| 2364 | ' statement.\n', |
| 2365 | 'continue': '\n' |
| 2366 | 'The "continue" statement\n' |
| 2367 | '************************\n' |
| 2368 | '\n' |
| 2369 | ' continue_stmt ::= "continue"\n' |
| 2370 | '\n' |
| 2371 | '"continue" may only occur syntactically nested in a "for" or ' |
| 2372 | '"while"\n' |
| 2373 | 'loop, but not nested in a function or class definition or ' |
| 2374 | '"finally"\n' |
| 2375 | 'clause within that loop. It continues with the next cycle of ' |
| 2376 | 'the\n' |
| 2377 | 'nearest enclosing loop.\n' |
| 2378 | '\n' |
| 2379 | 'When "continue" passes control out of a "try" statement with ' |
| 2380 | 'a\n' |
| 2381 | '"finally" clause, that "finally" clause is executed before ' |
| 2382 | 'really\n' |
| 2383 | 'starting the next loop cycle.\n', |
| 2384 | 'conversions': '\n' |
| 2385 | 'Arithmetic conversions\n' |
| 2386 | '**********************\n' |
| 2387 | '\n' |
| 2388 | 'When a description of an arithmetic operator below uses the ' |
| 2389 | 'phrase\n' |
| 2390 | '"the numeric arguments are converted to a common type," the ' |
| 2391 | 'arguments\n' |
| 2392 | 'are coerced using the coercion rules listed at Coercion ' |
| 2393 | 'rules. If\n' |
| 2394 | 'both arguments are standard numeric types, the following ' |
| 2395 | 'coercions are\n' |
| 2396 | 'applied:\n' |
| 2397 | '\n' |
| 2398 | '* If either argument is a complex number, the other is ' |
| 2399 | 'converted to\n' |
| 2400 | ' complex;\n' |
| 2401 | '\n' |
| 2402 | '* otherwise, if either argument is a floating point number, ' |
| 2403 | 'the\n' |
| 2404 | ' other is converted to floating point;\n' |
| 2405 | '\n' |
| 2406 | '* otherwise, if either argument is a long integer, the ' |
| 2407 | 'other is\n' |
| 2408 | ' converted to long integer;\n' |
| 2409 | '\n' |
| 2410 | '* otherwise, both must be plain integers and no conversion ' |
| 2411 | 'is\n' |
| 2412 | ' necessary.\n' |
| 2413 | '\n' |
| 2414 | 'Some additional rules apply for certain operators (e.g., a ' |
| 2415 | 'string left\n' |
| 2416 | "argument to the '%' operator). Extensions can define their " |
| 2417 | 'own\n' |
| 2418 | 'coercions.\n', |
| 2419 | 'customization': '\n' |
| 2420 | 'Basic customization\n' |
| 2421 | '*******************\n' |
| 2422 | '\n' |
| 2423 | 'object.__new__(cls[, ...])\n' |
| 2424 | '\n' |
| 2425 | ' Called to create a new instance of class *cls*. ' |
| 2426 | '"__new__()" is a\n' |
| 2427 | ' static method (special-cased so you need not declare ' |
| 2428 | 'it as such)\n' |
| 2429 | ' that takes the class of which an instance was ' |
| 2430 | 'requested as its\n' |
| 2431 | ' first argument. The remaining arguments are those ' |
| 2432 | 'passed to the\n' |
| 2433 | ' object constructor expression (the call to the ' |
| 2434 | 'class). The return\n' |
| 2435 | ' value of "__new__()" should be the new object instance ' |
| 2436 | '(usually an\n' |
| 2437 | ' instance of *cls*).\n' |
| 2438 | '\n' |
| 2439 | ' Typical implementations create a new instance of the ' |
| 2440 | 'class by\n' |
| 2441 | ' invoking the superclass\'s "__new__()" method using\n' |
| 2442 | ' "super(currentclass, cls).__new__(cls[, ...])" with ' |
| 2443 | 'appropriate\n' |
| 2444 | ' arguments and then modifying the newly-created ' |
| 2445 | 'instance as\n' |
| 2446 | ' necessary before returning it.\n' |
| 2447 | '\n' |
| 2448 | ' If "__new__()" returns an instance of *cls*, then the ' |
| 2449 | 'new\n' |
| 2450 | ' instance\'s "__init__()" method will be invoked like\n' |
| 2451 | ' "__init__(self[, ...])", where *self* is the new ' |
| 2452 | 'instance and the\n' |
| 2453 | ' remaining arguments are the same as were passed to ' |
| 2454 | '"__new__()".\n' |
| 2455 | '\n' |
| 2456 | ' If "__new__()" does not return an instance of *cls*, ' |
| 2457 | 'then the new\n' |
| 2458 | ' instance\'s "__init__()" method will not be invoked.\n' |
| 2459 | '\n' |
| 2460 | ' "__new__()" is intended mainly to allow subclasses of ' |
| 2461 | 'immutable\n' |
| 2462 | ' types (like int, str, or tuple) to customize instance ' |
| 2463 | 'creation. It\n' |
| 2464 | ' is also commonly overridden in custom metaclasses in ' |
| 2465 | 'order to\n' |
| 2466 | ' customize class creation.\n' |
| 2467 | '\n' |
| 2468 | 'object.__init__(self[, ...])\n' |
| 2469 | '\n' |
| 2470 | ' Called after the instance has been created (by ' |
| 2471 | '"__new__()"), but\n' |
| 2472 | ' before it is returned to the caller. The arguments ' |
| 2473 | 'are those\n' |
| 2474 | ' passed to the class constructor expression. If a base ' |
| 2475 | 'class has an\n' |
| 2476 | ' "__init__()" method, the derived class\'s "__init__()" ' |
| 2477 | 'method, if\n' |
| 2478 | ' any, must explicitly call it to ensure proper ' |
| 2479 | 'initialization of the\n' |
| 2480 | ' base class part of the instance; for example:\n' |
| 2481 | ' "BaseClass.__init__(self, [args...])".\n' |
| 2482 | '\n' |
| 2483 | ' Because "__new__()" and "__init__()" work together in ' |
| 2484 | 'constructing\n' |
| 2485 | ' objects ("__new__()" to create it, and "__init__()" to ' |
| 2486 | 'customise\n' |
| 2487 | ' it), no non-"None" value may be returned by ' |
| 2488 | '"__init__()"; doing so\n' |
| 2489 | ' will cause a "TypeError" to be raised at runtime.\n' |
| 2490 | '\n' |
| 2491 | 'object.__del__(self)\n' |
| 2492 | '\n' |
| 2493 | ' Called when the instance is about to be destroyed. ' |
| 2494 | 'This is also\n' |
| 2495 | ' called a destructor. If a base class has a ' |
| 2496 | '"__del__()" method, the\n' |
| 2497 | ' derived class\'s "__del__()" method, if any, must ' |
| 2498 | 'explicitly call it\n' |
| 2499 | ' to ensure proper deletion of the base class part of ' |
| 2500 | 'the instance.\n' |
| 2501 | ' Note that it is possible (though not recommended!) for ' |
| 2502 | 'the\n' |
| 2503 | ' "__del__()" method to postpone destruction of the ' |
| 2504 | 'instance by\n' |
| 2505 | ' creating a new reference to it. It may then be called ' |
| 2506 | 'at a later\n' |
| 2507 | ' time when this new reference is deleted. It is not ' |
| 2508 | 'guaranteed that\n' |
| 2509 | ' "__del__()" methods are called for objects that still ' |
| 2510 | 'exist when\n' |
| 2511 | ' the interpreter exits.\n' |
| 2512 | '\n' |
| 2513 | ' Note: "del x" doesn\'t directly call "x.__del__()" --- ' |
| 2514 | 'the former\n' |
| 2515 | ' decrements the reference count for "x" by one, and ' |
| 2516 | 'the latter is\n' |
| 2517 | ' only called when "x"\'s reference count reaches ' |
| 2518 | 'zero. Some common\n' |
| 2519 | ' situations that may prevent the reference count of ' |
| 2520 | 'an object from\n' |
| 2521 | ' going to zero include: circular references between ' |
| 2522 | 'objects (e.g.,\n' |
| 2523 | ' a doubly-linked list or a tree data structure with ' |
| 2524 | 'parent and\n' |
| 2525 | ' child pointers); a reference to the object on the ' |
| 2526 | 'stack frame of\n' |
| 2527 | ' a function that caught an exception (the traceback ' |
| 2528 | 'stored in\n' |
| 2529 | ' "sys.exc_traceback" keeps the stack frame alive); or ' |
| 2530 | 'a reference\n' |
| 2531 | ' to the object on the stack frame that raised an ' |
| 2532 | 'unhandled\n' |
| 2533 | ' exception in interactive mode (the traceback stored ' |
| 2534 | 'in\n' |
| 2535 | ' "sys.last_traceback" keeps the stack frame alive). ' |
| 2536 | 'The first\n' |
| 2537 | ' situation can only be remedied by explicitly ' |
| 2538 | 'breaking the cycles;\n' |
| 2539 | ' the latter two situations can be resolved by storing ' |
| 2540 | '"None" in\n' |
| 2541 | ' "sys.exc_traceback" or "sys.last_traceback". ' |
| 2542 | 'Circular references\n' |
| 2543 | ' which are garbage are detected when the option cycle ' |
| 2544 | 'detector is\n' |
| 2545 | " enabled (it's on by default), but can only be " |
| 2546 | 'cleaned up if there\n' |
| 2547 | ' are no Python-level "__del__()" methods involved. ' |
| 2548 | 'Refer to the\n' |
| 2549 | ' documentation for the "gc" module for more ' |
| 2550 | 'information about how\n' |
| 2551 | ' "__del__()" methods are handled by the cycle ' |
| 2552 | 'detector,\n' |
| 2553 | ' particularly the description of the "garbage" ' |
| 2554 | 'value.\n' |
| 2555 | '\n' |
| 2556 | ' Warning: Due to the precarious circumstances under ' |
| 2557 | 'which\n' |
| 2558 | ' "__del__()" methods are invoked, exceptions that ' |
| 2559 | 'occur during\n' |
| 2560 | ' their execution are ignored, and a warning is ' |
| 2561 | 'printed to\n' |
| 2562 | ' "sys.stderr" instead. Also, when "__del__()" is ' |
| 2563 | 'invoked in\n' |
| 2564 | ' response to a module being deleted (e.g., when ' |
| 2565 | 'execution of the\n' |
| 2566 | ' program is done), other globals referenced by the ' |
| 2567 | '"__del__()"\n' |
| 2568 | ' method may already have been deleted or in the ' |
| 2569 | 'process of being\n' |
| 2570 | ' torn down (e.g. the import machinery shutting ' |
| 2571 | 'down). For this\n' |
| 2572 | ' reason, "__del__()" methods should do the absolute ' |
| 2573 | 'minimum needed\n' |
| 2574 | ' to maintain external invariants. Starting with ' |
| 2575 | 'version 1.5,\n' |
| 2576 | ' Python guarantees that globals whose name begins ' |
| 2577 | 'with a single\n' |
| 2578 | ' underscore are deleted from their module before ' |
| 2579 | 'other globals are\n' |
| 2580 | ' deleted; if no other references to such globals ' |
| 2581 | 'exist, this may\n' |
| 2582 | ' help in assuring that imported modules are still ' |
| 2583 | 'available at the\n' |
| 2584 | ' time when the "__del__()" method is called.\n' |
| 2585 | '\n' |
| 2586 | ' See also the "-R" command-line option.\n' |
| 2587 | '\n' |
| 2588 | 'object.__repr__(self)\n' |
| 2589 | '\n' |
| 2590 | ' Called by the "repr()" built-in function and by string ' |
| 2591 | 'conversions\n' |
| 2592 | ' (reverse quotes) to compute the "official" string ' |
| 2593 | 'representation of\n' |
| 2594 | ' an object. If at all possible, this should look like ' |
| 2595 | 'a valid\n' |
| 2596 | ' Python expression that could be used to recreate an ' |
| 2597 | 'object with the\n' |
| 2598 | ' same value (given an appropriate environment). If ' |
| 2599 | 'this is not\n' |
| 2600 | ' possible, a string of the form "<...some useful ' |
| 2601 | 'description...>"\n' |
| 2602 | ' should be returned. The return value must be a string ' |
| 2603 | 'object. If a\n' |
| 2604 | ' class defines "__repr__()" but not "__str__()", then ' |
| 2605 | '"__repr__()"\n' |
| 2606 | ' is also used when an "informal" string representation ' |
| 2607 | 'of instances\n' |
| 2608 | ' of that class is required.\n' |
| 2609 | '\n' |
| 2610 | ' This is typically used for debugging, so it is ' |
| 2611 | 'important that the\n' |
| 2612 | ' representation is information-rich and unambiguous.\n' |
| 2613 | '\n' |
| 2614 | 'object.__str__(self)\n' |
| 2615 | '\n' |
| 2616 | ' Called by the "str()" built-in function and by the ' |
| 2617 | '"print"\n' |
| 2618 | ' statement to compute the "informal" string ' |
| 2619 | 'representation of an\n' |
| 2620 | ' object. This differs from "__repr__()" in that it ' |
| 2621 | 'does not have to\n' |
| 2622 | ' be a valid Python expression: a more convenient or ' |
| 2623 | 'concise\n' |
| 2624 | ' representation may be used instead. The return value ' |
| 2625 | 'must be a\n' |
| 2626 | ' string object.\n' |
| 2627 | '\n' |
| 2628 | 'object.__lt__(self, other)\n' |
| 2629 | 'object.__le__(self, other)\n' |
| 2630 | 'object.__eq__(self, other)\n' |
| 2631 | 'object.__ne__(self, other)\n' |
| 2632 | 'object.__gt__(self, other)\n' |
| 2633 | 'object.__ge__(self, other)\n' |
| 2634 | '\n' |
| 2635 | ' New in version 2.1.\n' |
| 2636 | '\n' |
| 2637 | ' These are the so-called "rich comparison" methods, and ' |
| 2638 | 'are called\n' |
| 2639 | ' for comparison operators in preference to "__cmp__()" ' |
| 2640 | 'below. The\n' |
| 2641 | ' correspondence between operator symbols and method ' |
| 2642 | 'names is as\n' |
| 2643 | ' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls ' |
| 2644 | '"x.__le__(y)",\n' |
| 2645 | ' "x==y" calls "x.__eq__(y)", "x!=y" and "x<>y" call ' |
| 2646 | '"x.__ne__(y)",\n' |
| 2647 | ' "x>y" calls "x.__gt__(y)", and "x>=y" calls ' |
| 2648 | '"x.__ge__(y)".\n' |
| 2649 | '\n' |
| 2650 | ' A rich comparison method may return the singleton ' |
| 2651 | '"NotImplemented"\n' |
| 2652 | ' if it does not implement the operation for a given ' |
| 2653 | 'pair of\n' |
| 2654 | ' arguments. By convention, "False" and "True" are ' |
| 2655 | 'returned for a\n' |
| 2656 | ' successful comparison. However, these methods can ' |
| 2657 | 'return any value,\n' |
| 2658 | ' so if the comparison operator is used in a Boolean ' |
| 2659 | 'context (e.g.,\n' |
| 2660 | ' in the condition of an "if" statement), Python will ' |
| 2661 | 'call "bool()"\n' |
| 2662 | ' on the value to determine if the result is true or ' |
| 2663 | 'false.\n' |
| 2664 | '\n' |
| 2665 | ' There are no implied relationships among the ' |
| 2666 | 'comparison operators.\n' |
| 2667 | ' The truth of "x==y" does not imply that "x!=y" is ' |
| 2668 | 'false.\n' |
| 2669 | ' Accordingly, when defining "__eq__()", one should also ' |
| 2670 | 'define\n' |
| 2671 | ' "__ne__()" so that the operators will behave as ' |
| 2672 | 'expected. See the\n' |
| 2673 | ' paragraph on "__hash__()" for some important notes on ' |
| 2674 | 'creating\n' |
| 2675 | ' *hashable* objects which support custom comparison ' |
| 2676 | 'operations and\n' |
| 2677 | ' are usable as dictionary keys.\n' |
| 2678 | '\n' |
| 2679 | ' There are no swapped-argument versions of these ' |
| 2680 | 'methods (to be used\n' |
| 2681 | ' when the left argument does not support the operation ' |
| 2682 | 'but the right\n' |
| 2683 | ' argument does); rather, "__lt__()" and "__gt__()" are ' |
| 2684 | "each other's\n" |
| 2685 | ' reflection, "__le__()" and "__ge__()" are each ' |
| 2686 | "other's reflection,\n" |
| 2687 | ' and "__eq__()" and "__ne__()" are their own ' |
| 2688 | 'reflection.\n' |
| 2689 | '\n' |
| 2690 | ' Arguments to rich comparison methods are never ' |
| 2691 | 'coerced.\n' |
| 2692 | '\n' |
| 2693 | ' To automatically generate ordering operations from a ' |
| 2694 | 'single root\n' |
| 2695 | ' operation, see "functools.total_ordering()".\n' |
| 2696 | '\n' |
| 2697 | 'object.__cmp__(self, other)\n' |
| 2698 | '\n' |
| 2699 | ' Called by comparison operations if rich comparison ' |
| 2700 | '(see above) is\n' |
| 2701 | ' not defined. Should return a negative integer if ' |
| 2702 | '"self < other",\n' |
| 2703 | ' zero if "self == other", a positive integer if "self > ' |
| 2704 | 'other". If\n' |
| 2705 | ' no "__cmp__()", "__eq__()" or "__ne__()" operation is ' |
| 2706 | 'defined,\n' |
| 2707 | ' class instances are compared by object identity ' |
| 2708 | '("address"). See\n' |
| 2709 | ' also the description of "__hash__()" for some ' |
| 2710 | 'important notes on\n' |
| 2711 | ' creating *hashable* objects which support custom ' |
| 2712 | 'comparison\n' |
| 2713 | ' operations and are usable as dictionary keys. (Note: ' |
| 2714 | 'the\n' |
| 2715 | ' restriction that exceptions are not propagated by ' |
| 2716 | '"__cmp__()" has\n' |
| 2717 | ' been removed since Python 1.5.)\n' |
| 2718 | '\n' |
| 2719 | 'object.__rcmp__(self, other)\n' |
| 2720 | '\n' |
| 2721 | ' Changed in version 2.1: No longer supported.\n' |
| 2722 | '\n' |
| 2723 | 'object.__hash__(self)\n' |
| 2724 | '\n' |
| 2725 | ' Called by built-in function "hash()" and for ' |
| 2726 | 'operations on members\n' |
| 2727 | ' of hashed collections including "set", "frozenset", ' |
| 2728 | 'and "dict".\n' |
| 2729 | ' "__hash__()" should return an integer. The only ' |
| 2730 | 'required property\n' |
| 2731 | ' is that objects which compare equal have the same hash ' |
| 2732 | 'value; it is\n' |
| 2733 | ' advised to somehow mix together (e.g. using exclusive ' |
| 2734 | 'or) the hash\n' |
| 2735 | ' values for the components of the object that also play ' |
| 2736 | 'a part in\n' |
| 2737 | ' comparison of objects.\n' |
| 2738 | '\n' |
| 2739 | ' If a class does not define a "__cmp__()" or "__eq__()" ' |
| 2740 | 'method it\n' |
| 2741 | ' should not define a "__hash__()" operation either; if ' |
| 2742 | 'it defines\n' |
| 2743 | ' "__cmp__()" or "__eq__()" but not "__hash__()", its ' |
| 2744 | 'instances will\n' |
| 2745 | ' not be usable in hashed collections. If a class ' |
| 2746 | 'defines mutable\n' |
| 2747 | ' objects and implements a "__cmp__()" or "__eq__()" ' |
| 2748 | 'method, it\n' |
| 2749 | ' should not implement "__hash__()", since hashable ' |
| 2750 | 'collection\n' |
| 2751 | " implementations require that a object's hash value is " |
| 2752 | 'immutable (if\n' |
| 2753 | " the object's hash value changes, it will be in the " |
| 2754 | 'wrong hash\n' |
| 2755 | ' bucket).\n' |
| 2756 | '\n' |
| 2757 | ' User-defined classes have "__cmp__()" and "__hash__()" ' |
| 2758 | 'methods by\n' |
| 2759 | ' default; with them, all objects compare unequal ' |
| 2760 | '(except with\n' |
| 2761 | ' themselves) and "x.__hash__()" returns a result ' |
| 2762 | 'derived from\n' |
| 2763 | ' "id(x)".\n' |
| 2764 | '\n' |
| 2765 | ' Classes which inherit a "__hash__()" method from a ' |
| 2766 | 'parent class but\n' |
| 2767 | ' change the meaning of "__cmp__()" or "__eq__()" such ' |
| 2768 | 'that the hash\n' |
| 2769 | ' value returned is no longer appropriate (e.g. by ' |
| 2770 | 'switching to a\n' |
| 2771 | ' value-based concept of equality instead of the default ' |
| 2772 | 'identity\n' |
| 2773 | ' based equality) can explicitly flag themselves as ' |
| 2774 | 'being unhashable\n' |
| 2775 | ' by setting "__hash__ = None" in the class definition. ' |
| 2776 | 'Doing so\n' |
| 2777 | ' means that not only will instances of the class raise ' |
| 2778 | 'an\n' |
| 2779 | ' appropriate "TypeError" when a program attempts to ' |
| 2780 | 'retrieve their\n' |
| 2781 | ' hash value, but they will also be correctly identified ' |
| 2782 | 'as\n' |
| 2783 | ' unhashable when checking "isinstance(obj, ' |
| 2784 | 'collections.Hashable)"\n' |
| 2785 | ' (unlike classes which define their own "__hash__()" to ' |
| 2786 | 'explicitly\n' |
| 2787 | ' raise "TypeError").\n' |
| 2788 | '\n' |
| 2789 | ' Changed in version 2.5: "__hash__()" may now also ' |
| 2790 | 'return a long\n' |
| 2791 | ' integer object; the 32-bit integer is then derived ' |
| 2792 | 'from the hash of\n' |
| 2793 | ' that object.\n' |
| 2794 | '\n' |
| 2795 | ' Changed in version 2.6: "__hash__" may now be set to ' |
| 2796 | '"None" to\n' |
| 2797 | ' explicitly flag instances of a class as unhashable.\n' |
| 2798 | '\n' |
| 2799 | 'object.__nonzero__(self)\n' |
| 2800 | '\n' |
| 2801 | ' Called to implement truth value testing and the ' |
| 2802 | 'built-in operation\n' |
| 2803 | ' "bool()"; should return "False" or "True", or their ' |
| 2804 | 'integer\n' |
| 2805 | ' equivalents "0" or "1". When this method is not ' |
| 2806 | 'defined,\n' |
| 2807 | ' "__len__()" is called, if it is defined, and the ' |
| 2808 | 'object is\n' |
| 2809 | ' considered true if its result is nonzero. If a class ' |
| 2810 | 'defines\n' |
| 2811 | ' neither "__len__()" nor "__nonzero__()", all its ' |
| 2812 | 'instances are\n' |
| 2813 | ' considered true.\n' |
| 2814 | '\n' |
| 2815 | 'object.__unicode__(self)\n' |
| 2816 | '\n' |
| 2817 | ' Called to implement "unicode()" built-in; should ' |
| 2818 | 'return a Unicode\n' |
| 2819 | ' object. When this method is not defined, string ' |
| 2820 | 'conversion is\n' |
| 2821 | ' attempted, and the result of string conversion is ' |
| 2822 | 'converted to\n' |
| 2823 | ' Unicode using the system default encoding.\n', |
| 2824 | 'debugger': '\n' |
| 2825 | '"pdb" --- The Python Debugger\n' |
| 2826 | '*****************************\n' |
| 2827 | '\n' |
| 2828 | '**Source code:** Lib/pdb.py\n' |
| 2829 | '\n' |
| 2830 | '======================================================================\n' |
| 2831 | '\n' |
| 2832 | 'The module "pdb" defines an interactive source code debugger ' |
| 2833 | 'for\n' |
| 2834 | 'Python programs. It supports setting (conditional) ' |
| 2835 | 'breakpoints and\n' |
| 2836 | 'single stepping at the source line level, inspection of stack ' |
| 2837 | 'frames,\n' |
| 2838 | 'source code listing, and evaluation of arbitrary Python code ' |
| 2839 | 'in the\n' |
| 2840 | 'context of any stack frame. It also supports post-mortem ' |
| 2841 | 'debugging\n' |
| 2842 | 'and can be called under program control.\n' |
| 2843 | '\n' |
| 2844 | 'The debugger is extensible --- it is actually defined as the ' |
| 2845 | 'class\n' |
| 2846 | '"Pdb". This is currently undocumented but easily understood by ' |
| 2847 | 'reading\n' |
| 2848 | 'the source. The extension interface uses the modules "bdb" ' |
| 2849 | 'and "cmd".\n' |
| 2850 | '\n' |
| 2851 | 'The debugger\'s prompt is "(Pdb)". Typical usage to run a ' |
| 2852 | 'program under\n' |
| 2853 | 'control of the debugger is:\n' |
| 2854 | '\n' |
| 2855 | ' >>> import pdb\n' |
| 2856 | ' >>> import mymodule\n' |
| 2857 | " >>> pdb.run('mymodule.test()')\n" |
| 2858 | ' > <string>(0)?()\n' |
| 2859 | ' (Pdb) continue\n' |
| 2860 | ' > <string>(1)?()\n' |
| 2861 | ' (Pdb) continue\n' |
| 2862 | " NameError: 'spam'\n" |
| 2863 | ' > <string>(1)?()\n' |
| 2864 | ' (Pdb)\n' |
| 2865 | '\n' |
| 2866 | '"pdb.py" can also be invoked as a script to debug other ' |
| 2867 | 'scripts. For\n' |
| 2868 | 'example:\n' |
| 2869 | '\n' |
| 2870 | ' python -m pdb myscript.py\n' |
| 2871 | '\n' |
| 2872 | 'When invoked as a script, pdb will automatically enter ' |
| 2873 | 'post-mortem\n' |
| 2874 | 'debugging if the program being debugged exits abnormally. ' |
| 2875 | 'After post-\n' |
| 2876 | 'mortem debugging (or after normal exit of the program), pdb ' |
| 2877 | 'will\n' |
| 2878 | "restart the program. Automatic restarting preserves pdb's " |
| 2879 | 'state (such\n' |
| 2880 | 'as breakpoints) and in most cases is more useful than quitting ' |
| 2881 | 'the\n' |
| 2882 | "debugger upon program's exit.\n" |
| 2883 | '\n' |
| 2884 | 'New in version 2.4: Restarting post-mortem behavior added.\n' |
| 2885 | '\n' |
| 2886 | 'The typical usage to break into the debugger from a running ' |
| 2887 | 'program is\n' |
| 2888 | 'to insert\n' |
| 2889 | '\n' |
| 2890 | ' import pdb; pdb.set_trace()\n' |
| 2891 | '\n' |
| 2892 | 'at the location you want to break into the debugger. You can ' |
| 2893 | 'then\n' |
| 2894 | 'step through the code following this statement, and continue ' |
| 2895 | 'running\n' |
| 2896 | 'without the debugger using the "c" command.\n' |
| 2897 | '\n' |
| 2898 | 'The typical usage to inspect a crashed program is:\n' |
| 2899 | '\n' |
| 2900 | ' >>> import pdb\n' |
| 2901 | ' >>> import mymodule\n' |
| 2902 | ' >>> mymodule.test()\n' |
| 2903 | ' Traceback (most recent call last):\n' |
| 2904 | ' File "<stdin>", line 1, in ?\n' |
| 2905 | ' File "./mymodule.py", line 4, in test\n' |
| 2906 | ' test2()\n' |
| 2907 | ' File "./mymodule.py", line 3, in test2\n' |
| 2908 | ' print spam\n' |
| 2909 | ' NameError: spam\n' |
| 2910 | ' >>> pdb.pm()\n' |
| 2911 | ' > ./mymodule.py(3)test2()\n' |
| 2912 | ' -> print spam\n' |
| 2913 | ' (Pdb)\n' |
| 2914 | '\n' |
| 2915 | 'The module defines the following functions; each enters the ' |
| 2916 | 'debugger\n' |
| 2917 | 'in a slightly different way:\n' |
| 2918 | '\n' |
| 2919 | 'pdb.run(statement[, globals[, locals]])\n' |
| 2920 | '\n' |
| 2921 | ' Execute the *statement* (given as a string) under debugger ' |
| 2922 | 'control.\n' |
| 2923 | ' The debugger prompt appears before any code is executed; ' |
| 2924 | 'you can\n' |
| 2925 | ' set breakpoints and type "continue", or you can step ' |
| 2926 | 'through the\n' |
| 2927 | ' statement using "step" or "next" (all these commands are ' |
| 2928 | 'explained\n' |
| 2929 | ' below). The optional *globals* and *locals* arguments ' |
| 2930 | 'specify the\n' |
| 2931 | ' environment in which the code is executed; by default the\n' |
| 2932 | ' dictionary of the module "__main__" is used. (See the ' |
| 2933 | 'explanation\n' |
| 2934 | ' of the "exec" statement or the "eval()" built-in ' |
| 2935 | 'function.)\n' |
| 2936 | '\n' |
| 2937 | 'pdb.runeval(expression[, globals[, locals]])\n' |
| 2938 | '\n' |
| 2939 | ' Evaluate the *expression* (given as a string) under ' |
| 2940 | 'debugger\n' |
| 2941 | ' control. When "runeval()" returns, it returns the value of ' |
| 2942 | 'the\n' |
| 2943 | ' expression. Otherwise this function is similar to ' |
| 2944 | '"run()".\n' |
| 2945 | '\n' |
| 2946 | 'pdb.runcall(function[, argument, ...])\n' |
| 2947 | '\n' |
| 2948 | ' Call the *function* (a function or method object, not a ' |
| 2949 | 'string)\n' |
| 2950 | ' with the given arguments. When "runcall()" returns, it ' |
| 2951 | 'returns\n' |
| 2952 | ' whatever the function call returned. The debugger prompt ' |
| 2953 | 'appears\n' |
| 2954 | ' as soon as the function is entered.\n' |
| 2955 | '\n' |
| 2956 | 'pdb.set_trace()\n' |
| 2957 | '\n' |
| 2958 | ' Enter the debugger at the calling stack frame. This is ' |
| 2959 | 'useful to\n' |
| 2960 | ' hard-code a breakpoint at a given point in a program, even ' |
| 2961 | 'if the\n' |
| 2962 | ' code is not otherwise being debugged (e.g. when an ' |
| 2963 | 'assertion\n' |
| 2964 | ' fails).\n' |
| 2965 | '\n' |
| 2966 | 'pdb.post_mortem([traceback])\n' |
| 2967 | '\n' |
| 2968 | ' Enter post-mortem debugging of the given *traceback* ' |
| 2969 | 'object. If no\n' |
| 2970 | ' *traceback* is given, it uses the one of the exception that ' |
| 2971 | 'is\n' |
| 2972 | ' currently being handled (an exception must be being handled ' |
| 2973 | 'if the\n' |
| 2974 | ' default is to be used).\n' |
| 2975 | '\n' |
| 2976 | 'pdb.pm()\n' |
| 2977 | '\n' |
| 2978 | ' Enter post-mortem debugging of the traceback found in\n' |
| 2979 | ' "sys.last_traceback".\n' |
| 2980 | '\n' |
| 2981 | 'The "run*" functions and "set_trace()" are aliases for ' |
| 2982 | 'instantiating\n' |
| 2983 | 'the "Pdb" class and calling the method of the same name. If ' |
| 2984 | 'you want\n' |
| 2985 | 'to access further features, you have to do this yourself:\n' |
| 2986 | '\n' |
| 2987 | "class class pdb.Pdb(completekey='tab', stdin=None, " |
| 2988 | 'stdout=None, skip=None)\n' |
| 2989 | '\n' |
| 2990 | ' "Pdb" is the debugger class.\n' |
| 2991 | '\n' |
| 2992 | ' The *completekey*, *stdin* and *stdout* arguments are ' |
| 2993 | 'passed to the\n' |
| 2994 | ' underlying "cmd.Cmd" class; see the description there.\n' |
| 2995 | '\n' |
| 2996 | ' The *skip* argument, if given, must be an iterable of ' |
| 2997 | 'glob-style\n' |
| 2998 | ' module name patterns. The debugger will not step into ' |
| 2999 | 'frames that\n' |
| 3000 | ' originate in a module that matches one of these patterns. ' |
| 3001 | '[1]\n' |
| 3002 | '\n' |
| 3003 | ' Example call to enable tracing with *skip*:\n' |
| 3004 | '\n' |
| 3005 | " import pdb; pdb.Pdb(skip=['django.*']).set_trace()\n" |
| 3006 | '\n' |
| 3007 | ' New in version 2.7: The *skip* argument.\n' |
| 3008 | '\n' |
| 3009 | ' run(statement[, globals[, locals]])\n' |
| 3010 | ' runeval(expression[, globals[, locals]])\n' |
| 3011 | ' runcall(function[, argument, ...])\n' |
| 3012 | ' set_trace()\n' |
| 3013 | '\n' |
| 3014 | ' See the documentation for the functions explained ' |
| 3015 | 'above.\n', |
| 3016 | 'del': '\n' |
| 3017 | 'The "del" statement\n' |
| 3018 | '*******************\n' |
| 3019 | '\n' |
| 3020 | ' del_stmt ::= "del" target_list\n' |
| 3021 | '\n' |
| 3022 | 'Deletion is recursively defined very similar to the way assignment ' |
| 3023 | 'is\n' |
| 3024 | 'defined. Rather than spelling it out in full details, here are ' |
| 3025 | 'some\n' |
| 3026 | 'hints.\n' |
| 3027 | '\n' |
| 3028 | 'Deletion of a target list recursively deletes each target, from ' |
| 3029 | 'left\n' |
| 3030 | 'to right.\n' |
| 3031 | '\n' |
| 3032 | 'Deletion of a name removes the binding of that name from the local ' |
| 3033 | 'or\n' |
| 3034 | 'global namespace, depending on whether the name occurs in a ' |
| 3035 | '"global"\n' |
| 3036 | 'statement in the same code block. If the name is unbound, a\n' |
| 3037 | '"NameError" exception will be raised.\n' |
| 3038 | '\n' |
| 3039 | 'It is illegal to delete a name from the local namespace if it ' |
| 3040 | 'occurs\n' |
| 3041 | 'as a free variable in a nested block.\n' |
| 3042 | '\n' |
| 3043 | 'Deletion of attribute references, subscriptions and slicings is ' |
| 3044 | 'passed\n' |
| 3045 | 'to the primary object involved; deletion of a slicing is in ' |
| 3046 | 'general\n' |
| 3047 | 'equivalent to assignment of an empty slice of the right type (but ' |
| 3048 | 'even\n' |
| 3049 | 'this is determined by the sliced object).\n', |
| 3050 | 'dict': '\n' |
| 3051 | 'Dictionary displays\n' |
| 3052 | '*******************\n' |
| 3053 | '\n' |
| 3054 | 'A dictionary display is a possibly empty series of key/datum ' |
| 3055 | 'pairs\n' |
| 3056 | 'enclosed in curly braces:\n' |
| 3057 | '\n' |
| 3058 | ' dict_display ::= "{" [key_datum_list | ' |
| 3059 | 'dict_comprehension] "}"\n' |
| 3060 | ' key_datum_list ::= key_datum ("," key_datum)* [","]\n' |
| 3061 | ' key_datum ::= expression ":" expression\n' |
| 3062 | ' dict_comprehension ::= expression ":" expression comp_for\n' |
| 3063 | '\n' |
| 3064 | 'A dictionary display yields a new dictionary object.\n' |
| 3065 | '\n' |
| 3066 | 'If a comma-separated sequence of key/datum pairs is given, they ' |
| 3067 | 'are\n' |
| 3068 | 'evaluated from left to right to define the entries of the ' |
| 3069 | 'dictionary:\n' |
| 3070 | 'each key object is used as a key into the dictionary to store the\n' |
| 3071 | 'corresponding datum. This means that you can specify the same ' |
| 3072 | 'key\n' |
| 3073 | "multiple times in the key/datum list, and the final dictionary's " |
| 3074 | 'value\n' |
| 3075 | 'for that key will be the last one given.\n' |
| 3076 | '\n' |
| 3077 | 'A dict comprehension, in contrast to list and set comprehensions,\n' |
| 3078 | 'needs two expressions separated with a colon followed by the ' |
| 3079 | 'usual\n' |
| 3080 | '"for" and "if" clauses. When the comprehension is run, the ' |
| 3081 | 'resulting\n' |
| 3082 | 'key and value elements are inserted in the new dictionary in the ' |
| 3083 | 'order\n' |
| 3084 | 'they are produced.\n' |
| 3085 | '\n' |
| 3086 | 'Restrictions on the types of the key values are listed earlier in\n' |
| 3087 | 'section The standard type hierarchy. (To summarize, the key type\n' |
| 3088 | 'should be *hashable*, which excludes all mutable objects.) ' |
| 3089 | 'Clashes\n' |
| 3090 | 'between duplicate keys are not detected; the last datum ' |
| 3091 | '(textually\n' |
| 3092 | 'rightmost in the display) stored for a given key value prevails.\n', |
| 3093 | 'dynamic-features': '\n' |
| 3094 | 'Interaction with dynamic features\n' |
| 3095 | '*********************************\n' |
| 3096 | '\n' |
| 3097 | 'There are several cases where Python statements are ' |
| 3098 | 'illegal when used\n' |
| 3099 | 'in conjunction with nested scopes that contain free ' |
| 3100 | 'variables.\n' |
| 3101 | '\n' |
| 3102 | 'If a variable is referenced in an enclosing scope, it ' |
| 3103 | 'is illegal to\n' |
| 3104 | 'delete the name. An error will be reported at compile ' |
| 3105 | 'time.\n' |
| 3106 | '\n' |
| 3107 | 'If the wild card form of import --- "import *" --- is ' |
| 3108 | 'used in a\n' |
| 3109 | 'function and the function contains or is a nested ' |
| 3110 | 'block with free\n' |
| 3111 | 'variables, the compiler will raise a "SyntaxError".\n' |
| 3112 | '\n' |
| 3113 | 'If "exec" is used in a function and the function ' |
| 3114 | 'contains or is a\n' |
| 3115 | 'nested block with free variables, the compiler will ' |
| 3116 | 'raise a\n' |
| 3117 | '"SyntaxError" unless the exec explicitly specifies the ' |
| 3118 | 'local namespace\n' |
| 3119 | 'for the "exec". (In other words, "exec obj" would be ' |
| 3120 | 'illegal, but\n' |
| 3121 | '"exec obj in ns" would be legal.)\n' |
| 3122 | '\n' |
| 3123 | 'The "eval()", "execfile()", and "input()" functions ' |
| 3124 | 'and the "exec"\n' |
| 3125 | 'statement do not have access to the full environment ' |
| 3126 | 'for resolving\n' |
| 3127 | 'names. Names may be resolved in the local and global ' |
| 3128 | 'namespaces of\n' |
| 3129 | 'the caller. Free variables are not resolved in the ' |
| 3130 | 'nearest enclosing\n' |
| 3131 | 'namespace, but in the global namespace. [1] The "exec" ' |
| 3132 | 'statement and\n' |
| 3133 | 'the "eval()" and "execfile()" functions have optional ' |
| 3134 | 'arguments to\n' |
| 3135 | 'override the global and local namespace. If only one ' |
| 3136 | 'namespace is\n' |
| 3137 | 'specified, it is used for both.\n', |
| 3138 | 'else': '\n' |
| 3139 | 'The "if" statement\n' |
| 3140 | '******************\n' |
| 3141 | '\n' |
| 3142 | 'The "if" statement is used for conditional execution:\n' |
| 3143 | '\n' |
| 3144 | ' if_stmt ::= "if" expression ":" suite\n' |
| 3145 | ' ( "elif" expression ":" suite )*\n' |
| 3146 | ' ["else" ":" suite]\n' |
| 3147 | '\n' |
| 3148 | 'It selects exactly one of the suites by evaluating the expressions ' |
| 3149 | 'one\n' |
| 3150 | 'by one until one is found to be true (see section Boolean ' |
| 3151 | 'operations\n' |
| 3152 | 'for the definition of true and false); then that suite is ' |
| 3153 | 'executed\n' |
| 3154 | '(and no other part of the "if" statement is executed or ' |
| 3155 | 'evaluated).\n' |
| 3156 | 'If all expressions are false, the suite of the "else" clause, if\n' |
| 3157 | 'present, is executed.\n', |
| 3158 | 'exceptions': '\n' |
| 3159 | 'Exceptions\n' |
| 3160 | '**********\n' |
| 3161 | '\n' |
| 3162 | 'Exceptions are a means of breaking out of the normal flow of ' |
| 3163 | 'control\n' |
| 3164 | 'of a code block in order to handle errors or other ' |
| 3165 | 'exceptional\n' |
| 3166 | 'conditions. An exception is *raised* at the point where the ' |
| 3167 | 'error is\n' |
| 3168 | 'detected; it may be *handled* by the surrounding code block ' |
| 3169 | 'or by any\n' |
| 3170 | 'code block that directly or indirectly invoked the code ' |
| 3171 | 'block where\n' |
| 3172 | 'the error occurred.\n' |
| 3173 | '\n' |
| 3174 | 'The Python interpreter raises an exception when it detects a ' |
| 3175 | 'run-time\n' |
| 3176 | 'error (such as division by zero). A Python program can ' |
| 3177 | 'also\n' |
| 3178 | 'explicitly raise an exception with the "raise" statement. ' |
| 3179 | 'Exception\n' |
| 3180 | 'handlers are specified with the "try" ... "except" ' |
| 3181 | 'statement. The\n' |
| 3182 | '"finally" clause of such a statement can be used to specify ' |
| 3183 | 'cleanup\n' |
| 3184 | 'code which does not handle the exception, but is executed ' |
| 3185 | 'whether an\n' |
| 3186 | 'exception occurred or not in the preceding code.\n' |
| 3187 | '\n' |
| 3188 | 'Python uses the "termination" model of error handling: an ' |
| 3189 | 'exception\n' |
| 3190 | 'handler can find out what happened and continue execution at ' |
| 3191 | 'an outer\n' |
| 3192 | 'level, but it cannot repair the cause of the error and retry ' |
| 3193 | 'the\n' |
| 3194 | 'failing operation (except by re-entering the offending piece ' |
| 3195 | 'of code\n' |
| 3196 | 'from the top).\n' |
| 3197 | '\n' |
| 3198 | 'When an exception is not handled at all, the interpreter ' |
| 3199 | 'terminates\n' |
| 3200 | 'execution of the program, or returns to its interactive main ' |
| 3201 | 'loop. In\n' |
| 3202 | 'either case, it prints a stack backtrace, except when the ' |
| 3203 | 'exception is\n' |
| 3204 | '"SystemExit".\n' |
| 3205 | '\n' |
| 3206 | 'Exceptions are identified by class instances. The "except" ' |
| 3207 | 'clause is\n' |
| 3208 | 'selected depending on the class of the instance: it must ' |
| 3209 | 'reference the\n' |
| 3210 | 'class of the instance or a base class thereof. The instance ' |
| 3211 | 'can be\n' |
| 3212 | 'received by the handler and can carry additional information ' |
| 3213 | 'about the\n' |
| 3214 | 'exceptional condition.\n' |
| 3215 | '\n' |
| 3216 | 'Exceptions can also be identified by strings, in which case ' |
| 3217 | 'the\n' |
| 3218 | '"except" clause is selected by object identity. An ' |
| 3219 | 'arbitrary value\n' |
| 3220 | 'can be raised along with the identifying string which can be ' |
| 3221 | 'passed to\n' |
| 3222 | 'the handler.\n' |
| 3223 | '\n' |
| 3224 | 'Note: Messages to exceptions are not part of the Python ' |
| 3225 | 'API. Their\n' |
| 3226 | ' contents may change from one version of Python to the next ' |
| 3227 | 'without\n' |
| 3228 | ' warning and should not be relied on by code which will run ' |
| 3229 | 'under\n' |
| 3230 | ' multiple versions of the interpreter.\n' |
| 3231 | '\n' |
| 3232 | 'See also the description of the "try" statement in section ' |
| 3233 | 'The try\n' |
| 3234 | 'statement and "raise" statement in section The raise ' |
| 3235 | 'statement.\n' |
| 3236 | '\n' |
| 3237 | '-[ Footnotes ]-\n' |
| 3238 | '\n' |
| 3239 | '[1] This limitation occurs because the code that is executed ' |
| 3240 | 'by\n' |
| 3241 | ' these operations is not available at the time the module ' |
| 3242 | 'is\n' |
| 3243 | ' compiled.\n', |
| 3244 | 'exec': '\n' |
| 3245 | 'The "exec" statement\n' |
| 3246 | '********************\n' |
| 3247 | '\n' |
| 3248 | ' exec_stmt ::= "exec" or_expr ["in" expression ["," ' |
| 3249 | 'expression]]\n' |
| 3250 | '\n' |
| 3251 | 'This statement supports dynamic execution of Python code. The ' |
| 3252 | 'first\n' |
| 3253 | 'expression should evaluate to either a Unicode string, a ' |
| 3254 | '*Latin-1*\n' |
| 3255 | 'encoded string, an open file object, a code object, or a tuple. ' |
| 3256 | 'If it\n' |
| 3257 | 'is a string, the string is parsed as a suite of Python statements\n' |
| 3258 | 'which is then executed (unless a syntax error occurs). [1] If it ' |
| 3259 | 'is an\n' |
| 3260 | 'open file, the file is parsed until EOF and executed. If it is a ' |
| 3261 | 'code\n' |
| 3262 | 'object, it is simply executed. For the interpretation of a tuple, ' |
| 3263 | 'see\n' |
| 3264 | "below. In all cases, the code that's executed is expected to be " |
| 3265 | 'valid\n' |
| 3266 | 'as file input (see section File input). Be aware that the ' |
| 3267 | '"return"\n' |
| 3268 | 'and "yield" statements may not be used outside of function ' |
| 3269 | 'definitions\n' |
| 3270 | 'even within the context of code passed to the "exec" statement.\n' |
| 3271 | '\n' |
| 3272 | 'In all cases, if the optional parts are omitted, the code is ' |
| 3273 | 'executed\n' |
| 3274 | 'in the current scope. If only the first expression after "in" is\n' |
| 3275 | 'specified, it should be a dictionary, which will be used for both ' |
| 3276 | 'the\n' |
| 3277 | 'global and the local variables. If two expressions are given, ' |
| 3278 | 'they\n' |
| 3279 | 'are used for the global and local variables, respectively. If\n' |
| 3280 | 'provided, *locals* can be any mapping object. Remember that at ' |
| 3281 | 'module\n' |
| 3282 | 'level, globals and locals are the same dictionary. If two ' |
| 3283 | 'separate\n' |
| 3284 | 'objects are given as *globals* and *locals*, the code will be ' |
| 3285 | 'executed\n' |
| 3286 | 'as if it were embedded in a class definition.\n' |
| 3287 | '\n' |
| 3288 | 'The first expression may also be a tuple of length 2 or 3. In ' |
| 3289 | 'this\n' |
| 3290 | 'case, the optional parts must be omitted. The form "exec(expr,\n' |
| 3291 | 'globals)" is equivalent to "exec expr in globals", while the form\n' |
| 3292 | '"exec(expr, globals, locals)" is equivalent to "exec expr in ' |
| 3293 | 'globals,\n' |
| 3294 | 'locals". The tuple form of "exec" provides compatibility with ' |
| 3295 | 'Python\n' |
| 3296 | '3, where "exec" is a function rather than a statement.\n' |
| 3297 | '\n' |
| 3298 | 'Changed in version 2.4: Formerly, *locals* was required to be a\n' |
| 3299 | 'dictionary.\n' |
| 3300 | '\n' |
| 3301 | 'As a side effect, an implementation may insert additional keys ' |
| 3302 | 'into\n' |
| 3303 | 'the dictionaries given besides those corresponding to variable ' |
| 3304 | 'names\n' |
| 3305 | 'set by the executed code. For example, the current implementation ' |
| 3306 | 'may\n' |
| 3307 | 'add a reference to the dictionary of the built-in module ' |
| 3308 | '"__builtin__"\n' |
| 3309 | 'under the key "__builtins__" (!).\n' |
| 3310 | '\n' |
| 3311 | "**Programmer's hints:** dynamic evaluation of expressions is " |
| 3312 | 'supported\n' |
| 3313 | 'by the built-in function "eval()". The built-in functions ' |
| 3314 | '"globals()"\n' |
| 3315 | 'and "locals()" return the current global and local dictionary,\n' |
| 3316 | 'respectively, which may be useful to pass around for use by ' |
| 3317 | '"exec".\n' |
| 3318 | '\n' |
| 3319 | '-[ Footnotes ]-\n' |
| 3320 | '\n' |
| 3321 | '[1] Note that the parser only accepts the Unix-style end of line\n' |
| 3322 | ' convention. If you are reading the code from a file, make sure ' |
| 3323 | 'to\n' |
| 3324 | ' use *universal newlines* mode to convert Windows or Mac-style\n' |
| 3325 | ' newlines.\n', |
| 3326 | 'execmodel': '\n' |
| 3327 | 'Execution model\n' |
| 3328 | '***************\n' |
| 3329 | '\n' |
| 3330 | '\n' |
| 3331 | 'Naming and binding\n' |
| 3332 | '==================\n' |
| 3333 | '\n' |
| 3334 | '*Names* refer to objects. Names are introduced by name ' |
| 3335 | 'binding\n' |
| 3336 | 'operations. Each occurrence of a name in the program text ' |
| 3337 | 'refers to\n' |
| 3338 | 'the *binding* of that name established in the innermost ' |
| 3339 | 'function block\n' |
| 3340 | 'containing the use.\n' |
| 3341 | '\n' |
| 3342 | 'A *block* is a piece of Python program text that is executed ' |
| 3343 | 'as a\n' |
| 3344 | 'unit. The following are blocks: a module, a function body, ' |
| 3345 | 'and a class\n' |
| 3346 | 'definition. Each command typed interactively is a block. A ' |
| 3347 | 'script\n' |
| 3348 | 'file (a file given as standard input to the interpreter or ' |
| 3349 | 'specified\n' |
| 3350 | 'on the interpreter command line the first argument) is a code ' |
| 3351 | 'block.\n' |
| 3352 | 'A script command (a command specified on the interpreter ' |
| 3353 | 'command line\n' |
| 3354 | "with the '**-c**' option) is a code block. The file read by " |
| 3355 | 'the\n' |
| 3356 | 'built-in function "execfile()" is a code block. The string ' |
| 3357 | 'argument\n' |
| 3358 | 'passed to the built-in function "eval()" and to the "exec" ' |
| 3359 | 'statement\n' |
| 3360 | 'is a code block. The expression read and evaluated by the ' |
| 3361 | 'built-in\n' |
| 3362 | 'function "input()" is a code block.\n' |
| 3363 | '\n' |
| 3364 | 'A code block is executed in an *execution frame*. A frame ' |
| 3365 | 'contains\n' |
| 3366 | 'some administrative information (used for debugging) and ' |
| 3367 | 'determines\n' |
| 3368 | "where and how execution continues after the code block's " |
| 3369 | 'execution has\n' |
| 3370 | 'completed.\n' |
| 3371 | '\n' |
| 3372 | 'A *scope* defines the visibility of a name within a block. ' |
| 3373 | 'If a local\n' |
| 3374 | 'variable is defined in a block, its scope includes that ' |
| 3375 | 'block. If the\n' |
| 3376 | 'definition occurs in a function block, the scope extends to ' |
| 3377 | 'any blocks\n' |
| 3378 | 'contained within the defining one, unless a contained block ' |
| 3379 | 'introduces\n' |
| 3380 | 'a different binding for the name. The scope of names defined ' |
| 3381 | 'in a\n' |
| 3382 | 'class block is limited to the class block; it does not extend ' |
| 3383 | 'to the\n' |
| 3384 | 'code blocks of methods -- this includes generator expressions ' |
| 3385 | 'since\n' |
| 3386 | 'they are implemented using a function scope. This means that ' |
| 3387 | 'the\n' |
| 3388 | 'following will fail:\n' |
| 3389 | '\n' |
| 3390 | ' class A:\n' |
| 3391 | ' a = 42\n' |
| 3392 | ' b = list(a + i for i in range(10))\n' |
| 3393 | '\n' |
| 3394 | 'When a name is used in a code block, it is resolved using the ' |
| 3395 | 'nearest\n' |
| 3396 | 'enclosing scope. The set of all such scopes visible to a ' |
| 3397 | 'code block\n' |
| 3398 | "is called the block's *environment*.\n" |
| 3399 | '\n' |
| 3400 | 'If a name is bound in a block, it is a local variable of that ' |
| 3401 | 'block.\n' |
| 3402 | 'If a name is bound at the module level, it is a global ' |
| 3403 | 'variable. (The\n' |
| 3404 | 'variables of the module code block are local and global.) If ' |
| 3405 | 'a\n' |
| 3406 | 'variable is used in a code block but not defined there, it is ' |
| 3407 | 'a *free\n' |
| 3408 | 'variable*.\n' |
| 3409 | '\n' |
| 3410 | 'When a name is not found at all, a "NameError" exception is ' |
| 3411 | 'raised.\n' |
| 3412 | 'If the name refers to a local variable that has not been ' |
| 3413 | 'bound, a\n' |
| 3414 | '"UnboundLocalError" exception is raised. "UnboundLocalError" ' |
| 3415 | 'is a\n' |
| 3416 | 'subclass of "NameError".\n' |
| 3417 | '\n' |
| 3418 | 'The following constructs bind names: formal parameters to ' |
| 3419 | 'functions,\n' |
| 3420 | '"import" statements, class and function definitions (these ' |
| 3421 | 'bind the\n' |
| 3422 | 'class or function name in the defining block), and targets ' |
| 3423 | 'that are\n' |
| 3424 | 'identifiers if occurring in an assignment, "for" loop header, ' |
| 3425 | 'in the\n' |
| 3426 | 'second position of an "except" clause header or after "as" in ' |
| 3427 | 'a "with"\n' |
| 3428 | 'statement. The "import" statement of the form "from ... ' |
| 3429 | 'import *"\n' |
| 3430 | 'binds all names defined in the imported module, except those ' |
| 3431 | 'beginning\n' |
| 3432 | 'with an underscore. This form may only be used at the module ' |
| 3433 | 'level.\n' |
| 3434 | '\n' |
| 3435 | 'A target occurring in a "del" statement is also considered ' |
| 3436 | 'bound for\n' |
| 3437 | 'this purpose (though the actual semantics are to unbind the ' |
| 3438 | 'name). It\n' |
| 3439 | 'is illegal to unbind a name that is referenced by an ' |
| 3440 | 'enclosing scope;\n' |
| 3441 | 'the compiler will report a "SyntaxError".\n' |
| 3442 | '\n' |
| 3443 | 'Each assignment or import statement occurs within a block ' |
| 3444 | 'defined by a\n' |
| 3445 | 'class or function definition or at the module level (the ' |
| 3446 | 'top-level\n' |
| 3447 | 'code block).\n' |
| 3448 | '\n' |
| 3449 | 'If a name binding operation occurs anywhere within a code ' |
| 3450 | 'block, all\n' |
| 3451 | 'uses of the name within the block are treated as references ' |
| 3452 | 'to the\n' |
| 3453 | 'current block. This can lead to errors when a name is used ' |
| 3454 | 'within a\n' |
| 3455 | 'block before it is bound. This rule is subtle. Python lacks\n' |
| 3456 | 'declarations and allows name binding operations to occur ' |
| 3457 | 'anywhere\n' |
| 3458 | 'within a code block. The local variables of a code block can ' |
| 3459 | 'be\n' |
| 3460 | 'determined by scanning the entire text of the block for name ' |
| 3461 | 'binding\n' |
| 3462 | 'operations.\n' |
| 3463 | '\n' |
| 3464 | 'If the global statement occurs within a block, all uses of ' |
| 3465 | 'the name\n' |
| 3466 | 'specified in the statement refer to the binding of that name ' |
| 3467 | 'in the\n' |
| 3468 | 'top-level namespace. Names are resolved in the top-level ' |
| 3469 | 'namespace by\n' |
| 3470 | 'searching the global namespace, i.e. the namespace of the ' |
| 3471 | 'module\n' |
| 3472 | 'containing the code block, and the builtins namespace, the ' |
| 3473 | 'namespace\n' |
| 3474 | 'of the module "__builtin__". The global namespace is ' |
| 3475 | 'searched first.\n' |
| 3476 | 'If the name is not found there, the builtins namespace is ' |
| 3477 | 'searched.\n' |
| 3478 | 'The global statement must precede all uses of the name.\n' |
| 3479 | '\n' |
| 3480 | 'The builtins namespace associated with the execution of a ' |
| 3481 | 'code block\n' |
| 3482 | 'is actually found by looking up the name "__builtins__" in ' |
| 3483 | 'its global\n' |
| 3484 | 'namespace; this should be a dictionary or a module (in the ' |
| 3485 | 'latter case\n' |
| 3486 | "the module's dictionary is used). By default, when in the " |
| 3487 | '"__main__"\n' |
| 3488 | 'module, "__builtins__" is the built-in module "__builtin__" ' |
| 3489 | '(note: no\n' |
| 3490 | '\'s\'); when in any other module, "__builtins__" is an alias ' |
| 3491 | 'for the\n' |
| 3492 | 'dictionary of the "__builtin__" module itself. ' |
| 3493 | '"__builtins__" can be\n' |
| 3494 | 'set to a user-created dictionary to create a weak form of ' |
| 3495 | 'restricted\n' |
| 3496 | 'execution.\n' |
| 3497 | '\n' |
| 3498 | '**CPython implementation detail:** Users should not touch\n' |
| 3499 | '"__builtins__"; it is strictly an implementation detail. ' |
| 3500 | 'Users\n' |
| 3501 | 'wanting to override values in the builtins namespace should ' |
| 3502 | '"import"\n' |
| 3503 | 'the "__builtin__" (no \'s\') module and modify its ' |
| 3504 | 'attributes\n' |
| 3505 | 'appropriately.\n' |
| 3506 | '\n' |
| 3507 | 'The namespace for a module is automatically created the first ' |
| 3508 | 'time a\n' |
| 3509 | 'module is imported. The main module for a script is always ' |
| 3510 | 'called\n' |
| 3511 | '"__main__".\n' |
| 3512 | '\n' |
| 3513 | 'The "global" statement has the same scope as a name binding ' |
| 3514 | 'operation\n' |
| 3515 | 'in the same block. If the nearest enclosing scope for a free ' |
| 3516 | 'variable\n' |
| 3517 | 'contains a global statement, the free variable is treated as ' |
| 3518 | 'a global.\n' |
| 3519 | '\n' |
| 3520 | 'A class definition is an executable statement that may use ' |
| 3521 | 'and define\n' |
| 3522 | 'names. These references follow the normal rules for name ' |
| 3523 | 'resolution.\n' |
| 3524 | 'The namespace of the class definition becomes the attribute ' |
| 3525 | 'dictionary\n' |
| 3526 | 'of the class. Names defined at the class scope are not ' |
| 3527 | 'visible in\n' |
| 3528 | 'methods.\n' |
| 3529 | '\n' |
| 3530 | '\n' |
| 3531 | 'Interaction with dynamic features\n' |
| 3532 | '---------------------------------\n' |
| 3533 | '\n' |
| 3534 | 'There are several cases where Python statements are illegal ' |
| 3535 | 'when used\n' |
| 3536 | 'in conjunction with nested scopes that contain free ' |
| 3537 | 'variables.\n' |
| 3538 | '\n' |
| 3539 | 'If a variable is referenced in an enclosing scope, it is ' |
| 3540 | 'illegal to\n' |
| 3541 | 'delete the name. An error will be reported at compile time.\n' |
| 3542 | '\n' |
| 3543 | 'If the wild card form of import --- "import *" --- is used in ' |
| 3544 | 'a\n' |
| 3545 | 'function and the function contains or is a nested block with ' |
| 3546 | 'free\n' |
| 3547 | 'variables, the compiler will raise a "SyntaxError".\n' |
| 3548 | '\n' |
| 3549 | 'If "exec" is used in a function and the function contains or ' |
| 3550 | 'is a\n' |
| 3551 | 'nested block with free variables, the compiler will raise a\n' |
| 3552 | '"SyntaxError" unless the exec explicitly specifies the local ' |
| 3553 | 'namespace\n' |
| 3554 | 'for the "exec". (In other words, "exec obj" would be ' |
| 3555 | 'illegal, but\n' |
| 3556 | '"exec obj in ns" would be legal.)\n' |
| 3557 | '\n' |
| 3558 | 'The "eval()", "execfile()", and "input()" functions and the ' |
| 3559 | '"exec"\n' |
| 3560 | 'statement do not have access to the full environment for ' |
| 3561 | 'resolving\n' |
| 3562 | 'names. Names may be resolved in the local and global ' |
| 3563 | 'namespaces of\n' |
| 3564 | 'the caller. Free variables are not resolved in the nearest ' |
| 3565 | 'enclosing\n' |
| 3566 | 'namespace, but in the global namespace. [1] The "exec" ' |
| 3567 | 'statement and\n' |
| 3568 | 'the "eval()" and "execfile()" functions have optional ' |
| 3569 | 'arguments to\n' |
| 3570 | 'override the global and local namespace. If only one ' |
| 3571 | 'namespace is\n' |
| 3572 | 'specified, it is used for both.\n' |
| 3573 | '\n' |
| 3574 | '\n' |
| 3575 | 'Exceptions\n' |
| 3576 | '==========\n' |
| 3577 | '\n' |
| 3578 | 'Exceptions are a means of breaking out of the normal flow of ' |
| 3579 | 'control\n' |
| 3580 | 'of a code block in order to handle errors or other ' |
| 3581 | 'exceptional\n' |
| 3582 | 'conditions. An exception is *raised* at the point where the ' |
| 3583 | 'error is\n' |
| 3584 | 'detected; it may be *handled* by the surrounding code block ' |
| 3585 | 'or by any\n' |
| 3586 | 'code block that directly or indirectly invoked the code block ' |
| 3587 | 'where\n' |
| 3588 | 'the error occurred.\n' |
| 3589 | '\n' |
| 3590 | 'The Python interpreter raises an exception when it detects a ' |
| 3591 | 'run-time\n' |
| 3592 | 'error (such as division by zero). A Python program can also\n' |
| 3593 | 'explicitly raise an exception with the "raise" statement. ' |
| 3594 | 'Exception\n' |
| 3595 | 'handlers are specified with the "try" ... "except" ' |
| 3596 | 'statement. The\n' |
| 3597 | '"finally" clause of such a statement can be used to specify ' |
| 3598 | 'cleanup\n' |
| 3599 | 'code which does not handle the exception, but is executed ' |
| 3600 | 'whether an\n' |
| 3601 | 'exception occurred or not in the preceding code.\n' |
| 3602 | '\n' |
| 3603 | 'Python uses the "termination" model of error handling: an ' |
| 3604 | 'exception\n' |
| 3605 | 'handler can find out what happened and continue execution at ' |
| 3606 | 'an outer\n' |
| 3607 | 'level, but it cannot repair the cause of the error and retry ' |
| 3608 | 'the\n' |
| 3609 | 'failing operation (except by re-entering the offending piece ' |
| 3610 | 'of code\n' |
| 3611 | 'from the top).\n' |
| 3612 | '\n' |
| 3613 | 'When an exception is not handled at all, the interpreter ' |
| 3614 | 'terminates\n' |
| 3615 | 'execution of the program, or returns to its interactive main ' |
| 3616 | 'loop. In\n' |
| 3617 | 'either case, it prints a stack backtrace, except when the ' |
| 3618 | 'exception is\n' |
| 3619 | '"SystemExit".\n' |
| 3620 | '\n' |
| 3621 | 'Exceptions are identified by class instances. The "except" ' |
| 3622 | 'clause is\n' |
| 3623 | 'selected depending on the class of the instance: it must ' |
| 3624 | 'reference the\n' |
| 3625 | 'class of the instance or a base class thereof. The instance ' |
| 3626 | 'can be\n' |
| 3627 | 'received by the handler and can carry additional information ' |
| 3628 | 'about the\n' |
| 3629 | 'exceptional condition.\n' |
| 3630 | '\n' |
| 3631 | 'Exceptions can also be identified by strings, in which case ' |
| 3632 | 'the\n' |
| 3633 | '"except" clause is selected by object identity. An arbitrary ' |
| 3634 | 'value\n' |
| 3635 | 'can be raised along with the identifying string which can be ' |
| 3636 | 'passed to\n' |
| 3637 | 'the handler.\n' |
| 3638 | '\n' |
| 3639 | 'Note: Messages to exceptions are not part of the Python API. ' |
| 3640 | 'Their\n' |
| 3641 | ' contents may change from one version of Python to the next ' |
| 3642 | 'without\n' |
| 3643 | ' warning and should not be relied on by code which will run ' |
| 3644 | 'under\n' |
| 3645 | ' multiple versions of the interpreter.\n' |
| 3646 | '\n' |
| 3647 | 'See also the description of the "try" statement in section ' |
| 3648 | 'The try\n' |
| 3649 | 'statement and "raise" statement in section The raise ' |
| 3650 | 'statement.\n' |
| 3651 | '\n' |
| 3652 | '-[ Footnotes ]-\n' |
| 3653 | '\n' |
| 3654 | '[1] This limitation occurs because the code that is executed ' |
| 3655 | 'by\n' |
| 3656 | ' these operations is not available at the time the module ' |
| 3657 | 'is\n' |
| 3658 | ' compiled.\n', |
| 3659 | 'exprlists': '\n' |
| 3660 | 'Expression lists\n' |
| 3661 | '****************\n' |
| 3662 | '\n' |
| 3663 | ' expression_list ::= expression ( "," expression )* [","]\n' |
| 3664 | '\n' |
| 3665 | 'An expression list containing at least one comma yields a ' |
| 3666 | 'tuple. The\n' |
| 3667 | 'length of the tuple is the number of expressions in the ' |
| 3668 | 'list. The\n' |
| 3669 | 'expressions are evaluated from left to right.\n' |
| 3670 | '\n' |
| 3671 | 'The trailing comma is required only to create a single tuple ' |
| 3672 | '(a.k.a. a\n' |
| 3673 | '*singleton*); it is optional in all other cases. A single ' |
| 3674 | 'expression\n' |
| 3675 | "without a trailing comma doesn't create a tuple, but rather " |
| 3676 | 'yields the\n' |
| 3677 | 'value of that expression. (To create an empty tuple, use an ' |
| 3678 | 'empty pair\n' |
| 3679 | 'of parentheses: "()".)\n', |
| 3680 | 'floating': '\n' |
| 3681 | 'Floating point literals\n' |
| 3682 | '***********************\n' |
| 3683 | '\n' |
| 3684 | 'Floating point literals are described by the following ' |
| 3685 | 'lexical\n' |
| 3686 | 'definitions:\n' |
| 3687 | '\n' |
| 3688 | ' floatnumber ::= pointfloat | exponentfloat\n' |
| 3689 | ' pointfloat ::= [intpart] fraction | intpart "."\n' |
| 3690 | ' exponentfloat ::= (intpart | pointfloat) exponent\n' |
| 3691 | ' intpart ::= digit+\n' |
| 3692 | ' fraction ::= "." digit+\n' |
| 3693 | ' exponent ::= ("e" | "E") ["+" | "-"] digit+\n' |
| 3694 | '\n' |
| 3695 | 'Note that the integer and exponent parts of floating point ' |
| 3696 | 'numbers can\n' |
| 3697 | 'look like octal integers, but are interpreted using radix 10. ' |
| 3698 | 'For\n' |
| 3699 | 'example, "077e010" is legal, and denotes the same number as ' |
| 3700 | '"77e10".\n' |
| 3701 | 'The allowed range of floating point literals is ' |
| 3702 | 'implementation-\n' |
| 3703 | 'dependent. Some examples of floating point literals:\n' |
| 3704 | '\n' |
| 3705 | ' 3.14 10. .001 1e100 3.14e-10 0e0\n' |
| 3706 | '\n' |
| 3707 | 'Note that numeric literals do not include a sign; a phrase ' |
| 3708 | 'like "-1"\n' |
| 3709 | 'is actually an expression composed of the unary operator "-" ' |
| 3710 | 'and the\n' |
| 3711 | 'literal "1".\n', |
| 3712 | 'for': '\n' |
| 3713 | 'The "for" statement\n' |
| 3714 | '*******************\n' |
| 3715 | '\n' |
| 3716 | 'The "for" statement is used to iterate over the elements of a ' |
| 3717 | 'sequence\n' |
| 3718 | '(such as a string, tuple or list) or other iterable object:\n' |
| 3719 | '\n' |
| 3720 | ' for_stmt ::= "for" target_list "in" expression_list ":" suite\n' |
| 3721 | ' ["else" ":" suite]\n' |
| 3722 | '\n' |
| 3723 | 'The expression list is evaluated once; it should yield an iterable\n' |
| 3724 | 'object. An iterator is created for the result of the\n' |
| 3725 | '"expression_list". The suite is then executed once for each item\n' |
| 3726 | 'provided by the iterator, in the order of ascending indices. Each\n' |
| 3727 | 'item in turn is assigned to the target list using the standard ' |
| 3728 | 'rules\n' |
| 3729 | 'for assignments, and then the suite is executed. When the items ' |
| 3730 | 'are\n' |
| 3731 | 'exhausted (which is immediately when the sequence is empty), the ' |
| 3732 | 'suite\n' |
| 3733 | 'in the "else" clause, if present, is executed, and the loop\n' |
| 3734 | 'terminates.\n' |
| 3735 | '\n' |
| 3736 | 'A "break" statement executed in the first suite terminates the ' |
| 3737 | 'loop\n' |
| 3738 | 'without executing the "else" clause\'s suite. A "continue" ' |
| 3739 | 'statement\n' |
| 3740 | 'executed in the first suite skips the rest of the suite and ' |
| 3741 | 'continues\n' |
| 3742 | 'with the next item, or with the "else" clause if there was no next\n' |
| 3743 | 'item.\n' |
| 3744 | '\n' |
| 3745 | 'The suite may assign to the variable(s) in the target list; this ' |
| 3746 | 'does\n' |
| 3747 | 'not affect the next item assigned to it.\n' |
| 3748 | '\n' |
| 3749 | 'The target list is not deleted when the loop is finished, but if ' |
| 3750 | 'the\n' |
| 3751 | 'sequence is empty, it will not have been assigned to at all by the\n' |
| 3752 | 'loop. Hint: the built-in function "range()" returns a sequence of\n' |
| 3753 | 'integers suitable to emulate the effect of Pascal\'s "for i := a to ' |
| 3754 | 'b\n' |
| 3755 | 'do"; e.g., "range(3)" returns the list "[0, 1, 2]".\n' |
| 3756 | '\n' |
| 3757 | 'Note: There is a subtlety when the sequence is being modified by ' |
| 3758 | 'the\n' |
| 3759 | ' loop (this can only occur for mutable sequences, i.e. lists). An\n' |
| 3760 | ' internal counter is used to keep track of which item is used ' |
| 3761 | 'next,\n' |
| 3762 | ' and this is incremented on each iteration. When this counter ' |
| 3763 | 'has\n' |
| 3764 | ' reached the length of the sequence the loop terminates. This ' |
| 3765 | 'means\n' |
| 3766 | ' that if the suite deletes the current (or a previous) item from ' |
| 3767 | 'the\n' |
| 3768 | ' sequence, the next item will be skipped (since it gets the index ' |
| 3769 | 'of\n' |
| 3770 | ' the current item which has already been treated). Likewise, if ' |
| 3771 | 'the\n' |
| 3772 | ' suite inserts an item in the sequence before the current item, ' |
| 3773 | 'the\n' |
| 3774 | ' current item will be treated again the next time through the ' |
| 3775 | 'loop.\n' |
| 3776 | ' This can lead to nasty bugs that can be avoided by making a\n' |
| 3777 | ' temporary copy using a slice of the whole sequence, e.g.,\n' |
| 3778 | '\n' |
| 3779 | ' for x in a[:]:\n' |
| 3780 | ' if x < 0: a.remove(x)\n', |
| 3781 | 'formatstrings': '\n' |
| 3782 | 'Format String Syntax\n' |
| 3783 | '********************\n' |
| 3784 | '\n' |
| 3785 | 'The "str.format()" method and the "Formatter" class share ' |
| 3786 | 'the same\n' |
| 3787 | 'syntax for format strings (although in the case of ' |
| 3788 | '"Formatter",\n' |
| 3789 | 'subclasses can define their own format string syntax).\n' |
| 3790 | '\n' |
| 3791 | 'Format strings contain "replacement fields" surrounded by ' |
| 3792 | 'curly braces\n' |
| 3793 | '"{}". Anything that is not contained in braces is ' |
| 3794 | 'considered literal\n' |
| 3795 | 'text, which is copied unchanged to the output. If you ' |
| 3796 | 'need to include\n' |
| 3797 | 'a brace character in the literal text, it can be escaped ' |
| 3798 | 'by doubling:\n' |
| 3799 | '"{{" and "}}".\n' |
| 3800 | '\n' |
| 3801 | 'The grammar for a replacement field is as follows:\n' |
| 3802 | '\n' |
| 3803 | ' replacement_field ::= "{" [field_name] ["!" ' |
| 3804 | 'conversion] [":" format_spec] "}"\n' |
| 3805 | ' field_name ::= arg_name ("." attribute_name ' |
| 3806 | '| "[" element_index "]")*\n' |
| 3807 | ' arg_name ::= [identifier | integer]\n' |
| 3808 | ' attribute_name ::= identifier\n' |
| 3809 | ' element_index ::= integer | index_string\n' |
| 3810 | ' index_string ::= <any source character except ' |
| 3811 | '"]"> +\n' |
| 3812 | ' conversion ::= "r" | "s"\n' |
| 3813 | ' format_spec ::= <described in the next ' |
| 3814 | 'section>\n' |
| 3815 | '\n' |
| 3816 | 'In less formal terms, the replacement field can start ' |
| 3817 | 'with a\n' |
| 3818 | '*field_name* that specifies the object whose value is to ' |
| 3819 | 'be formatted\n' |
| 3820 | 'and inserted into the output instead of the replacement ' |
| 3821 | 'field. The\n' |
| 3822 | '*field_name* is optionally followed by a *conversion* ' |
| 3823 | 'field, which is\n' |
| 3824 | 'preceded by an exclamation point "\'!\'", and a ' |
| 3825 | '*format_spec*, which is\n' |
| 3826 | 'preceded by a colon "\':\'". These specify a non-default ' |
| 3827 | 'format for the\n' |
| 3828 | 'replacement value.\n' |
| 3829 | '\n' |
| 3830 | 'See also the Format Specification Mini-Language section.\n' |
| 3831 | '\n' |
| 3832 | 'The *field_name* itself begins with an *arg_name* that is ' |
| 3833 | 'either a\n' |
| 3834 | "number or a keyword. If it's a number, it refers to a " |
| 3835 | 'positional\n' |
| 3836 | "argument, and if it's a keyword, it refers to a named " |
| 3837 | 'keyword\n' |
| 3838 | 'argument. If the numerical arg_names in a format string ' |
| 3839 | 'are 0, 1, 2,\n' |
| 3840 | '... in sequence, they can all be omitted (not just some) ' |
| 3841 | 'and the\n' |
| 3842 | 'numbers 0, 1, 2, ... will be automatically inserted in ' |
| 3843 | 'that order.\n' |
| 3844 | 'Because *arg_name* is not quote-delimited, it is not ' |
| 3845 | 'possible to\n' |
| 3846 | 'specify arbitrary dictionary keys (e.g., the strings ' |
| 3847 | '"\'10\'" or\n' |
| 3848 | '"\':-]\'") within a format string. The *arg_name* can be ' |
| 3849 | 'followed by any\n' |
| 3850 | 'number of index or attribute expressions. An expression ' |
| 3851 | 'of the form\n' |
| 3852 | '"\'.name\'" selects the named attribute using ' |
| 3853 | '"getattr()", while an\n' |
| 3854 | 'expression of the form "\'[index]\'" does an index lookup ' |
| 3855 | 'using\n' |
| 3856 | '"__getitem__()".\n' |
| 3857 | '\n' |
| 3858 | 'Changed in version 2.7: The positional argument ' |
| 3859 | 'specifiers can be\n' |
| 3860 | 'omitted, so "\'{} {}\'" is equivalent to "\'{0} {1}\'".\n' |
| 3861 | '\n' |
| 3862 | 'Some simple format string examples:\n' |
| 3863 | '\n' |
| 3864 | ' "First, thou shalt count to {0}" # References first ' |
| 3865 | 'positional argument\n' |
| 3866 | ' "Bring me a {}" # Implicitly ' |
| 3867 | 'references the first positional argument\n' |
| 3868 | ' "From {} to {}" # Same as "From {0} ' |
| 3869 | 'to {1}"\n' |
| 3870 | ' "My quest is {name}" # References keyword ' |
| 3871 | "argument 'name'\n" |
| 3872 | ' "Weight in tons {0.weight}" # \'weight\' ' |
| 3873 | 'attribute of first positional arg\n' |
| 3874 | ' "Units destroyed: {players[0]}" # First element of ' |
| 3875 | "keyword argument 'players'.\n" |
| 3876 | '\n' |
| 3877 | 'The *conversion* field causes a type coercion before ' |
| 3878 | 'formatting.\n' |
| 3879 | 'Normally, the job of formatting a value is done by the ' |
| 3880 | '"__format__()"\n' |
| 3881 | 'method of the value itself. However, in some cases it is ' |
| 3882 | 'desirable to\n' |
| 3883 | 'force a type to be formatted as a string, overriding its ' |
| 3884 | 'own\n' |
| 3885 | 'definition of formatting. By converting the value to a ' |
| 3886 | 'string before\n' |
| 3887 | 'calling "__format__()", the normal formatting logic is ' |
| 3888 | 'bypassed.\n' |
| 3889 | '\n' |
| 3890 | 'Two conversion flags are currently supported: "\'!s\'" ' |
| 3891 | 'which calls\n' |
| 3892 | '"str()" on the value, and "\'!r\'" which calls "repr()".\n' |
| 3893 | '\n' |
| 3894 | 'Some examples:\n' |
| 3895 | '\n' |
| 3896 | ' "Harold\'s a clever {0!s}" # Calls str() on the ' |
| 3897 | 'argument first\n' |
| 3898 | ' "Bring out the holy {name!r}" # Calls repr() on the ' |
| 3899 | 'argument first\n' |
| 3900 | '\n' |
| 3901 | 'The *format_spec* field contains a specification of how ' |
| 3902 | 'the value\n' |
| 3903 | 'should be presented, including such details as field ' |
| 3904 | 'width, alignment,\n' |
| 3905 | 'padding, decimal precision and so on. Each value type ' |
| 3906 | 'can define its\n' |
| 3907 | 'own "formatting mini-language" or interpretation of the ' |
| 3908 | '*format_spec*.\n' |
| 3909 | '\n' |
| 3910 | 'Most built-in types support a common formatting ' |
| 3911 | 'mini-language, which\n' |
| 3912 | 'is described in the next section.\n' |
| 3913 | '\n' |
| 3914 | 'A *format_spec* field can also include nested replacement ' |
| 3915 | 'fields\n' |
| 3916 | 'within it. These nested replacement fields can contain ' |
| 3917 | 'only a field\n' |
| 3918 | 'name; conversion flags and format specifications are not ' |
| 3919 | 'allowed. The\n' |
| 3920 | 'replacement fields within the format_spec are substituted ' |
| 3921 | 'before the\n' |
| 3922 | '*format_spec* string is interpreted. This allows the ' |
| 3923 | 'formatting of a\n' |
| 3924 | 'value to be dynamically specified.\n' |
| 3925 | '\n' |
| 3926 | 'See the Format examples section for some examples.\n' |
| 3927 | '\n' |
| 3928 | '\n' |
| 3929 | 'Format Specification Mini-Language\n' |
| 3930 | '==================================\n' |
| 3931 | '\n' |
| 3932 | '"Format specifications" are used within replacement ' |
| 3933 | 'fields contained\n' |
| 3934 | 'within a format string to define how individual values ' |
| 3935 | 'are presented\n' |
| 3936 | '(see Format String Syntax). They can also be passed ' |
| 3937 | 'directly to the\n' |
| 3938 | 'built-in "format()" function. Each formattable type may ' |
| 3939 | 'define how\n' |
| 3940 | 'the format specification is to be interpreted.\n' |
| 3941 | '\n' |
| 3942 | 'Most built-in types implement the following options for ' |
| 3943 | 'format\n' |
| 3944 | 'specifications, although some of the formatting options ' |
| 3945 | 'are only\n' |
| 3946 | 'supported by the numeric types.\n' |
| 3947 | '\n' |
| 3948 | 'A general convention is that an empty format string ' |
| 3949 | '("""") produces\n' |
| 3950 | 'the same result as if you had called "str()" on the ' |
| 3951 | 'value. A non-empty\n' |
| 3952 | 'format string typically modifies the result.\n' |
| 3953 | '\n' |
| 3954 | 'The general form of a *standard format specifier* is:\n' |
| 3955 | '\n' |
| 3956 | ' format_spec ::= ' |
| 3957 | '[[fill]align][sign][#][0][width][,][.precision][type]\n' |
| 3958 | ' fill ::= <any character>\n' |
| 3959 | ' align ::= "<" | ">" | "=" | "^"\n' |
| 3960 | ' sign ::= "+" | "-" | " "\n' |
| 3961 | ' width ::= integer\n' |
| 3962 | ' precision ::= integer\n' |
| 3963 | ' type ::= "b" | "c" | "d" | "e" | "E" | "f" | ' |
| 3964 | '"F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"\n' |
| 3965 | '\n' |
| 3966 | 'If a valid *align* value is specified, it can be preceded ' |
| 3967 | 'by a *fill*\n' |
| 3968 | 'character that can be any character and defaults to a ' |
| 3969 | 'space if\n' |
| 3970 | 'omitted. Note that it is not possible to use "{" and "}" ' |
| 3971 | 'as *fill*\n' |
| 3972 | 'char while using the "str.format()" method; this ' |
| 3973 | 'limitation however\n' |
| 3974 | 'doesn\'t affect the "format()" function.\n' |
| 3975 | '\n' |
| 3976 | 'The meaning of the various alignment options is as ' |
| 3977 | 'follows:\n' |
| 3978 | '\n' |
| 3979 | ' ' |
| 3980 | '+-----------+------------------------------------------------------------+\n' |
| 3981 | ' | Option | ' |
| 3982 | 'Meaning ' |
| 3983 | '|\n' |
| 3984 | ' ' |
| 3985 | '+===========+============================================================+\n' |
| 3986 | ' | "\'<\'" | Forces the field to be left-aligned ' |
| 3987 | 'within the available |\n' |
| 3988 | ' | | space (this is the default for most ' |
| 3989 | 'objects). |\n' |
| 3990 | ' ' |
| 3991 | '+-----------+------------------------------------------------------------+\n' |
| 3992 | ' | "\'>\'" | Forces the field to be right-aligned ' |
| 3993 | 'within the available |\n' |
| 3994 | ' | | space (this is the default for ' |
| 3995 | 'numbers). |\n' |
| 3996 | ' ' |
| 3997 | '+-----------+------------------------------------------------------------+\n' |
| 3998 | ' | "\'=\'" | Forces the padding to be placed after ' |
| 3999 | 'the sign (if any) |\n' |
| 4000 | ' | | but before the digits. This is used for ' |
| 4001 | 'printing fields |\n' |
| 4002 | " | | in the form '+000000120'. This alignment " |
| 4003 | 'option is only |\n' |
| 4004 | ' | | valid for numeric ' |
| 4005 | 'types. |\n' |
| 4006 | ' ' |
| 4007 | '+-----------+------------------------------------------------------------+\n' |
| 4008 | ' | "\'^\'" | Forces the field to be centered within ' |
| 4009 | 'the available |\n' |
| 4010 | ' | | ' |
| 4011 | 'space. ' |
| 4012 | '|\n' |
| 4013 | ' ' |
| 4014 | '+-----------+------------------------------------------------------------+\n' |
| 4015 | '\n' |
| 4016 | 'Note that unless a minimum field width is defined, the ' |
| 4017 | 'field width\n' |
| 4018 | 'will always be the same size as the data to fill it, so ' |
| 4019 | 'that the\n' |
| 4020 | 'alignment option has no meaning in this case.\n' |
| 4021 | '\n' |
| 4022 | 'The *sign* option is only valid for number types, and can ' |
| 4023 | 'be one of\n' |
| 4024 | 'the following:\n' |
| 4025 | '\n' |
| 4026 | ' ' |
| 4027 | '+-----------+------------------------------------------------------------+\n' |
| 4028 | ' | Option | ' |
| 4029 | 'Meaning ' |
| 4030 | '|\n' |
| 4031 | ' ' |
| 4032 | '+===========+============================================================+\n' |
| 4033 | ' | "\'+\'" | indicates that a sign should be used ' |
| 4034 | 'for both positive as |\n' |
| 4035 | ' | | well as negative ' |
| 4036 | 'numbers. |\n' |
| 4037 | ' ' |
| 4038 | '+-----------+------------------------------------------------------------+\n' |
| 4039 | ' | "\'-\'" | indicates that a sign should be used ' |
| 4040 | 'only for negative |\n' |
| 4041 | ' | | numbers (this is the default ' |
| 4042 | 'behavior). |\n' |
| 4043 | ' ' |
| 4044 | '+-----------+------------------------------------------------------------+\n' |
| 4045 | ' | space | indicates that a leading space should be ' |
| 4046 | 'used on positive |\n' |
| 4047 | ' | | numbers, and a minus sign on negative ' |
| 4048 | 'numbers. |\n' |
| 4049 | ' ' |
| 4050 | '+-----------+------------------------------------------------------------+\n' |
| 4051 | '\n' |
| 4052 | 'The "\'#\'" option is only valid for integers, and only ' |
| 4053 | 'for binary,\n' |
| 4054 | 'octal, or hexadecimal output. If present, it specifies ' |
| 4055 | 'that the\n' |
| 4056 | 'output will be prefixed by "\'0b\'", "\'0o\'", or ' |
| 4057 | '"\'0x\'", respectively.\n' |
| 4058 | '\n' |
| 4059 | 'The "\',\'" option signals the use of a comma for a ' |
| 4060 | 'thousands separator.\n' |
| 4061 | 'For a locale aware separator, use the "\'n\'" integer ' |
| 4062 | 'presentation type\n' |
| 4063 | 'instead.\n' |
| 4064 | '\n' |
| 4065 | 'Changed in version 2.7: Added the "\',\'" option (see ' |
| 4066 | 'also **PEP 378**).\n' |
| 4067 | '\n' |
| 4068 | '*width* is a decimal integer defining the minimum field ' |
| 4069 | 'width. If not\n' |
| 4070 | 'specified, then the field width will be determined by the ' |
| 4071 | 'content.\n' |
| 4072 | '\n' |
| 4073 | 'Preceding the *width* field by a zero ("\'0\'") character ' |
| 4074 | 'enables sign-\n' |
| 4075 | 'aware zero-padding for numeric types. This is equivalent ' |
| 4076 | 'to a *fill*\n' |
| 4077 | 'character of "\'0\'" with an *alignment* type of ' |
| 4078 | '"\'=\'".\n' |
| 4079 | '\n' |
| 4080 | 'The *precision* is a decimal number indicating how many ' |
| 4081 | 'digits should\n' |
| 4082 | 'be displayed after the decimal point for a floating point ' |
| 4083 | 'value\n' |
| 4084 | 'formatted with "\'f\'" and "\'F\'", or before and after ' |
| 4085 | 'the decimal point\n' |
| 4086 | 'for a floating point value formatted with "\'g\'" or ' |
| 4087 | '"\'G\'". For non-\n' |
| 4088 | 'number types the field indicates the maximum field size - ' |
| 4089 | 'in other\n' |
| 4090 | 'words, how many characters will be used from the field ' |
| 4091 | 'content. The\n' |
| 4092 | '*precision* is not allowed for integer values.\n' |
| 4093 | '\n' |
| 4094 | 'Finally, the *type* determines how the data should be ' |
| 4095 | 'presented.\n' |
| 4096 | '\n' |
| 4097 | 'The available string presentation types are:\n' |
| 4098 | '\n' |
| 4099 | ' ' |
| 4100 | '+-----------+------------------------------------------------------------+\n' |
| 4101 | ' | Type | ' |
| 4102 | 'Meaning ' |
| 4103 | '|\n' |
| 4104 | ' ' |
| 4105 | '+===========+============================================================+\n' |
| 4106 | ' | "\'s\'" | String format. This is the default ' |
| 4107 | 'type for strings and |\n' |
| 4108 | ' | | may be ' |
| 4109 | 'omitted. |\n' |
| 4110 | ' ' |
| 4111 | '+-----------+------------------------------------------------------------+\n' |
| 4112 | ' | None | The same as ' |
| 4113 | '"\'s\'". |\n' |
| 4114 | ' ' |
| 4115 | '+-----------+------------------------------------------------------------+\n' |
| 4116 | '\n' |
| 4117 | 'The available integer presentation types are:\n' |
| 4118 | '\n' |
| 4119 | ' ' |
| 4120 | '+-----------+------------------------------------------------------------+\n' |
| 4121 | ' | Type | ' |
| 4122 | 'Meaning ' |
| 4123 | '|\n' |
| 4124 | ' ' |
| 4125 | '+===========+============================================================+\n' |
| 4126 | ' | "\'b\'" | Binary format. Outputs the number in ' |
| 4127 | 'base 2. |\n' |
| 4128 | ' ' |
| 4129 | '+-----------+------------------------------------------------------------+\n' |
| 4130 | ' | "\'c\'" | Character. Converts the integer to the ' |
| 4131 | 'corresponding |\n' |
| 4132 | ' | | unicode character before ' |
| 4133 | 'printing. |\n' |
| 4134 | ' ' |
| 4135 | '+-----------+------------------------------------------------------------+\n' |
| 4136 | ' | "\'d\'" | Decimal Integer. Outputs the number in ' |
| 4137 | 'base 10. |\n' |
| 4138 | ' ' |
| 4139 | '+-----------+------------------------------------------------------------+\n' |
| 4140 | ' | "\'o\'" | Octal format. Outputs the number in ' |
| 4141 | 'base 8. |\n' |
| 4142 | ' ' |
| 4143 | '+-----------+------------------------------------------------------------+\n' |
| 4144 | ' | "\'x\'" | Hex format. Outputs the number in base ' |
| 4145 | '16, using lower- |\n' |
| 4146 | ' | | case letters for the digits above ' |
| 4147 | '9. |\n' |
| 4148 | ' ' |
| 4149 | '+-----------+------------------------------------------------------------+\n' |
| 4150 | ' | "\'X\'" | Hex format. Outputs the number in base ' |
| 4151 | '16, using upper- |\n' |
| 4152 | ' | | case letters for the digits above ' |
| 4153 | '9. |\n' |
| 4154 | ' ' |
| 4155 | '+-----------+------------------------------------------------------------+\n' |
| 4156 | ' | "\'n\'" | Number. This is the same as "\'d\'", ' |
| 4157 | 'except that it uses the |\n' |
| 4158 | ' | | current locale setting to insert the ' |
| 4159 | 'appropriate number |\n' |
| 4160 | ' | | separator ' |
| 4161 | 'characters. |\n' |
| 4162 | ' ' |
| 4163 | '+-----------+------------------------------------------------------------+\n' |
| 4164 | ' | None | The same as ' |
| 4165 | '"\'d\'". |\n' |
| 4166 | ' ' |
| 4167 | '+-----------+------------------------------------------------------------+\n' |
| 4168 | '\n' |
| 4169 | 'In addition to the above presentation types, integers can ' |
| 4170 | 'be formatted\n' |
| 4171 | 'with the floating point presentation types listed below ' |
| 4172 | '(except "\'n\'"\n' |
| 4173 | 'and None). When doing so, "float()" is used to convert ' |
| 4174 | 'the integer to\n' |
| 4175 | 'a floating point number before formatting.\n' |
| 4176 | '\n' |
| 4177 | 'The available presentation types for floating point and ' |
| 4178 | 'decimal values\n' |
| 4179 | 'are:\n' |
| 4180 | '\n' |
| 4181 | ' ' |
| 4182 | '+-----------+------------------------------------------------------------+\n' |
| 4183 | ' | Type | ' |
| 4184 | 'Meaning ' |
| 4185 | '|\n' |
| 4186 | ' ' |
| 4187 | '+===========+============================================================+\n' |
| 4188 | ' | "\'e\'" | Exponent notation. Prints the number ' |
| 4189 | 'in scientific |\n' |
| 4190 | " | | notation using the letter 'e' to " |
| 4191 | 'indicate the exponent. |\n' |
| 4192 | ' | | The default precision is ' |
| 4193 | '"6". |\n' |
| 4194 | ' ' |
| 4195 | '+-----------+------------------------------------------------------------+\n' |
| 4196 | ' | "\'E\'" | Exponent notation. Same as "\'e\'" ' |
| 4197 | 'except it uses an upper |\n' |
| 4198 | " | | case 'E' as the separator " |
| 4199 | 'character. |\n' |
| 4200 | ' ' |
| 4201 | '+-----------+------------------------------------------------------------+\n' |
| 4202 | ' | "\'f\'" | Fixed point. Displays the number as a ' |
| 4203 | 'fixed-point number. |\n' |
| 4204 | ' | | The default precision is ' |
| 4205 | '"6". |\n' |
| 4206 | ' ' |
| 4207 | '+-----------+------------------------------------------------------------+\n' |
| 4208 | ' | "\'F\'" | Fixed point. Same as ' |
| 4209 | '"\'f\'". |\n' |
| 4210 | ' ' |
| 4211 | '+-----------+------------------------------------------------------------+\n' |
| 4212 | ' | "\'g\'" | General format. For a given precision ' |
| 4213 | '"p >= 1", this |\n' |
| 4214 | ' | | rounds the number to "p" significant ' |
| 4215 | 'digits and then |\n' |
| 4216 | ' | | formats the result in either fixed-point ' |
| 4217 | 'format or in |\n' |
| 4218 | ' | | scientific notation, depending on its ' |
| 4219 | 'magnitude. The |\n' |
| 4220 | ' | | precise rules are as follows: suppose ' |
| 4221 | 'that the result |\n' |
| 4222 | ' | | formatted with presentation type "\'e\'" ' |
| 4223 | 'and precision "p-1" |\n' |
| 4224 | ' | | would have exponent "exp". Then if "-4 ' |
| 4225 | '<= exp < p", the |\n' |
| 4226 | ' | | number is formatted with presentation ' |
| 4227 | 'type "\'f\'" and |\n' |
| 4228 | ' | | precision "p-1-exp". Otherwise, the ' |
| 4229 | 'number is formatted |\n' |
| 4230 | ' | | with presentation type "\'e\'" and ' |
| 4231 | 'precision "p-1". In both |\n' |
| 4232 | ' | | cases insignificant trailing zeros are ' |
| 4233 | 'removed from the |\n' |
| 4234 | ' | | significand, and the decimal point is ' |
| 4235 | 'also removed if |\n' |
| 4236 | ' | | there are no remaining digits following ' |
| 4237 | 'it. Positive and |\n' |
| 4238 | ' | | negative infinity, positive and negative ' |
| 4239 | 'zero, and nans, |\n' |
| 4240 | ' | | are formatted as "inf", "-inf", "0", ' |
| 4241 | '"-0" and "nan" |\n' |
| 4242 | ' | | respectively, regardless of the ' |
| 4243 | 'precision. A precision of |\n' |
| 4244 | ' | | "0" is treated as equivalent to a ' |
| 4245 | 'precision of "1". The |\n' |
| 4246 | ' | | default precision is ' |
| 4247 | '"6". |\n' |
| 4248 | ' ' |
| 4249 | '+-----------+------------------------------------------------------------+\n' |
| 4250 | ' | "\'G\'" | General format. Same as "\'g\'" except ' |
| 4251 | 'switches to "\'E\'" if |\n' |
| 4252 | ' | | the number gets too large. The ' |
| 4253 | 'representations of infinity |\n' |
| 4254 | ' | | and NaN are uppercased, ' |
| 4255 | 'too. |\n' |
| 4256 | ' ' |
| 4257 | '+-----------+------------------------------------------------------------+\n' |
| 4258 | ' | "\'n\'" | Number. This is the same as "\'g\'", ' |
| 4259 | 'except that it uses the |\n' |
| 4260 | ' | | current locale setting to insert the ' |
| 4261 | 'appropriate number |\n' |
| 4262 | ' | | separator ' |
| 4263 | 'characters. |\n' |
| 4264 | ' ' |
| 4265 | '+-----------+------------------------------------------------------------+\n' |
| 4266 | ' | "\'%\'" | Percentage. Multiplies the number by ' |
| 4267 | '100 and displays in |\n' |
| 4268 | ' | | fixed ("\'f\'") format, followed by a ' |
| 4269 | 'percent sign. |\n' |
| 4270 | ' ' |
| 4271 | '+-----------+------------------------------------------------------------+\n' |
| 4272 | ' | None | The same as ' |
| 4273 | '"\'g\'". |\n' |
| 4274 | ' ' |
| 4275 | '+-----------+------------------------------------------------------------+\n' |
| 4276 | '\n' |
| 4277 | '\n' |
| 4278 | 'Format examples\n' |
| 4279 | '===============\n' |
| 4280 | '\n' |
| 4281 | 'This section contains examples of the new format syntax ' |
| 4282 | 'and comparison\n' |
| 4283 | 'with the old "%"-formatting.\n' |
| 4284 | '\n' |
| 4285 | 'In most of the cases the syntax is similar to the old ' |
| 4286 | '"%"-formatting,\n' |
| 4287 | 'with the addition of the "{}" and with ":" used instead ' |
| 4288 | 'of "%". For\n' |
| 4289 | 'example, "\'%03.2f\'" can be translated to ' |
| 4290 | '"\'{:03.2f}\'".\n' |
| 4291 | '\n' |
| 4292 | 'The new format syntax also supports new and different ' |
| 4293 | 'options, shown\n' |
| 4294 | 'in the follow examples.\n' |
| 4295 | '\n' |
| 4296 | 'Accessing arguments by position:\n' |
| 4297 | '\n' |
| 4298 | " >>> '{0}, {1}, {2}'.format('a', 'b', 'c')\n" |
| 4299 | " 'a, b, c'\n" |
| 4300 | " >>> '{}, {}, {}'.format('a', 'b', 'c') # 2.7+ only\n" |
| 4301 | " 'a, b, c'\n" |
| 4302 | " >>> '{2}, {1}, {0}'.format('a', 'b', 'c')\n" |
| 4303 | " 'c, b, a'\n" |
| 4304 | " >>> '{2}, {1}, {0}'.format(*'abc') # unpacking " |
| 4305 | 'argument sequence\n' |
| 4306 | " 'c, b, a'\n" |
| 4307 | " >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' " |
| 4308 | 'indices can be repeated\n' |
| 4309 | " 'abracadabra'\n" |
| 4310 | '\n' |
| 4311 | 'Accessing arguments by name:\n' |
| 4312 | '\n' |
| 4313 | " >>> 'Coordinates: {latitude}, " |
| 4314 | "{longitude}'.format(latitude='37.24N', " |
| 4315 | "longitude='-115.81W')\n" |
| 4316 | " 'Coordinates: 37.24N, -115.81W'\n" |
| 4317 | " >>> coord = {'latitude': '37.24N', 'longitude': " |
| 4318 | "'-115.81W'}\n" |
| 4319 | " >>> 'Coordinates: {latitude}, " |
| 4320 | "{longitude}'.format(**coord)\n" |
| 4321 | " 'Coordinates: 37.24N, -115.81W'\n" |
| 4322 | '\n' |
| 4323 | "Accessing arguments' attributes:\n" |
| 4324 | '\n' |
| 4325 | ' >>> c = 3-5j\n' |
| 4326 | " >>> ('The complex number {0} is formed from the real " |
| 4327 | "part {0.real} '\n" |
| 4328 | " ... 'and the imaginary part {0.imag}.').format(c)\n" |
| 4329 | " 'The complex number (3-5j) is formed from the real " |
| 4330 | "part 3.0 and the imaginary part -5.0.'\n" |
| 4331 | ' >>> class Point(object):\n' |
| 4332 | ' ... def __init__(self, x, y):\n' |
| 4333 | ' ... self.x, self.y = x, y\n' |
| 4334 | ' ... def __str__(self):\n' |
| 4335 | " ... return 'Point({self.x}, " |
| 4336 | "{self.y})'.format(self=self)\n" |
| 4337 | ' ...\n' |
| 4338 | ' >>> str(Point(4, 2))\n' |
| 4339 | " 'Point(4, 2)'\n" |
| 4340 | '\n' |
| 4341 | "Accessing arguments' items:\n" |
| 4342 | '\n' |
| 4343 | ' >>> coord = (3, 5)\n' |
| 4344 | " >>> 'X: {0[0]}; Y: {0[1]}'.format(coord)\n" |
| 4345 | " 'X: 3; Y: 5'\n" |
| 4346 | '\n' |
| 4347 | 'Replacing "%s" and "%r":\n' |
| 4348 | '\n' |
| 4349 | ' >>> "repr() shows quotes: {!r}; str() doesn\'t: ' |
| 4350 | '{!s}".format(\'test1\', \'test2\')\n' |
| 4351 | ' "repr() shows quotes: \'test1\'; str() doesn\'t: ' |
| 4352 | 'test2"\n' |
| 4353 | '\n' |
| 4354 | 'Aligning the text and specifying a width:\n' |
| 4355 | '\n' |
| 4356 | " >>> '{:<30}'.format('left aligned')\n" |
| 4357 | " 'left aligned '\n" |
| 4358 | " >>> '{:>30}'.format('right aligned')\n" |
| 4359 | " ' right aligned'\n" |
| 4360 | " >>> '{:^30}'.format('centered')\n" |
| 4361 | " ' centered '\n" |
| 4362 | " >>> '{:*^30}'.format('centered') # use '*' as a fill " |
| 4363 | 'char\n' |
| 4364 | " '***********centered***********'\n" |
| 4365 | '\n' |
| 4366 | 'Replacing "%+f", "%-f", and "% f" and specifying a sign:\n' |
| 4367 | '\n' |
| 4368 | " >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it " |
| 4369 | 'always\n' |
| 4370 | " '+3.140000; -3.140000'\n" |
| 4371 | " >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space " |
| 4372 | 'for positive numbers\n' |
| 4373 | " ' 3.140000; -3.140000'\n" |
| 4374 | " >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only " |
| 4375 | "the minus -- same as '{:f}; {:f}'\n" |
| 4376 | " '3.140000; -3.140000'\n" |
| 4377 | '\n' |
| 4378 | 'Replacing "%x" and "%o" and converting the value to ' |
| 4379 | 'different bases:\n' |
| 4380 | '\n' |
| 4381 | ' >>> # format also supports binary numbers\n' |
| 4382 | ' >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: ' |
| 4383 | '{0:b}".format(42)\n' |
| 4384 | " 'int: 42; hex: 2a; oct: 52; bin: 101010'\n" |
| 4385 | ' >>> # with 0x, 0o, or 0b as prefix:\n' |
| 4386 | ' >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: ' |
| 4387 | '{0:#b}".format(42)\n' |
| 4388 | " 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'\n" |
| 4389 | '\n' |
| 4390 | 'Using the comma as a thousands separator:\n' |
| 4391 | '\n' |
| 4392 | " >>> '{:,}'.format(1234567890)\n" |
| 4393 | " '1,234,567,890'\n" |
| 4394 | '\n' |
| 4395 | 'Expressing a percentage:\n' |
| 4396 | '\n' |
| 4397 | ' >>> points = 19.5\n' |
| 4398 | ' >>> total = 22\n' |
| 4399 | " >>> 'Correct answers: {:.2%}'.format(points/total)\n" |
| 4400 | " 'Correct answers: 88.64%'\n" |
| 4401 | '\n' |
| 4402 | 'Using type-specific formatting:\n' |
| 4403 | '\n' |
| 4404 | ' >>> import datetime\n' |
| 4405 | ' >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)\n' |
| 4406 | " >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)\n" |
| 4407 | " '2010-07-04 12:15:58'\n" |
| 4408 | '\n' |
| 4409 | 'Nesting arguments and more complex examples:\n' |
| 4410 | '\n' |
| 4411 | " >>> for align, text in zip('<^>', ['left', 'center', " |
| 4412 | "'right']):\n" |
| 4413 | " ... '{0:{fill}{align}16}'.format(text, fill=align, " |
| 4414 | 'align=align)\n' |
| 4415 | ' ...\n' |
| 4416 | " 'left<<<<<<<<<<<<'\n" |
| 4417 | " '^^^^^center^^^^^'\n" |
| 4418 | " '>>>>>>>>>>>right'\n" |
| 4419 | ' >>>\n' |
| 4420 | ' >>> octets = [192, 168, 0, 1]\n' |
| 4421 | " >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)\n" |
| 4422 | " 'C0A80001'\n" |
| 4423 | ' >>> int(_, 16)\n' |
| 4424 | ' 3232235521\n' |
| 4425 | ' >>>\n' |
| 4426 | ' >>> width = 5\n' |
| 4427 | ' >>> for num in range(5,12):\n' |
| 4428 | " ... for base in 'dXob':\n" |
| 4429 | " ... print '{0:{width}{base}}'.format(num, " |
| 4430 | 'base=base, width=width),\n' |
| 4431 | ' ... print\n' |
| 4432 | ' ...\n' |
| 4433 | ' 5 5 5 101\n' |
| 4434 | ' 6 6 6 110\n' |
| 4435 | ' 7 7 7 111\n' |
| 4436 | ' 8 8 10 1000\n' |
| 4437 | ' 9 9 11 1001\n' |
| 4438 | ' 10 A 12 1010\n' |
| 4439 | ' 11 B 13 1011\n', |
| 4440 | 'function': '\n' |
| 4441 | 'Function definitions\n' |
| 4442 | '********************\n' |
| 4443 | '\n' |
| 4444 | 'A function definition defines a user-defined function object ' |
| 4445 | '(see\n' |
| 4446 | 'section The standard type hierarchy):\n' |
| 4447 | '\n' |
| 4448 | ' decorated ::= decorators (classdef | funcdef)\n' |
| 4449 | ' decorators ::= decorator+\n' |
| 4450 | ' decorator ::= "@" dotted_name ["(" [argument_list ' |
| 4451 | '[","]] ")"] NEWLINE\n' |
| 4452 | ' funcdef ::= "def" funcname "(" [parameter_list] ")" ' |
| 4453 | '":" suite\n' |
| 4454 | ' dotted_name ::= identifier ("." identifier)*\n' |
| 4455 | ' parameter_list ::= (defparameter ",")*\n' |
| 4456 | ' ( "*" identifier ["," "**" identifier]\n' |
| 4457 | ' | "**" identifier\n' |
| 4458 | ' | defparameter [","] )\n' |
| 4459 | ' defparameter ::= parameter ["=" expression]\n' |
| 4460 | ' sublist ::= parameter ("," parameter)* [","]\n' |
| 4461 | ' parameter ::= identifier | "(" sublist ")"\n' |
| 4462 | ' funcname ::= identifier\n' |
| 4463 | '\n' |
| 4464 | 'A function definition is an executable statement. Its ' |
| 4465 | 'execution binds\n' |
| 4466 | 'the function name in the current local namespace to a function ' |
| 4467 | 'object\n' |
| 4468 | '(a wrapper around the executable code for the function). ' |
| 4469 | 'This\n' |
| 4470 | 'function object contains a reference to the current global ' |
| 4471 | 'namespace\n' |
| 4472 | 'as the global namespace to be used when the function is ' |
| 4473 | 'called.\n' |
| 4474 | '\n' |
| 4475 | 'The function definition does not execute the function body; ' |
| 4476 | 'this gets\n' |
| 4477 | 'executed only when the function is called. [3]\n' |
| 4478 | '\n' |
| 4479 | 'A function definition may be wrapped by one or more ' |
| 4480 | '*decorator*\n' |
| 4481 | 'expressions. Decorator expressions are evaluated when the ' |
| 4482 | 'function is\n' |
| 4483 | 'defined, in the scope that contains the function definition. ' |
| 4484 | 'The\n' |
| 4485 | 'result must be a callable, which is invoked with the function ' |
| 4486 | 'object\n' |
| 4487 | 'as the only argument. The returned value is bound to the ' |
| 4488 | 'function name\n' |
| 4489 | 'instead of the function object. Multiple decorators are ' |
| 4490 | 'applied in\n' |
| 4491 | 'nested fashion. For example, the following code:\n' |
| 4492 | '\n' |
| 4493 | ' @f1(arg)\n' |
| 4494 | ' @f2\n' |
| 4495 | ' def func(): pass\n' |
| 4496 | '\n' |
| 4497 | 'is equivalent to:\n' |
| 4498 | '\n' |
| 4499 | ' def func(): pass\n' |
| 4500 | ' func = f1(arg)(f2(func))\n' |
| 4501 | '\n' |
| 4502 | 'When one or more top-level *parameters* have the form ' |
| 4503 | '*parameter* "="\n' |
| 4504 | '*expression*, the function is said to have "default parameter ' |
| 4505 | 'values."\n' |
| 4506 | 'For a parameter with a default value, the corresponding ' |
| 4507 | '*argument* may\n' |
| 4508 | "be omitted from a call, in which case the parameter's default " |
| 4509 | 'value is\n' |
| 4510 | 'substituted. If a parameter has a default value, all ' |
| 4511 | 'following\n' |
| 4512 | 'parameters must also have a default value --- this is a ' |
| 4513 | 'syntactic\n' |
| 4514 | 'restriction that is not expressed by the grammar.\n' |
| 4515 | '\n' |
| 4516 | '**Default parameter values are evaluated when the function ' |
| 4517 | 'definition\n' |
| 4518 | 'is executed.** This means that the expression is evaluated ' |
| 4519 | 'once, when\n' |
| 4520 | 'the function is defined, and that the same "pre-computed" ' |
| 4521 | 'value is\n' |
| 4522 | 'used for each call. This is especially important to ' |
| 4523 | 'understand when a\n' |
| 4524 | 'default parameter is a mutable object, such as a list or a ' |
| 4525 | 'dictionary:\n' |
| 4526 | 'if the function modifies the object (e.g. by appending an item ' |
| 4527 | 'to a\n' |
| 4528 | 'list), the default value is in effect modified. This is ' |
| 4529 | 'generally not\n' |
| 4530 | 'what was intended. A way around this is to use "None" as ' |
| 4531 | 'the\n' |
| 4532 | 'default, and explicitly test for it in the body of the ' |
| 4533 | 'function, e.g.:\n' |
| 4534 | '\n' |
| 4535 | ' def whats_on_the_telly(penguin=None):\n' |
| 4536 | ' if penguin is None:\n' |
| 4537 | ' penguin = []\n' |
| 4538 | ' penguin.append("property of the zoo")\n' |
| 4539 | ' return penguin\n' |
| 4540 | '\n' |
| 4541 | 'Function call semantics are described in more detail in ' |
| 4542 | 'section Calls.\n' |
| 4543 | 'A function call always assigns values to all parameters ' |
| 4544 | 'mentioned in\n' |
| 4545 | 'the parameter list, either from position arguments, from ' |
| 4546 | 'keyword\n' |
| 4547 | 'arguments, or from default values. If the form ' |
| 4548 | '""*identifier"" is\n' |
| 4549 | 'present, it is initialized to a tuple receiving any excess ' |
| 4550 | 'positional\n' |
| 4551 | 'parameters, defaulting to the empty tuple. If the form\n' |
| 4552 | '""**identifier"" is present, it is initialized to a new ' |
| 4553 | 'dictionary\n' |
| 4554 | 'receiving any excess keyword arguments, defaulting to a new ' |
| 4555 | 'empty\n' |
| 4556 | 'dictionary.\n' |
| 4557 | '\n' |
| 4558 | 'It is also possible to create anonymous functions (functions ' |
| 4559 | 'not bound\n' |
| 4560 | 'to a name), for immediate use in expressions. This uses ' |
| 4561 | 'lambda\n' |
| 4562 | 'expressions, described in section Lambdas. Note that the ' |
| 4563 | 'lambda\n' |
| 4564 | 'expression is merely a shorthand for a simplified function ' |
| 4565 | 'definition;\n' |
| 4566 | 'a function defined in a ""def"" statement can be passed around ' |
| 4567 | 'or\n' |
| 4568 | 'assigned to another name just like a function defined by a ' |
| 4569 | 'lambda\n' |
| 4570 | 'expression. The ""def"" form is actually more powerful since ' |
| 4571 | 'it\n' |
| 4572 | 'allows the execution of multiple statements.\n' |
| 4573 | '\n' |
| 4574 | "**Programmer's note:** Functions are first-class objects. A " |
| 4575 | '""def""\n' |
| 4576 | 'form executed inside a function definition defines a local ' |
| 4577 | 'function\n' |
| 4578 | 'that can be returned or passed around. Free variables used in ' |
| 4579 | 'the\n' |
| 4580 | 'nested function can access the local variables of the ' |
| 4581 | 'function\n' |
| 4582 | 'containing the def. See section Naming and binding for ' |
| 4583 | 'details.\n', |
| 4584 | 'global': '\n' |
| 4585 | 'The "global" statement\n' |
| 4586 | '**********************\n' |
| 4587 | '\n' |
| 4588 | ' global_stmt ::= "global" identifier ("," identifier)*\n' |
| 4589 | '\n' |
| 4590 | 'The "global" statement is a declaration which holds for the ' |
| 4591 | 'entire\n' |
| 4592 | 'current code block. It means that the listed identifiers are to ' |
| 4593 | 'be\n' |
| 4594 | 'interpreted as globals. It would be impossible to assign to a ' |
| 4595 | 'global\n' |
| 4596 | 'variable without "global", although free variables may refer to\n' |
| 4597 | 'globals without being declared global.\n' |
| 4598 | '\n' |
| 4599 | 'Names listed in a "global" statement must not be used in the ' |
| 4600 | 'same code\n' |
| 4601 | 'block textually preceding that "global" statement.\n' |
| 4602 | '\n' |
| 4603 | 'Names listed in a "global" statement must not be defined as ' |
| 4604 | 'formal\n' |
| 4605 | 'parameters or in a "for" loop control target, "class" ' |
| 4606 | 'definition,\n' |
| 4607 | 'function definition, or "import" statement.\n' |
| 4608 | '\n' |
| 4609 | '**CPython implementation detail:** The current implementation ' |
| 4610 | 'does not\n' |
| 4611 | 'enforce the latter two restrictions, but programs should not ' |
| 4612 | 'abuse\n' |
| 4613 | 'this freedom, as future implementations may enforce them or ' |
| 4614 | 'silently\n' |
| 4615 | 'change the meaning of the program.\n' |
| 4616 | '\n' |
| 4617 | '**Programmer\'s note:** the "global" is a directive to the ' |
| 4618 | 'parser. It\n' |
| 4619 | 'applies only to code parsed at the same time as the "global"\n' |
| 4620 | 'statement. In particular, a "global" statement contained in an ' |
| 4621 | '"exec"\n' |
| 4622 | 'statement does not affect the code block *containing* the ' |
| 4623 | '"exec"\n' |
| 4624 | 'statement, and code contained in an "exec" statement is ' |
| 4625 | 'unaffected by\n' |
| 4626 | '"global" statements in the code containing the "exec" ' |
| 4627 | 'statement. The\n' |
| 4628 | 'same applies to the "eval()", "execfile()" and "compile()" ' |
| 4629 | 'functions.\n', |
| 4630 | 'id-classes': '\n' |
| 4631 | 'Reserved classes of identifiers\n' |
| 4632 | '*******************************\n' |
| 4633 | '\n' |
| 4634 | 'Certain classes of identifiers (besides keywords) have ' |
| 4635 | 'special\n' |
| 4636 | 'meanings. These classes are identified by the patterns of ' |
| 4637 | 'leading and\n' |
| 4638 | 'trailing underscore characters:\n' |
| 4639 | '\n' |
| 4640 | '"_*"\n' |
| 4641 | ' Not imported by "from module import *". The special ' |
| 4642 | 'identifier "_"\n' |
| 4643 | ' is used in the interactive interpreter to store the ' |
| 4644 | 'result of the\n' |
| 4645 | ' last evaluation; it is stored in the "__builtin__" ' |
| 4646 | 'module. When\n' |
| 4647 | ' not in interactive mode, "_" has no special meaning and ' |
| 4648 | 'is not\n' |
| 4649 | ' defined. See section The import statement.\n' |
| 4650 | '\n' |
| 4651 | ' Note: The name "_" is often used in conjunction with\n' |
| 4652 | ' internationalization; refer to the documentation for ' |
| 4653 | 'the\n' |
| 4654 | ' "gettext" module for more information on this ' |
| 4655 | 'convention.\n' |
| 4656 | '\n' |
| 4657 | '"__*__"\n' |
| 4658 | ' System-defined names. These names are defined by the ' |
| 4659 | 'interpreter\n' |
| 4660 | ' and its implementation (including the standard library). ' |
| 4661 | 'Current\n' |
| 4662 | ' system names are discussed in the Special method names ' |
| 4663 | 'section and\n' |
| 4664 | ' elsewhere. More will likely be defined in future ' |
| 4665 | 'versions of\n' |
| 4666 | ' Python. *Any* use of "__*__" names, in any context, that ' |
| 4667 | 'does not\n' |
| 4668 | ' follow explicitly documented use, is subject to breakage ' |
| 4669 | 'without\n' |
| 4670 | ' warning.\n' |
| 4671 | '\n' |
| 4672 | '"__*"\n' |
| 4673 | ' Class-private names. Names in this category, when used ' |
| 4674 | 'within the\n' |
| 4675 | ' context of a class definition, are re-written to use a ' |
| 4676 | 'mangled form\n' |
| 4677 | ' to help avoid name clashes between "private" attributes ' |
| 4678 | 'of base and\n' |
| 4679 | ' derived classes. See section Identifiers (Names).\n', |
| 4680 | 'identifiers': '\n' |
| 4681 | 'Identifiers and keywords\n' |
| 4682 | '************************\n' |
| 4683 | '\n' |
| 4684 | 'Identifiers (also referred to as *names*) are described by ' |
| 4685 | 'the\n' |
| 4686 | 'following lexical definitions:\n' |
| 4687 | '\n' |
| 4688 | ' identifier ::= (letter|"_") (letter | digit | "_")*\n' |
| 4689 | ' letter ::= lowercase | uppercase\n' |
| 4690 | ' lowercase ::= "a"..."z"\n' |
| 4691 | ' uppercase ::= "A"..."Z"\n' |
| 4692 | ' digit ::= "0"..."9"\n' |
| 4693 | '\n' |
| 4694 | 'Identifiers are unlimited in length. Case is significant.\n' |
| 4695 | '\n' |
| 4696 | '\n' |
| 4697 | 'Keywords\n' |
| 4698 | '========\n' |
| 4699 | '\n' |
| 4700 | 'The following identifiers are used as reserved words, or ' |
| 4701 | '*keywords* of\n' |
| 4702 | 'the language, and cannot be used as ordinary identifiers. ' |
| 4703 | 'They must\n' |
| 4704 | 'be spelled exactly as written here:\n' |
| 4705 | '\n' |
| 4706 | ' and del from not while\n' |
| 4707 | ' as elif global or with\n' |
| 4708 | ' assert else if pass yield\n' |
| 4709 | ' break except import print\n' |
| 4710 | ' class exec in raise\n' |
| 4711 | ' continue finally is return\n' |
| 4712 | ' def for lambda try\n' |
| 4713 | '\n' |
| 4714 | 'Changed in version 2.4: "None" became a constant and is now ' |
| 4715 | 'recognized\n' |
| 4716 | 'by the compiler as a name for the built-in object "None". ' |
| 4717 | 'Although it\n' |
| 4718 | 'is not a keyword, you cannot assign a different object to ' |
| 4719 | 'it.\n' |
| 4720 | '\n' |
| 4721 | 'Changed in version 2.5: Using "as" and "with" as ' |
| 4722 | 'identifiers triggers\n' |
| 4723 | 'a warning. To use them as keywords, enable the ' |
| 4724 | '"with_statement"\n' |
| 4725 | 'future feature .\n' |
| 4726 | '\n' |
| 4727 | 'Changed in version 2.6: "as" and "with" are full keywords.\n' |
| 4728 | '\n' |
| 4729 | '\n' |
| 4730 | 'Reserved classes of identifiers\n' |
| 4731 | '===============================\n' |
| 4732 | '\n' |
| 4733 | 'Certain classes of identifiers (besides keywords) have ' |
| 4734 | 'special\n' |
| 4735 | 'meanings. These classes are identified by the patterns of ' |
| 4736 | 'leading and\n' |
| 4737 | 'trailing underscore characters:\n' |
| 4738 | '\n' |
| 4739 | '"_*"\n' |
| 4740 | ' Not imported by "from module import *". The special ' |
| 4741 | 'identifier "_"\n' |
| 4742 | ' is used in the interactive interpreter to store the ' |
| 4743 | 'result of the\n' |
| 4744 | ' last evaluation; it is stored in the "__builtin__" ' |
| 4745 | 'module. When\n' |
| 4746 | ' not in interactive mode, "_" has no special meaning and ' |
| 4747 | 'is not\n' |
| 4748 | ' defined. See section The import statement.\n' |
| 4749 | '\n' |
| 4750 | ' Note: The name "_" is often used in conjunction with\n' |
| 4751 | ' internationalization; refer to the documentation for ' |
| 4752 | 'the\n' |
| 4753 | ' "gettext" module for more information on this ' |
| 4754 | 'convention.\n' |
| 4755 | '\n' |
| 4756 | '"__*__"\n' |
| 4757 | ' System-defined names. These names are defined by the ' |
| 4758 | 'interpreter\n' |
| 4759 | ' and its implementation (including the standard ' |
| 4760 | 'library). Current\n' |
| 4761 | ' system names are discussed in the Special method names ' |
| 4762 | 'section and\n' |
| 4763 | ' elsewhere. More will likely be defined in future ' |
| 4764 | 'versions of\n' |
| 4765 | ' Python. *Any* use of "__*__" names, in any context, ' |
| 4766 | 'that does not\n' |
| 4767 | ' follow explicitly documented use, is subject to breakage ' |
| 4768 | 'without\n' |
| 4769 | ' warning.\n' |
| 4770 | '\n' |
| 4771 | '"__*"\n' |
| 4772 | ' Class-private names. Names in this category, when used ' |
| 4773 | 'within the\n' |
| 4774 | ' context of a class definition, are re-written to use a ' |
| 4775 | 'mangled form\n' |
| 4776 | ' to help avoid name clashes between "private" attributes ' |
| 4777 | 'of base and\n' |
| 4778 | ' derived classes. See section Identifiers (Names).\n', |
| 4779 | 'if': '\n' |
| 4780 | 'The "if" statement\n' |
| 4781 | '******************\n' |
| 4782 | '\n' |
| 4783 | 'The "if" statement is used for conditional execution:\n' |
| 4784 | '\n' |
| 4785 | ' if_stmt ::= "if" expression ":" suite\n' |
| 4786 | ' ( "elif" expression ":" suite )*\n' |
| 4787 | ' ["else" ":" suite]\n' |
| 4788 | '\n' |
| 4789 | 'It selects exactly one of the suites by evaluating the expressions ' |
| 4790 | 'one\n' |
| 4791 | 'by one until one is found to be true (see section Boolean ' |
| 4792 | 'operations\n' |
| 4793 | 'for the definition of true and false); then that suite is executed\n' |
| 4794 | '(and no other part of the "if" statement is executed or evaluated).\n' |
| 4795 | 'If all expressions are false, the suite of the "else" clause, if\n' |
| 4796 | 'present, is executed.\n', |
| 4797 | 'imaginary': '\n' |
| 4798 | 'Imaginary literals\n' |
| 4799 | '******************\n' |
| 4800 | '\n' |
| 4801 | 'Imaginary literals are described by the following lexical ' |
| 4802 | 'definitions:\n' |
| 4803 | '\n' |
| 4804 | ' imagnumber ::= (floatnumber | intpart) ("j" | "J")\n' |
| 4805 | '\n' |
| 4806 | 'An imaginary literal yields a complex number with a real part ' |
| 4807 | 'of 0.0.\n' |
| 4808 | 'Complex numbers are represented as a pair of floating point ' |
| 4809 | 'numbers\n' |
| 4810 | 'and have the same restrictions on their range. To create a ' |
| 4811 | 'complex\n' |
| 4812 | 'number with a nonzero real part, add a floating point number ' |
| 4813 | 'to it,\n' |
| 4814 | 'e.g., "(3+4j)". Some examples of imaginary literals:\n' |
| 4815 | '\n' |
| 4816 | ' 3.14j 10.j 10j .001j 1e100j 3.14e-10j\n', |
| 4817 | 'import': '\n' |
| 4818 | 'The "import" statement\n' |
| 4819 | '**********************\n' |
| 4820 | '\n' |
| 4821 | ' import_stmt ::= "import" module ["as" name] ( "," module ' |
| 4822 | '["as" name] )*\n' |
| 4823 | ' | "from" relative_module "import" identifier ' |
| 4824 | '["as" name]\n' |
| 4825 | ' ( "," identifier ["as" name] )*\n' |
| 4826 | ' | "from" relative_module "import" "(" ' |
| 4827 | 'identifier ["as" name]\n' |
| 4828 | ' ( "," identifier ["as" name] )* [","] ")"\n' |
| 4829 | ' | "from" module "import" "*"\n' |
| 4830 | ' module ::= (identifier ".")* identifier\n' |
| 4831 | ' relative_module ::= "."* module | "."+\n' |
| 4832 | ' name ::= identifier\n' |
| 4833 | '\n' |
| 4834 | 'Import statements are executed in two steps: (1) find a module, ' |
| 4835 | 'and\n' |
| 4836 | 'initialize it if necessary; (2) define a name or names in the ' |
| 4837 | 'local\n' |
| 4838 | 'namespace (of the scope where the "import" statement occurs). ' |
| 4839 | 'The\n' |
| 4840 | 'statement comes in two forms differing on whether it uses the ' |
| 4841 | '"from"\n' |
| 4842 | 'keyword. The first form (without "from") repeats these steps for ' |
| 4843 | 'each\n' |
| 4844 | 'identifier in the list. The form with "from" performs step (1) ' |
| 4845 | 'once,\n' |
| 4846 | 'and then performs step (2) repeatedly.\n' |
| 4847 | '\n' |
| 4848 | 'To understand how step (1) occurs, one must first understand ' |
| 4849 | 'how\n' |
| 4850 | 'Python handles hierarchical naming of modules. To help organize\n' |
| 4851 | 'modules and provide a hierarchy in naming, Python has a concept ' |
| 4852 | 'of\n' |
| 4853 | 'packages. A package can contain other packages and modules ' |
| 4854 | 'while\n' |
| 4855 | 'modules cannot contain other modules or packages. From a file ' |
| 4856 | 'system\n' |
| 4857 | 'perspective, packages are directories and modules are files.\n' |
| 4858 | '\n' |
| 4859 | 'Once the name of the module is known (unless otherwise ' |
| 4860 | 'specified, the\n' |
| 4861 | 'term "module" will refer to both packages and modules), ' |
| 4862 | 'searching for\n' |
| 4863 | 'the module or package can begin. The first place checked is\n' |
| 4864 | '"sys.modules", the cache of all modules that have been imported\n' |
| 4865 | 'previously. If the module is found there then it is used in step ' |
| 4866 | '(2)\n' |
| 4867 | 'of import.\n' |
| 4868 | '\n' |
| 4869 | 'If the module is not found in the cache, then "sys.meta_path" ' |
| 4870 | 'is\n' |
| 4871 | 'searched (the specification for "sys.meta_path" can be found in ' |
| 4872 | '**PEP\n' |
| 4873 | '302**). The object is a list of *finder* objects which are ' |
| 4874 | 'queried in\n' |
| 4875 | 'order as to whether they know how to load the module by calling ' |
| 4876 | 'their\n' |
| 4877 | '"find_module()" method with the name of the module. If the ' |
| 4878 | 'module\n' |
| 4879 | 'happens to be contained within a package (as denoted by the ' |
| 4880 | 'existence\n' |
| 4881 | 'of a dot in the name), then a second argument to "find_module()" ' |
| 4882 | 'is\n' |
| 4883 | 'given as the value of the "__path__" attribute from the parent ' |
| 4884 | 'package\n' |
| 4885 | '(everything up to the last dot in the name of the module being\n' |
| 4886 | 'imported). If a finder can find the module it returns a ' |
| 4887 | '*loader*\n' |
| 4888 | '(discussed later) or returns "None".\n' |
| 4889 | '\n' |
| 4890 | 'If none of the finders on "sys.meta_path" are able to find the ' |
| 4891 | 'module\n' |
| 4892 | 'then some implicitly defined finders are queried. ' |
| 4893 | 'Implementations of\n' |
| 4894 | 'Python vary in what implicit meta path finders are defined. The ' |
| 4895 | 'one\n' |
| 4896 | 'they all do define, though, is one that handles ' |
| 4897 | '"sys.path_hooks",\n' |
| 4898 | '"sys.path_importer_cache", and "sys.path".\n' |
| 4899 | '\n' |
| 4900 | 'The implicit finder searches for the requested module in the ' |
| 4901 | '"paths"\n' |
| 4902 | 'specified in one of two places ("paths" do not have to be file ' |
| 4903 | 'system\n' |
| 4904 | 'paths). If the module being imported is supposed to be ' |
| 4905 | 'contained\n' |
| 4906 | 'within a package then the second argument passed to ' |
| 4907 | '"find_module()",\n' |
| 4908 | '"__path__" on the parent package, is used as the source of ' |
| 4909 | 'paths. If\n' |
| 4910 | 'the module is not contained in a package then "sys.path" is used ' |
| 4911 | 'as\n' |
| 4912 | 'the source of paths.\n' |
| 4913 | '\n' |
| 4914 | 'Once the source of paths is chosen it is iterated over to find ' |
| 4915 | 'a\n' |
| 4916 | 'finder that can handle that path. The dict at\n' |
| 4917 | '"sys.path_importer_cache" caches finders for paths and is ' |
| 4918 | 'checked for\n' |
| 4919 | 'a finder. If the path does not have a finder cached then\n' |
| 4920 | '"sys.path_hooks" is searched by calling each object in the list ' |
| 4921 | 'with a\n' |
| 4922 | 'single argument of the path, returning a finder or raises\n' |
| 4923 | '"ImportError". If a finder is returned then it is cached in\n' |
| 4924 | '"sys.path_importer_cache" and then used for that path entry. If ' |
| 4925 | 'no\n' |
| 4926 | 'finder can be found but the path exists then a value of "None" ' |
| 4927 | 'is\n' |
| 4928 | 'stored in "sys.path_importer_cache" to signify that an implicit, ' |
| 4929 | 'file-\n' |
| 4930 | 'based finder that handles modules stored as individual files ' |
| 4931 | 'should be\n' |
| 4932 | 'used for that path. If the path does not exist then a finder ' |
| 4933 | 'which\n' |
| 4934 | 'always returns "None" is placed in the cache for the path.\n' |
| 4935 | '\n' |
| 4936 | 'If no finder can find the module then "ImportError" is raised.\n' |
| 4937 | 'Otherwise some finder returned a loader whose "load_module()" ' |
| 4938 | 'method\n' |
| 4939 | 'is called with the name of the module to load (see **PEP 302** ' |
| 4940 | 'for the\n' |
| 4941 | 'original definition of loaders). A loader has several ' |
| 4942 | 'responsibilities\n' |
| 4943 | 'to perform on a module it loads. First, if the module already ' |
| 4944 | 'exists\n' |
| 4945 | 'in "sys.modules" (a possibility if the loader is called outside ' |
| 4946 | 'of the\n' |
| 4947 | 'import machinery) then it is to use that module for ' |
| 4948 | 'initialization and\n' |
| 4949 | 'not a new module. But if the module does not exist in ' |
| 4950 | '"sys.modules"\n' |
| 4951 | 'then it is to be added to that dict before initialization ' |
| 4952 | 'begins. If\n' |
| 4953 | 'an error occurs during loading of the module and it was added ' |
| 4954 | 'to\n' |
| 4955 | '"sys.modules" it is to be removed from the dict. If an error ' |
| 4956 | 'occurs\n' |
| 4957 | 'but the module was already in "sys.modules" it is left in the ' |
| 4958 | 'dict.\n' |
| 4959 | '\n' |
| 4960 | 'The loader must set several attributes on the module. "__name__" ' |
| 4961 | 'is to\n' |
| 4962 | 'be set to the name of the module. "__file__" is to be the "path" ' |
| 4963 | 'to\n' |
| 4964 | 'the file unless the module is built-in (and thus listed in\n' |
| 4965 | '"sys.builtin_module_names") in which case the attribute is not ' |
| 4966 | 'set. If\n' |
| 4967 | 'what is being imported is a package then "__path__" is to be set ' |
| 4968 | 'to a\n' |
| 4969 | 'list of paths to be searched when looking for modules and ' |
| 4970 | 'packages\n' |
| 4971 | 'contained within the package being imported. "__package__" is ' |
| 4972 | 'optional\n' |
| 4973 | 'but should be set to the name of package that contains the ' |
| 4974 | 'module or\n' |
| 4975 | 'package (the empty string is used for module not contained in a\n' |
| 4976 | 'package). "__loader__" is also optional but should be set to ' |
| 4977 | 'the\n' |
| 4978 | 'loader object that is loading the module.\n' |
| 4979 | '\n' |
| 4980 | 'If an error occurs during loading then the loader raises ' |
| 4981 | '"ImportError"\n' |
| 4982 | 'if some other exception is not already being propagated. ' |
| 4983 | 'Otherwise the\n' |
| 4984 | 'loader returns the module that was loaded and initialized.\n' |
| 4985 | '\n' |
| 4986 | 'When step (1) finishes without raising an exception, step (2) ' |
| 4987 | 'can\n' |
| 4988 | 'begin.\n' |
| 4989 | '\n' |
| 4990 | 'The first form of "import" statement binds the module name in ' |
| 4991 | 'the\n' |
| 4992 | 'local namespace to the module object, and then goes on to import ' |
| 4993 | 'the\n' |
| 4994 | 'next identifier, if any. If the module name is followed by ' |
| 4995 | '"as", the\n' |
| 4996 | 'name following "as" is used as the local name for the module.\n' |
| 4997 | '\n' |
| 4998 | 'The "from" form does not bind the module name: it goes through ' |
| 4999 | 'the\n' |
| 5000 | 'list of identifiers, looks each one of them up in the module ' |
| 5001 | 'found in\n' |
| 5002 | 'step (1), and binds the name in the local namespace to the ' |
| 5003 | 'object thus\n' |
| 5004 | 'found. As with the first form of "import", an alternate local ' |
| 5005 | 'name\n' |
| 5006 | 'can be supplied by specifying ""as" localname". If a name is ' |
| 5007 | 'not\n' |
| 5008 | 'found, "ImportError" is raised. If the list of identifiers is\n' |
| 5009 | 'replaced by a star ("\'*\'"), all public names defined in the ' |
| 5010 | 'module are\n' |
| 5011 | 'bound in the local namespace of the "import" statement..\n' |
| 5012 | '\n' |
| 5013 | 'The *public names* defined by a module are determined by ' |
| 5014 | 'checking the\n' |
| 5015 | 'module\'s namespace for a variable named "__all__"; if defined, ' |
| 5016 | 'it must\n' |
| 5017 | 'be a sequence of strings which are names defined or imported by ' |
| 5018 | 'that\n' |
| 5019 | 'module. The names given in "__all__" are all considered public ' |
| 5020 | 'and\n' |
| 5021 | 'are required to exist. If "__all__" is not defined, the set of ' |
| 5022 | 'public\n' |
| 5023 | "names includes all names found in the module's namespace which " |
| 5024 | 'do not\n' |
| 5025 | 'begin with an underscore character ("\'_\'"). "__all__" should ' |
| 5026 | 'contain\n' |
| 5027 | 'the entire public API. It is intended to avoid accidentally ' |
| 5028 | 'exporting\n' |
| 5029 | 'items that are not part of the API (such as library modules ' |
| 5030 | 'which were\n' |
| 5031 | 'imported and used within the module).\n' |
| 5032 | '\n' |
| 5033 | 'The "from" form with "*" may only occur in a module scope. If ' |
| 5034 | 'the\n' |
| 5035 | 'wild card form of import --- "import *" --- is used in a ' |
| 5036 | 'function and\n' |
| 5037 | 'the function contains or is a nested block with free variables, ' |
| 5038 | 'the\n' |
| 5039 | 'compiler will raise a "SyntaxError".\n' |
| 5040 | '\n' |
| 5041 | 'When specifying what module to import you do not have to specify ' |
| 5042 | 'the\n' |
| 5043 | 'absolute name of the module. When a module or package is ' |
| 5044 | 'contained\n' |
| 5045 | 'within another package it is possible to make a relative import ' |
| 5046 | 'within\n' |
| 5047 | 'the same top package without having to mention the package name. ' |
| 5048 | 'By\n' |
| 5049 | 'using leading dots in the specified module or package after ' |
| 5050 | '"from" you\n' |
| 5051 | 'can specify how high to traverse up the current package ' |
| 5052 | 'hierarchy\n' |
| 5053 | 'without specifying exact names. One leading dot means the ' |
| 5054 | 'current\n' |
| 5055 | 'package where the module making the import exists. Two dots ' |
| 5056 | 'means up\n' |
| 5057 | 'one package level. Three dots is up two levels, etc. So if you ' |
| 5058 | 'execute\n' |
| 5059 | '"from . import mod" from a module in the "pkg" package then you ' |
| 5060 | 'will\n' |
| 5061 | 'end up importing "pkg.mod". If you execute "from ..subpkg2 ' |
| 5062 | 'import mod"\n' |
| 5063 | 'from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". ' |
| 5064 | 'The\n' |
| 5065 | 'specification for relative imports is contained within **PEP ' |
| 5066 | '328**.\n' |
| 5067 | '\n' |
| 5068 | '"importlib.import_module()" is provided to support applications ' |
| 5069 | 'that\n' |
| 5070 | 'determine which modules need to be loaded dynamically.\n' |
| 5071 | '\n' |
| 5072 | '\n' |
| 5073 | 'Future statements\n' |
| 5074 | '=================\n' |
| 5075 | '\n' |
| 5076 | 'A *future statement* is a directive to the compiler that a ' |
| 5077 | 'particular\n' |
| 5078 | 'module should be compiled using syntax or semantics that will ' |
| 5079 | 'be\n' |
| 5080 | 'available in a specified future release of Python. The future\n' |
| 5081 | 'statement is intended to ease migration to future versions of ' |
| 5082 | 'Python\n' |
| 5083 | 'that introduce incompatible changes to the language. It allows ' |
| 5084 | 'use of\n' |
| 5085 | 'the new features on a per-module basis before the release in ' |
| 5086 | 'which the\n' |
| 5087 | 'feature becomes standard.\n' |
| 5088 | '\n' |
| 5089 | ' future_statement ::= "from" "__future__" "import" feature ' |
| 5090 | '["as" name]\n' |
| 5091 | ' ("," feature ["as" name])*\n' |
| 5092 | ' | "from" "__future__" "import" "(" ' |
| 5093 | 'feature ["as" name]\n' |
| 5094 | ' ("," feature ["as" name])* [","] ")"\n' |
| 5095 | ' feature ::= identifier\n' |
| 5096 | ' name ::= identifier\n' |
| 5097 | '\n' |
| 5098 | 'A future statement must appear near the top of the module. The ' |
| 5099 | 'only\n' |
| 5100 | 'lines that can appear before a future statement are:\n' |
| 5101 | '\n' |
| 5102 | '* the module docstring (if any),\n' |
| 5103 | '\n' |
| 5104 | '* comments,\n' |
| 5105 | '\n' |
| 5106 | '* blank lines, and\n' |
| 5107 | '\n' |
| 5108 | '* other future statements.\n' |
| 5109 | '\n' |
| 5110 | 'The features recognized by Python 2.6 are "unicode_literals",\n' |
| 5111 | '"print_function", "absolute_import", "division", "generators",\n' |
| 5112 | '"nested_scopes" and "with_statement". "generators", ' |
| 5113 | '"with_statement",\n' |
| 5114 | '"nested_scopes" are redundant in Python version 2.6 and above ' |
| 5115 | 'because\n' |
| 5116 | 'they are always enabled.\n' |
| 5117 | '\n' |
| 5118 | 'A future statement is recognized and treated specially at ' |
| 5119 | 'compile\n' |
| 5120 | 'time: Changes to the semantics of core constructs are often\n' |
| 5121 | 'implemented by generating different code. It may even be the ' |
| 5122 | 'case\n' |
| 5123 | 'that a new feature introduces new incompatible syntax (such as a ' |
| 5124 | 'new\n' |
| 5125 | 'reserved word), in which case the compiler may need to parse ' |
| 5126 | 'the\n' |
| 5127 | 'module differently. Such decisions cannot be pushed off until\n' |
| 5128 | 'runtime.\n' |
| 5129 | '\n' |
| 5130 | 'For any given release, the compiler knows which feature names ' |
| 5131 | 'have\n' |
| 5132 | 'been defined, and raises a compile-time error if a future ' |
| 5133 | 'statement\n' |
| 5134 | 'contains a feature not known to it.\n' |
| 5135 | '\n' |
| 5136 | 'The direct runtime semantics are the same as for any import ' |
| 5137 | 'statement:\n' |
| 5138 | 'there is a standard module "__future__", described later, and it ' |
| 5139 | 'will\n' |
| 5140 | 'be imported in the usual way at the time the future statement ' |
| 5141 | 'is\n' |
| 5142 | 'executed.\n' |
| 5143 | '\n' |
| 5144 | 'The interesting runtime semantics depend on the specific ' |
| 5145 | 'feature\n' |
| 5146 | 'enabled by the future statement.\n' |
| 5147 | '\n' |
| 5148 | 'Note that there is nothing special about the statement:\n' |
| 5149 | '\n' |
| 5150 | ' import __future__ [as name]\n' |
| 5151 | '\n' |
| 5152 | "That is not a future statement; it's an ordinary import " |
| 5153 | 'statement with\n' |
| 5154 | 'no special semantics or syntax restrictions.\n' |
| 5155 | '\n' |
| 5156 | 'Code compiled by an "exec" statement or calls to the built-in\n' |
| 5157 | 'functions "compile()" and "execfile()" that occur in a module ' |
| 5158 | '"M"\n' |
| 5159 | 'containing a future statement will, by default, use the new ' |
| 5160 | 'syntax or\n' |
| 5161 | 'semantics associated with the future statement. This can, ' |
| 5162 | 'starting\n' |
| 5163 | 'with Python 2.2 be controlled by optional arguments to ' |
| 5164 | '"compile()" ---\n' |
| 5165 | 'see the documentation of that function for details.\n' |
| 5166 | '\n' |
| 5167 | 'A future statement typed at an interactive interpreter prompt ' |
| 5168 | 'will\n' |
| 5169 | 'take effect for the rest of the interpreter session. If an\n' |
| 5170 | 'interpreter is started with the "-i" option, is passed a script ' |
| 5171 | 'name\n' |
| 5172 | 'to execute, and the script includes a future statement, it will ' |
| 5173 | 'be in\n' |
| 5174 | 'effect in the interactive session started after the script is\n' |
| 5175 | 'executed.\n' |
| 5176 | '\n' |
| 5177 | 'See also: **PEP 236** - Back to the __future__\n' |
| 5178 | '\n' |
| 5179 | ' The original proposal for the __future__ mechanism.\n', |
| 5180 | 'in': '\n' |
| 5181 | 'Comparisons\n' |
| 5182 | '***********\n' |
| 5183 | '\n' |
| 5184 | 'Unlike C, all comparison operations in Python have the same ' |
| 5185 | 'priority,\n' |
| 5186 | 'which is lower than that of any arithmetic, shifting or bitwise\n' |
| 5187 | 'operation. Also unlike C, expressions like "a < b < c" have the\n' |
| 5188 | 'interpretation that is conventional in mathematics:\n' |
| 5189 | '\n' |
| 5190 | ' comparison ::= or_expr ( comp_operator or_expr )*\n' |
| 5191 | ' comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | "!="\n' |
| 5192 | ' | "is" ["not"] | ["not"] "in"\n' |
| 5193 | '\n' |
| 5194 | 'Comparisons yield boolean values: "True" or "False".\n' |
| 5195 | '\n' |
| 5196 | 'Comparisons can be chained arbitrarily, e.g., "x < y <= z" is\n' |
| 5197 | 'equivalent to "x < y and y <= z", except that "y" is evaluated only\n' |
| 5198 | 'once (but in both cases "z" is not evaluated at all when "x < y" is\n' |
| 5199 | 'found to be false).\n' |
| 5200 | '\n' |
| 5201 | 'Formally, if *a*, *b*, *c*, ..., *y*, *z* are expressions and ' |
| 5202 | '*op1*,\n' |
| 5203 | '*op2*, ..., *opN* are comparison operators, then "a op1 b op2 c ... ' |
| 5204 | 'y\n' |
| 5205 | 'opN z" is equivalent to "a op1 b and b op2 c and ... y opN z", ' |
| 5206 | 'except\n' |
| 5207 | 'that each expression is evaluated at most once.\n' |
| 5208 | '\n' |
| 5209 | 'Note that "a op1 b op2 c" doesn\'t imply any kind of comparison ' |
| 5210 | 'between\n' |
| 5211 | '*a* and *c*, so that, e.g., "x < y > z" is perfectly legal (though\n' |
| 5212 | 'perhaps not pretty).\n' |
| 5213 | '\n' |
| 5214 | 'The forms "<>" and "!=" are equivalent; for consistency with C, ' |
| 5215 | '"!="\n' |
| 5216 | 'is preferred; where "!=" is mentioned below "<>" is also accepted.\n' |
| 5217 | 'The "<>" spelling is considered obsolescent.\n' |
| 5218 | '\n' |
| 5219 | 'The operators "<", ">", "==", ">=", "<=", and "!=" compare the ' |
| 5220 | 'values\n' |
| 5221 | 'of two objects. The objects need not have the same type. If both ' |
| 5222 | 'are\n' |
| 5223 | 'numbers, they are converted to a common type. Otherwise, objects ' |
| 5224 | 'of\n' |
| 5225 | 'different types *always* compare unequal, and are ordered ' |
| 5226 | 'consistently\n' |
| 5227 | 'but arbitrarily. You can control comparison behavior of objects of\n' |
| 5228 | 'non-built-in types by defining a "__cmp__" method or rich ' |
| 5229 | 'comparison\n' |
| 5230 | 'methods like "__gt__", described in section Special method names.\n' |
| 5231 | '\n' |
| 5232 | '(This unusual definition of comparison was used to simplify the\n' |
| 5233 | 'definition of operations like sorting and the "in" and "not in"\n' |
| 5234 | 'operators. In the future, the comparison rules for objects of\n' |
| 5235 | 'different types are likely to change.)\n' |
| 5236 | '\n' |
| 5237 | 'Comparison of objects of the same type depends on the type:\n' |
| 5238 | '\n' |
| 5239 | '* Numbers are compared arithmetically.\n' |
| 5240 | '\n' |
| 5241 | '* Strings are compared lexicographically using the numeric\n' |
| 5242 | ' equivalents (the result of the built-in function "ord()") of ' |
| 5243 | 'their\n' |
| 5244 | ' characters. Unicode and 8-bit strings are fully interoperable in\n' |
| 5245 | ' this behavior. [4]\n' |
| 5246 | '\n' |
| 5247 | '* Tuples and lists are compared lexicographically using comparison\n' |
| 5248 | ' of corresponding elements. This means that to compare equal, ' |
| 5249 | 'each\n' |
| 5250 | ' element must compare equal and the two sequences must be of the ' |
| 5251 | 'same\n' |
| 5252 | ' type and have the same length.\n' |
| 5253 | '\n' |
| 5254 | ' If not equal, the sequences are ordered the same as their first\n' |
| 5255 | ' differing elements. For example, "cmp([1,2,x], [1,2,y])" returns\n' |
| 5256 | ' the same as "cmp(x,y)". If the corresponding element does not\n' |
| 5257 | ' exist, the shorter sequence is ordered first (for example, "[1,2] ' |
| 5258 | '<\n' |
| 5259 | ' [1,2,3]").\n' |
| 5260 | '\n' |
| 5261 | '* Mappings (dictionaries) compare equal if and only if their sorted\n' |
| 5262 | ' (key, value) lists compare equal. [5] Outcomes other than ' |
| 5263 | 'equality\n' |
| 5264 | ' are resolved consistently, but are not otherwise defined. [6]\n' |
| 5265 | '\n' |
| 5266 | '* Most other objects of built-in types compare unequal unless they\n' |
| 5267 | ' are the same object; the choice whether one object is considered\n' |
| 5268 | ' smaller or larger than another one is made arbitrarily but\n' |
| 5269 | ' consistently within one execution of a program.\n' |
| 5270 | '\n' |
| 5271 | 'The operators "in" and "not in" test for collection membership. "x ' |
| 5272 | 'in\n' |
| 5273 | 's" evaluates to true if *x* is a member of the collection *s*, and\n' |
| 5274 | 'false otherwise. "x not in s" returns the negation of "x in s". ' |
| 5275 | 'The\n' |
| 5276 | 'collection membership test has traditionally been bound to ' |
| 5277 | 'sequences;\n' |
| 5278 | 'an object is a member of a collection if the collection is a ' |
| 5279 | 'sequence\n' |
| 5280 | 'and contains an element equal to that object. However, it make ' |
| 5281 | 'sense\n' |
| 5282 | 'for many other object types to support membership tests without ' |
| 5283 | 'being\n' |
| 5284 | 'a sequence. In particular, dictionaries (for keys) and sets ' |
| 5285 | 'support\n' |
| 5286 | 'membership testing.\n' |
| 5287 | '\n' |
| 5288 | 'For the list and tuple types, "x in y" is true if and only if there\n' |
| 5289 | 'exists an index *i* such that "x == y[i]" is true.\n' |
| 5290 | '\n' |
| 5291 | 'For the Unicode and string types, "x in y" is true if and only if ' |
| 5292 | '*x*\n' |
| 5293 | 'is a substring of *y*. An equivalent test is "y.find(x) != -1".\n' |
| 5294 | 'Note, *x* and *y* need not be the same type; consequently, "u\'ab\' ' |
| 5295 | 'in\n' |
| 5296 | '\'abc\'" will return "True". Empty strings are always considered to ' |
| 5297 | 'be a\n' |
| 5298 | 'substring of any other string, so """ in "abc"" will return "True".\n' |
| 5299 | '\n' |
| 5300 | 'Changed in version 2.3: Previously, *x* was required to be a string ' |
| 5301 | 'of\n' |
| 5302 | 'length "1".\n' |
| 5303 | '\n' |
| 5304 | 'For user-defined classes which define the "__contains__()" method, ' |
| 5305 | '"x\n' |
| 5306 | 'in y" is true if and only if "y.__contains__(x)" is true.\n' |
| 5307 | '\n' |
| 5308 | 'For user-defined classes which do not define "__contains__()" but ' |
| 5309 | 'do\n' |
| 5310 | 'define "__iter__()", "x in y" is true if some value "z" with "x == ' |
| 5311 | 'z"\n' |
| 5312 | 'is produced while iterating over "y". If an exception is raised\n' |
| 5313 | 'during the iteration, it is as if "in" raised that exception.\n' |
| 5314 | '\n' |
| 5315 | 'Lastly, the old-style iteration protocol is tried: if a class ' |
| 5316 | 'defines\n' |
| 5317 | '"__getitem__()", "x in y" is true if and only if there is a non-\n' |
| 5318 | 'negative integer index *i* such that "x == y[i]", and all lower\n' |
| 5319 | 'integer indices do not raise "IndexError" exception. (If any other\n' |
| 5320 | 'exception is raised, it is as if "in" raised that exception).\n' |
| 5321 | '\n' |
| 5322 | 'The operator "not in" is defined to have the inverse true value of\n' |
| 5323 | '"in".\n' |
| 5324 | '\n' |
| 5325 | 'The operators "is" and "is not" test for object identity: "x is y" ' |
| 5326 | 'is\n' |
| 5327 | 'true if and only if *x* and *y* are the same object. "x is not y"\n' |
| 5328 | 'yields the inverse truth value. [7]\n', |
| 5329 | 'integers': '\n' |
| 5330 | 'Integer and long integer literals\n' |
| 5331 | '*********************************\n' |
| 5332 | '\n' |
| 5333 | 'Integer and long integer literals are described by the ' |
| 5334 | 'following\n' |
| 5335 | 'lexical definitions:\n' |
| 5336 | '\n' |
| 5337 | ' longinteger ::= integer ("l" | "L")\n' |
| 5338 | ' integer ::= decimalinteger | octinteger | hexinteger ' |
| 5339 | '| bininteger\n' |
| 5340 | ' decimalinteger ::= nonzerodigit digit* | "0"\n' |
| 5341 | ' octinteger ::= "0" ("o" | "O") octdigit+ | "0" ' |
| 5342 | 'octdigit+\n' |
| 5343 | ' hexinteger ::= "0" ("x" | "X") hexdigit+\n' |
| 5344 | ' bininteger ::= "0" ("b" | "B") bindigit+\n' |
| 5345 | ' nonzerodigit ::= "1"..."9"\n' |
| 5346 | ' octdigit ::= "0"..."7"\n' |
| 5347 | ' bindigit ::= "0" | "1"\n' |
| 5348 | ' hexdigit ::= digit | "a"..."f" | "A"..."F"\n' |
| 5349 | '\n' |
| 5350 | 'Although both lower case "\'l\'" and upper case "\'L\'" are ' |
| 5351 | 'allowed as\n' |
| 5352 | 'suffix for long integers, it is strongly recommended to always ' |
| 5353 | 'use\n' |
| 5354 | '"\'L\'", since the letter "\'l\'" looks too much like the ' |
| 5355 | 'digit "\'1\'".\n' |
| 5356 | '\n' |
| 5357 | 'Plain integer literals that are above the largest ' |
| 5358 | 'representable plain\n' |
| 5359 | 'integer (e.g., 2147483647 when using 32-bit arithmetic) are ' |
| 5360 | 'accepted\n' |
| 5361 | 'as if they were long integers instead. [1] There is no limit ' |
| 5362 | 'for long\n' |
| 5363 | 'integer literals apart from what can be stored in available ' |
| 5364 | 'memory.\n' |
| 5365 | '\n' |
| 5366 | 'Some examples of plain integer literals (first row) and long ' |
| 5367 | 'integer\n' |
| 5368 | 'literals (second and third rows):\n' |
| 5369 | '\n' |
| 5370 | ' 7 2147483647 0177\n' |
| 5371 | ' 3L 79228162514264337593543950336L 0377L ' |
| 5372 | '0x100000000L\n' |
| 5373 | ' 79228162514264337593543950336 0xdeadbeef\n', |
| 5374 | 'lambda': '\n' |
| 5375 | 'Lambdas\n' |
| 5376 | '*******\n' |
| 5377 | '\n' |
| 5378 | ' lambda_expr ::= "lambda" [parameter_list]: expression\n' |
| 5379 | ' old_lambda_expr ::= "lambda" [parameter_list]: ' |
| 5380 | 'old_expression\n' |
| 5381 | '\n' |
| 5382 | 'Lambda expressions (sometimes called lambda forms) have the ' |
| 5383 | 'same\n' |
| 5384 | 'syntactic position as expressions. They are a shorthand to ' |
| 5385 | 'create\n' |
| 5386 | 'anonymous functions; the expression "lambda arguments: ' |
| 5387 | 'expression"\n' |
| 5388 | 'yields a function object. The unnamed object behaves like a ' |
| 5389 | 'function\n' |
| 5390 | 'object defined with\n' |
| 5391 | '\n' |
| 5392 | ' def name(arguments):\n' |
| 5393 | ' return expression\n' |
| 5394 | '\n' |
| 5395 | 'See section Function definitions for the syntax of parameter ' |
| 5396 | 'lists.\n' |
| 5397 | 'Note that functions created with lambda expressions cannot ' |
| 5398 | 'contain\n' |
| 5399 | 'statements.\n', |
| 5400 | 'lists': '\n' |
| 5401 | 'List displays\n' |
| 5402 | '*************\n' |
| 5403 | '\n' |
| 5404 | 'A list display is a possibly empty series of expressions enclosed ' |
| 5405 | 'in\n' |
| 5406 | 'square brackets:\n' |
| 5407 | '\n' |
| 5408 | ' list_display ::= "[" [expression_list | ' |
| 5409 | 'list_comprehension] "]"\n' |
| 5410 | ' list_comprehension ::= expression list_for\n' |
| 5411 | ' list_for ::= "for" target_list "in" ' |
| 5412 | 'old_expression_list [list_iter]\n' |
| 5413 | ' old_expression_list ::= old_expression [("," old_expression)+ ' |
| 5414 | '[","]]\n' |
| 5415 | ' old_expression ::= or_test | old_lambda_expr\n' |
| 5416 | ' list_iter ::= list_for | list_if\n' |
| 5417 | ' list_if ::= "if" old_expression [list_iter]\n' |
| 5418 | '\n' |
| 5419 | 'A list display yields a new list object. Its contents are ' |
| 5420 | 'specified\n' |
| 5421 | 'by providing either a list of expressions or a list ' |
| 5422 | 'comprehension.\n' |
| 5423 | 'When a comma-separated list of expressions is supplied, its ' |
| 5424 | 'elements\n' |
| 5425 | 'are evaluated from left to right and placed into the list object ' |
| 5426 | 'in\n' |
| 5427 | 'that order. When a list comprehension is supplied, it consists ' |
| 5428 | 'of a\n' |
| 5429 | 'single expression followed by at least one "for" clause and zero ' |
| 5430 | 'or\n' |
| 5431 | 'more "for" or "if" clauses. In this case, the elements of the ' |
| 5432 | 'new\n' |
| 5433 | 'list are those that would be produced by considering each of the ' |
| 5434 | '"for"\n' |
| 5435 | 'or "if" clauses a block, nesting from left to right, and ' |
| 5436 | 'evaluating\n' |
| 5437 | 'the expression to produce a list element each time the innermost ' |
| 5438 | 'block\n' |
| 5439 | 'is reached [1].\n', |
| 5440 | 'naming': '\n' |
| 5441 | 'Naming and binding\n' |
| 5442 | '******************\n' |
| 5443 | '\n' |
| 5444 | '*Names* refer to objects. Names are introduced by name binding\n' |
| 5445 | 'operations. Each occurrence of a name in the program text refers ' |
| 5446 | 'to\n' |
| 5447 | 'the *binding* of that name established in the innermost function ' |
| 5448 | 'block\n' |
| 5449 | 'containing the use.\n' |
| 5450 | '\n' |
| 5451 | 'A *block* is a piece of Python program text that is executed as ' |
| 5452 | 'a\n' |
| 5453 | 'unit. The following are blocks: a module, a function body, and a ' |
| 5454 | 'class\n' |
| 5455 | 'definition. Each command typed interactively is a block. A ' |
| 5456 | 'script\n' |
| 5457 | 'file (a file given as standard input to the interpreter or ' |
| 5458 | 'specified\n' |
| 5459 | 'on the interpreter command line the first argument) is a code ' |
| 5460 | 'block.\n' |
| 5461 | 'A script command (a command specified on the interpreter command ' |
| 5462 | 'line\n' |
| 5463 | "with the '**-c**' option) is a code block. The file read by " |
| 5464 | 'the\n' |
| 5465 | 'built-in function "execfile()" is a code block. The string ' |
| 5466 | 'argument\n' |
| 5467 | 'passed to the built-in function "eval()" and to the "exec" ' |
| 5468 | 'statement\n' |
| 5469 | 'is a code block. The expression read and evaluated by the ' |
| 5470 | 'built-in\n' |
| 5471 | 'function "input()" is a code block.\n' |
| 5472 | '\n' |
| 5473 | 'A code block is executed in an *execution frame*. A frame ' |
| 5474 | 'contains\n' |
| 5475 | 'some administrative information (used for debugging) and ' |
| 5476 | 'determines\n' |
| 5477 | "where and how execution continues after the code block's " |
| 5478 | 'execution has\n' |
| 5479 | 'completed.\n' |
| 5480 | '\n' |
| 5481 | 'A *scope* defines the visibility of a name within a block. If a ' |
| 5482 | 'local\n' |
| 5483 | 'variable is defined in a block, its scope includes that block. ' |
| 5484 | 'If the\n' |
| 5485 | 'definition occurs in a function block, the scope extends to any ' |
| 5486 | 'blocks\n' |
| 5487 | 'contained within the defining one, unless a contained block ' |
| 5488 | 'introduces\n' |
| 5489 | 'a different binding for the name. The scope of names defined in ' |
| 5490 | 'a\n' |
| 5491 | 'class block is limited to the class block; it does not extend to ' |
| 5492 | 'the\n' |
| 5493 | 'code blocks of methods -- this includes generator expressions ' |
| 5494 | 'since\n' |
| 5495 | 'they are implemented using a function scope. This means that ' |
| 5496 | 'the\n' |
| 5497 | 'following will fail:\n' |
| 5498 | '\n' |
| 5499 | ' class A:\n' |
| 5500 | ' a = 42\n' |
| 5501 | ' b = list(a + i for i in range(10))\n' |
| 5502 | '\n' |
| 5503 | 'When a name is used in a code block, it is resolved using the ' |
| 5504 | 'nearest\n' |
| 5505 | 'enclosing scope. The set of all such scopes visible to a code ' |
| 5506 | 'block\n' |
| 5507 | "is called the block's *environment*.\n" |
| 5508 | '\n' |
| 5509 | 'If a name is bound in a block, it is a local variable of that ' |
| 5510 | 'block.\n' |
| 5511 | 'If a name is bound at the module level, it is a global ' |
| 5512 | 'variable. (The\n' |
| 5513 | 'variables of the module code block are local and global.) If a\n' |
| 5514 | 'variable is used in a code block but not defined there, it is a ' |
| 5515 | '*free\n' |
| 5516 | 'variable*.\n' |
| 5517 | '\n' |
| 5518 | 'When a name is not found at all, a "NameError" exception is ' |
| 5519 | 'raised.\n' |
| 5520 | 'If the name refers to a local variable that has not been bound, ' |
| 5521 | 'a\n' |
| 5522 | '"UnboundLocalError" exception is raised. "UnboundLocalError" is ' |
| 5523 | 'a\n' |
| 5524 | 'subclass of "NameError".\n' |
| 5525 | '\n' |
| 5526 | 'The following constructs bind names: formal parameters to ' |
| 5527 | 'functions,\n' |
| 5528 | '"import" statements, class and function definitions (these bind ' |
| 5529 | 'the\n' |
| 5530 | 'class or function name in the defining block), and targets that ' |
| 5531 | 'are\n' |
| 5532 | 'identifiers if occurring in an assignment, "for" loop header, in ' |
| 5533 | 'the\n' |
| 5534 | 'second position of an "except" clause header or after "as" in a ' |
| 5535 | '"with"\n' |
| 5536 | 'statement. The "import" statement of the form "from ... import ' |
| 5537 | '*"\n' |
| 5538 | 'binds all names defined in the imported module, except those ' |
| 5539 | 'beginning\n' |
| 5540 | 'with an underscore. This form may only be used at the module ' |
| 5541 | 'level.\n' |
| 5542 | '\n' |
| 5543 | 'A target occurring in a "del" statement is also considered bound ' |
| 5544 | 'for\n' |
| 5545 | 'this purpose (though the actual semantics are to unbind the ' |
| 5546 | 'name). It\n' |
| 5547 | 'is illegal to unbind a name that is referenced by an enclosing ' |
| 5548 | 'scope;\n' |
| 5549 | 'the compiler will report a "SyntaxError".\n' |
| 5550 | '\n' |
| 5551 | 'Each assignment or import statement occurs within a block ' |
| 5552 | 'defined by a\n' |
| 5553 | 'class or function definition or at the module level (the ' |
| 5554 | 'top-level\n' |
| 5555 | 'code block).\n' |
| 5556 | '\n' |
| 5557 | 'If a name binding operation occurs anywhere within a code block, ' |
| 5558 | 'all\n' |
| 5559 | 'uses of the name within the block are treated as references to ' |
| 5560 | 'the\n' |
| 5561 | 'current block. This can lead to errors when a name is used ' |
| 5562 | 'within a\n' |
| 5563 | 'block before it is bound. This rule is subtle. Python lacks\n' |
| 5564 | 'declarations and allows name binding operations to occur ' |
| 5565 | 'anywhere\n' |
| 5566 | 'within a code block. The local variables of a code block can ' |
| 5567 | 'be\n' |
| 5568 | 'determined by scanning the entire text of the block for name ' |
| 5569 | 'binding\n' |
| 5570 | 'operations.\n' |
| 5571 | '\n' |
| 5572 | 'If the global statement occurs within a block, all uses of the ' |
| 5573 | 'name\n' |
| 5574 | 'specified in the statement refer to the binding of that name in ' |
| 5575 | 'the\n' |
| 5576 | 'top-level namespace. Names are resolved in the top-level ' |
| 5577 | 'namespace by\n' |
| 5578 | 'searching the global namespace, i.e. the namespace of the ' |
| 5579 | 'module\n' |
| 5580 | 'containing the code block, and the builtins namespace, the ' |
| 5581 | 'namespace\n' |
| 5582 | 'of the module "__builtin__". The global namespace is searched ' |
| 5583 | 'first.\n' |
| 5584 | 'If the name is not found there, the builtins namespace is ' |
| 5585 | 'searched.\n' |
| 5586 | 'The global statement must precede all uses of the name.\n' |
| 5587 | '\n' |
| 5588 | 'The builtins namespace associated with the execution of a code ' |
| 5589 | 'block\n' |
| 5590 | 'is actually found by looking up the name "__builtins__" in its ' |
| 5591 | 'global\n' |
| 5592 | 'namespace; this should be a dictionary or a module (in the ' |
| 5593 | 'latter case\n' |
| 5594 | "the module's dictionary is used). By default, when in the " |
| 5595 | '"__main__"\n' |
| 5596 | 'module, "__builtins__" is the built-in module "__builtin__" ' |
| 5597 | '(note: no\n' |
| 5598 | '\'s\'); when in any other module, "__builtins__" is an alias for ' |
| 5599 | 'the\n' |
| 5600 | 'dictionary of the "__builtin__" module itself. "__builtins__" ' |
| 5601 | 'can be\n' |
| 5602 | 'set to a user-created dictionary to create a weak form of ' |
| 5603 | 'restricted\n' |
| 5604 | 'execution.\n' |
| 5605 | '\n' |
| 5606 | '**CPython implementation detail:** Users should not touch\n' |
| 5607 | '"__builtins__"; it is strictly an implementation detail. Users\n' |
| 5608 | 'wanting to override values in the builtins namespace should ' |
| 5609 | '"import"\n' |
| 5610 | 'the "__builtin__" (no \'s\') module and modify its attributes\n' |
| 5611 | 'appropriately.\n' |
| 5612 | '\n' |
| 5613 | 'The namespace for a module is automatically created the first ' |
| 5614 | 'time a\n' |
| 5615 | 'module is imported. The main module for a script is always ' |
| 5616 | 'called\n' |
| 5617 | '"__main__".\n' |
| 5618 | '\n' |
| 5619 | 'The "global" statement has the same scope as a name binding ' |
| 5620 | 'operation\n' |
| 5621 | 'in the same block. If the nearest enclosing scope for a free ' |
| 5622 | 'variable\n' |
| 5623 | 'contains a global statement, the free variable is treated as a ' |
| 5624 | 'global.\n' |
| 5625 | '\n' |
| 5626 | 'A class definition is an executable statement that may use and ' |
| 5627 | 'define\n' |
| 5628 | 'names. These references follow the normal rules for name ' |
| 5629 | 'resolution.\n' |
| 5630 | 'The namespace of the class definition becomes the attribute ' |
| 5631 | 'dictionary\n' |
| 5632 | 'of the class. Names defined at the class scope are not visible ' |
| 5633 | 'in\n' |
| 5634 | 'methods.\n' |
| 5635 | '\n' |
| 5636 | '\n' |
| 5637 | 'Interaction with dynamic features\n' |
| 5638 | '=================================\n' |
| 5639 | '\n' |
| 5640 | 'There are several cases where Python statements are illegal when ' |
| 5641 | 'used\n' |
| 5642 | 'in conjunction with nested scopes that contain free variables.\n' |
| 5643 | '\n' |
| 5644 | 'If a variable is referenced in an enclosing scope, it is illegal ' |
| 5645 | 'to\n' |
| 5646 | 'delete the name. An error will be reported at compile time.\n' |
| 5647 | '\n' |
| 5648 | 'If the wild card form of import --- "import *" --- is used in a\n' |
| 5649 | 'function and the function contains or is a nested block with ' |
| 5650 | 'free\n' |
| 5651 | 'variables, the compiler will raise a "SyntaxError".\n' |
| 5652 | '\n' |
| 5653 | 'If "exec" is used in a function and the function contains or is ' |
| 5654 | 'a\n' |
| 5655 | 'nested block with free variables, the compiler will raise a\n' |
| 5656 | '"SyntaxError" unless the exec explicitly specifies the local ' |
| 5657 | 'namespace\n' |
| 5658 | 'for the "exec". (In other words, "exec obj" would be illegal, ' |
| 5659 | 'but\n' |
| 5660 | '"exec obj in ns" would be legal.)\n' |
| 5661 | '\n' |
| 5662 | 'The "eval()", "execfile()", and "input()" functions and the ' |
| 5663 | '"exec"\n' |
| 5664 | 'statement do not have access to the full environment for ' |
| 5665 | 'resolving\n' |
| 5666 | 'names. Names may be resolved in the local and global namespaces ' |
| 5667 | 'of\n' |
| 5668 | 'the caller. Free variables are not resolved in the nearest ' |
| 5669 | 'enclosing\n' |
| 5670 | 'namespace, but in the global namespace. [1] The "exec" statement ' |
| 5671 | 'and\n' |
| 5672 | 'the "eval()" and "execfile()" functions have optional arguments ' |
| 5673 | 'to\n' |
| 5674 | 'override the global and local namespace. If only one namespace ' |
| 5675 | 'is\n' |
| 5676 | 'specified, it is used for both.\n', |
| 5677 | 'numbers': '\n' |
| 5678 | 'Numeric literals\n' |
| 5679 | '****************\n' |
| 5680 | '\n' |
| 5681 | 'There are four types of numeric literals: plain integers, long\n' |
| 5682 | 'integers, floating point numbers, and imaginary numbers. There ' |
| 5683 | 'are no\n' |
| 5684 | 'complex literals (complex numbers can be formed by adding a ' |
| 5685 | 'real\n' |
| 5686 | 'number and an imaginary number).\n' |
| 5687 | '\n' |
| 5688 | 'Note that numeric literals do not include a sign; a phrase like ' |
| 5689 | '"-1"\n' |
| 5690 | 'is actually an expression composed of the unary operator ' |
| 5691 | '\'"-"\' and the\n' |
| 5692 | 'literal "1".\n', |
| 5693 | 'numeric-types': '\n' |
| 5694 | 'Emulating numeric types\n' |
| 5695 | '***********************\n' |
| 5696 | '\n' |
| 5697 | 'The following methods can be defined to emulate numeric ' |
| 5698 | 'objects.\n' |
| 5699 | 'Methods corresponding to operations that are not ' |
| 5700 | 'supported by the\n' |
| 5701 | 'particular kind of number implemented (e.g., bitwise ' |
| 5702 | 'operations for\n' |
| 5703 | 'non-integral numbers) should be left undefined.\n' |
| 5704 | '\n' |
| 5705 | 'object.__add__(self, other)\n' |
| 5706 | 'object.__sub__(self, other)\n' |
| 5707 | 'object.__mul__(self, other)\n' |
| 5708 | 'object.__floordiv__(self, other)\n' |
| 5709 | 'object.__mod__(self, other)\n' |
| 5710 | 'object.__divmod__(self, other)\n' |
| 5711 | 'object.__pow__(self, other[, modulo])\n' |
| 5712 | 'object.__lshift__(self, other)\n' |
| 5713 | 'object.__rshift__(self, other)\n' |
| 5714 | 'object.__and__(self, other)\n' |
| 5715 | 'object.__xor__(self, other)\n' |
| 5716 | 'object.__or__(self, other)\n' |
| 5717 | '\n' |
| 5718 | ' These methods are called to implement the binary ' |
| 5719 | 'arithmetic\n' |
| 5720 | ' operations ("+", "-", "*", "//", "%", "divmod()", ' |
| 5721 | '"pow()", "**",\n' |
| 5722 | ' "<<", ">>", "&", "^", "|"). For instance, to evaluate ' |
| 5723 | 'the\n' |
| 5724 | ' expression "x + y", where *x* is an instance of a ' |
| 5725 | 'class that has an\n' |
| 5726 | ' "__add__()" method, "x.__add__(y)" is called. The ' |
| 5727 | '"__divmod__()"\n' |
| 5728 | ' method should be the equivalent to using ' |
| 5729 | '"__floordiv__()" and\n' |
| 5730 | ' "__mod__()"; it should not be related to ' |
| 5731 | '"__truediv__()" (described\n' |
| 5732 | ' below). Note that "__pow__()" should be defined to ' |
| 5733 | 'accept an\n' |
| 5734 | ' optional third argument if the ternary version of the ' |
| 5735 | 'built-in\n' |
| 5736 | ' "pow()" function is to be supported.\n' |
| 5737 | '\n' |
| 5738 | ' If one of those methods does not support the operation ' |
| 5739 | 'with the\n' |
| 5740 | ' supplied arguments, it should return ' |
| 5741 | '"NotImplemented".\n' |
| 5742 | '\n' |
| 5743 | 'object.__div__(self, other)\n' |
| 5744 | 'object.__truediv__(self, other)\n' |
| 5745 | '\n' |
| 5746 | ' The division operator ("/") is implemented by these ' |
| 5747 | 'methods. The\n' |
| 5748 | ' "__truediv__()" method is used when ' |
| 5749 | '"__future__.division" is in\n' |
| 5750 | ' effect, otherwise "__div__()" is used. If only one of ' |
| 5751 | 'these two\n' |
| 5752 | ' methods is defined, the object will not support ' |
| 5753 | 'division in the\n' |
| 5754 | ' alternate context; "TypeError" will be raised ' |
| 5755 | 'instead.\n' |
| 5756 | '\n' |
| 5757 | 'object.__radd__(self, other)\n' |
| 5758 | 'object.__rsub__(self, other)\n' |
| 5759 | 'object.__rmul__(self, other)\n' |
| 5760 | 'object.__rdiv__(self, other)\n' |
| 5761 | 'object.__rtruediv__(self, other)\n' |
| 5762 | 'object.__rfloordiv__(self, other)\n' |
| 5763 | 'object.__rmod__(self, other)\n' |
| 5764 | 'object.__rdivmod__(self, other)\n' |
| 5765 | 'object.__rpow__(self, other)\n' |
| 5766 | 'object.__rlshift__(self, other)\n' |
| 5767 | 'object.__rrshift__(self, other)\n' |
| 5768 | 'object.__rand__(self, other)\n' |
| 5769 | 'object.__rxor__(self, other)\n' |
| 5770 | 'object.__ror__(self, other)\n' |
| 5771 | '\n' |
| 5772 | ' These methods are called to implement the binary ' |
| 5773 | 'arithmetic\n' |
| 5774 | ' operations ("+", "-", "*", "/", "%", "divmod()", ' |
| 5775 | '"pow()", "**",\n' |
| 5776 | ' "<<", ">>", "&", "^", "|") with reflected (swapped) ' |
| 5777 | 'operands.\n' |
| 5778 | ' These functions are only called if the left operand ' |
| 5779 | 'does not\n' |
| 5780 | ' support the corresponding operation and the operands ' |
| 5781 | 'are of\n' |
| 5782 | ' different types. [2] For instance, to evaluate the ' |
| 5783 | 'expression "x -\n' |
| 5784 | ' y", where *y* is an instance of a class that has an ' |
| 5785 | '"__rsub__()"\n' |
| 5786 | ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" ' |
| 5787 | 'returns\n' |
| 5788 | ' *NotImplemented*.\n' |
| 5789 | '\n' |
| 5790 | ' Note that ternary "pow()" will not try calling ' |
| 5791 | '"__rpow__()" (the\n' |
| 5792 | ' coercion rules would become too complicated).\n' |
| 5793 | '\n' |
| 5794 | " Note: If the right operand's type is a subclass of the " |
| 5795 | 'left\n' |
| 5796 | " operand's type and that subclass provides the " |
| 5797 | 'reflected method\n' |
| 5798 | ' for the operation, this method will be called before ' |
| 5799 | 'the left\n' |
| 5800 | " operand's non-reflected method. This behavior " |
| 5801 | 'allows subclasses\n' |
| 5802 | " to override their ancestors' operations.\n" |
| 5803 | '\n' |
| 5804 | 'object.__iadd__(self, other)\n' |
| 5805 | 'object.__isub__(self, other)\n' |
| 5806 | 'object.__imul__(self, other)\n' |
| 5807 | 'object.__idiv__(self, other)\n' |
| 5808 | 'object.__itruediv__(self, other)\n' |
| 5809 | 'object.__ifloordiv__(self, other)\n' |
| 5810 | 'object.__imod__(self, other)\n' |
| 5811 | 'object.__ipow__(self, other[, modulo])\n' |
| 5812 | 'object.__ilshift__(self, other)\n' |
| 5813 | 'object.__irshift__(self, other)\n' |
| 5814 | 'object.__iand__(self, other)\n' |
| 5815 | 'object.__ixor__(self, other)\n' |
| 5816 | 'object.__ior__(self, other)\n' |
| 5817 | '\n' |
| 5818 | ' These methods are called to implement the augmented ' |
| 5819 | 'arithmetic\n' |
| 5820 | ' assignments ("+=", "-=", "*=", "/=", "//=", "%=", ' |
| 5821 | '"**=", "<<=",\n' |
| 5822 | ' ">>=", "&=", "^=", "|="). These methods should ' |
| 5823 | 'attempt to do the\n' |
| 5824 | ' operation in-place (modifying *self*) and return the ' |
| 5825 | 'result (which\n' |
| 5826 | ' could be, but does not have to be, *self*). If a ' |
| 5827 | 'specific method\n' |
| 5828 | ' is not defined, the augmented assignment falls back to ' |
| 5829 | 'the normal\n' |
| 5830 | ' methods. For instance, to execute the statement "x += ' |
| 5831 | 'y", where\n' |
| 5832 | ' *x* is an instance of a class that has an "__iadd__()" ' |
| 5833 | 'method,\n' |
| 5834 | ' "x.__iadd__(y)" is called. If *x* is an instance of a ' |
| 5835 | 'class that\n' |
| 5836 | ' does not define a "__iadd__()" method, "x.__add__(y)" ' |
| 5837 | 'and\n' |
| 5838 | ' "y.__radd__(x)" are considered, as with the evaluation ' |
| 5839 | 'of "x + y".\n' |
| 5840 | '\n' |
| 5841 | 'object.__neg__(self)\n' |
| 5842 | 'object.__pos__(self)\n' |
| 5843 | 'object.__abs__(self)\n' |
| 5844 | 'object.__invert__(self)\n' |
| 5845 | '\n' |
| 5846 | ' Called to implement the unary arithmetic operations ' |
| 5847 | '("-", "+",\n' |
| 5848 | ' "abs()" and "~").\n' |
| 5849 | '\n' |
| 5850 | 'object.__complex__(self)\n' |
| 5851 | 'object.__int__(self)\n' |
| 5852 | 'object.__long__(self)\n' |
| 5853 | 'object.__float__(self)\n' |
| 5854 | '\n' |
| 5855 | ' Called to implement the built-in functions ' |
| 5856 | '"complex()", "int()",\n' |
| 5857 | ' "long()", and "float()". Should return a value of the ' |
| 5858 | 'appropriate\n' |
| 5859 | ' type.\n' |
| 5860 | '\n' |
| 5861 | 'object.__oct__(self)\n' |
| 5862 | 'object.__hex__(self)\n' |
| 5863 | '\n' |
| 5864 | ' Called to implement the built-in functions "oct()" and ' |
| 5865 | '"hex()".\n' |
| 5866 | ' Should return a string value.\n' |
| 5867 | '\n' |
| 5868 | 'object.__index__(self)\n' |
| 5869 | '\n' |
| 5870 | ' Called to implement "operator.index()". Also called ' |
| 5871 | 'whenever\n' |
| 5872 | ' Python needs an integer object (such as in slicing). ' |
| 5873 | 'Must return\n' |
| 5874 | ' an integer (int or long).\n' |
| 5875 | '\n' |
| 5876 | ' New in version 2.5.\n' |
| 5877 | '\n' |
| 5878 | 'object.__coerce__(self, other)\n' |
| 5879 | '\n' |
| 5880 | ' Called to implement "mixed-mode" numeric arithmetic. ' |
| 5881 | 'Should either\n' |
| 5882 | ' return a 2-tuple containing *self* and *other* ' |
| 5883 | 'converted to a\n' |
| 5884 | ' common numeric type, or "None" if conversion is ' |
| 5885 | 'impossible. When\n' |
| 5886 | ' the common type would be the type of "other", it is ' |
| 5887 | 'sufficient to\n' |
| 5888 | ' return "None", since the interpreter will also ask the ' |
| 5889 | 'other object\n' |
| 5890 | ' to attempt a coercion (but sometimes, if the ' |
| 5891 | 'implementation of the\n' |
| 5892 | ' other type cannot be changed, it is useful to do the ' |
| 5893 | 'conversion to\n' |
| 5894 | ' the other type here). A return value of ' |
| 5895 | '"NotImplemented" is\n' |
| 5896 | ' equivalent to returning "None".\n', |
| 5897 | 'objects': '\n' |
| 5898 | 'Objects, values and types\n' |
| 5899 | '*************************\n' |
| 5900 | '\n' |
| 5901 | "*Objects* are Python's abstraction for data. All data in a " |
| 5902 | 'Python\n' |
| 5903 | 'program is represented by objects or by relations between ' |
| 5904 | 'objects. (In\n' |
| 5905 | "a sense, and in conformance to Von Neumann's model of a " |
| 5906 | '"stored\n' |
| 5907 | 'program computer," code is also represented by objects.)\n' |
| 5908 | '\n' |
| 5909 | "Every object has an identity, a type and a value. An object's\n" |
| 5910 | '*identity* never changes once it has been created; you may ' |
| 5911 | 'think of it\n' |
| 5912 | 'as the object\'s address in memory. The \'"is"\' operator ' |
| 5913 | 'compares the\n' |
| 5914 | 'identity of two objects; the "id()" function returns an ' |
| 5915 | 'integer\n' |
| 5916 | 'representing its identity (currently implemented as its ' |
| 5917 | 'address). An\n' |
| 5918 | "object's *type* is also unchangeable. [1] An object's type " |
| 5919 | 'determines\n' |
| 5920 | 'the operations that the object supports (e.g., "does it have a\n' |
| 5921 | 'length?") and also defines the possible values for objects of ' |
| 5922 | 'that\n' |
| 5923 | 'type. The "type()" function returns an object\'s type (which ' |
| 5924 | 'is an\n' |
| 5925 | 'object itself). The *value* of some objects can change. ' |
| 5926 | 'Objects\n' |
| 5927 | 'whose value can change are said to be *mutable*; objects whose ' |
| 5928 | 'value\n' |
| 5929 | 'is unchangeable once they are created are called *immutable*. ' |
| 5930 | '(The\n' |
| 5931 | 'value of an immutable container object that contains a ' |
| 5932 | 'reference to a\n' |
| 5933 | "mutable object can change when the latter's value is changed; " |
| 5934 | 'however\n' |
| 5935 | 'the container is still considered immutable, because the ' |
| 5936 | 'collection of\n' |
| 5937 | 'objects it contains cannot be changed. So, immutability is ' |
| 5938 | 'not\n' |
| 5939 | 'strictly the same as having an unchangeable value, it is more ' |
| 5940 | 'subtle.)\n' |
| 5941 | "An object's mutability is determined by its type; for " |
| 5942 | 'instance,\n' |
| 5943 | 'numbers, strings and tuples are immutable, while dictionaries ' |
| 5944 | 'and\n' |
| 5945 | 'lists are mutable.\n' |
| 5946 | '\n' |
| 5947 | 'Objects are never explicitly destroyed; however, when they ' |
| 5948 | 'become\n' |
| 5949 | 'unreachable they may be garbage-collected. An implementation ' |
| 5950 | 'is\n' |
| 5951 | 'allowed to postpone garbage collection or omit it altogether ' |
| 5952 | '--- it is\n' |
| 5953 | 'a matter of implementation quality how garbage collection is\n' |
| 5954 | 'implemented, as long as no objects are collected that are ' |
| 5955 | 'still\n' |
| 5956 | 'reachable.\n' |
| 5957 | '\n' |
| 5958 | '**CPython implementation detail:** CPython currently uses a ' |
| 5959 | 'reference-\n' |
| 5960 | 'counting scheme with (optional) delayed detection of cyclically ' |
| 5961 | 'linked\n' |
| 5962 | 'garbage, which collects most objects as soon as they become\n' |
| 5963 | 'unreachable, but is not guaranteed to collect garbage ' |
| 5964 | 'containing\n' |
| 5965 | 'circular references. See the documentation of the "gc" module ' |
| 5966 | 'for\n' |
| 5967 | 'information on controlling the collection of cyclic garbage. ' |
| 5968 | 'Other\n' |
| 5969 | 'implementations act differently and CPython may change. Do not ' |
| 5970 | 'depend\n' |
| 5971 | 'on immediate finalization of objects when they become ' |
| 5972 | 'unreachable (ex:\n' |
| 5973 | 'always close files).\n' |
| 5974 | '\n' |
| 5975 | "Note that the use of the implementation's tracing or debugging\n" |
| 5976 | 'facilities may keep objects alive that would normally be ' |
| 5977 | 'collectable.\n' |
| 5978 | 'Also note that catching an exception with a ' |
| 5979 | '\'"try"..."except"\'\n' |
| 5980 | 'statement may keep objects alive.\n' |
| 5981 | '\n' |
| 5982 | 'Some objects contain references to "external" resources such as ' |
| 5983 | 'open\n' |
| 5984 | 'files or windows. It is understood that these resources are ' |
| 5985 | 'freed\n' |
| 5986 | 'when the object is garbage-collected, but since garbage ' |
| 5987 | 'collection is\n' |
| 5988 | 'not guaranteed to happen, such objects also provide an explicit ' |
| 5989 | 'way to\n' |
| 5990 | 'release the external resource, usually a "close()" method. ' |
| 5991 | 'Programs\n' |
| 5992 | 'are strongly recommended to explicitly close such objects. ' |
| 5993 | 'The\n' |
| 5994 | '\'"try"..."finally"\' statement provides a convenient way to do ' |
| 5995 | 'this.\n' |
| 5996 | '\n' |
| 5997 | 'Some objects contain references to other objects; these are ' |
| 5998 | 'called\n' |
| 5999 | '*containers*. Examples of containers are tuples, lists and\n' |
| 6000 | "dictionaries. The references are part of a container's value. " |
| 6001 | 'In\n' |
| 6002 | 'most cases, when we talk about the value of a container, we ' |
| 6003 | 'imply the\n' |
| 6004 | 'values, not the identities of the contained objects; however, ' |
| 6005 | 'when we\n' |
| 6006 | 'talk about the mutability of a container, only the identities ' |
| 6007 | 'of the\n' |
| 6008 | 'immediately contained objects are implied. So, if an ' |
| 6009 | 'immutable\n' |
| 6010 | 'container (like a tuple) contains a reference to a mutable ' |
| 6011 | 'object, its\n' |
| 6012 | 'value changes if that mutable object is changed.\n' |
| 6013 | '\n' |
| 6014 | 'Types affect almost all aspects of object behavior. Even the\n' |
| 6015 | 'importance of object identity is affected in some sense: for ' |
| 6016 | 'immutable\n' |
| 6017 | 'types, operations that compute new values may actually return ' |
| 6018 | 'a\n' |
| 6019 | 'reference to any existing object with the same type and value, ' |
| 6020 | 'while\n' |
| 6021 | 'for mutable objects this is not allowed. E.g., after "a = 1; b ' |
| 6022 | '= 1",\n' |
| 6023 | '"a" and "b" may or may not refer to the same object with the ' |
| 6024 | 'value\n' |
| 6025 | 'one, depending on the implementation, but after "c = []; d = ' |
| 6026 | '[]", "c"\n' |
| 6027 | 'and "d" are guaranteed to refer to two different, unique, ' |
| 6028 | 'newly\n' |
| 6029 | 'created empty lists. (Note that "c = d = []" assigns the same ' |
| 6030 | 'object\n' |
| 6031 | 'to both "c" and "d".)\n', |
| 6032 | 'operator-summary': '\n' |
| 6033 | 'Operator precedence\n' |
| 6034 | '*******************\n' |
| 6035 | '\n' |
| 6036 | 'The following table summarizes the operator ' |
| 6037 | 'precedences in Python,\n' |
| 6038 | 'from lowest precedence (least binding) to highest ' |
| 6039 | 'precedence (most\n' |
| 6040 | 'binding). Operators in the same box have the same ' |
| 6041 | 'precedence. Unless\n' |
| 6042 | 'the syntax is explicitly given, operators are binary. ' |
| 6043 | 'Operators in\n' |
| 6044 | 'the same box group left to right (except for ' |
| 6045 | 'comparisons, including\n' |
| 6046 | 'tests, which all have the same precedence and chain ' |
| 6047 | 'from left to right\n' |
| 6048 | '--- see section Comparisons --- and exponentiation, ' |
| 6049 | 'which groups from\n' |
| 6050 | 'right to left).\n' |
| 6051 | '\n' |
| 6052 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6053 | '| Operator | ' |
| 6054 | 'Description |\n' |
| 6055 | '+=================================================+=======================================+\n' |
| 6056 | '| "lambda" | ' |
| 6057 | 'Lambda expression |\n' |
| 6058 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6059 | '| "if" -- "else" | ' |
| 6060 | 'Conditional expression |\n' |
| 6061 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6062 | '| "or" | ' |
| 6063 | 'Boolean OR |\n' |
| 6064 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6065 | '| "and" | ' |
| 6066 | 'Boolean AND |\n' |
| 6067 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6068 | '| "not" "x" | ' |
| 6069 | 'Boolean NOT |\n' |
| 6070 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6071 | '| "in", "not in", "is", "is not", "<", "<=", ">", | ' |
| 6072 | 'Comparisons, including membership |\n' |
| 6073 | '| ">=", "<>", "!=", "==" | ' |
| 6074 | 'tests and identity tests |\n' |
| 6075 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6076 | '| "|" | ' |
| 6077 | 'Bitwise OR |\n' |
| 6078 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6079 | '| "^" | ' |
| 6080 | 'Bitwise XOR |\n' |
| 6081 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6082 | '| "&" | ' |
| 6083 | 'Bitwise AND |\n' |
| 6084 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6085 | '| "<<", ">>" | ' |
| 6086 | 'Shifts |\n' |
| 6087 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6088 | '| "+", "-" | ' |
| 6089 | 'Addition and subtraction |\n' |
| 6090 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6091 | '| "*", "/", "//", "%" | ' |
| 6092 | 'Multiplication, division, remainder |\n' |
| 6093 | '| | ' |
| 6094 | '[8] |\n' |
| 6095 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6096 | '| "+x", "-x", "~x" | ' |
| 6097 | 'Positive, negative, bitwise NOT |\n' |
| 6098 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6099 | '| "**" | ' |
| 6100 | 'Exponentiation [9] |\n' |
| 6101 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6102 | '| "x[index]", "x[index:index]", | ' |
| 6103 | 'Subscription, slicing, call, |\n' |
| 6104 | '| "x(arguments...)", "x.attribute" | ' |
| 6105 | 'attribute reference |\n' |
| 6106 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6107 | '| "(expressions...)", "[expressions...]", "{key: | ' |
| 6108 | 'Binding or tuple display, list |\n' |
| 6109 | '| value...}", "`expressions...`" | ' |
| 6110 | 'display, dictionary display, string |\n' |
| 6111 | '| | ' |
| 6112 | 'conversion |\n' |
| 6113 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6114 | '\n' |
| 6115 | '-[ Footnotes ]-\n' |
| 6116 | '\n' |
| 6117 | '[1] In Python 2.3 and later releases, a list ' |
| 6118 | 'comprehension "leaks"\n' |
| 6119 | ' the control variables of each "for" it contains ' |
| 6120 | 'into the\n' |
| 6121 | ' containing scope. However, this behavior is ' |
| 6122 | 'deprecated, and\n' |
| 6123 | ' relying on it will not work in Python 3.\n' |
| 6124 | '\n' |
| 6125 | '[2] While "abs(x%y) < abs(y)" is true mathematically, ' |
| 6126 | 'for floats\n' |
| 6127 | ' it may not be true numerically due to roundoff. ' |
| 6128 | 'For example, and\n' |
| 6129 | ' assuming a platform on which a Python float is an ' |
| 6130 | 'IEEE 754 double-\n' |
| 6131 | ' precision number, in order that "-1e-100 % 1e100" ' |
| 6132 | 'have the same\n' |
| 6133 | ' sign as "1e100", the computed result is "-1e-100 + ' |
| 6134 | '1e100", which\n' |
| 6135 | ' is numerically exactly equal to "1e100". The ' |
| 6136 | 'function\n' |
| 6137 | ' "math.fmod()" returns a result whose sign matches ' |
| 6138 | 'the sign of the\n' |
| 6139 | ' first argument instead, and so returns "-1e-100" ' |
| 6140 | 'in this case.\n' |
| 6141 | ' Which approach is more appropriate depends on the ' |
| 6142 | 'application.\n' |
| 6143 | '\n' |
| 6144 | '[3] If x is very close to an exact integer multiple of ' |
| 6145 | "y, it's\n" |
| 6146 | ' possible for "floor(x/y)" to be one larger than ' |
| 6147 | '"(x-x%y)/y" due to\n' |
| 6148 | ' rounding. In such cases, Python returns the ' |
| 6149 | 'latter result, in\n' |
| 6150 | ' order to preserve that "divmod(x,y)[0] * y + x % ' |
| 6151 | 'y" be very close\n' |
| 6152 | ' to "x".\n' |
| 6153 | '\n' |
| 6154 | '[4] While comparisons between unicode strings make ' |
| 6155 | 'sense at the\n' |
| 6156 | ' byte level, they may be counter-intuitive to ' |
| 6157 | 'users. For example,\n' |
| 6158 | ' the strings "u"\\u00C7"" and "u"\\u0043\\u0327"" ' |
| 6159 | 'compare differently,\n' |
| 6160 | ' even though they both represent the same unicode ' |
| 6161 | 'character (LATIN\n' |
| 6162 | ' CAPITAL LETTER C WITH CEDILLA). To compare strings ' |
| 6163 | 'in a human\n' |
| 6164 | ' recognizable way, compare using ' |
| 6165 | '"unicodedata.normalize()".\n' |
| 6166 | '\n' |
| 6167 | '[5] The implementation computes this efficiently, ' |
| 6168 | 'without\n' |
| 6169 | ' constructing lists or sorting.\n' |
| 6170 | '\n' |
| 6171 | '[6] Earlier versions of Python used lexicographic ' |
| 6172 | 'comparison of\n' |
| 6173 | ' the sorted (key, value) lists, but this was very ' |
| 6174 | 'expensive for the\n' |
| 6175 | ' common case of comparing for equality. An even ' |
| 6176 | 'earlier version of\n' |
| 6177 | ' Python compared dictionaries by identity only, but ' |
| 6178 | 'this caused\n' |
| 6179 | ' surprises because people expected to be able to ' |
| 6180 | 'test a dictionary\n' |
| 6181 | ' for emptiness by comparing it to "{}".\n' |
| 6182 | '\n' |
| 6183 | '[7] Due to automatic garbage-collection, free lists, ' |
| 6184 | 'and the\n' |
| 6185 | ' dynamic nature of descriptors, you may notice ' |
| 6186 | 'seemingly unusual\n' |
| 6187 | ' behaviour in certain uses of the "is" operator, ' |
| 6188 | 'like those\n' |
| 6189 | ' involving comparisons between instance methods, or ' |
| 6190 | 'constants.\n' |
| 6191 | ' Check their documentation for more info.\n' |
| 6192 | '\n' |
| 6193 | '[8] The "%" operator is also used for string ' |
| 6194 | 'formatting; the same\n' |
| 6195 | ' precedence applies.\n' |
| 6196 | '\n' |
| 6197 | '[9] The power operator "**" binds less tightly than an ' |
| 6198 | 'arithmetic\n' |
| 6199 | ' or bitwise unary operator on its right, that is, ' |
| 6200 | '"2**-1" is "0.5".\n', |
| 6201 | 'pass': '\n' |
| 6202 | 'The "pass" statement\n' |
| 6203 | '********************\n' |
| 6204 | '\n' |
| 6205 | ' pass_stmt ::= "pass"\n' |
| 6206 | '\n' |
| 6207 | '"pass" is a null operation --- when it is executed, nothing ' |
| 6208 | 'happens.\n' |
| 6209 | 'It is useful as a placeholder when a statement is required\n' |
| 6210 | 'syntactically, but no code needs to be executed, for example:\n' |
| 6211 | '\n' |
| 6212 | ' def f(arg): pass # a function that does nothing (yet)\n' |
| 6213 | '\n' |
| 6214 | ' class C: pass # a class with no methods (yet)\n', |
| 6215 | 'power': '\n' |
| 6216 | 'The power operator\n' |
| 6217 | '******************\n' |
| 6218 | '\n' |
| 6219 | 'The power operator binds more tightly than unary operators on ' |
| 6220 | 'its\n' |
| 6221 | 'left; it binds less tightly than unary operators on its right. ' |
| 6222 | 'The\n' |
| 6223 | 'syntax is:\n' |
| 6224 | '\n' |
| 6225 | ' power ::= primary ["**" u_expr]\n' |
| 6226 | '\n' |
| 6227 | 'Thus, in an unparenthesized sequence of power and unary ' |
| 6228 | 'operators, the\n' |
| 6229 | 'operators are evaluated from right to left (this does not ' |
| 6230 | 'constrain\n' |
| 6231 | 'the evaluation order for the operands): "-1**2" results in "-1".\n' |
| 6232 | '\n' |
| 6233 | 'The power operator has the same semantics as the built-in ' |
| 6234 | '"pow()"\n' |
| 6235 | 'function, when called with two arguments: it yields its left ' |
| 6236 | 'argument\n' |
| 6237 | 'raised to the power of its right argument. The numeric arguments ' |
| 6238 | 'are\n' |
| 6239 | 'first converted to a common type. The result type is that of ' |
| 6240 | 'the\n' |
| 6241 | 'arguments after coercion.\n' |
| 6242 | '\n' |
| 6243 | 'With mixed operand types, the coercion rules for binary ' |
| 6244 | 'arithmetic\n' |
| 6245 | 'operators apply. For int and long int operands, the result has ' |
| 6246 | 'the\n' |
| 6247 | 'same type as the operands (after coercion) unless the second ' |
| 6248 | 'argument\n' |
| 6249 | 'is negative; in that case, all arguments are converted to float ' |
| 6250 | 'and a\n' |
| 6251 | 'float result is delivered. For example, "10**2" returns "100", ' |
| 6252 | 'but\n' |
| 6253 | '"10**-2" returns "0.01". (This last feature was added in Python ' |
| 6254 | '2.2.\n' |
| 6255 | 'In Python 2.1 and before, if both arguments were of integer types ' |
| 6256 | 'and\n' |
| 6257 | 'the second argument was negative, an exception was raised).\n' |
| 6258 | '\n' |
| 6259 | 'Raising "0.0" to a negative power results in a ' |
| 6260 | '"ZeroDivisionError".\n' |
| 6261 | 'Raising a negative number to a fractional power results in a\n' |
| 6262 | '"ValueError".\n', |
| 6263 | 'print': '\n' |
| 6264 | 'The "print" statement\n' |
| 6265 | '*********************\n' |
| 6266 | '\n' |
| 6267 | ' print_stmt ::= "print" ([expression ("," expression)* [","]]\n' |
| 6268 | ' | ">>" expression [("," expression)+ [","]])\n' |
| 6269 | '\n' |
| 6270 | '"print" evaluates each expression in turn and writes the ' |
| 6271 | 'resulting\n' |
| 6272 | 'object to standard output (see below). If an object is not a ' |
| 6273 | 'string,\n' |
| 6274 | 'it is first converted to a string using the rules for string\n' |
| 6275 | 'conversions. The (resulting or original) string is then ' |
| 6276 | 'written. A\n' |
| 6277 | 'space is written before each object is (converted and) written, ' |
| 6278 | 'unless\n' |
| 6279 | 'the output system believes it is positioned at the beginning of ' |
| 6280 | 'a\n' |
| 6281 | 'line. This is the case (1) when no characters have yet been ' |
| 6282 | 'written\n' |
| 6283 | 'to standard output, (2) when the last character written to ' |
| 6284 | 'standard\n' |
| 6285 | 'output is a whitespace character except "\' \'", or (3) when the ' |
| 6286 | 'last\n' |
| 6287 | 'write operation on standard output was not a "print" statement. ' |
| 6288 | '(In\n' |
| 6289 | 'some cases it may be functional to write an empty string to ' |
| 6290 | 'standard\n' |
| 6291 | 'output for this reason.)\n' |
| 6292 | '\n' |
| 6293 | 'Note: Objects which act like file objects but which are not the\n' |
| 6294 | ' built-in file objects often do not properly emulate this aspect ' |
| 6295 | 'of\n' |
| 6296 | " the file object's behavior, so it is best not to rely on this.\n" |
| 6297 | '\n' |
| 6298 | 'A "\'\\n\'" character is written at the end, unless the "print" ' |
| 6299 | 'statement\n' |
| 6300 | 'ends with a comma. This is the only action if the statement ' |
| 6301 | 'contains\n' |
| 6302 | 'just the keyword "print".\n' |
| 6303 | '\n' |
| 6304 | 'Standard output is defined as the file object named "stdout" in ' |
| 6305 | 'the\n' |
| 6306 | 'built-in module "sys". If no such object exists, or if it does ' |
| 6307 | 'not\n' |
| 6308 | 'have a "write()" method, a "RuntimeError" exception is raised.\n' |
| 6309 | '\n' |
| 6310 | '"print" also has an extended form, defined by the second portion ' |
| 6311 | 'of\n' |
| 6312 | 'the syntax described above. This form is sometimes referred to ' |
| 6313 | 'as\n' |
| 6314 | '""print" chevron." In this form, the first expression after the ' |
| 6315 | '">>"\n' |
| 6316 | 'must evaluate to a "file-like" object, specifically an object ' |
| 6317 | 'that has\n' |
| 6318 | 'a "write()" method as described above. With this extended form, ' |
| 6319 | 'the\n' |
| 6320 | 'subsequent expressions are printed to this file object. If the ' |
| 6321 | 'first\n' |
| 6322 | 'expression evaluates to "None", then "sys.stdout" is used as the ' |
| 6323 | 'file\n' |
| 6324 | 'for output.\n', |
| 6325 | 'raise': '\n' |
| 6326 | 'The "raise" statement\n' |
| 6327 | '*********************\n' |
| 6328 | '\n' |
| 6329 | ' raise_stmt ::= "raise" [expression ["," expression ["," ' |
| 6330 | 'expression]]]\n' |
| 6331 | '\n' |
| 6332 | 'If no expressions are present, "raise" re-raises the last ' |
| 6333 | 'exception\n' |
| 6334 | 'that was active in the current scope. If no exception is active ' |
| 6335 | 'in\n' |
| 6336 | 'the current scope, a "TypeError" exception is raised indicating ' |
| 6337 | 'that\n' |
| 6338 | 'this is an error (if running under IDLE, a "Queue.Empty" ' |
| 6339 | 'exception is\n' |
| 6340 | 'raised instead).\n' |
| 6341 | '\n' |
| 6342 | 'Otherwise, "raise" evaluates the expressions to get three ' |
| 6343 | 'objects,\n' |
| 6344 | 'using "None" as the value of omitted expressions. The first two\n' |
| 6345 | 'objects are used to determine the *type* and *value* of the ' |
| 6346 | 'exception.\n' |
| 6347 | '\n' |
| 6348 | 'If the first object is an instance, the type of the exception is ' |
| 6349 | 'the\n' |
| 6350 | 'class of the instance, the instance itself is the value, and the\n' |
| 6351 | 'second object must be "None".\n' |
| 6352 | '\n' |
| 6353 | 'If the first object is a class, it becomes the type of the ' |
| 6354 | 'exception.\n' |
| 6355 | 'The second object is used to determine the exception value: If it ' |
| 6356 | 'is\n' |
| 6357 | 'an instance of the class, the instance becomes the exception ' |
| 6358 | 'value. If\n' |
| 6359 | 'the second object is a tuple, it is used as the argument list for ' |
| 6360 | 'the\n' |
| 6361 | 'class constructor; if it is "None", an empty argument list is ' |
| 6362 | 'used,\n' |
| 6363 | 'and any other object is treated as a single argument to the\n' |
| 6364 | 'constructor. The instance so created by calling the constructor ' |
| 6365 | 'is\n' |
| 6366 | 'used as the exception value.\n' |
| 6367 | '\n' |
| 6368 | 'If a third object is present and not "None", it must be a ' |
| 6369 | 'traceback\n' |
| 6370 | 'object (see section The standard type hierarchy), and it is\n' |
| 6371 | 'substituted instead of the current location as the place where ' |
| 6372 | 'the\n' |
| 6373 | 'exception occurred. If the third object is present and not a\n' |
| 6374 | 'traceback object or "None", a "TypeError" exception is raised. ' |
| 6375 | 'The\n' |
| 6376 | 'three-expression form of "raise" is useful to re-raise an ' |
| 6377 | 'exception\n' |
| 6378 | 'transparently in an except clause, but "raise" with no ' |
| 6379 | 'expressions\n' |
| 6380 | 'should be preferred if the exception to be re-raised was the ' |
| 6381 | 'most\n' |
| 6382 | 'recently active exception in the current scope.\n' |
| 6383 | '\n' |
| 6384 | 'Additional information on exceptions can be found in section\n' |
| 6385 | 'Exceptions, and information about handling exceptions is in ' |
| 6386 | 'section\n' |
| 6387 | 'The try statement.\n', |
| 6388 | 'return': '\n' |
| 6389 | 'The "return" statement\n' |
| 6390 | '**********************\n' |
| 6391 | '\n' |
| 6392 | ' return_stmt ::= "return" [expression_list]\n' |
| 6393 | '\n' |
| 6394 | '"return" may only occur syntactically nested in a function ' |
| 6395 | 'definition,\n' |
| 6396 | 'not within a nested class definition.\n' |
| 6397 | '\n' |
| 6398 | 'If an expression list is present, it is evaluated, else "None" ' |
| 6399 | 'is\n' |
| 6400 | 'substituted.\n' |
| 6401 | '\n' |
| 6402 | '"return" leaves the current function call with the expression ' |
| 6403 | 'list (or\n' |
| 6404 | '"None") as return value.\n' |
| 6405 | '\n' |
| 6406 | 'When "return" passes control out of a "try" statement with a ' |
| 6407 | '"finally"\n' |
| 6408 | 'clause, that "finally" clause is executed before really leaving ' |
| 6409 | 'the\n' |
| 6410 | 'function.\n' |
| 6411 | '\n' |
| 6412 | 'In a generator function, the "return" statement is not allowed ' |
| 6413 | 'to\n' |
| 6414 | 'include an "expression_list". In that context, a bare "return"\n' |
| 6415 | 'indicates that the generator is done and will cause ' |
| 6416 | '"StopIteration" to\n' |
| 6417 | 'be raised.\n', |
| 6418 | 'sequence-types': '\n' |
| 6419 | 'Emulating container types\n' |
| 6420 | '*************************\n' |
| 6421 | '\n' |
| 6422 | 'The following methods can be defined to implement ' |
| 6423 | 'container objects.\n' |
| 6424 | 'Containers usually are sequences (such as lists or ' |
| 6425 | 'tuples) or mappings\n' |
| 6426 | '(like dictionaries), but can represent other containers ' |
| 6427 | 'as well. The\n' |
| 6428 | 'first set of methods is used either to emulate a ' |
| 6429 | 'sequence or to\n' |
| 6430 | 'emulate a mapping; the difference is that for a ' |
| 6431 | 'sequence, the\n' |
| 6432 | 'allowable keys should be the integers *k* for which "0 ' |
| 6433 | '<= k < N" where\n' |
| 6434 | '*N* is the length of the sequence, or slice objects, ' |
| 6435 | 'which define a\n' |
| 6436 | 'range of items. (For backwards compatibility, the ' |
| 6437 | 'method\n' |
| 6438 | '"__getslice__()" (see below) can also be defined to ' |
| 6439 | 'handle simple, but\n' |
| 6440 | 'not extended slices.) It is also recommended that ' |
| 6441 | 'mappings provide the\n' |
| 6442 | 'methods "keys()", "values()", "items()", "has_key()", ' |
| 6443 | '"get()",\n' |
| 6444 | '"clear()", "setdefault()", "iterkeys()", ' |
| 6445 | '"itervalues()",\n' |
| 6446 | '"iteritems()", "pop()", "popitem()", "copy()", and ' |
| 6447 | '"update()" behaving\n' |
| 6448 | "similar to those for Python's standard dictionary " |
| 6449 | 'objects. The\n' |
| 6450 | '"UserDict" module provides a "DictMixin" class to help ' |
| 6451 | 'create those\n' |
| 6452 | 'methods from a base set of "__getitem__()", ' |
| 6453 | '"__setitem__()",\n' |
| 6454 | '"__delitem__()", and "keys()". Mutable sequences should ' |
| 6455 | 'provide\n' |
| 6456 | 'methods "append()", "count()", "index()", "extend()", ' |
| 6457 | '"insert()",\n' |
| 6458 | '"pop()", "remove()", "reverse()" and "sort()", like ' |
| 6459 | 'Python standard\n' |
| 6460 | 'list objects. Finally, sequence types should implement ' |
| 6461 | 'addition\n' |
| 6462 | '(meaning concatenation) and multiplication (meaning ' |
| 6463 | 'repetition) by\n' |
| 6464 | 'defining the methods "__add__()", "__radd__()", ' |
| 6465 | '"__iadd__()",\n' |
| 6466 | '"__mul__()", "__rmul__()" and "__imul__()" described ' |
| 6467 | 'below; they\n' |
| 6468 | 'should not define "__coerce__()" or other numerical ' |
| 6469 | 'operators. It is\n' |
| 6470 | 'recommended that both mappings and sequences implement ' |
| 6471 | 'the\n' |
| 6472 | '"__contains__()" method to allow efficient use of the ' |
| 6473 | '"in" operator;\n' |
| 6474 | 'for mappings, "in" should be equivalent of "has_key()"; ' |
| 6475 | 'for sequences,\n' |
| 6476 | 'it should search through the values. It is further ' |
| 6477 | 'recommended that\n' |
| 6478 | 'both mappings and sequences implement the "__iter__()" ' |
| 6479 | 'method to allow\n' |
| 6480 | 'efficient iteration through the container; for mappings, ' |
| 6481 | '"__iter__()"\n' |
| 6482 | 'should be the same as "iterkeys()"; for sequences, it ' |
| 6483 | 'should iterate\n' |
| 6484 | 'through the values.\n' |
| 6485 | '\n' |
| 6486 | 'object.__len__(self)\n' |
| 6487 | '\n' |
| 6488 | ' Called to implement the built-in function "len()". ' |
| 6489 | 'Should return\n' |
| 6490 | ' the length of the object, an integer ">=" 0. Also, ' |
| 6491 | 'an object that\n' |
| 6492 | ' doesn\'t define a "__nonzero__()" method and whose ' |
| 6493 | '"__len__()"\n' |
| 6494 | ' method returns zero is considered to be false in a ' |
| 6495 | 'Boolean context.\n' |
| 6496 | '\n' |
| 6497 | 'object.__getitem__(self, key)\n' |
| 6498 | '\n' |
| 6499 | ' Called to implement evaluation of "self[key]". For ' |
| 6500 | 'sequence types,\n' |
| 6501 | ' the accepted keys should be integers and slice ' |
| 6502 | 'objects. Note that\n' |
| 6503 | ' the special interpretation of negative indexes (if ' |
| 6504 | 'the class wishes\n' |
| 6505 | ' to emulate a sequence type) is up to the ' |
| 6506 | '"__getitem__()" method. If\n' |
| 6507 | ' *key* is of an inappropriate type, "TypeError" may be ' |
| 6508 | 'raised; if of\n' |
| 6509 | ' a value outside the set of indexes for the sequence ' |
| 6510 | '(after any\n' |
| 6511 | ' special interpretation of negative values), ' |
| 6512 | '"IndexError" should be\n' |
| 6513 | ' raised. For mapping types, if *key* is missing (not ' |
| 6514 | 'in the\n' |
| 6515 | ' container), "KeyError" should be raised.\n' |
| 6516 | '\n' |
| 6517 | ' Note: "for" loops expect that an "IndexError" will be ' |
| 6518 | 'raised for\n' |
| 6519 | ' illegal indexes to allow proper detection of the ' |
| 6520 | 'end of the\n' |
| 6521 | ' sequence.\n' |
| 6522 | '\n' |
| 6523 | 'object.__missing__(self, key)\n' |
| 6524 | '\n' |
| 6525 | ' Called by "dict"."__getitem__()" to implement ' |
| 6526 | '"self[key]" for dict\n' |
| 6527 | ' subclasses when key is not in the dictionary.\n' |
| 6528 | '\n' |
| 6529 | 'object.__setitem__(self, key, value)\n' |
| 6530 | '\n' |
| 6531 | ' Called to implement assignment to "self[key]". Same ' |
| 6532 | 'note as for\n' |
| 6533 | ' "__getitem__()". This should only be implemented for ' |
| 6534 | 'mappings if\n' |
| 6535 | ' the objects support changes to the values for keys, ' |
| 6536 | 'or if new keys\n' |
| 6537 | ' can be added, or for sequences if elements can be ' |
| 6538 | 'replaced. The\n' |
| 6539 | ' same exceptions should be raised for improper *key* ' |
| 6540 | 'values as for\n' |
| 6541 | ' the "__getitem__()" method.\n' |
| 6542 | '\n' |
| 6543 | 'object.__delitem__(self, key)\n' |
| 6544 | '\n' |
| 6545 | ' Called to implement deletion of "self[key]". Same ' |
| 6546 | 'note as for\n' |
| 6547 | ' "__getitem__()". This should only be implemented for ' |
| 6548 | 'mappings if\n' |
| 6549 | ' the objects support removal of keys, or for sequences ' |
| 6550 | 'if elements\n' |
| 6551 | ' can be removed from the sequence. The same ' |
| 6552 | 'exceptions should be\n' |
| 6553 | ' raised for improper *key* values as for the ' |
| 6554 | '"__getitem__()" method.\n' |
| 6555 | '\n' |
| 6556 | 'object.__iter__(self)\n' |
| 6557 | '\n' |
| 6558 | ' This method is called when an iterator is required ' |
| 6559 | 'for a container.\n' |
| 6560 | ' This method should return a new iterator object that ' |
| 6561 | 'can iterate\n' |
| 6562 | ' over all the objects in the container. For mappings, ' |
| 6563 | 'it should\n' |
| 6564 | ' iterate over the keys of the container, and should ' |
| 6565 | 'also be made\n' |
| 6566 | ' available as the method "iterkeys()".\n' |
| 6567 | '\n' |
| 6568 | ' Iterator objects also need to implement this method; ' |
| 6569 | 'they are\n' |
| 6570 | ' required to return themselves. For more information ' |
| 6571 | 'on iterator\n' |
| 6572 | ' objects, see Iterator Types.\n' |
| 6573 | '\n' |
| 6574 | 'object.__reversed__(self)\n' |
| 6575 | '\n' |
| 6576 | ' Called (if present) by the "reversed()" built-in to ' |
| 6577 | 'implement\n' |
| 6578 | ' reverse iteration. It should return a new iterator ' |
| 6579 | 'object that\n' |
| 6580 | ' iterates over all the objects in the container in ' |
| 6581 | 'reverse order.\n' |
| 6582 | '\n' |
| 6583 | ' If the "__reversed__()" method is not provided, the ' |
| 6584 | '"reversed()"\n' |
| 6585 | ' built-in will fall back to using the sequence ' |
| 6586 | 'protocol ("__len__()"\n' |
| 6587 | ' and "__getitem__()"). Objects that support the ' |
| 6588 | 'sequence protocol\n' |
| 6589 | ' should only provide "__reversed__()" if they can ' |
| 6590 | 'provide an\n' |
| 6591 | ' implementation that is more efficient than the one ' |
| 6592 | 'provided by\n' |
| 6593 | ' "reversed()".\n' |
| 6594 | '\n' |
| 6595 | ' New in version 2.6.\n' |
| 6596 | '\n' |
| 6597 | 'The membership test operators ("in" and "not in") are ' |
| 6598 | 'normally\n' |
| 6599 | 'implemented as an iteration through a sequence. ' |
| 6600 | 'However, container\n' |
| 6601 | 'objects can supply the following special method with a ' |
| 6602 | 'more efficient\n' |
| 6603 | 'implementation, which also does not require the object ' |
| 6604 | 'be a sequence.\n' |
| 6605 | '\n' |
| 6606 | 'object.__contains__(self, item)\n' |
| 6607 | '\n' |
| 6608 | ' Called to implement membership test operators. ' |
| 6609 | 'Should return true\n' |
| 6610 | ' if *item* is in *self*, false otherwise. For mapping ' |
| 6611 | 'objects, this\n' |
| 6612 | ' should consider the keys of the mapping rather than ' |
| 6613 | 'the values or\n' |
| 6614 | ' the key-item pairs.\n' |
| 6615 | '\n' |
| 6616 | ' For objects that don\'t define "__contains__()", the ' |
| 6617 | 'membership test\n' |
| 6618 | ' first tries iteration via "__iter__()", then the old ' |
| 6619 | 'sequence\n' |
| 6620 | ' iteration protocol via "__getitem__()", see this ' |
| 6621 | 'section in the\n' |
| 6622 | ' language reference.\n', |
| 6623 | 'shifting': '\n' |
| 6624 | 'Shifting operations\n' |
| 6625 | '*******************\n' |
| 6626 | '\n' |
| 6627 | 'The shifting operations have lower priority than the ' |
| 6628 | 'arithmetic\n' |
| 6629 | 'operations:\n' |
| 6630 | '\n' |
| 6631 | ' shift_expr ::= a_expr | shift_expr ( "<<" | ">>" ) a_expr\n' |
| 6632 | '\n' |
| 6633 | 'These operators accept plain or long integers as arguments. ' |
| 6634 | 'The\n' |
| 6635 | 'arguments are converted to a common type. They shift the ' |
| 6636 | 'first\n' |
| 6637 | 'argument to the left or right by the number of bits given by ' |
| 6638 | 'the\n' |
| 6639 | 'second argument.\n' |
| 6640 | '\n' |
| 6641 | 'A right shift by *n* bits is defined as division by "pow(2, ' |
| 6642 | 'n)". A\n' |
| 6643 | 'left shift by *n* bits is defined as multiplication with ' |
| 6644 | '"pow(2, n)".\n' |
| 6645 | 'Negative shift counts raise a "ValueError" exception.\n' |
| 6646 | '\n' |
| 6647 | 'Note: In the current implementation, the right-hand operand ' |
| 6648 | 'is\n' |
| 6649 | ' required to be at most "sys.maxsize". If the right-hand ' |
| 6650 | 'operand is\n' |
| 6651 | ' larger than "sys.maxsize" an "OverflowError" exception is ' |
| 6652 | 'raised.\n', |
| 6653 | 'slicings': '\n' |
| 6654 | 'Slicings\n' |
| 6655 | '********\n' |
| 6656 | '\n' |
| 6657 | 'A slicing selects a range of items in a sequence object (e.g., ' |
| 6658 | 'a\n' |
| 6659 | 'string, tuple or list). Slicings may be used as expressions ' |
| 6660 | 'or as\n' |
| 6661 | 'targets in assignment or "del" statements. The syntax for a ' |
| 6662 | 'slicing:\n' |
| 6663 | '\n' |
| 6664 | ' slicing ::= simple_slicing | extended_slicing\n' |
| 6665 | ' simple_slicing ::= primary "[" short_slice "]"\n' |
| 6666 | ' extended_slicing ::= primary "[" slice_list "]"\n' |
| 6667 | ' slice_list ::= slice_item ("," slice_item)* [","]\n' |
| 6668 | ' slice_item ::= expression | proper_slice | ellipsis\n' |
| 6669 | ' proper_slice ::= short_slice | long_slice\n' |
| 6670 | ' short_slice ::= [lower_bound] ":" [upper_bound]\n' |
| 6671 | ' long_slice ::= short_slice ":" [stride]\n' |
| 6672 | ' lower_bound ::= expression\n' |
| 6673 | ' upper_bound ::= expression\n' |
| 6674 | ' stride ::= expression\n' |
| 6675 | ' ellipsis ::= "..."\n' |
| 6676 | '\n' |
| 6677 | 'There is ambiguity in the formal syntax here: anything that ' |
| 6678 | 'looks like\n' |
| 6679 | 'an expression list also looks like a slice list, so any ' |
| 6680 | 'subscription\n' |
| 6681 | 'can be interpreted as a slicing. Rather than further ' |
| 6682 | 'complicating the\n' |
| 6683 | 'syntax, this is disambiguated by defining that in this case ' |
| 6684 | 'the\n' |
| 6685 | 'interpretation as a subscription takes priority over the\n' |
| 6686 | 'interpretation as a slicing (this is the case if the slice ' |
| 6687 | 'list\n' |
| 6688 | 'contains no proper slice nor ellipses). Similarly, when the ' |
| 6689 | 'slice\n' |
| 6690 | 'list has exactly one short slice and no trailing comma, the\n' |
| 6691 | 'interpretation as a simple slicing takes priority over that as ' |
| 6692 | 'an\n' |
| 6693 | 'extended slicing.\n' |
| 6694 | '\n' |
| 6695 | 'The semantics for a simple slicing are as follows. The ' |
| 6696 | 'primary must\n' |
| 6697 | 'evaluate to a sequence object. The lower and upper bound ' |
| 6698 | 'expressions,\n' |
| 6699 | 'if present, must evaluate to plain integers; defaults are zero ' |
| 6700 | 'and the\n' |
| 6701 | '"sys.maxint", respectively. If either bound is negative, the\n' |
| 6702 | "sequence's length is added to it. The slicing now selects all " |
| 6703 | 'items\n' |
| 6704 | 'with index *k* such that "i <= k < j" where *i* and *j* are ' |
| 6705 | 'the\n' |
| 6706 | 'specified lower and upper bounds. This may be an empty ' |
| 6707 | 'sequence. It\n' |
| 6708 | 'is not an error if *i* or *j* lie outside the range of valid ' |
| 6709 | 'indexes\n' |
| 6710 | "(such items don't exist so they aren't selected).\n" |
| 6711 | '\n' |
| 6712 | 'The semantics for an extended slicing are as follows. The ' |
| 6713 | 'primary\n' |
| 6714 | 'must evaluate to a mapping object, and it is indexed with a ' |
| 6715 | 'key that\n' |
| 6716 | 'is constructed from the slice list, as follows. If the slice ' |
| 6717 | 'list\n' |
| 6718 | 'contains at least one comma, the key is a tuple containing ' |
| 6719 | 'the\n' |
| 6720 | 'conversion of the slice items; otherwise, the conversion of ' |
| 6721 | 'the lone\n' |
| 6722 | 'slice item is the key. The conversion of a slice item that is ' |
| 6723 | 'an\n' |
| 6724 | 'expression is that expression. The conversion of an ellipsis ' |
| 6725 | 'slice\n' |
| 6726 | 'item is the built-in "Ellipsis" object. The conversion of a ' |
| 6727 | 'proper\n' |
| 6728 | 'slice is a slice object (see section The standard type ' |
| 6729 | 'hierarchy)\n' |
| 6730 | 'whose "start", "stop" and "step" attributes are the values of ' |
| 6731 | 'the\n' |
| 6732 | 'expressions given as lower bound, upper bound and stride,\n' |
| 6733 | 'respectively, substituting "None" for missing expressions.\n', |
| 6734 | 'specialattrs': '\n' |
| 6735 | 'Special Attributes\n' |
| 6736 | '******************\n' |
| 6737 | '\n' |
| 6738 | 'The implementation adds a few special read-only attributes ' |
| 6739 | 'to several\n' |
| 6740 | 'object types, where they are relevant. Some of these are ' |
| 6741 | 'not reported\n' |
| 6742 | 'by the "dir()" built-in function.\n' |
| 6743 | '\n' |
| 6744 | 'object.__dict__\n' |
| 6745 | '\n' |
| 6746 | ' A dictionary or other mapping object used to store an ' |
| 6747 | "object's\n" |
| 6748 | ' (writable) attributes.\n' |
| 6749 | '\n' |
| 6750 | 'object.__methods__\n' |
| 6751 | '\n' |
| 6752 | ' Deprecated since version 2.2: Use the built-in function ' |
| 6753 | '"dir()" to\n' |
| 6754 | " get a list of an object's attributes. This attribute is " |
| 6755 | 'no longer\n' |
| 6756 | ' available.\n' |
| 6757 | '\n' |
| 6758 | 'object.__members__\n' |
| 6759 | '\n' |
| 6760 | ' Deprecated since version 2.2: Use the built-in function ' |
| 6761 | '"dir()" to\n' |
| 6762 | " get a list of an object's attributes. This attribute is " |
| 6763 | 'no longer\n' |
| 6764 | ' available.\n' |
| 6765 | '\n' |
| 6766 | 'instance.__class__\n' |
| 6767 | '\n' |
| 6768 | ' The class to which a class instance belongs.\n' |
| 6769 | '\n' |
| 6770 | 'class.__bases__\n' |
| 6771 | '\n' |
| 6772 | ' The tuple of base classes of a class object.\n' |
| 6773 | '\n' |
| 6774 | 'class.__name__\n' |
| 6775 | '\n' |
| 6776 | ' The name of the class or type.\n' |
| 6777 | '\n' |
| 6778 | 'The following attributes are only supported by *new-style ' |
| 6779 | 'class*es.\n' |
| 6780 | '\n' |
| 6781 | 'class.__mro__\n' |
| 6782 | '\n' |
| 6783 | ' This attribute is a tuple of classes that are ' |
| 6784 | 'considered when\n' |
| 6785 | ' looking for base classes during method resolution.\n' |
| 6786 | '\n' |
| 6787 | 'class.mro()\n' |
| 6788 | '\n' |
| 6789 | ' This method can be overridden by a metaclass to ' |
| 6790 | 'customize the\n' |
| 6791 | ' method resolution order for its instances. It is ' |
| 6792 | 'called at class\n' |
| 6793 | ' instantiation, and its result is stored in "__mro__".\n' |
| 6794 | '\n' |
| 6795 | 'class.__subclasses__()\n' |
| 6796 | '\n' |
| 6797 | ' Each new-style class keeps a list of weak references to ' |
| 6798 | 'its\n' |
| 6799 | ' immediate subclasses. This method returns a list of ' |
| 6800 | 'all those\n' |
| 6801 | ' references still alive. Example:\n' |
| 6802 | '\n' |
| 6803 | ' >>> int.__subclasses__()\n' |
| 6804 | " [<type 'bool'>]\n" |
| 6805 | '\n' |
| 6806 | '-[ Footnotes ]-\n' |
| 6807 | '\n' |
| 6808 | '[1] Additional information on these special methods may be ' |
| 6809 | 'found\n' |
| 6810 | ' in the Python Reference Manual (Basic customization).\n' |
| 6811 | '\n' |
| 6812 | '[2] As a consequence, the list "[1, 2]" is considered ' |
| 6813 | 'equal to\n' |
| 6814 | ' "[1.0, 2.0]", and similarly for tuples.\n' |
| 6815 | '\n' |
| 6816 | "[3] They must have since the parser can't tell the type of " |
| 6817 | 'the\n' |
| 6818 | ' operands.\n' |
| 6819 | '\n' |
| 6820 | '[4] Cased characters are those with general category ' |
| 6821 | 'property\n' |
| 6822 | ' being one of "Lu" (Letter, uppercase), "Ll" (Letter, ' |
| 6823 | 'lowercase),\n' |
| 6824 | ' or "Lt" (Letter, titlecase).\n' |
| 6825 | '\n' |
| 6826 | '[5] To format only a tuple you should therefore provide a\n' |
| 6827 | ' singleton tuple whose only element is the tuple to be ' |
| 6828 | 'formatted.\n' |
| 6829 | '\n' |
| 6830 | '[6] The advantage of leaving the newline on is that ' |
| 6831 | 'returning an\n' |
| 6832 | ' empty string is then an unambiguous EOF indication. ' |
| 6833 | 'It is also\n' |
| 6834 | ' possible (in cases where it might matter, for example, ' |
| 6835 | 'if you want\n' |
| 6836 | ' to make an exact copy of a file while scanning its ' |
| 6837 | 'lines) to tell\n' |
| 6838 | ' whether the last line of a file ended in a newline or ' |
| 6839 | 'not (yes\n' |
| 6840 | ' this happens!).\n', |
| 6841 | 'specialnames': '\n' |
| 6842 | 'Special method names\n' |
| 6843 | '********************\n' |
| 6844 | '\n' |
| 6845 | 'A class can implement certain operations that are invoked ' |
| 6846 | 'by special\n' |
| 6847 | 'syntax (such as arithmetic operations or subscripting and ' |
| 6848 | 'slicing) by\n' |
| 6849 | "defining methods with special names. This is Python's " |
| 6850 | 'approach to\n' |
| 6851 | '*operator overloading*, allowing classes to define their ' |
| 6852 | 'own behavior\n' |
| 6853 | 'with respect to language operators. For instance, if a ' |
| 6854 | 'class defines\n' |
| 6855 | 'a method named "__getitem__()", and "x" is an instance of ' |
| 6856 | 'this class,\n' |
| 6857 | 'then "x[i]" is roughly equivalent to "x.__getitem__(i)" ' |
| 6858 | 'for old-style\n' |
| 6859 | 'classes and "type(x).__getitem__(x, i)" for new-style ' |
| 6860 | 'classes. Except\n' |
| 6861 | 'where mentioned, attempts to execute an operation raise an ' |
| 6862 | 'exception\n' |
| 6863 | 'when no appropriate method is defined (typically ' |
| 6864 | '"AttributeError" or\n' |
| 6865 | '"TypeError").\n' |
| 6866 | '\n' |
| 6867 | 'When implementing a class that emulates any built-in type, ' |
| 6868 | 'it is\n' |
| 6869 | 'important that the emulation only be implemented to the ' |
| 6870 | 'degree that it\n' |
| 6871 | 'makes sense for the object being modelled. For example, ' |
| 6872 | 'some\n' |
| 6873 | 'sequences may work well with retrieval of individual ' |
| 6874 | 'elements, but\n' |
| 6875 | 'extracting a slice may not make sense. (One example of ' |
| 6876 | 'this is the\n' |
| 6877 | '"NodeList" interface in the W3C\'s Document Object ' |
| 6878 | 'Model.)\n' |
| 6879 | '\n' |
| 6880 | '\n' |
| 6881 | 'Basic customization\n' |
| 6882 | '===================\n' |
| 6883 | '\n' |
| 6884 | 'object.__new__(cls[, ...])\n' |
| 6885 | '\n' |
| 6886 | ' Called to create a new instance of class *cls*. ' |
| 6887 | '"__new__()" is a\n' |
| 6888 | ' static method (special-cased so you need not declare it ' |
| 6889 | 'as such)\n' |
| 6890 | ' that takes the class of which an instance was requested ' |
| 6891 | 'as its\n' |
| 6892 | ' first argument. The remaining arguments are those ' |
| 6893 | 'passed to the\n' |
| 6894 | ' object constructor expression (the call to the class). ' |
| 6895 | 'The return\n' |
| 6896 | ' value of "__new__()" should be the new object instance ' |
| 6897 | '(usually an\n' |
| 6898 | ' instance of *cls*).\n' |
| 6899 | '\n' |
| 6900 | ' Typical implementations create a new instance of the ' |
| 6901 | 'class by\n' |
| 6902 | ' invoking the superclass\'s "__new__()" method using\n' |
| 6903 | ' "super(currentclass, cls).__new__(cls[, ...])" with ' |
| 6904 | 'appropriate\n' |
| 6905 | ' arguments and then modifying the newly-created instance ' |
| 6906 | 'as\n' |
| 6907 | ' necessary before returning it.\n' |
| 6908 | '\n' |
| 6909 | ' If "__new__()" returns an instance of *cls*, then the ' |
| 6910 | 'new\n' |
| 6911 | ' instance\'s "__init__()" method will be invoked like\n' |
| 6912 | ' "__init__(self[, ...])", where *self* is the new ' |
| 6913 | 'instance and the\n' |
| 6914 | ' remaining arguments are the same as were passed to ' |
| 6915 | '"__new__()".\n' |
| 6916 | '\n' |
| 6917 | ' If "__new__()" does not return an instance of *cls*, ' |
| 6918 | 'then the new\n' |
| 6919 | ' instance\'s "__init__()" method will not be invoked.\n' |
| 6920 | '\n' |
| 6921 | ' "__new__()" is intended mainly to allow subclasses of ' |
| 6922 | 'immutable\n' |
| 6923 | ' types (like int, str, or tuple) to customize instance ' |
| 6924 | 'creation. It\n' |
| 6925 | ' is also commonly overridden in custom metaclasses in ' |
| 6926 | 'order to\n' |
| 6927 | ' customize class creation.\n' |
| 6928 | '\n' |
| 6929 | 'object.__init__(self[, ...])\n' |
| 6930 | '\n' |
| 6931 | ' Called after the instance has been created (by ' |
| 6932 | '"__new__()"), but\n' |
| 6933 | ' before it is returned to the caller. The arguments are ' |
| 6934 | 'those\n' |
| 6935 | ' passed to the class constructor expression. If a base ' |
| 6936 | 'class has an\n' |
| 6937 | ' "__init__()" method, the derived class\'s "__init__()" ' |
| 6938 | 'method, if\n' |
| 6939 | ' any, must explicitly call it to ensure proper ' |
| 6940 | 'initialization of the\n' |
| 6941 | ' base class part of the instance; for example:\n' |
| 6942 | ' "BaseClass.__init__(self, [args...])".\n' |
| 6943 | '\n' |
| 6944 | ' Because "__new__()" and "__init__()" work together in ' |
| 6945 | 'constructing\n' |
| 6946 | ' objects ("__new__()" to create it, and "__init__()" to ' |
| 6947 | 'customise\n' |
| 6948 | ' it), no non-"None" value may be returned by ' |
| 6949 | '"__init__()"; doing so\n' |
| 6950 | ' will cause a "TypeError" to be raised at runtime.\n' |
| 6951 | '\n' |
| 6952 | 'object.__del__(self)\n' |
| 6953 | '\n' |
| 6954 | ' Called when the instance is about to be destroyed. ' |
| 6955 | 'This is also\n' |
| 6956 | ' called a destructor. If a base class has a "__del__()" ' |
| 6957 | 'method, the\n' |
| 6958 | ' derived class\'s "__del__()" method, if any, must ' |
| 6959 | 'explicitly call it\n' |
| 6960 | ' to ensure proper deletion of the base class part of the ' |
| 6961 | 'instance.\n' |
| 6962 | ' Note that it is possible (though not recommended!) for ' |
| 6963 | 'the\n' |
| 6964 | ' "__del__()" method to postpone destruction of the ' |
| 6965 | 'instance by\n' |
| 6966 | ' creating a new reference to it. It may then be called ' |
| 6967 | 'at a later\n' |
| 6968 | ' time when this new reference is deleted. It is not ' |
| 6969 | 'guaranteed that\n' |
| 6970 | ' "__del__()" methods are called for objects that still ' |
| 6971 | 'exist when\n' |
| 6972 | ' the interpreter exits.\n' |
| 6973 | '\n' |
| 6974 | ' Note: "del x" doesn\'t directly call "x.__del__()" --- ' |
| 6975 | 'the former\n' |
| 6976 | ' decrements the reference count for "x" by one, and ' |
| 6977 | 'the latter is\n' |
| 6978 | ' only called when "x"\'s reference count reaches ' |
| 6979 | 'zero. Some common\n' |
| 6980 | ' situations that may prevent the reference count of an ' |
| 6981 | 'object from\n' |
| 6982 | ' going to zero include: circular references between ' |
| 6983 | 'objects (e.g.,\n' |
| 6984 | ' a doubly-linked list or a tree data structure with ' |
| 6985 | 'parent and\n' |
| 6986 | ' child pointers); a reference to the object on the ' |
| 6987 | 'stack frame of\n' |
| 6988 | ' a function that caught an exception (the traceback ' |
| 6989 | 'stored in\n' |
| 6990 | ' "sys.exc_traceback" keeps the stack frame alive); or ' |
| 6991 | 'a reference\n' |
| 6992 | ' to the object on the stack frame that raised an ' |
| 6993 | 'unhandled\n' |
| 6994 | ' exception in interactive mode (the traceback stored ' |
| 6995 | 'in\n' |
| 6996 | ' "sys.last_traceback" keeps the stack frame alive). ' |
| 6997 | 'The first\n' |
| 6998 | ' situation can only be remedied by explicitly breaking ' |
| 6999 | 'the cycles;\n' |
| 7000 | ' the latter two situations can be resolved by storing ' |
| 7001 | '"None" in\n' |
| 7002 | ' "sys.exc_traceback" or "sys.last_traceback". ' |
| 7003 | 'Circular references\n' |
| 7004 | ' which are garbage are detected when the option cycle ' |
| 7005 | 'detector is\n' |
| 7006 | " enabled (it's on by default), but can only be cleaned " |
| 7007 | 'up if there\n' |
| 7008 | ' are no Python-level "__del__()" methods involved. ' |
| 7009 | 'Refer to the\n' |
| 7010 | ' documentation for the "gc" module for more ' |
| 7011 | 'information about how\n' |
| 7012 | ' "__del__()" methods are handled by the cycle ' |
| 7013 | 'detector,\n' |
| 7014 | ' particularly the description of the "garbage" value.\n' |
| 7015 | '\n' |
| 7016 | ' Warning: Due to the precarious circumstances under ' |
| 7017 | 'which\n' |
| 7018 | ' "__del__()" methods are invoked, exceptions that ' |
| 7019 | 'occur during\n' |
| 7020 | ' their execution are ignored, and a warning is printed ' |
| 7021 | 'to\n' |
| 7022 | ' "sys.stderr" instead. Also, when "__del__()" is ' |
| 7023 | 'invoked in\n' |
| 7024 | ' response to a module being deleted (e.g., when ' |
| 7025 | 'execution of the\n' |
| 7026 | ' program is done), other globals referenced by the ' |
| 7027 | '"__del__()"\n' |
| 7028 | ' method may already have been deleted or in the ' |
| 7029 | 'process of being\n' |
| 7030 | ' torn down (e.g. the import machinery shutting down). ' |
| 7031 | 'For this\n' |
| 7032 | ' reason, "__del__()" methods should do the absolute ' |
| 7033 | 'minimum needed\n' |
| 7034 | ' to maintain external invariants. Starting with ' |
| 7035 | 'version 1.5,\n' |
| 7036 | ' Python guarantees that globals whose name begins with ' |
| 7037 | 'a single\n' |
| 7038 | ' underscore are deleted from their module before other ' |
| 7039 | 'globals are\n' |
| 7040 | ' deleted; if no other references to such globals ' |
| 7041 | 'exist, this may\n' |
| 7042 | ' help in assuring that imported modules are still ' |
| 7043 | 'available at the\n' |
| 7044 | ' time when the "__del__()" method is called.\n' |
| 7045 | '\n' |
| 7046 | ' See also the "-R" command-line option.\n' |
| 7047 | '\n' |
| 7048 | 'object.__repr__(self)\n' |
| 7049 | '\n' |
| 7050 | ' Called by the "repr()" built-in function and by string ' |
| 7051 | 'conversions\n' |
| 7052 | ' (reverse quotes) to compute the "official" string ' |
| 7053 | 'representation of\n' |
| 7054 | ' an object. If at all possible, this should look like a ' |
| 7055 | 'valid\n' |
| 7056 | ' Python expression that could be used to recreate an ' |
| 7057 | 'object with the\n' |
| 7058 | ' same value (given an appropriate environment). If this ' |
| 7059 | 'is not\n' |
| 7060 | ' possible, a string of the form "<...some useful ' |
| 7061 | 'description...>"\n' |
| 7062 | ' should be returned. The return value must be a string ' |
| 7063 | 'object. If a\n' |
| 7064 | ' class defines "__repr__()" but not "__str__()", then ' |
| 7065 | '"__repr__()"\n' |
| 7066 | ' is also used when an "informal" string representation ' |
| 7067 | 'of instances\n' |
| 7068 | ' of that class is required.\n' |
| 7069 | '\n' |
| 7070 | ' This is typically used for debugging, so it is ' |
| 7071 | 'important that the\n' |
| 7072 | ' representation is information-rich and unambiguous.\n' |
| 7073 | '\n' |
| 7074 | 'object.__str__(self)\n' |
| 7075 | '\n' |
| 7076 | ' Called by the "str()" built-in function and by the ' |
| 7077 | '"print"\n' |
| 7078 | ' statement to compute the "informal" string ' |
| 7079 | 'representation of an\n' |
| 7080 | ' object. This differs from "__repr__()" in that it does ' |
| 7081 | 'not have to\n' |
| 7082 | ' be a valid Python expression: a more convenient or ' |
| 7083 | 'concise\n' |
| 7084 | ' representation may be used instead. The return value ' |
| 7085 | 'must be a\n' |
| 7086 | ' string object.\n' |
| 7087 | '\n' |
| 7088 | 'object.__lt__(self, other)\n' |
| 7089 | 'object.__le__(self, other)\n' |
| 7090 | 'object.__eq__(self, other)\n' |
| 7091 | 'object.__ne__(self, other)\n' |
| 7092 | 'object.__gt__(self, other)\n' |
| 7093 | 'object.__ge__(self, other)\n' |
| 7094 | '\n' |
| 7095 | ' New in version 2.1.\n' |
| 7096 | '\n' |
| 7097 | ' These are the so-called "rich comparison" methods, and ' |
| 7098 | 'are called\n' |
| 7099 | ' for comparison operators in preference to "__cmp__()" ' |
| 7100 | 'below. The\n' |
| 7101 | ' correspondence between operator symbols and method ' |
| 7102 | 'names is as\n' |
| 7103 | ' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls ' |
| 7104 | '"x.__le__(y)",\n' |
| 7105 | ' "x==y" calls "x.__eq__(y)", "x!=y" and "x<>y" call ' |
| 7106 | '"x.__ne__(y)",\n' |
| 7107 | ' "x>y" calls "x.__gt__(y)", and "x>=y" calls ' |
| 7108 | '"x.__ge__(y)".\n' |
| 7109 | '\n' |
| 7110 | ' A rich comparison method may return the singleton ' |
| 7111 | '"NotImplemented"\n' |
| 7112 | ' if it does not implement the operation for a given pair ' |
| 7113 | 'of\n' |
| 7114 | ' arguments. By convention, "False" and "True" are ' |
| 7115 | 'returned for a\n' |
| 7116 | ' successful comparison. However, these methods can ' |
| 7117 | 'return any value,\n' |
| 7118 | ' so if the comparison operator is used in a Boolean ' |
| 7119 | 'context (e.g.,\n' |
| 7120 | ' in the condition of an "if" statement), Python will ' |
| 7121 | 'call "bool()"\n' |
| 7122 | ' on the value to determine if the result is true or ' |
| 7123 | 'false.\n' |
| 7124 | '\n' |
| 7125 | ' There are no implied relationships among the comparison ' |
| 7126 | 'operators.\n' |
| 7127 | ' The truth of "x==y" does not imply that "x!=y" is ' |
| 7128 | 'false.\n' |
| 7129 | ' Accordingly, when defining "__eq__()", one should also ' |
| 7130 | 'define\n' |
| 7131 | ' "__ne__()" so that the operators will behave as ' |
| 7132 | 'expected. See the\n' |
| 7133 | ' paragraph on "__hash__()" for some important notes on ' |
| 7134 | 'creating\n' |
| 7135 | ' *hashable* objects which support custom comparison ' |
| 7136 | 'operations and\n' |
| 7137 | ' are usable as dictionary keys.\n' |
| 7138 | '\n' |
| 7139 | ' There are no swapped-argument versions of these methods ' |
| 7140 | '(to be used\n' |
| 7141 | ' when the left argument does not support the operation ' |
| 7142 | 'but the right\n' |
| 7143 | ' argument does); rather, "__lt__()" and "__gt__()" are ' |
| 7144 | "each other's\n" |
| 7145 | ' reflection, "__le__()" and "__ge__()" are each other\'s ' |
| 7146 | 'reflection,\n' |
| 7147 | ' and "__eq__()" and "__ne__()" are their own ' |
| 7148 | 'reflection.\n' |
| 7149 | '\n' |
| 7150 | ' Arguments to rich comparison methods are never ' |
| 7151 | 'coerced.\n' |
| 7152 | '\n' |
| 7153 | ' To automatically generate ordering operations from a ' |
| 7154 | 'single root\n' |
| 7155 | ' operation, see "functools.total_ordering()".\n' |
| 7156 | '\n' |
| 7157 | 'object.__cmp__(self, other)\n' |
| 7158 | '\n' |
| 7159 | ' Called by comparison operations if rich comparison (see ' |
| 7160 | 'above) is\n' |
| 7161 | ' not defined. Should return a negative integer if "self ' |
| 7162 | '< other",\n' |
| 7163 | ' zero if "self == other", a positive integer if "self > ' |
| 7164 | 'other". If\n' |
| 7165 | ' no "__cmp__()", "__eq__()" or "__ne__()" operation is ' |
| 7166 | 'defined,\n' |
| 7167 | ' class instances are compared by object identity ' |
| 7168 | '("address"). See\n' |
| 7169 | ' also the description of "__hash__()" for some important ' |
| 7170 | 'notes on\n' |
| 7171 | ' creating *hashable* objects which support custom ' |
| 7172 | 'comparison\n' |
| 7173 | ' operations and are usable as dictionary keys. (Note: ' |
| 7174 | 'the\n' |
| 7175 | ' restriction that exceptions are not propagated by ' |
| 7176 | '"__cmp__()" has\n' |
| 7177 | ' been removed since Python 1.5.)\n' |
| 7178 | '\n' |
| 7179 | 'object.__rcmp__(self, other)\n' |
| 7180 | '\n' |
| 7181 | ' Changed in version 2.1: No longer supported.\n' |
| 7182 | '\n' |
| 7183 | 'object.__hash__(self)\n' |
| 7184 | '\n' |
| 7185 | ' Called by built-in function "hash()" and for operations ' |
| 7186 | 'on members\n' |
| 7187 | ' of hashed collections including "set", "frozenset", and ' |
| 7188 | '"dict".\n' |
| 7189 | ' "__hash__()" should return an integer. The only ' |
| 7190 | 'required property\n' |
| 7191 | ' is that objects which compare equal have the same hash ' |
| 7192 | 'value; it is\n' |
| 7193 | ' advised to somehow mix together (e.g. using exclusive ' |
| 7194 | 'or) the hash\n' |
| 7195 | ' values for the components of the object that also play ' |
| 7196 | 'a part in\n' |
| 7197 | ' comparison of objects.\n' |
| 7198 | '\n' |
| 7199 | ' If a class does not define a "__cmp__()" or "__eq__()" ' |
| 7200 | 'method it\n' |
| 7201 | ' should not define a "__hash__()" operation either; if ' |
| 7202 | 'it defines\n' |
| 7203 | ' "__cmp__()" or "__eq__()" but not "__hash__()", its ' |
| 7204 | 'instances will\n' |
| 7205 | ' not be usable in hashed collections. If a class ' |
| 7206 | 'defines mutable\n' |
| 7207 | ' objects and implements a "__cmp__()" or "__eq__()" ' |
| 7208 | 'method, it\n' |
| 7209 | ' should not implement "__hash__()", since hashable ' |
| 7210 | 'collection\n' |
| 7211 | " implementations require that a object's hash value is " |
| 7212 | 'immutable (if\n' |
| 7213 | " the object's hash value changes, it will be in the " |
| 7214 | 'wrong hash\n' |
| 7215 | ' bucket).\n' |
| 7216 | '\n' |
| 7217 | ' User-defined classes have "__cmp__()" and "__hash__()" ' |
| 7218 | 'methods by\n' |
| 7219 | ' default; with them, all objects compare unequal (except ' |
| 7220 | 'with\n' |
| 7221 | ' themselves) and "x.__hash__()" returns a result derived ' |
| 7222 | 'from\n' |
| 7223 | ' "id(x)".\n' |
| 7224 | '\n' |
| 7225 | ' Classes which inherit a "__hash__()" method from a ' |
| 7226 | 'parent class but\n' |
| 7227 | ' change the meaning of "__cmp__()" or "__eq__()" such ' |
| 7228 | 'that the hash\n' |
| 7229 | ' value returned is no longer appropriate (e.g. by ' |
| 7230 | 'switching to a\n' |
| 7231 | ' value-based concept of equality instead of the default ' |
| 7232 | 'identity\n' |
| 7233 | ' based equality) can explicitly flag themselves as being ' |
| 7234 | 'unhashable\n' |
| 7235 | ' by setting "__hash__ = None" in the class definition. ' |
| 7236 | 'Doing so\n' |
| 7237 | ' means that not only will instances of the class raise ' |
| 7238 | 'an\n' |
| 7239 | ' appropriate "TypeError" when a program attempts to ' |
| 7240 | 'retrieve their\n' |
| 7241 | ' hash value, but they will also be correctly identified ' |
| 7242 | 'as\n' |
| 7243 | ' unhashable when checking "isinstance(obj, ' |
| 7244 | 'collections.Hashable)"\n' |
| 7245 | ' (unlike classes which define their own "__hash__()" to ' |
| 7246 | 'explicitly\n' |
| 7247 | ' raise "TypeError").\n' |
| 7248 | '\n' |
| 7249 | ' Changed in version 2.5: "__hash__()" may now also ' |
| 7250 | 'return a long\n' |
| 7251 | ' integer object; the 32-bit integer is then derived from ' |
| 7252 | 'the hash of\n' |
| 7253 | ' that object.\n' |
| 7254 | '\n' |
| 7255 | ' Changed in version 2.6: "__hash__" may now be set to ' |
| 7256 | '"None" to\n' |
| 7257 | ' explicitly flag instances of a class as unhashable.\n' |
| 7258 | '\n' |
| 7259 | 'object.__nonzero__(self)\n' |
| 7260 | '\n' |
| 7261 | ' Called to implement truth value testing and the ' |
| 7262 | 'built-in operation\n' |
| 7263 | ' "bool()"; should return "False" or "True", or their ' |
| 7264 | 'integer\n' |
| 7265 | ' equivalents "0" or "1". When this method is not ' |
| 7266 | 'defined,\n' |
| 7267 | ' "__len__()" is called, if it is defined, and the object ' |
| 7268 | 'is\n' |
| 7269 | ' considered true if its result is nonzero. If a class ' |
| 7270 | 'defines\n' |
| 7271 | ' neither "__len__()" nor "__nonzero__()", all its ' |
| 7272 | 'instances are\n' |
| 7273 | ' considered true.\n' |
| 7274 | '\n' |
| 7275 | 'object.__unicode__(self)\n' |
| 7276 | '\n' |
| 7277 | ' Called to implement "unicode()" built-in; should return ' |
| 7278 | 'a Unicode\n' |
| 7279 | ' object. When this method is not defined, string ' |
| 7280 | 'conversion is\n' |
| 7281 | ' attempted, and the result of string conversion is ' |
| 7282 | 'converted to\n' |
| 7283 | ' Unicode using the system default encoding.\n' |
| 7284 | '\n' |
| 7285 | '\n' |
| 7286 | 'Customizing attribute access\n' |
| 7287 | '============================\n' |
| 7288 | '\n' |
| 7289 | 'The following methods can be defined to customize the ' |
| 7290 | 'meaning of\n' |
| 7291 | 'attribute access (use of, assignment to, or deletion of ' |
| 7292 | '"x.name") for\n' |
| 7293 | 'class instances.\n' |
| 7294 | '\n' |
| 7295 | 'object.__getattr__(self, name)\n' |
| 7296 | '\n' |
| 7297 | ' Called when an attribute lookup has not found the ' |
| 7298 | 'attribute in the\n' |
| 7299 | ' usual places (i.e. it is not an instance attribute nor ' |
| 7300 | 'is it found\n' |
| 7301 | ' in the class tree for "self"). "name" is the attribute ' |
| 7302 | 'name. This\n' |
| 7303 | ' method should return the (computed) attribute value or ' |
| 7304 | 'raise an\n' |
| 7305 | ' "AttributeError" exception.\n' |
| 7306 | '\n' |
| 7307 | ' Note that if the attribute is found through the normal ' |
| 7308 | 'mechanism,\n' |
| 7309 | ' "__getattr__()" is not called. (This is an intentional ' |
| 7310 | 'asymmetry\n' |
| 7311 | ' between "__getattr__()" and "__setattr__()".) This is ' |
| 7312 | 'done both for\n' |
| 7313 | ' efficiency reasons and because otherwise ' |
| 7314 | '"__getattr__()" would have\n' |
| 7315 | ' no way to access other attributes of the instance. ' |
| 7316 | 'Note that at\n' |
| 7317 | ' least for instance variables, you can fake total ' |
| 7318 | 'control by not\n' |
| 7319 | ' inserting any values in the instance attribute ' |
| 7320 | 'dictionary (but\n' |
| 7321 | ' instead inserting them in another object). See the\n' |
| 7322 | ' "__getattribute__()" method below for a way to actually ' |
| 7323 | 'get total\n' |
| 7324 | ' control in new-style classes.\n' |
| 7325 | '\n' |
| 7326 | 'object.__setattr__(self, name, value)\n' |
| 7327 | '\n' |
| 7328 | ' Called when an attribute assignment is attempted. This ' |
| 7329 | 'is called\n' |
| 7330 | ' instead of the normal mechanism (i.e. store the value ' |
| 7331 | 'in the\n' |
| 7332 | ' instance dictionary). *name* is the attribute name, ' |
| 7333 | '*value* is the\n' |
| 7334 | ' value to be assigned to it.\n' |
| 7335 | '\n' |
| 7336 | ' If "__setattr__()" wants to assign to an instance ' |
| 7337 | 'attribute, it\n' |
| 7338 | ' should not simply execute "self.name = value" --- this ' |
| 7339 | 'would cause\n' |
| 7340 | ' a recursive call to itself. Instead, it should insert ' |
| 7341 | 'the value in\n' |
| 7342 | ' the dictionary of instance attributes, e.g., ' |
| 7343 | '"self.__dict__[name] =\n' |
| 7344 | ' value". For new-style classes, rather than accessing ' |
| 7345 | 'the instance\n' |
| 7346 | ' dictionary, it should call the base class method with ' |
| 7347 | 'the same\n' |
| 7348 | ' name, for example, "object.__setattr__(self, name, ' |
| 7349 | 'value)".\n' |
| 7350 | '\n' |
| 7351 | 'object.__delattr__(self, name)\n' |
| 7352 | '\n' |
| 7353 | ' Like "__setattr__()" but for attribute deletion instead ' |
| 7354 | 'of\n' |
| 7355 | ' assignment. This should only be implemented if "del ' |
| 7356 | 'obj.name" is\n' |
| 7357 | ' meaningful for the object.\n' |
| 7358 | '\n' |
| 7359 | '\n' |
| 7360 | 'More attribute access for new-style classes\n' |
| 7361 | '-------------------------------------------\n' |
| 7362 | '\n' |
| 7363 | 'The following methods only apply to new-style classes.\n' |
| 7364 | '\n' |
| 7365 | 'object.__getattribute__(self, name)\n' |
| 7366 | '\n' |
| 7367 | ' Called unconditionally to implement attribute accesses ' |
| 7368 | 'for\n' |
| 7369 | ' instances of the class. If the class also defines ' |
| 7370 | '"__getattr__()",\n' |
| 7371 | ' the latter will not be called unless ' |
| 7372 | '"__getattribute__()" either\n' |
| 7373 | ' calls it explicitly or raises an "AttributeError". This ' |
| 7374 | 'method\n' |
| 7375 | ' should return the (computed) attribute value or raise ' |
| 7376 | 'an\n' |
| 7377 | ' "AttributeError" exception. In order to avoid infinite ' |
| 7378 | 'recursion in\n' |
| 7379 | ' this method, its implementation should always call the ' |
| 7380 | 'base class\n' |
| 7381 | ' method with the same name to access any attributes it ' |
| 7382 | 'needs, for\n' |
| 7383 | ' example, "object.__getattribute__(self, name)".\n' |
| 7384 | '\n' |
| 7385 | ' Note: This method may still be bypassed when looking up ' |
| 7386 | 'special\n' |
| 7387 | ' methods as the result of implicit invocation via ' |
| 7388 | 'language syntax\n' |
| 7389 | ' or built-in functions. See Special method lookup for ' |
| 7390 | 'new-style\n' |
| 7391 | ' classes.\n' |
| 7392 | '\n' |
| 7393 | '\n' |
| 7394 | 'Implementing Descriptors\n' |
| 7395 | '------------------------\n' |
| 7396 | '\n' |
| 7397 | 'The following methods only apply when an instance of the ' |
| 7398 | 'class\n' |
| 7399 | 'containing the method (a so-called *descriptor* class) ' |
| 7400 | 'appears in an\n' |
| 7401 | '*owner* class (the descriptor must be in either the ' |
| 7402 | "owner's class\n" |
| 7403 | 'dictionary or in the class dictionary for one of its ' |
| 7404 | 'parents). In the\n' |
| 7405 | 'examples below, "the attribute" refers to the attribute ' |
| 7406 | 'whose name is\n' |
| 7407 | 'the key of the property in the owner class\' "__dict__".\n' |
| 7408 | '\n' |
| 7409 | 'object.__get__(self, instance, owner)\n' |
| 7410 | '\n' |
| 7411 | ' Called to get the attribute of the owner class (class ' |
| 7412 | 'attribute\n' |
| 7413 | ' access) or of an instance of that class (instance ' |
| 7414 | 'attribute\n' |
| 7415 | ' access). *owner* is always the owner class, while ' |
| 7416 | '*instance* is the\n' |
| 7417 | ' instance that the attribute was accessed through, or ' |
| 7418 | '"None" when\n' |
| 7419 | ' the attribute is accessed through the *owner*. This ' |
| 7420 | 'method should\n' |
| 7421 | ' return the (computed) attribute value or raise an ' |
| 7422 | '"AttributeError"\n' |
| 7423 | ' exception.\n' |
| 7424 | '\n' |
| 7425 | 'object.__set__(self, instance, value)\n' |
| 7426 | '\n' |
| 7427 | ' Called to set the attribute on an instance *instance* ' |
| 7428 | 'of the owner\n' |
| 7429 | ' class to a new value, *value*.\n' |
| 7430 | '\n' |
| 7431 | 'object.__delete__(self, instance)\n' |
| 7432 | '\n' |
| 7433 | ' Called to delete the attribute on an instance ' |
| 7434 | '*instance* of the\n' |
| 7435 | ' owner class.\n' |
| 7436 | '\n' |
| 7437 | '\n' |
| 7438 | 'Invoking Descriptors\n' |
| 7439 | '--------------------\n' |
| 7440 | '\n' |
| 7441 | 'In general, a descriptor is an object attribute with ' |
| 7442 | '"binding\n' |
| 7443 | 'behavior", one whose attribute access has been overridden ' |
| 7444 | 'by methods\n' |
| 7445 | 'in the descriptor protocol: "__get__()", "__set__()", ' |
| 7446 | 'and\n' |
| 7447 | '"__delete__()". If any of those methods are defined for an ' |
| 7448 | 'object, it\n' |
| 7449 | 'is said to be a descriptor.\n' |
| 7450 | '\n' |
| 7451 | 'The default behavior for attribute access is to get, set, ' |
| 7452 | 'or delete\n' |
| 7453 | "the attribute from an object's dictionary. For instance, " |
| 7454 | '"a.x" has a\n' |
| 7455 | 'lookup chain starting with "a.__dict__[\'x\']", then\n' |
| 7456 | '"type(a).__dict__[\'x\']", and continuing through the base ' |
| 7457 | 'classes of\n' |
| 7458 | '"type(a)" excluding metaclasses.\n' |
| 7459 | '\n' |
| 7460 | 'However, if the looked-up value is an object defining one ' |
| 7461 | 'of the\n' |
| 7462 | 'descriptor methods, then Python may override the default ' |
| 7463 | 'behavior and\n' |
| 7464 | 'invoke the descriptor method instead. Where this occurs ' |
| 7465 | 'in the\n' |
| 7466 | 'precedence chain depends on which descriptor methods were ' |
| 7467 | 'defined and\n' |
| 7468 | 'how they were called. Note that descriptors are only ' |
| 7469 | 'invoked for new\n' |
| 7470 | 'style objects or classes (ones that subclass "object()" or ' |
| 7471 | '"type()").\n' |
| 7472 | '\n' |
| 7473 | 'The starting point for descriptor invocation is a binding, ' |
| 7474 | '"a.x". How\n' |
| 7475 | 'the arguments are assembled depends on "a":\n' |
| 7476 | '\n' |
| 7477 | 'Direct Call\n' |
| 7478 | ' The simplest and least common call is when user code ' |
| 7479 | 'directly\n' |
| 7480 | ' invokes a descriptor method: "x.__get__(a)".\n' |
| 7481 | '\n' |
| 7482 | 'Instance Binding\n' |
| 7483 | ' If binding to a new-style object instance, "a.x" is ' |
| 7484 | 'transformed\n' |
| 7485 | ' into the call: "type(a).__dict__[\'x\'].__get__(a, ' |
| 7486 | 'type(a))".\n' |
| 7487 | '\n' |
| 7488 | 'Class Binding\n' |
| 7489 | ' If binding to a new-style class, "A.x" is transformed ' |
| 7490 | 'into the\n' |
| 7491 | ' call: "A.__dict__[\'x\'].__get__(None, A)".\n' |
| 7492 | '\n' |
| 7493 | 'Super Binding\n' |
| 7494 | ' If "a" is an instance of "super", then the binding ' |
| 7495 | '"super(B,\n' |
| 7496 | ' obj).m()" searches "obj.__class__.__mro__" for the base ' |
| 7497 | 'class "A"\n' |
| 7498 | ' immediately preceding "B" and then invokes the ' |
| 7499 | 'descriptor with the\n' |
| 7500 | ' call: "A.__dict__[\'m\'].__get__(obj, obj.__class__)".\n' |
| 7501 | '\n' |
| 7502 | 'For instance bindings, the precedence of descriptor ' |
| 7503 | 'invocation depends\n' |
| 7504 | 'on the which descriptor methods are defined. A descriptor ' |
| 7505 | 'can define\n' |
| 7506 | 'any combination of "__get__()", "__set__()" and ' |
| 7507 | '"__delete__()". If it\n' |
| 7508 | 'does not define "__get__()", then accessing the attribute ' |
| 7509 | 'will return\n' |
| 7510 | 'the descriptor object itself unless there is a value in ' |
| 7511 | "the object's\n" |
| 7512 | 'instance dictionary. If the descriptor defines ' |
| 7513 | '"__set__()" and/or\n' |
| 7514 | '"__delete__()", it is a data descriptor; if it defines ' |
| 7515 | 'neither, it is\n' |
| 7516 | 'a non-data descriptor. Normally, data descriptors define ' |
| 7517 | 'both\n' |
| 7518 | '"__get__()" and "__set__()", while non-data descriptors ' |
| 7519 | 'have just the\n' |
| 7520 | '"__get__()" method. Data descriptors with "__set__()" and ' |
| 7521 | '"__get__()"\n' |
| 7522 | 'defined always override a redefinition in an instance ' |
| 7523 | 'dictionary. In\n' |
| 7524 | 'contrast, non-data descriptors can be overridden by ' |
| 7525 | 'instances.\n' |
| 7526 | '\n' |
| 7527 | 'Python methods (including "staticmethod()" and ' |
| 7528 | '"classmethod()") are\n' |
| 7529 | 'implemented as non-data descriptors. Accordingly, ' |
| 7530 | 'instances can\n' |
| 7531 | 'redefine and override methods. This allows individual ' |
| 7532 | 'instances to\n' |
| 7533 | 'acquire behaviors that differ from other instances of the ' |
| 7534 | 'same class.\n' |
| 7535 | '\n' |
| 7536 | 'The "property()" function is implemented as a data ' |
| 7537 | 'descriptor.\n' |
| 7538 | 'Accordingly, instances cannot override the behavior of a ' |
| 7539 | 'property.\n' |
| 7540 | '\n' |
| 7541 | '\n' |
| 7542 | '__slots__\n' |
| 7543 | '---------\n' |
| 7544 | '\n' |
| 7545 | 'By default, instances of both old and new-style classes ' |
| 7546 | 'have a\n' |
| 7547 | 'dictionary for attribute storage. This wastes space for ' |
| 7548 | 'objects\n' |
| 7549 | 'having very few instance variables. The space consumption ' |
| 7550 | 'can become\n' |
| 7551 | 'acute when creating large numbers of instances.\n' |
| 7552 | '\n' |
| 7553 | 'The default can be overridden by defining *__slots__* in a ' |
| 7554 | 'new-style\n' |
| 7555 | 'class definition. The *__slots__* declaration takes a ' |
| 7556 | 'sequence of\n' |
| 7557 | 'instance variables and reserves just enough space in each ' |
| 7558 | 'instance to\n' |
| 7559 | 'hold a value for each variable. Space is saved because ' |
| 7560 | '*__dict__* is\n' |
| 7561 | 'not created for each instance.\n' |
| 7562 | '\n' |
| 7563 | '__slots__\n' |
| 7564 | '\n' |
| 7565 | ' This class variable can be assigned a string, iterable, ' |
| 7566 | 'or sequence\n' |
| 7567 | ' of strings with variable names used by instances. If ' |
| 7568 | 'defined in a\n' |
| 7569 | ' new-style class, *__slots__* reserves space for the ' |
| 7570 | 'declared\n' |
| 7571 | ' variables and prevents the automatic creation of ' |
| 7572 | '*__dict__* and\n' |
| 7573 | ' *__weakref__* for each instance.\n' |
| 7574 | '\n' |
| 7575 | ' New in version 2.2.\n' |
| 7576 | '\n' |
| 7577 | 'Notes on using *__slots__*\n' |
| 7578 | '\n' |
| 7579 | '* When inheriting from a class without *__slots__*, the ' |
| 7580 | '*__dict__*\n' |
| 7581 | ' attribute of that class will always be accessible, so a ' |
| 7582 | '*__slots__*\n' |
| 7583 | ' definition in the subclass is meaningless.\n' |
| 7584 | '\n' |
| 7585 | '* Without a *__dict__* variable, instances cannot be ' |
| 7586 | 'assigned new\n' |
| 7587 | ' variables not listed in the *__slots__* definition. ' |
| 7588 | 'Attempts to\n' |
| 7589 | ' assign to an unlisted variable name raises ' |
| 7590 | '"AttributeError". If\n' |
| 7591 | ' dynamic assignment of new variables is desired, then ' |
| 7592 | 'add\n' |
| 7593 | ' "\'__dict__\'" to the sequence of strings in the ' |
| 7594 | '*__slots__*\n' |
| 7595 | ' declaration.\n' |
| 7596 | '\n' |
| 7597 | ' Changed in version 2.3: Previously, adding ' |
| 7598 | '"\'__dict__\'" to the\n' |
| 7599 | ' *__slots__* declaration would not enable the assignment ' |
| 7600 | 'of new\n' |
| 7601 | ' attributes not specifically listed in the sequence of ' |
| 7602 | 'instance\n' |
| 7603 | ' variable names.\n' |
| 7604 | '\n' |
| 7605 | '* Without a *__weakref__* variable for each instance, ' |
| 7606 | 'classes\n' |
| 7607 | ' defining *__slots__* do not support weak references to ' |
| 7608 | 'its\n' |
| 7609 | ' instances. If weak reference support is needed, then ' |
| 7610 | 'add\n' |
| 7611 | ' "\'__weakref__\'" to the sequence of strings in the ' |
| 7612 | '*__slots__*\n' |
| 7613 | ' declaration.\n' |
| 7614 | '\n' |
| 7615 | ' Changed in version 2.3: Previously, adding ' |
| 7616 | '"\'__weakref__\'" to the\n' |
| 7617 | ' *__slots__* declaration would not enable support for ' |
| 7618 | 'weak\n' |
| 7619 | ' references.\n' |
| 7620 | '\n' |
| 7621 | '* *__slots__* are implemented at the class level by ' |
| 7622 | 'creating\n' |
| 7623 | ' descriptors (Implementing Descriptors) for each variable ' |
| 7624 | 'name. As a\n' |
| 7625 | ' result, class attributes cannot be used to set default ' |
| 7626 | 'values for\n' |
| 7627 | ' instance variables defined by *__slots__*; otherwise, ' |
| 7628 | 'the class\n' |
| 7629 | ' attribute would overwrite the descriptor assignment.\n' |
| 7630 | '\n' |
| 7631 | '* The action of a *__slots__* declaration is limited to ' |
| 7632 | 'the class\n' |
| 7633 | ' where it is defined. As a result, subclasses will have ' |
| 7634 | 'a *__dict__*\n' |
| 7635 | ' unless they also define *__slots__* (which must only ' |
| 7636 | 'contain names\n' |
| 7637 | ' of any *additional* slots).\n' |
| 7638 | '\n' |
| 7639 | '* If a class defines a slot also defined in a base class, ' |
| 7640 | 'the\n' |
| 7641 | ' instance variable defined by the base class slot is ' |
| 7642 | 'inaccessible\n' |
| 7643 | ' (except by retrieving its descriptor directly from the ' |
| 7644 | 'base class).\n' |
| 7645 | ' This renders the meaning of the program undefined. In ' |
| 7646 | 'the future, a\n' |
| 7647 | ' check may be added to prevent this.\n' |
| 7648 | '\n' |
| 7649 | '* Nonempty *__slots__* does not work for classes derived ' |
| 7650 | 'from\n' |
| 7651 | ' "variable-length" built-in types such as "long", "str" ' |
| 7652 | 'and "tuple".\n' |
| 7653 | '\n' |
| 7654 | '* Any non-string iterable may be assigned to *__slots__*. ' |
| 7655 | 'Mappings\n' |
| 7656 | ' may also be used; however, in the future, special ' |
| 7657 | 'meaning may be\n' |
| 7658 | ' assigned to the values corresponding to each key.\n' |
| 7659 | '\n' |
| 7660 | '* *__class__* assignment works only if both classes have ' |
| 7661 | 'the same\n' |
| 7662 | ' *__slots__*.\n' |
| 7663 | '\n' |
| 7664 | ' Changed in version 2.6: Previously, *__class__* ' |
| 7665 | 'assignment raised an\n' |
| 7666 | ' error if either new or old class had *__slots__*.\n' |
| 7667 | '\n' |
| 7668 | '\n' |
| 7669 | 'Customizing class creation\n' |
| 7670 | '==========================\n' |
| 7671 | '\n' |
| 7672 | 'By default, new-style classes are constructed using ' |
| 7673 | '"type()". A class\n' |
| 7674 | 'definition is read into a separate namespace and the value ' |
| 7675 | 'of class\n' |
| 7676 | 'name is bound to the result of "type(name, bases, dict)".\n' |
| 7677 | '\n' |
| 7678 | 'When the class definition is read, if *__metaclass__* is ' |
| 7679 | 'defined then\n' |
| 7680 | 'the callable assigned to it will be called instead of ' |
| 7681 | '"type()". This\n' |
| 7682 | 'allows classes or functions to be written which monitor or ' |
| 7683 | 'alter the\n' |
| 7684 | 'class creation process:\n' |
| 7685 | '\n' |
| 7686 | '* Modifying the class dictionary prior to the class being ' |
| 7687 | 'created.\n' |
| 7688 | '\n' |
| 7689 | '* Returning an instance of another class -- essentially ' |
| 7690 | 'performing\n' |
| 7691 | ' the role of a factory function.\n' |
| 7692 | '\n' |
| 7693 | "These steps will have to be performed in the metaclass's " |
| 7694 | '"__new__()"\n' |
| 7695 | 'method -- "type.__new__()" can then be called from this ' |
| 7696 | 'method to\n' |
| 7697 | 'create a class with different properties. This example ' |
| 7698 | 'adds a new\n' |
| 7699 | 'element to the class dictionary before creating the ' |
| 7700 | 'class:\n' |
| 7701 | '\n' |
| 7702 | ' class metacls(type):\n' |
| 7703 | ' def __new__(mcs, name, bases, dict):\n' |
| 7704 | " dict['foo'] = 'metacls was here'\n" |
| 7705 | ' return type.__new__(mcs, name, bases, dict)\n' |
| 7706 | '\n' |
| 7707 | 'You can of course also override other class methods (or ' |
| 7708 | 'add new\n' |
| 7709 | 'methods); for example defining a custom "__call__()" ' |
| 7710 | 'method in the\n' |
| 7711 | 'metaclass allows custom behavior when the class is called, ' |
| 7712 | 'e.g. not\n' |
| 7713 | 'always creating a new instance.\n' |
| 7714 | '\n' |
| 7715 | '__metaclass__\n' |
| 7716 | '\n' |
| 7717 | ' This variable can be any callable accepting arguments ' |
| 7718 | 'for "name",\n' |
| 7719 | ' "bases", and "dict". Upon class creation, the callable ' |
| 7720 | 'is used\n' |
| 7721 | ' instead of the built-in "type()".\n' |
| 7722 | '\n' |
| 7723 | ' New in version 2.2.\n' |
| 7724 | '\n' |
| 7725 | 'The appropriate metaclass is determined by the following ' |
| 7726 | 'precedence\n' |
| 7727 | 'rules:\n' |
| 7728 | '\n' |
| 7729 | '* If "dict[\'__metaclass__\']" exists, it is used.\n' |
| 7730 | '\n' |
| 7731 | '* Otherwise, if there is at least one base class, its ' |
| 7732 | 'metaclass is\n' |
| 7733 | ' used (this looks for a *__class__* attribute first and ' |
| 7734 | 'if not found,\n' |
| 7735 | ' uses its type).\n' |
| 7736 | '\n' |
| 7737 | '* Otherwise, if a global variable named __metaclass__ ' |
| 7738 | 'exists, it is\n' |
| 7739 | ' used.\n' |
| 7740 | '\n' |
| 7741 | '* Otherwise, the old-style, classic metaclass ' |
| 7742 | '(types.ClassType) is\n' |
| 7743 | ' used.\n' |
| 7744 | '\n' |
| 7745 | 'The potential uses for metaclasses are boundless. Some ' |
| 7746 | 'ideas that have\n' |
| 7747 | 'been explored including logging, interface checking, ' |
| 7748 | 'automatic\n' |
| 7749 | 'delegation, automatic property creation, proxies, ' |
| 7750 | 'frameworks, and\n' |
| 7751 | 'automatic resource locking/synchronization.\n' |
| 7752 | '\n' |
| 7753 | '\n' |
| 7754 | 'Customizing instance and subclass checks\n' |
| 7755 | '========================================\n' |
| 7756 | '\n' |
| 7757 | 'New in version 2.6.\n' |
| 7758 | '\n' |
| 7759 | 'The following methods are used to override the default ' |
| 7760 | 'behavior of the\n' |
| 7761 | '"isinstance()" and "issubclass()" built-in functions.\n' |
| 7762 | '\n' |
| 7763 | 'In particular, the metaclass "abc.ABCMeta" implements ' |
| 7764 | 'these methods in\n' |
| 7765 | 'order to allow the addition of Abstract Base Classes ' |
| 7766 | '(ABCs) as\n' |
| 7767 | '"virtual base classes" to any class or type (including ' |
| 7768 | 'built-in\n' |
| 7769 | 'types), including other ABCs.\n' |
| 7770 | '\n' |
| 7771 | 'class.__instancecheck__(self, instance)\n' |
| 7772 | '\n' |
| 7773 | ' Return true if *instance* should be considered a ' |
| 7774 | '(direct or\n' |
| 7775 | ' indirect) instance of *class*. If defined, called to ' |
| 7776 | 'implement\n' |
| 7777 | ' "isinstance(instance, class)".\n' |
| 7778 | '\n' |
| 7779 | 'class.__subclasscheck__(self, subclass)\n' |
| 7780 | '\n' |
| 7781 | ' Return true if *subclass* should be considered a ' |
| 7782 | '(direct or\n' |
| 7783 | ' indirect) subclass of *class*. If defined, called to ' |
| 7784 | 'implement\n' |
| 7785 | ' "issubclass(subclass, class)".\n' |
| 7786 | '\n' |
| 7787 | 'Note that these methods are looked up on the type ' |
| 7788 | '(metaclass) of a\n' |
| 7789 | 'class. They cannot be defined as class methods in the ' |
| 7790 | 'actual class.\n' |
| 7791 | 'This is consistent with the lookup of special methods that ' |
| 7792 | 'are called\n' |
| 7793 | 'on instances, only in this case the instance is itself a ' |
| 7794 | 'class.\n' |
| 7795 | '\n' |
| 7796 | 'See also: **PEP 3119** - Introducing Abstract Base ' |
| 7797 | 'Classes\n' |
| 7798 | '\n' |
| 7799 | ' Includes the specification for customizing ' |
| 7800 | '"isinstance()" and\n' |
| 7801 | ' "issubclass()" behavior through "__instancecheck__()" ' |
| 7802 | 'and\n' |
| 7803 | ' "__subclasscheck__()", with motivation for this ' |
| 7804 | 'functionality in\n' |
| 7805 | ' the context of adding Abstract Base Classes (see the ' |
| 7806 | '"abc"\n' |
| 7807 | ' module) to the language.\n' |
| 7808 | '\n' |
| 7809 | '\n' |
| 7810 | 'Emulating callable objects\n' |
| 7811 | '==========================\n' |
| 7812 | '\n' |
| 7813 | 'object.__call__(self[, args...])\n' |
| 7814 | '\n' |
| 7815 | ' Called when the instance is "called" as a function; if ' |
| 7816 | 'this method\n' |
| 7817 | ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n' |
| 7818 | ' "x.__call__(arg1, arg2, ...)".\n' |
| 7819 | '\n' |
| 7820 | '\n' |
| 7821 | 'Emulating container types\n' |
| 7822 | '=========================\n' |
| 7823 | '\n' |
| 7824 | 'The following methods can be defined to implement ' |
| 7825 | 'container objects.\n' |
| 7826 | 'Containers usually are sequences (such as lists or tuples) ' |
| 7827 | 'or mappings\n' |
| 7828 | '(like dictionaries), but can represent other containers as ' |
| 7829 | 'well. The\n' |
| 7830 | 'first set of methods is used either to emulate a sequence ' |
| 7831 | 'or to\n' |
| 7832 | 'emulate a mapping; the difference is that for a sequence, ' |
| 7833 | 'the\n' |
| 7834 | 'allowable keys should be the integers *k* for which "0 <= ' |
| 7835 | 'k < N" where\n' |
| 7836 | '*N* is the length of the sequence, or slice objects, which ' |
| 7837 | 'define a\n' |
| 7838 | 'range of items. (For backwards compatibility, the method\n' |
| 7839 | '"__getslice__()" (see below) can also be defined to handle ' |
| 7840 | 'simple, but\n' |
| 7841 | 'not extended slices.) It is also recommended that mappings ' |
| 7842 | 'provide the\n' |
| 7843 | 'methods "keys()", "values()", "items()", "has_key()", ' |
| 7844 | '"get()",\n' |
| 7845 | '"clear()", "setdefault()", "iterkeys()", "itervalues()",\n' |
| 7846 | '"iteritems()", "pop()", "popitem()", "copy()", and ' |
| 7847 | '"update()" behaving\n' |
| 7848 | "similar to those for Python's standard dictionary " |
| 7849 | 'objects. The\n' |
| 7850 | '"UserDict" module provides a "DictMixin" class to help ' |
| 7851 | 'create those\n' |
| 7852 | 'methods from a base set of "__getitem__()", ' |
| 7853 | '"__setitem__()",\n' |
| 7854 | '"__delitem__()", and "keys()". Mutable sequences should ' |
| 7855 | 'provide\n' |
| 7856 | 'methods "append()", "count()", "index()", "extend()", ' |
| 7857 | '"insert()",\n' |
| 7858 | '"pop()", "remove()", "reverse()" and "sort()", like Python ' |
| 7859 | 'standard\n' |
| 7860 | 'list objects. Finally, sequence types should implement ' |
| 7861 | 'addition\n' |
| 7862 | '(meaning concatenation) and multiplication (meaning ' |
| 7863 | 'repetition) by\n' |
| 7864 | 'defining the methods "__add__()", "__radd__()", ' |
| 7865 | '"__iadd__()",\n' |
| 7866 | '"__mul__()", "__rmul__()" and "__imul__()" described ' |
| 7867 | 'below; they\n' |
| 7868 | 'should not define "__coerce__()" or other numerical ' |
| 7869 | 'operators. It is\n' |
| 7870 | 'recommended that both mappings and sequences implement ' |
| 7871 | 'the\n' |
| 7872 | '"__contains__()" method to allow efficient use of the "in" ' |
| 7873 | 'operator;\n' |
| 7874 | 'for mappings, "in" should be equivalent of "has_key()"; ' |
| 7875 | 'for sequences,\n' |
| 7876 | 'it should search through the values. It is further ' |
| 7877 | 'recommended that\n' |
| 7878 | 'both mappings and sequences implement the "__iter__()" ' |
| 7879 | 'method to allow\n' |
| 7880 | 'efficient iteration through the container; for mappings, ' |
| 7881 | '"__iter__()"\n' |
| 7882 | 'should be the same as "iterkeys()"; for sequences, it ' |
| 7883 | 'should iterate\n' |
| 7884 | 'through the values.\n' |
| 7885 | '\n' |
| 7886 | 'object.__len__(self)\n' |
| 7887 | '\n' |
| 7888 | ' Called to implement the built-in function "len()". ' |
| 7889 | 'Should return\n' |
| 7890 | ' the length of the object, an integer ">=" 0. Also, an ' |
| 7891 | 'object that\n' |
| 7892 | ' doesn\'t define a "__nonzero__()" method and whose ' |
| 7893 | '"__len__()"\n' |
| 7894 | ' method returns zero is considered to be false in a ' |
| 7895 | 'Boolean context.\n' |
| 7896 | '\n' |
| 7897 | 'object.__getitem__(self, key)\n' |
| 7898 | '\n' |
| 7899 | ' Called to implement evaluation of "self[key]". For ' |
| 7900 | 'sequence types,\n' |
| 7901 | ' the accepted keys should be integers and slice ' |
| 7902 | 'objects. Note that\n' |
| 7903 | ' the special interpretation of negative indexes (if the ' |
| 7904 | 'class wishes\n' |
| 7905 | ' to emulate a sequence type) is up to the ' |
| 7906 | '"__getitem__()" method. If\n' |
| 7907 | ' *key* is of an inappropriate type, "TypeError" may be ' |
| 7908 | 'raised; if of\n' |
| 7909 | ' a value outside the set of indexes for the sequence ' |
| 7910 | '(after any\n' |
| 7911 | ' special interpretation of negative values), ' |
| 7912 | '"IndexError" should be\n' |
| 7913 | ' raised. For mapping types, if *key* is missing (not in ' |
| 7914 | 'the\n' |
| 7915 | ' container), "KeyError" should be raised.\n' |
| 7916 | '\n' |
| 7917 | ' Note: "for" loops expect that an "IndexError" will be ' |
| 7918 | 'raised for\n' |
| 7919 | ' illegal indexes to allow proper detection of the end ' |
| 7920 | 'of the\n' |
| 7921 | ' sequence.\n' |
| 7922 | '\n' |
| 7923 | 'object.__missing__(self, key)\n' |
| 7924 | '\n' |
| 7925 | ' Called by "dict"."__getitem__()" to implement ' |
| 7926 | '"self[key]" for dict\n' |
| 7927 | ' subclasses when key is not in the dictionary.\n' |
| 7928 | '\n' |
| 7929 | 'object.__setitem__(self, key, value)\n' |
| 7930 | '\n' |
| 7931 | ' Called to implement assignment to "self[key]". Same ' |
| 7932 | 'note as for\n' |
| 7933 | ' "__getitem__()". This should only be implemented for ' |
| 7934 | 'mappings if\n' |
| 7935 | ' the objects support changes to the values for keys, or ' |
| 7936 | 'if new keys\n' |
| 7937 | ' can be added, or for sequences if elements can be ' |
| 7938 | 'replaced. The\n' |
| 7939 | ' same exceptions should be raised for improper *key* ' |
| 7940 | 'values as for\n' |
| 7941 | ' the "__getitem__()" method.\n' |
| 7942 | '\n' |
| 7943 | 'object.__delitem__(self, key)\n' |
| 7944 | '\n' |
| 7945 | ' Called to implement deletion of "self[key]". Same note ' |
| 7946 | 'as for\n' |
| 7947 | ' "__getitem__()". This should only be implemented for ' |
| 7948 | 'mappings if\n' |
| 7949 | ' the objects support removal of keys, or for sequences ' |
| 7950 | 'if elements\n' |
| 7951 | ' can be removed from the sequence. The same exceptions ' |
| 7952 | 'should be\n' |
| 7953 | ' raised for improper *key* values as for the ' |
| 7954 | '"__getitem__()" method.\n' |
| 7955 | '\n' |
| 7956 | 'object.__iter__(self)\n' |
| 7957 | '\n' |
| 7958 | ' This method is called when an iterator is required for ' |
| 7959 | 'a container.\n' |
| 7960 | ' This method should return a new iterator object that ' |
| 7961 | 'can iterate\n' |
| 7962 | ' over all the objects in the container. For mappings, ' |
| 7963 | 'it should\n' |
| 7964 | ' iterate over the keys of the container, and should also ' |
| 7965 | 'be made\n' |
| 7966 | ' available as the method "iterkeys()".\n' |
| 7967 | '\n' |
| 7968 | ' Iterator objects also need to implement this method; ' |
| 7969 | 'they are\n' |
| 7970 | ' required to return themselves. For more information on ' |
| 7971 | 'iterator\n' |
| 7972 | ' objects, see Iterator Types.\n' |
| 7973 | '\n' |
| 7974 | 'object.__reversed__(self)\n' |
| 7975 | '\n' |
| 7976 | ' Called (if present) by the "reversed()" built-in to ' |
| 7977 | 'implement\n' |
| 7978 | ' reverse iteration. It should return a new iterator ' |
| 7979 | 'object that\n' |
| 7980 | ' iterates over all the objects in the container in ' |
| 7981 | 'reverse order.\n' |
| 7982 | '\n' |
| 7983 | ' If the "__reversed__()" method is not provided, the ' |
| 7984 | '"reversed()"\n' |
| 7985 | ' built-in will fall back to using the sequence protocol ' |
| 7986 | '("__len__()"\n' |
| 7987 | ' and "__getitem__()"). Objects that support the ' |
| 7988 | 'sequence protocol\n' |
| 7989 | ' should only provide "__reversed__()" if they can ' |
| 7990 | 'provide an\n' |
| 7991 | ' implementation that is more efficient than the one ' |
| 7992 | 'provided by\n' |
| 7993 | ' "reversed()".\n' |
| 7994 | '\n' |
| 7995 | ' New in version 2.6.\n' |
| 7996 | '\n' |
| 7997 | 'The membership test operators ("in" and "not in") are ' |
| 7998 | 'normally\n' |
| 7999 | 'implemented as an iteration through a sequence. However, ' |
| 8000 | 'container\n' |
| 8001 | 'objects can supply the following special method with a ' |
| 8002 | 'more efficient\n' |
| 8003 | 'implementation, which also does not require the object be ' |
| 8004 | 'a sequence.\n' |
| 8005 | '\n' |
| 8006 | 'object.__contains__(self, item)\n' |
| 8007 | '\n' |
| 8008 | ' Called to implement membership test operators. Should ' |
| 8009 | 'return true\n' |
| 8010 | ' if *item* is in *self*, false otherwise. For mapping ' |
| 8011 | 'objects, this\n' |
| 8012 | ' should consider the keys of the mapping rather than the ' |
| 8013 | 'values or\n' |
| 8014 | ' the key-item pairs.\n' |
| 8015 | '\n' |
| 8016 | ' For objects that don\'t define "__contains__()", the ' |
| 8017 | 'membership test\n' |
| 8018 | ' first tries iteration via "__iter__()", then the old ' |
| 8019 | 'sequence\n' |
| 8020 | ' iteration protocol via "__getitem__()", see this ' |
| 8021 | 'section in the\n' |
| 8022 | ' language reference.\n' |
| 8023 | '\n' |
| 8024 | '\n' |
| 8025 | 'Additional methods for emulation of sequence types\n' |
| 8026 | '==================================================\n' |
| 8027 | '\n' |
| 8028 | 'The following optional methods can be defined to further ' |
| 8029 | 'emulate\n' |
| 8030 | 'sequence objects. Immutable sequences methods should at ' |
| 8031 | 'most only\n' |
| 8032 | 'define "__getslice__()"; mutable sequences might define ' |
| 8033 | 'all three\n' |
| 8034 | 'methods.\n' |
| 8035 | '\n' |
| 8036 | 'object.__getslice__(self, i, j)\n' |
| 8037 | '\n' |
| 8038 | ' Deprecated since version 2.0: Support slice objects as ' |
| 8039 | 'parameters\n' |
| 8040 | ' to the "__getitem__()" method. (However, built-in types ' |
| 8041 | 'in CPython\n' |
| 8042 | ' currently still implement "__getslice__()". Therefore, ' |
| 8043 | 'you have to\n' |
| 8044 | ' override it in derived classes when implementing ' |
| 8045 | 'slicing.)\n' |
| 8046 | '\n' |
| 8047 | ' Called to implement evaluation of "self[i:j]". The ' |
| 8048 | 'returned object\n' |
| 8049 | ' should be of the same type as *self*. Note that ' |
| 8050 | 'missing *i* or *j*\n' |
| 8051 | ' in the slice expression are replaced by zero or ' |
| 8052 | '"sys.maxsize",\n' |
| 8053 | ' respectively. If negative indexes are used in the ' |
| 8054 | 'slice, the\n' |
| 8055 | ' length of the sequence is added to that index. If the ' |
| 8056 | 'instance does\n' |
| 8057 | ' not implement the "__len__()" method, an ' |
| 8058 | '"AttributeError" is\n' |
| 8059 | ' raised. No guarantee is made that indexes adjusted this ' |
| 8060 | 'way are not\n' |
| 8061 | ' still negative. Indexes which are greater than the ' |
| 8062 | 'length of the\n' |
| 8063 | ' sequence are not modified. If no "__getslice__()" is ' |
| 8064 | 'found, a slice\n' |
| 8065 | ' object is created instead, and passed to ' |
| 8066 | '"__getitem__()" instead.\n' |
| 8067 | '\n' |
| 8068 | 'object.__setslice__(self, i, j, sequence)\n' |
| 8069 | '\n' |
| 8070 | ' Called to implement assignment to "self[i:j]". Same ' |
| 8071 | 'notes for *i*\n' |
| 8072 | ' and *j* as for "__getslice__()".\n' |
| 8073 | '\n' |
| 8074 | ' This method is deprecated. If no "__setslice__()" is ' |
| 8075 | 'found, or for\n' |
| 8076 | ' extended slicing of the form "self[i:j:k]", a slice ' |
| 8077 | 'object is\n' |
| 8078 | ' created, and passed to "__setitem__()", instead of ' |
| 8079 | '"__setslice__()"\n' |
| 8080 | ' being called.\n' |
| 8081 | '\n' |
| 8082 | 'object.__delslice__(self, i, j)\n' |
| 8083 | '\n' |
| 8084 | ' Called to implement deletion of "self[i:j]". Same notes ' |
| 8085 | 'for *i* and\n' |
| 8086 | ' *j* as for "__getslice__()". This method is deprecated. ' |
| 8087 | 'If no\n' |
| 8088 | ' "__delslice__()" is found, or for extended slicing of ' |
| 8089 | 'the form\n' |
| 8090 | ' "self[i:j:k]", a slice object is created, and passed ' |
| 8091 | 'to\n' |
| 8092 | ' "__delitem__()", instead of "__delslice__()" being ' |
| 8093 | 'called.\n' |
| 8094 | '\n' |
| 8095 | 'Notice that these methods are only invoked when a single ' |
| 8096 | 'slice with a\n' |
| 8097 | 'single colon is used, and the slice method is available. ' |
| 8098 | 'For slice\n' |
| 8099 | 'operations involving extended slice notation, or in ' |
| 8100 | 'absence of the\n' |
| 8101 | 'slice methods, "__getitem__()", "__setitem__()" or ' |
| 8102 | '"__delitem__()" is\n' |
| 8103 | 'called with a slice object as argument.\n' |
| 8104 | '\n' |
| 8105 | 'The following example demonstrate how to make your program ' |
| 8106 | 'or module\n' |
| 8107 | 'compatible with earlier versions of Python (assuming that ' |
| 8108 | 'methods\n' |
| 8109 | '"__getitem__()", "__setitem__()" and "__delitem__()" ' |
| 8110 | 'support slice\n' |
| 8111 | 'objects as arguments):\n' |
| 8112 | '\n' |
| 8113 | ' class MyClass:\n' |
| 8114 | ' ...\n' |
| 8115 | ' def __getitem__(self, index):\n' |
| 8116 | ' ...\n' |
| 8117 | ' def __setitem__(self, index, value):\n' |
| 8118 | ' ...\n' |
| 8119 | ' def __delitem__(self, index):\n' |
| 8120 | ' ...\n' |
| 8121 | '\n' |
| 8122 | ' if sys.version_info < (2, 0):\n' |
| 8123 | " # They won't be defined if version is at least " |
| 8124 | '2.0 final\n' |
| 8125 | '\n' |
| 8126 | ' def __getslice__(self, i, j):\n' |
| 8127 | ' return self[max(0, i):max(0, j):]\n' |
| 8128 | ' def __setslice__(self, i, j, seq):\n' |
| 8129 | ' self[max(0, i):max(0, j):] = seq\n' |
| 8130 | ' def __delslice__(self, i, j):\n' |
| 8131 | ' del self[max(0, i):max(0, j):]\n' |
| 8132 | ' ...\n' |
| 8133 | '\n' |
| 8134 | 'Note the calls to "max()"; these are necessary because of ' |
| 8135 | 'the handling\n' |
| 8136 | 'of negative indices before the "__*slice__()" methods are ' |
| 8137 | 'called.\n' |
| 8138 | 'When negative indexes are used, the "__*item__()" methods ' |
| 8139 | 'receive them\n' |
| 8140 | 'as provided, but the "__*slice__()" methods get a "cooked" ' |
| 8141 | 'form of the\n' |
| 8142 | 'index values. For each negative index value, the length ' |
| 8143 | 'of the\n' |
| 8144 | 'sequence is added to the index before calling the method ' |
| 8145 | '(which may\n' |
| 8146 | 'still result in a negative index); this is the customary ' |
| 8147 | 'handling of\n' |
| 8148 | 'negative indexes by the built-in sequence types, and the ' |
| 8149 | '"__*item__()"\n' |
| 8150 | 'methods are expected to do this as well. However, since ' |
| 8151 | 'they should\n' |
| 8152 | 'already be doing that, negative indexes cannot be passed ' |
| 8153 | 'in; they must\n' |
| 8154 | 'be constrained to the bounds of the sequence before being ' |
| 8155 | 'passed to\n' |
| 8156 | 'the "__*item__()" methods. Calling "max(0, i)" ' |
| 8157 | 'conveniently returns\n' |
| 8158 | 'the proper value.\n' |
| 8159 | '\n' |
| 8160 | '\n' |
| 8161 | 'Emulating numeric types\n' |
| 8162 | '=======================\n' |
| 8163 | '\n' |
| 8164 | 'The following methods can be defined to emulate numeric ' |
| 8165 | 'objects.\n' |
| 8166 | 'Methods corresponding to operations that are not supported ' |
| 8167 | 'by the\n' |
| 8168 | 'particular kind of number implemented (e.g., bitwise ' |
| 8169 | 'operations for\n' |
| 8170 | 'non-integral numbers) should be left undefined.\n' |
| 8171 | '\n' |
| 8172 | 'object.__add__(self, other)\n' |
| 8173 | 'object.__sub__(self, other)\n' |
| 8174 | 'object.__mul__(self, other)\n' |
| 8175 | 'object.__floordiv__(self, other)\n' |
| 8176 | 'object.__mod__(self, other)\n' |
| 8177 | 'object.__divmod__(self, other)\n' |
| 8178 | 'object.__pow__(self, other[, modulo])\n' |
| 8179 | 'object.__lshift__(self, other)\n' |
| 8180 | 'object.__rshift__(self, other)\n' |
| 8181 | 'object.__and__(self, other)\n' |
| 8182 | 'object.__xor__(self, other)\n' |
| 8183 | 'object.__or__(self, other)\n' |
| 8184 | '\n' |
| 8185 | ' These methods are called to implement the binary ' |
| 8186 | 'arithmetic\n' |
| 8187 | ' operations ("+", "-", "*", "//", "%", "divmod()", ' |
| 8188 | '"pow()", "**",\n' |
| 8189 | ' "<<", ">>", "&", "^", "|"). For instance, to evaluate ' |
| 8190 | 'the\n' |
| 8191 | ' expression "x + y", where *x* is an instance of a class ' |
| 8192 | 'that has an\n' |
| 8193 | ' "__add__()" method, "x.__add__(y)" is called. The ' |
| 8194 | '"__divmod__()"\n' |
| 8195 | ' method should be the equivalent to using ' |
| 8196 | '"__floordiv__()" and\n' |
| 8197 | ' "__mod__()"; it should not be related to ' |
| 8198 | '"__truediv__()" (described\n' |
| 8199 | ' below). Note that "__pow__()" should be defined to ' |
| 8200 | 'accept an\n' |
| 8201 | ' optional third argument if the ternary version of the ' |
| 8202 | 'built-in\n' |
| 8203 | ' "pow()" function is to be supported.\n' |
| 8204 | '\n' |
| 8205 | ' If one of those methods does not support the operation ' |
| 8206 | 'with the\n' |
| 8207 | ' supplied arguments, it should return "NotImplemented".\n' |
| 8208 | '\n' |
| 8209 | 'object.__div__(self, other)\n' |
| 8210 | 'object.__truediv__(self, other)\n' |
| 8211 | '\n' |
| 8212 | ' The division operator ("/") is implemented by these ' |
| 8213 | 'methods. The\n' |
| 8214 | ' "__truediv__()" method is used when ' |
| 8215 | '"__future__.division" is in\n' |
| 8216 | ' effect, otherwise "__div__()" is used. If only one of ' |
| 8217 | 'these two\n' |
| 8218 | ' methods is defined, the object will not support ' |
| 8219 | 'division in the\n' |
| 8220 | ' alternate context; "TypeError" will be raised instead.\n' |
| 8221 | '\n' |
| 8222 | 'object.__radd__(self, other)\n' |
| 8223 | 'object.__rsub__(self, other)\n' |
| 8224 | 'object.__rmul__(self, other)\n' |
| 8225 | 'object.__rdiv__(self, other)\n' |
| 8226 | 'object.__rtruediv__(self, other)\n' |
| 8227 | 'object.__rfloordiv__(self, other)\n' |
| 8228 | 'object.__rmod__(self, other)\n' |
| 8229 | 'object.__rdivmod__(self, other)\n' |
| 8230 | 'object.__rpow__(self, other)\n' |
| 8231 | 'object.__rlshift__(self, other)\n' |
| 8232 | 'object.__rrshift__(self, other)\n' |
| 8233 | 'object.__rand__(self, other)\n' |
| 8234 | 'object.__rxor__(self, other)\n' |
| 8235 | 'object.__ror__(self, other)\n' |
| 8236 | '\n' |
| 8237 | ' These methods are called to implement the binary ' |
| 8238 | 'arithmetic\n' |
| 8239 | ' operations ("+", "-", "*", "/", "%", "divmod()", ' |
| 8240 | '"pow()", "**",\n' |
| 8241 | ' "<<", ">>", "&", "^", "|") with reflected (swapped) ' |
| 8242 | 'operands.\n' |
| 8243 | ' These functions are only called if the left operand ' |
| 8244 | 'does not\n' |
| 8245 | ' support the corresponding operation and the operands ' |
| 8246 | 'are of\n' |
| 8247 | ' different types. [2] For instance, to evaluate the ' |
| 8248 | 'expression "x -\n' |
| 8249 | ' y", where *y* is an instance of a class that has an ' |
| 8250 | '"__rsub__()"\n' |
| 8251 | ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" ' |
| 8252 | 'returns\n' |
| 8253 | ' *NotImplemented*.\n' |
| 8254 | '\n' |
| 8255 | ' Note that ternary "pow()" will not try calling ' |
| 8256 | '"__rpow__()" (the\n' |
| 8257 | ' coercion rules would become too complicated).\n' |
| 8258 | '\n' |
| 8259 | " Note: If the right operand's type is a subclass of the " |
| 8260 | 'left\n' |
| 8261 | " operand's type and that subclass provides the " |
| 8262 | 'reflected method\n' |
| 8263 | ' for the operation, this method will be called before ' |
| 8264 | 'the left\n' |
| 8265 | " operand's non-reflected method. This behavior allows " |
| 8266 | 'subclasses\n' |
| 8267 | " to override their ancestors' operations.\n" |
| 8268 | '\n' |
| 8269 | 'object.__iadd__(self, other)\n' |
| 8270 | 'object.__isub__(self, other)\n' |
| 8271 | 'object.__imul__(self, other)\n' |
| 8272 | 'object.__idiv__(self, other)\n' |
| 8273 | 'object.__itruediv__(self, other)\n' |
| 8274 | 'object.__ifloordiv__(self, other)\n' |
| 8275 | 'object.__imod__(self, other)\n' |
| 8276 | 'object.__ipow__(self, other[, modulo])\n' |
| 8277 | 'object.__ilshift__(self, other)\n' |
| 8278 | 'object.__irshift__(self, other)\n' |
| 8279 | 'object.__iand__(self, other)\n' |
| 8280 | 'object.__ixor__(self, other)\n' |
| 8281 | 'object.__ior__(self, other)\n' |
| 8282 | '\n' |
| 8283 | ' These methods are called to implement the augmented ' |
| 8284 | 'arithmetic\n' |
| 8285 | ' assignments ("+=", "-=", "*=", "/=", "//=", "%=", ' |
| 8286 | '"**=", "<<=",\n' |
| 8287 | ' ">>=", "&=", "^=", "|="). These methods should attempt ' |
| 8288 | 'to do the\n' |
| 8289 | ' operation in-place (modifying *self*) and return the ' |
| 8290 | 'result (which\n' |
| 8291 | ' could be, but does not have to be, *self*). If a ' |
| 8292 | 'specific method\n' |
| 8293 | ' is not defined, the augmented assignment falls back to ' |
| 8294 | 'the normal\n' |
| 8295 | ' methods. For instance, to execute the statement "x += ' |
| 8296 | 'y", where\n' |
| 8297 | ' *x* is an instance of a class that has an "__iadd__()" ' |
| 8298 | 'method,\n' |
| 8299 | ' "x.__iadd__(y)" is called. If *x* is an instance of a ' |
| 8300 | 'class that\n' |
| 8301 | ' does not define a "__iadd__()" method, "x.__add__(y)" ' |
| 8302 | 'and\n' |
| 8303 | ' "y.__radd__(x)" are considered, as with the evaluation ' |
| 8304 | 'of "x + y".\n' |
| 8305 | '\n' |
| 8306 | 'object.__neg__(self)\n' |
| 8307 | 'object.__pos__(self)\n' |
| 8308 | 'object.__abs__(self)\n' |
| 8309 | 'object.__invert__(self)\n' |
| 8310 | '\n' |
| 8311 | ' Called to implement the unary arithmetic operations ' |
| 8312 | '("-", "+",\n' |
| 8313 | ' "abs()" and "~").\n' |
| 8314 | '\n' |
| 8315 | 'object.__complex__(self)\n' |
| 8316 | 'object.__int__(self)\n' |
| 8317 | 'object.__long__(self)\n' |
| 8318 | 'object.__float__(self)\n' |
| 8319 | '\n' |
| 8320 | ' Called to implement the built-in functions "complex()", ' |
| 8321 | '"int()",\n' |
| 8322 | ' "long()", and "float()". Should return a value of the ' |
| 8323 | 'appropriate\n' |
| 8324 | ' type.\n' |
| 8325 | '\n' |
| 8326 | 'object.__oct__(self)\n' |
| 8327 | 'object.__hex__(self)\n' |
| 8328 | '\n' |
| 8329 | ' Called to implement the built-in functions "oct()" and ' |
| 8330 | '"hex()".\n' |
| 8331 | ' Should return a string value.\n' |
| 8332 | '\n' |
| 8333 | 'object.__index__(self)\n' |
| 8334 | '\n' |
| 8335 | ' Called to implement "operator.index()". Also called ' |
| 8336 | 'whenever\n' |
| 8337 | ' Python needs an integer object (such as in slicing). ' |
| 8338 | 'Must return\n' |
| 8339 | ' an integer (int or long).\n' |
| 8340 | '\n' |
| 8341 | ' New in version 2.5.\n' |
| 8342 | '\n' |
| 8343 | 'object.__coerce__(self, other)\n' |
| 8344 | '\n' |
| 8345 | ' Called to implement "mixed-mode" numeric arithmetic. ' |
| 8346 | 'Should either\n' |
| 8347 | ' return a 2-tuple containing *self* and *other* ' |
| 8348 | 'converted to a\n' |
| 8349 | ' common numeric type, or "None" if conversion is ' |
| 8350 | 'impossible. When\n' |
| 8351 | ' the common type would be the type of "other", it is ' |
| 8352 | 'sufficient to\n' |
| 8353 | ' return "None", since the interpreter will also ask the ' |
| 8354 | 'other object\n' |
| 8355 | ' to attempt a coercion (but sometimes, if the ' |
| 8356 | 'implementation of the\n' |
| 8357 | ' other type cannot be changed, it is useful to do the ' |
| 8358 | 'conversion to\n' |
| 8359 | ' the other type here). A return value of ' |
| 8360 | '"NotImplemented" is\n' |
| 8361 | ' equivalent to returning "None".\n' |
| 8362 | '\n' |
| 8363 | '\n' |
| 8364 | 'Coercion rules\n' |
| 8365 | '==============\n' |
| 8366 | '\n' |
| 8367 | 'This section used to document the rules for coercion. As ' |
| 8368 | 'the language\n' |
| 8369 | 'has evolved, the coercion rules have become hard to ' |
| 8370 | 'document\n' |
| 8371 | 'precisely; documenting what one version of one particular\n' |
| 8372 | 'implementation does is undesirable. Instead, here are ' |
| 8373 | 'some informal\n' |
| 8374 | 'guidelines regarding coercion. In Python 3, coercion will ' |
| 8375 | 'not be\n' |
| 8376 | 'supported.\n' |
| 8377 | '\n' |
| 8378 | '* If the left operand of a % operator is a string or ' |
| 8379 | 'Unicode object,\n' |
| 8380 | ' no coercion takes place and the string formatting ' |
| 8381 | 'operation is\n' |
| 8382 | ' invoked instead.\n' |
| 8383 | '\n' |
| 8384 | '* It is no longer recommended to define a coercion ' |
| 8385 | 'operation. Mixed-\n' |
| 8386 | " mode operations on types that don't define coercion pass " |
| 8387 | 'the\n' |
| 8388 | ' original arguments to the operation.\n' |
| 8389 | '\n' |
| 8390 | '* New-style classes (those derived from "object") never ' |
| 8391 | 'invoke the\n' |
| 8392 | ' "__coerce__()" method in response to a binary operator; ' |
| 8393 | 'the only\n' |
| 8394 | ' time "__coerce__()" is invoked is when the built-in ' |
| 8395 | 'function\n' |
| 8396 | ' "coerce()" is called.\n' |
| 8397 | '\n' |
| 8398 | '* For most intents and purposes, an operator that returns\n' |
| 8399 | ' "NotImplemented" is treated the same as one that is not ' |
| 8400 | 'implemented\n' |
| 8401 | ' at all.\n' |
| 8402 | '\n' |
| 8403 | '* Below, "__op__()" and "__rop__()" are used to signify ' |
| 8404 | 'the generic\n' |
| 8405 | ' method names corresponding to an operator; "__iop__()" ' |
| 8406 | 'is used for\n' |
| 8407 | ' the corresponding in-place operator. For example, for ' |
| 8408 | 'the operator\n' |
| 8409 | ' \'"+"\', "__add__()" and "__radd__()" are used for the ' |
| 8410 | 'left and right\n' |
| 8411 | ' variant of the binary operator, and "__iadd__()" for the ' |
| 8412 | 'in-place\n' |
| 8413 | ' variant.\n' |
| 8414 | '\n' |
| 8415 | '* For objects *x* and *y*, first "x.__op__(y)" is tried. ' |
| 8416 | 'If this is\n' |
| 8417 | ' not implemented or returns "NotImplemented", ' |
| 8418 | '"y.__rop__(x)" is\n' |
| 8419 | ' tried. If this is also not implemented or returns ' |
| 8420 | '"NotImplemented",\n' |
| 8421 | ' a "TypeError" exception is raised. But see the ' |
| 8422 | 'following exception:\n' |
| 8423 | '\n' |
| 8424 | '* Exception to the previous item: if the left operand is ' |
| 8425 | 'an instance\n' |
| 8426 | ' of a built-in type or a new-style class, and the right ' |
| 8427 | 'operand is an\n' |
| 8428 | ' instance of a proper subclass of that type or class and ' |
| 8429 | 'overrides\n' |
| 8430 | ' the base\'s "__rop__()" method, the right operand\'s ' |
| 8431 | '"__rop__()"\n' |
| 8432 | ' method is tried *before* the left operand\'s "__op__()" ' |
| 8433 | 'method.\n' |
| 8434 | '\n' |
| 8435 | ' This is done so that a subclass can completely override ' |
| 8436 | 'binary\n' |
| 8437 | ' operators. Otherwise, the left operand\'s "__op__()" ' |
| 8438 | 'method would\n' |
| 8439 | ' always accept the right operand: when an instance of a ' |
| 8440 | 'given class\n' |
| 8441 | ' is expected, an instance of a subclass of that class is ' |
| 8442 | 'always\n' |
| 8443 | ' acceptable.\n' |
| 8444 | '\n' |
| 8445 | '* When either operand type defines a coercion, this ' |
| 8446 | 'coercion is\n' |
| 8447 | ' called before that type\'s "__op__()" or "__rop__()" ' |
| 8448 | 'method is\n' |
| 8449 | ' called, but no sooner. If the coercion returns an ' |
| 8450 | 'object of a\n' |
| 8451 | ' different type for the operand whose coercion is ' |
| 8452 | 'invoked, part of\n' |
| 8453 | ' the process is redone using the new object.\n' |
| 8454 | '\n' |
| 8455 | '* When an in-place operator (like \'"+="\') is used, if ' |
| 8456 | 'the left\n' |
| 8457 | ' operand implements "__iop__()", it is invoked without ' |
| 8458 | 'any coercion.\n' |
| 8459 | ' When the operation falls back to "__op__()" and/or ' |
| 8460 | '"__rop__()", the\n' |
| 8461 | ' normal coercion rules apply.\n' |
| 8462 | '\n' |
| 8463 | '* In "x + y", if *x* is a sequence that implements ' |
| 8464 | 'sequence\n' |
| 8465 | ' concatenation, sequence concatenation is invoked.\n' |
| 8466 | '\n' |
| 8467 | '* In "x * y", if one operand is a sequence that implements ' |
| 8468 | 'sequence\n' |
| 8469 | ' repetition, and the other is an integer ("int" or ' |
| 8470 | '"long"), sequence\n' |
| 8471 | ' repetition is invoked.\n' |
| 8472 | '\n' |
| 8473 | '* Rich comparisons (implemented by methods "__eq__()" and ' |
| 8474 | 'so on)\n' |
| 8475 | ' never use coercion. Three-way comparison (implemented ' |
| 8476 | 'by\n' |
| 8477 | ' "__cmp__()") does use coercion under the same conditions ' |
| 8478 | 'as other\n' |
| 8479 | ' binary operations use it.\n' |
| 8480 | '\n' |
| 8481 | '* In the current implementation, the built-in numeric ' |
| 8482 | 'types "int",\n' |
| 8483 | ' "long", "float", and "complex" do not use coercion. All ' |
| 8484 | 'these types\n' |
| 8485 | ' implement a "__coerce__()" method, for use by the ' |
| 8486 | 'built-in\n' |
| 8487 | ' "coerce()" function.\n' |
| 8488 | '\n' |
| 8489 | ' Changed in version 2.7: The complex type no longer makes ' |
| 8490 | 'implicit\n' |
| 8491 | ' calls to the "__coerce__()" method for mixed-type binary ' |
| 8492 | 'arithmetic\n' |
| 8493 | ' operations.\n' |
| 8494 | '\n' |
| 8495 | '\n' |
| 8496 | 'With Statement Context Managers\n' |
| 8497 | '===============================\n' |
| 8498 | '\n' |
| 8499 | 'New in version 2.5.\n' |
| 8500 | '\n' |
| 8501 | 'A *context manager* is an object that defines the runtime ' |
| 8502 | 'context to\n' |
| 8503 | 'be established when executing a "with" statement. The ' |
| 8504 | 'context manager\n' |
| 8505 | 'handles the entry into, and the exit from, the desired ' |
| 8506 | 'runtime context\n' |
| 8507 | 'for the execution of the block of code. Context managers ' |
| 8508 | 'are normally\n' |
| 8509 | 'invoked using the "with" statement (described in section ' |
| 8510 | 'The with\n' |
| 8511 | 'statement), but can also be used by directly invoking ' |
| 8512 | 'their methods.\n' |
| 8513 | '\n' |
| 8514 | 'Typical uses of context managers include saving and ' |
| 8515 | 'restoring various\n' |
| 8516 | 'kinds of global state, locking and unlocking resources, ' |
| 8517 | 'closing opened\n' |
| 8518 | 'files, etc.\n' |
| 8519 | '\n' |
| 8520 | 'For more information on context managers, see Context ' |
| 8521 | 'Manager Types.\n' |
| 8522 | '\n' |
| 8523 | 'object.__enter__(self)\n' |
| 8524 | '\n' |
| 8525 | ' Enter the runtime context related to this object. The ' |
| 8526 | '"with"\n' |
| 8527 | " statement will bind this method's return value to the " |
| 8528 | 'target(s)\n' |
| 8529 | ' specified in the "as" clause of the statement, if any.\n' |
| 8530 | '\n' |
| 8531 | 'object.__exit__(self, exc_type, exc_value, traceback)\n' |
| 8532 | '\n' |
| 8533 | ' Exit the runtime context related to this object. The ' |
| 8534 | 'parameters\n' |
| 8535 | ' describe the exception that caused the context to be ' |
| 8536 | 'exited. If the\n' |
| 8537 | ' context was exited without an exception, all three ' |
| 8538 | 'arguments will\n' |
| 8539 | ' be "None".\n' |
| 8540 | '\n' |
| 8541 | ' If an exception is supplied, and the method wishes to ' |
| 8542 | 'suppress the\n' |
| 8543 | ' exception (i.e., prevent it from being propagated), it ' |
| 8544 | 'should\n' |
| 8545 | ' return a true value. Otherwise, the exception will be ' |
| 8546 | 'processed\n' |
| 8547 | ' normally upon exit from this method.\n' |
| 8548 | '\n' |
| 8549 | ' Note that "__exit__()" methods should not reraise the ' |
| 8550 | 'passed-in\n' |
| 8551 | " exception; this is the caller's responsibility.\n" |
| 8552 | '\n' |
| 8553 | 'See also: **PEP 0343** - The "with" statement\n' |
| 8554 | '\n' |
| 8555 | ' The specification, background, and examples for the ' |
| 8556 | 'Python "with"\n' |
| 8557 | ' statement.\n' |
| 8558 | '\n' |
| 8559 | '\n' |
| 8560 | 'Special method lookup for old-style classes\n' |
| 8561 | '===========================================\n' |
| 8562 | '\n' |
| 8563 | 'For old-style classes, special methods are always looked ' |
| 8564 | 'up in exactly\n' |
| 8565 | 'the same way as any other method or attribute. This is the ' |
| 8566 | 'case\n' |
| 8567 | 'regardless of whether the method is being looked up ' |
| 8568 | 'explicitly as in\n' |
| 8569 | '"x.__getitem__(i)" or implicitly as in "x[i]".\n' |
| 8570 | '\n' |
| 8571 | 'This behaviour means that special methods may exhibit ' |
| 8572 | 'different\n' |
| 8573 | 'behaviour for different instances of a single old-style ' |
| 8574 | 'class if the\n' |
| 8575 | 'appropriate special attributes are set differently:\n' |
| 8576 | '\n' |
| 8577 | ' >>> class C:\n' |
| 8578 | ' ... pass\n' |
| 8579 | ' ...\n' |
| 8580 | ' >>> c1 = C()\n' |
| 8581 | ' >>> c2 = C()\n' |
| 8582 | ' >>> c1.__len__ = lambda: 5\n' |
| 8583 | ' >>> c2.__len__ = lambda: 9\n' |
| 8584 | ' >>> len(c1)\n' |
| 8585 | ' 5\n' |
| 8586 | ' >>> len(c2)\n' |
| 8587 | ' 9\n' |
| 8588 | '\n' |
| 8589 | '\n' |
| 8590 | 'Special method lookup for new-style classes\n' |
| 8591 | '===========================================\n' |
| 8592 | '\n' |
| 8593 | 'For new-style classes, implicit invocations of special ' |
| 8594 | 'methods are\n' |
| 8595 | 'only guaranteed to work correctly if defined on an ' |
| 8596 | "object's type, not\n" |
| 8597 | "in the object's instance dictionary. That behaviour is " |
| 8598 | 'the reason why\n' |
| 8599 | 'the following code raises an exception (unlike the ' |
| 8600 | 'equivalent example\n' |
| 8601 | 'with old-style classes):\n' |
| 8602 | '\n' |
| 8603 | ' >>> class C(object):\n' |
| 8604 | ' ... pass\n' |
| 8605 | ' ...\n' |
| 8606 | ' >>> c = C()\n' |
| 8607 | ' >>> c.__len__ = lambda: 5\n' |
| 8608 | ' >>> len(c)\n' |
| 8609 | ' Traceback (most recent call last):\n' |
| 8610 | ' File "<stdin>", line 1, in <module>\n' |
| 8611 | " TypeError: object of type 'C' has no len()\n" |
| 8612 | '\n' |
| 8613 | 'The rationale behind this behaviour lies with a number of ' |
| 8614 | 'special\n' |
| 8615 | 'methods such as "__hash__()" and "__repr__()" that are ' |
| 8616 | 'implemented by\n' |
| 8617 | 'all objects, including type objects. If the implicit ' |
| 8618 | 'lookup of these\n' |
| 8619 | 'methods used the conventional lookup process, they would ' |
| 8620 | 'fail when\n' |
| 8621 | 'invoked on the type object itself:\n' |
| 8622 | '\n' |
| 8623 | ' >>> 1 .__hash__() == hash(1)\n' |
| 8624 | ' True\n' |
| 8625 | ' >>> int.__hash__() == hash(int)\n' |
| 8626 | ' Traceback (most recent call last):\n' |
| 8627 | ' File "<stdin>", line 1, in <module>\n' |
| 8628 | " TypeError: descriptor '__hash__' of 'int' object needs " |
| 8629 | 'an argument\n' |
| 8630 | '\n' |
| 8631 | 'Incorrectly attempting to invoke an unbound method of a ' |
| 8632 | 'class in this\n' |
| 8633 | "way is sometimes referred to as 'metaclass confusion', and " |
| 8634 | 'is avoided\n' |
| 8635 | 'by bypassing the instance when looking up special ' |
| 8636 | 'methods:\n' |
| 8637 | '\n' |
| 8638 | ' >>> type(1).__hash__(1) == hash(1)\n' |
| 8639 | ' True\n' |
| 8640 | ' >>> type(int).__hash__(int) == hash(int)\n' |
| 8641 | ' True\n' |
| 8642 | '\n' |
| 8643 | 'In addition to bypassing any instance attributes in the ' |
| 8644 | 'interest of\n' |
| 8645 | 'correctness, implicit special method lookup generally also ' |
| 8646 | 'bypasses\n' |
| 8647 | 'the "__getattribute__()" method even of the object\'s ' |
| 8648 | 'metaclass:\n' |
| 8649 | '\n' |
| 8650 | ' >>> class Meta(type):\n' |
| 8651 | ' ... def __getattribute__(*args):\n' |
| 8652 | ' ... print "Metaclass getattribute invoked"\n' |
| 8653 | ' ... return type.__getattribute__(*args)\n' |
| 8654 | ' ...\n' |
| 8655 | ' >>> class C(object):\n' |
| 8656 | ' ... __metaclass__ = Meta\n' |
| 8657 | ' ... def __len__(self):\n' |
| 8658 | ' ... return 10\n' |
| 8659 | ' ... def __getattribute__(*args):\n' |
| 8660 | ' ... print "Class getattribute invoked"\n' |
| 8661 | ' ... return object.__getattribute__(*args)\n' |
| 8662 | ' ...\n' |
| 8663 | ' >>> c = C()\n' |
| 8664 | ' >>> c.__len__() # Explicit lookup via ' |
| 8665 | 'instance\n' |
| 8666 | ' Class getattribute invoked\n' |
| 8667 | ' 10\n' |
| 8668 | ' >>> type(c).__len__(c) # Explicit lookup via ' |
| 8669 | 'type\n' |
| 8670 | ' Metaclass getattribute invoked\n' |
| 8671 | ' 10\n' |
| 8672 | ' >>> len(c) # Implicit lookup\n' |
| 8673 | ' 10\n' |
| 8674 | '\n' |
| 8675 | 'Bypassing the "__getattribute__()" machinery in this ' |
| 8676 | 'fashion provides\n' |
| 8677 | 'significant scope for speed optimisations within the ' |
| 8678 | 'interpreter, at\n' |
| 8679 | 'the cost of some flexibility in the handling of special ' |
| 8680 | 'methods (the\n' |
| 8681 | 'special method *must* be set on the class object itself in ' |
| 8682 | 'order to be\n' |
| 8683 | 'consistently invoked by the interpreter).\n' |
| 8684 | '\n' |
| 8685 | '-[ Footnotes ]-\n' |
| 8686 | '\n' |
| 8687 | "[1] It *is* possible in some cases to change an object's " |
| 8688 | 'type,\n' |
| 8689 | ' under certain controlled conditions. It generally ' |
| 8690 | "isn't a good\n" |
| 8691 | ' idea though, since it can lead to some very strange ' |
| 8692 | 'behaviour if\n' |
| 8693 | ' it is handled incorrectly.\n' |
| 8694 | '\n' |
| 8695 | '[2] For operands of the same type, it is assumed that if ' |
| 8696 | 'the non-\n' |
| 8697 | ' reflected method (such as "__add__()") fails the ' |
| 8698 | 'operation is not\n' |
| 8699 | ' supported, which is why the reflected method is not ' |
| 8700 | 'called.\n', |
| 8701 | 'string-methods': '\n' |
| 8702 | 'String Methods\n' |
| 8703 | '**************\n' |
| 8704 | '\n' |
| 8705 | 'Below are listed the string methods which both 8-bit ' |
| 8706 | 'strings and\n' |
| 8707 | 'Unicode objects support. Some of them are also ' |
| 8708 | 'available on\n' |
| 8709 | '"bytearray" objects.\n' |
| 8710 | '\n' |
| 8711 | "In addition, Python's strings support the sequence type " |
| 8712 | 'methods\n' |
| 8713 | 'described in the Sequence Types --- str, unicode, list, ' |
| 8714 | 'tuple,\n' |
| 8715 | 'bytearray, buffer, xrange section. To output formatted ' |
| 8716 | 'strings use\n' |
| 8717 | 'template strings or the "%" operator described in the ' |
| 8718 | 'String\n' |
| 8719 | 'Formatting Operations section. Also, see the "re" module ' |
| 8720 | 'for string\n' |
| 8721 | 'functions based on regular expressions.\n' |
| 8722 | '\n' |
| 8723 | 'str.capitalize()\n' |
| 8724 | '\n' |
| 8725 | ' Return a copy of the string with its first character ' |
| 8726 | 'capitalized\n' |
| 8727 | ' and the rest lowercased.\n' |
| 8728 | '\n' |
| 8729 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 8730 | '\n' |
| 8731 | 'str.center(width[, fillchar])\n' |
| 8732 | '\n' |
| 8733 | ' Return centered in a string of length *width*. ' |
| 8734 | 'Padding is done\n' |
| 8735 | ' using the specified *fillchar* (default is a space).\n' |
| 8736 | '\n' |
| 8737 | ' Changed in version 2.4: Support for the *fillchar* ' |
| 8738 | 'argument.\n' |
| 8739 | '\n' |
| 8740 | 'str.count(sub[, start[, end]])\n' |
| 8741 | '\n' |
| 8742 | ' Return the number of non-overlapping occurrences of ' |
| 8743 | 'substring *sub*\n' |
| 8744 | ' in the range [*start*, *end*]. Optional arguments ' |
| 8745 | '*start* and\n' |
| 8746 | ' *end* are interpreted as in slice notation.\n' |
| 8747 | '\n' |
| 8748 | 'str.decode([encoding[, errors]])\n' |
| 8749 | '\n' |
| 8750 | ' Decodes the string using the codec registered for ' |
| 8751 | '*encoding*.\n' |
| 8752 | ' *encoding* defaults to the default string encoding. ' |
| 8753 | '*errors* may\n' |
| 8754 | ' be given to set a different error handling scheme. ' |
| 8755 | 'The default is\n' |
| 8756 | ' "\'strict\'", meaning that encoding errors raise ' |
| 8757 | '"UnicodeError".\n' |
| 8758 | ' Other possible values are "\'ignore\'", "\'replace\'" ' |
| 8759 | 'and any other\n' |
| 8760 | ' name registered via "codecs.register_error()", see ' |
| 8761 | 'section Codec\n' |
| 8762 | ' Base Classes.\n' |
| 8763 | '\n' |
| 8764 | ' New in version 2.2.\n' |
| 8765 | '\n' |
| 8766 | ' Changed in version 2.3: Support for other error ' |
| 8767 | 'handling schemes\n' |
| 8768 | ' added.\n' |
| 8769 | '\n' |
| 8770 | ' Changed in version 2.7: Support for keyword arguments ' |
| 8771 | 'added.\n' |
| 8772 | '\n' |
| 8773 | 'str.encode([encoding[, errors]])\n' |
| 8774 | '\n' |
| 8775 | ' Return an encoded version of the string. Default ' |
| 8776 | 'encoding is the\n' |
| 8777 | ' current default string encoding. *errors* may be ' |
| 8778 | 'given to set a\n' |
| 8779 | ' different error handling scheme. The default for ' |
| 8780 | '*errors* is\n' |
| 8781 | ' "\'strict\'", meaning that encoding errors raise a ' |
| 8782 | '"UnicodeError".\n' |
| 8783 | ' Other possible values are "\'ignore\'", ' |
| 8784 | '"\'replace\'",\n' |
| 8785 | ' "\'xmlcharrefreplace\'", "\'backslashreplace\'" and ' |
| 8786 | 'any other name\n' |
| 8787 | ' registered via "codecs.register_error()", see section ' |
| 8788 | 'Codec Base\n' |
| 8789 | ' Classes. For a list of possible encodings, see ' |
| 8790 | 'section Standard\n' |
| 8791 | ' Encodings.\n' |
| 8792 | '\n' |
| 8793 | ' New in version 2.0.\n' |
| 8794 | '\n' |
| 8795 | ' Changed in version 2.3: Support for ' |
| 8796 | '"\'xmlcharrefreplace\'" and\n' |
| 8797 | ' "\'backslashreplace\'" and other error handling ' |
| 8798 | 'schemes added.\n' |
| 8799 | '\n' |
| 8800 | ' Changed in version 2.7: Support for keyword arguments ' |
| 8801 | 'added.\n' |
| 8802 | '\n' |
| 8803 | 'str.endswith(suffix[, start[, end]])\n' |
| 8804 | '\n' |
| 8805 | ' Return "True" if the string ends with the specified ' |
| 8806 | '*suffix*,\n' |
| 8807 | ' otherwise return "False". *suffix* can also be a ' |
| 8808 | 'tuple of suffixes\n' |
| 8809 | ' to look for. With optional *start*, test beginning ' |
| 8810 | 'at that\n' |
| 8811 | ' position. With optional *end*, stop comparing at ' |
| 8812 | 'that position.\n' |
| 8813 | '\n' |
| 8814 | ' Changed in version 2.5: Accept tuples as *suffix*.\n' |
| 8815 | '\n' |
| 8816 | 'str.expandtabs([tabsize])\n' |
| 8817 | '\n' |
| 8818 | ' Return a copy of the string where all tab characters ' |
| 8819 | 'are replaced\n' |
| 8820 | ' by one or more spaces, depending on the current ' |
| 8821 | 'column and the\n' |
| 8822 | ' given tab size. Tab positions occur every *tabsize* ' |
| 8823 | 'characters\n' |
| 8824 | ' (default is 8, giving tab positions at columns 0, 8, ' |
| 8825 | '16 and so on).\n' |
| 8826 | ' To expand the string, the current column is set to ' |
| 8827 | 'zero and the\n' |
| 8828 | ' string is examined character by character. If the ' |
| 8829 | 'character is a\n' |
| 8830 | ' tab ("\\t"), one or more space characters are ' |
| 8831 | 'inserted in the result\n' |
| 8832 | ' until the current column is equal to the next tab ' |
| 8833 | 'position. (The\n' |
| 8834 | ' tab character itself is not copied.) If the ' |
| 8835 | 'character is a newline\n' |
| 8836 | ' ("\\n") or return ("\\r"), it is copied and the ' |
| 8837 | 'current column is\n' |
| 8838 | ' reset to zero. Any other character is copied ' |
| 8839 | 'unchanged and the\n' |
| 8840 | ' current column is incremented by one regardless of ' |
| 8841 | 'how the\n' |
| 8842 | ' character is represented when printed.\n' |
| 8843 | '\n' |
| 8844 | " >>> '01\\t012\\t0123\\t01234'.expandtabs()\n" |
| 8845 | " '01 012 0123 01234'\n" |
| 8846 | " >>> '01\\t012\\t0123\\t01234'.expandtabs(4)\n" |
| 8847 | " '01 012 0123 01234'\n" |
| 8848 | '\n' |
| 8849 | 'str.find(sub[, start[, end]])\n' |
| 8850 | '\n' |
| 8851 | ' Return the lowest index in the string where substring ' |
| 8852 | '*sub* is\n' |
| 8853 | ' found, such that *sub* is contained in the slice ' |
| 8854 | '"s[start:end]".\n' |
| 8855 | ' Optional arguments *start* and *end* are interpreted ' |
| 8856 | 'as in slice\n' |
| 8857 | ' notation. Return "-1" if *sub* is not found.\n' |
| 8858 | '\n' |
| 8859 | ' Note: The "find()" method should be used only if you ' |
| 8860 | 'need to know\n' |
| 8861 | ' the position of *sub*. To check if *sub* is a ' |
| 8862 | 'substring or not,\n' |
| 8863 | ' use the "in" operator:\n' |
| 8864 | '\n' |
| 8865 | " >>> 'Py' in 'Python'\n" |
| 8866 | ' True\n' |
| 8867 | '\n' |
| 8868 | 'str.format(*args, **kwargs)\n' |
| 8869 | '\n' |
| 8870 | ' Perform a string formatting operation. The string on ' |
| 8871 | 'which this\n' |
| 8872 | ' method is called can contain literal text or ' |
| 8873 | 'replacement fields\n' |
| 8874 | ' delimited by braces "{}". Each replacement field ' |
| 8875 | 'contains either\n' |
| 8876 | ' the numeric index of a positional argument, or the ' |
| 8877 | 'name of a\n' |
| 8878 | ' keyword argument. Returns a copy of the string where ' |
| 8879 | 'each\n' |
| 8880 | ' replacement field is replaced with the string value ' |
| 8881 | 'of the\n' |
| 8882 | ' corresponding argument.\n' |
| 8883 | '\n' |
| 8884 | ' >>> "The sum of 1 + 2 is {0}".format(1+2)\n' |
| 8885 | " 'The sum of 1 + 2 is 3'\n" |
| 8886 | '\n' |
| 8887 | ' See Format String Syntax for a description of the ' |
| 8888 | 'various\n' |
| 8889 | ' formatting options that can be specified in format ' |
| 8890 | 'strings.\n' |
| 8891 | '\n' |
| 8892 | ' This method of string formatting is the new standard ' |
| 8893 | 'in Python 3,\n' |
| 8894 | ' and should be preferred to the "%" formatting ' |
| 8895 | 'described in String\n' |
| 8896 | ' Formatting Operations in new code.\n' |
| 8897 | '\n' |
| 8898 | ' New in version 2.6.\n' |
| 8899 | '\n' |
| 8900 | 'str.index(sub[, start[, end]])\n' |
| 8901 | '\n' |
| 8902 | ' Like "find()", but raise "ValueError" when the ' |
| 8903 | 'substring is not\n' |
| 8904 | ' found.\n' |
| 8905 | '\n' |
| 8906 | 'str.isalnum()\n' |
| 8907 | '\n' |
| 8908 | ' Return true if all characters in the string are ' |
| 8909 | 'alphanumeric and\n' |
| 8910 | ' there is at least one character, false otherwise.\n' |
| 8911 | '\n' |
| 8912 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 8913 | '\n' |
| 8914 | 'str.isalpha()\n' |
| 8915 | '\n' |
| 8916 | ' Return true if all characters in the string are ' |
| 8917 | 'alphabetic and\n' |
| 8918 | ' there is at least one character, false otherwise.\n' |
| 8919 | '\n' |
| 8920 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 8921 | '\n' |
| 8922 | 'str.isdigit()\n' |
| 8923 | '\n' |
| 8924 | ' Return true if all characters in the string are ' |
| 8925 | 'digits and there is\n' |
| 8926 | ' at least one character, false otherwise.\n' |
| 8927 | '\n' |
| 8928 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 8929 | '\n' |
| 8930 | 'str.islower()\n' |
| 8931 | '\n' |
| 8932 | ' Return true if all cased characters [4] in the string ' |
| 8933 | 'are lowercase\n' |
| 8934 | ' and there is at least one cased character, false ' |
| 8935 | 'otherwise.\n' |
| 8936 | '\n' |
| 8937 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 8938 | '\n' |
| 8939 | 'str.isspace()\n' |
| 8940 | '\n' |
| 8941 | ' Return true if there are only whitespace characters ' |
| 8942 | 'in the string\n' |
| 8943 | ' and there is at least one character, false ' |
| 8944 | 'otherwise.\n' |
| 8945 | '\n' |
| 8946 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 8947 | '\n' |
| 8948 | 'str.istitle()\n' |
| 8949 | '\n' |
| 8950 | ' Return true if the string is a titlecased string and ' |
| 8951 | 'there is at\n' |
| 8952 | ' least one character, for example uppercase characters ' |
| 8953 | 'may only\n' |
| 8954 | ' follow uncased characters and lowercase characters ' |
| 8955 | 'only cased ones.\n' |
| 8956 | ' Return false otherwise.\n' |
| 8957 | '\n' |
| 8958 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 8959 | '\n' |
| 8960 | 'str.isupper()\n' |
| 8961 | '\n' |
| 8962 | ' Return true if all cased characters [4] in the string ' |
| 8963 | 'are uppercase\n' |
| 8964 | ' and there is at least one cased character, false ' |
| 8965 | 'otherwise.\n' |
| 8966 | '\n' |
| 8967 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 8968 | '\n' |
| 8969 | 'str.join(iterable)\n' |
| 8970 | '\n' |
| 8971 | ' Return a string which is the concatenation of the ' |
| 8972 | 'strings in the\n' |
| 8973 | ' *iterable* *iterable*. The separator between ' |
| 8974 | 'elements is the\n' |
| 8975 | ' string providing this method.\n' |
| 8976 | '\n' |
| 8977 | 'str.ljust(width[, fillchar])\n' |
| 8978 | '\n' |
| 8979 | ' Return the string left justified in a string of ' |
| 8980 | 'length *width*.\n' |
| 8981 | ' Padding is done using the specified *fillchar* ' |
| 8982 | '(default is a\n' |
| 8983 | ' space). The original string is returned if *width* ' |
| 8984 | 'is less than or\n' |
| 8985 | ' equal to "len(s)".\n' |
| 8986 | '\n' |
| 8987 | ' Changed in version 2.4: Support for the *fillchar* ' |
| 8988 | 'argument.\n' |
| 8989 | '\n' |
| 8990 | 'str.lower()\n' |
| 8991 | '\n' |
| 8992 | ' Return a copy of the string with all the cased ' |
| 8993 | 'characters [4]\n' |
| 8994 | ' converted to lowercase.\n' |
| 8995 | '\n' |
| 8996 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 8997 | '\n' |
| 8998 | 'str.lstrip([chars])\n' |
| 8999 | '\n' |
| 9000 | ' Return a copy of the string with leading characters ' |
| 9001 | 'removed. The\n' |
| 9002 | ' *chars* argument is a string specifying the set of ' |
| 9003 | 'characters to be\n' |
| 9004 | ' removed. If omitted or "None", the *chars* argument ' |
| 9005 | 'defaults to\n' |
| 9006 | ' removing whitespace. The *chars* argument is not a ' |
| 9007 | 'prefix; rather,\n' |
| 9008 | ' all combinations of its values are stripped:\n' |
| 9009 | '\n' |
| 9010 | " >>> ' spacious '.lstrip()\n" |
| 9011 | " 'spacious '\n" |
| 9012 | " >>> 'www.example.com'.lstrip('cmowz.')\n" |
| 9013 | " 'example.com'\n" |
| 9014 | '\n' |
| 9015 | ' Changed in version 2.2.2: Support for the *chars* ' |
| 9016 | 'argument.\n' |
| 9017 | '\n' |
| 9018 | 'str.partition(sep)\n' |
| 9019 | '\n' |
| 9020 | ' Split the string at the first occurrence of *sep*, ' |
| 9021 | 'and return a\n' |
| 9022 | ' 3-tuple containing the part before the separator, the ' |
| 9023 | 'separator\n' |
| 9024 | ' itself, and the part after the separator. If the ' |
| 9025 | 'separator is not\n' |
| 9026 | ' found, return a 3-tuple containing the string itself, ' |
| 9027 | 'followed by\n' |
| 9028 | ' two empty strings.\n' |
| 9029 | '\n' |
| 9030 | ' New in version 2.5.\n' |
| 9031 | '\n' |
| 9032 | 'str.replace(old, new[, count])\n' |
| 9033 | '\n' |
| 9034 | ' Return a copy of the string with all occurrences of ' |
| 9035 | 'substring *old*\n' |
| 9036 | ' replaced by *new*. If the optional argument *count* ' |
| 9037 | 'is given, only\n' |
| 9038 | ' the first *count* occurrences are replaced.\n' |
| 9039 | '\n' |
| 9040 | 'str.rfind(sub[, start[, end]])\n' |
| 9041 | '\n' |
| 9042 | ' Return the highest index in the string where ' |
| 9043 | 'substring *sub* is\n' |
| 9044 | ' found, such that *sub* is contained within ' |
| 9045 | '"s[start:end]".\n' |
| 9046 | ' Optional arguments *start* and *end* are interpreted ' |
| 9047 | 'as in slice\n' |
| 9048 | ' notation. Return "-1" on failure.\n' |
| 9049 | '\n' |
| 9050 | 'str.rindex(sub[, start[, end]])\n' |
| 9051 | '\n' |
| 9052 | ' Like "rfind()" but raises "ValueError" when the ' |
| 9053 | 'substring *sub* is\n' |
| 9054 | ' not found.\n' |
| 9055 | '\n' |
| 9056 | 'str.rjust(width[, fillchar])\n' |
| 9057 | '\n' |
| 9058 | ' Return the string right justified in a string of ' |
| 9059 | 'length *width*.\n' |
| 9060 | ' Padding is done using the specified *fillchar* ' |
| 9061 | '(default is a\n' |
| 9062 | ' space). The original string is returned if *width* is ' |
| 9063 | 'less than or\n' |
| 9064 | ' equal to "len(s)".\n' |
| 9065 | '\n' |
| 9066 | ' Changed in version 2.4: Support for the *fillchar* ' |
| 9067 | 'argument.\n' |
| 9068 | '\n' |
| 9069 | 'str.rpartition(sep)\n' |
| 9070 | '\n' |
| 9071 | ' Split the string at the last occurrence of *sep*, and ' |
| 9072 | 'return a\n' |
| 9073 | ' 3-tuple containing the part before the separator, the ' |
| 9074 | 'separator\n' |
| 9075 | ' itself, and the part after the separator. If the ' |
| 9076 | 'separator is not\n' |
| 9077 | ' found, return a 3-tuple containing two empty strings, ' |
| 9078 | 'followed by\n' |
| 9079 | ' the string itself.\n' |
| 9080 | '\n' |
| 9081 | ' New in version 2.5.\n' |
| 9082 | '\n' |
| 9083 | 'str.rsplit([sep[, maxsplit]])\n' |
| 9084 | '\n' |
| 9085 | ' Return a list of the words in the string, using *sep* ' |
| 9086 | 'as the\n' |
| 9087 | ' delimiter string. If *maxsplit* is given, at most ' |
| 9088 | '*maxsplit* splits\n' |
| 9089 | ' are done, the *rightmost* ones. If *sep* is not ' |
| 9090 | 'specified or\n' |
| 9091 | ' "None", any whitespace string is a separator. Except ' |
| 9092 | 'for splitting\n' |
| 9093 | ' from the right, "rsplit()" behaves like "split()" ' |
| 9094 | 'which is\n' |
| 9095 | ' described in detail below.\n' |
| 9096 | '\n' |
| 9097 | ' New in version 2.4.\n' |
| 9098 | '\n' |
| 9099 | 'str.rstrip([chars])\n' |
| 9100 | '\n' |
| 9101 | ' Return a copy of the string with trailing characters ' |
| 9102 | 'removed. The\n' |
| 9103 | ' *chars* argument is a string specifying the set of ' |
| 9104 | 'characters to be\n' |
| 9105 | ' removed. If omitted or "None", the *chars* argument ' |
| 9106 | 'defaults to\n' |
| 9107 | ' removing whitespace. The *chars* argument is not a ' |
| 9108 | 'suffix; rather,\n' |
| 9109 | ' all combinations of its values are stripped:\n' |
| 9110 | '\n' |
| 9111 | " >>> ' spacious '.rstrip()\n" |
| 9112 | " ' spacious'\n" |
| 9113 | " >>> 'mississippi'.rstrip('ipz')\n" |
| 9114 | " 'mississ'\n" |
| 9115 | '\n' |
| 9116 | ' Changed in version 2.2.2: Support for the *chars* ' |
| 9117 | 'argument.\n' |
| 9118 | '\n' |
| 9119 | 'str.split([sep[, maxsplit]])\n' |
| 9120 | '\n' |
| 9121 | ' Return a list of the words in the string, using *sep* ' |
| 9122 | 'as the\n' |
| 9123 | ' delimiter string. If *maxsplit* is given, at most ' |
| 9124 | '*maxsplit*\n' |
| 9125 | ' splits are done (thus, the list will have at most ' |
| 9126 | '"maxsplit+1"\n' |
| 9127 | ' elements). If *maxsplit* is not specified or "-1", ' |
| 9128 | 'then there is\n' |
| 9129 | ' no limit on the number of splits (all possible splits ' |
| 9130 | 'are made).\n' |
| 9131 | '\n' |
| 9132 | ' If *sep* is given, consecutive delimiters are not ' |
| 9133 | 'grouped together\n' |
| 9134 | ' and are deemed to delimit empty strings (for ' |
| 9135 | 'example,\n' |
| 9136 | ' "\'1,,2\'.split(\',\')" returns "[\'1\', \'\', ' |
| 9137 | '\'2\']"). The *sep* argument\n' |
| 9138 | ' may consist of multiple characters (for example,\n' |
| 9139 | ' "\'1<>2<>3\'.split(\'<>\')" returns "[\'1\', \'2\', ' |
| 9140 | '\'3\']"). Splitting an\n' |
| 9141 | ' empty string with a specified separator returns ' |
| 9142 | '"[\'\']".\n' |
| 9143 | '\n' |
| 9144 | ' If *sep* is not specified or is "None", a different ' |
| 9145 | 'splitting\n' |
| 9146 | ' algorithm is applied: runs of consecutive whitespace ' |
| 9147 | 'are regarded\n' |
| 9148 | ' as a single separator, and the result will contain no ' |
| 9149 | 'empty strings\n' |
| 9150 | ' at the start or end if the string has leading or ' |
| 9151 | 'trailing\n' |
| 9152 | ' whitespace. Consequently, splitting an empty string ' |
| 9153 | 'or a string\n' |
| 9154 | ' consisting of just whitespace with a "None" separator ' |
| 9155 | 'returns "[]".\n' |
| 9156 | '\n' |
| 9157 | ' For example, "\' 1 2 3 \'.split()" returns ' |
| 9158 | '"[\'1\', \'2\', \'3\']", and\n' |
| 9159 | ' "\' 1 2 3 \'.split(None, 1)" returns "[\'1\', ' |
| 9160 | '\'2 3 \']".\n' |
| 9161 | '\n' |
| 9162 | 'str.splitlines([keepends])\n' |
| 9163 | '\n' |
| 9164 | ' Return a list of the lines in the string, breaking at ' |
| 9165 | 'line\n' |
| 9166 | ' boundaries. This method uses the *universal newlines* ' |
| 9167 | 'approach to\n' |
| 9168 | ' splitting lines. Line breaks are not included in the ' |
| 9169 | 'resulting list\n' |
| 9170 | ' unless *keepends* is given and true.\n' |
| 9171 | '\n' |
| 9172 | ' For example, "\'ab c\\n\\nde ' |
| 9173 | 'fg\\rkl\\r\\n\'.splitlines()" returns "[\'ab\n' |
| 9174 | ' c\', \'\', \'de fg\', \'kl\']", while the same call ' |
| 9175 | 'with\n' |
| 9176 | ' "splitlines(True)" returns "[\'ab c\\n\', \'\\n\', ' |
| 9177 | '\'de fg\\r\', \'kl\\r\\n\']".\n' |
| 9178 | '\n' |
| 9179 | ' Unlike "split()" when a delimiter string *sep* is ' |
| 9180 | 'given, this\n' |
| 9181 | ' method returns an empty list for the empty string, ' |
| 9182 | 'and a terminal\n' |
| 9183 | ' line break does not result in an extra line.\n' |
| 9184 | '\n' |
| 9185 | 'str.startswith(prefix[, start[, end]])\n' |
| 9186 | '\n' |
| 9187 | ' Return "True" if string starts with the *prefix*, ' |
| 9188 | 'otherwise return\n' |
| 9189 | ' "False". *prefix* can also be a tuple of prefixes to ' |
| 9190 | 'look for.\n' |
| 9191 | ' With optional *start*, test string beginning at that ' |
| 9192 | 'position.\n' |
| 9193 | ' With optional *end*, stop comparing string at that ' |
| 9194 | 'position.\n' |
| 9195 | '\n' |
| 9196 | ' Changed in version 2.5: Accept tuples as *prefix*.\n' |
| 9197 | '\n' |
| 9198 | 'str.strip([chars])\n' |
| 9199 | '\n' |
| 9200 | ' Return a copy of the string with the leading and ' |
| 9201 | 'trailing\n' |
| 9202 | ' characters removed. The *chars* argument is a string ' |
| 9203 | 'specifying the\n' |
| 9204 | ' set of characters to be removed. If omitted or ' |
| 9205 | '"None", the *chars*\n' |
| 9206 | ' argument defaults to removing whitespace. The *chars* ' |
| 9207 | 'argument is\n' |
| 9208 | ' not a prefix or suffix; rather, all combinations of ' |
| 9209 | 'its values are\n' |
| 9210 | ' stripped:\n' |
| 9211 | '\n' |
| 9212 | " >>> ' spacious '.strip()\n" |
| 9213 | " 'spacious'\n" |
| 9214 | " >>> 'www.example.com'.strip('cmowz.')\n" |
| 9215 | " 'example'\n" |
| 9216 | '\n' |
| 9217 | ' Changed in version 2.2.2: Support for the *chars* ' |
| 9218 | 'argument.\n' |
| 9219 | '\n' |
| 9220 | 'str.swapcase()\n' |
| 9221 | '\n' |
| 9222 | ' Return a copy of the string with uppercase characters ' |
| 9223 | 'converted to\n' |
| 9224 | ' lowercase and vice versa.\n' |
| 9225 | '\n' |
| 9226 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9227 | '\n' |
| 9228 | 'str.title()\n' |
| 9229 | '\n' |
| 9230 | ' Return a titlecased version of the string where words ' |
| 9231 | 'start with an\n' |
| 9232 | ' uppercase character and the remaining characters are ' |
| 9233 | 'lowercase.\n' |
| 9234 | '\n' |
| 9235 | ' The algorithm uses a simple language-independent ' |
| 9236 | 'definition of a\n' |
| 9237 | ' word as groups of consecutive letters. The ' |
| 9238 | 'definition works in\n' |
| 9239 | ' many contexts but it means that apostrophes in ' |
| 9240 | 'contractions and\n' |
| 9241 | ' possessives form word boundaries, which may not be ' |
| 9242 | 'the desired\n' |
| 9243 | ' result:\n' |
| 9244 | '\n' |
| 9245 | ' >>> "they\'re bill\'s friends from the ' |
| 9246 | 'UK".title()\n' |
| 9247 | ' "They\'Re Bill\'S Friends From The Uk"\n' |
| 9248 | '\n' |
| 9249 | ' A workaround for apostrophes can be constructed using ' |
| 9250 | 'regular\n' |
| 9251 | ' expressions:\n' |
| 9252 | '\n' |
| 9253 | ' >>> import re\n' |
| 9254 | ' >>> def titlecase(s):\n' |
| 9255 | ' ... return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n' |
| 9256 | ' ... lambda mo: ' |
| 9257 | 'mo.group(0)[0].upper() +\n' |
| 9258 | ' ... ' |
| 9259 | 'mo.group(0)[1:].lower(),\n' |
| 9260 | ' ... s)\n' |
| 9261 | ' ...\n' |
| 9262 | ' >>> titlecase("they\'re bill\'s friends.")\n' |
| 9263 | ' "They\'re Bill\'s Friends."\n' |
| 9264 | '\n' |
| 9265 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9266 | '\n' |
| 9267 | 'str.translate(table[, deletechars])\n' |
| 9268 | '\n' |
| 9269 | ' Return a copy of the string where all characters ' |
| 9270 | 'occurring in the\n' |
| 9271 | ' optional argument *deletechars* are removed, and the ' |
| 9272 | 'remaining\n' |
| 9273 | ' characters have been mapped through the given ' |
| 9274 | 'translation table,\n' |
| 9275 | ' which must be a string of length 256.\n' |
| 9276 | '\n' |
| 9277 | ' You can use the "maketrans()" helper function in the ' |
| 9278 | '"string"\n' |
| 9279 | ' module to create a translation table. For string ' |
| 9280 | 'objects, set the\n' |
| 9281 | ' *table* argument to "None" for translations that only ' |
| 9282 | 'delete\n' |
| 9283 | ' characters:\n' |
| 9284 | '\n' |
| 9285 | " >>> 'read this short text'.translate(None, 'aeiou')\n" |
| 9286 | " 'rd ths shrt txt'\n" |
| 9287 | '\n' |
| 9288 | ' New in version 2.6: Support for a "None" *table* ' |
| 9289 | 'argument.\n' |
| 9290 | '\n' |
| 9291 | ' For Unicode objects, the "translate()" method does ' |
| 9292 | 'not accept the\n' |
| 9293 | ' optional *deletechars* argument. Instead, it returns ' |
| 9294 | 'a copy of the\n' |
| 9295 | ' *s* where all characters have been mapped through the ' |
| 9296 | 'given\n' |
| 9297 | ' translation table which must be a mapping of Unicode ' |
| 9298 | 'ordinals to\n' |
| 9299 | ' Unicode ordinals, Unicode strings or "None". Unmapped ' |
| 9300 | 'characters\n' |
| 9301 | ' are left untouched. Characters mapped to "None" are ' |
| 9302 | 'deleted. Note,\n' |
| 9303 | ' a more flexible approach is to create a custom ' |
| 9304 | 'character mapping\n' |
| 9305 | ' codec using the "codecs" module (see ' |
| 9306 | '"encodings.cp1251" for an\n' |
| 9307 | ' example).\n' |
| 9308 | '\n' |
| 9309 | 'str.upper()\n' |
| 9310 | '\n' |
| 9311 | ' Return a copy of the string with all the cased ' |
| 9312 | 'characters [4]\n' |
| 9313 | ' converted to uppercase. Note that ' |
| 9314 | '"str.upper().isupper()" might be\n' |
| 9315 | ' "False" if "s" contains uncased characters or if the ' |
| 9316 | 'Unicode\n' |
| 9317 | ' category of the resulting character(s) is not "Lu" ' |
| 9318 | '(Letter,\n' |
| 9319 | ' uppercase), but e.g. "Lt" (Letter, titlecase).\n' |
| 9320 | '\n' |
| 9321 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9322 | '\n' |
| 9323 | 'str.zfill(width)\n' |
| 9324 | '\n' |
| 9325 | ' Return the numeric string left filled with zeros in a ' |
| 9326 | 'string of\n' |
| 9327 | ' length *width*. A sign prefix is handled correctly. ' |
| 9328 | 'The original\n' |
| 9329 | ' string is returned if *width* is less than or equal ' |
| 9330 | 'to "len(s)".\n' |
| 9331 | '\n' |
| 9332 | ' New in version 2.2.2.\n' |
| 9333 | '\n' |
| 9334 | 'The following methods are present only on unicode ' |
| 9335 | 'objects:\n' |
| 9336 | '\n' |
| 9337 | 'unicode.isnumeric()\n' |
| 9338 | '\n' |
| 9339 | ' Return "True" if there are only numeric characters in ' |
| 9340 | 'S, "False"\n' |
| 9341 | ' otherwise. Numeric characters include digit ' |
| 9342 | 'characters, and all\n' |
| 9343 | ' characters that have the Unicode numeric value ' |
| 9344 | 'property, e.g.\n' |
| 9345 | ' U+2155, VULGAR FRACTION ONE FIFTH.\n' |
| 9346 | '\n' |
| 9347 | 'unicode.isdecimal()\n' |
| 9348 | '\n' |
| 9349 | ' Return "True" if there are only decimal characters in ' |
| 9350 | 'S, "False"\n' |
| 9351 | ' otherwise. Decimal characters include digit ' |
| 9352 | 'characters, and all\n' |
| 9353 | ' characters that can be used to form decimal-radix ' |
| 9354 | 'numbers, e.g.\n' |
| 9355 | ' U+0660, ARABIC-INDIC DIGIT ZERO.\n', |
| 9356 | 'strings': '\n' |
| 9357 | 'String literals\n' |
| 9358 | '***************\n' |
| 9359 | '\n' |
| 9360 | 'String literals are described by the following lexical ' |
| 9361 | 'definitions:\n' |
| 9362 | '\n' |
| 9363 | ' stringliteral ::= [stringprefix](shortstring | ' |
| 9364 | 'longstring)\n' |
| 9365 | ' stringprefix ::= "r" | "u" | "ur" | "R" | "U" | "UR" | ' |
| 9366 | '"Ur" | "uR"\n' |
| 9367 | ' | "b" | "B" | "br" | "Br" | "bR" | "BR"\n' |
| 9368 | ' shortstring ::= "\'" shortstringitem* "\'" | \'"\' ' |
| 9369 | 'shortstringitem* \'"\'\n' |
| 9370 | ' longstring ::= "\'\'\'" longstringitem* "\'\'\'"\n' |
| 9371 | ' | \'"""\' longstringitem* \'"""\'\n' |
| 9372 | ' shortstringitem ::= shortstringchar | escapeseq\n' |
| 9373 | ' longstringitem ::= longstringchar | escapeseq\n' |
| 9374 | ' shortstringchar ::= <any source character except "\\" or ' |
| 9375 | 'newline or the quote>\n' |
| 9376 | ' longstringchar ::= <any source character except "\\">\n' |
| 9377 | ' escapeseq ::= "\\" <any ASCII character>\n' |
| 9378 | '\n' |
| 9379 | 'One syntactic restriction not indicated by these productions is ' |
| 9380 | 'that\n' |
| 9381 | 'whitespace is not allowed between the "stringprefix" and the ' |
| 9382 | 'rest of\n' |
| 9383 | 'the string literal. The source character set is defined by the\n' |
| 9384 | 'encoding declaration; it is ASCII if no encoding declaration is ' |
| 9385 | 'given\n' |
| 9386 | 'in the source file; see section Encoding declarations.\n' |
| 9387 | '\n' |
| 9388 | 'In plain English: String literals can be enclosed in matching ' |
| 9389 | 'single\n' |
| 9390 | 'quotes ("\'") or double quotes ("""). They can also be ' |
| 9391 | 'enclosed in\n' |
| 9392 | 'matching groups of three single or double quotes (these are ' |
| 9393 | 'generally\n' |
| 9394 | 'referred to as *triple-quoted strings*). The backslash ("\\")\n' |
| 9395 | 'character is used to escape characters that otherwise have a ' |
| 9396 | 'special\n' |
| 9397 | 'meaning, such as newline, backslash itself, or the quote ' |
| 9398 | 'character.\n' |
| 9399 | 'String literals may optionally be prefixed with a letter ' |
| 9400 | '"\'r\'" or\n' |
| 9401 | '"\'R\'"; such strings are called *raw strings* and use ' |
| 9402 | 'different rules\n' |
| 9403 | 'for interpreting backslash escape sequences. A prefix of ' |
| 9404 | '"\'u\'" or\n' |
| 9405 | '"\'U\'" makes the string a Unicode string. Unicode strings use ' |
| 9406 | 'the\n' |
| 9407 | 'Unicode character set as defined by the Unicode Consortium and ' |
| 9408 | 'ISO\n' |
| 9409 | '10646. Some additional escape sequences, described below, are\n' |
| 9410 | 'available in Unicode strings. A prefix of "\'b\'" or "\'B\'" is ' |
| 9411 | 'ignored in\n' |
| 9412 | 'Python 2; it indicates that the literal should become a bytes ' |
| 9413 | 'literal\n' |
| 9414 | 'in Python 3 (e.g. when code is automatically converted with ' |
| 9415 | '2to3). A\n' |
| 9416 | '"\'u\'" or "\'b\'" prefix may be followed by an "\'r\'" ' |
| 9417 | 'prefix.\n' |
| 9418 | '\n' |
| 9419 | 'In triple-quoted strings, unescaped newlines and quotes are ' |
| 9420 | 'allowed\n' |
| 9421 | '(and are retained), except that three unescaped quotes in a ' |
| 9422 | 'row\n' |
| 9423 | 'terminate the string. (A "quote" is the character used to open ' |
| 9424 | 'the\n' |
| 9425 | 'string, i.e. either "\'" or """.)\n' |
| 9426 | '\n' |
| 9427 | 'Unless an "\'r\'" or "\'R\'" prefix is present, escape ' |
| 9428 | 'sequences in\n' |
| 9429 | 'strings are interpreted according to rules similar to those ' |
| 9430 | 'used by\n' |
| 9431 | 'Standard C. The recognized escape sequences are:\n' |
| 9432 | '\n' |
| 9433 | '+-------------------+-----------------------------------+---------+\n' |
| 9434 | '| Escape Sequence | Meaning | ' |
| 9435 | 'Notes |\n' |
| 9436 | '+===================+===================================+=========+\n' |
| 9437 | '| "\\newline" | Ignored ' |
| 9438 | '| |\n' |
| 9439 | '+-------------------+-----------------------------------+---------+\n' |
| 9440 | '| "\\\\" | Backslash ("\\") ' |
| 9441 | '| |\n' |
| 9442 | '+-------------------+-----------------------------------+---------+\n' |
| 9443 | '| "\\\'" | Single quote ("\'") ' |
| 9444 | '| |\n' |
| 9445 | '+-------------------+-----------------------------------+---------+\n' |
| 9446 | '| "\\"" | Double quote (""") ' |
| 9447 | '| |\n' |
| 9448 | '+-------------------+-----------------------------------+---------+\n' |
| 9449 | '| "\\a" | ASCII Bell (BEL) ' |
| 9450 | '| |\n' |
| 9451 | '+-------------------+-----------------------------------+---------+\n' |
| 9452 | '| "\\b" | ASCII Backspace (BS) ' |
| 9453 | '| |\n' |
| 9454 | '+-------------------+-----------------------------------+---------+\n' |
| 9455 | '| "\\f" | ASCII Formfeed (FF) ' |
| 9456 | '| |\n' |
| 9457 | '+-------------------+-----------------------------------+---------+\n' |
| 9458 | '| "\\n" | ASCII Linefeed (LF) ' |
| 9459 | '| |\n' |
| 9460 | '+-------------------+-----------------------------------+---------+\n' |
| 9461 | '| "\\N{name}" | Character named *name* in the ' |
| 9462 | '| |\n' |
| 9463 | '| | Unicode database (Unicode only) ' |
| 9464 | '| |\n' |
| 9465 | '+-------------------+-----------------------------------+---------+\n' |
| 9466 | '| "\\r" | ASCII Carriage Return (CR) ' |
| 9467 | '| |\n' |
| 9468 | '+-------------------+-----------------------------------+---------+\n' |
| 9469 | '| "\\t" | ASCII Horizontal Tab (TAB) ' |
| 9470 | '| |\n' |
| 9471 | '+-------------------+-----------------------------------+---------+\n' |
| 9472 | '| "\\uxxxx" | Character with 16-bit hex value | ' |
| 9473 | '(1) |\n' |
| 9474 | '| | *xxxx* (Unicode only) ' |
| 9475 | '| |\n' |
| 9476 | '+-------------------+-----------------------------------+---------+\n' |
| 9477 | '| "\\Uxxxxxxxx" | Character with 32-bit hex value | ' |
| 9478 | '(2) |\n' |
| 9479 | '| | *xxxxxxxx* (Unicode only) ' |
| 9480 | '| |\n' |
| 9481 | '+-------------------+-----------------------------------+---------+\n' |
| 9482 | '| "\\v" | ASCII Vertical Tab (VT) ' |
| 9483 | '| |\n' |
| 9484 | '+-------------------+-----------------------------------+---------+\n' |
| 9485 | '| "\\ooo" | Character with octal value *ooo* | ' |
| 9486 | '(3,5) |\n' |
| 9487 | '+-------------------+-----------------------------------+---------+\n' |
| 9488 | '| "\\xhh" | Character with hex value *hh* | ' |
| 9489 | '(4,5) |\n' |
| 9490 | '+-------------------+-----------------------------------+---------+\n' |
| 9491 | '\n' |
| 9492 | 'Notes:\n' |
| 9493 | '\n' |
| 9494 | '1. Individual code units which form parts of a surrogate pair ' |
| 9495 | 'can\n' |
| 9496 | ' be encoded using this escape sequence.\n' |
| 9497 | '\n' |
| 9498 | '2. Any Unicode character can be encoded this way, but ' |
| 9499 | 'characters\n' |
| 9500 | ' outside the Basic Multilingual Plane (BMP) will be encoded ' |
| 9501 | 'using a\n' |
| 9502 | ' surrogate pair if Python is compiled to use 16-bit code ' |
| 9503 | 'units (the\n' |
| 9504 | ' default).\n' |
| 9505 | '\n' |
| 9506 | '3. As in Standard C, up to three octal digits are accepted.\n' |
| 9507 | '\n' |
| 9508 | '4. Unlike in Standard C, exactly two hex digits are required.\n' |
| 9509 | '\n' |
| 9510 | '5. In a string literal, hexadecimal and octal escapes denote ' |
| 9511 | 'the\n' |
| 9512 | ' byte with the given value; it is not necessary that the ' |
| 9513 | 'byte\n' |
| 9514 | ' encodes a character in the source character set. In a ' |
| 9515 | 'Unicode\n' |
| 9516 | ' literal, these escapes denote a Unicode character with the ' |
| 9517 | 'given\n' |
| 9518 | ' value.\n' |
| 9519 | '\n' |
| 9520 | 'Unlike Standard C, all unrecognized escape sequences are left ' |
| 9521 | 'in the\n' |
| 9522 | 'string unchanged, i.e., *the backslash is left in the string*. ' |
| 9523 | '(This\n' |
| 9524 | 'behavior is useful when debugging: if an escape sequence is ' |
| 9525 | 'mistyped,\n' |
| 9526 | 'the resulting output is more easily recognized as broken.) It ' |
| 9527 | 'is also\n' |
| 9528 | 'important to note that the escape sequences marked as "(Unicode ' |
| 9529 | 'only)"\n' |
| 9530 | 'in the table above fall into the category of unrecognized ' |
| 9531 | 'escapes for\n' |
| 9532 | 'non-Unicode string literals.\n' |
| 9533 | '\n' |
| 9534 | 'When an "\'r\'" or "\'R\'" prefix is present, a character ' |
| 9535 | 'following a\n' |
| 9536 | 'backslash is included in the string without change, and *all\n' |
| 9537 | 'backslashes are left in the string*. For example, the string ' |
| 9538 | 'literal\n' |
| 9539 | '"r"\\n"" consists of two characters: a backslash and a ' |
| 9540 | 'lowercase "\'n\'".\n' |
| 9541 | 'String quotes can be escaped with a backslash, but the ' |
| 9542 | 'backslash\n' |
| 9543 | 'remains in the string; for example, "r"\\""" is a valid string ' |
| 9544 | 'literal\n' |
| 9545 | 'consisting of two characters: a backslash and a double quote; ' |
| 9546 | '"r"\\""\n' |
| 9547 | 'is not a valid string literal (even a raw string cannot end in ' |
| 9548 | 'an odd\n' |
| 9549 | 'number of backslashes). Specifically, *a raw string cannot end ' |
| 9550 | 'in a\n' |
| 9551 | 'single backslash* (since the backslash would escape the ' |
| 9552 | 'following\n' |
| 9553 | 'quote character). Note also that a single backslash followed ' |
| 9554 | 'by a\n' |
| 9555 | 'newline is interpreted as those two characters as part of the ' |
| 9556 | 'string,\n' |
| 9557 | '*not* as a line continuation.\n' |
| 9558 | '\n' |
| 9559 | 'When an "\'r\'" or "\'R\'" prefix is used in conjunction with a ' |
| 9560 | '"\'u\'" or\n' |
| 9561 | '"\'U\'" prefix, then the "\\uXXXX" and "\\UXXXXXXXX" escape ' |
| 9562 | 'sequences are\n' |
| 9563 | 'processed while *all other backslashes are left in the ' |
| 9564 | 'string*. For\n' |
| 9565 | 'example, the string literal "ur"\\u0062\\n"" consists of three ' |
| 9566 | 'Unicode\n' |
| 9567 | "characters: 'LATIN SMALL LETTER B', 'REVERSE SOLIDUS', and " |
| 9568 | "'LATIN\n" |
| 9569 | "SMALL LETTER N'. Backslashes can be escaped with a preceding\n" |
| 9570 | 'backslash; however, both remain in the string. As a result, ' |
| 9571 | '"\\uXXXX"\n' |
| 9572 | 'escape sequences are only recognized when there are an odd ' |
| 9573 | 'number of\n' |
| 9574 | 'backslashes.\n', |
| 9575 | 'subscriptions': '\n' |
| 9576 | 'Subscriptions\n' |
| 9577 | '*************\n' |
| 9578 | '\n' |
| 9579 | 'A subscription selects an item of a sequence (string, ' |
| 9580 | 'tuple or list)\n' |
| 9581 | 'or mapping (dictionary) object:\n' |
| 9582 | '\n' |
| 9583 | ' subscription ::= primary "[" expression_list "]"\n' |
| 9584 | '\n' |
| 9585 | 'The primary must evaluate to an object of a sequence or ' |
| 9586 | 'mapping type.\n' |
| 9587 | '\n' |
| 9588 | 'If the primary is a mapping, the expression list must ' |
| 9589 | 'evaluate to an\n' |
| 9590 | 'object whose value is one of the keys of the mapping, and ' |
| 9591 | 'the\n' |
| 9592 | 'subscription selects the value in the mapping that ' |
| 9593 | 'corresponds to that\n' |
| 9594 | 'key. (The expression list is a tuple except if it has ' |
| 9595 | 'exactly one\n' |
| 9596 | 'item.)\n' |
| 9597 | '\n' |
| 9598 | 'If the primary is a sequence, the expression (list) must ' |
| 9599 | 'evaluate to a\n' |
| 9600 | 'plain integer. If this value is negative, the length of ' |
| 9601 | 'the sequence\n' |
| 9602 | 'is added to it (so that, e.g., "x[-1]" selects the last ' |
| 9603 | 'item of "x".)\n' |
| 9604 | 'The resulting value must be a nonnegative integer less ' |
| 9605 | 'than the number\n' |
| 9606 | 'of items in the sequence, and the subscription selects ' |
| 9607 | 'the item whose\n' |
| 9608 | 'index is that value (counting from zero).\n' |
| 9609 | '\n' |
| 9610 | "A string's items are characters. A character is not a " |
| 9611 | 'separate data\n' |
| 9612 | 'type but a string of exactly one character.\n', |
| 9613 | 'truth': '\n' |
| 9614 | 'Truth Value Testing\n' |
| 9615 | '*******************\n' |
| 9616 | '\n' |
| 9617 | 'Any object can be tested for truth value, for use in an "if" or\n' |
| 9618 | '"while" condition or as operand of the Boolean operations below. ' |
| 9619 | 'The\n' |
| 9620 | 'following values are considered false:\n' |
| 9621 | '\n' |
| 9622 | '* "None"\n' |
| 9623 | '\n' |
| 9624 | '* "False"\n' |
| 9625 | '\n' |
| 9626 | '* zero of any numeric type, for example, "0", "0L", "0.0", "0j".\n' |
| 9627 | '\n' |
| 9628 | '* any empty sequence, for example, "\'\'", "()", "[]".\n' |
| 9629 | '\n' |
| 9630 | '* any empty mapping, for example, "{}".\n' |
| 9631 | '\n' |
| 9632 | '* instances of user-defined classes, if the class defines a\n' |
| 9633 | ' "__nonzero__()" or "__len__()" method, when that method returns ' |
| 9634 | 'the\n' |
| 9635 | ' integer zero or "bool" value "False". [1]\n' |
| 9636 | '\n' |
| 9637 | 'All other values are considered true --- so objects of many types ' |
| 9638 | 'are\n' |
| 9639 | 'always true.\n' |
| 9640 | '\n' |
| 9641 | 'Operations and built-in functions that have a Boolean result ' |
| 9642 | 'always\n' |
| 9643 | 'return "0" or "False" for false and "1" or "True" for true, ' |
| 9644 | 'unless\n' |
| 9645 | 'otherwise stated. (Important exception: the Boolean operations ' |
| 9646 | '"or"\n' |
| 9647 | 'and "and" always return one of their operands.)\n', |
| 9648 | 'try': '\n' |
| 9649 | 'The "try" statement\n' |
| 9650 | '*******************\n' |
| 9651 | '\n' |
| 9652 | 'The "try" statement specifies exception handlers and/or cleanup ' |
| 9653 | 'code\n' |
| 9654 | 'for a group of statements:\n' |
| 9655 | '\n' |
| 9656 | ' try_stmt ::= try1_stmt | try2_stmt\n' |
| 9657 | ' try1_stmt ::= "try" ":" suite\n' |
| 9658 | ' ("except" [expression [("as" | ",") identifier]] ' |
| 9659 | '":" suite)+\n' |
| 9660 | ' ["else" ":" suite]\n' |
| 9661 | ' ["finally" ":" suite]\n' |
| 9662 | ' try2_stmt ::= "try" ":" suite\n' |
| 9663 | ' "finally" ":" suite\n' |
| 9664 | '\n' |
| 9665 | 'Changed in version 2.5: In previous versions of Python,\n' |
| 9666 | '"try"..."except"..."finally" did not work. "try"..."except" had to ' |
| 9667 | 'be\n' |
| 9668 | 'nested in "try"..."finally".\n' |
| 9669 | '\n' |
| 9670 | 'The "except" clause(s) specify one or more exception handlers. When ' |
| 9671 | 'no\n' |
| 9672 | 'exception occurs in the "try" clause, no exception handler is\n' |
| 9673 | 'executed. When an exception occurs in the "try" suite, a search for ' |
| 9674 | 'an\n' |
| 9675 | 'exception handler is started. This search inspects the except ' |
| 9676 | 'clauses\n' |
| 9677 | 'in turn until one is found that matches the exception. An ' |
| 9678 | 'expression-\n' |
| 9679 | 'less except clause, if present, must be last; it matches any\n' |
| 9680 | 'exception. For an except clause with an expression, that ' |
| 9681 | 'expression\n' |
| 9682 | 'is evaluated, and the clause matches the exception if the ' |
| 9683 | 'resulting\n' |
| 9684 | 'object is "compatible" with the exception. An object is ' |
| 9685 | 'compatible\n' |
| 9686 | 'with an exception if it is the class or a base class of the ' |
| 9687 | 'exception\n' |
| 9688 | 'object, or a tuple containing an item compatible with the ' |
| 9689 | 'exception.\n' |
| 9690 | '\n' |
| 9691 | 'If no except clause matches the exception, the search for an ' |
| 9692 | 'exception\n' |
| 9693 | 'handler continues in the surrounding code and on the invocation ' |
| 9694 | 'stack.\n' |
| 9695 | '[1]\n' |
| 9696 | '\n' |
| 9697 | 'If the evaluation of an expression in the header of an except ' |
| 9698 | 'clause\n' |
| 9699 | 'raises an exception, the original search for a handler is canceled ' |
| 9700 | 'and\n' |
| 9701 | 'a search starts for the new exception in the surrounding code and ' |
| 9702 | 'on\n' |
| 9703 | 'the call stack (it is treated as if the entire "try" statement ' |
| 9704 | 'raised\n' |
| 9705 | 'the exception).\n' |
| 9706 | '\n' |
| 9707 | 'When a matching except clause is found, the exception is assigned ' |
| 9708 | 'to\n' |
| 9709 | 'the target specified in that except clause, if present, and the ' |
| 9710 | 'except\n' |
| 9711 | "clause's suite is executed. All except clauses must have an\n" |
| 9712 | 'executable block. When the end of this block is reached, ' |
| 9713 | 'execution\n' |
| 9714 | 'continues normally after the entire try statement. (This means ' |
| 9715 | 'that\n' |
| 9716 | 'if two nested handlers exist for the same exception, and the ' |
| 9717 | 'exception\n' |
| 9718 | 'occurs in the try clause of the inner handler, the outer handler ' |
| 9719 | 'will\n' |
| 9720 | 'not handle the exception.)\n' |
| 9721 | '\n' |
| 9722 | "Before an except clause's suite is executed, details about the\n" |
| 9723 | 'exception are assigned to three variables in the "sys" module:\n' |
| 9724 | '"sys.exc_type" receives the object identifying the exception;\n' |
| 9725 | '"sys.exc_value" receives the exception\'s parameter;\n' |
| 9726 | '"sys.exc_traceback" receives a traceback object (see section The\n' |
| 9727 | 'standard type hierarchy) identifying the point in the program ' |
| 9728 | 'where\n' |
| 9729 | 'the exception occurred. These details are also available through ' |
| 9730 | 'the\n' |
| 9731 | '"sys.exc_info()" function, which returns a tuple "(exc_type,\n' |
| 9732 | 'exc_value, exc_traceback)". Use of the corresponding variables is\n' |
| 9733 | 'deprecated in favor of this function, since their use is unsafe in ' |
| 9734 | 'a\n' |
| 9735 | 'threaded program. As of Python 1.5, the variables are restored to\n' |
| 9736 | 'their previous values (before the call) when returning from a ' |
| 9737 | 'function\n' |
| 9738 | 'that handled an exception.\n' |
| 9739 | '\n' |
| 9740 | 'The optional "else" clause is executed if and when control flows ' |
| 9741 | 'off\n' |
| 9742 | 'the end of the "try" clause. [2] Exceptions in the "else" clause ' |
| 9743 | 'are\n' |
| 9744 | 'not handled by the preceding "except" clauses.\n' |
| 9745 | '\n' |
| 9746 | 'If "finally" is present, it specifies a \'cleanup\' handler. The ' |
| 9747 | '"try"\n' |
| 9748 | 'clause is executed, including any "except" and "else" clauses. If ' |
| 9749 | 'an\n' |
| 9750 | 'exception occurs in any of the clauses and is not handled, the\n' |
| 9751 | 'exception is temporarily saved. The "finally" clause is executed. ' |
| 9752 | 'If\n' |
| 9753 | 'there is a saved exception, it is re-raised at the end of the\n' |
| 9754 | '"finally" clause. If the "finally" clause raises another exception ' |
| 9755 | 'or\n' |
| 9756 | 'executes a "return" or "break" statement, the saved exception is\n' |
| 9757 | 'discarded:\n' |
| 9758 | '\n' |
| 9759 | ' >>> def f():\n' |
| 9760 | ' ... try:\n' |
| 9761 | ' ... 1/0\n' |
| 9762 | ' ... finally:\n' |
| 9763 | ' ... return 42\n' |
| 9764 | ' ...\n' |
| 9765 | ' >>> f()\n' |
| 9766 | ' 42\n' |
| 9767 | '\n' |
| 9768 | 'The exception information is not available to the program during\n' |
| 9769 | 'execution of the "finally" clause.\n' |
| 9770 | '\n' |
| 9771 | 'When a "return", "break" or "continue" statement is executed in ' |
| 9772 | 'the\n' |
| 9773 | '"try" suite of a "try"..."finally" statement, the "finally" clause ' |
| 9774 | 'is\n' |
| 9775 | 'also executed \'on the way out.\' A "continue" statement is illegal ' |
| 9776 | 'in\n' |
| 9777 | 'the "finally" clause. (The reason is a problem with the current\n' |
| 9778 | 'implementation --- this restriction may be lifted in the future).\n' |
| 9779 | '\n' |
| 9780 | 'The return value of a function is determined by the last "return"\n' |
| 9781 | 'statement executed. Since the "finally" clause always executes, a\n' |
| 9782 | '"return" statement executed in the "finally" clause will always be ' |
| 9783 | 'the\n' |
| 9784 | 'last one executed:\n' |
| 9785 | '\n' |
| 9786 | ' >>> def foo():\n' |
| 9787 | ' ... try:\n' |
| 9788 | " ... return 'try'\n" |
| 9789 | ' ... finally:\n' |
| 9790 | " ... return 'finally'\n" |
| 9791 | ' ...\n' |
| 9792 | ' >>> foo()\n' |
| 9793 | " 'finally'\n" |
| 9794 | '\n' |
| 9795 | 'Additional information on exceptions can be found in section\n' |
| 9796 | 'Exceptions, and information on using the "raise" statement to ' |
| 9797 | 'generate\n' |
| 9798 | 'exceptions may be found in section The raise statement.\n', |
| 9799 | 'types': '\n' |
| 9800 | 'The standard type hierarchy\n' |
| 9801 | '***************************\n' |
| 9802 | '\n' |
| 9803 | 'Below is a list of the types that are built into Python. ' |
| 9804 | 'Extension\n' |
| 9805 | 'modules (written in C, Java, or other languages, depending on ' |
| 9806 | 'the\n' |
| 9807 | 'implementation) can define additional types. Future versions of\n' |
| 9808 | 'Python may add types to the type hierarchy (e.g., rational ' |
| 9809 | 'numbers,\n' |
| 9810 | 'efficiently stored arrays of integers, etc.).\n' |
| 9811 | '\n' |
| 9812 | 'Some of the type descriptions below contain a paragraph listing\n' |
| 9813 | "'special attributes.' These are attributes that provide access " |
| 9814 | 'to the\n' |
| 9815 | 'implementation and are not intended for general use. Their ' |
| 9816 | 'definition\n' |
| 9817 | 'may change in the future.\n' |
| 9818 | '\n' |
| 9819 | 'None\n' |
| 9820 | ' This type has a single value. There is a single object with ' |
| 9821 | 'this\n' |
| 9822 | ' value. This object is accessed through the built-in name ' |
| 9823 | '"None". It\n' |
| 9824 | ' is used to signify the absence of a value in many situations, ' |
| 9825 | 'e.g.,\n' |
| 9826 | " it is returned from functions that don't explicitly return\n" |
| 9827 | ' anything. Its truth value is false.\n' |
| 9828 | '\n' |
| 9829 | 'NotImplemented\n' |
| 9830 | ' This type has a single value. There is a single object with ' |
| 9831 | 'this\n' |
| 9832 | ' value. This object is accessed through the built-in name\n' |
| 9833 | ' "NotImplemented". Numeric methods and rich comparison methods ' |
| 9834 | 'may\n' |
| 9835 | ' return this value if they do not implement the operation for ' |
| 9836 | 'the\n' |
| 9837 | ' operands provided. (The interpreter will then try the ' |
| 9838 | 'reflected\n' |
| 9839 | ' operation, or some other fallback, depending on the ' |
| 9840 | 'operator.) Its\n' |
| 9841 | ' truth value is true.\n' |
| 9842 | '\n' |
| 9843 | 'Ellipsis\n' |
| 9844 | ' This type has a single value. There is a single object with ' |
| 9845 | 'this\n' |
| 9846 | ' value. This object is accessed through the built-in name\n' |
| 9847 | ' "Ellipsis". It is used to indicate the presence of the "..." ' |
| 9848 | 'syntax\n' |
| 9849 | ' in a slice. Its truth value is true.\n' |
| 9850 | '\n' |
| 9851 | '"numbers.Number"\n' |
| 9852 | ' These are created by numeric literals and returned as results ' |
| 9853 | 'by\n' |
| 9854 | ' arithmetic operators and arithmetic built-in functions. ' |
| 9855 | 'Numeric\n' |
| 9856 | ' objects are immutable; once created their value never ' |
| 9857 | 'changes.\n' |
| 9858 | ' Python numbers are of course strongly related to mathematical\n' |
| 9859 | ' numbers, but subject to the limitations of numerical ' |
| 9860 | 'representation\n' |
| 9861 | ' in computers.\n' |
| 9862 | '\n' |
| 9863 | ' Python distinguishes between integers, floating point numbers, ' |
| 9864 | 'and\n' |
| 9865 | ' complex numbers:\n' |
| 9866 | '\n' |
| 9867 | ' "numbers.Integral"\n' |
| 9868 | ' These represent elements from the mathematical set of ' |
| 9869 | 'integers\n' |
| 9870 | ' (positive and negative).\n' |
| 9871 | '\n' |
| 9872 | ' There are three types of integers:\n' |
| 9873 | '\n' |
| 9874 | ' Plain integers\n' |
| 9875 | ' These represent numbers in the range -2147483648 ' |
| 9876 | 'through\n' |
| 9877 | ' 2147483647. (The range may be larger on machines with a\n' |
| 9878 | ' larger natural word size, but not smaller.) When the ' |
| 9879 | 'result\n' |
| 9880 | ' of an operation would fall outside this range, the ' |
| 9881 | 'result is\n' |
| 9882 | ' normally returned as a long integer (in some cases, the\n' |
| 9883 | ' exception "OverflowError" is raised instead). For the\n' |
| 9884 | ' purpose of shift and mask operations, integers are ' |
| 9885 | 'assumed to\n' |
| 9886 | " have a binary, 2's complement notation using 32 or more " |
| 9887 | 'bits,\n' |
| 9888 | ' and hiding no bits from the user (i.e., all 4294967296\n' |
| 9889 | ' different bit patterns correspond to different values).\n' |
| 9890 | '\n' |
| 9891 | ' Long integers\n' |
| 9892 | ' These represent numbers in an unlimited range, subject ' |
| 9893 | 'to\n' |
| 9894 | ' available (virtual) memory only. For the purpose of ' |
| 9895 | 'shift\n' |
| 9896 | ' and mask operations, a binary representation is assumed, ' |
| 9897 | 'and\n' |
| 9898 | " negative numbers are represented in a variant of 2's\n" |
| 9899 | ' complement which gives the illusion of an infinite ' |
| 9900 | 'string of\n' |
| 9901 | ' sign bits extending to the left.\n' |
| 9902 | '\n' |
| 9903 | ' Booleans\n' |
| 9904 | ' These represent the truth values False and True. The ' |
| 9905 | 'two\n' |
| 9906 | ' objects representing the values "False" and "True" are ' |
| 9907 | 'the\n' |
| 9908 | ' only Boolean objects. The Boolean type is a subtype of ' |
| 9909 | 'plain\n' |
| 9910 | ' integers, and Boolean values behave like the values 0 ' |
| 9911 | 'and 1,\n' |
| 9912 | ' respectively, in almost all contexts, the exception ' |
| 9913 | 'being\n' |
| 9914 | ' that when converted to a string, the strings ""False"" ' |
| 9915 | 'or\n' |
| 9916 | ' ""True"" are returned, respectively.\n' |
| 9917 | '\n' |
| 9918 | ' The rules for integer representation are intended to give ' |
| 9919 | 'the\n' |
| 9920 | ' most meaningful interpretation of shift and mask ' |
| 9921 | 'operations\n' |
| 9922 | ' involving negative integers and the least surprises when\n' |
| 9923 | ' switching between the plain and long integer domains. Any\n' |
| 9924 | ' operation, if it yields a result in the plain integer ' |
| 9925 | 'domain,\n' |
| 9926 | ' will yield the same result in the long integer domain or ' |
| 9927 | 'when\n' |
| 9928 | ' using mixed operands. The switch between domains is ' |
| 9929 | 'transparent\n' |
| 9930 | ' to the programmer.\n' |
| 9931 | '\n' |
| 9932 | ' "numbers.Real" ("float")\n' |
| 9933 | ' These represent machine-level double precision floating ' |
| 9934 | 'point\n' |
| 9935 | ' numbers. You are at the mercy of the underlying machine\n' |
| 9936 | ' architecture (and C or Java implementation) for the ' |
| 9937 | 'accepted\n' |
| 9938 | ' range and handling of overflow. Python does not support ' |
| 9939 | 'single-\n' |
| 9940 | ' precision floating point numbers; the savings in processor ' |
| 9941 | 'and\n' |
| 9942 | ' memory usage that are usually the reason for using these ' |
| 9943 | 'are\n' |
| 9944 | ' dwarfed by the overhead of using objects in Python, so ' |
| 9945 | 'there is\n' |
| 9946 | ' no reason to complicate the language with two kinds of ' |
| 9947 | 'floating\n' |
| 9948 | ' point numbers.\n' |
| 9949 | '\n' |
| 9950 | ' "numbers.Complex"\n' |
| 9951 | ' These represent complex numbers as a pair of machine-level\n' |
| 9952 | ' double precision floating point numbers. The same caveats ' |
| 9953 | 'apply\n' |
| 9954 | ' as for floating point numbers. The real and imaginary parts ' |
| 9955 | 'of a\n' |
| 9956 | ' complex number "z" can be retrieved through the read-only\n' |
| 9957 | ' attributes "z.real" and "z.imag".\n' |
| 9958 | '\n' |
| 9959 | 'Sequences\n' |
| 9960 | ' These represent finite ordered sets indexed by non-negative\n' |
| 9961 | ' numbers. The built-in function "len()" returns the number of ' |
| 9962 | 'items\n' |
| 9963 | ' of a sequence. When the length of a sequence is *n*, the index ' |
| 9964 | 'set\n' |
| 9965 | ' contains the numbers 0, 1, ..., *n*-1. Item *i* of sequence ' |
| 9966 | '*a* is\n' |
| 9967 | ' selected by "a[i]".\n' |
| 9968 | '\n' |
| 9969 | ' Sequences also support slicing: "a[i:j]" selects all items ' |
| 9970 | 'with\n' |
| 9971 | ' index *k* such that *i* "<=" *k* "<" *j*. When used as an\n' |
| 9972 | ' expression, a slice is a sequence of the same type. This ' |
| 9973 | 'implies\n' |
| 9974 | ' that the index set is renumbered so that it starts at 0.\n' |
| 9975 | '\n' |
| 9976 | ' Some sequences also support "extended slicing" with a third ' |
| 9977 | '"step"\n' |
| 9978 | ' parameter: "a[i:j:k]" selects all items of *a* with index *x* ' |
| 9979 | 'where\n' |
| 9980 | ' "x = i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*.\n' |
| 9981 | '\n' |
| 9982 | ' Sequences are distinguished according to their mutability:\n' |
| 9983 | '\n' |
| 9984 | ' Immutable sequences\n' |
| 9985 | ' An object of an immutable sequence type cannot change once ' |
| 9986 | 'it is\n' |
| 9987 | ' created. (If the object contains references to other ' |
| 9988 | 'objects,\n' |
| 9989 | ' these other objects may be mutable and may be changed; ' |
| 9990 | 'however,\n' |
| 9991 | ' the collection of objects directly referenced by an ' |
| 9992 | 'immutable\n' |
| 9993 | ' object cannot change.)\n' |
| 9994 | '\n' |
| 9995 | ' The following types are immutable sequences:\n' |
| 9996 | '\n' |
| 9997 | ' Strings\n' |
| 9998 | ' The items of a string are characters. There is no ' |
| 9999 | 'separate\n' |
| 10000 | ' character type; a character is represented by a string ' |
| 10001 | 'of one\n' |
| 10002 | ' item. Characters represent (at least) 8-bit bytes. The\n' |
| 10003 | ' built-in functions "chr()" and "ord()" convert between\n' |
| 10004 | ' characters and nonnegative integers representing the ' |
| 10005 | 'byte\n' |
| 10006 | ' values. Bytes with the values 0-127 usually represent ' |
| 10007 | 'the\n' |
| 10008 | ' corresponding ASCII values, but the interpretation of ' |
| 10009 | 'values\n' |
| 10010 | ' is up to the program. The string data type is also used ' |
| 10011 | 'to\n' |
| 10012 | ' represent arrays of bytes, e.g., to hold data read from ' |
| 10013 | 'a\n' |
| 10014 | ' file.\n' |
| 10015 | '\n' |
| 10016 | ' (On systems whose native character set is not ASCII, ' |
| 10017 | 'strings\n' |
| 10018 | ' may use EBCDIC in their internal representation, ' |
| 10019 | 'provided the\n' |
| 10020 | ' functions "chr()" and "ord()" implement a mapping ' |
| 10021 | 'between\n' |
| 10022 | ' ASCII and EBCDIC, and string comparison preserves the ' |
| 10023 | 'ASCII\n' |
| 10024 | ' order. Or perhaps someone can propose a better rule?)\n' |
| 10025 | '\n' |
| 10026 | ' Unicode\n' |
| 10027 | ' The items of a Unicode object are Unicode code units. ' |
| 10028 | 'A\n' |
| 10029 | ' Unicode code unit is represented by a Unicode object of ' |
| 10030 | 'one\n' |
| 10031 | ' item and can hold either a 16-bit or 32-bit value\n' |
| 10032 | ' representing a Unicode ordinal (the maximum value for ' |
| 10033 | 'the\n' |
| 10034 | ' ordinal is given in "sys.maxunicode", and depends on ' |
| 10035 | 'how\n' |
| 10036 | ' Python is configured at compile time). Surrogate pairs ' |
| 10037 | 'may\n' |
| 10038 | ' be present in the Unicode object, and will be reported ' |
| 10039 | 'as two\n' |
| 10040 | ' separate items. The built-in functions "unichr()" and\n' |
| 10041 | ' "ord()" convert between code units and nonnegative ' |
| 10042 | 'integers\n' |
| 10043 | ' representing the Unicode ordinals as defined in the ' |
| 10044 | 'Unicode\n' |
| 10045 | ' Standard 3.0. Conversion from and to other encodings ' |
| 10046 | 'are\n' |
| 10047 | ' possible through the Unicode method "encode()" and the ' |
| 10048 | 'built-\n' |
| 10049 | ' in function "unicode()".\n' |
| 10050 | '\n' |
| 10051 | ' Tuples\n' |
| 10052 | ' The items of a tuple are arbitrary Python objects. ' |
| 10053 | 'Tuples of\n' |
| 10054 | ' two or more items are formed by comma-separated lists ' |
| 10055 | 'of\n' |
| 10056 | " expressions. A tuple of one item (a 'singleton') can " |
| 10057 | 'be\n' |
| 10058 | ' formed by affixing a comma to an expression (an ' |
| 10059 | 'expression by\n' |
| 10060 | ' itself does not create a tuple, since parentheses must ' |
| 10061 | 'be\n' |
| 10062 | ' usable for grouping of expressions). An empty tuple can ' |
| 10063 | 'be\n' |
| 10064 | ' formed by an empty pair of parentheses.\n' |
| 10065 | '\n' |
| 10066 | ' Mutable sequences\n' |
| 10067 | ' Mutable sequences can be changed after they are created. ' |
| 10068 | 'The\n' |
| 10069 | ' subscription and slicing notations can be used as the ' |
| 10070 | 'target of\n' |
| 10071 | ' assignment and "del" (delete) statements.\n' |
| 10072 | '\n' |
| 10073 | ' There are currently two intrinsic mutable sequence types:\n' |
| 10074 | '\n' |
| 10075 | ' Lists\n' |
| 10076 | ' The items of a list are arbitrary Python objects. Lists ' |
| 10077 | 'are\n' |
| 10078 | ' formed by placing a comma-separated list of expressions ' |
| 10079 | 'in\n' |
| 10080 | ' square brackets. (Note that there are no special cases ' |
| 10081 | 'needed\n' |
| 10082 | ' to form lists of length 0 or 1.)\n' |
| 10083 | '\n' |
| 10084 | ' Byte Arrays\n' |
| 10085 | ' A bytearray object is a mutable array. They are created ' |
| 10086 | 'by\n' |
| 10087 | ' the built-in "bytearray()" constructor. Aside from ' |
| 10088 | 'being\n' |
| 10089 | ' mutable (and hence unhashable), byte arrays otherwise ' |
| 10090 | 'provide\n' |
| 10091 | ' the same interface and functionality as immutable bytes\n' |
| 10092 | ' objects.\n' |
| 10093 | '\n' |
| 10094 | ' The extension module "array" provides an additional example ' |
| 10095 | 'of a\n' |
| 10096 | ' mutable sequence type.\n' |
| 10097 | '\n' |
| 10098 | 'Set types\n' |
| 10099 | ' These represent unordered, finite sets of unique, immutable\n' |
| 10100 | ' objects. As such, they cannot be indexed by any subscript. ' |
| 10101 | 'However,\n' |
| 10102 | ' they can be iterated over, and the built-in function "len()"\n' |
| 10103 | ' returns the number of items in a set. Common uses for sets are ' |
| 10104 | 'fast\n' |
| 10105 | ' membership testing, removing duplicates from a sequence, and\n' |
| 10106 | ' computing mathematical operations such as intersection, ' |
| 10107 | 'union,\n' |
| 10108 | ' difference, and symmetric difference.\n' |
| 10109 | '\n' |
| 10110 | ' For set elements, the same immutability rules apply as for\n' |
| 10111 | ' dictionary keys. Note that numeric types obey the normal rules ' |
| 10112 | 'for\n' |
| 10113 | ' numeric comparison: if two numbers compare equal (e.g., "1" ' |
| 10114 | 'and\n' |
| 10115 | ' "1.0"), only one of them can be contained in a set.\n' |
| 10116 | '\n' |
| 10117 | ' There are currently two intrinsic set types:\n' |
| 10118 | '\n' |
| 10119 | ' Sets\n' |
| 10120 | ' These represent a mutable set. They are created by the ' |
| 10121 | 'built-in\n' |
| 10122 | ' "set()" constructor and can be modified afterwards by ' |
| 10123 | 'several\n' |
| 10124 | ' methods, such as "add()".\n' |
| 10125 | '\n' |
| 10126 | ' Frozen sets\n' |
| 10127 | ' These represent an immutable set. They are created by the\n' |
| 10128 | ' built-in "frozenset()" constructor. As a frozenset is ' |
| 10129 | 'immutable\n' |
| 10130 | ' and *hashable*, it can be used again as an element of ' |
| 10131 | 'another\n' |
| 10132 | ' set, or as a dictionary key.\n' |
| 10133 | '\n' |
| 10134 | 'Mappings\n' |
| 10135 | ' These represent finite sets of objects indexed by arbitrary ' |
| 10136 | 'index\n' |
| 10137 | ' sets. The subscript notation "a[k]" selects the item indexed ' |
| 10138 | 'by "k"\n' |
| 10139 | ' from the mapping "a"; this can be used in expressions and as ' |
| 10140 | 'the\n' |
| 10141 | ' target of assignments or "del" statements. The built-in ' |
| 10142 | 'function\n' |
| 10143 | ' "len()" returns the number of items in a mapping.\n' |
| 10144 | '\n' |
| 10145 | ' There is currently a single intrinsic mapping type:\n' |
| 10146 | '\n' |
| 10147 | ' Dictionaries\n' |
| 10148 | ' These represent finite sets of objects indexed by nearly\n' |
| 10149 | ' arbitrary values. The only types of values not acceptable ' |
| 10150 | 'as\n' |
| 10151 | ' keys are values containing lists or dictionaries or other\n' |
| 10152 | ' mutable types that are compared by value rather than by ' |
| 10153 | 'object\n' |
| 10154 | ' identity, the reason being that the efficient ' |
| 10155 | 'implementation of\n' |
| 10156 | " dictionaries requires a key's hash value to remain " |
| 10157 | 'constant.\n' |
| 10158 | ' Numeric types used for keys obey the normal rules for ' |
| 10159 | 'numeric\n' |
| 10160 | ' comparison: if two numbers compare equal (e.g., "1" and ' |
| 10161 | '"1.0")\n' |
| 10162 | ' then they can be used interchangeably to index the same\n' |
| 10163 | ' dictionary entry.\n' |
| 10164 | '\n' |
| 10165 | ' Dictionaries are mutable; they can be created by the ' |
| 10166 | '"{...}"\n' |
| 10167 | ' notation (see section Dictionary displays).\n' |
| 10168 | '\n' |
| 10169 | ' The extension modules "dbm", "gdbm", and "bsddb" provide\n' |
| 10170 | ' additional examples of mapping types.\n' |
| 10171 | '\n' |
| 10172 | 'Callable types\n' |
| 10173 | ' These are the types to which the function call operation (see\n' |
| 10174 | ' section Calls) can be applied:\n' |
| 10175 | '\n' |
| 10176 | ' User-defined functions\n' |
| 10177 | ' A user-defined function object is created by a function\n' |
| 10178 | ' definition (see section Function definitions). It should ' |
| 10179 | 'be\n' |
| 10180 | ' called with an argument list containing the same number of ' |
| 10181 | 'items\n' |
| 10182 | " as the function's formal parameter list.\n" |
| 10183 | '\n' |
| 10184 | ' Special attributes:\n' |
| 10185 | '\n' |
| 10186 | ' ' |
| 10187 | '+-------------------------+---------------------------------+-------------+\n' |
| 10188 | ' | Attribute | Meaning ' |
| 10189 | '| |\n' |
| 10190 | ' ' |
| 10191 | '+=========================+=================================+=============+\n' |
| 10192 | ' | "__doc__" "func_doc" | The function\'s ' |
| 10193 | 'documentation | Writable |\n' |
| 10194 | ' | | string, or "None" if ' |
| 10195 | '| |\n' |
| 10196 | ' | | unavailable. ' |
| 10197 | '| |\n' |
| 10198 | ' ' |
| 10199 | '+-------------------------+---------------------------------+-------------+\n' |
| 10200 | ' | "__name__" "func_name" | The function\'s ' |
| 10201 | 'name. | Writable |\n' |
| 10202 | ' ' |
| 10203 | '+-------------------------+---------------------------------+-------------+\n' |
| 10204 | ' | "__module__" | The name of the module the ' |
| 10205 | '| Writable |\n' |
| 10206 | ' | | function was defined in, or ' |
| 10207 | '| |\n' |
| 10208 | ' | | "None" if unavailable. ' |
| 10209 | '| |\n' |
| 10210 | ' ' |
| 10211 | '+-------------------------+---------------------------------+-------------+\n' |
| 10212 | ' | "__defaults__" | A tuple containing default ' |
| 10213 | '| Writable |\n' |
| 10214 | ' | "func_defaults" | argument values for those ' |
| 10215 | '| |\n' |
| 10216 | ' | | arguments that have defaults, ' |
| 10217 | '| |\n' |
| 10218 | ' | | or "None" if no arguments have ' |
| 10219 | '| |\n' |
| 10220 | ' | | a default value. ' |
| 10221 | '| |\n' |
| 10222 | ' ' |
| 10223 | '+-------------------------+---------------------------------+-------------+\n' |
| 10224 | ' | "__code__" "func_code" | The code object representing ' |
| 10225 | '| Writable |\n' |
| 10226 | ' | | the compiled function body. ' |
| 10227 | '| |\n' |
| 10228 | ' ' |
| 10229 | '+-------------------------+---------------------------------+-------------+\n' |
| 10230 | ' | "__globals__" | A reference to the dictionary ' |
| 10231 | '| Read-only |\n' |
| 10232 | ' | "func_globals" | that holds the ' |
| 10233 | "function's | |\n" |
| 10234 | ' | | global variables --- the global ' |
| 10235 | '| |\n' |
| 10236 | ' | | namespace of the module in ' |
| 10237 | '| |\n' |
| 10238 | ' | | which the function was defined. ' |
| 10239 | '| |\n' |
| 10240 | ' ' |
| 10241 | '+-------------------------+---------------------------------+-------------+\n' |
| 10242 | ' | "__dict__" "func_dict" | The namespace supporting ' |
| 10243 | '| Writable |\n' |
| 10244 | ' | | arbitrary function attributes. ' |
| 10245 | '| |\n' |
| 10246 | ' ' |
| 10247 | '+-------------------------+---------------------------------+-------------+\n' |
| 10248 | ' | "__closure__" | "None" or a tuple of cells that ' |
| 10249 | '| Read-only |\n' |
| 10250 | ' | "func_closure" | contain bindings for the ' |
| 10251 | '| |\n' |
| 10252 | " | | function's free variables. " |
| 10253 | '| |\n' |
| 10254 | ' ' |
| 10255 | '+-------------------------+---------------------------------+-------------+\n' |
| 10256 | '\n' |
| 10257 | ' Most of the attributes labelled "Writable" check the type ' |
| 10258 | 'of the\n' |
| 10259 | ' assigned value.\n' |
| 10260 | '\n' |
| 10261 | ' Changed in version 2.4: "func_name" is now writable.\n' |
| 10262 | '\n' |
| 10263 | ' Changed in version 2.6: The double-underscore attributes\n' |
| 10264 | ' "__closure__", "__code__", "__defaults__", and ' |
| 10265 | '"__globals__"\n' |
| 10266 | ' were introduced as aliases for the corresponding "func_*"\n' |
| 10267 | ' attributes for forwards compatibility with Python 3.\n' |
| 10268 | '\n' |
| 10269 | ' Function objects also support getting and setting ' |
| 10270 | 'arbitrary\n' |
| 10271 | ' attributes, which can be used, for example, to attach ' |
| 10272 | 'metadata\n' |
| 10273 | ' to functions. Regular attribute dot-notation is used to ' |
| 10274 | 'get and\n' |
| 10275 | ' set such attributes. *Note that the current implementation ' |
| 10276 | 'only\n' |
| 10277 | ' supports function attributes on user-defined functions. ' |
| 10278 | 'Function\n' |
| 10279 | ' attributes on built-in functions may be supported in the\n' |
| 10280 | ' future.*\n' |
| 10281 | '\n' |
| 10282 | " Additional information about a function's definition can " |
| 10283 | 'be\n' |
| 10284 | ' retrieved from its code object; see the description of ' |
| 10285 | 'internal\n' |
| 10286 | ' types below.\n' |
| 10287 | '\n' |
| 10288 | ' User-defined methods\n' |
| 10289 | ' A user-defined method object combines a class, a class ' |
| 10290 | 'instance\n' |
| 10291 | ' (or "None") and any callable object (normally a ' |
| 10292 | 'user-defined\n' |
| 10293 | ' function).\n' |
| 10294 | '\n' |
| 10295 | ' Special read-only attributes: "im_self" is the class ' |
| 10296 | 'instance\n' |
| 10297 | ' object, "im_func" is the function object; "im_class" is ' |
| 10298 | 'the\n' |
| 10299 | ' class of "im_self" for bound methods or the class that ' |
| 10300 | 'asked for\n' |
| 10301 | ' the method for unbound methods; "__doc__" is the method\'s\n' |
| 10302 | ' documentation (same as "im_func.__doc__"); "__name__" is ' |
| 10303 | 'the\n' |
| 10304 | ' method name (same as "im_func.__name__"); "__module__" is ' |
| 10305 | 'the\n' |
| 10306 | ' name of the module the method was defined in, or "None" if\n' |
| 10307 | ' unavailable.\n' |
| 10308 | '\n' |
| 10309 | ' Changed in version 2.2: "im_self" used to refer to the ' |
| 10310 | 'class\n' |
| 10311 | ' that defined the method.\n' |
| 10312 | '\n' |
| 10313 | ' Changed in version 2.6: For Python 3 ' |
| 10314 | 'forward-compatibility,\n' |
| 10315 | ' "im_func" is also available as "__func__", and "im_self" ' |
| 10316 | 'as\n' |
| 10317 | ' "__self__".\n' |
| 10318 | '\n' |
| 10319 | ' Methods also support accessing (but not setting) the ' |
| 10320 | 'arbitrary\n' |
| 10321 | ' function attributes on the underlying function object.\n' |
| 10322 | '\n' |
| 10323 | ' User-defined method objects may be created when getting an\n' |
| 10324 | ' attribute of a class (perhaps via an instance of that ' |
| 10325 | 'class), if\n' |
| 10326 | ' that attribute is a user-defined function object, an ' |
| 10327 | 'unbound\n' |
| 10328 | ' user-defined method object, or a class method object. When ' |
| 10329 | 'the\n' |
| 10330 | ' attribute is a user-defined method object, a new method ' |
| 10331 | 'object\n' |
| 10332 | ' is only created if the class from which it is being ' |
| 10333 | 'retrieved is\n' |
| 10334 | ' the same as, or a derived class of, the class stored in ' |
| 10335 | 'the\n' |
| 10336 | ' original method object; otherwise, the original method ' |
| 10337 | 'object is\n' |
| 10338 | ' used as it is.\n' |
| 10339 | '\n' |
| 10340 | ' When a user-defined method object is created by retrieving ' |
| 10341 | 'a\n' |
| 10342 | ' user-defined function object from a class, its "im_self"\n' |
| 10343 | ' attribute is "None" and the method object is said to be ' |
| 10344 | 'unbound.\n' |
| 10345 | ' When one is created by retrieving a user-defined function ' |
| 10346 | 'object\n' |
| 10347 | ' from a class via one of its instances, its "im_self" ' |
| 10348 | 'attribute\n' |
| 10349 | ' is the instance, and the method object is said to be bound. ' |
| 10350 | 'In\n' |
| 10351 | ' either case, the new method\'s "im_class" attribute is the ' |
| 10352 | 'class\n' |
| 10353 | ' from which the retrieval takes place, and its "im_func"\n' |
| 10354 | ' attribute is the original function object.\n' |
| 10355 | '\n' |
| 10356 | ' When a user-defined method object is created by retrieving\n' |
| 10357 | ' another method object from a class or instance, the ' |
| 10358 | 'behaviour is\n' |
| 10359 | ' the same as for a function object, except that the ' |
| 10360 | '"im_func"\n' |
| 10361 | ' attribute of the new instance is not the original method ' |
| 10362 | 'object\n' |
| 10363 | ' but its "im_func" attribute.\n' |
| 10364 | '\n' |
| 10365 | ' When a user-defined method object is created by retrieving ' |
| 10366 | 'a\n' |
| 10367 | ' class method object from a class or instance, its ' |
| 10368 | '"im_self"\n' |
| 10369 | ' attribute is the class itself, and its "im_func" attribute ' |
| 10370 | 'is\n' |
| 10371 | ' the function object underlying the class method.\n' |
| 10372 | '\n' |
| 10373 | ' When an unbound user-defined method object is called, the\n' |
| 10374 | ' underlying function ("im_func") is called, with the ' |
| 10375 | 'restriction\n' |
| 10376 | ' that the first argument must be an instance of the proper ' |
| 10377 | 'class\n' |
| 10378 | ' ("im_class") or of a derived class thereof.\n' |
| 10379 | '\n' |
| 10380 | ' When a bound user-defined method object is called, the\n' |
| 10381 | ' underlying function ("im_func") is called, inserting the ' |
| 10382 | 'class\n' |
| 10383 | ' instance ("im_self") in front of the argument list. For\n' |
| 10384 | ' instance, when "C" is a class which contains a definition ' |
| 10385 | 'for a\n' |
| 10386 | ' function "f()", and "x" is an instance of "C", calling ' |
| 10387 | '"x.f(1)"\n' |
| 10388 | ' is equivalent to calling "C.f(x, 1)".\n' |
| 10389 | '\n' |
| 10390 | ' When a user-defined method object is derived from a class ' |
| 10391 | 'method\n' |
| 10392 | ' object, the "class instance" stored in "im_self" will ' |
| 10393 | 'actually\n' |
| 10394 | ' be the class itself, so that calling either "x.f(1)" or ' |
| 10395 | '"C.f(1)"\n' |
| 10396 | ' is equivalent to calling "f(C,1)" where "f" is the ' |
| 10397 | 'underlying\n' |
| 10398 | ' function.\n' |
| 10399 | '\n' |
| 10400 | ' Note that the transformation from function object to ' |
| 10401 | '(unbound or\n' |
| 10402 | ' bound) method object happens each time the attribute is\n' |
| 10403 | ' retrieved from the class or instance. In some cases, a ' |
| 10404 | 'fruitful\n' |
| 10405 | ' optimization is to assign the attribute to a local variable ' |
| 10406 | 'and\n' |
| 10407 | ' call that local variable. Also notice that this ' |
| 10408 | 'transformation\n' |
| 10409 | ' only happens for user-defined functions; other callable ' |
| 10410 | 'objects\n' |
| 10411 | ' (and all non-callable objects) are retrieved without\n' |
| 10412 | ' transformation. It is also important to note that ' |
| 10413 | 'user-defined\n' |
| 10414 | ' functions which are attributes of a class instance are not\n' |
| 10415 | ' converted to bound methods; this *only* happens when the\n' |
| 10416 | ' function is an attribute of the class.\n' |
| 10417 | '\n' |
| 10418 | ' Generator functions\n' |
| 10419 | ' A function or method which uses the "yield" statement (see\n' |
| 10420 | ' section The yield statement) is called a *generator ' |
| 10421 | 'function*.\n' |
| 10422 | ' Such a function, when called, always returns an iterator ' |
| 10423 | 'object\n' |
| 10424 | ' which can be used to execute the body of the function: ' |
| 10425 | 'calling\n' |
| 10426 | ' the iterator\'s "next()" method will cause the function to\n' |
| 10427 | ' execute until it provides a value using the "yield" ' |
| 10428 | 'statement.\n' |
| 10429 | ' When the function executes a "return" statement or falls ' |
| 10430 | 'off the\n' |
| 10431 | ' end, a "StopIteration" exception is raised and the iterator ' |
| 10432 | 'will\n' |
| 10433 | ' have reached the end of the set of values to be returned.\n' |
| 10434 | '\n' |
| 10435 | ' Built-in functions\n' |
| 10436 | ' A built-in function object is a wrapper around a C ' |
| 10437 | 'function.\n' |
| 10438 | ' Examples of built-in functions are "len()" and ' |
| 10439 | '"math.sin()"\n' |
| 10440 | ' ("math" is a standard built-in module). The number and type ' |
| 10441 | 'of\n' |
| 10442 | ' the arguments are determined by the C function. Special ' |
| 10443 | 'read-\n' |
| 10444 | ' only attributes: "__doc__" is the function\'s ' |
| 10445 | 'documentation\n' |
| 10446 | ' string, or "None" if unavailable; "__name__" is the ' |
| 10447 | "function's\n" |
| 10448 | ' name; "__self__" is set to "None" (but see the next item);\n' |
| 10449 | ' "__module__" is the name of the module the function was ' |
| 10450 | 'defined\n' |
| 10451 | ' in or "None" if unavailable.\n' |
| 10452 | '\n' |
| 10453 | ' Built-in methods\n' |
| 10454 | ' This is really a different disguise of a built-in function, ' |
| 10455 | 'this\n' |
| 10456 | ' time containing an object passed to the C function as an\n' |
| 10457 | ' implicit extra argument. An example of a built-in method ' |
| 10458 | 'is\n' |
| 10459 | ' "alist.append()", assuming *alist* is a list object. In ' |
| 10460 | 'this\n' |
| 10461 | ' case, the special read-only attribute "__self__" is set to ' |
| 10462 | 'the\n' |
| 10463 | ' object denoted by *alist*.\n' |
| 10464 | '\n' |
| 10465 | ' Class Types\n' |
| 10466 | ' Class types, or "new-style classes," are callable. These\n' |
| 10467 | ' objects normally act as factories for new instances of\n' |
| 10468 | ' themselves, but variations are possible for class types ' |
| 10469 | 'that\n' |
| 10470 | ' override "__new__()". The arguments of the call are passed ' |
| 10471 | 'to\n' |
| 10472 | ' "__new__()" and, in the typical case, to "__init__()" to\n' |
| 10473 | ' initialize the new instance.\n' |
| 10474 | '\n' |
| 10475 | ' Classic Classes\n' |
| 10476 | ' Class objects are described below. When a class object is\n' |
| 10477 | ' called, a new class instance (also described below) is ' |
| 10478 | 'created\n' |
| 10479 | " and returned. This implies a call to the class's " |
| 10480 | '"__init__()"\n' |
| 10481 | ' method if it has one. Any arguments are passed on to the\n' |
| 10482 | ' "__init__()" method. If there is no "__init__()" method, ' |
| 10483 | 'the\n' |
| 10484 | ' class must be called without arguments.\n' |
| 10485 | '\n' |
| 10486 | ' Class instances\n' |
| 10487 | ' Class instances are described below. Class instances are\n' |
| 10488 | ' callable only when the class has a "__call__()" method;\n' |
| 10489 | ' "x(arguments)" is a shorthand for "x.__call__(arguments)".\n' |
| 10490 | '\n' |
| 10491 | 'Modules\n' |
| 10492 | ' Modules are imported by the "import" statement (see section ' |
| 10493 | 'The\n' |
| 10494 | ' import statement). A module object has a namespace implemented ' |
| 10495 | 'by a\n' |
| 10496 | ' dictionary object (this is the dictionary referenced by the\n' |
| 10497 | ' func_globals attribute of functions defined in the module).\n' |
| 10498 | ' Attribute references are translated to lookups in this ' |
| 10499 | 'dictionary,\n' |
| 10500 | ' e.g., "m.x" is equivalent to "m.__dict__["x"]". A module ' |
| 10501 | 'object\n' |
| 10502 | ' does not contain the code object used to initialize the ' |
| 10503 | 'module\n' |
| 10504 | " (since it isn't needed once the initialization is done).\n" |
| 10505 | '\n' |
| 10506 | " Attribute assignment updates the module's namespace " |
| 10507 | 'dictionary,\n' |
| 10508 | ' e.g., "m.x = 1" is equivalent to "m.__dict__["x"] = 1".\n' |
| 10509 | '\n' |
| 10510 | ' Special read-only attribute: "__dict__" is the module\'s ' |
| 10511 | 'namespace\n' |
| 10512 | ' as a dictionary object.\n' |
| 10513 | '\n' |
| 10514 | ' **CPython implementation detail:** Because of the way CPython\n' |
| 10515 | ' clears module dictionaries, the module dictionary will be ' |
| 10516 | 'cleared\n' |
| 10517 | ' when the module falls out of scope even if the dictionary ' |
| 10518 | 'still has\n' |
| 10519 | ' live references. To avoid this, copy the dictionary or keep ' |
| 10520 | 'the\n' |
| 10521 | ' module around while using its dictionary directly.\n' |
| 10522 | '\n' |
| 10523 | ' Predefined (writable) attributes: "__name__" is the module\'s ' |
| 10524 | 'name;\n' |
| 10525 | ' "__doc__" is the module\'s documentation string, or "None" if\n' |
| 10526 | ' unavailable; "__file__" is the pathname of the file from which ' |
| 10527 | 'the\n' |
| 10528 | ' module was loaded, if it was loaded from a file. The ' |
| 10529 | '"__file__"\n' |
| 10530 | ' attribute is not present for C modules that are statically ' |
| 10531 | 'linked\n' |
| 10532 | ' into the interpreter; for extension modules loaded dynamically ' |
| 10533 | 'from\n' |
| 10534 | ' a shared library, it is the pathname of the shared library ' |
| 10535 | 'file.\n' |
| 10536 | '\n' |
| 10537 | 'Classes\n' |
| 10538 | ' Both class types (new-style classes) and class objects (old-\n' |
| 10539 | ' style/classic classes) are typically created by class ' |
| 10540 | 'definitions\n' |
| 10541 | ' (see section Class definitions). A class has a namespace\n' |
| 10542 | ' implemented by a dictionary object. Class attribute references ' |
| 10543 | 'are\n' |
| 10544 | ' translated to lookups in this dictionary, e.g., "C.x" is ' |
| 10545 | 'translated\n' |
| 10546 | ' to "C.__dict__["x"]" (although for new-style classes in ' |
| 10547 | 'particular\n' |
| 10548 | ' there are a number of hooks which allow for other means of ' |
| 10549 | 'locating\n' |
| 10550 | ' attributes). When the attribute name is not found there, the\n' |
| 10551 | ' attribute search continues in the base classes. For ' |
| 10552 | 'old-style\n' |
| 10553 | ' classes, the search is depth-first, left-to-right in the order ' |
| 10554 | 'of\n' |
| 10555 | ' occurrence in the base class list. New-style classes use the ' |
| 10556 | 'more\n' |
| 10557 | ' complex C3 method resolution order which behaves correctly ' |
| 10558 | 'even in\n' |
| 10559 | " the presence of 'diamond' inheritance structures where there " |
| 10560 | 'are\n' |
| 10561 | ' multiple inheritance paths leading back to a common ancestor.\n' |
| 10562 | ' Additional details on the C3 MRO used by new-style classes can ' |
| 10563 | 'be\n' |
| 10564 | ' found in the documentation accompanying the 2.3 release at\n' |
| 10565 | ' https://www.python.org/download/releases/2.3/mro/.\n' |
| 10566 | '\n' |
| 10567 | ' When a class attribute reference (for class "C", say) would ' |
| 10568 | 'yield a\n' |
| 10569 | ' user-defined function object or an unbound user-defined ' |
| 10570 | 'method\n' |
| 10571 | ' object whose associated class is either "C" or one of its ' |
| 10572 | 'base\n' |
| 10573 | ' classes, it is transformed into an unbound user-defined ' |
| 10574 | 'method\n' |
| 10575 | ' object whose "im_class" attribute is "C". When it would yield ' |
| 10576 | 'a\n' |
| 10577 | ' class method object, it is transformed into a bound ' |
| 10578 | 'user-defined\n' |
| 10579 | ' method object whose "im_self" attribute is "C". When it ' |
| 10580 | 'would\n' |
| 10581 | ' yield a static method object, it is transformed into the ' |
| 10582 | 'object\n' |
| 10583 | ' wrapped by the static method object. See section Implementing\n' |
| 10584 | ' Descriptors for another way in which attributes retrieved from ' |
| 10585 | 'a\n' |
| 10586 | ' class may differ from those actually contained in its ' |
| 10587 | '"__dict__"\n' |
| 10588 | ' (note that only new-style classes support descriptors).\n' |
| 10589 | '\n' |
| 10590 | " Class attribute assignments update the class's dictionary, " |
| 10591 | 'never\n' |
| 10592 | ' the dictionary of a base class.\n' |
| 10593 | '\n' |
| 10594 | ' A class object can be called (see above) to yield a class ' |
| 10595 | 'instance\n' |
| 10596 | ' (see below).\n' |
| 10597 | '\n' |
| 10598 | ' Special attributes: "__name__" is the class name; "__module__" ' |
| 10599 | 'is\n' |
| 10600 | ' the module name in which the class was defined; "__dict__" is ' |
| 10601 | 'the\n' |
| 10602 | ' dictionary containing the class\'s namespace; "__bases__" is a ' |
| 10603 | 'tuple\n' |
| 10604 | ' (possibly empty or a singleton) containing the base classes, ' |
| 10605 | 'in the\n' |
| 10606 | ' order of their occurrence in the base class list; "__doc__" is ' |
| 10607 | 'the\n' |
| 10608 | " class's documentation string, or None if undefined.\n" |
| 10609 | '\n' |
| 10610 | 'Class instances\n' |
| 10611 | ' A class instance is created by calling a class object (see ' |
| 10612 | 'above).\n' |
| 10613 | ' A class instance has a namespace implemented as a dictionary ' |
| 10614 | 'which\n' |
| 10615 | ' is the first place in which attribute references are ' |
| 10616 | 'searched.\n' |
| 10617 | " When an attribute is not found there, and the instance's class " |
| 10618 | 'has\n' |
| 10619 | ' an attribute by that name, the search continues with the ' |
| 10620 | 'class\n' |
| 10621 | ' attributes. If a class attribute is found that is a ' |
| 10622 | 'user-defined\n' |
| 10623 | ' function object or an unbound user-defined method object ' |
| 10624 | 'whose\n' |
| 10625 | ' associated class is the class (call it "C") of the instance ' |
| 10626 | 'for\n' |
| 10627 | ' which the attribute reference was initiated or one of its ' |
| 10628 | 'bases, it\n' |
| 10629 | ' is transformed into a bound user-defined method object whose\n' |
| 10630 | ' "im_class" attribute is "C" and whose "im_self" attribute is ' |
| 10631 | 'the\n' |
| 10632 | ' instance. Static method and class method objects are also\n' |
| 10633 | ' transformed, as if they had been retrieved from class "C"; ' |
| 10634 | 'see\n' |
| 10635 | ' above under "Classes". See section Implementing Descriptors ' |
| 10636 | 'for\n' |
| 10637 | ' another way in which attributes of a class retrieved via its\n' |
| 10638 | ' instances may differ from the objects actually stored in the\n' |
| 10639 | ' class\'s "__dict__". If no class attribute is found, and the\n' |
| 10640 | ' object\'s class has a "__getattr__()" method, that is called ' |
| 10641 | 'to\n' |
| 10642 | ' satisfy the lookup.\n' |
| 10643 | '\n' |
| 10644 | " Attribute assignments and deletions update the instance's\n" |
| 10645 | " dictionary, never a class's dictionary. If the class has a\n" |
| 10646 | ' "__setattr__()" or "__delattr__()" method, this is called ' |
| 10647 | 'instead\n' |
| 10648 | ' of updating the instance dictionary directly.\n' |
| 10649 | '\n' |
| 10650 | ' Class instances can pretend to be numbers, sequences, or ' |
| 10651 | 'mappings\n' |
| 10652 | ' if they have methods with certain special names. See section\n' |
| 10653 | ' Special method names.\n' |
| 10654 | '\n' |
| 10655 | ' Special attributes: "__dict__" is the attribute dictionary;\n' |
| 10656 | ' "__class__" is the instance\'s class.\n' |
| 10657 | '\n' |
| 10658 | 'Files\n' |
| 10659 | ' A file object represents an open file. File objects are ' |
| 10660 | 'created by\n' |
| 10661 | ' the "open()" built-in function, and also by "os.popen()",\n' |
| 10662 | ' "os.fdopen()", and the "makefile()" method of socket objects ' |
| 10663 | '(and\n' |
| 10664 | ' perhaps by other functions or methods provided by extension\n' |
| 10665 | ' modules). The objects "sys.stdin", "sys.stdout" and ' |
| 10666 | '"sys.stderr"\n' |
| 10667 | ' are initialized to file objects corresponding to the ' |
| 10668 | "interpreter's\n" |
| 10669 | ' standard input, output and error streams. See File Objects ' |
| 10670 | 'for\n' |
| 10671 | ' complete documentation of file objects.\n' |
| 10672 | '\n' |
| 10673 | 'Internal types\n' |
| 10674 | ' A few types used internally by the interpreter are exposed to ' |
| 10675 | 'the\n' |
| 10676 | ' user. Their definitions may change with future versions of ' |
| 10677 | 'the\n' |
| 10678 | ' interpreter, but they are mentioned here for completeness.\n' |
| 10679 | '\n' |
| 10680 | ' Code objects\n' |
| 10681 | ' Code objects represent *byte-compiled* executable Python ' |
| 10682 | 'code,\n' |
| 10683 | ' or *bytecode*. The difference between a code object and a\n' |
| 10684 | ' function object is that the function object contains an ' |
| 10685 | 'explicit\n' |
| 10686 | " reference to the function's globals (the module in which it " |
| 10687 | 'was\n' |
| 10688 | ' defined), while a code object contains no context; also ' |
| 10689 | 'the\n' |
| 10690 | ' default argument values are stored in the function object, ' |
| 10691 | 'not\n' |
| 10692 | ' in the code object (because they represent values ' |
| 10693 | 'calculated at\n' |
| 10694 | ' run-time). Unlike function objects, code objects are ' |
| 10695 | 'immutable\n' |
| 10696 | ' and contain no references (directly or indirectly) to ' |
| 10697 | 'mutable\n' |
| 10698 | ' objects.\n' |
| 10699 | '\n' |
| 10700 | ' Special read-only attributes: "co_name" gives the function ' |
| 10701 | 'name;\n' |
| 10702 | ' "co_argcount" is the number of positional arguments ' |
| 10703 | '(including\n' |
| 10704 | ' arguments with default values); "co_nlocals" is the number ' |
| 10705 | 'of\n' |
| 10706 | ' local variables used by the function (including ' |
| 10707 | 'arguments);\n' |
| 10708 | ' "co_varnames" is a tuple containing the names of the local\n' |
| 10709 | ' variables (starting with the argument names); "co_cellvars" ' |
| 10710 | 'is a\n' |
| 10711 | ' tuple containing the names of local variables that are\n' |
| 10712 | ' referenced by nested functions; "co_freevars" is a tuple\n' |
| 10713 | ' containing the names of free variables; "co_code" is a ' |
| 10714 | 'string\n' |
| 10715 | ' representing the sequence of bytecode instructions; ' |
| 10716 | '"co_consts"\n' |
| 10717 | ' is a tuple containing the literals used by the bytecode;\n' |
| 10718 | ' "co_names" is a tuple containing the names used by the ' |
| 10719 | 'bytecode;\n' |
| 10720 | ' "co_filename" is the filename from which the code was ' |
| 10721 | 'compiled;\n' |
| 10722 | ' "co_firstlineno" is the first line number of the function;\n' |
| 10723 | ' "co_lnotab" is a string encoding the mapping from bytecode\n' |
| 10724 | ' offsets to line numbers (for details see the source code of ' |
| 10725 | 'the\n' |
| 10726 | ' interpreter); "co_stacksize" is the required stack size\n' |
| 10727 | ' (including local variables); "co_flags" is an integer ' |
| 10728 | 'encoding a\n' |
| 10729 | ' number of flags for the interpreter.\n' |
| 10730 | '\n' |
| 10731 | ' The following flag bits are defined for "co_flags": bit ' |
| 10732 | '"0x04"\n' |
| 10733 | ' is set if the function uses the "*arguments" syntax to ' |
| 10734 | 'accept an\n' |
| 10735 | ' arbitrary number of positional arguments; bit "0x08" is set ' |
| 10736 | 'if\n' |
| 10737 | ' the function uses the "**keywords" syntax to accept ' |
| 10738 | 'arbitrary\n' |
| 10739 | ' keyword arguments; bit "0x20" is set if the function is a\n' |
| 10740 | ' generator.\n' |
| 10741 | '\n' |
| 10742 | ' Future feature declarations ("from __future__ import ' |
| 10743 | 'division")\n' |
| 10744 | ' also use bits in "co_flags" to indicate whether a code ' |
| 10745 | 'object\n' |
| 10746 | ' was compiled with a particular feature enabled: bit ' |
| 10747 | '"0x2000" is\n' |
| 10748 | ' set if the function was compiled with future division ' |
| 10749 | 'enabled;\n' |
| 10750 | ' bits "0x10" and "0x1000" were used in earlier versions of\n' |
| 10751 | ' Python.\n' |
| 10752 | '\n' |
| 10753 | ' Other bits in "co_flags" are reserved for internal use.\n' |
| 10754 | '\n' |
| 10755 | ' If a code object represents a function, the first item in\n' |
| 10756 | ' "co_consts" is the documentation string of the function, ' |
| 10757 | 'or\n' |
| 10758 | ' "None" if undefined.\n' |
| 10759 | '\n' |
| 10760 | ' Frame objects\n' |
| 10761 | ' Frame objects represent execution frames. They may occur ' |
| 10762 | 'in\n' |
| 10763 | ' traceback objects (see below).\n' |
| 10764 | '\n' |
| 10765 | ' Special read-only attributes: "f_back" is to the previous ' |
| 10766 | 'stack\n' |
| 10767 | ' frame (towards the caller), or "None" if this is the ' |
| 10768 | 'bottom\n' |
| 10769 | ' stack frame; "f_code" is the code object being executed in ' |
| 10770 | 'this\n' |
| 10771 | ' frame; "f_locals" is the dictionary used to look up local\n' |
| 10772 | ' variables; "f_globals" is used for global variables;\n' |
| 10773 | ' "f_builtins" is used for built-in (intrinsic) names;\n' |
| 10774 | ' "f_restricted" is a flag indicating whether the function ' |
| 10775 | 'is\n' |
| 10776 | ' executing in restricted execution mode; "f_lasti" gives ' |
| 10777 | 'the\n' |
| 10778 | ' precise instruction (this is an index into the bytecode ' |
| 10779 | 'string\n' |
| 10780 | ' of the code object).\n' |
| 10781 | '\n' |
| 10782 | ' Special writable attributes: "f_trace", if not "None", is ' |
| 10783 | 'a\n' |
| 10784 | ' function called at the start of each source code line (this ' |
| 10785 | 'is\n' |
| 10786 | ' used by the debugger); "f_exc_type", "f_exc_value",\n' |
| 10787 | ' "f_exc_traceback" represent the last exception raised in ' |
| 10788 | 'the\n' |
| 10789 | ' parent frame provided another exception was ever raised in ' |
| 10790 | 'the\n' |
| 10791 | ' current frame (in all other cases they are None); ' |
| 10792 | '"f_lineno" is\n' |
| 10793 | ' the current line number of the frame --- writing to this ' |
| 10794 | 'from\n' |
| 10795 | ' within a trace function jumps to the given line (only for ' |
| 10796 | 'the\n' |
| 10797 | ' bottom-most frame). A debugger can implement a Jump ' |
| 10798 | 'command\n' |
| 10799 | ' (aka Set Next Statement) by writing to f_lineno.\n' |
| 10800 | '\n' |
| 10801 | ' Traceback objects\n' |
| 10802 | ' Traceback objects represent a stack trace of an exception. ' |
| 10803 | 'A\n' |
| 10804 | ' traceback object is created when an exception occurs. When ' |
| 10805 | 'the\n' |
| 10806 | ' search for an exception handler unwinds the execution ' |
| 10807 | 'stack, at\n' |
| 10808 | ' each unwound level a traceback object is inserted in front ' |
| 10809 | 'of\n' |
| 10810 | ' the current traceback. When an exception handler is ' |
| 10811 | 'entered,\n' |
| 10812 | ' the stack trace is made available to the program. (See ' |
| 10813 | 'section\n' |
| 10814 | ' The try statement.) It is accessible as ' |
| 10815 | '"sys.exc_traceback", and\n' |
| 10816 | ' also as the third item of the tuple returned by\n' |
| 10817 | ' "sys.exc_info()". The latter is the preferred interface, ' |
| 10818 | 'since\n' |
| 10819 | ' it works correctly when the program is using multiple ' |
| 10820 | 'threads.\n' |
| 10821 | ' When the program contains no suitable handler, the stack ' |
| 10822 | 'trace\n' |
| 10823 | ' is written (nicely formatted) to the standard error stream; ' |
| 10824 | 'if\n' |
| 10825 | ' the interpreter is interactive, it is also made available ' |
| 10826 | 'to the\n' |
| 10827 | ' user as "sys.last_traceback".\n' |
| 10828 | '\n' |
| 10829 | ' Special read-only attributes: "tb_next" is the next level ' |
| 10830 | 'in the\n' |
| 10831 | ' stack trace (towards the frame where the exception ' |
| 10832 | 'occurred), or\n' |
| 10833 | ' "None" if there is no next level; "tb_frame" points to the\n' |
| 10834 | ' execution frame of the current level; "tb_lineno" gives the ' |
| 10835 | 'line\n' |
| 10836 | ' number where the exception occurred; "tb_lasti" indicates ' |
| 10837 | 'the\n' |
| 10838 | ' precise instruction. The line number and last instruction ' |
| 10839 | 'in\n' |
| 10840 | ' the traceback may differ from the line number of its frame\n' |
| 10841 | ' object if the exception occurred in a "try" statement with ' |
| 10842 | 'no\n' |
| 10843 | ' matching except clause or with a finally clause.\n' |
| 10844 | '\n' |
| 10845 | ' Slice objects\n' |
| 10846 | ' Slice objects are used to represent slices when *extended ' |
| 10847 | 'slice\n' |
| 10848 | ' syntax* is used. This is a slice using two colons, or ' |
| 10849 | 'multiple\n' |
| 10850 | ' slices or ellipses separated by commas, e.g., ' |
| 10851 | '"a[i:j:step]",\n' |
| 10852 | ' "a[i:j, k:l]", or "a[..., i:j]". They are also created by ' |
| 10853 | 'the\n' |
| 10854 | ' built-in "slice()" function.\n' |
| 10855 | '\n' |
| 10856 | ' Special read-only attributes: "start" is the lower bound; ' |
| 10857 | '"stop"\n' |
| 10858 | ' is the upper bound; "step" is the step value; each is ' |
| 10859 | '"None" if\n' |
| 10860 | ' omitted. These attributes can have any type.\n' |
| 10861 | '\n' |
| 10862 | ' Slice objects support one method:\n' |
| 10863 | '\n' |
| 10864 | ' slice.indices(self, length)\n' |
| 10865 | '\n' |
| 10866 | ' This method takes a single integer argument *length* ' |
| 10867 | 'and\n' |
| 10868 | ' computes information about the extended slice that the ' |
| 10869 | 'slice\n' |
| 10870 | ' object would describe if applied to a sequence of ' |
| 10871 | '*length*\n' |
| 10872 | ' items. It returns a tuple of three integers; ' |
| 10873 | 'respectively\n' |
| 10874 | ' these are the *start* and *stop* indices and the *step* ' |
| 10875 | 'or\n' |
| 10876 | ' stride length of the slice. Missing or out-of-bounds ' |
| 10877 | 'indices\n' |
| 10878 | ' are handled in a manner consistent with regular slices.\n' |
| 10879 | '\n' |
| 10880 | ' New in version 2.3.\n' |
| 10881 | '\n' |
| 10882 | ' Static method objects\n' |
| 10883 | ' Static method objects provide a way of defeating the\n' |
| 10884 | ' transformation of function objects to method objects ' |
| 10885 | 'described\n' |
| 10886 | ' above. A static method object is a wrapper around any ' |
| 10887 | 'other\n' |
| 10888 | ' object, usually a user-defined method object. When a ' |
| 10889 | 'static\n' |
| 10890 | ' method object is retrieved from a class or a class ' |
| 10891 | 'instance, the\n' |
| 10892 | ' object actually returned is the wrapped object, which is ' |
| 10893 | 'not\n' |
| 10894 | ' subject to any further transformation. Static method ' |
| 10895 | 'objects are\n' |
| 10896 | ' not themselves callable, although the objects they wrap ' |
| 10897 | 'usually\n' |
| 10898 | ' are. Static method objects are created by the built-in\n' |
| 10899 | ' "staticmethod()" constructor.\n' |
| 10900 | '\n' |
| 10901 | ' Class method objects\n' |
| 10902 | ' A class method object, like a static method object, is a ' |
| 10903 | 'wrapper\n' |
| 10904 | ' around another object that alters the way in which that ' |
| 10905 | 'object\n' |
| 10906 | ' is retrieved from classes and class instances. The ' |
| 10907 | 'behaviour of\n' |
| 10908 | ' class method objects upon such retrieval is described ' |
| 10909 | 'above,\n' |
| 10910 | ' under "User-defined methods". Class method objects are ' |
| 10911 | 'created\n' |
| 10912 | ' by the built-in "classmethod()" constructor.\n', |
| 10913 | 'typesfunctions': '\n' |
| 10914 | 'Functions\n' |
| 10915 | '*********\n' |
| 10916 | '\n' |
| 10917 | 'Function objects are created by function definitions. ' |
| 10918 | 'The only\n' |
| 10919 | 'operation on a function object is to call it: ' |
| 10920 | '"func(argument-list)".\n' |
| 10921 | '\n' |
| 10922 | 'There are really two flavors of function objects: ' |
| 10923 | 'built-in functions\n' |
| 10924 | 'and user-defined functions. Both support the same ' |
| 10925 | 'operation (to call\n' |
| 10926 | 'the function), but the implementation is different, ' |
| 10927 | 'hence the\n' |
| 10928 | 'different object types.\n' |
| 10929 | '\n' |
| 10930 | 'See Function definitions for more information.\n', |
| 10931 | 'typesmapping': '\n' |
| 10932 | 'Mapping Types --- "dict"\n' |
| 10933 | '************************\n' |
| 10934 | '\n' |
| 10935 | 'A *mapping* object maps *hashable* values to arbitrary ' |
| 10936 | 'objects.\n' |
| 10937 | 'Mappings are mutable objects. There is currently only one ' |
| 10938 | 'standard\n' |
| 10939 | 'mapping type, the *dictionary*. (For other containers see ' |
| 10940 | 'the built\n' |
| 10941 | 'in "list", "set", and "tuple" classes, and the ' |
| 10942 | '"collections" module.)\n' |
| 10943 | '\n' |
| 10944 | "A dictionary's keys are *almost* arbitrary values. Values " |
| 10945 | 'that are\n' |
| 10946 | 'not *hashable*, that is, values containing lists, ' |
| 10947 | 'dictionaries or\n' |
| 10948 | 'other mutable types (that are compared by value rather ' |
| 10949 | 'than by object\n' |
| 10950 | 'identity) may not be used as keys. Numeric types used for ' |
| 10951 | 'keys obey\n' |
| 10952 | 'the normal rules for numeric comparison: if two numbers ' |
| 10953 | 'compare equal\n' |
| 10954 | '(such as "1" and "1.0") then they can be used ' |
| 10955 | 'interchangeably to index\n' |
| 10956 | 'the same dictionary entry. (Note however, that since ' |
| 10957 | 'computers store\n' |
| 10958 | 'floating-point numbers as approximations it is usually ' |
| 10959 | 'unwise to use\n' |
| 10960 | 'them as dictionary keys.)\n' |
| 10961 | '\n' |
| 10962 | 'Dictionaries can be created by placing a comma-separated ' |
| 10963 | 'list of "key:\n' |
| 10964 | 'value" pairs within braces, for example: "{\'jack\': 4098, ' |
| 10965 | "'sjoerd':\n" |
| 10966 | '4127}" or "{4098: \'jack\', 4127: \'sjoerd\'}", or by the ' |
| 10967 | '"dict"\n' |
| 10968 | 'constructor.\n' |
| 10969 | '\n' |
| 10970 | 'class class dict(**kwarg)\n' |
| 10971 | 'class class dict(mapping, **kwarg)\n' |
| 10972 | 'class class dict(iterable, **kwarg)\n' |
| 10973 | '\n' |
| 10974 | ' Return a new dictionary initialized from an optional ' |
| 10975 | 'positional\n' |
| 10976 | ' argument and a possibly empty set of keyword ' |
| 10977 | 'arguments.\n' |
| 10978 | '\n' |
| 10979 | ' If no positional argument is given, an empty dictionary ' |
| 10980 | 'is created.\n' |
| 10981 | ' If a positional argument is given and it is a mapping ' |
| 10982 | 'object, a\n' |
| 10983 | ' dictionary is created with the same key-value pairs as ' |
| 10984 | 'the mapping\n' |
| 10985 | ' object. Otherwise, the positional argument must be an ' |
| 10986 | '*iterable*\n' |
| 10987 | ' object. Each item in the iterable must itself be an ' |
| 10988 | 'iterable with\n' |
| 10989 | ' exactly two objects. The first object of each item ' |
| 10990 | 'becomes a key\n' |
| 10991 | ' in the new dictionary, and the second object the ' |
| 10992 | 'corresponding\n' |
| 10993 | ' value. If a key occurs more than once, the last value ' |
| 10994 | 'for that key\n' |
| 10995 | ' becomes the corresponding value in the new dictionary.\n' |
| 10996 | '\n' |
| 10997 | ' If keyword arguments are given, the keyword arguments ' |
| 10998 | 'and their\n' |
| 10999 | ' values are added to the dictionary created from the ' |
| 11000 | 'positional\n' |
| 11001 | ' argument. If a key being added is already present, the ' |
| 11002 | 'value from\n' |
| 11003 | ' the keyword argument replaces the value from the ' |
| 11004 | 'positional\n' |
| 11005 | ' argument.\n' |
| 11006 | '\n' |
| 11007 | ' To illustrate, the following examples all return a ' |
| 11008 | 'dictionary equal\n' |
| 11009 | ' to "{"one": 1, "two": 2, "three": 3}":\n' |
| 11010 | '\n' |
| 11011 | ' >>> a = dict(one=1, two=2, three=3)\n' |
| 11012 | " >>> b = {'one': 1, 'two': 2, 'three': 3}\n" |
| 11013 | " >>> c = dict(zip(['one', 'two', 'three'], [1, 2, " |
| 11014 | '3]))\n' |
| 11015 | " >>> d = dict([('two', 2), ('one', 1), ('three', " |
| 11016 | '3)])\n' |
| 11017 | " >>> e = dict({'three': 3, 'one': 1, 'two': 2})\n" |
| 11018 | ' >>> a == b == c == d == e\n' |
| 11019 | ' True\n' |
| 11020 | '\n' |
| 11021 | ' Providing keyword arguments as in the first example ' |
| 11022 | 'only works for\n' |
| 11023 | ' keys that are valid Python identifiers. Otherwise, any ' |
| 11024 | 'valid keys\n' |
| 11025 | ' can be used.\n' |
| 11026 | '\n' |
| 11027 | ' New in version 2.2.\n' |
| 11028 | '\n' |
| 11029 | ' Changed in version 2.3: Support for building a ' |
| 11030 | 'dictionary from\n' |
| 11031 | ' keyword arguments added.\n' |
| 11032 | '\n' |
| 11033 | ' These are the operations that dictionaries support (and ' |
| 11034 | 'therefore,\n' |
| 11035 | ' custom mapping types should support too):\n' |
| 11036 | '\n' |
| 11037 | ' len(d)\n' |
| 11038 | '\n' |
| 11039 | ' Return the number of items in the dictionary *d*.\n' |
| 11040 | '\n' |
| 11041 | ' d[key]\n' |
| 11042 | '\n' |
| 11043 | ' Return the item of *d* with key *key*. Raises a ' |
| 11044 | '"KeyError" if\n' |
| 11045 | ' *key* is not in the map.\n' |
| 11046 | '\n' |
| 11047 | ' If a subclass of dict defines a method ' |
| 11048 | '"__missing__()" and *key*\n' |
| 11049 | ' is not present, the "d[key]" operation calls that ' |
| 11050 | 'method with\n' |
| 11051 | ' the key *key* as argument. The "d[key]" operation ' |
| 11052 | 'then returns\n' |
| 11053 | ' or raises whatever is returned or raised by the\n' |
| 11054 | ' "__missing__(key)" call. No other operations or ' |
| 11055 | 'methods invoke\n' |
| 11056 | ' "__missing__()". If "__missing__()" is not defined, ' |
| 11057 | '"KeyError"\n' |
| 11058 | ' is raised. "__missing__()" must be a method; it ' |
| 11059 | 'cannot be an\n' |
| 11060 | ' instance variable:\n' |
| 11061 | '\n' |
| 11062 | ' >>> class Counter(dict):\n' |
| 11063 | ' ... def __missing__(self, key):\n' |
| 11064 | ' ... return 0\n' |
| 11065 | ' >>> c = Counter()\n' |
| 11066 | " >>> c['red']\n" |
| 11067 | ' 0\n' |
| 11068 | " >>> c['red'] += 1\n" |
| 11069 | " >>> c['red']\n" |
| 11070 | ' 1\n' |
| 11071 | '\n' |
| 11072 | ' The example above shows part of the implementation ' |
| 11073 | 'of\n' |
| 11074 | ' "collections.Counter". A different "__missing__" ' |
| 11075 | 'method is used\n' |
| 11076 | ' by "collections.defaultdict".\n' |
| 11077 | '\n' |
| 11078 | ' New in version 2.5: Recognition of __missing__ ' |
| 11079 | 'methods of dict\n' |
| 11080 | ' subclasses.\n' |
| 11081 | '\n' |
| 11082 | ' d[key] = value\n' |
| 11083 | '\n' |
| 11084 | ' Set "d[key]" to *value*.\n' |
| 11085 | '\n' |
| 11086 | ' del d[key]\n' |
| 11087 | '\n' |
| 11088 | ' Remove "d[key]" from *d*. Raises a "KeyError" if ' |
| 11089 | '*key* is not\n' |
| 11090 | ' in the map.\n' |
| 11091 | '\n' |
| 11092 | ' key in d\n' |
| 11093 | '\n' |
| 11094 | ' Return "True" if *d* has a key *key*, else "False".\n' |
| 11095 | '\n' |
| 11096 | ' New in version 2.2.\n' |
| 11097 | '\n' |
| 11098 | ' key not in d\n' |
| 11099 | '\n' |
| 11100 | ' Equivalent to "not key in d".\n' |
| 11101 | '\n' |
| 11102 | ' New in version 2.2.\n' |
| 11103 | '\n' |
| 11104 | ' iter(d)\n' |
| 11105 | '\n' |
| 11106 | ' Return an iterator over the keys of the dictionary. ' |
| 11107 | 'This is a\n' |
| 11108 | ' shortcut for "iterkeys()".\n' |
| 11109 | '\n' |
| 11110 | ' clear()\n' |
| 11111 | '\n' |
| 11112 | ' Remove all items from the dictionary.\n' |
| 11113 | '\n' |
| 11114 | ' copy()\n' |
| 11115 | '\n' |
| 11116 | ' Return a shallow copy of the dictionary.\n' |
| 11117 | '\n' |
| 11118 | ' fromkeys(seq[, value])\n' |
| 11119 | '\n' |
| 11120 | ' Create a new dictionary with keys from *seq* and ' |
| 11121 | 'values set to\n' |
| 11122 | ' *value*.\n' |
| 11123 | '\n' |
| 11124 | ' "fromkeys()" is a class method that returns a new ' |
| 11125 | 'dictionary.\n' |
| 11126 | ' *value* defaults to "None".\n' |
| 11127 | '\n' |
| 11128 | ' New in version 2.3.\n' |
| 11129 | '\n' |
| 11130 | ' get(key[, default])\n' |
| 11131 | '\n' |
| 11132 | ' Return the value for *key* if *key* is in the ' |
| 11133 | 'dictionary, else\n' |
| 11134 | ' *default*. If *default* is not given, it defaults to ' |
| 11135 | '"None", so\n' |
| 11136 | ' that this method never raises a "KeyError".\n' |
| 11137 | '\n' |
| 11138 | ' has_key(key)\n' |
| 11139 | '\n' |
| 11140 | ' Test for the presence of *key* in the dictionary. ' |
| 11141 | '"has_key()"\n' |
| 11142 | ' is deprecated in favor of "key in d".\n' |
| 11143 | '\n' |
| 11144 | ' items()\n' |
| 11145 | '\n' |
| 11146 | ' Return a copy of the dictionary\'s list of "(key, ' |
| 11147 | 'value)" pairs.\n' |
| 11148 | '\n' |
| 11149 | ' **CPython implementation detail:** Keys and values ' |
| 11150 | 'are listed in\n' |
| 11151 | ' an arbitrary order which is non-random, varies ' |
| 11152 | 'across Python\n' |
| 11153 | " implementations, and depends on the dictionary's " |
| 11154 | 'history of\n' |
| 11155 | ' insertions and deletions.\n' |
| 11156 | '\n' |
| 11157 | ' If "items()", "keys()", "values()", "iteritems()", ' |
| 11158 | '"iterkeys()",\n' |
| 11159 | ' and "itervalues()" are called with no intervening ' |
| 11160 | 'modifications\n' |
| 11161 | ' to the dictionary, the lists will directly ' |
| 11162 | 'correspond. This\n' |
| 11163 | ' allows the creation of "(value, key)" pairs using ' |
| 11164 | '"zip()":\n' |
| 11165 | ' "pairs = zip(d.values(), d.keys())". The same ' |
| 11166 | 'relationship\n' |
| 11167 | ' holds for the "iterkeys()" and "itervalues()" ' |
| 11168 | 'methods: "pairs =\n' |
| 11169 | ' zip(d.itervalues(), d.iterkeys())" provides the same ' |
| 11170 | 'value for\n' |
| 11171 | ' "pairs". Another way to create the same list is ' |
| 11172 | '"pairs = [(v, k)\n' |
| 11173 | ' for (k, v) in d.iteritems()]".\n' |
| 11174 | '\n' |
| 11175 | ' iteritems()\n' |
| 11176 | '\n' |
| 11177 | ' Return an iterator over the dictionary\'s "(key, ' |
| 11178 | 'value)" pairs.\n' |
| 11179 | ' See the note for "dict.items()".\n' |
| 11180 | '\n' |
| 11181 | ' Using "iteritems()" while adding or deleting entries ' |
| 11182 | 'in the\n' |
| 11183 | ' dictionary may raise a "RuntimeError" or fail to ' |
| 11184 | 'iterate over\n' |
| 11185 | ' all entries.\n' |
| 11186 | '\n' |
| 11187 | ' New in version 2.2.\n' |
| 11188 | '\n' |
| 11189 | ' iterkeys()\n' |
| 11190 | '\n' |
| 11191 | " Return an iterator over the dictionary's keys. See " |
| 11192 | 'the note for\n' |
| 11193 | ' "dict.items()".\n' |
| 11194 | '\n' |
| 11195 | ' Using "iterkeys()" while adding or deleting entries ' |
| 11196 | 'in the\n' |
| 11197 | ' dictionary may raise a "RuntimeError" or fail to ' |
| 11198 | 'iterate over\n' |
| 11199 | ' all entries.\n' |
| 11200 | '\n' |
| 11201 | ' New in version 2.2.\n' |
| 11202 | '\n' |
| 11203 | ' itervalues()\n' |
| 11204 | '\n' |
| 11205 | " Return an iterator over the dictionary's values. " |
| 11206 | 'See the note\n' |
| 11207 | ' for "dict.items()".\n' |
| 11208 | '\n' |
| 11209 | ' Using "itervalues()" while adding or deleting ' |
| 11210 | 'entries in the\n' |
| 11211 | ' dictionary may raise a "RuntimeError" or fail to ' |
| 11212 | 'iterate over\n' |
| 11213 | ' all entries.\n' |
| 11214 | '\n' |
| 11215 | ' New in version 2.2.\n' |
| 11216 | '\n' |
| 11217 | ' keys()\n' |
| 11218 | '\n' |
| 11219 | " Return a copy of the dictionary's list of keys. See " |
| 11220 | 'the note\n' |
| 11221 | ' for "dict.items()".\n' |
| 11222 | '\n' |
| 11223 | ' pop(key[, default])\n' |
| 11224 | '\n' |
| 11225 | ' If *key* is in the dictionary, remove it and return ' |
| 11226 | 'its value,\n' |
| 11227 | ' else return *default*. If *default* is not given ' |
| 11228 | 'and *key* is\n' |
| 11229 | ' not in the dictionary, a "KeyError" is raised.\n' |
| 11230 | '\n' |
| 11231 | ' New in version 2.3.\n' |
| 11232 | '\n' |
| 11233 | ' popitem()\n' |
| 11234 | '\n' |
| 11235 | ' Remove and return an arbitrary "(key, value)" pair ' |
| 11236 | 'from the\n' |
| 11237 | ' dictionary.\n' |
| 11238 | '\n' |
| 11239 | ' "popitem()" is useful to destructively iterate over ' |
| 11240 | 'a\n' |
| 11241 | ' dictionary, as often used in set algorithms. If the ' |
| 11242 | 'dictionary\n' |
| 11243 | ' is empty, calling "popitem()" raises a "KeyError".\n' |
| 11244 | '\n' |
| 11245 | ' setdefault(key[, default])\n' |
| 11246 | '\n' |
| 11247 | ' If *key* is in the dictionary, return its value. If ' |
| 11248 | 'not, insert\n' |
| 11249 | ' *key* with a value of *default* and return ' |
| 11250 | '*default*. *default*\n' |
| 11251 | ' defaults to "None".\n' |
| 11252 | '\n' |
| 11253 | ' update([other])\n' |
| 11254 | '\n' |
| 11255 | ' Update the dictionary with the key/value pairs from ' |
| 11256 | '*other*,\n' |
| 11257 | ' overwriting existing keys. Return "None".\n' |
| 11258 | '\n' |
| 11259 | ' "update()" accepts either another dictionary object ' |
| 11260 | 'or an\n' |
| 11261 | ' iterable of key/value pairs (as tuples or other ' |
| 11262 | 'iterables of\n' |
| 11263 | ' length two). If keyword arguments are specified, ' |
| 11264 | 'the dictionary\n' |
| 11265 | ' is then updated with those key/value pairs: ' |
| 11266 | '"d.update(red=1,\n' |
| 11267 | ' blue=2)".\n' |
| 11268 | '\n' |
| 11269 | ' Changed in version 2.4: Allowed the argument to be ' |
| 11270 | 'an iterable\n' |
| 11271 | ' of key/value pairs and allowed keyword arguments.\n' |
| 11272 | '\n' |
| 11273 | ' values()\n' |
| 11274 | '\n' |
| 11275 | " Return a copy of the dictionary's list of values. " |
| 11276 | 'See the note\n' |
| 11277 | ' for "dict.items()".\n' |
| 11278 | '\n' |
| 11279 | ' viewitems()\n' |
| 11280 | '\n' |
| 11281 | ' Return a new view of the dictionary\'s items ("(key, ' |
| 11282 | 'value)"\n' |
| 11283 | ' pairs). See below for documentation of view ' |
| 11284 | 'objects.\n' |
| 11285 | '\n' |
| 11286 | ' New in version 2.7.\n' |
| 11287 | '\n' |
| 11288 | ' viewkeys()\n' |
| 11289 | '\n' |
| 11290 | " Return a new view of the dictionary's keys. See " |
| 11291 | 'below for\n' |
| 11292 | ' documentation of view objects.\n' |
| 11293 | '\n' |
| 11294 | ' New in version 2.7.\n' |
| 11295 | '\n' |
| 11296 | ' viewvalues()\n' |
| 11297 | '\n' |
| 11298 | " Return a new view of the dictionary's values. See " |
| 11299 | 'below for\n' |
| 11300 | ' documentation of view objects.\n' |
| 11301 | '\n' |
| 11302 | ' New in version 2.7.\n' |
| 11303 | '\n' |
| 11304 | ' Dictionaries compare equal if and only if they have the ' |
| 11305 | 'same "(key,\n' |
| 11306 | ' value)" pairs.\n' |
| 11307 | '\n' |
| 11308 | '\n' |
| 11309 | 'Dictionary view objects\n' |
| 11310 | '=======================\n' |
| 11311 | '\n' |
| 11312 | 'The objects returned by "dict.viewkeys()", ' |
| 11313 | '"dict.viewvalues()" and\n' |
| 11314 | '"dict.viewitems()" are *view objects*. They provide a ' |
| 11315 | 'dynamic view on\n' |
| 11316 | "the dictionary's entries, which means that when the " |
| 11317 | 'dictionary\n' |
| 11318 | 'changes, the view reflects these changes.\n' |
| 11319 | '\n' |
| 11320 | 'Dictionary views can be iterated over to yield their ' |
| 11321 | 'respective data,\n' |
| 11322 | 'and support membership tests:\n' |
| 11323 | '\n' |
| 11324 | 'len(dictview)\n' |
| 11325 | '\n' |
| 11326 | ' Return the number of entries in the dictionary.\n' |
| 11327 | '\n' |
| 11328 | 'iter(dictview)\n' |
| 11329 | '\n' |
| 11330 | ' Return an iterator over the keys, values or items ' |
| 11331 | '(represented as\n' |
| 11332 | ' tuples of "(key, value)") in the dictionary.\n' |
| 11333 | '\n' |
| 11334 | ' Keys and values are iterated over in an arbitrary order ' |
| 11335 | 'which is\n' |
| 11336 | ' non-random, varies across Python implementations, and ' |
| 11337 | 'depends on\n' |
| 11338 | " the dictionary's history of insertions and deletions. " |
| 11339 | 'If keys,\n' |
| 11340 | ' values and items views are iterated over with no ' |
| 11341 | 'intervening\n' |
| 11342 | ' modifications to the dictionary, the order of items ' |
| 11343 | 'will directly\n' |
| 11344 | ' correspond. This allows the creation of "(value, key)" ' |
| 11345 | 'pairs using\n' |
| 11346 | ' "zip()": "pairs = zip(d.values(), d.keys())". Another ' |
| 11347 | 'way to\n' |
| 11348 | ' create the same list is "pairs = [(v, k) for (k, v) in ' |
| 11349 | 'd.items()]".\n' |
| 11350 | '\n' |
| 11351 | ' Iterating views while adding or deleting entries in the ' |
| 11352 | 'dictionary\n' |
| 11353 | ' may raise a "RuntimeError" or fail to iterate over all ' |
| 11354 | 'entries.\n' |
| 11355 | '\n' |
| 11356 | 'x in dictview\n' |
| 11357 | '\n' |
| 11358 | ' Return "True" if *x* is in the underlying dictionary\'s ' |
| 11359 | 'keys, values\n' |
| 11360 | ' or items (in the latter case, *x* should be a "(key, ' |
| 11361 | 'value)"\n' |
| 11362 | ' tuple).\n' |
| 11363 | '\n' |
| 11364 | 'Keys views are set-like since their entries are unique and ' |
| 11365 | 'hashable.\n' |
| 11366 | 'If all values are hashable, so that (key, value) pairs are ' |
| 11367 | 'unique and\n' |
| 11368 | 'hashable, then the items view is also set-like. (Values ' |
| 11369 | 'views are not\n' |
| 11370 | 'treated as set-like since the entries are generally not ' |
| 11371 | 'unique.) Then\n' |
| 11372 | 'these set operations are available ("other" refers either ' |
| 11373 | 'to another\n' |
| 11374 | 'view or a set):\n' |
| 11375 | '\n' |
| 11376 | 'dictview & other\n' |
| 11377 | '\n' |
| 11378 | ' Return the intersection of the dictview and the other ' |
| 11379 | 'object as a\n' |
| 11380 | ' new set.\n' |
| 11381 | '\n' |
| 11382 | 'dictview | other\n' |
| 11383 | '\n' |
| 11384 | ' Return the union of the dictview and the other object ' |
| 11385 | 'as a new set.\n' |
| 11386 | '\n' |
| 11387 | 'dictview - other\n' |
| 11388 | '\n' |
| 11389 | ' Return the difference between the dictview and the ' |
| 11390 | 'other object\n' |
| 11391 | " (all elements in *dictview* that aren't in *other*) as " |
| 11392 | 'a new set.\n' |
| 11393 | '\n' |
| 11394 | 'dictview ^ other\n' |
| 11395 | '\n' |
| 11396 | ' Return the symmetric difference (all elements either in ' |
| 11397 | '*dictview*\n' |
| 11398 | ' or *other*, but not in both) of the dictview and the ' |
| 11399 | 'other object\n' |
| 11400 | ' as a new set.\n' |
| 11401 | '\n' |
| 11402 | 'An example of dictionary view usage:\n' |
| 11403 | '\n' |
| 11404 | " >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, " |
| 11405 | "'spam': 500}\n" |
| 11406 | ' >>> keys = dishes.viewkeys()\n' |
| 11407 | ' >>> values = dishes.viewvalues()\n' |
| 11408 | '\n' |
| 11409 | ' >>> # iteration\n' |
| 11410 | ' >>> n = 0\n' |
| 11411 | ' >>> for val in values:\n' |
| 11412 | ' ... n += val\n' |
| 11413 | ' >>> print(n)\n' |
| 11414 | ' 504\n' |
| 11415 | '\n' |
| 11416 | ' >>> # keys and values are iterated over in the same ' |
| 11417 | 'order\n' |
| 11418 | ' >>> list(keys)\n' |
| 11419 | " ['eggs', 'bacon', 'sausage', 'spam']\n" |
| 11420 | ' >>> list(values)\n' |
| 11421 | ' [2, 1, 1, 500]\n' |
| 11422 | '\n' |
| 11423 | ' >>> # view objects are dynamic and reflect dict ' |
| 11424 | 'changes\n' |
| 11425 | " >>> del dishes['eggs']\n" |
| 11426 | " >>> del dishes['sausage']\n" |
| 11427 | ' >>> list(keys)\n' |
| 11428 | " ['spam', 'bacon']\n" |
| 11429 | '\n' |
| 11430 | ' >>> # set operations\n' |
| 11431 | " >>> keys & {'eggs', 'bacon', 'salad'}\n" |
| 11432 | " {'bacon'}\n", |
| 11433 | 'typesmethods': '\n' |
| 11434 | 'Methods\n' |
| 11435 | '*******\n' |
| 11436 | '\n' |
| 11437 | 'Methods are functions that are called using the attribute ' |
| 11438 | 'notation.\n' |
| 11439 | 'There are two flavors: built-in methods (such as ' |
| 11440 | '"append()" on lists)\n' |
| 11441 | 'and class instance methods. Built-in methods are ' |
| 11442 | 'described with the\n' |
| 11443 | 'types that support them.\n' |
| 11444 | '\n' |
| 11445 | 'The implementation adds two special read-only attributes ' |
| 11446 | 'to class\n' |
| 11447 | 'instance methods: "m.im_self" is the object on which the ' |
| 11448 | 'method\n' |
| 11449 | 'operates, and "m.im_func" is the function implementing the ' |
| 11450 | 'method.\n' |
| 11451 | 'Calling "m(arg-1, arg-2, ..., arg-n)" is completely ' |
| 11452 | 'equivalent to\n' |
| 11453 | 'calling "m.im_func(m.im_self, arg-1, arg-2, ..., arg-n)".\n' |
| 11454 | '\n' |
| 11455 | 'Class instance methods are either *bound* or *unbound*, ' |
| 11456 | 'referring to\n' |
| 11457 | 'whether the method was accessed through an instance or a ' |
| 11458 | 'class,\n' |
| 11459 | 'respectively. When a method is unbound, its "im_self" ' |
| 11460 | 'attribute will\n' |
| 11461 | 'be "None" and if called, an explicit "self" object must be ' |
| 11462 | 'passed as\n' |
| 11463 | 'the first argument. In this case, "self" must be an ' |
| 11464 | 'instance of the\n' |
| 11465 | "unbound method's class (or a subclass of that class), " |
| 11466 | 'otherwise a\n' |
| 11467 | '"TypeError" is raised.\n' |
| 11468 | '\n' |
| 11469 | 'Like function objects, methods objects support getting ' |
| 11470 | 'arbitrary\n' |
| 11471 | 'attributes. However, since method attributes are actually ' |
| 11472 | 'stored on\n' |
| 11473 | 'the underlying function object ("meth.im_func"), setting ' |
| 11474 | 'method\n' |
| 11475 | 'attributes on either bound or unbound methods is ' |
| 11476 | 'disallowed.\n' |
| 11477 | 'Attempting to set an attribute on a method results in an\n' |
| 11478 | '"AttributeError" being raised. In order to set a method ' |
| 11479 | 'attribute,\n' |
| 11480 | 'you need to explicitly set it on the underlying function ' |
| 11481 | 'object:\n' |
| 11482 | '\n' |
| 11483 | ' >>> class C:\n' |
| 11484 | ' ... def method(self):\n' |
| 11485 | ' ... pass\n' |
| 11486 | ' ...\n' |
| 11487 | ' >>> c = C()\n' |
| 11488 | " >>> c.method.whoami = 'my name is method' # can't set " |
| 11489 | 'on the method\n' |
| 11490 | ' Traceback (most recent call last):\n' |
| 11491 | ' File "<stdin>", line 1, in <module>\n' |
| 11492 | " AttributeError: 'instancemethod' object has no " |
| 11493 | "attribute 'whoami'\n" |
| 11494 | " >>> c.method.im_func.whoami = 'my name is method'\n" |
| 11495 | ' >>> c.method.whoami\n' |
| 11496 | " 'my name is method'\n" |
| 11497 | '\n' |
| 11498 | 'See The standard type hierarchy for more information.\n', |
| 11499 | 'typesmodules': '\n' |
| 11500 | 'Modules\n' |
| 11501 | '*******\n' |
| 11502 | '\n' |
| 11503 | 'The only special operation on a module is attribute ' |
| 11504 | 'access: "m.name",\n' |
| 11505 | 'where *m* is a module and *name* accesses a name defined ' |
| 11506 | "in *m*'s\n" |
| 11507 | 'symbol table. Module attributes can be assigned to. (Note ' |
| 11508 | 'that the\n' |
| 11509 | '"import" statement is not, strictly speaking, an operation ' |
| 11510 | 'on a module\n' |
| 11511 | 'object; "import foo" does not require a module object ' |
| 11512 | 'named *foo* to\n' |
| 11513 | 'exist, rather it requires an (external) *definition* for a ' |
| 11514 | 'module\n' |
| 11515 | 'named *foo* somewhere.)\n' |
| 11516 | '\n' |
| 11517 | 'A special attribute of every module is "__dict__". This is ' |
| 11518 | 'the\n' |
| 11519 | "dictionary containing the module's symbol table. Modifying " |
| 11520 | 'this\n' |
| 11521 | "dictionary will actually change the module's symbol table, " |
| 11522 | 'but direct\n' |
| 11523 | 'assignment to the "__dict__" attribute is not possible ' |
| 11524 | '(you can write\n' |
| 11525 | '"m.__dict__[\'a\'] = 1", which defines "m.a" to be "1", ' |
| 11526 | "but you can't\n" |
| 11527 | 'write "m.__dict__ = {}"). Modifying "__dict__" directly ' |
| 11528 | 'is not\n' |
| 11529 | 'recommended.\n' |
| 11530 | '\n' |
| 11531 | 'Modules built into the interpreter are written like this: ' |
| 11532 | '"<module\n' |
| 11533 | '\'sys\' (built-in)>". If loaded from a file, they are ' |
| 11534 | 'written as\n' |
| 11535 | '"<module \'os\' from ' |
| 11536 | '\'/usr/local/lib/pythonX.Y/os.pyc\'>".\n', |
| 11537 | 'typesseq': '\n' |
| 11538 | 'Sequence Types --- "str", "unicode", "list", "tuple", ' |
| 11539 | '"bytearray", "buffer", "xrange"\n' |
| 11540 | '*************************************************************************************\n' |
| 11541 | '\n' |
| 11542 | 'There are seven sequence types: strings, Unicode strings, ' |
| 11543 | 'lists,\n' |
| 11544 | 'tuples, bytearrays, buffers, and xrange objects.\n' |
| 11545 | '\n' |
| 11546 | 'For other containers see the built in "dict" and "set" ' |
| 11547 | 'classes, and\n' |
| 11548 | 'the "collections" module.\n' |
| 11549 | '\n' |
| 11550 | 'String literals are written in single or double quotes: ' |
| 11551 | '"\'xyzzy\'",\n' |
| 11552 | '""frobozz"". See String literals for more about string ' |
| 11553 | 'literals.\n' |
| 11554 | 'Unicode strings are much like strings, but are specified in ' |
| 11555 | 'the syntax\n' |
| 11556 | 'using a preceding "\'u\'" character: "u\'abc\'", "u"def"". In ' |
| 11557 | 'addition to\n' |
| 11558 | 'the functionality described here, there are also ' |
| 11559 | 'string-specific\n' |
| 11560 | 'methods described in the String Methods section. Lists are ' |
| 11561 | 'constructed\n' |
| 11562 | 'with square brackets, separating items with commas: "[a, b, ' |
| 11563 | 'c]".\n' |
| 11564 | 'Tuples are constructed by the comma operator (not within ' |
| 11565 | 'square\n' |
| 11566 | 'brackets), with or without enclosing parentheses, but an empty ' |
| 11567 | 'tuple\n' |
| 11568 | 'must have the enclosing parentheses, such as "a, b, c" or ' |
| 11569 | '"()". A\n' |
| 11570 | 'single item tuple must have a trailing comma, such as "(d,)".\n' |
| 11571 | '\n' |
| 11572 | 'Bytearray objects are created with the built-in function\n' |
| 11573 | '"bytearray()".\n' |
| 11574 | '\n' |
| 11575 | 'Buffer objects are not directly supported by Python syntax, ' |
| 11576 | 'but can be\n' |
| 11577 | 'created by calling the built-in function "buffer()". They ' |
| 11578 | "don't\n" |
| 11579 | 'support concatenation or repetition.\n' |
| 11580 | '\n' |
| 11581 | 'Objects of type xrange are similar to buffers in that there is ' |
| 11582 | 'no\n' |
| 11583 | 'specific syntax to create them, but they are created using ' |
| 11584 | 'the\n' |
| 11585 | '"xrange()" function. They don\'t support slicing, ' |
| 11586 | 'concatenation or\n' |
| 11587 | 'repetition, and using "in", "not in", "min()" or "max()" on ' |
| 11588 | 'them is\n' |
| 11589 | 'inefficient.\n' |
| 11590 | '\n' |
| 11591 | 'Most sequence types support the following operations. The ' |
| 11592 | '"in" and\n' |
| 11593 | '"not in" operations have the same priorities as the ' |
| 11594 | 'comparison\n' |
| 11595 | 'operations. The "+" and "*" operations have the same priority ' |
| 11596 | 'as the\n' |
| 11597 | 'corresponding numeric operations. [3] Additional methods are ' |
| 11598 | 'provided\n' |
| 11599 | 'for Mutable Sequence Types.\n' |
| 11600 | '\n' |
| 11601 | 'This table lists the sequence operations sorted in ascending ' |
| 11602 | 'priority.\n' |
| 11603 | 'In the table, *s* and *t* are sequences of the same type; *n*, ' |
| 11604 | '*i* and\n' |
| 11605 | '*j* are integers:\n' |
| 11606 | '\n' |
| 11607 | '+--------------------+----------------------------------+------------+\n' |
| 11608 | '| Operation | Result | ' |
| 11609 | 'Notes |\n' |
| 11610 | '+====================+==================================+============+\n' |
| 11611 | '| "x in s" | "True" if an item of *s* is | ' |
| 11612 | '(1) |\n' |
| 11613 | '| | equal to *x*, else "False" ' |
| 11614 | '| |\n' |
| 11615 | '+--------------------+----------------------------------+------------+\n' |
| 11616 | '| "x not in s" | "False" if an item of *s* is | ' |
| 11617 | '(1) |\n' |
| 11618 | '| | equal to *x*, else "True" ' |
| 11619 | '| |\n' |
| 11620 | '+--------------------+----------------------------------+------------+\n' |
| 11621 | '| "s + t" | the concatenation of *s* and *t* | ' |
| 11622 | '(6) |\n' |
| 11623 | '+--------------------+----------------------------------+------------+\n' |
| 11624 | '| "s * n, n * s" | equivalent to adding *s* to | ' |
| 11625 | '(2) |\n' |
| 11626 | '| | itself *n* times ' |
| 11627 | '| |\n' |
| 11628 | '+--------------------+----------------------------------+------------+\n' |
| 11629 | '| "s[i]" | *i*th item of *s*, origin 0 | ' |
| 11630 | '(3) |\n' |
| 11631 | '+--------------------+----------------------------------+------------+\n' |
| 11632 | '| "s[i:j]" | slice of *s* from *i* to *j* | ' |
| 11633 | '(3)(4) |\n' |
| 11634 | '+--------------------+----------------------------------+------------+\n' |
| 11635 | '| "s[i:j:k]" | slice of *s* from *i* to *j* | ' |
| 11636 | '(3)(5) |\n' |
| 11637 | '| | with step *k* ' |
| 11638 | '| |\n' |
| 11639 | '+--------------------+----------------------------------+------------+\n' |
| 11640 | '| "len(s)" | length of *s* ' |
| 11641 | '| |\n' |
| 11642 | '+--------------------+----------------------------------+------------+\n' |
| 11643 | '| "min(s)" | smallest item of *s* ' |
| 11644 | '| |\n' |
| 11645 | '+--------------------+----------------------------------+------------+\n' |
| 11646 | '| "max(s)" | largest item of *s* ' |
| 11647 | '| |\n' |
| 11648 | '+--------------------+----------------------------------+------------+\n' |
| 11649 | '| "s.index(x)" | index of the first occurrence of ' |
| 11650 | '| |\n' |
| 11651 | '| | *x* in *s* ' |
| 11652 | '| |\n' |
| 11653 | '+--------------------+----------------------------------+------------+\n' |
| 11654 | '| "s.count(x)" | total number of occurrences of ' |
| 11655 | '| |\n' |
| 11656 | '| | *x* in *s* ' |
| 11657 | '| |\n' |
| 11658 | '+--------------------+----------------------------------+------------+\n' |
| 11659 | '\n' |
| 11660 | 'Sequence types also support comparisons. In particular, tuples ' |
| 11661 | 'and\n' |
| 11662 | 'lists are compared lexicographically by comparing ' |
| 11663 | 'corresponding\n' |
| 11664 | 'elements. This means that to compare equal, every element must ' |
| 11665 | 'compare\n' |
| 11666 | 'equal and the two sequences must be of the same type and have ' |
| 11667 | 'the same\n' |
| 11668 | 'length. (For full details see Comparisons in the language ' |
| 11669 | 'reference.)\n' |
| 11670 | '\n' |
| 11671 | 'Notes:\n' |
| 11672 | '\n' |
| 11673 | '1. When *s* is a string or Unicode string object the "in" and ' |
| 11674 | '"not\n' |
| 11675 | ' in" operations act like a substring test. In Python ' |
| 11676 | 'versions\n' |
| 11677 | ' before 2.3, *x* had to be a string of length 1. In Python ' |
| 11678 | '2.3 and\n' |
| 11679 | ' beyond, *x* may be a string of any length.\n' |
| 11680 | '\n' |
| 11681 | '2. Values of *n* less than "0" are treated as "0" (which ' |
| 11682 | 'yields an\n' |
| 11683 | ' empty sequence of the same type as *s*). Note that items ' |
| 11684 | 'in the\n' |
| 11685 | ' sequence *s* are not copied; they are referenced multiple ' |
| 11686 | 'times.\n' |
| 11687 | ' This often haunts new Python programmers; consider:\n' |
| 11688 | '\n' |
| 11689 | ' >>> lists = [[]] * 3\n' |
| 11690 | ' >>> lists\n' |
| 11691 | ' [[], [], []]\n' |
| 11692 | ' >>> lists[0].append(3)\n' |
| 11693 | ' >>> lists\n' |
| 11694 | ' [[3], [3], [3]]\n' |
| 11695 | '\n' |
| 11696 | ' What has happened is that "[[]]" is a one-element list ' |
| 11697 | 'containing\n' |
| 11698 | ' an empty list, so all three elements of "[[]] * 3" are ' |
| 11699 | 'references\n' |
| 11700 | ' to this single empty list. Modifying any of the elements ' |
| 11701 | 'of\n' |
| 11702 | ' "lists" modifies this single list. You can create a list ' |
| 11703 | 'of\n' |
| 11704 | ' different lists this way:\n' |
| 11705 | '\n' |
| 11706 | ' >>> lists = [[] for i in range(3)]\n' |
| 11707 | ' >>> lists[0].append(3)\n' |
| 11708 | ' >>> lists[1].append(5)\n' |
| 11709 | ' >>> lists[2].append(7)\n' |
| 11710 | ' >>> lists\n' |
| 11711 | ' [[3], [5], [7]]\n' |
| 11712 | '\n' |
| 11713 | ' Further explanation is available in the FAQ entry How do I ' |
| 11714 | 'create a\n' |
| 11715 | ' multidimensional list?.\n' |
| 11716 | '\n' |
| 11717 | '3. If *i* or *j* is negative, the index is relative to the end ' |
| 11718 | 'of\n' |
| 11719 | ' the string: "len(s) + i" or "len(s) + j" is substituted. ' |
| 11720 | 'But note\n' |
| 11721 | ' that "-0" is still "0".\n' |
| 11722 | '\n' |
| 11723 | '4. The slice of *s* from *i* to *j* is defined as the sequence ' |
| 11724 | 'of\n' |
| 11725 | ' items with index *k* such that "i <= k < j". If *i* or *j* ' |
| 11726 | 'is\n' |
| 11727 | ' greater than "len(s)", use "len(s)". If *i* is omitted or ' |
| 11728 | '"None",\n' |
| 11729 | ' use "0". If *j* is omitted or "None", use "len(s)". If ' |
| 11730 | '*i* is\n' |
| 11731 | ' greater than or equal to *j*, the slice is empty.\n' |
| 11732 | '\n' |
| 11733 | '5. The slice of *s* from *i* to *j* with step *k* is defined ' |
| 11734 | 'as the\n' |
| 11735 | ' sequence of items with index "x = i + n*k" such that "0 <= ' |
| 11736 | 'n <\n' |
| 11737 | ' (j-i)/k". In other words, the indices are "i", "i+k", ' |
| 11738 | '"i+2*k",\n' |
| 11739 | ' "i+3*k" and so on, stopping when *j* is reached (but never\n' |
| 11740 | ' including *j*). If *i* or *j* is greater than "len(s)", ' |
| 11741 | 'use\n' |
| 11742 | ' "len(s)". If *i* or *j* are omitted or "None", they become ' |
| 11743 | '"end"\n' |
| 11744 | ' values (which end depends on the sign of *k*). Note, *k* ' |
| 11745 | 'cannot be\n' |
| 11746 | ' zero. If *k* is "None", it is treated like "1".\n' |
| 11747 | '\n' |
| 11748 | '6. **CPython implementation detail:** If *s* and *t* are both\n' |
| 11749 | ' strings, some Python implementations such as CPython can ' |
| 11750 | 'usually\n' |
| 11751 | ' perform an in-place optimization for assignments of the ' |
| 11752 | 'form "s = s\n' |
| 11753 | ' + t" or "s += t". When applicable, this optimization ' |
| 11754 | 'makes\n' |
| 11755 | ' quadratic run-time much less likely. This optimization is ' |
| 11756 | 'both\n' |
| 11757 | ' version and implementation dependent. For performance ' |
| 11758 | 'sensitive\n' |
| 11759 | ' code, it is preferable to use the "str.join()" method which ' |
| 11760 | 'assures\n' |
| 11761 | ' consistent linear concatenation performance across versions ' |
| 11762 | 'and\n' |
| 11763 | ' implementations.\n' |
| 11764 | '\n' |
| 11765 | ' Changed in version 2.4: Formerly, string concatenation ' |
| 11766 | 'never\n' |
| 11767 | ' occurred in-place.\n' |
| 11768 | '\n' |
| 11769 | '\n' |
| 11770 | 'String Methods\n' |
| 11771 | '==============\n' |
| 11772 | '\n' |
| 11773 | 'Below are listed the string methods which both 8-bit strings ' |
| 11774 | 'and\n' |
| 11775 | 'Unicode objects support. Some of them are also available on\n' |
| 11776 | '"bytearray" objects.\n' |
| 11777 | '\n' |
| 11778 | "In addition, Python's strings support the sequence type " |
| 11779 | 'methods\n' |
| 11780 | 'described in the Sequence Types --- str, unicode, list, ' |
| 11781 | 'tuple,\n' |
| 11782 | 'bytearray, buffer, xrange section. To output formatted strings ' |
| 11783 | 'use\n' |
| 11784 | 'template strings or the "%" operator described in the String\n' |
| 11785 | 'Formatting Operations section. Also, see the "re" module for ' |
| 11786 | 'string\n' |
| 11787 | 'functions based on regular expressions.\n' |
| 11788 | '\n' |
| 11789 | 'str.capitalize()\n' |
| 11790 | '\n' |
| 11791 | ' Return a copy of the string with its first character ' |
| 11792 | 'capitalized\n' |
| 11793 | ' and the rest lowercased.\n' |
| 11794 | '\n' |
| 11795 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 11796 | '\n' |
| 11797 | 'str.center(width[, fillchar])\n' |
| 11798 | '\n' |
| 11799 | ' Return centered in a string of length *width*. Padding is ' |
| 11800 | 'done\n' |
| 11801 | ' using the specified *fillchar* (default is a space).\n' |
| 11802 | '\n' |
| 11803 | ' Changed in version 2.4: Support for the *fillchar* ' |
| 11804 | 'argument.\n' |
| 11805 | '\n' |
| 11806 | 'str.count(sub[, start[, end]])\n' |
| 11807 | '\n' |
| 11808 | ' Return the number of non-overlapping occurrences of ' |
| 11809 | 'substring *sub*\n' |
| 11810 | ' in the range [*start*, *end*]. Optional arguments *start* ' |
| 11811 | 'and\n' |
| 11812 | ' *end* are interpreted as in slice notation.\n' |
| 11813 | '\n' |
| 11814 | 'str.decode([encoding[, errors]])\n' |
| 11815 | '\n' |
| 11816 | ' Decodes the string using the codec registered for ' |
| 11817 | '*encoding*.\n' |
| 11818 | ' *encoding* defaults to the default string encoding. ' |
| 11819 | '*errors* may\n' |
| 11820 | ' be given to set a different error handling scheme. The ' |
| 11821 | 'default is\n' |
| 11822 | ' "\'strict\'", meaning that encoding errors raise ' |
| 11823 | '"UnicodeError".\n' |
| 11824 | ' Other possible values are "\'ignore\'", "\'replace\'" and ' |
| 11825 | 'any other\n' |
| 11826 | ' name registered via "codecs.register_error()", see section ' |
| 11827 | 'Codec\n' |
| 11828 | ' Base Classes.\n' |
| 11829 | '\n' |
| 11830 | ' New in version 2.2.\n' |
| 11831 | '\n' |
| 11832 | ' Changed in version 2.3: Support for other error handling ' |
| 11833 | 'schemes\n' |
| 11834 | ' added.\n' |
| 11835 | '\n' |
| 11836 | ' Changed in version 2.7: Support for keyword arguments ' |
| 11837 | 'added.\n' |
| 11838 | '\n' |
| 11839 | 'str.encode([encoding[, errors]])\n' |
| 11840 | '\n' |
| 11841 | ' Return an encoded version of the string. Default encoding ' |
| 11842 | 'is the\n' |
| 11843 | ' current default string encoding. *errors* may be given to ' |
| 11844 | 'set a\n' |
| 11845 | ' different error handling scheme. The default for *errors* ' |
| 11846 | 'is\n' |
| 11847 | ' "\'strict\'", meaning that encoding errors raise a ' |
| 11848 | '"UnicodeError".\n' |
| 11849 | ' Other possible values are "\'ignore\'", "\'replace\'",\n' |
| 11850 | ' "\'xmlcharrefreplace\'", "\'backslashreplace\'" and any ' |
| 11851 | 'other name\n' |
| 11852 | ' registered via "codecs.register_error()", see section Codec ' |
| 11853 | 'Base\n' |
| 11854 | ' Classes. For a list of possible encodings, see section ' |
| 11855 | 'Standard\n' |
| 11856 | ' Encodings.\n' |
| 11857 | '\n' |
| 11858 | ' New in version 2.0.\n' |
| 11859 | '\n' |
| 11860 | ' Changed in version 2.3: Support for "\'xmlcharrefreplace\'" ' |
| 11861 | 'and\n' |
| 11862 | ' "\'backslashreplace\'" and other error handling schemes ' |
| 11863 | 'added.\n' |
| 11864 | '\n' |
| 11865 | ' Changed in version 2.7: Support for keyword arguments ' |
| 11866 | 'added.\n' |
| 11867 | '\n' |
| 11868 | 'str.endswith(suffix[, start[, end]])\n' |
| 11869 | '\n' |
| 11870 | ' Return "True" if the string ends with the specified ' |
| 11871 | '*suffix*,\n' |
| 11872 | ' otherwise return "False". *suffix* can also be a tuple of ' |
| 11873 | 'suffixes\n' |
| 11874 | ' to look for. With optional *start*, test beginning at ' |
| 11875 | 'that\n' |
| 11876 | ' position. With optional *end*, stop comparing at that ' |
| 11877 | 'position.\n' |
| 11878 | '\n' |
| 11879 | ' Changed in version 2.5: Accept tuples as *suffix*.\n' |
| 11880 | '\n' |
| 11881 | 'str.expandtabs([tabsize])\n' |
| 11882 | '\n' |
| 11883 | ' Return a copy of the string where all tab characters are ' |
| 11884 | 'replaced\n' |
| 11885 | ' by one or more spaces, depending on the current column and ' |
| 11886 | 'the\n' |
| 11887 | ' given tab size. Tab positions occur every *tabsize* ' |
| 11888 | 'characters\n' |
| 11889 | ' (default is 8, giving tab positions at columns 0, 8, 16 and ' |
| 11890 | 'so on).\n' |
| 11891 | ' To expand the string, the current column is set to zero and ' |
| 11892 | 'the\n' |
| 11893 | ' string is examined character by character. If the ' |
| 11894 | 'character is a\n' |
| 11895 | ' tab ("\\t"), one or more space characters are inserted in ' |
| 11896 | 'the result\n' |
| 11897 | ' until the current column is equal to the next tab position. ' |
| 11898 | '(The\n' |
| 11899 | ' tab character itself is not copied.) If the character is a ' |
| 11900 | 'newline\n' |
| 11901 | ' ("\\n") or return ("\\r"), it is copied and the current ' |
| 11902 | 'column is\n' |
| 11903 | ' reset to zero. Any other character is copied unchanged and ' |
| 11904 | 'the\n' |
| 11905 | ' current column is incremented by one regardless of how the\n' |
| 11906 | ' character is represented when printed.\n' |
| 11907 | '\n' |
| 11908 | " >>> '01\\t012\\t0123\\t01234'.expandtabs()\n" |
| 11909 | " '01 012 0123 01234'\n" |
| 11910 | " >>> '01\\t012\\t0123\\t01234'.expandtabs(4)\n" |
| 11911 | " '01 012 0123 01234'\n" |
| 11912 | '\n' |
| 11913 | 'str.find(sub[, start[, end]])\n' |
| 11914 | '\n' |
| 11915 | ' Return the lowest index in the string where substring *sub* ' |
| 11916 | 'is\n' |
| 11917 | ' found, such that *sub* is contained in the slice ' |
| 11918 | '"s[start:end]".\n' |
| 11919 | ' Optional arguments *start* and *end* are interpreted as in ' |
| 11920 | 'slice\n' |
| 11921 | ' notation. Return "-1" if *sub* is not found.\n' |
| 11922 | '\n' |
| 11923 | ' Note: The "find()" method should be used only if you need ' |
| 11924 | 'to know\n' |
| 11925 | ' the position of *sub*. To check if *sub* is a substring ' |
| 11926 | 'or not,\n' |
| 11927 | ' use the "in" operator:\n' |
| 11928 | '\n' |
| 11929 | " >>> 'Py' in 'Python'\n" |
| 11930 | ' True\n' |
| 11931 | '\n' |
| 11932 | 'str.format(*args, **kwargs)\n' |
| 11933 | '\n' |
| 11934 | ' Perform a string formatting operation. The string on which ' |
| 11935 | 'this\n' |
| 11936 | ' method is called can contain literal text or replacement ' |
| 11937 | 'fields\n' |
| 11938 | ' delimited by braces "{}". Each replacement field contains ' |
| 11939 | 'either\n' |
| 11940 | ' the numeric index of a positional argument, or the name of ' |
| 11941 | 'a\n' |
| 11942 | ' keyword argument. Returns a copy of the string where each\n' |
| 11943 | ' replacement field is replaced with the string value of the\n' |
| 11944 | ' corresponding argument.\n' |
| 11945 | '\n' |
| 11946 | ' >>> "The sum of 1 + 2 is {0}".format(1+2)\n' |
| 11947 | " 'The sum of 1 + 2 is 3'\n" |
| 11948 | '\n' |
| 11949 | ' See Format String Syntax for a description of the various\n' |
| 11950 | ' formatting options that can be specified in format ' |
| 11951 | 'strings.\n' |
| 11952 | '\n' |
| 11953 | ' This method of string formatting is the new standard in ' |
| 11954 | 'Python 3,\n' |
| 11955 | ' and should be preferred to the "%" formatting described in ' |
| 11956 | 'String\n' |
| 11957 | ' Formatting Operations in new code.\n' |
| 11958 | '\n' |
| 11959 | ' New in version 2.6.\n' |
| 11960 | '\n' |
| 11961 | 'str.index(sub[, start[, end]])\n' |
| 11962 | '\n' |
| 11963 | ' Like "find()", but raise "ValueError" when the substring is ' |
| 11964 | 'not\n' |
| 11965 | ' found.\n' |
| 11966 | '\n' |
| 11967 | 'str.isalnum()\n' |
| 11968 | '\n' |
| 11969 | ' Return true if all characters in the string are ' |
| 11970 | 'alphanumeric and\n' |
| 11971 | ' there is at least one character, false otherwise.\n' |
| 11972 | '\n' |
| 11973 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 11974 | '\n' |
| 11975 | 'str.isalpha()\n' |
| 11976 | '\n' |
| 11977 | ' Return true if all characters in the string are alphabetic ' |
| 11978 | 'and\n' |
| 11979 | ' there is at least one character, false otherwise.\n' |
| 11980 | '\n' |
| 11981 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 11982 | '\n' |
| 11983 | 'str.isdigit()\n' |
| 11984 | '\n' |
| 11985 | ' Return true if all characters in the string are digits and ' |
| 11986 | 'there is\n' |
| 11987 | ' at least one character, false otherwise.\n' |
| 11988 | '\n' |
| 11989 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 11990 | '\n' |
| 11991 | 'str.islower()\n' |
| 11992 | '\n' |
| 11993 | ' Return true if all cased characters [4] in the string are ' |
| 11994 | 'lowercase\n' |
| 11995 | ' and there is at least one cased character, false ' |
| 11996 | 'otherwise.\n' |
| 11997 | '\n' |
| 11998 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 11999 | '\n' |
| 12000 | 'str.isspace()\n' |
| 12001 | '\n' |
| 12002 | ' Return true if there are only whitespace characters in the ' |
| 12003 | 'string\n' |
| 12004 | ' and there is at least one character, false otherwise.\n' |
| 12005 | '\n' |
| 12006 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12007 | '\n' |
| 12008 | 'str.istitle()\n' |
| 12009 | '\n' |
| 12010 | ' Return true if the string is a titlecased string and there ' |
| 12011 | 'is at\n' |
| 12012 | ' least one character, for example uppercase characters may ' |
| 12013 | 'only\n' |
| 12014 | ' follow uncased characters and lowercase characters only ' |
| 12015 | 'cased ones.\n' |
| 12016 | ' Return false otherwise.\n' |
| 12017 | '\n' |
| 12018 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12019 | '\n' |
| 12020 | 'str.isupper()\n' |
| 12021 | '\n' |
| 12022 | ' Return true if all cased characters [4] in the string are ' |
| 12023 | 'uppercase\n' |
| 12024 | ' and there is at least one cased character, false ' |
| 12025 | 'otherwise.\n' |
| 12026 | '\n' |
| 12027 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12028 | '\n' |
| 12029 | 'str.join(iterable)\n' |
| 12030 | '\n' |
| 12031 | ' Return a string which is the concatenation of the strings ' |
| 12032 | 'in the\n' |
| 12033 | ' *iterable* *iterable*. The separator between elements is ' |
| 12034 | 'the\n' |
| 12035 | ' string providing this method.\n' |
| 12036 | '\n' |
| 12037 | 'str.ljust(width[, fillchar])\n' |
| 12038 | '\n' |
| 12039 | ' Return the string left justified in a string of length ' |
| 12040 | '*width*.\n' |
| 12041 | ' Padding is done using the specified *fillchar* (default is ' |
| 12042 | 'a\n' |
| 12043 | ' space). The original string is returned if *width* is less ' |
| 12044 | 'than or\n' |
| 12045 | ' equal to "len(s)".\n' |
| 12046 | '\n' |
| 12047 | ' Changed in version 2.4: Support for the *fillchar* ' |
| 12048 | 'argument.\n' |
| 12049 | '\n' |
| 12050 | 'str.lower()\n' |
| 12051 | '\n' |
| 12052 | ' Return a copy of the string with all the cased characters ' |
| 12053 | '[4]\n' |
| 12054 | ' converted to lowercase.\n' |
| 12055 | '\n' |
| 12056 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12057 | '\n' |
| 12058 | 'str.lstrip([chars])\n' |
| 12059 | '\n' |
| 12060 | ' Return a copy of the string with leading characters ' |
| 12061 | 'removed. The\n' |
| 12062 | ' *chars* argument is a string specifying the set of ' |
| 12063 | 'characters to be\n' |
| 12064 | ' removed. If omitted or "None", the *chars* argument ' |
| 12065 | 'defaults to\n' |
| 12066 | ' removing whitespace. The *chars* argument is not a prefix; ' |
| 12067 | 'rather,\n' |
| 12068 | ' all combinations of its values are stripped:\n' |
| 12069 | '\n' |
| 12070 | " >>> ' spacious '.lstrip()\n" |
| 12071 | " 'spacious '\n" |
| 12072 | " >>> 'www.example.com'.lstrip('cmowz.')\n" |
| 12073 | " 'example.com'\n" |
| 12074 | '\n' |
| 12075 | ' Changed in version 2.2.2: Support for the *chars* ' |
| 12076 | 'argument.\n' |
| 12077 | '\n' |
| 12078 | 'str.partition(sep)\n' |
| 12079 | '\n' |
| 12080 | ' Split the string at the first occurrence of *sep*, and ' |
| 12081 | 'return a\n' |
| 12082 | ' 3-tuple containing the part before the separator, the ' |
| 12083 | 'separator\n' |
| 12084 | ' itself, and the part after the separator. If the separator ' |
| 12085 | 'is not\n' |
| 12086 | ' found, return a 3-tuple containing the string itself, ' |
| 12087 | 'followed by\n' |
| 12088 | ' two empty strings.\n' |
| 12089 | '\n' |
| 12090 | ' New in version 2.5.\n' |
| 12091 | '\n' |
| 12092 | 'str.replace(old, new[, count])\n' |
| 12093 | '\n' |
| 12094 | ' Return a copy of the string with all occurrences of ' |
| 12095 | 'substring *old*\n' |
| 12096 | ' replaced by *new*. If the optional argument *count* is ' |
| 12097 | 'given, only\n' |
| 12098 | ' the first *count* occurrences are replaced.\n' |
| 12099 | '\n' |
| 12100 | 'str.rfind(sub[, start[, end]])\n' |
| 12101 | '\n' |
| 12102 | ' Return the highest index in the string where substring ' |
| 12103 | '*sub* is\n' |
| 12104 | ' found, such that *sub* is contained within "s[start:end]".\n' |
| 12105 | ' Optional arguments *start* and *end* are interpreted as in ' |
| 12106 | 'slice\n' |
| 12107 | ' notation. Return "-1" on failure.\n' |
| 12108 | '\n' |
| 12109 | 'str.rindex(sub[, start[, end]])\n' |
| 12110 | '\n' |
| 12111 | ' Like "rfind()" but raises "ValueError" when the substring ' |
| 12112 | '*sub* is\n' |
| 12113 | ' not found.\n' |
| 12114 | '\n' |
| 12115 | 'str.rjust(width[, fillchar])\n' |
| 12116 | '\n' |
| 12117 | ' Return the string right justified in a string of length ' |
| 12118 | '*width*.\n' |
| 12119 | ' Padding is done using the specified *fillchar* (default is ' |
| 12120 | 'a\n' |
| 12121 | ' space). The original string is returned if *width* is less ' |
| 12122 | 'than or\n' |
| 12123 | ' equal to "len(s)".\n' |
| 12124 | '\n' |
| 12125 | ' Changed in version 2.4: Support for the *fillchar* ' |
| 12126 | 'argument.\n' |
| 12127 | '\n' |
| 12128 | 'str.rpartition(sep)\n' |
| 12129 | '\n' |
| 12130 | ' Split the string at the last occurrence of *sep*, and ' |
| 12131 | 'return a\n' |
| 12132 | ' 3-tuple containing the part before the separator, the ' |
| 12133 | 'separator\n' |
| 12134 | ' itself, and the part after the separator. If the separator ' |
| 12135 | 'is not\n' |
| 12136 | ' found, return a 3-tuple containing two empty strings, ' |
| 12137 | 'followed by\n' |
| 12138 | ' the string itself.\n' |
| 12139 | '\n' |
| 12140 | ' New in version 2.5.\n' |
| 12141 | '\n' |
| 12142 | 'str.rsplit([sep[, maxsplit]])\n' |
| 12143 | '\n' |
| 12144 | ' Return a list of the words in the string, using *sep* as ' |
| 12145 | 'the\n' |
| 12146 | ' delimiter string. If *maxsplit* is given, at most ' |
| 12147 | '*maxsplit* splits\n' |
| 12148 | ' are done, the *rightmost* ones. If *sep* is not specified ' |
| 12149 | 'or\n' |
| 12150 | ' "None", any whitespace string is a separator. Except for ' |
| 12151 | 'splitting\n' |
| 12152 | ' from the right, "rsplit()" behaves like "split()" which is\n' |
| 12153 | ' described in detail below.\n' |
| 12154 | '\n' |
| 12155 | ' New in version 2.4.\n' |
| 12156 | '\n' |
| 12157 | 'str.rstrip([chars])\n' |
| 12158 | '\n' |
| 12159 | ' Return a copy of the string with trailing characters ' |
| 12160 | 'removed. The\n' |
| 12161 | ' *chars* argument is a string specifying the set of ' |
| 12162 | 'characters to be\n' |
| 12163 | ' removed. If omitted or "None", the *chars* argument ' |
| 12164 | 'defaults to\n' |
| 12165 | ' removing whitespace. The *chars* argument is not a suffix; ' |
| 12166 | 'rather,\n' |
| 12167 | ' all combinations of its values are stripped:\n' |
| 12168 | '\n' |
| 12169 | " >>> ' spacious '.rstrip()\n" |
| 12170 | " ' spacious'\n" |
| 12171 | " >>> 'mississippi'.rstrip('ipz')\n" |
| 12172 | " 'mississ'\n" |
| 12173 | '\n' |
| 12174 | ' Changed in version 2.2.2: Support for the *chars* ' |
| 12175 | 'argument.\n' |
| 12176 | '\n' |
| 12177 | 'str.split([sep[, maxsplit]])\n' |
| 12178 | '\n' |
| 12179 | ' Return a list of the words in the string, using *sep* as ' |
| 12180 | 'the\n' |
| 12181 | ' delimiter string. If *maxsplit* is given, at most ' |
| 12182 | '*maxsplit*\n' |
| 12183 | ' splits are done (thus, the list will have at most ' |
| 12184 | '"maxsplit+1"\n' |
| 12185 | ' elements). If *maxsplit* is not specified or "-1", then ' |
| 12186 | 'there is\n' |
| 12187 | ' no limit on the number of splits (all possible splits are ' |
| 12188 | 'made).\n' |
| 12189 | '\n' |
| 12190 | ' If *sep* is given, consecutive delimiters are not grouped ' |
| 12191 | 'together\n' |
| 12192 | ' and are deemed to delimit empty strings (for example,\n' |
| 12193 | ' "\'1,,2\'.split(\',\')" returns "[\'1\', \'\', \'2\']"). ' |
| 12194 | 'The *sep* argument\n' |
| 12195 | ' may consist of multiple characters (for example,\n' |
| 12196 | ' "\'1<>2<>3\'.split(\'<>\')" returns "[\'1\', \'2\', ' |
| 12197 | '\'3\']"). Splitting an\n' |
| 12198 | ' empty string with a specified separator returns "[\'\']".\n' |
| 12199 | '\n' |
| 12200 | ' If *sep* is not specified or is "None", a different ' |
| 12201 | 'splitting\n' |
| 12202 | ' algorithm is applied: runs of consecutive whitespace are ' |
| 12203 | 'regarded\n' |
| 12204 | ' as a single separator, and the result will contain no empty ' |
| 12205 | 'strings\n' |
| 12206 | ' at the start or end if the string has leading or trailing\n' |
| 12207 | ' whitespace. Consequently, splitting an empty string or a ' |
| 12208 | 'string\n' |
| 12209 | ' consisting of just whitespace with a "None" separator ' |
| 12210 | 'returns "[]".\n' |
| 12211 | '\n' |
| 12212 | ' For example, "\' 1 2 3 \'.split()" returns "[\'1\', ' |
| 12213 | '\'2\', \'3\']", and\n' |
| 12214 | ' "\' 1 2 3 \'.split(None, 1)" returns "[\'1\', \'2 3 ' |
| 12215 | '\']".\n' |
| 12216 | '\n' |
| 12217 | 'str.splitlines([keepends])\n' |
| 12218 | '\n' |
| 12219 | ' Return a list of the lines in the string, breaking at line\n' |
| 12220 | ' boundaries. This method uses the *universal newlines* ' |
| 12221 | 'approach to\n' |
| 12222 | ' splitting lines. Line breaks are not included in the ' |
| 12223 | 'resulting list\n' |
| 12224 | ' unless *keepends* is given and true.\n' |
| 12225 | '\n' |
| 12226 | ' For example, "\'ab c\\n\\nde fg\\rkl\\r\\n\'.splitlines()" ' |
| 12227 | 'returns "[\'ab\n' |
| 12228 | ' c\', \'\', \'de fg\', \'kl\']", while the same call with\n' |
| 12229 | ' "splitlines(True)" returns "[\'ab c\\n\', \'\\n\', \'de ' |
| 12230 | 'fg\\r\', \'kl\\r\\n\']".\n' |
| 12231 | '\n' |
| 12232 | ' Unlike "split()" when a delimiter string *sep* is given, ' |
| 12233 | 'this\n' |
| 12234 | ' method returns an empty list for the empty string, and a ' |
| 12235 | 'terminal\n' |
| 12236 | ' line break does not result in an extra line.\n' |
| 12237 | '\n' |
| 12238 | 'str.startswith(prefix[, start[, end]])\n' |
| 12239 | '\n' |
| 12240 | ' Return "True" if string starts with the *prefix*, otherwise ' |
| 12241 | 'return\n' |
| 12242 | ' "False". *prefix* can also be a tuple of prefixes to look ' |
| 12243 | 'for.\n' |
| 12244 | ' With optional *start*, test string beginning at that ' |
| 12245 | 'position.\n' |
| 12246 | ' With optional *end*, stop comparing string at that ' |
| 12247 | 'position.\n' |
| 12248 | '\n' |
| 12249 | ' Changed in version 2.5: Accept tuples as *prefix*.\n' |
| 12250 | '\n' |
| 12251 | 'str.strip([chars])\n' |
| 12252 | '\n' |
| 12253 | ' Return a copy of the string with the leading and trailing\n' |
| 12254 | ' characters removed. The *chars* argument is a string ' |
| 12255 | 'specifying the\n' |
| 12256 | ' set of characters to be removed. If omitted or "None", the ' |
| 12257 | '*chars*\n' |
| 12258 | ' argument defaults to removing whitespace. The *chars* ' |
| 12259 | 'argument is\n' |
| 12260 | ' not a prefix or suffix; rather, all combinations of its ' |
| 12261 | 'values are\n' |
| 12262 | ' stripped:\n' |
| 12263 | '\n' |
| 12264 | " >>> ' spacious '.strip()\n" |
| 12265 | " 'spacious'\n" |
| 12266 | " >>> 'www.example.com'.strip('cmowz.')\n" |
| 12267 | " 'example'\n" |
| 12268 | '\n' |
| 12269 | ' Changed in version 2.2.2: Support for the *chars* ' |
| 12270 | 'argument.\n' |
| 12271 | '\n' |
| 12272 | 'str.swapcase()\n' |
| 12273 | '\n' |
| 12274 | ' Return a copy of the string with uppercase characters ' |
| 12275 | 'converted to\n' |
| 12276 | ' lowercase and vice versa.\n' |
| 12277 | '\n' |
| 12278 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12279 | '\n' |
| 12280 | 'str.title()\n' |
| 12281 | '\n' |
| 12282 | ' Return a titlecased version of the string where words start ' |
| 12283 | 'with an\n' |
| 12284 | ' uppercase character and the remaining characters are ' |
| 12285 | 'lowercase.\n' |
| 12286 | '\n' |
| 12287 | ' The algorithm uses a simple language-independent definition ' |
| 12288 | 'of a\n' |
| 12289 | ' word as groups of consecutive letters. The definition ' |
| 12290 | 'works in\n' |
| 12291 | ' many contexts but it means that apostrophes in contractions ' |
| 12292 | 'and\n' |
| 12293 | ' possessives form word boundaries, which may not be the ' |
| 12294 | 'desired\n' |
| 12295 | ' result:\n' |
| 12296 | '\n' |
| 12297 | ' >>> "they\'re bill\'s friends from the UK".title()\n' |
| 12298 | ' "They\'Re Bill\'S Friends From The Uk"\n' |
| 12299 | '\n' |
| 12300 | ' A workaround for apostrophes can be constructed using ' |
| 12301 | 'regular\n' |
| 12302 | ' expressions:\n' |
| 12303 | '\n' |
| 12304 | ' >>> import re\n' |
| 12305 | ' >>> def titlecase(s):\n' |
| 12306 | ' ... return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n' |
| 12307 | ' ... lambda mo: mo.group(0)[0].upper() ' |
| 12308 | '+\n' |
| 12309 | ' ... ' |
| 12310 | 'mo.group(0)[1:].lower(),\n' |
| 12311 | ' ... s)\n' |
| 12312 | ' ...\n' |
| 12313 | ' >>> titlecase("they\'re bill\'s friends.")\n' |
| 12314 | ' "They\'re Bill\'s Friends."\n' |
| 12315 | '\n' |
| 12316 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12317 | '\n' |
| 12318 | 'str.translate(table[, deletechars])\n' |
| 12319 | '\n' |
| 12320 | ' Return a copy of the string where all characters occurring ' |
| 12321 | 'in the\n' |
| 12322 | ' optional argument *deletechars* are removed, and the ' |
| 12323 | 'remaining\n' |
| 12324 | ' characters have been mapped through the given translation ' |
| 12325 | 'table,\n' |
| 12326 | ' which must be a string of length 256.\n' |
| 12327 | '\n' |
| 12328 | ' You can use the "maketrans()" helper function in the ' |
| 12329 | '"string"\n' |
| 12330 | ' module to create a translation table. For string objects, ' |
| 12331 | 'set the\n' |
| 12332 | ' *table* argument to "None" for translations that only ' |
| 12333 | 'delete\n' |
| 12334 | ' characters:\n' |
| 12335 | '\n' |
| 12336 | " >>> 'read this short text'.translate(None, 'aeiou')\n" |
| 12337 | " 'rd ths shrt txt'\n" |
| 12338 | '\n' |
| 12339 | ' New in version 2.6: Support for a "None" *table* argument.\n' |
| 12340 | '\n' |
| 12341 | ' For Unicode objects, the "translate()" method does not ' |
| 12342 | 'accept the\n' |
| 12343 | ' optional *deletechars* argument. Instead, it returns a ' |
| 12344 | 'copy of the\n' |
| 12345 | ' *s* where all characters have been mapped through the ' |
| 12346 | 'given\n' |
| 12347 | ' translation table which must be a mapping of Unicode ' |
| 12348 | 'ordinals to\n' |
| 12349 | ' Unicode ordinals, Unicode strings or "None". Unmapped ' |
| 12350 | 'characters\n' |
| 12351 | ' are left untouched. Characters mapped to "None" are ' |
| 12352 | 'deleted. Note,\n' |
| 12353 | ' a more flexible approach is to create a custom character ' |
| 12354 | 'mapping\n' |
| 12355 | ' codec using the "codecs" module (see "encodings.cp1251" for ' |
| 12356 | 'an\n' |
| 12357 | ' example).\n' |
| 12358 | '\n' |
| 12359 | 'str.upper()\n' |
| 12360 | '\n' |
| 12361 | ' Return a copy of the string with all the cased characters ' |
| 12362 | '[4]\n' |
| 12363 | ' converted to uppercase. Note that "str.upper().isupper()" ' |
| 12364 | 'might be\n' |
| 12365 | ' "False" if "s" contains uncased characters or if the ' |
| 12366 | 'Unicode\n' |
| 12367 | ' category of the resulting character(s) is not "Lu" ' |
| 12368 | '(Letter,\n' |
| 12369 | ' uppercase), but e.g. "Lt" (Letter, titlecase).\n' |
| 12370 | '\n' |
| 12371 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12372 | '\n' |
| 12373 | 'str.zfill(width)\n' |
| 12374 | '\n' |
| 12375 | ' Return the numeric string left filled with zeros in a ' |
| 12376 | 'string of\n' |
| 12377 | ' length *width*. A sign prefix is handled correctly. The ' |
| 12378 | 'original\n' |
| 12379 | ' string is returned if *width* is less than or equal to ' |
| 12380 | '"len(s)".\n' |
| 12381 | '\n' |
| 12382 | ' New in version 2.2.2.\n' |
| 12383 | '\n' |
| 12384 | 'The following methods are present only on unicode objects:\n' |
| 12385 | '\n' |
| 12386 | 'unicode.isnumeric()\n' |
| 12387 | '\n' |
| 12388 | ' Return "True" if there are only numeric characters in S, ' |
| 12389 | '"False"\n' |
| 12390 | ' otherwise. Numeric characters include digit characters, and ' |
| 12391 | 'all\n' |
| 12392 | ' characters that have the Unicode numeric value property, ' |
| 12393 | 'e.g.\n' |
| 12394 | ' U+2155, VULGAR FRACTION ONE FIFTH.\n' |
| 12395 | '\n' |
| 12396 | 'unicode.isdecimal()\n' |
| 12397 | '\n' |
| 12398 | ' Return "True" if there are only decimal characters in S, ' |
| 12399 | '"False"\n' |
| 12400 | ' otherwise. Decimal characters include digit characters, and ' |
| 12401 | 'all\n' |
| 12402 | ' characters that can be used to form decimal-radix numbers, ' |
| 12403 | 'e.g.\n' |
| 12404 | ' U+0660, ARABIC-INDIC DIGIT ZERO.\n' |
| 12405 | '\n' |
| 12406 | '\n' |
| 12407 | 'String Formatting Operations\n' |
| 12408 | '============================\n' |
| 12409 | '\n' |
| 12410 | 'String and Unicode objects have one unique built-in operation: ' |
| 12411 | 'the "%"\n' |
| 12412 | 'operator (modulo). This is also known as the string ' |
| 12413 | '*formatting* or\n' |
| 12414 | '*interpolation* operator. Given "format % values" (where ' |
| 12415 | '*format* is\n' |
| 12416 | 'a string or Unicode object), "%" conversion specifications in ' |
| 12417 | '*format*\n' |
| 12418 | 'are replaced with zero or more elements of *values*. The ' |
| 12419 | 'effect is\n' |
| 12420 | 'similar to the using "sprintf()" in the C language. If ' |
| 12421 | '*format* is a\n' |
| 12422 | 'Unicode object, or if any of the objects being converted using ' |
| 12423 | 'the\n' |
| 12424 | '"%s" conversion are Unicode objects, the result will also be a ' |
| 12425 | 'Unicode\n' |
| 12426 | 'object.\n' |
| 12427 | '\n' |
| 12428 | 'If *format* requires a single argument, *values* may be a ' |
| 12429 | 'single non-\n' |
| 12430 | 'tuple object. [5] Otherwise, *values* must be a tuple with ' |
| 12431 | 'exactly\n' |
| 12432 | 'the number of items specified by the format string, or a ' |
| 12433 | 'single\n' |
| 12434 | 'mapping object (for example, a dictionary).\n' |
| 12435 | '\n' |
| 12436 | 'A conversion specifier contains two or more characters and has ' |
| 12437 | 'the\n' |
| 12438 | 'following components, which must occur in this order:\n' |
| 12439 | '\n' |
| 12440 | '1. The "\'%\'" character, which marks the start of the ' |
| 12441 | 'specifier.\n' |
| 12442 | '\n' |
| 12443 | '2. Mapping key (optional), consisting of a parenthesised ' |
| 12444 | 'sequence\n' |
| 12445 | ' of characters (for example, "(somename)").\n' |
| 12446 | '\n' |
| 12447 | '3. Conversion flags (optional), which affect the result of ' |
| 12448 | 'some\n' |
| 12449 | ' conversion types.\n' |
| 12450 | '\n' |
| 12451 | '4. Minimum field width (optional). If specified as an ' |
| 12452 | '"\'*\'"\n' |
| 12453 | ' (asterisk), the actual width is read from the next element ' |
| 12454 | 'of the\n' |
| 12455 | ' tuple in *values*, and the object to convert comes after ' |
| 12456 | 'the\n' |
| 12457 | ' minimum field width and optional precision.\n' |
| 12458 | '\n' |
| 12459 | '5. Precision (optional), given as a "\'.\'" (dot) followed by ' |
| 12460 | 'the\n' |
| 12461 | ' precision. If specified as "\'*\'" (an asterisk), the ' |
| 12462 | 'actual width\n' |
| 12463 | ' is read from the next element of the tuple in *values*, and ' |
| 12464 | 'the\n' |
| 12465 | ' value to convert comes after the precision.\n' |
| 12466 | '\n' |
| 12467 | '6. Length modifier (optional).\n' |
| 12468 | '\n' |
| 12469 | '7. Conversion type.\n' |
| 12470 | '\n' |
| 12471 | 'When the right argument is a dictionary (or other mapping ' |
| 12472 | 'type), then\n' |
| 12473 | 'the formats in the string *must* include a parenthesised ' |
| 12474 | 'mapping key\n' |
| 12475 | 'into that dictionary inserted immediately after the "\'%\'" ' |
| 12476 | 'character.\n' |
| 12477 | 'The mapping key selects the value to be formatted from the ' |
| 12478 | 'mapping.\n' |
| 12479 | 'For example:\n' |
| 12480 | '\n' |
| 12481 | ">>> print '%(language)s has %(number)03d quote types.' % \\\n" |
| 12482 | '... {"language": "Python", "number": 2}\n' |
| 12483 | 'Python has 002 quote types.\n' |
| 12484 | '\n' |
| 12485 | 'In this case no "*" specifiers may occur in a format (since ' |
| 12486 | 'they\n' |
| 12487 | 'require a sequential parameter list).\n' |
| 12488 | '\n' |
| 12489 | 'The conversion flag characters are:\n' |
| 12490 | '\n' |
| 12491 | '+-----------+-----------------------------------------------------------------------+\n' |
| 12492 | '| Flag | ' |
| 12493 | 'Meaning ' |
| 12494 | '|\n' |
| 12495 | '+===========+=======================================================================+\n' |
| 12496 | '| "\'#\'" | The value conversion will use the "alternate ' |
| 12497 | 'form" (where defined |\n' |
| 12498 | '| | ' |
| 12499 | 'below). ' |
| 12500 | '|\n' |
| 12501 | '+-----------+-----------------------------------------------------------------------+\n' |
| 12502 | '| "\'0\'" | The conversion will be zero padded for numeric ' |
| 12503 | 'values. |\n' |
| 12504 | '+-----------+-----------------------------------------------------------------------+\n' |
| 12505 | '| "\'-\'" | The converted value is left adjusted ' |
| 12506 | '(overrides the "\'0\'" conversion |\n' |
| 12507 | '| | if both are ' |
| 12508 | 'given). |\n' |
| 12509 | '+-----------+-----------------------------------------------------------------------+\n' |
| 12510 | '| "\' \'" | (a space) A blank should be left before a ' |
| 12511 | 'positive number (or empty |\n' |
| 12512 | '| | string) produced by a signed ' |
| 12513 | 'conversion. |\n' |
| 12514 | '+-----------+-----------------------------------------------------------------------+\n' |
| 12515 | '| "\'+\'" | A sign character ("\'+\'" or "\'-\'") will ' |
| 12516 | 'precede the conversion |\n' |
| 12517 | '| | (overrides a "space" ' |
| 12518 | 'flag). |\n' |
| 12519 | '+-----------+-----------------------------------------------------------------------+\n' |
| 12520 | '\n' |
| 12521 | 'A length modifier ("h", "l", or "L") may be present, but is ' |
| 12522 | 'ignored as\n' |
| 12523 | 'it is not necessary for Python -- so e.g. "%ld" is identical ' |
| 12524 | 'to "%d".\n' |
| 12525 | '\n' |
| 12526 | 'The conversion types are:\n' |
| 12527 | '\n' |
| 12528 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12529 | '| Conversion | ' |
| 12530 | 'Meaning | ' |
| 12531 | 'Notes |\n' |
| 12532 | '+==============+=======================================================+=========+\n' |
| 12533 | '| "\'d\'" | Signed integer ' |
| 12534 | 'decimal. | |\n' |
| 12535 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12536 | '| "\'i\'" | Signed integer ' |
| 12537 | 'decimal. | |\n' |
| 12538 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12539 | '| "\'o\'" | Signed octal ' |
| 12540 | 'value. | (1) |\n' |
| 12541 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12542 | '| "\'u\'" | Obsolete type -- it is identical to ' |
| 12543 | '"\'d\'". | (7) |\n' |
| 12544 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12545 | '| "\'x\'" | Signed hexadecimal ' |
| 12546 | '(lowercase). | (2) |\n' |
| 12547 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12548 | '| "\'X\'" | Signed hexadecimal ' |
| 12549 | '(uppercase). | (2) |\n' |
| 12550 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12551 | '| "\'e\'" | Floating point exponential format ' |
| 12552 | '(lowercase). | (3) |\n' |
| 12553 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12554 | '| "\'E\'" | Floating point exponential format ' |
| 12555 | '(uppercase). | (3) |\n' |
| 12556 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12557 | '| "\'f\'" | Floating point decimal ' |
| 12558 | 'format. | (3) |\n' |
| 12559 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12560 | '| "\'F\'" | Floating point decimal ' |
| 12561 | 'format. | (3) |\n' |
| 12562 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12563 | '| "\'g\'" | Floating point format. Uses lowercase ' |
| 12564 | 'exponential | (4) |\n' |
| 12565 | '| | format if exponent is less than -4 or not ' |
| 12566 | 'less than | |\n' |
| 12567 | '| | precision, decimal format ' |
| 12568 | 'otherwise. | |\n' |
| 12569 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12570 | '| "\'G\'" | Floating point format. Uses uppercase ' |
| 12571 | 'exponential | (4) |\n' |
| 12572 | '| | format if exponent is less than -4 or not ' |
| 12573 | 'less than | |\n' |
| 12574 | '| | precision, decimal format ' |
| 12575 | 'otherwise. | |\n' |
| 12576 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12577 | '| "\'c\'" | Single character (accepts integer or single ' |
| 12578 | 'character | |\n' |
| 12579 | '| | ' |
| 12580 | 'string). ' |
| 12581 | '| |\n' |
| 12582 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12583 | '| "\'r\'" | String (converts any Python object using ' |
| 12584 | 'repr()). | (5) |\n' |
| 12585 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12586 | '| "\'s\'" | String (converts any Python object using ' |
| 12587 | '"str()"). | (6) |\n' |
| 12588 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12589 | '| "\'%\'" | No argument is converted, results in a ' |
| 12590 | '"\'%\'" | |\n' |
| 12591 | '| | character in the ' |
| 12592 | 'result. | |\n' |
| 12593 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12594 | '\n' |
| 12595 | 'Notes:\n' |
| 12596 | '\n' |
| 12597 | '1. The alternate form causes a leading zero ("\'0\'") to be ' |
| 12598 | 'inserted\n' |
| 12599 | ' between left-hand padding and the formatting of the number ' |
| 12600 | 'if the\n' |
| 12601 | ' leading character of the result is not already a zero.\n' |
| 12602 | '\n' |
| 12603 | '2. The alternate form causes a leading "\'0x\'" or "\'0X\'" ' |
| 12604 | '(depending\n' |
| 12605 | ' on whether the "\'x\'" or "\'X\'" format was used) to be ' |
| 12606 | 'inserted\n' |
| 12607 | ' between left-hand padding and the formatting of the number ' |
| 12608 | 'if the\n' |
| 12609 | ' leading character of the result is not already a zero.\n' |
| 12610 | '\n' |
| 12611 | '3. The alternate form causes the result to always contain a ' |
| 12612 | 'decimal\n' |
| 12613 | ' point, even if no digits follow it.\n' |
| 12614 | '\n' |
| 12615 | ' The precision determines the number of digits after the ' |
| 12616 | 'decimal\n' |
| 12617 | ' point and defaults to 6.\n' |
| 12618 | '\n' |
| 12619 | '4. The alternate form causes the result to always contain a ' |
| 12620 | 'decimal\n' |
| 12621 | ' point, and trailing zeroes are not removed as they would ' |
| 12622 | 'otherwise\n' |
| 12623 | ' be.\n' |
| 12624 | '\n' |
| 12625 | ' The precision determines the number of significant digits ' |
| 12626 | 'before\n' |
| 12627 | ' and after the decimal point and defaults to 6.\n' |
| 12628 | '\n' |
| 12629 | '5. The "%r" conversion was added in Python 2.0.\n' |
| 12630 | '\n' |
| 12631 | ' The precision determines the maximal number of characters ' |
| 12632 | 'used.\n' |
| 12633 | '\n' |
| 12634 | '6. If the object or format provided is a "unicode" string, ' |
| 12635 | 'the\n' |
| 12636 | ' resulting string will also be "unicode".\n' |
| 12637 | '\n' |
| 12638 | ' The precision determines the maximal number of characters ' |
| 12639 | 'used.\n' |
| 12640 | '\n' |
| 12641 | '7. See **PEP 237**.\n' |
| 12642 | '\n' |
| 12643 | 'Since Python strings have an explicit length, "%s" conversions ' |
| 12644 | 'do not\n' |
| 12645 | 'assume that "\'\\0\'" is the end of the string.\n' |
| 12646 | '\n' |
| 12647 | 'Changed in version 2.7: "%f" conversions for numbers whose ' |
| 12648 | 'absolute\n' |
| 12649 | 'value is over 1e50 are no longer replaced by "%g" ' |
| 12650 | 'conversions.\n' |
| 12651 | '\n' |
| 12652 | 'Additional string operations are defined in standard modules ' |
| 12653 | '"string"\n' |
| 12654 | 'and "re".\n' |
| 12655 | '\n' |
| 12656 | '\n' |
| 12657 | 'XRange Type\n' |
| 12658 | '===========\n' |
| 12659 | '\n' |
| 12660 | 'The "xrange" type is an immutable sequence which is commonly ' |
| 12661 | 'used for\n' |
| 12662 | 'looping. The advantage of the "xrange" type is that an ' |
| 12663 | '"xrange"\n' |
| 12664 | 'object will always take the same amount of memory, no matter ' |
| 12665 | 'the size\n' |
| 12666 | 'of the range it represents. There are no consistent ' |
| 12667 | 'performance\n' |
| 12668 | 'advantages.\n' |
| 12669 | '\n' |
| 12670 | 'XRange objects have very little behavior: they only support ' |
| 12671 | 'indexing,\n' |
| 12672 | 'iteration, and the "len()" function.\n' |
| 12673 | '\n' |
| 12674 | '\n' |
| 12675 | 'Mutable Sequence Types\n' |
| 12676 | '======================\n' |
| 12677 | '\n' |
| 12678 | 'List and "bytearray" objects support additional operations ' |
| 12679 | 'that allow\n' |
| 12680 | 'in-place modification of the object. Other mutable sequence ' |
| 12681 | 'types\n' |
| 12682 | '(when added to the language) should also support these ' |
| 12683 | 'operations.\n' |
| 12684 | 'Strings and tuples are immutable sequence types: such objects ' |
| 12685 | 'cannot\n' |
| 12686 | 'be modified once created. The following operations are defined ' |
| 12687 | 'on\n' |
| 12688 | 'mutable sequence types (where *x* is an arbitrary object):\n' |
| 12689 | '\n' |
| 12690 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12691 | '| Operation | ' |
| 12692 | 'Result | Notes |\n' |
| 12693 | '+================================+==================================+=======================+\n' |
| 12694 | '| "s[i] = x" | item *i* of *s* is replaced ' |
| 12695 | 'by | |\n' |
| 12696 | '| | ' |
| 12697 | '*x* | |\n' |
| 12698 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12699 | '| "s[i:j] = t" | slice of *s* from *i* to ' |
| 12700 | '*j* is | |\n' |
| 12701 | '| | replaced by the contents of ' |
| 12702 | 'the | |\n' |
| 12703 | '| | iterable ' |
| 12704 | '*t* | |\n' |
| 12705 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12706 | '| "del s[i:j]" | same as "s[i:j] = ' |
| 12707 | '[]" | |\n' |
| 12708 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12709 | '| "s[i:j:k] = t" | the elements of "s[i:j:k]" ' |
| 12710 | 'are | (1) |\n' |
| 12711 | '| | replaced by those of ' |
| 12712 | '*t* | |\n' |
| 12713 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12714 | '| "del s[i:j:k]" | removes the elements ' |
| 12715 | 'of | |\n' |
| 12716 | '| | "s[i:j:k]" from the ' |
| 12717 | 'list | |\n' |
| 12718 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12719 | '| "s.append(x)" | same as "s[len(s):len(s)] = ' |
| 12720 | '[x]" | (2) |\n' |
| 12721 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12722 | '| "s.extend(x)" or "s += t" | for the most part the same ' |
| 12723 | 'as | (3) |\n' |
| 12724 | '| | "s[len(s):len(s)] = ' |
| 12725 | 'x" | |\n' |
| 12726 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12727 | '| "s *= n" | updates *s* with its ' |
| 12728 | 'contents | (11) |\n' |
| 12729 | '| | repeated *n* ' |
| 12730 | 'times | |\n' |
| 12731 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12732 | '| "s.count(x)" | return number of *i*\'s for ' |
| 12733 | 'which | |\n' |
| 12734 | '| | "s[i] == ' |
| 12735 | 'x" | |\n' |
| 12736 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12737 | '| "s.index(x[, i[, j]])" | return smallest *k* such ' |
| 12738 | 'that | (4) |\n' |
| 12739 | '| | "s[k] == x" and "i <= k < ' |
| 12740 | 'j" | |\n' |
| 12741 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12742 | '| "s.insert(i, x)" | same as "s[i:i] = ' |
| 12743 | '[x]" | (5) |\n' |
| 12744 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12745 | '| "s.pop([i])" | same as "x = s[i]; del ' |
| 12746 | 's[i]; | (6) |\n' |
| 12747 | '| | return ' |
| 12748 | 'x" | |\n' |
| 12749 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12750 | '| "s.remove(x)" | same as "del ' |
| 12751 | 's[s.index(x)]" | (4) |\n' |
| 12752 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12753 | '| "s.reverse()" | reverses the items of *s* ' |
| 12754 | 'in | (7) |\n' |
| 12755 | '| | ' |
| 12756 | 'place | |\n' |
| 12757 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12758 | '| "s.sort([cmp[, key[, | sort the items of *s* in ' |
| 12759 | 'place | (7)(8)(9)(10) |\n' |
| 12760 | '| reverse]]])" ' |
| 12761 | '| | |\n' |
| 12762 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12763 | '\n' |
| 12764 | 'Notes:\n' |
| 12765 | '\n' |
| 12766 | '1. *t* must have the same length as the slice it is ' |
| 12767 | 'replacing.\n' |
| 12768 | '\n' |
| 12769 | '2. The C implementation of Python has historically accepted\n' |
| 12770 | ' multiple parameters and implicitly joined them into a ' |
| 12771 | 'tuple; this\n' |
| 12772 | ' no longer works in Python 2.0. Use of this misfeature has ' |
| 12773 | 'been\n' |
| 12774 | ' deprecated since Python 1.4.\n' |
| 12775 | '\n' |
| 12776 | '3. *x* can be any iterable object.\n' |
| 12777 | '\n' |
| 12778 | '4. Raises "ValueError" when *x* is not found in *s*. When a\n' |
| 12779 | ' negative index is passed as the second or third parameter ' |
| 12780 | 'to the\n' |
| 12781 | ' "index()" method, the list length is added, as for slice ' |
| 12782 | 'indices.\n' |
| 12783 | ' If it is still negative, it is truncated to zero, as for ' |
| 12784 | 'slice\n' |
| 12785 | ' indices.\n' |
| 12786 | '\n' |
| 12787 | ' Changed in version 2.3: Previously, "index()" didn\'t have ' |
| 12788 | 'arguments\n' |
| 12789 | ' for specifying start and stop positions.\n' |
| 12790 | '\n' |
| 12791 | '5. When a negative index is passed as the first parameter to ' |
| 12792 | 'the\n' |
| 12793 | ' "insert()" method, the list length is added, as for slice ' |
| 12794 | 'indices.\n' |
| 12795 | ' If it is still negative, it is truncated to zero, as for ' |
| 12796 | 'slice\n' |
| 12797 | ' indices.\n' |
| 12798 | '\n' |
| 12799 | ' Changed in version 2.3: Previously, all negative indices ' |
| 12800 | 'were\n' |
| 12801 | ' truncated to zero.\n' |
| 12802 | '\n' |
| 12803 | '6. The "pop()" method\'s optional argument *i* defaults to ' |
| 12804 | '"-1", so\n' |
| 12805 | ' that by default the last item is removed and returned.\n' |
| 12806 | '\n' |
| 12807 | '7. The "sort()" and "reverse()" methods modify the list in ' |
| 12808 | 'place\n' |
| 12809 | ' for economy of space when sorting or reversing a large ' |
| 12810 | 'list. To\n' |
| 12811 | " remind you that they operate by side effect, they don't " |
| 12812 | 'return the\n' |
| 12813 | ' sorted or reversed list.\n' |
| 12814 | '\n' |
| 12815 | '8. The "sort()" method takes optional arguments for ' |
| 12816 | 'controlling the\n' |
| 12817 | ' comparisons.\n' |
| 12818 | '\n' |
| 12819 | ' *cmp* specifies a custom comparison function of two ' |
| 12820 | 'arguments (list\n' |
| 12821 | ' items) which should return a negative, zero or positive ' |
| 12822 | 'number\n' |
| 12823 | ' depending on whether the first argument is considered ' |
| 12824 | 'smaller than,\n' |
| 12825 | ' equal to, or larger than the second argument: "cmp=lambda ' |
| 12826 | 'x,y:\n' |
| 12827 | ' cmp(x.lower(), y.lower())". The default value is "None".\n' |
| 12828 | '\n' |
| 12829 | ' *key* specifies a function of one argument that is used to ' |
| 12830 | 'extract\n' |
| 12831 | ' a comparison key from each list element: "key=str.lower". ' |
| 12832 | 'The\n' |
| 12833 | ' default value is "None".\n' |
| 12834 | '\n' |
| 12835 | ' *reverse* is a boolean value. If set to "True", then the ' |
| 12836 | 'list\n' |
| 12837 | ' elements are sorted as if each comparison were reversed.\n' |
| 12838 | '\n' |
| 12839 | ' In general, the *key* and *reverse* conversion processes ' |
| 12840 | 'are much\n' |
| 12841 | ' faster than specifying an equivalent *cmp* function. This ' |
| 12842 | 'is\n' |
| 12843 | ' because *cmp* is called multiple times for each list ' |
| 12844 | 'element while\n' |
| 12845 | ' *key* and *reverse* touch each element only once. Use\n' |
| 12846 | ' "functools.cmp_to_key()" to convert an old-style *cmp* ' |
| 12847 | 'function to\n' |
| 12848 | ' a *key* function.\n' |
| 12849 | '\n' |
| 12850 | ' Changed in version 2.3: Support for "None" as an equivalent ' |
| 12851 | 'to\n' |
| 12852 | ' omitting *cmp* was added.\n' |
| 12853 | '\n' |
| 12854 | ' Changed in version 2.4: Support for *key* and *reverse* was ' |
| 12855 | 'added.\n' |
| 12856 | '\n' |
| 12857 | '9. Starting with Python 2.3, the "sort()" method is guaranteed ' |
| 12858 | 'to\n' |
| 12859 | ' be stable. A sort is stable if it guarantees not to change ' |
| 12860 | 'the\n' |
| 12861 | ' relative order of elements that compare equal --- this is ' |
| 12862 | 'helpful\n' |
| 12863 | ' for sorting in multiple passes (for example, sort by ' |
| 12864 | 'department,\n' |
| 12865 | ' then by salary grade).\n' |
| 12866 | '\n' |
| 12867 | '10. **CPython implementation detail:** While a list is being\n' |
| 12868 | ' sorted, the effect of attempting to mutate, or even ' |
| 12869 | 'inspect, the\n' |
| 12870 | ' list is undefined. The C implementation of Python 2.3 and ' |
| 12871 | 'newer\n' |
| 12872 | ' makes the list appear empty for the duration, and raises\n' |
| 12873 | ' "ValueError" if it can detect that the list has been ' |
| 12874 | 'mutated\n' |
| 12875 | ' during a sort.\n' |
| 12876 | '\n' |
| 12877 | '11. The value *n* is an integer, or an object implementing\n' |
| 12878 | ' "__index__()". Zero and negative values of *n* clear the\n' |
| 12879 | ' sequence. Items in the sequence are not copied; they are\n' |
| 12880 | ' referenced multiple times, as explained for "s * n" under ' |
| 12881 | 'Sequence\n' |
| 12882 | ' Types --- str, unicode, list, tuple, bytearray, buffer, ' |
| 12883 | 'xrange.\n', |
| 12884 | 'typesseq-mutable': '\n' |
| 12885 | 'Mutable Sequence Types\n' |
| 12886 | '**********************\n' |
| 12887 | '\n' |
| 12888 | 'List and "bytearray" objects support additional ' |
| 12889 | 'operations that allow\n' |
| 12890 | 'in-place modification of the object. Other mutable ' |
| 12891 | 'sequence types\n' |
| 12892 | '(when added to the language) should also support these ' |
| 12893 | 'operations.\n' |
| 12894 | 'Strings and tuples are immutable sequence types: such ' |
| 12895 | 'objects cannot\n' |
| 12896 | 'be modified once created. The following operations are ' |
| 12897 | 'defined on\n' |
| 12898 | 'mutable sequence types (where *x* is an arbitrary ' |
| 12899 | 'object):\n' |
| 12900 | '\n' |
| 12901 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12902 | '| Operation | ' |
| 12903 | 'Result | ' |
| 12904 | 'Notes |\n' |
| 12905 | '+================================+==================================+=======================+\n' |
| 12906 | '| "s[i] = x" | item *i* of *s* is ' |
| 12907 | 'replaced by | |\n' |
| 12908 | '| | ' |
| 12909 | '*x* ' |
| 12910 | '| |\n' |
| 12911 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12912 | '| "s[i:j] = t" | slice of *s* from ' |
| 12913 | '*i* to *j* is | |\n' |
| 12914 | '| | replaced by the ' |
| 12915 | 'contents of the | |\n' |
| 12916 | '| | iterable ' |
| 12917 | '*t* | |\n' |
| 12918 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12919 | '| "del s[i:j]" | same as "s[i:j] = ' |
| 12920 | '[]" | |\n' |
| 12921 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12922 | '| "s[i:j:k] = t" | the elements of ' |
| 12923 | '"s[i:j:k]" are | (1) |\n' |
| 12924 | '| | replaced by those ' |
| 12925 | 'of *t* | |\n' |
| 12926 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12927 | '| "del s[i:j:k]" | removes the ' |
| 12928 | 'elements of | |\n' |
| 12929 | '| | "s[i:j:k]" from the ' |
| 12930 | 'list | |\n' |
| 12931 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12932 | '| "s.append(x)" | same as ' |
| 12933 | '"s[len(s):len(s)] = [x]" | (2) |\n' |
| 12934 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12935 | '| "s.extend(x)" or "s += t" | for the most part ' |
| 12936 | 'the same as | (3) |\n' |
| 12937 | '| | "s[len(s):len(s)] = ' |
| 12938 | 'x" | |\n' |
| 12939 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12940 | '| "s *= n" | updates *s* with ' |
| 12941 | 'its contents | (11) |\n' |
| 12942 | '| | repeated *n* ' |
| 12943 | 'times | |\n' |
| 12944 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12945 | '| "s.count(x)" | return number of ' |
| 12946 | "*i*'s for which | |\n" |
| 12947 | '| | "s[i] == ' |
| 12948 | 'x" | |\n' |
| 12949 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12950 | '| "s.index(x[, i[, j]])" | return smallest *k* ' |
| 12951 | 'such that | (4) |\n' |
| 12952 | '| | "s[k] == x" and "i ' |
| 12953 | '<= k < j" | |\n' |
| 12954 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12955 | '| "s.insert(i, x)" | same as "s[i:i] = ' |
| 12956 | '[x]" | (5) |\n' |
| 12957 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12958 | '| "s.pop([i])" | same as "x = s[i]; ' |
| 12959 | 'del s[i]; | (6) |\n' |
| 12960 | '| | return ' |
| 12961 | 'x" | |\n' |
| 12962 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12963 | '| "s.remove(x)" | same as "del ' |
| 12964 | 's[s.index(x)]" | (4) |\n' |
| 12965 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12966 | '| "s.reverse()" | reverses the items ' |
| 12967 | 'of *s* in | (7) |\n' |
| 12968 | '| | ' |
| 12969 | 'place ' |
| 12970 | '| |\n' |
| 12971 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12972 | '| "s.sort([cmp[, key[, | sort the items of ' |
| 12973 | '*s* in place | (7)(8)(9)(10) |\n' |
| 12974 | '| reverse]]])" ' |
| 12975 | '| ' |
| 12976 | '| |\n' |
| 12977 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12978 | '\n' |
| 12979 | 'Notes:\n' |
| 12980 | '\n' |
| 12981 | '1. *t* must have the same length as the slice it is ' |
| 12982 | 'replacing.\n' |
| 12983 | '\n' |
| 12984 | '2. The C implementation of Python has historically ' |
| 12985 | 'accepted\n' |
| 12986 | ' multiple parameters and implicitly joined them into ' |
| 12987 | 'a tuple; this\n' |
| 12988 | ' no longer works in Python 2.0. Use of this ' |
| 12989 | 'misfeature has been\n' |
| 12990 | ' deprecated since Python 1.4.\n' |
| 12991 | '\n' |
| 12992 | '3. *x* can be any iterable object.\n' |
| 12993 | '\n' |
| 12994 | '4. Raises "ValueError" when *x* is not found in *s*. ' |
| 12995 | 'When a\n' |
| 12996 | ' negative index is passed as the second or third ' |
| 12997 | 'parameter to the\n' |
| 12998 | ' "index()" method, the list length is added, as for ' |
| 12999 | 'slice indices.\n' |
| 13000 | ' If it is still negative, it is truncated to zero, ' |
| 13001 | 'as for slice\n' |
| 13002 | ' indices.\n' |
| 13003 | '\n' |
| 13004 | ' Changed in version 2.3: Previously, "index()" ' |
| 13005 | "didn't have arguments\n" |
| 13006 | ' for specifying start and stop positions.\n' |
| 13007 | '\n' |
| 13008 | '5. When a negative index is passed as the first ' |
| 13009 | 'parameter to the\n' |
| 13010 | ' "insert()" method, the list length is added, as for ' |
| 13011 | 'slice indices.\n' |
| 13012 | ' If it is still negative, it is truncated to zero, ' |
| 13013 | 'as for slice\n' |
| 13014 | ' indices.\n' |
| 13015 | '\n' |
| 13016 | ' Changed in version 2.3: Previously, all negative ' |
| 13017 | 'indices were\n' |
| 13018 | ' truncated to zero.\n' |
| 13019 | '\n' |
| 13020 | '6. The "pop()" method\'s optional argument *i* ' |
| 13021 | 'defaults to "-1", so\n' |
| 13022 | ' that by default the last item is removed and ' |
| 13023 | 'returned.\n' |
| 13024 | '\n' |
| 13025 | '7. The "sort()" and "reverse()" methods modify the ' |
| 13026 | 'list in place\n' |
| 13027 | ' for economy of space when sorting or reversing a ' |
| 13028 | 'large list. To\n' |
| 13029 | ' remind you that they operate by side effect, they ' |
| 13030 | "don't return the\n" |
| 13031 | ' sorted or reversed list.\n' |
| 13032 | '\n' |
| 13033 | '8. The "sort()" method takes optional arguments for ' |
| 13034 | 'controlling the\n' |
| 13035 | ' comparisons.\n' |
| 13036 | '\n' |
| 13037 | ' *cmp* specifies a custom comparison function of two ' |
| 13038 | 'arguments (list\n' |
| 13039 | ' items) which should return a negative, zero or ' |
| 13040 | 'positive number\n' |
| 13041 | ' depending on whether the first argument is ' |
| 13042 | 'considered smaller than,\n' |
| 13043 | ' equal to, or larger than the second argument: ' |
| 13044 | '"cmp=lambda x,y:\n' |
| 13045 | ' cmp(x.lower(), y.lower())". The default value is ' |
| 13046 | '"None".\n' |
| 13047 | '\n' |
| 13048 | ' *key* specifies a function of one argument that is ' |
| 13049 | 'used to extract\n' |
| 13050 | ' a comparison key from each list element: ' |
| 13051 | '"key=str.lower". The\n' |
| 13052 | ' default value is "None".\n' |
| 13053 | '\n' |
| 13054 | ' *reverse* is a boolean value. If set to "True", ' |
| 13055 | 'then the list\n' |
| 13056 | ' elements are sorted as if each comparison were ' |
| 13057 | 'reversed.\n' |
| 13058 | '\n' |
| 13059 | ' In general, the *key* and *reverse* conversion ' |
| 13060 | 'processes are much\n' |
| 13061 | ' faster than specifying an equivalent *cmp* ' |
| 13062 | 'function. This is\n' |
| 13063 | ' because *cmp* is called multiple times for each ' |
| 13064 | 'list element while\n' |
| 13065 | ' *key* and *reverse* touch each element only once. ' |
| 13066 | 'Use\n' |
| 13067 | ' "functools.cmp_to_key()" to convert an old-style ' |
| 13068 | '*cmp* function to\n' |
| 13069 | ' a *key* function.\n' |
| 13070 | '\n' |
| 13071 | ' Changed in version 2.3: Support for "None" as an ' |
| 13072 | 'equivalent to\n' |
| 13073 | ' omitting *cmp* was added.\n' |
| 13074 | '\n' |
| 13075 | ' Changed in version 2.4: Support for *key* and ' |
| 13076 | '*reverse* was added.\n' |
| 13077 | '\n' |
| 13078 | '9. Starting with Python 2.3, the "sort()" method is ' |
| 13079 | 'guaranteed to\n' |
| 13080 | ' be stable. A sort is stable if it guarantees not ' |
| 13081 | 'to change the\n' |
| 13082 | ' relative order of elements that compare equal --- ' |
| 13083 | 'this is helpful\n' |
| 13084 | ' for sorting in multiple passes (for example, sort ' |
| 13085 | 'by department,\n' |
| 13086 | ' then by salary grade).\n' |
| 13087 | '\n' |
| 13088 | '10. **CPython implementation detail:** While a list is ' |
| 13089 | 'being\n' |
| 13090 | ' sorted, the effect of attempting to mutate, or ' |
| 13091 | 'even inspect, the\n' |
| 13092 | ' list is undefined. The C implementation of Python ' |
| 13093 | '2.3 and newer\n' |
| 13094 | ' makes the list appear empty for the duration, and ' |
| 13095 | 'raises\n' |
| 13096 | ' "ValueError" if it can detect that the list has ' |
| 13097 | 'been mutated\n' |
| 13098 | ' during a sort.\n' |
| 13099 | '\n' |
| 13100 | '11. The value *n* is an integer, or an object ' |
| 13101 | 'implementing\n' |
| 13102 | ' "__index__()". Zero and negative values of *n* ' |
| 13103 | 'clear the\n' |
| 13104 | ' sequence. Items in the sequence are not copied; ' |
| 13105 | 'they are\n' |
| 13106 | ' referenced multiple times, as explained for "s * ' |
| 13107 | 'n" under Sequence\n' |
| 13108 | ' Types --- str, unicode, list, tuple, bytearray, ' |
| 13109 | 'buffer, xrange.\n', |
| 13110 | 'unary': '\n' |
| 13111 | 'Unary arithmetic and bitwise operations\n' |
| 13112 | '***************************************\n' |
| 13113 | '\n' |
| 13114 | 'All unary arithmetic and bitwise operations have the same ' |
| 13115 | 'priority:\n' |
| 13116 | '\n' |
| 13117 | ' u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr\n' |
| 13118 | '\n' |
| 13119 | 'The unary "-" (minus) operator yields the negation of its ' |
| 13120 | 'numeric\n' |
| 13121 | 'argument.\n' |
| 13122 | '\n' |
| 13123 | 'The unary "+" (plus) operator yields its numeric argument ' |
| 13124 | 'unchanged.\n' |
| 13125 | '\n' |
| 13126 | 'The unary "~" (invert) operator yields the bitwise inversion of ' |
| 13127 | 'its\n' |
| 13128 | 'plain or long integer argument. The bitwise inversion of "x" is\n' |
| 13129 | 'defined as "-(x+1)". It only applies to integral numbers.\n' |
| 13130 | '\n' |
| 13131 | 'In all three cases, if the argument does not have the proper ' |
| 13132 | 'type, a\n' |
| 13133 | '"TypeError" exception is raised.\n', |
| 13134 | 'while': '\n' |
| 13135 | 'The "while" statement\n' |
| 13136 | '*********************\n' |
| 13137 | '\n' |
| 13138 | 'The "while" statement is used for repeated execution as long as ' |
| 13139 | 'an\n' |
| 13140 | 'expression is true:\n' |
| 13141 | '\n' |
| 13142 | ' while_stmt ::= "while" expression ":" suite\n' |
| 13143 | ' ["else" ":" suite]\n' |
| 13144 | '\n' |
| 13145 | 'This repeatedly tests the expression and, if it is true, executes ' |
| 13146 | 'the\n' |
| 13147 | 'first suite; if the expression is false (which may be the first ' |
| 13148 | 'time\n' |
| 13149 | 'it is tested) the suite of the "else" clause, if present, is ' |
| 13150 | 'executed\n' |
| 13151 | 'and the loop terminates.\n' |
| 13152 | '\n' |
| 13153 | 'A "break" statement executed in the first suite terminates the ' |
| 13154 | 'loop\n' |
| 13155 | 'without executing the "else" clause\'s suite. A "continue" ' |
| 13156 | 'statement\n' |
| 13157 | 'executed in the first suite skips the rest of the suite and goes ' |
| 13158 | 'back\n' |
| 13159 | 'to testing the expression.\n', |
| 13160 | 'with': '\n' |
| 13161 | 'The "with" statement\n' |
| 13162 | '********************\n' |
| 13163 | '\n' |
| 13164 | 'New in version 2.5.\n' |
| 13165 | '\n' |
| 13166 | 'The "with" statement is used to wrap the execution of a block ' |
| 13167 | 'with\n' |
| 13168 | 'methods defined by a context manager (see section With Statement\n' |
| 13169 | 'Context Managers). This allows common ' |
| 13170 | '"try"..."except"..."finally"\n' |
| 13171 | 'usage patterns to be encapsulated for convenient reuse.\n' |
| 13172 | '\n' |
| 13173 | ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n' |
| 13174 | ' with_item ::= expression ["as" target]\n' |
| 13175 | '\n' |
| 13176 | 'The execution of the "with" statement with one "item" proceeds as\n' |
| 13177 | 'follows:\n' |
| 13178 | '\n' |
| 13179 | '1. The context expression (the expression given in the ' |
| 13180 | '"with_item")\n' |
| 13181 | ' is evaluated to obtain a context manager.\n' |
| 13182 | '\n' |
| 13183 | '2. The context manager\'s "__exit__()" is loaded for later use.\n' |
| 13184 | '\n' |
| 13185 | '3. The context manager\'s "__enter__()" method is invoked.\n' |
| 13186 | '\n' |
| 13187 | '4. If a target was included in the "with" statement, the return\n' |
| 13188 | ' value from "__enter__()" is assigned to it.\n' |
| 13189 | '\n' |
| 13190 | ' Note: The "with" statement guarantees that if the ' |
| 13191 | '"__enter__()"\n' |
| 13192 | ' method returns without an error, then "__exit__()" will ' |
| 13193 | 'always be\n' |
| 13194 | ' called. Thus, if an error occurs during the assignment to ' |
| 13195 | 'the\n' |
| 13196 | ' target list, it will be treated the same as an error ' |
| 13197 | 'occurring\n' |
| 13198 | ' within the suite would be. See step 6 below.\n' |
| 13199 | '\n' |
| 13200 | '5. The suite is executed.\n' |
| 13201 | '\n' |
| 13202 | '6. The context manager\'s "__exit__()" method is invoked. If an\n' |
| 13203 | ' exception caused the suite to be exited, its type, value, and\n' |
| 13204 | ' traceback are passed as arguments to "__exit__()". Otherwise, ' |
| 13205 | 'three\n' |
| 13206 | ' "None" arguments are supplied.\n' |
| 13207 | '\n' |
| 13208 | ' If the suite was exited due to an exception, and the return ' |
| 13209 | 'value\n' |
| 13210 | ' from the "__exit__()" method was false, the exception is ' |
| 13211 | 'reraised.\n' |
| 13212 | ' If the return value was true, the exception is suppressed, and\n' |
| 13213 | ' execution continues with the statement following the "with"\n' |
| 13214 | ' statement.\n' |
| 13215 | '\n' |
| 13216 | ' If the suite was exited for any reason other than an exception, ' |
| 13217 | 'the\n' |
| 13218 | ' return value from "__exit__()" is ignored, and execution ' |
| 13219 | 'proceeds\n' |
| 13220 | ' at the normal location for the kind of exit that was taken.\n' |
| 13221 | '\n' |
| 13222 | 'With more than one item, the context managers are processed as if\n' |
| 13223 | 'multiple "with" statements were nested:\n' |
| 13224 | '\n' |
| 13225 | ' with A() as a, B() as b:\n' |
| 13226 | ' suite\n' |
| 13227 | '\n' |
| 13228 | 'is equivalent to\n' |
| 13229 | '\n' |
| 13230 | ' with A() as a:\n' |
| 13231 | ' with B() as b:\n' |
| 13232 | ' suite\n' |
| 13233 | '\n' |
| 13234 | 'Note: In Python 2.5, the "with" statement is only allowed when ' |
| 13235 | 'the\n' |
| 13236 | ' "with_statement" feature has been enabled. It is always enabled ' |
| 13237 | 'in\n' |
| 13238 | ' Python 2.6.\n' |
| 13239 | '\n' |
| 13240 | 'Changed in version 2.7: Support for multiple context expressions.\n' |
| 13241 | '\n' |
| 13242 | 'See also: **PEP 0343** - The "with" statement\n' |
| 13243 | '\n' |
| 13244 | ' The specification, background, and examples for the Python ' |
| 13245 | '"with"\n' |
| 13246 | ' statement.\n', |
| 13247 | 'yield': '\n' |
| 13248 | 'The "yield" statement\n' |
| 13249 | '*********************\n' |
| 13250 | '\n' |
| 13251 | ' yield_stmt ::= yield_expression\n' |
| 13252 | '\n' |
| 13253 | 'The "yield" statement is only used when defining a generator ' |
| 13254 | 'function,\n' |
| 13255 | 'and is only used in the body of the generator function. Using a\n' |
| 13256 | '"yield" statement in a function definition is sufficient to cause ' |
| 13257 | 'that\n' |
| 13258 | 'definition to create a generator function instead of a normal\n' |
| 13259 | 'function.\n' |
| 13260 | '\n' |
| 13261 | 'When a generator function is called, it returns an iterator known ' |
| 13262 | 'as a\n' |
| 13263 | 'generator iterator, or more commonly, a generator. The body of ' |
| 13264 | 'the\n' |
| 13265 | "generator function is executed by calling the generator's " |
| 13266 | '"next()"\n' |
| 13267 | 'method repeatedly until it raises an exception.\n' |
| 13268 | '\n' |
| 13269 | 'When a "yield" statement is executed, the state of the generator ' |
| 13270 | 'is\n' |
| 13271 | 'frozen and the value of "expression_list" is returned to ' |
| 13272 | '"next()"\'s\n' |
| 13273 | 'caller. By "frozen" we mean that all local state is retained,\n' |
| 13274 | 'including the current bindings of local variables, the ' |
| 13275 | 'instruction\n' |
| 13276 | 'pointer, and the internal evaluation stack: enough information ' |
| 13277 | 'is\n' |
| 13278 | 'saved so that the next time "next()" is invoked, the function ' |
| 13279 | 'can\n' |
| 13280 | 'proceed exactly as if the "yield" statement were just another ' |
| 13281 | 'external\n' |
| 13282 | 'call.\n' |
| 13283 | '\n' |
| 13284 | 'As of Python version 2.5, the "yield" statement is now allowed in ' |
| 13285 | 'the\n' |
| 13286 | '"try" clause of a "try" ... "finally" construct. If the ' |
| 13287 | 'generator is\n' |
| 13288 | 'not resumed before it is finalized (by reaching a zero reference ' |
| 13289 | 'count\n' |
| 13290 | "or by being garbage collected), the generator-iterator's " |
| 13291 | '"close()"\n' |
| 13292 | 'method will be called, allowing any pending "finally" clauses to\n' |
| 13293 | 'execute.\n' |
| 13294 | '\n' |
| 13295 | 'For full details of "yield" semantics, refer to the Yield ' |
| 13296 | 'expressions\n' |
| 13297 | 'section.\n' |
| 13298 | '\n' |
| 13299 | 'Note: In Python 2.2, the "yield" statement was only allowed when ' |
| 13300 | 'the\n' |
| 13301 | ' "generators" feature has been enabled. This "__future__" ' |
| 13302 | 'import\n' |
| 13303 | ' statement was used to enable the feature:\n' |
| 13304 | '\n' |
| 13305 | ' from __future__ import generators\n' |
| 13306 | '\n' |
| 13307 | 'See also: **PEP 0255** - Simple Generators\n' |
| 13308 | '\n' |
| 13309 | ' The proposal for adding generators and the "yield" statement ' |
| 13310 | 'to\n' |
| 13311 | ' Python.\n' |
| 13312 | '\n' |
| 13313 | ' **PEP 0342** - Coroutines via Enhanced Generators\n' |
| 13314 | ' The proposal that, among other generator enhancements, ' |
| 13315 | 'proposed\n' |
| 13316 | ' allowing "yield" to appear inside a "try" ... "finally" ' |
| 13317 | 'block.\n'} |