Benjamin Peterson | d58cec2 | 2014-11-25 18:25:06 -0600 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Benjamin Peterson | 48455e2 | 2017-08-26 11:17:02 -0700 | [diff] [blame] | 2 | # Autogenerated by Sphinx on Sat Aug 26 11:16:28 2017 |
| 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" under\n' |
| 28 | 'normal circumstances, "False" when optimization is requested ' |
| 29 | '(command\n' |
| 30 | 'line option -O). The current code generator emits no code for an\n' |
| 31 | 'assert statement when optimization is requested at compile time. ' |
| 32 | 'Note\n' |
| 33 | 'that it is unnecessary to include the source code for the ' |
| 34 | 'expression\n' |
| 35 | 'that failed in the error message; it will be displayed as part of ' |
| 36 | 'the\n' |
| 37 | 'stack trace.\n' |
| 38 | '\n' |
| 39 | 'Assignments to "__debug__" are illegal. The value for the ' |
| 40 | 'built-in\n' |
| 41 | 'variable is determined when the interpreter starts.\n', |
| 42 | 'assignment': '\n' |
| 43 | 'Assignment statements\n' |
| 44 | '*********************\n' |
| 45 | '\n' |
| 46 | 'Assignment statements are used to (re)bind names to values and ' |
| 47 | 'to\n' |
| 48 | 'modify attributes or items of mutable objects:\n' |
| 49 | '\n' |
| 50 | ' assignment_stmt ::= (target_list "=")+ (expression_list | ' |
| 51 | 'yield_expression)\n' |
| 52 | ' target_list ::= target ("," target)* [","]\n' |
| 53 | ' target ::= identifier\n' |
| 54 | ' | "(" target_list ")"\n' |
| 55 | ' | "[" [target_list] "]"\n' |
| 56 | ' | attributeref\n' |
| 57 | ' | subscription\n' |
| 58 | ' | slicing\n' |
| 59 | '\n' |
| 60 | '(See section Primaries for the syntax definitions for the last ' |
| 61 | 'three\n' |
| 62 | 'symbols.)\n' |
| 63 | '\n' |
| 64 | 'An assignment statement evaluates the expression list ' |
| 65 | '(remember that\n' |
| 66 | 'this can be a single expression or a comma-separated list, the ' |
| 67 | 'latter\n' |
| 68 | 'yielding a tuple) and assigns the single resulting object to ' |
| 69 | 'each of\n' |
| 70 | 'the target lists, from left to right.\n' |
| 71 | '\n' |
| 72 | 'Assignment is defined recursively depending on the form of the ' |
| 73 | 'target\n' |
| 74 | '(list). When a target is part of a mutable object (an ' |
| 75 | 'attribute\n' |
| 76 | 'reference, subscription or slicing), the mutable object must\n' |
| 77 | 'ultimately perform the assignment and decide about its ' |
| 78 | 'validity, and\n' |
| 79 | 'may raise an exception if the assignment is unacceptable. The ' |
| 80 | 'rules\n' |
| 81 | 'observed by various types and the exceptions raised are given ' |
| 82 | 'with the\n' |
| 83 | 'definition of the object types (see section The standard type\n' |
| 84 | 'hierarchy).\n' |
| 85 | '\n' |
| 86 | 'Assignment of an object to a target list is recursively ' |
| 87 | 'defined as\n' |
| 88 | 'follows.\n' |
| 89 | '\n' |
| 90 | '* If the target list is a single target: The object is ' |
| 91 | 'assigned to\n' |
| 92 | ' that target.\n' |
| 93 | '\n' |
| 94 | '* If the target list is a comma-separated list of targets: ' |
| 95 | 'The\n' |
| 96 | ' object must be an iterable with the same number of items as ' |
| 97 | 'there\n' |
| 98 | ' are targets in the target list, and the items are assigned, ' |
| 99 | 'from\n' |
| 100 | ' left to right, to the corresponding targets.\n' |
| 101 | '\n' |
| 102 | 'Assignment of an object to a single target is recursively ' |
| 103 | 'defined as\n' |
| 104 | 'follows.\n' |
| 105 | '\n' |
| 106 | '* If the target is an identifier (name):\n' |
| 107 | '\n' |
| 108 | ' * If the name does not occur in a "global" statement in the\n' |
| 109 | ' current code block: the name is bound to the object in the ' |
| 110 | 'current\n' |
| 111 | ' local namespace.\n' |
| 112 | '\n' |
| 113 | ' * Otherwise: the name is bound to the object in the current ' |
| 114 | 'global\n' |
| 115 | ' namespace.\n' |
| 116 | '\n' |
| 117 | ' The name is rebound if it was already bound. This may cause ' |
| 118 | 'the\n' |
| 119 | ' reference count for the object previously bound to the name ' |
| 120 | 'to reach\n' |
| 121 | ' zero, causing the object to be deallocated and its ' |
| 122 | 'destructor (if it\n' |
| 123 | ' has one) to be called.\n' |
| 124 | '\n' |
| 125 | '* If the target is a target list enclosed in parentheses or ' |
| 126 | 'in\n' |
| 127 | ' square brackets: The object must be an iterable with the ' |
| 128 | 'same number\n' |
| 129 | ' of items as there are targets in the target list, and its ' |
| 130 | 'items are\n' |
| 131 | ' assigned, from left to right, to the corresponding targets.\n' |
| 132 | '\n' |
| 133 | '* If the target is an attribute reference: The primary ' |
| 134 | 'expression in\n' |
| 135 | ' the reference is evaluated. It should yield an object with\n' |
| 136 | ' assignable attributes; if this is not the case, "TypeError" ' |
| 137 | 'is\n' |
| 138 | ' raised. That object is then asked to assign the assigned ' |
| 139 | 'object to\n' |
| 140 | ' the given attribute; if it cannot perform the assignment, it ' |
| 141 | 'raises\n' |
| 142 | ' an exception (usually but not necessarily ' |
| 143 | '"AttributeError").\n' |
| 144 | '\n' |
| 145 | ' Note: If the object is a class instance and the attribute ' |
| 146 | 'reference\n' |
| 147 | ' occurs on both sides of the assignment operator, the RHS ' |
| 148 | 'expression,\n' |
| 149 | ' "a.x" can access either an instance attribute or (if no ' |
| 150 | 'instance\n' |
| 151 | ' attribute exists) a class attribute. The LHS target "a.x" ' |
| 152 | 'is always\n' |
| 153 | ' set as an instance attribute, creating it if necessary. ' |
| 154 | 'Thus, the\n' |
| 155 | ' two occurrences of "a.x" do not necessarily refer to the ' |
| 156 | 'same\n' |
| 157 | ' attribute: if the RHS expression refers to a class ' |
| 158 | 'attribute, the\n' |
| 159 | ' LHS creates a new instance attribute as the target of the\n' |
| 160 | ' assignment:\n' |
| 161 | '\n' |
| 162 | ' class Cls:\n' |
| 163 | ' x = 3 # class variable\n' |
| 164 | ' inst = Cls()\n' |
| 165 | ' inst.x = inst.x + 1 # writes inst.x as 4 leaving Cls.x ' |
| 166 | 'as 3\n' |
| 167 | '\n' |
| 168 | ' This description does not necessarily apply to descriptor\n' |
| 169 | ' attributes, such as properties created with "property()".\n' |
| 170 | '\n' |
| 171 | '* If the target is a subscription: The primary expression in ' |
| 172 | 'the\n' |
| 173 | ' reference is evaluated. It should yield either a mutable ' |
| 174 | 'sequence\n' |
| 175 | ' object (such as a list) or a mapping object (such as a ' |
| 176 | 'dictionary).\n' |
| 177 | ' Next, the subscript expression is evaluated.\n' |
| 178 | '\n' |
| 179 | ' If the primary is a mutable sequence object (such as a ' |
| 180 | 'list), the\n' |
| 181 | ' subscript must yield a plain integer. If it is negative, ' |
| 182 | 'the\n' |
| 183 | " sequence's length is added to it. The resulting value must " |
| 184 | 'be a\n' |
| 185 | " nonnegative integer less than the sequence's length, and " |
| 186 | 'the\n' |
| 187 | ' sequence is asked to assign the assigned object to its item ' |
| 188 | 'with\n' |
| 189 | ' that index. If the index is out of range, "IndexError" is ' |
| 190 | 'raised\n' |
| 191 | ' (assignment to a subscripted sequence cannot add new items ' |
| 192 | 'to a\n' |
| 193 | ' list).\n' |
| 194 | '\n' |
| 195 | ' If the primary is a mapping object (such as a dictionary), ' |
| 196 | 'the\n' |
| 197 | " subscript must have a type compatible with the mapping's key " |
| 198 | 'type,\n' |
| 199 | ' and the mapping is then asked to create a key/datum pair ' |
| 200 | 'which maps\n' |
| 201 | ' the subscript to the assigned object. This can either ' |
| 202 | 'replace an\n' |
| 203 | ' existing key/value pair with the same key value, or insert a ' |
| 204 | 'new\n' |
| 205 | ' key/value pair (if no key with the same value existed).\n' |
| 206 | '\n' |
| 207 | '* If the target is a slicing: The primary expression in the\n' |
| 208 | ' reference is evaluated. It should yield a mutable sequence ' |
| 209 | 'object\n' |
| 210 | ' (such as a list). The assigned object should be a sequence ' |
| 211 | 'object\n' |
| 212 | ' of the same type. Next, the lower and upper bound ' |
| 213 | 'expressions are\n' |
| 214 | ' evaluated, insofar they are present; defaults are zero and ' |
| 215 | 'the\n' |
| 216 | " sequence's length. The bounds should evaluate to (small) " |
| 217 | 'integers.\n' |
| 218 | " If either bound is negative, the sequence's length is added " |
| 219 | 'to it.\n' |
| 220 | ' The resulting bounds are clipped to lie between zero and ' |
| 221 | 'the\n' |
| 222 | " sequence's length, inclusive. Finally, the sequence object " |
| 223 | 'is asked\n' |
| 224 | ' to replace the slice with the items of the assigned ' |
| 225 | 'sequence. The\n' |
| 226 | ' length of the slice may be different from the length of the ' |
| 227 | 'assigned\n' |
| 228 | ' sequence, thus changing the length of the target sequence, ' |
| 229 | 'if the\n' |
| 230 | ' object allows it.\n' |
| 231 | '\n' |
| 232 | '**CPython implementation detail:** In the current ' |
| 233 | 'implementation, the\n' |
| 234 | 'syntax for targets is taken to be the same as for expressions, ' |
| 235 | 'and\n' |
| 236 | 'invalid syntax is rejected during the code generation phase, ' |
| 237 | 'causing\n' |
| 238 | 'less detailed error messages.\n' |
| 239 | '\n' |
| 240 | 'WARNING: Although the definition of assignment implies that ' |
| 241 | 'overlaps\n' |
| 242 | "between the left-hand side and the right-hand side are 'safe' " |
| 243 | '(for\n' |
| 244 | 'example "a, b = b, a" swaps two variables), overlaps *within* ' |
| 245 | 'the\n' |
| 246 | 'collection of assigned-to variables are not safe! For ' |
| 247 | 'instance, the\n' |
| 248 | 'following program prints "[0, 2]":\n' |
| 249 | '\n' |
| 250 | ' x = [0, 1]\n' |
| 251 | ' i = 0\n' |
| 252 | ' i, x[i] = 1, 2\n' |
| 253 | ' print x\n' |
| 254 | '\n' |
| 255 | '\n' |
| 256 | 'Augmented assignment statements\n' |
| 257 | '===============================\n' |
| 258 | '\n' |
| 259 | 'Augmented assignment is the combination, in a single ' |
| 260 | 'statement, of a\n' |
| 261 | 'binary operation and an assignment statement:\n' |
| 262 | '\n' |
| 263 | ' augmented_assignment_stmt ::= augtarget augop ' |
| 264 | '(expression_list | yield_expression)\n' |
| 265 | ' augtarget ::= identifier | attributeref | ' |
| 266 | 'subscription | slicing\n' |
| 267 | ' augop ::= "+=" | "-=" | "*=" | "/=" | ' |
| 268 | '"//=" | "%=" | "**="\n' |
| 269 | ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n' |
| 270 | '\n' |
| 271 | '(See section Primaries for the syntax definitions for the last ' |
| 272 | 'three\n' |
| 273 | 'symbols.)\n' |
| 274 | '\n' |
| 275 | 'An augmented assignment evaluates the target (which, unlike ' |
| 276 | 'normal\n' |
| 277 | 'assignment statements, cannot be an unpacking) and the ' |
| 278 | 'expression\n' |
| 279 | 'list, performs the binary operation specific to the type of ' |
| 280 | 'assignment\n' |
| 281 | 'on the two operands, and assigns the result to the original ' |
| 282 | 'target.\n' |
| 283 | 'The target is only evaluated once.\n' |
| 284 | '\n' |
| 285 | 'An augmented assignment expression like "x += 1" can be ' |
| 286 | 'rewritten as\n' |
| 287 | '"x = x + 1" to achieve a similar, but not exactly equal ' |
| 288 | 'effect. In the\n' |
| 289 | 'augmented version, "x" is only evaluated once. Also, when ' |
| 290 | 'possible,\n' |
| 291 | 'the actual operation is performed *in-place*, meaning that ' |
| 292 | 'rather than\n' |
| 293 | 'creating a new object and assigning that to the target, the ' |
| 294 | 'old object\n' |
| 295 | 'is modified instead.\n' |
| 296 | '\n' |
| 297 | 'With the exception of assigning to tuples and multiple targets ' |
| 298 | 'in a\n' |
| 299 | 'single statement, the assignment done by augmented assignment\n' |
| 300 | 'statements is handled the same way as normal assignments. ' |
| 301 | 'Similarly,\n' |
| 302 | 'with the exception of the possible *in-place* behavior, the ' |
| 303 | 'binary\n' |
| 304 | 'operation performed by augmented assignment is the same as the ' |
| 305 | 'normal\n' |
| 306 | 'binary operations.\n' |
| 307 | '\n' |
| 308 | 'For targets which are attribute references, the same caveat ' |
| 309 | 'about\n' |
| 310 | 'class and instance attributes applies as for regular ' |
| 311 | 'assignments.\n', |
| 312 | 'atom-identifiers': '\n' |
| 313 | 'Identifiers (Names)\n' |
| 314 | '*******************\n' |
| 315 | '\n' |
| 316 | 'An identifier occurring as an atom is a name. See ' |
| 317 | 'section Identifiers\n' |
| 318 | 'and keywords for lexical definition and section Naming ' |
| 319 | 'and binding for\n' |
| 320 | 'documentation of naming and binding.\n' |
| 321 | '\n' |
| 322 | 'When the name is bound to an object, evaluation of the ' |
| 323 | 'atom yields\n' |
| 324 | 'that object. When a name is not bound, an attempt to ' |
| 325 | 'evaluate it\n' |
| 326 | 'raises a "NameError" exception.\n' |
| 327 | '\n' |
| 328 | '**Private name mangling:** When an identifier that ' |
| 329 | 'textually occurs in\n' |
| 330 | 'a class definition begins with two or more underscore ' |
| 331 | 'characters and\n' |
| 332 | 'does not end in two or more underscores, it is ' |
| 333 | 'considered a *private\n' |
| 334 | 'name* of that class. Private names are transformed to a ' |
| 335 | 'longer form\n' |
| 336 | 'before code is generated for them. The transformation ' |
| 337 | 'inserts the\n' |
| 338 | 'class name, with leading underscores removed and a ' |
| 339 | 'single underscore\n' |
| 340 | 'inserted, in front of the name. For example, the ' |
| 341 | 'identifier "__spam"\n' |
| 342 | 'occurring in a class named "Ham" will be transformed to ' |
| 343 | '"_Ham__spam".\n' |
| 344 | 'This transformation is independent of the syntactical ' |
| 345 | 'context in which\n' |
| 346 | 'the identifier is used. If the transformed name is ' |
| 347 | 'extremely long\n' |
| 348 | '(longer than 255 characters), implementation defined ' |
| 349 | 'truncation may\n' |
| 350 | 'happen. If the class name consists only of underscores, ' |
| 351 | 'no\n' |
| 352 | 'transformation is done.\n', |
| 353 | 'atom-literals': '\n' |
| 354 | 'Literals\n' |
| 355 | '********\n' |
| 356 | '\n' |
| 357 | 'Python supports string literals and various numeric ' |
| 358 | 'literals:\n' |
| 359 | '\n' |
| 360 | ' literal ::= stringliteral | integer | longinteger\n' |
| 361 | ' | floatnumber | imagnumber\n' |
| 362 | '\n' |
| 363 | 'Evaluation of a literal yields an object of the given type ' |
| 364 | '(string,\n' |
| 365 | 'integer, long integer, floating point number, complex ' |
| 366 | 'number) with the\n' |
| 367 | 'given value. The value may be approximated in the case of ' |
| 368 | 'floating\n' |
| 369 | 'point and imaginary (complex) literals. See section ' |
| 370 | 'Literals for\n' |
| 371 | 'details.\n' |
| 372 | '\n' |
| 373 | 'All literals correspond to immutable data types, and hence ' |
| 374 | 'the\n' |
| 375 | "object's identity is less important than its value. " |
| 376 | 'Multiple\n' |
| 377 | 'evaluations of literals with the same value (either the ' |
| 378 | 'same\n' |
| 379 | 'occurrence in the program text or a different occurrence) ' |
| 380 | 'may obtain\n' |
| 381 | 'the same object or a different object with the same ' |
| 382 | 'value.\n', |
| 383 | 'attribute-access': '\n' |
| 384 | 'Customizing attribute access\n' |
| 385 | '****************************\n' |
| 386 | '\n' |
| 387 | 'The following methods can be defined to customize the ' |
| 388 | 'meaning of\n' |
| 389 | 'attribute access (use of, assignment to, or deletion of ' |
| 390 | '"x.name") for\n' |
| 391 | 'class instances.\n' |
| 392 | '\n' |
| 393 | 'object.__getattr__(self, name)\n' |
| 394 | '\n' |
| 395 | ' Called when an attribute lookup has not found the ' |
| 396 | 'attribute in the\n' |
| 397 | ' usual places (i.e. it is not an instance attribute ' |
| 398 | 'nor is it found\n' |
| 399 | ' in the class tree for "self"). "name" is the ' |
| 400 | 'attribute name. This\n' |
| 401 | ' method should return the (computed) attribute value ' |
| 402 | 'or raise an\n' |
| 403 | ' "AttributeError" exception.\n' |
| 404 | '\n' |
| 405 | ' Note that if the attribute is found through the ' |
| 406 | 'normal mechanism,\n' |
| 407 | ' "__getattr__()" is not called. (This is an ' |
| 408 | 'intentional asymmetry\n' |
| 409 | ' between "__getattr__()" and "__setattr__()".) This is ' |
| 410 | 'done both for\n' |
| 411 | ' efficiency reasons and because otherwise ' |
| 412 | '"__getattr__()" would have\n' |
| 413 | ' no way to access other attributes of the instance. ' |
| 414 | 'Note that at\n' |
| 415 | ' least for instance variables, you can fake total ' |
| 416 | 'control by not\n' |
| 417 | ' inserting any values in the instance attribute ' |
| 418 | 'dictionary (but\n' |
| 419 | ' instead inserting them in another object). See the\n' |
| 420 | ' "__getattribute__()" method below for a way to ' |
| 421 | 'actually get total\n' |
| 422 | ' control in new-style classes.\n' |
| 423 | '\n' |
| 424 | 'object.__setattr__(self, name, value)\n' |
| 425 | '\n' |
| 426 | ' Called when an attribute assignment is attempted. ' |
| 427 | 'This is called\n' |
| 428 | ' instead of the normal mechanism (i.e. store the value ' |
| 429 | 'in the\n' |
| 430 | ' instance dictionary). *name* is the attribute name, ' |
| 431 | '*value* is the\n' |
| 432 | ' value to be assigned to it.\n' |
| 433 | '\n' |
| 434 | ' If "__setattr__()" wants to assign to an instance ' |
| 435 | 'attribute, it\n' |
| 436 | ' should not simply execute "self.name = value" --- ' |
| 437 | 'this would cause\n' |
| 438 | ' a recursive call to itself. Instead, it should ' |
| 439 | 'insert the value in\n' |
| 440 | ' the dictionary of instance attributes, e.g., ' |
| 441 | '"self.__dict__[name] =\n' |
| 442 | ' value". For new-style classes, rather than accessing ' |
| 443 | 'the instance\n' |
| 444 | ' dictionary, it should call the base class method with ' |
| 445 | 'the same\n' |
| 446 | ' name, for example, "object.__setattr__(self, name, ' |
| 447 | 'value)".\n' |
| 448 | '\n' |
| 449 | 'object.__delattr__(self, name)\n' |
| 450 | '\n' |
| 451 | ' Like "__setattr__()" but for attribute deletion ' |
| 452 | 'instead of\n' |
| 453 | ' assignment. This should only be implemented if "del ' |
| 454 | 'obj.name" is\n' |
| 455 | ' meaningful for the object.\n' |
| 456 | '\n' |
| 457 | '\n' |
| 458 | 'More attribute access for new-style classes\n' |
| 459 | '===========================================\n' |
| 460 | '\n' |
| 461 | 'The following methods only apply to new-style classes.\n' |
| 462 | '\n' |
| 463 | 'object.__getattribute__(self, name)\n' |
| 464 | '\n' |
| 465 | ' Called unconditionally to implement attribute ' |
| 466 | 'accesses for\n' |
| 467 | ' instances of the class. If the class also defines ' |
| 468 | '"__getattr__()",\n' |
| 469 | ' the latter will not be called unless ' |
| 470 | '"__getattribute__()" either\n' |
| 471 | ' calls it explicitly or raises an "AttributeError". ' |
| 472 | 'This method\n' |
| 473 | ' should return the (computed) attribute value or raise ' |
| 474 | 'an\n' |
| 475 | ' "AttributeError" exception. In order to avoid ' |
| 476 | 'infinite recursion in\n' |
| 477 | ' this method, its implementation should always call ' |
| 478 | 'the base class\n' |
| 479 | ' method with the same name to access any attributes it ' |
| 480 | 'needs, for\n' |
| 481 | ' example, "object.__getattribute__(self, name)".\n' |
| 482 | '\n' |
| 483 | ' Note: This method may still be bypassed when looking ' |
| 484 | 'up special\n' |
| 485 | ' methods as the result of implicit invocation via ' |
| 486 | 'language syntax\n' |
| 487 | ' or built-in functions. See Special method lookup ' |
| 488 | 'for new-style\n' |
| 489 | ' classes.\n' |
| 490 | '\n' |
| 491 | '\n' |
| 492 | 'Implementing Descriptors\n' |
| 493 | '========================\n' |
| 494 | '\n' |
| 495 | 'The following methods only apply when an instance of the ' |
| 496 | 'class\n' |
| 497 | 'containing the method (a so-called *descriptor* class) ' |
| 498 | 'appears in an\n' |
| 499 | '*owner* class (the descriptor must be in either the ' |
| 500 | "owner's class\n" |
| 501 | 'dictionary or in the class dictionary for one of its ' |
| 502 | 'parents). In the\n' |
| 503 | 'examples below, "the attribute" refers to the attribute ' |
| 504 | 'whose name is\n' |
| 505 | "the key of the property in the owner class' " |
| 506 | '"__dict__".\n' |
| 507 | '\n' |
| 508 | 'object.__get__(self, instance, owner)\n' |
| 509 | '\n' |
| 510 | ' Called to get the attribute of the owner class (class ' |
| 511 | 'attribute\n' |
| 512 | ' access) or of an instance of that class (instance ' |
| 513 | 'attribute\n' |
| 514 | ' access). *owner* is always the owner class, while ' |
| 515 | '*instance* is the\n' |
| 516 | ' instance that the attribute was accessed through, or ' |
| 517 | '"None" when\n' |
| 518 | ' the attribute is accessed through the *owner*. This ' |
| 519 | 'method should\n' |
| 520 | ' return the (computed) attribute value or raise an ' |
| 521 | '"AttributeError"\n' |
| 522 | ' exception.\n' |
| 523 | '\n' |
| 524 | 'object.__set__(self, instance, value)\n' |
| 525 | '\n' |
| 526 | ' Called to set the attribute on an instance *instance* ' |
| 527 | 'of the owner\n' |
| 528 | ' class to a new value, *value*.\n' |
| 529 | '\n' |
| 530 | 'object.__delete__(self, instance)\n' |
| 531 | '\n' |
| 532 | ' Called to delete the attribute on an instance ' |
| 533 | '*instance* of the\n' |
| 534 | ' owner class.\n' |
| 535 | '\n' |
| 536 | '\n' |
| 537 | 'Invoking Descriptors\n' |
| 538 | '====================\n' |
| 539 | '\n' |
| 540 | 'In general, a descriptor is an object attribute with ' |
| 541 | '"binding\n' |
| 542 | 'behavior", one whose attribute access has been ' |
| 543 | 'overridden by methods\n' |
| 544 | 'in the descriptor protocol: "__get__()", "__set__()", ' |
| 545 | 'and\n' |
| 546 | '"__delete__()". If any of those methods are defined for ' |
| 547 | 'an object, it\n' |
| 548 | 'is said to be a descriptor.\n' |
| 549 | '\n' |
| 550 | 'The default behavior for attribute access is to get, ' |
| 551 | 'set, or delete\n' |
| 552 | "the attribute from an object's dictionary. For instance, " |
| 553 | '"a.x" has a\n' |
| 554 | 'lookup chain starting with "a.__dict__[\'x\']", then\n' |
| 555 | '"type(a).__dict__[\'x\']", and continuing through the ' |
| 556 | 'base classes of\n' |
| 557 | '"type(a)" excluding metaclasses.\n' |
| 558 | '\n' |
| 559 | 'However, if the looked-up value is an object defining ' |
| 560 | 'one of the\n' |
| 561 | 'descriptor methods, then Python may override the default ' |
| 562 | 'behavior and\n' |
| 563 | 'invoke the descriptor method instead. Where this occurs ' |
| 564 | 'in the\n' |
| 565 | 'precedence chain depends on which descriptor methods ' |
| 566 | 'were defined and\n' |
| 567 | 'how they were called. Note that descriptors are only ' |
| 568 | 'invoked for new\n' |
| 569 | 'style objects or classes (ones that subclass "object()" ' |
| 570 | 'or "type()").\n' |
| 571 | '\n' |
| 572 | 'The starting point for descriptor invocation is a ' |
| 573 | 'binding, "a.x". How\n' |
| 574 | 'the arguments are assembled depends on "a":\n' |
| 575 | '\n' |
| 576 | 'Direct Call\n' |
| 577 | ' The simplest and least common call is when user code ' |
| 578 | 'directly\n' |
| 579 | ' invokes a descriptor method: "x.__get__(a)".\n' |
| 580 | '\n' |
| 581 | 'Instance Binding\n' |
| 582 | ' If binding to a new-style object instance, "a.x" is ' |
| 583 | 'transformed\n' |
| 584 | ' into the call: "type(a).__dict__[\'x\'].__get__(a, ' |
| 585 | 'type(a))".\n' |
| 586 | '\n' |
| 587 | 'Class Binding\n' |
| 588 | ' If binding to a new-style class, "A.x" is transformed ' |
| 589 | 'into the\n' |
| 590 | ' call: "A.__dict__[\'x\'].__get__(None, A)".\n' |
| 591 | '\n' |
| 592 | 'Super Binding\n' |
| 593 | ' If "a" is an instance of "super", then the binding ' |
| 594 | '"super(B,\n' |
| 595 | ' obj).m()" searches "obj.__class__.__mro__" for the ' |
| 596 | 'base class "A"\n' |
| 597 | ' immediately preceding "B" and then invokes the ' |
| 598 | 'descriptor with the\n' |
| 599 | ' call: "A.__dict__[\'m\'].__get__(obj, ' |
| 600 | 'obj.__class__)".\n' |
| 601 | '\n' |
| 602 | 'For instance bindings, the precedence of descriptor ' |
| 603 | 'invocation depends\n' |
| 604 | 'on the which descriptor methods are defined. A ' |
| 605 | 'descriptor can define\n' |
| 606 | 'any combination of "__get__()", "__set__()" and ' |
| 607 | '"__delete__()". If it\n' |
| 608 | 'does not define "__get__()", then accessing the ' |
| 609 | 'attribute will return\n' |
| 610 | 'the descriptor object itself unless there is a value in ' |
| 611 | "the object's\n" |
| 612 | 'instance dictionary. If the descriptor defines ' |
| 613 | '"__set__()" and/or\n' |
| 614 | '"__delete__()", it is a data descriptor; if it defines ' |
| 615 | 'neither, it is\n' |
| 616 | 'a non-data descriptor. Normally, data descriptors ' |
| 617 | 'define both\n' |
| 618 | '"__get__()" and "__set__()", while non-data descriptors ' |
| 619 | 'have just the\n' |
| 620 | '"__get__()" method. Data descriptors with "__set__()" ' |
| 621 | 'and "__get__()"\n' |
| 622 | 'defined always override a redefinition in an instance ' |
| 623 | 'dictionary. In\n' |
| 624 | 'contrast, non-data descriptors can be overridden by ' |
| 625 | 'instances.\n' |
| 626 | '\n' |
| 627 | 'Python methods (including "staticmethod()" and ' |
| 628 | '"classmethod()") are\n' |
| 629 | 'implemented as non-data descriptors. Accordingly, ' |
| 630 | 'instances can\n' |
| 631 | 'redefine and override methods. This allows individual ' |
| 632 | 'instances to\n' |
| 633 | 'acquire behaviors that differ from other instances of ' |
| 634 | 'the same class.\n' |
| 635 | '\n' |
| 636 | 'The "property()" function is implemented as a data ' |
| 637 | 'descriptor.\n' |
| 638 | 'Accordingly, instances cannot override the behavior of a ' |
| 639 | 'property.\n' |
| 640 | '\n' |
| 641 | '\n' |
| 642 | '__slots__\n' |
| 643 | '=========\n' |
| 644 | '\n' |
| 645 | 'By default, instances of both old and new-style classes ' |
| 646 | 'have a\n' |
| 647 | 'dictionary for attribute storage. This wastes space for ' |
| 648 | 'objects\n' |
| 649 | 'having very few instance variables. The space ' |
| 650 | 'consumption can become\n' |
| 651 | 'acute when creating large numbers of instances.\n' |
| 652 | '\n' |
| 653 | 'The default can be overridden by defining *__slots__* in ' |
| 654 | 'a new-style\n' |
| 655 | 'class definition. The *__slots__* declaration takes a ' |
| 656 | 'sequence of\n' |
| 657 | 'instance variables and reserves just enough space in ' |
| 658 | 'each instance to\n' |
| 659 | 'hold a value for each variable. Space is saved because ' |
| 660 | '*__dict__* is\n' |
| 661 | 'not created for each instance.\n' |
| 662 | '\n' |
| 663 | '__slots__\n' |
| 664 | '\n' |
| 665 | ' This class variable can be assigned a string, ' |
| 666 | 'iterable, or sequence\n' |
| 667 | ' of strings with variable names used by instances. If ' |
| 668 | 'defined in a\n' |
| 669 | ' new-style class, *__slots__* reserves space for the ' |
| 670 | 'declared\n' |
| 671 | ' variables and prevents the automatic creation of ' |
| 672 | '*__dict__* and\n' |
| 673 | ' *__weakref__* for each instance.\n' |
| 674 | '\n' |
| 675 | ' New in version 2.2.\n' |
| 676 | '\n' |
| 677 | 'Notes on using *__slots__*\n' |
| 678 | '\n' |
| 679 | '* When inheriting from a class without *__slots__*, the ' |
| 680 | '*__dict__*\n' |
| 681 | ' attribute of that class will always be accessible, so ' |
| 682 | 'a *__slots__*\n' |
| 683 | ' definition in the subclass is meaningless.\n' |
| 684 | '\n' |
| 685 | '* Without a *__dict__* variable, instances cannot be ' |
| 686 | 'assigned new\n' |
| 687 | ' variables not listed in the *__slots__* definition. ' |
| 688 | 'Attempts to\n' |
| 689 | ' assign to an unlisted variable name raises ' |
| 690 | '"AttributeError". If\n' |
| 691 | ' dynamic assignment of new variables is desired, then ' |
| 692 | 'add\n' |
| 693 | ' "\'__dict__\'" to the sequence of strings in the ' |
| 694 | '*__slots__*\n' |
| 695 | ' declaration.\n' |
| 696 | '\n' |
| 697 | ' Changed in version 2.3: Previously, adding ' |
| 698 | '"\'__dict__\'" to the\n' |
| 699 | ' *__slots__* declaration would not enable the ' |
| 700 | 'assignment of new\n' |
| 701 | ' attributes not specifically listed in the sequence of ' |
| 702 | 'instance\n' |
| 703 | ' variable names.\n' |
| 704 | '\n' |
| 705 | '* Without a *__weakref__* variable for each instance, ' |
| 706 | 'classes\n' |
| 707 | ' defining *__slots__* do not support weak references to ' |
| 708 | 'its\n' |
| 709 | ' instances. If weak reference support is needed, then ' |
| 710 | 'add\n' |
| 711 | ' "\'__weakref__\'" to the sequence of strings in the ' |
| 712 | '*__slots__*\n' |
| 713 | ' declaration.\n' |
| 714 | '\n' |
| 715 | ' Changed in version 2.3: Previously, adding ' |
| 716 | '"\'__weakref__\'" to the\n' |
| 717 | ' *__slots__* declaration would not enable support for ' |
| 718 | 'weak\n' |
| 719 | ' references.\n' |
| 720 | '\n' |
| 721 | '* *__slots__* are implemented at the class level by ' |
| 722 | 'creating\n' |
| 723 | ' descriptors (Implementing Descriptors) for each ' |
| 724 | 'variable name. As a\n' |
| 725 | ' result, class attributes cannot be used to set default ' |
| 726 | 'values for\n' |
| 727 | ' instance variables defined by *__slots__*; otherwise, ' |
| 728 | 'the class\n' |
| 729 | ' attribute would overwrite the descriptor assignment.\n' |
| 730 | '\n' |
| 731 | '* The action of a *__slots__* declaration is limited to ' |
| 732 | 'the class\n' |
| 733 | ' where it is defined. As a result, subclasses will ' |
| 734 | 'have a *__dict__*\n' |
| 735 | ' unless they also define *__slots__* (which must only ' |
| 736 | 'contain names\n' |
| 737 | ' of any *additional* slots).\n' |
| 738 | '\n' |
| 739 | '* If a class defines a slot also defined in a base ' |
| 740 | 'class, the\n' |
| 741 | ' instance variable defined by the base class slot is ' |
| 742 | 'inaccessible\n' |
| 743 | ' (except by retrieving its descriptor directly from the ' |
| 744 | 'base class).\n' |
| 745 | ' This renders the meaning of the program undefined. In ' |
| 746 | 'the future, a\n' |
| 747 | ' check may be added to prevent this.\n' |
| 748 | '\n' |
| 749 | '* Nonempty *__slots__* does not work for classes derived ' |
| 750 | 'from\n' |
| 751 | ' "variable-length" built-in types such as "long", "str" ' |
| 752 | 'and "tuple".\n' |
| 753 | '\n' |
| 754 | '* Any non-string iterable may be assigned to ' |
| 755 | '*__slots__*. Mappings\n' |
| 756 | ' may also be used; however, in the future, special ' |
| 757 | 'meaning may be\n' |
| 758 | ' assigned to the values corresponding to each key.\n' |
| 759 | '\n' |
| 760 | '* *__class__* assignment works only if both classes have ' |
| 761 | 'the same\n' |
| 762 | ' *__slots__*.\n' |
| 763 | '\n' |
| 764 | ' Changed in version 2.6: Previously, *__class__* ' |
| 765 | 'assignment raised an\n' |
| 766 | ' error if either new or old class had *__slots__*.\n', |
| 767 | 'attribute-references': '\n' |
| 768 | 'Attribute references\n' |
| 769 | '********************\n' |
| 770 | '\n' |
| 771 | 'An attribute reference is a primary followed by a ' |
| 772 | 'period and a name:\n' |
| 773 | '\n' |
| 774 | ' attributeref ::= primary "." identifier\n' |
| 775 | '\n' |
| 776 | 'The primary must evaluate to an object of a type ' |
| 777 | 'that supports\n' |
| 778 | 'attribute references, e.g., a module, list, or an ' |
| 779 | 'instance. This\n' |
| 780 | 'object is then asked to produce the attribute whose ' |
| 781 | 'name is the\n' |
| 782 | 'identifier. If this attribute is not available, the ' |
| 783 | 'exception\n' |
| 784 | '"AttributeError" is raised. Otherwise, the type and ' |
| 785 | 'value of the\n' |
| 786 | 'object produced is determined by the object. ' |
| 787 | 'Multiple evaluations of\n' |
| 788 | 'the same attribute reference may yield different ' |
| 789 | 'objects.\n', |
| 790 | 'augassign': '\n' |
| 791 | 'Augmented assignment statements\n' |
| 792 | '*******************************\n' |
| 793 | '\n' |
| 794 | 'Augmented assignment is the combination, in a single statement, ' |
| 795 | 'of a\n' |
| 796 | 'binary operation and an assignment statement:\n' |
| 797 | '\n' |
| 798 | ' augmented_assignment_stmt ::= augtarget augop ' |
| 799 | '(expression_list | yield_expression)\n' |
| 800 | ' augtarget ::= identifier | attributeref | ' |
| 801 | 'subscription | slicing\n' |
| 802 | ' augop ::= "+=" | "-=" | "*=" | "/=" | ' |
| 803 | '"//=" | "%=" | "**="\n' |
| 804 | ' | ">>=" | "<<=" | "&=" | "^=" | "|="\n' |
| 805 | '\n' |
| 806 | '(See section Primaries for the syntax definitions for the last ' |
| 807 | 'three\n' |
| 808 | 'symbols.)\n' |
| 809 | '\n' |
| 810 | 'An augmented assignment evaluates the target (which, unlike ' |
| 811 | 'normal\n' |
| 812 | 'assignment statements, cannot be an unpacking) and the ' |
| 813 | 'expression\n' |
| 814 | 'list, performs the binary operation specific to the type of ' |
| 815 | 'assignment\n' |
| 816 | 'on the two operands, and assigns the result to the original ' |
| 817 | 'target.\n' |
| 818 | 'The target is only evaluated once.\n' |
| 819 | '\n' |
| 820 | 'An augmented assignment expression like "x += 1" can be ' |
| 821 | 'rewritten as\n' |
| 822 | '"x = x + 1" to achieve a similar, but not exactly equal effect. ' |
| 823 | 'In the\n' |
| 824 | 'augmented version, "x" is only evaluated once. Also, when ' |
| 825 | 'possible,\n' |
| 826 | 'the actual operation is performed *in-place*, meaning that ' |
| 827 | 'rather than\n' |
| 828 | 'creating a new object and assigning that to the target, the old ' |
| 829 | 'object\n' |
| 830 | 'is modified instead.\n' |
| 831 | '\n' |
| 832 | 'With the exception of assigning to tuples and multiple targets ' |
| 833 | 'in a\n' |
| 834 | 'single statement, the assignment done by augmented assignment\n' |
| 835 | 'statements is handled the same way as normal assignments. ' |
| 836 | 'Similarly,\n' |
| 837 | 'with the exception of the possible *in-place* behavior, the ' |
| 838 | 'binary\n' |
| 839 | 'operation performed by augmented assignment is the same as the ' |
| 840 | 'normal\n' |
| 841 | 'binary operations.\n' |
| 842 | '\n' |
| 843 | 'For targets which are attribute references, the same caveat ' |
| 844 | 'about\n' |
| 845 | 'class and instance attributes applies as for regular ' |
| 846 | 'assignments.\n', |
| 847 | 'binary': '\n' |
| 848 | 'Binary arithmetic operations\n' |
| 849 | '****************************\n' |
| 850 | '\n' |
| 851 | 'The binary arithmetic operations have the conventional priority\n' |
| 852 | 'levels. Note that some of these operations also apply to certain ' |
| 853 | 'non-\n' |
| 854 | 'numeric types. Apart from the power operator, there are only two\n' |
| 855 | 'levels, one for multiplicative operators and one for additive\n' |
| 856 | 'operators:\n' |
| 857 | '\n' |
| 858 | ' m_expr ::= u_expr | m_expr "*" u_expr | m_expr "//" u_expr | ' |
| 859 | 'm_expr "/" u_expr\n' |
| 860 | ' | m_expr "%" u_expr\n' |
| 861 | ' a_expr ::= m_expr | a_expr "+" m_expr | a_expr "-" m_expr\n' |
| 862 | '\n' |
| 863 | 'The "*" (multiplication) operator yields the product of its ' |
| 864 | 'arguments.\n' |
| 865 | 'The arguments must either both be numbers, or one argument must be ' |
| 866 | 'an\n' |
| 867 | 'integer (plain or long) and the other must be a sequence. In the\n' |
| 868 | 'former case, the numbers are converted to a common type and then\n' |
| 869 | 'multiplied together. In the latter case, sequence repetition is\n' |
| 870 | 'performed; a negative repetition factor yields an empty sequence.\n' |
| 871 | '\n' |
| 872 | 'The "/" (division) and "//" (floor division) operators yield the\n' |
| 873 | 'quotient of their arguments. The numeric arguments are first\n' |
| 874 | 'converted to a common type. Plain or long integer division yields ' |
| 875 | 'an\n' |
| 876 | 'integer of the same type; the result is that of mathematical ' |
| 877 | 'division\n' |
| 878 | "with the 'floor' function applied to the result. Division by zero\n" |
| 879 | 'raises the "ZeroDivisionError" exception.\n' |
| 880 | '\n' |
| 881 | 'The "%" (modulo) operator yields the remainder from the division ' |
| 882 | 'of\n' |
| 883 | 'the first argument by the second. The numeric arguments are ' |
| 884 | 'first\n' |
| 885 | 'converted to a common type. A zero right argument raises the\n' |
| 886 | '"ZeroDivisionError" exception. The arguments may be floating ' |
| 887 | 'point\n' |
| 888 | 'numbers, e.g., "3.14%0.7" equals "0.34" (since "3.14" equals ' |
| 889 | '"4*0.7 +\n' |
| 890 | '0.34".) The modulo operator always yields a result with the same ' |
| 891 | 'sign\n' |
| 892 | 'as its second operand (or zero); the absolute value of the result ' |
| 893 | 'is\n' |
| 894 | 'strictly smaller than the absolute value of the second operand ' |
| 895 | '[2].\n' |
| 896 | '\n' |
| 897 | 'The integer division and modulo operators are connected by the\n' |
| 898 | 'following identity: "x == (x/y)*y + (x%y)". Integer division and\n' |
| 899 | 'modulo are also connected with the built-in function "divmod()":\n' |
| 900 | '"divmod(x, y) == (x/y, x%y)". These identities don\'t hold for\n' |
| 901 | 'floating point numbers; there similar identities hold ' |
| 902 | 'approximately\n' |
| 903 | 'where "x/y" is replaced by "floor(x/y)" or "floor(x/y) - 1" [3].\n' |
| 904 | '\n' |
| 905 | 'In addition to performing the modulo operation on numbers, the ' |
| 906 | '"%"\n' |
| 907 | 'operator is also overloaded by string and unicode objects to ' |
| 908 | 'perform\n' |
| 909 | 'string formatting (also known as interpolation). The syntax for ' |
| 910 | 'string\n' |
| 911 | 'formatting is described in the Python Library Reference, section\n' |
| 912 | 'String Formatting Operations.\n' |
| 913 | '\n' |
| 914 | 'Deprecated since version 2.3: The floor division operator, the ' |
| 915 | 'modulo\n' |
| 916 | 'operator, and the "divmod()" function are no longer defined for\n' |
| 917 | 'complex numbers. Instead, convert to a floating point number ' |
| 918 | 'using\n' |
| 919 | 'the "abs()" function if appropriate.\n' |
| 920 | '\n' |
| 921 | 'The "+" (addition) operator yields the sum of its arguments. The\n' |
| 922 | 'arguments must either both be numbers or both sequences of the ' |
| 923 | 'same\n' |
| 924 | 'type. In the former case, the numbers are converted to a common ' |
| 925 | 'type\n' |
| 926 | 'and then added together. In the latter case, the sequences are\n' |
| 927 | 'concatenated.\n' |
| 928 | '\n' |
| 929 | 'The "-" (subtraction) operator yields the difference of its ' |
| 930 | 'arguments.\n' |
| 931 | 'The numeric arguments are first converted to a common type.\n', |
| 932 | 'bitwise': '\n' |
| 933 | 'Binary bitwise operations\n' |
| 934 | '*************************\n' |
| 935 | '\n' |
| 936 | 'Each of the three bitwise operations has a different priority ' |
| 937 | 'level:\n' |
| 938 | '\n' |
| 939 | ' and_expr ::= shift_expr | and_expr "&" shift_expr\n' |
| 940 | ' xor_expr ::= and_expr | xor_expr "^" and_expr\n' |
| 941 | ' or_expr ::= xor_expr | or_expr "|" xor_expr\n' |
| 942 | '\n' |
| 943 | 'The "&" operator yields the bitwise AND of its arguments, which ' |
| 944 | 'must\n' |
| 945 | 'be plain or long integers. The arguments are converted to a ' |
| 946 | 'common\n' |
| 947 | 'type.\n' |
| 948 | '\n' |
| 949 | 'The "^" operator yields the bitwise XOR (exclusive OR) of its\n' |
| 950 | 'arguments, which must be plain or long integers. The arguments ' |
| 951 | 'are\n' |
| 952 | 'converted to a common type.\n' |
| 953 | '\n' |
| 954 | 'The "|" operator yields the bitwise (inclusive) OR of its ' |
| 955 | 'arguments,\n' |
| 956 | 'which must be plain or long integers. The arguments are ' |
| 957 | 'converted to\n' |
| 958 | 'a common type.\n', |
| 959 | 'bltin-code-objects': '\n' |
| 960 | 'Code Objects\n' |
| 961 | '************\n' |
| 962 | '\n' |
| 963 | 'Code objects are used by the implementation to ' |
| 964 | 'represent "pseudo-\n' |
| 965 | 'compiled" executable Python code such as a function ' |
| 966 | 'body. They differ\n' |
| 967 | "from function objects because they don't contain a " |
| 968 | 'reference to their\n' |
| 969 | 'global execution environment. Code objects are ' |
| 970 | 'returned by the built-\n' |
| 971 | 'in "compile()" function and can be extracted from ' |
| 972 | 'function objects\n' |
| 973 | 'through their "func_code" attribute. See also the ' |
| 974 | '"code" module.\n' |
| 975 | '\n' |
| 976 | 'A code object can be executed or evaluated by passing ' |
| 977 | 'it (instead of a\n' |
| 978 | 'source string) to the "exec" statement or the built-in ' |
| 979 | '"eval()"\n' |
| 980 | 'function.\n' |
| 981 | '\n' |
| 982 | 'See The standard type hierarchy for more ' |
| 983 | 'information.\n', |
| 984 | 'bltin-ellipsis-object': '\n' |
| 985 | 'The Ellipsis Object\n' |
| 986 | '*******************\n' |
| 987 | '\n' |
| 988 | 'This object is used by extended slice notation (see ' |
| 989 | 'Slicings). It\n' |
| 990 | 'supports no special operations. There is exactly ' |
| 991 | 'one ellipsis object,\n' |
| 992 | 'named "Ellipsis" (a built-in name).\n' |
| 993 | '\n' |
| 994 | 'It is written as "Ellipsis". When in a subscript, ' |
| 995 | 'it can also be\n' |
| 996 | 'written as "...", for example "seq[...]".\n', |
| 997 | 'bltin-file-objects': '\n' |
| 998 | 'File Objects\n' |
| 999 | '************\n' |
| 1000 | '\n' |
| 1001 | 'File objects are implemented using C\'s "stdio" ' |
| 1002 | 'package and can be\n' |
| 1003 | 'created with the built-in "open()" function. File ' |
| 1004 | 'objects are also\n' |
| 1005 | 'returned by some other built-in functions and methods, ' |
| 1006 | 'such as\n' |
| 1007 | '"os.popen()" and "os.fdopen()" and the "makefile()" ' |
| 1008 | 'method of socket\n' |
| 1009 | 'objects. Temporary files can be created using the ' |
| 1010 | '"tempfile" module,\n' |
| 1011 | 'and high-level file operations such as copying, ' |
| 1012 | 'moving, and deleting\n' |
| 1013 | 'files and directories can be achieved with the ' |
| 1014 | '"shutil" module.\n' |
| 1015 | '\n' |
| 1016 | 'When a file operation fails for an I/O-related reason, ' |
| 1017 | 'the exception\n' |
| 1018 | '"IOError" is raised. This includes situations where ' |
| 1019 | 'the operation is\n' |
| 1020 | 'not defined for some reason, like "seek()" on a tty ' |
| 1021 | 'device or writing\n' |
| 1022 | 'a file opened for reading.\n' |
| 1023 | '\n' |
| 1024 | 'Files have the following methods:\n' |
| 1025 | '\n' |
| 1026 | 'file.close()\n' |
| 1027 | '\n' |
| 1028 | ' Close the file. A closed file cannot be read or ' |
| 1029 | 'written any more.\n' |
| 1030 | ' Any operation which requires that the file be open ' |
| 1031 | 'will raise a\n' |
| 1032 | ' "ValueError" after the file has been closed. ' |
| 1033 | 'Calling "close()"\n' |
| 1034 | ' more than once is allowed.\n' |
| 1035 | '\n' |
| 1036 | ' As of Python 2.5, you can avoid having to call this ' |
| 1037 | 'method\n' |
| 1038 | ' explicitly if you use the "with" statement. For ' |
| 1039 | 'example, the\n' |
| 1040 | ' following code will automatically close *f* when ' |
| 1041 | 'the "with" block\n' |
| 1042 | ' is exited:\n' |
| 1043 | '\n' |
| 1044 | ' from __future__ import with_statement # This ' |
| 1045 | "isn't required in Python 2.6\n" |
| 1046 | '\n' |
| 1047 | ' with open("hello.txt") as f:\n' |
| 1048 | ' for line in f:\n' |
| 1049 | ' print line,\n' |
| 1050 | '\n' |
| 1051 | ' In older versions of Python, you would have needed ' |
| 1052 | 'to do this to\n' |
| 1053 | ' get the same effect:\n' |
| 1054 | '\n' |
| 1055 | ' f = open("hello.txt")\n' |
| 1056 | ' try:\n' |
| 1057 | ' for line in f:\n' |
| 1058 | ' print line,\n' |
| 1059 | ' finally:\n' |
| 1060 | ' f.close()\n' |
| 1061 | '\n' |
| 1062 | ' Note: Not all "file-like" types in Python support ' |
| 1063 | 'use as a\n' |
| 1064 | ' context manager for the "with" statement. If ' |
| 1065 | 'your code is\n' |
| 1066 | ' intended to work with any file-like object, you ' |
| 1067 | 'can use the\n' |
| 1068 | ' function "contextlib.closing()" instead of using ' |
| 1069 | 'the object\n' |
| 1070 | ' directly.\n' |
| 1071 | '\n' |
| 1072 | 'file.flush()\n' |
| 1073 | '\n' |
| 1074 | ' Flush the internal buffer, like "stdio"\'s ' |
| 1075 | '"fflush()". This may be\n' |
| 1076 | ' a no-op on some file-like objects.\n' |
| 1077 | '\n' |
| 1078 | ' Note: "flush()" does not necessarily write the ' |
| 1079 | "file's data to\n" |
| 1080 | ' disk. Use "flush()" followed by "os.fsync()" to ' |
| 1081 | 'ensure this\n' |
| 1082 | ' behavior.\n' |
| 1083 | '\n' |
| 1084 | 'file.fileno()\n' |
| 1085 | '\n' |
| 1086 | ' Return the integer "file descriptor" that is used ' |
| 1087 | 'by the underlying\n' |
| 1088 | ' implementation to request I/O operations from the ' |
| 1089 | 'operating system.\n' |
| 1090 | ' This can be useful for other, lower level ' |
| 1091 | 'interfaces that use file\n' |
| 1092 | ' descriptors, such as the "fcntl" module or ' |
| 1093 | '"os.read()" and friends.\n' |
| 1094 | '\n' |
| 1095 | ' Note: File-like objects which do not have a real ' |
| 1096 | 'file descriptor\n' |
| 1097 | ' should *not* provide this method!\n' |
| 1098 | '\n' |
| 1099 | 'file.isatty()\n' |
| 1100 | '\n' |
| 1101 | ' Return "True" if the file is connected to a ' |
| 1102 | 'tty(-like) device, else\n' |
| 1103 | ' "False".\n' |
| 1104 | '\n' |
| 1105 | ' Note: If a file-like object is not associated with ' |
| 1106 | 'a real file,\n' |
| 1107 | ' this method should *not* be implemented.\n' |
| 1108 | '\n' |
| 1109 | 'file.next()\n' |
| 1110 | '\n' |
| 1111 | ' A file object is its own iterator, for example ' |
| 1112 | '"iter(f)" returns\n' |
| 1113 | ' *f* (unless *f* is closed). When a file is used as ' |
| 1114 | 'an iterator,\n' |
| 1115 | ' typically in a "for" loop (for example, "for line ' |
| 1116 | 'in f: print\n' |
| 1117 | ' line.strip()"), the "next()" method is called ' |
| 1118 | 'repeatedly. This\n' |
| 1119 | ' method returns the next input line, or raises ' |
| 1120 | '"StopIteration" when\n' |
| 1121 | ' EOF is hit when the file is open for reading ' |
| 1122 | '(behavior is undefined\n' |
| 1123 | ' when the file is open for writing). In order to ' |
| 1124 | 'make a "for" loop\n' |
| 1125 | ' the most efficient way of looping over the lines of ' |
| 1126 | 'a file (a very\n' |
| 1127 | ' common operation), the "next()" method uses a ' |
| 1128 | 'hidden read-ahead\n' |
| 1129 | ' buffer. As a consequence of using a read-ahead ' |
| 1130 | 'buffer, combining\n' |
| 1131 | ' "next()" with other file methods (like ' |
| 1132 | '"readline()") does not work\n' |
| 1133 | ' right. However, using "seek()" to reposition the ' |
| 1134 | 'file to an\n' |
| 1135 | ' absolute position will flush the read-ahead ' |
| 1136 | 'buffer.\n' |
| 1137 | '\n' |
| 1138 | ' New in version 2.3.\n' |
| 1139 | '\n' |
| 1140 | 'file.read([size])\n' |
| 1141 | '\n' |
| 1142 | ' Read at most *size* bytes from the file (less if ' |
| 1143 | 'the read hits EOF\n' |
| 1144 | ' before obtaining *size* bytes). If the *size* ' |
| 1145 | 'argument is negative\n' |
| 1146 | ' or omitted, read all data until EOF is reached. ' |
| 1147 | 'The bytes are\n' |
| 1148 | ' returned as a string object. An empty string is ' |
| 1149 | 'returned when EOF\n' |
| 1150 | ' is encountered immediately. (For certain files, ' |
| 1151 | 'like ttys, it\n' |
| 1152 | ' makes sense to continue reading after an EOF is ' |
| 1153 | 'hit.) Note that\n' |
| 1154 | ' this method may call the underlying C function ' |
| 1155 | '"fread()" more than\n' |
| 1156 | ' once in an effort to acquire as close to *size* ' |
| 1157 | 'bytes as possible.\n' |
| 1158 | ' Also note that when in non-blocking mode, less data ' |
| 1159 | 'than was\n' |
| 1160 | ' requested may be returned, even if no *size* ' |
| 1161 | 'parameter was given.\n' |
| 1162 | '\n' |
| 1163 | ' Note: This function is simply a wrapper for the ' |
| 1164 | 'underlying\n' |
| 1165 | ' "fread()" C function, and will behave the same in ' |
| 1166 | 'corner cases,\n' |
| 1167 | ' such as whether the EOF value is cached.\n' |
| 1168 | '\n' |
| 1169 | 'file.readline([size])\n' |
| 1170 | '\n' |
| 1171 | ' Read one entire line from the file. A trailing ' |
| 1172 | 'newline character\n' |
| 1173 | ' is kept in the string (but may be absent when a ' |
| 1174 | 'file ends with an\n' |
| 1175 | ' incomplete line). [6] If the *size* argument is ' |
| 1176 | 'present and non-\n' |
| 1177 | ' negative, it is a maximum byte count (including the ' |
| 1178 | 'trailing\n' |
| 1179 | ' newline) and an incomplete line may be returned. ' |
| 1180 | 'When *size* is not\n' |
| 1181 | ' 0, an empty string is returned *only* when EOF is ' |
| 1182 | 'encountered\n' |
| 1183 | ' immediately.\n' |
| 1184 | '\n' |
| 1185 | ' Note: Unlike "stdio"\'s "fgets()", the returned ' |
| 1186 | 'string contains\n' |
| 1187 | ' null characters ("\'\\0\'") if they occurred in ' |
| 1188 | 'the input.\n' |
| 1189 | '\n' |
| 1190 | 'file.readlines([sizehint])\n' |
| 1191 | '\n' |
| 1192 | ' Read until EOF using "readline()" and return a list ' |
| 1193 | 'containing the\n' |
| 1194 | ' lines thus read. If the optional *sizehint* ' |
| 1195 | 'argument is present,\n' |
| 1196 | ' instead of reading up to EOF, whole lines totalling ' |
| 1197 | 'approximately\n' |
| 1198 | ' *sizehint* bytes (possibly after rounding up to an ' |
| 1199 | 'internal buffer\n' |
| 1200 | ' size) are read. Objects implementing a file-like ' |
| 1201 | 'interface may\n' |
| 1202 | ' choose to ignore *sizehint* if it cannot be ' |
| 1203 | 'implemented, or cannot\n' |
| 1204 | ' be implemented efficiently.\n' |
| 1205 | '\n' |
| 1206 | 'file.xreadlines()\n' |
| 1207 | '\n' |
| 1208 | ' This method returns the same thing as "iter(f)".\n' |
| 1209 | '\n' |
| 1210 | ' New in version 2.1.\n' |
| 1211 | '\n' |
| 1212 | ' Deprecated since version 2.3: Use "for line in ' |
| 1213 | 'file" instead.\n' |
| 1214 | '\n' |
| 1215 | 'file.seek(offset[, whence])\n' |
| 1216 | '\n' |
| 1217 | ' Set the file\'s current position, like "stdio"\'s ' |
| 1218 | '"fseek()". The\n' |
| 1219 | ' *whence* argument is optional and defaults to ' |
| 1220 | '"os.SEEK_SET" or "0"\n' |
| 1221 | ' (absolute file positioning); other values are ' |
| 1222 | '"os.SEEK_CUR" or "1"\n' |
| 1223 | ' (seek relative to the current position) and ' |
| 1224 | '"os.SEEK_END" or "2"\n' |
| 1225 | " (seek relative to the file's end). There is no " |
| 1226 | 'return value.\n' |
| 1227 | '\n' |
| 1228 | ' For example, "f.seek(2, os.SEEK_CUR)" advances the ' |
| 1229 | 'position by two\n' |
| 1230 | ' and "f.seek(-3, os.SEEK_END)" sets the position to ' |
| 1231 | 'the third to\n' |
| 1232 | ' last.\n' |
| 1233 | '\n' |
| 1234 | ' Note that if the file is opened for appending (mode ' |
| 1235 | '"\'a\'" or\n' |
| 1236 | ' "\'a+\'"), any "seek()" operations will be undone ' |
| 1237 | 'at the next write.\n' |
| 1238 | ' If the file is only opened for writing in append ' |
| 1239 | 'mode (mode "\'a\'"),\n' |
| 1240 | ' this method is essentially a no-op, but it remains ' |
| 1241 | 'useful for files\n' |
| 1242 | ' opened in append mode with reading enabled (mode ' |
| 1243 | '"\'a+\'"). If the\n' |
| 1244 | ' file is opened in text mode (without "\'b\'"), only ' |
| 1245 | 'offsets returned\n' |
| 1246 | ' by "tell()" are legal. Use of other offsets causes ' |
| 1247 | 'undefined\n' |
| 1248 | ' behavior.\n' |
| 1249 | '\n' |
| 1250 | ' Note that not all file objects are seekable.\n' |
| 1251 | '\n' |
| 1252 | ' Changed in version 2.6: Passing float values as ' |
| 1253 | 'offset has been\n' |
| 1254 | ' deprecated.\n' |
| 1255 | '\n' |
| 1256 | 'file.tell()\n' |
| 1257 | '\n' |
| 1258 | " Return the file's current position, like " |
| 1259 | '"stdio"\'s "ftell()".\n' |
| 1260 | '\n' |
| 1261 | ' Note: On Windows, "tell()" can return illegal ' |
| 1262 | 'values (after an\n' |
| 1263 | ' "fgets()") when reading files with Unix-style ' |
| 1264 | 'line-endings. Use\n' |
| 1265 | ' binary mode ("\'rb\'") to circumvent this ' |
| 1266 | 'problem.\n' |
| 1267 | '\n' |
| 1268 | 'file.truncate([size])\n' |
| 1269 | '\n' |
| 1270 | " Truncate the file's size. If the optional *size* " |
| 1271 | 'argument is\n' |
| 1272 | ' present, the file is truncated to (at most) that ' |
| 1273 | 'size. The size\n' |
| 1274 | ' defaults to the current position. The current file ' |
| 1275 | 'position is not\n' |
| 1276 | ' changed. Note that if a specified size exceeds the ' |
| 1277 | "file's current\n" |
| 1278 | ' size, the result is platform-dependent: ' |
| 1279 | 'possibilities include that\n' |
| 1280 | ' the file may remain unchanged, increase to the ' |
| 1281 | 'specified size as if\n' |
| 1282 | ' zero-filled, or increase to the specified size with ' |
| 1283 | 'undefined new\n' |
| 1284 | ' content. Availability: Windows, many Unix ' |
| 1285 | 'variants.\n' |
| 1286 | '\n' |
| 1287 | 'file.write(str)\n' |
| 1288 | '\n' |
| 1289 | ' Write a string to the file. There is no return ' |
| 1290 | 'value. Due to\n' |
| 1291 | ' buffering, the string may not actually show up in ' |
| 1292 | 'the file until\n' |
| 1293 | ' the "flush()" or "close()" method is called.\n' |
| 1294 | '\n' |
| 1295 | 'file.writelines(sequence)\n' |
| 1296 | '\n' |
| 1297 | ' Write a sequence of strings to the file. The ' |
| 1298 | 'sequence can be any\n' |
| 1299 | ' iterable object producing strings, typically a list ' |
| 1300 | 'of strings.\n' |
| 1301 | ' There is no return value. (The name is intended to ' |
| 1302 | 'match\n' |
| 1303 | ' "readlines()"; "writelines()" does not add line ' |
| 1304 | 'separators.)\n' |
| 1305 | '\n' |
| 1306 | 'Files support the iterator protocol. Each iteration ' |
| 1307 | 'returns the same\n' |
| 1308 | 'result as "readline()", and iteration ends when the ' |
| 1309 | '"readline()"\n' |
| 1310 | 'method returns an empty string.\n' |
| 1311 | '\n' |
| 1312 | 'File objects also offer a number of other interesting ' |
| 1313 | 'attributes.\n' |
| 1314 | 'These are not required for file-like objects, but ' |
| 1315 | 'should be\n' |
| 1316 | 'implemented if they make sense for the particular ' |
| 1317 | 'object.\n' |
| 1318 | '\n' |
| 1319 | 'file.closed\n' |
| 1320 | '\n' |
| 1321 | ' bool indicating the current state of the file ' |
| 1322 | 'object. This is a\n' |
| 1323 | ' read-only attribute; the "close()" method changes ' |
| 1324 | 'the value. It may\n' |
| 1325 | ' not be available on all file-like objects.\n' |
| 1326 | '\n' |
| 1327 | 'file.encoding\n' |
| 1328 | '\n' |
| 1329 | ' The encoding that this file uses. When Unicode ' |
| 1330 | 'strings are written\n' |
| 1331 | ' to a file, they will be converted to byte strings ' |
| 1332 | 'using this\n' |
| 1333 | ' encoding. In addition, when the file is connected ' |
| 1334 | 'to a terminal,\n' |
| 1335 | ' the attribute gives the encoding that the terminal ' |
| 1336 | 'is likely to use\n' |
| 1337 | ' (that information might be incorrect if the user ' |
| 1338 | 'has misconfigured\n' |
| 1339 | ' the terminal). The attribute is read-only and may ' |
| 1340 | 'not be present\n' |
| 1341 | ' on all file-like objects. It may also be "None", in ' |
| 1342 | 'which case the\n' |
| 1343 | ' file uses the system default encoding for ' |
| 1344 | 'converting Unicode\n' |
| 1345 | ' strings.\n' |
| 1346 | '\n' |
| 1347 | ' New in version 2.3.\n' |
| 1348 | '\n' |
| 1349 | 'file.errors\n' |
| 1350 | '\n' |
| 1351 | ' The Unicode error handler used along with the ' |
| 1352 | 'encoding.\n' |
| 1353 | '\n' |
| 1354 | ' New in version 2.6.\n' |
| 1355 | '\n' |
| 1356 | 'file.mode\n' |
| 1357 | '\n' |
| 1358 | ' The I/O mode for the file. If the file was created ' |
| 1359 | 'using the\n' |
| 1360 | ' "open()" built-in function, this will be the value ' |
| 1361 | 'of the *mode*\n' |
| 1362 | ' parameter. This is a read-only attribute and may ' |
| 1363 | 'not be present on\n' |
| 1364 | ' all file-like objects.\n' |
| 1365 | '\n' |
| 1366 | 'file.name\n' |
| 1367 | '\n' |
| 1368 | ' If the file object was created using "open()", the ' |
| 1369 | 'name of the\n' |
| 1370 | ' file. Otherwise, some string that indicates the ' |
| 1371 | 'source of the file\n' |
| 1372 | ' object, of the form "<...>". This is a read-only ' |
| 1373 | 'attribute and may\n' |
| 1374 | ' not be present on all file-like objects.\n' |
| 1375 | '\n' |
| 1376 | 'file.newlines\n' |
| 1377 | '\n' |
| 1378 | ' If Python was built with *universal newlines* ' |
| 1379 | 'enabled (the default)\n' |
| 1380 | ' this read-only attribute exists, and for files ' |
| 1381 | 'opened in universal\n' |
| 1382 | ' newline read mode it keeps track of the types of ' |
| 1383 | 'newlines\n' |
| 1384 | ' encountered while reading the file. The values it ' |
| 1385 | 'can take are\n' |
| 1386 | ' "\'\\r\'", "\'\\n\'", "\'\\r\\n\'", "None" ' |
| 1387 | '(unknown, no newlines read yet) or\n' |
| 1388 | ' a tuple containing all the newline types seen, to ' |
| 1389 | 'indicate that\n' |
| 1390 | ' multiple newline conventions were encountered. For ' |
| 1391 | 'files not opened\n' |
| 1392 | ' in universal newlines read mode the value of this ' |
| 1393 | 'attribute will be\n' |
| 1394 | ' "None".\n' |
| 1395 | '\n' |
| 1396 | 'file.softspace\n' |
| 1397 | '\n' |
| 1398 | ' Boolean that indicates whether a space character ' |
| 1399 | 'needs to be\n' |
| 1400 | ' printed before another value when using the "print" ' |
| 1401 | 'statement.\n' |
| 1402 | ' Classes that are trying to simulate a file object ' |
| 1403 | 'should also have\n' |
| 1404 | ' a writable "softspace" attribute, which should be ' |
| 1405 | 'initialized to\n' |
| 1406 | ' zero. This will be automatic for most classes ' |
| 1407 | 'implemented in\n' |
| 1408 | ' Python (care may be needed for objects that ' |
| 1409 | 'override attribute\n' |
| 1410 | ' access); types implemented in C will have to ' |
| 1411 | 'provide a writable\n' |
| 1412 | ' "softspace" attribute.\n' |
| 1413 | '\n' |
| 1414 | ' Note: This attribute is not used to control the ' |
| 1415 | '"print"\n' |
| 1416 | ' statement, but to allow the implementation of ' |
| 1417 | '"print" to keep\n' |
| 1418 | ' track of its internal state.\n', |
| 1419 | 'bltin-null-object': '\n' |
| 1420 | 'The Null Object\n' |
| 1421 | '***************\n' |
| 1422 | '\n' |
| 1423 | "This object is returned by functions that don't " |
| 1424 | 'explicitly return a\n' |
| 1425 | 'value. It supports no special operations. There is ' |
| 1426 | 'exactly one null\n' |
| 1427 | 'object, named "None" (a built-in name).\n' |
| 1428 | '\n' |
| 1429 | 'It is written as "None".\n', |
| 1430 | 'bltin-type-objects': '\n' |
| 1431 | 'Type Objects\n' |
| 1432 | '************\n' |
| 1433 | '\n' |
| 1434 | 'Type objects represent the various object types. An ' |
| 1435 | "object's type is\n" |
| 1436 | 'accessed by the built-in function "type()". There are ' |
| 1437 | 'no special\n' |
| 1438 | 'operations on types. The standard module "types" ' |
| 1439 | 'defines names for\n' |
| 1440 | 'all standard built-in types.\n' |
| 1441 | '\n' |
| 1442 | 'Types are written like this: "<type \'int\'>".\n', |
| 1443 | 'booleans': '\n' |
| 1444 | 'Boolean operations\n' |
| 1445 | '******************\n' |
| 1446 | '\n' |
| 1447 | ' or_test ::= and_test | or_test "or" and_test\n' |
| 1448 | ' and_test ::= not_test | and_test "and" not_test\n' |
| 1449 | ' not_test ::= comparison | "not" not_test\n' |
| 1450 | '\n' |
| 1451 | 'In the context of Boolean operations, and also when expressions ' |
| 1452 | 'are\n' |
| 1453 | 'used by control flow statements, the following values are ' |
| 1454 | 'interpreted\n' |
| 1455 | 'as false: "False", "None", numeric zero of all types, and empty\n' |
| 1456 | 'strings and containers (including strings, tuples, lists,\n' |
| 1457 | 'dictionaries, sets and frozensets). All other values are ' |
| 1458 | 'interpreted\n' |
| 1459 | 'as true. (See the "__nonzero__()" special method for a way to ' |
| 1460 | 'change\n' |
| 1461 | 'this.)\n' |
| 1462 | '\n' |
| 1463 | 'The operator "not" yields "True" if its argument is false, ' |
| 1464 | '"False"\n' |
| 1465 | 'otherwise.\n' |
| 1466 | '\n' |
| 1467 | 'The expression "x and y" first evaluates *x*; if *x* is false, ' |
| 1468 | 'its\n' |
| 1469 | 'value is returned; otherwise, *y* is evaluated and the resulting ' |
| 1470 | 'value\n' |
| 1471 | 'is returned.\n' |
| 1472 | '\n' |
| 1473 | 'The expression "x or y" first evaluates *x*; if *x* is true, its ' |
| 1474 | 'value\n' |
| 1475 | 'is returned; otherwise, *y* is evaluated and the resulting value ' |
| 1476 | 'is\n' |
| 1477 | 'returned.\n' |
| 1478 | '\n' |
| 1479 | '(Note that neither "and" nor "or" restrict the value and type ' |
| 1480 | 'they\n' |
| 1481 | 'return to "False" and "True", but rather return the last ' |
| 1482 | 'evaluated\n' |
| 1483 | 'argument. This is sometimes useful, e.g., if "s" is a string ' |
| 1484 | 'that\n' |
| 1485 | 'should be replaced by a default value if it is empty, the ' |
| 1486 | 'expression\n' |
| 1487 | '"s or \'foo\'" yields the desired value. Because "not" has to ' |
| 1488 | 'invent a\n' |
| 1489 | 'value anyway, it does not bother to return a value of the same ' |
| 1490 | 'type as\n' |
| 1491 | 'its argument, so e.g., "not \'foo\'" yields "False", not ' |
| 1492 | '"\'\'".)\n', |
| 1493 | 'break': '\n' |
| 1494 | 'The "break" statement\n' |
| 1495 | '*********************\n' |
| 1496 | '\n' |
| 1497 | ' break_stmt ::= "break"\n' |
| 1498 | '\n' |
| 1499 | '"break" may only occur syntactically nested in a "for" or "while"\n' |
| 1500 | 'loop, but not nested in a function or class definition within that\n' |
| 1501 | 'loop.\n' |
| 1502 | '\n' |
| 1503 | 'It terminates the nearest enclosing loop, skipping the optional ' |
| 1504 | '"else"\n' |
| 1505 | 'clause if the loop has one.\n' |
| 1506 | '\n' |
| 1507 | 'If a "for" loop is terminated by "break", the loop control target\n' |
| 1508 | 'keeps its current value.\n' |
| 1509 | '\n' |
| 1510 | 'When "break" passes control out of a "try" statement with a ' |
| 1511 | '"finally"\n' |
| 1512 | 'clause, that "finally" clause is executed before really leaving ' |
| 1513 | 'the\n' |
| 1514 | 'loop.\n', |
| 1515 | 'callable-types': '\n' |
| 1516 | 'Emulating callable objects\n' |
| 1517 | '**************************\n' |
| 1518 | '\n' |
| 1519 | 'object.__call__(self[, args...])\n' |
| 1520 | '\n' |
| 1521 | ' Called when the instance is "called" as a function; if ' |
| 1522 | 'this method\n' |
| 1523 | ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n' |
| 1524 | ' "x.__call__(arg1, arg2, ...)".\n', |
| 1525 | 'calls': '\n' |
| 1526 | 'Calls\n' |
| 1527 | '*****\n' |
| 1528 | '\n' |
| 1529 | 'A call calls a callable object (e.g., a *function*) with a ' |
| 1530 | 'possibly\n' |
| 1531 | 'empty series of *arguments*:\n' |
| 1532 | '\n' |
| 1533 | ' call ::= primary "(" [argument_list [","]\n' |
| 1534 | ' | expression genexpr_for] ")"\n' |
| 1535 | ' argument_list ::= positional_arguments ["," ' |
| 1536 | 'keyword_arguments]\n' |
| 1537 | ' ["," "*" expression] ["," ' |
| 1538 | 'keyword_arguments]\n' |
| 1539 | ' ["," "**" expression]\n' |
| 1540 | ' | keyword_arguments ["," "*" expression]\n' |
| 1541 | ' ["," "**" expression]\n' |
| 1542 | ' | "*" expression ["," keyword_arguments] ["," ' |
| 1543 | '"**" expression]\n' |
| 1544 | ' | "**" expression\n' |
| 1545 | ' positional_arguments ::= expression ("," expression)*\n' |
| 1546 | ' keyword_arguments ::= keyword_item ("," keyword_item)*\n' |
| 1547 | ' keyword_item ::= identifier "=" expression\n' |
| 1548 | '\n' |
| 1549 | 'A trailing comma may be present after the positional and keyword\n' |
| 1550 | 'arguments but does not affect the semantics.\n' |
| 1551 | '\n' |
| 1552 | 'The primary must evaluate to a callable object (user-defined\n' |
| 1553 | 'functions, built-in functions, methods of built-in objects, class\n' |
| 1554 | 'objects, methods of class instances, and certain class instances\n' |
| 1555 | 'themselves are callable; extensions may define additional callable\n' |
| 1556 | 'object types). All argument expressions are evaluated before the ' |
| 1557 | 'call\n' |
| 1558 | 'is attempted. Please refer to section Function definitions for ' |
| 1559 | 'the\n' |
| 1560 | 'syntax of formal *parameter* lists.\n' |
| 1561 | '\n' |
| 1562 | 'If keyword arguments are present, they are first converted to\n' |
| 1563 | 'positional arguments, as follows. First, a list of unfilled slots ' |
| 1564 | 'is\n' |
| 1565 | 'created for the formal parameters. If there are N positional\n' |
| 1566 | 'arguments, they are placed in the first N slots. Next, for each\n' |
| 1567 | 'keyword argument, the identifier is used to determine the\n' |
| 1568 | 'corresponding slot (if the identifier is the same as the first ' |
| 1569 | 'formal\n' |
| 1570 | 'parameter name, the first slot is used, and so on). If the slot ' |
| 1571 | 'is\n' |
| 1572 | 'already filled, a "TypeError" exception is raised. Otherwise, the\n' |
| 1573 | 'value of the argument is placed in the slot, filling it (even if ' |
| 1574 | 'the\n' |
| 1575 | 'expression is "None", it fills the slot). When all arguments have\n' |
| 1576 | 'been processed, the slots that are still unfilled are filled with ' |
| 1577 | 'the\n' |
| 1578 | 'corresponding default value from the function definition. ' |
| 1579 | '(Default\n' |
| 1580 | 'values are calculated, once, when the function is defined; thus, a\n' |
| 1581 | 'mutable object such as a list or dictionary used as default value ' |
| 1582 | 'will\n' |
| 1583 | "be shared by all calls that don't specify an argument value for " |
| 1584 | 'the\n' |
| 1585 | 'corresponding slot; this should usually be avoided.) If there are ' |
| 1586 | 'any\n' |
| 1587 | 'unfilled slots for which no default value is specified, a ' |
| 1588 | '"TypeError"\n' |
| 1589 | 'exception is raised. Otherwise, the list of filled slots is used ' |
| 1590 | 'as\n' |
| 1591 | 'the argument list for the call.\n' |
| 1592 | '\n' |
| 1593 | '**CPython implementation detail:** An implementation may provide\n' |
| 1594 | 'built-in functions whose positional parameters do not have names, ' |
| 1595 | 'even\n' |
| 1596 | "if they are 'named' for the purpose of documentation, and which\n" |
| 1597 | 'therefore cannot be supplied by keyword. In CPython, this is the ' |
| 1598 | 'case\n' |
| 1599 | 'for functions implemented in C that use "PyArg_ParseTuple()" to ' |
| 1600 | 'parse\n' |
| 1601 | 'their arguments.\n' |
| 1602 | '\n' |
| 1603 | 'If there are more positional arguments than there are formal ' |
| 1604 | 'parameter\n' |
| 1605 | 'slots, a "TypeError" exception is raised, unless a formal ' |
| 1606 | 'parameter\n' |
| 1607 | 'using the syntax "*identifier" is present; in this case, that ' |
| 1608 | 'formal\n' |
| 1609 | 'parameter receives a tuple containing the excess positional ' |
| 1610 | 'arguments\n' |
| 1611 | '(or an empty tuple if there were no excess positional arguments).\n' |
| 1612 | '\n' |
| 1613 | 'If any keyword argument does not correspond to a formal parameter\n' |
| 1614 | 'name, a "TypeError" exception is raised, unless a formal parameter\n' |
| 1615 | 'using the syntax "**identifier" is present; in this case, that ' |
| 1616 | 'formal\n' |
| 1617 | 'parameter receives a dictionary containing the excess keyword\n' |
| 1618 | 'arguments (using the keywords as keys and the argument values as\n' |
| 1619 | 'corresponding values), or a (new) empty dictionary if there were ' |
| 1620 | 'no\n' |
| 1621 | 'excess keyword arguments.\n' |
| 1622 | '\n' |
| 1623 | 'If the syntax "*expression" appears in the function call, ' |
| 1624 | '"expression"\n' |
| 1625 | 'must evaluate to an iterable. Elements from this iterable are ' |
| 1626 | 'treated\n' |
| 1627 | 'as if they were additional positional arguments; if there are\n' |
| 1628 | 'positional arguments *x1*, ..., *xN*, and "expression" evaluates to ' |
| 1629 | 'a\n' |
| 1630 | 'sequence *y1*, ..., *yM*, this is equivalent to a call with M+N\n' |
| 1631 | 'positional arguments *x1*, ..., *xN*, *y1*, ..., *yM*.\n' |
| 1632 | '\n' |
| 1633 | 'A consequence of this is that although the "*expression" syntax ' |
| 1634 | 'may\n' |
| 1635 | 'appear *after* some keyword arguments, it is processed *before* ' |
| 1636 | 'the\n' |
| 1637 | 'keyword arguments (and the "**expression" argument, if any -- see\n' |
| 1638 | 'below). So:\n' |
| 1639 | '\n' |
| 1640 | ' >>> def f(a, b):\n' |
| 1641 | ' ... print a, b\n' |
| 1642 | ' ...\n' |
| 1643 | ' >>> f(b=1, *(2,))\n' |
| 1644 | ' 2 1\n' |
| 1645 | ' >>> f(a=1, *(2,))\n' |
| 1646 | ' Traceback (most recent call last):\n' |
| 1647 | ' File "<stdin>", line 1, in <module>\n' |
| 1648 | " TypeError: f() got multiple values for keyword argument 'a'\n" |
| 1649 | ' >>> f(1, *(2,))\n' |
| 1650 | ' 1 2\n' |
| 1651 | '\n' |
| 1652 | 'It is unusual for both keyword arguments and the "*expression" ' |
| 1653 | 'syntax\n' |
| 1654 | 'to be used in the same call, so in practice this confusion does ' |
| 1655 | 'not\n' |
| 1656 | 'arise.\n' |
| 1657 | '\n' |
| 1658 | 'If the syntax "**expression" appears in the function call,\n' |
| 1659 | '"expression" must evaluate to a mapping, the contents of which are\n' |
| 1660 | 'treated as additional keyword arguments. In the case of a keyword\n' |
| 1661 | 'appearing in both "expression" and as an explicit keyword argument, ' |
| 1662 | 'a\n' |
| 1663 | '"TypeError" exception is raised.\n' |
| 1664 | '\n' |
| 1665 | 'Formal parameters using the syntax "*identifier" or "**identifier"\n' |
| 1666 | 'cannot be used as positional argument slots or as keyword argument\n' |
| 1667 | 'names. Formal parameters using the syntax "(sublist)" cannot be ' |
| 1668 | 'used\n' |
| 1669 | 'as keyword argument names; the outermost sublist corresponds to a\n' |
| 1670 | 'single unnamed argument slot, and the argument value is assigned ' |
| 1671 | 'to\n' |
| 1672 | 'the sublist using the usual tuple assignment rules after all other\n' |
| 1673 | 'parameter processing is done.\n' |
| 1674 | '\n' |
| 1675 | 'A call always returns some value, possibly "None", unless it raises ' |
| 1676 | 'an\n' |
| 1677 | 'exception. How this value is computed depends on the type of the\n' |
| 1678 | 'callable object.\n' |
| 1679 | '\n' |
| 1680 | 'If it is---\n' |
| 1681 | '\n' |
| 1682 | 'a user-defined function:\n' |
| 1683 | ' The code block for the function is executed, passing it the\n' |
| 1684 | ' argument list. The first thing the code block will do is bind ' |
| 1685 | 'the\n' |
| 1686 | ' formal parameters to the arguments; this is described in ' |
| 1687 | 'section\n' |
| 1688 | ' Function definitions. When the code block executes a "return"\n' |
| 1689 | ' statement, this specifies the return value of the function ' |
| 1690 | 'call.\n' |
| 1691 | '\n' |
| 1692 | 'a built-in function or method:\n' |
| 1693 | ' The result is up to the interpreter; see Built-in Functions for ' |
| 1694 | 'the\n' |
| 1695 | ' descriptions of built-in functions and methods.\n' |
| 1696 | '\n' |
| 1697 | 'a class object:\n' |
| 1698 | ' A new instance of that class is returned.\n' |
| 1699 | '\n' |
| 1700 | 'a class instance method:\n' |
| 1701 | ' The corresponding user-defined function is called, with an ' |
| 1702 | 'argument\n' |
| 1703 | ' list that is one longer than the argument list of the call: the\n' |
| 1704 | ' instance becomes the first argument.\n' |
| 1705 | '\n' |
| 1706 | 'a class instance:\n' |
| 1707 | ' The class must define a "__call__()" method; the effect is then ' |
| 1708 | 'the\n' |
| 1709 | ' same as if that method was called.\n', |
| 1710 | 'class': '\n' |
| 1711 | 'Class definitions\n' |
| 1712 | '*****************\n' |
| 1713 | '\n' |
| 1714 | 'A class definition defines a class object (see section The ' |
| 1715 | 'standard\n' |
| 1716 | 'type hierarchy):\n' |
| 1717 | '\n' |
| 1718 | ' classdef ::= "class" classname [inheritance] ":" suite\n' |
| 1719 | ' inheritance ::= "(" [expression_list] ")"\n' |
| 1720 | ' classname ::= identifier\n' |
| 1721 | '\n' |
| 1722 | 'A class definition is an executable statement. It first evaluates ' |
| 1723 | 'the\n' |
| 1724 | 'inheritance list, if present. Each item in the inheritance list\n' |
| 1725 | 'should evaluate to a class object or class type which allows\n' |
| 1726 | "subclassing. The class's suite is then executed in a new " |
| 1727 | 'execution\n' |
| 1728 | 'frame (see section Naming and binding), using a newly created ' |
| 1729 | 'local\n' |
| 1730 | 'namespace and the original global namespace. (Usually, the suite\n' |
| 1731 | "contains only function definitions.) When the class's suite " |
| 1732 | 'finishes\n' |
| 1733 | 'execution, its execution frame is discarded but its local namespace ' |
| 1734 | 'is\n' |
| 1735 | 'saved. [4] A class object is then created using the inheritance ' |
| 1736 | 'list\n' |
| 1737 | 'for the base classes and the saved local namespace for the ' |
| 1738 | 'attribute\n' |
| 1739 | 'dictionary. The class name is bound to this class object in the\n' |
| 1740 | 'original local namespace.\n' |
| 1741 | '\n' |
| 1742 | "**Programmer's note:** Variables defined in the class definition " |
| 1743 | 'are\n' |
| 1744 | 'class variables; they are shared by all instances. To create ' |
| 1745 | 'instance\n' |
| 1746 | 'variables, they can be set in a method with "self.name = value". ' |
| 1747 | 'Both\n' |
| 1748 | 'class and instance variables are accessible through the notation\n' |
| 1749 | '""self.name"", and an instance variable hides a class variable ' |
| 1750 | 'with\n' |
| 1751 | 'the same name when accessed in this way. Class variables can be ' |
| 1752 | 'used\n' |
| 1753 | 'as defaults for instance variables, but using mutable values there ' |
| 1754 | 'can\n' |
| 1755 | 'lead to unexpected results. For *new-style class*es, descriptors ' |
| 1756 | 'can\n' |
| 1757 | 'be used to create instance variables with different implementation\n' |
| 1758 | 'details.\n' |
| 1759 | '\n' |
| 1760 | 'Class definitions, like function definitions, may be wrapped by one ' |
| 1761 | 'or\n' |
| 1762 | 'more *decorator* expressions. The evaluation rules for the ' |
| 1763 | 'decorator\n' |
| 1764 | 'expressions are the same as for functions. The result must be a ' |
| 1765 | 'class\n' |
| 1766 | 'object, which is then bound to the class name.\n' |
| 1767 | '\n' |
| 1768 | '-[ Footnotes ]-\n' |
| 1769 | '\n' |
| 1770 | '[1] The exception is propagated to the invocation stack unless\n' |
| 1771 | ' there is a "finally" clause which happens to raise another\n' |
| 1772 | ' exception. That new exception causes the old one to be lost.\n' |
| 1773 | '\n' |
| 1774 | '[2] Currently, control "flows off the end" except in the case of\n' |
| 1775 | ' an exception or the execution of a "return", "continue", or\n' |
| 1776 | ' "break" statement.\n' |
| 1777 | '\n' |
| 1778 | '[3] A string literal appearing as the first statement in the\n' |
| 1779 | ' function body is transformed into the function\'s "__doc__"\n' |
| 1780 | " attribute and therefore the function's *docstring*.\n" |
| 1781 | '\n' |
| 1782 | '[4] A string literal appearing as the first statement in the class\n' |
| 1783 | ' body is transformed into the namespace\'s "__doc__" item and\n' |
| 1784 | " therefore the class's *docstring*.\n", |
| 1785 | 'comparisons': '\n' |
| 1786 | 'Comparisons\n' |
| 1787 | '***********\n' |
| 1788 | '\n' |
| 1789 | 'Unlike C, all comparison operations in Python have the same ' |
| 1790 | 'priority,\n' |
| 1791 | 'which is lower than that of any arithmetic, shifting or ' |
| 1792 | 'bitwise\n' |
| 1793 | 'operation. Also unlike C, expressions like "a < b < c" have ' |
| 1794 | 'the\n' |
| 1795 | 'interpretation that is conventional in mathematics:\n' |
| 1796 | '\n' |
| 1797 | ' comparison ::= or_expr ( comp_operator or_expr )*\n' |
| 1798 | ' comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | ' |
| 1799 | '"!="\n' |
| 1800 | ' | "is" ["not"] | ["not"] "in"\n' |
| 1801 | '\n' |
| 1802 | 'Comparisons yield boolean values: "True" or "False".\n' |
| 1803 | '\n' |
| 1804 | 'Comparisons can be chained arbitrarily, e.g., "x < y <= z" ' |
| 1805 | 'is\n' |
| 1806 | 'equivalent to "x < y and y <= z", except that "y" is ' |
| 1807 | 'evaluated only\n' |
| 1808 | 'once (but in both cases "z" is not evaluated at all when "x < ' |
| 1809 | 'y" is\n' |
| 1810 | 'found to be false).\n' |
| 1811 | '\n' |
| 1812 | 'Formally, if *a*, *b*, *c*, ..., *y*, *z* are expressions and ' |
| 1813 | '*op1*,\n' |
| 1814 | '*op2*, ..., *opN* are comparison operators, then "a op1 b op2 ' |
| 1815 | 'c ... y\n' |
| 1816 | 'opN z" is equivalent to "a op1 b and b op2 c and ... y opN ' |
| 1817 | 'z", except\n' |
| 1818 | 'that each expression is evaluated at most once.\n' |
| 1819 | '\n' |
| 1820 | 'Note that "a op1 b op2 c" doesn\'t imply any kind of ' |
| 1821 | 'comparison between\n' |
| 1822 | '*a* and *c*, so that, e.g., "x < y > z" is perfectly legal ' |
| 1823 | '(though\n' |
| 1824 | 'perhaps not pretty).\n' |
| 1825 | '\n' |
| 1826 | 'The forms "<>" and "!=" are equivalent; for consistency with ' |
| 1827 | 'C, "!="\n' |
| 1828 | 'is preferred; where "!=" is mentioned below "<>" is also ' |
| 1829 | 'accepted.\n' |
| 1830 | 'The "<>" spelling is considered obsolescent.\n' |
| 1831 | '\n' |
| 1832 | '\n' |
| 1833 | 'Value comparisons\n' |
| 1834 | '=================\n' |
| 1835 | '\n' |
| 1836 | 'The operators "<", ">", "==", ">=", "<=", and "!=" compare ' |
| 1837 | 'the values\n' |
| 1838 | 'of two objects. The objects do not need to have the same ' |
| 1839 | 'type.\n' |
| 1840 | '\n' |
| 1841 | 'Chapter Objects, values and types states that objects have a ' |
| 1842 | 'value (in\n' |
| 1843 | 'addition to type and identity). The value of an object is a ' |
| 1844 | 'rather\n' |
| 1845 | 'abstract notion in Python: For example, there is no canonical ' |
| 1846 | 'access\n' |
| 1847 | "method for an object's value. Also, there is no requirement " |
| 1848 | 'that the\n' |
| 1849 | 'value of an object should be constructed in a particular way, ' |
| 1850 | 'e.g.\n' |
| 1851 | 'comprised of all its data attributes. Comparison operators ' |
| 1852 | 'implement a\n' |
| 1853 | 'particular notion of what the value of an object is. One can ' |
| 1854 | 'think of\n' |
| 1855 | 'them as defining the value of an object indirectly, by means ' |
| 1856 | 'of their\n' |
| 1857 | 'comparison implementation.\n' |
| 1858 | '\n' |
| 1859 | 'Types can customize their comparison behavior by implementing ' |
| 1860 | 'a\n' |
| 1861 | '"__cmp__()" method or *rich comparison methods* like ' |
| 1862 | '"__lt__()",\n' |
| 1863 | 'described in Basic customization.\n' |
| 1864 | '\n' |
| 1865 | 'The default behavior for equality comparison ("==" and "!=") ' |
| 1866 | 'is based\n' |
| 1867 | 'on the identity of the objects. Hence, equality comparison ' |
| 1868 | 'of\n' |
| 1869 | 'instances with the same identity results in equality, and ' |
| 1870 | 'equality\n' |
| 1871 | 'comparison of instances with different identities results in\n' |
| 1872 | 'inequality. A motivation for this default behavior is the ' |
| 1873 | 'desire that\n' |
| 1874 | 'all objects should be reflexive (i.e. "x is y" implies "x == ' |
| 1875 | 'y").\n' |
| 1876 | '\n' |
| 1877 | 'The default order comparison ("<", ">", "<=", and ">=") gives ' |
| 1878 | 'a\n' |
| 1879 | 'consistent but arbitrary order.\n' |
| 1880 | '\n' |
| 1881 | '(This unusual definition of comparison was used to simplify ' |
| 1882 | 'the\n' |
| 1883 | 'definition of operations like sorting and the "in" and "not ' |
| 1884 | 'in"\n' |
| 1885 | 'operators. In the future, the comparison rules for objects ' |
| 1886 | 'of\n' |
| 1887 | 'different types are likely to change.)\n' |
| 1888 | '\n' |
| 1889 | 'The behavior of the default equality comparison, that ' |
| 1890 | 'instances with\n' |
| 1891 | 'different identities are always unequal, may be in contrast ' |
| 1892 | 'to what\n' |
| 1893 | 'types will need that have a sensible definition of object ' |
| 1894 | 'value and\n' |
| 1895 | 'value-based equality. Such types will need to customize ' |
| 1896 | 'their\n' |
| 1897 | 'comparison behavior, and in fact, a number of built-in types ' |
| 1898 | 'have done\n' |
| 1899 | 'that.\n' |
| 1900 | '\n' |
| 1901 | 'The following list describes the comparison behavior of the ' |
| 1902 | 'most\n' |
| 1903 | 'important built-in types.\n' |
| 1904 | '\n' |
| 1905 | '* Numbers of built-in numeric types (Numeric Types --- int, ' |
| 1906 | 'float,\n' |
| 1907 | ' long, complex) and of the standard library types\n' |
| 1908 | ' "fractions.Fraction" and "decimal.Decimal" can be compared ' |
| 1909 | 'within\n' |
| 1910 | ' and across their types, with the restriction that complex ' |
| 1911 | 'numbers do\n' |
| 1912 | ' not support order comparison. Within the limits of the ' |
| 1913 | 'types\n' |
| 1914 | ' involved, they compare mathematically (algorithmically) ' |
| 1915 | 'correct\n' |
| 1916 | ' without loss of precision.\n' |
| 1917 | '\n' |
| 1918 | '* Strings (instances of "str" or "unicode") compare\n' |
| 1919 | ' lexicographically using the numeric equivalents (the result ' |
| 1920 | 'of the\n' |
| 1921 | ' built-in function "ord()") of their characters. [4] When ' |
| 1922 | 'comparing\n' |
| 1923 | ' an 8-bit string and a Unicode string, the 8-bit string is ' |
| 1924 | 'converted\n' |
| 1925 | ' to Unicode. If the conversion fails, the strings are ' |
| 1926 | 'considered\n' |
| 1927 | ' unequal.\n' |
| 1928 | '\n' |
| 1929 | '* Instances of "tuple" or "list" can be compared only within ' |
| 1930 | 'each of\n' |
| 1931 | ' their types. Equality comparison across these types ' |
| 1932 | 'results in\n' |
| 1933 | ' unequality, and ordering comparison across these types ' |
| 1934 | 'gives an\n' |
| 1935 | ' arbitrary order.\n' |
| 1936 | '\n' |
| 1937 | ' These sequences compare lexicographically using comparison ' |
| 1938 | 'of\n' |
| 1939 | ' corresponding elements, whereby reflexivity of the elements ' |
| 1940 | 'is\n' |
| 1941 | ' enforced.\n' |
| 1942 | '\n' |
| 1943 | ' In enforcing reflexivity of elements, the comparison of ' |
| 1944 | 'collections\n' |
| 1945 | ' assumes that for a collection element "x", "x == x" is ' |
| 1946 | 'always true.\n' |
| 1947 | ' Based on that assumption, element identity is compared ' |
| 1948 | 'first, and\n' |
| 1949 | ' element comparison is performed only for distinct ' |
| 1950 | 'elements. This\n' |
| 1951 | ' approach yields the same result as a strict element ' |
| 1952 | 'comparison\n' |
| 1953 | ' would, if the compared elements are reflexive. For ' |
| 1954 | 'non-reflexive\n' |
| 1955 | ' elements, the result is different than for strict element\n' |
| 1956 | ' comparison.\n' |
| 1957 | '\n' |
| 1958 | ' Lexicographical comparison between built-in collections ' |
| 1959 | 'works as\n' |
| 1960 | ' follows:\n' |
| 1961 | '\n' |
| 1962 | ' * For two collections to compare equal, they must be of the ' |
| 1963 | 'same\n' |
| 1964 | ' type, have the same length, and each pair of ' |
| 1965 | 'corresponding\n' |
| 1966 | ' elements must compare equal (for example, "[1,2] == ' |
| 1967 | '(1,2)" is\n' |
| 1968 | ' false because the type is not the same).\n' |
| 1969 | '\n' |
| 1970 | ' * Collections are ordered the same as their first unequal ' |
| 1971 | 'elements\n' |
| 1972 | ' (for example, "cmp([1,2,x], [1,2,y])" returns the same ' |
| 1973 | 'as\n' |
| 1974 | ' "cmp(x,y)"). If a corresponding element does not exist, ' |
| 1975 | 'the\n' |
| 1976 | ' shorter collection is ordered first (for example, "[1,2] ' |
| 1977 | '<\n' |
| 1978 | ' [1,2,3]" is true).\n' |
| 1979 | '\n' |
| 1980 | '* Mappings (instances of "dict") compare equal if and only if ' |
| 1981 | 'they\n' |
| 1982 | ' have equal *(key, value)* pairs. Equality comparison of the ' |
| 1983 | 'keys and\n' |
| 1984 | ' values enforces reflexivity.\n' |
| 1985 | '\n' |
| 1986 | ' Outcomes other than equality are resolved consistently, but ' |
| 1987 | 'are not\n' |
| 1988 | ' otherwise defined. [5]\n' |
| 1989 | '\n' |
| 1990 | '* Most other objects of built-in types compare unequal unless ' |
| 1991 | 'they\n' |
| 1992 | ' are the same object; the choice whether one object is ' |
| 1993 | 'considered\n' |
| 1994 | ' smaller or larger than another one is made arbitrarily but\n' |
| 1995 | ' consistently within one execution of a program.\n' |
| 1996 | '\n' |
| 1997 | 'User-defined classes that customize their comparison behavior ' |
| 1998 | 'should\n' |
| 1999 | 'follow some consistency rules, if possible:\n' |
| 2000 | '\n' |
| 2001 | '* Equality comparison should be reflexive. In other words, ' |
| 2002 | 'identical\n' |
| 2003 | ' objects should compare equal:\n' |
| 2004 | '\n' |
| 2005 | ' "x is y" implies "x == y"\n' |
| 2006 | '\n' |
| 2007 | '* Comparison should be symmetric. In other words, the ' |
| 2008 | 'following\n' |
| 2009 | ' expressions should have the same result:\n' |
| 2010 | '\n' |
| 2011 | ' "x == y" and "y == x"\n' |
| 2012 | '\n' |
| 2013 | ' "x != y" and "y != x"\n' |
| 2014 | '\n' |
| 2015 | ' "x < y" and "y > x"\n' |
| 2016 | '\n' |
| 2017 | ' "x <= y" and "y >= x"\n' |
| 2018 | '\n' |
| 2019 | '* Comparison should be transitive. The following ' |
| 2020 | '(non-exhaustive)\n' |
| 2021 | ' examples illustrate that:\n' |
| 2022 | '\n' |
| 2023 | ' "x > y and y > z" implies "x > z"\n' |
| 2024 | '\n' |
| 2025 | ' "x < y and y <= z" implies "x < z"\n' |
| 2026 | '\n' |
| 2027 | '* Inverse comparison should result in the boolean negation. ' |
| 2028 | 'In other\n' |
| 2029 | ' words, the following expressions should have the same ' |
| 2030 | 'result:\n' |
| 2031 | '\n' |
| 2032 | ' "x == y" and "not x != y"\n' |
| 2033 | '\n' |
| 2034 | ' "x < y" and "not x >= y" (for total ordering)\n' |
| 2035 | '\n' |
| 2036 | ' "x > y" and "not x <= y" (for total ordering)\n' |
| 2037 | '\n' |
| 2038 | ' The last two expressions apply to totally ordered ' |
| 2039 | 'collections (e.g.\n' |
| 2040 | ' to sequences, but not to sets or mappings). See also the\n' |
| 2041 | ' "total_ordering()" decorator.\n' |
| 2042 | '\n' |
| 2043 | '* The "hash()" result should be consistent with equality. ' |
| 2044 | 'Objects\n' |
| 2045 | ' that are equal should either have the same hash value, or ' |
| 2046 | 'be marked\n' |
| 2047 | ' as unhashable.\n' |
| 2048 | '\n' |
| 2049 | 'Python does not enforce these consistency rules.\n' |
| 2050 | '\n' |
| 2051 | '\n' |
| 2052 | 'Membership test operations\n' |
| 2053 | '==========================\n' |
| 2054 | '\n' |
| 2055 | 'The operators "in" and "not in" test for membership. "x in ' |
| 2056 | 's"\n' |
| 2057 | 'evaluates to "True" if *x* is a member of *s*, and "False" ' |
| 2058 | 'otherwise.\n' |
| 2059 | '"x not in s" returns the negation of "x in s". All built-in ' |
| 2060 | 'sequences\n' |
| 2061 | 'and set types support this as well as dictionary, for which ' |
| 2062 | '"in" tests\n' |
| 2063 | 'whether the dictionary has a given key. For container types ' |
| 2064 | 'such as\n' |
| 2065 | 'list, tuple, set, frozenset, dict, or collections.deque, the\n' |
| 2066 | 'expression "x in y" is equivalent to "any(x is e or x == e ' |
| 2067 | 'for e in\n' |
| 2068 | 'y)".\n' |
| 2069 | '\n' |
| 2070 | 'For the string and bytes types, "x in y" is "True" if and ' |
| 2071 | 'only if *x*\n' |
| 2072 | 'is a substring of *y*. An equivalent test is "y.find(x) != ' |
| 2073 | '-1".\n' |
| 2074 | 'Empty strings are always considered to be a substring of any ' |
| 2075 | 'other\n' |
| 2076 | 'string, so """ in "abc"" will return "True".\n' |
| 2077 | '\n' |
| 2078 | 'For user-defined classes which define the "__contains__()" ' |
| 2079 | 'method, "x\n' |
| 2080 | 'in y" returns "True" if "y.__contains__(x)" returns a true ' |
| 2081 | 'value, and\n' |
| 2082 | '"False" otherwise.\n' |
| 2083 | '\n' |
| 2084 | 'For user-defined classes which do not define "__contains__()" ' |
| 2085 | 'but do\n' |
| 2086 | 'define "__iter__()", "x in y" is "True" if some value "z" ' |
| 2087 | 'with "x ==\n' |
| 2088 | 'z" is produced while iterating over "y". If an exception is ' |
| 2089 | 'raised\n' |
| 2090 | 'during the iteration, it is as if "in" raised that ' |
| 2091 | 'exception.\n' |
| 2092 | '\n' |
| 2093 | 'Lastly, the old-style iteration protocol is tried: if a class ' |
| 2094 | 'defines\n' |
| 2095 | '"__getitem__()", "x in y" is "True" if and only if there is a ' |
| 2096 | 'non-\n' |
| 2097 | 'negative integer index *i* such that "x == y[i]", and all ' |
| 2098 | 'lower\n' |
| 2099 | 'integer indices do not raise "IndexError" exception. (If any ' |
| 2100 | 'other\n' |
| 2101 | 'exception is raised, it is as if "in" raised that ' |
| 2102 | 'exception).\n' |
| 2103 | '\n' |
| 2104 | 'The operator "not in" is defined to have the inverse true ' |
| 2105 | 'value of\n' |
| 2106 | '"in".\n' |
| 2107 | '\n' |
| 2108 | '\n' |
| 2109 | 'Identity comparisons\n' |
| 2110 | '====================\n' |
| 2111 | '\n' |
| 2112 | 'The operators "is" and "is not" test for object identity: "x ' |
| 2113 | 'is y" is\n' |
| 2114 | 'true if and only if *x* and *y* are the same object. "x is ' |
| 2115 | 'not y"\n' |
| 2116 | 'yields the inverse truth value. [6]\n', |
| 2117 | 'compound': '\n' |
| 2118 | 'Compound statements\n' |
| 2119 | '*******************\n' |
| 2120 | '\n' |
| 2121 | 'Compound statements contain (groups of) other statements; they ' |
| 2122 | 'affect\n' |
| 2123 | 'or control the execution of those other statements in some way. ' |
| 2124 | 'In\n' |
| 2125 | 'general, compound statements span multiple lines, although in ' |
| 2126 | 'simple\n' |
| 2127 | 'incarnations a whole compound statement may be contained in one ' |
| 2128 | 'line.\n' |
| 2129 | '\n' |
| 2130 | 'The "if", "while" and "for" statements implement traditional ' |
| 2131 | 'control\n' |
| 2132 | 'flow constructs. "try" specifies exception handlers and/or ' |
| 2133 | 'cleanup\n' |
| 2134 | 'code for a group of statements. Function and class definitions ' |
| 2135 | 'are\n' |
| 2136 | 'also syntactically compound statements.\n' |
| 2137 | '\n' |
| 2138 | "Compound statements consist of one or more 'clauses.' A clause\n" |
| 2139 | "consists of a header and a 'suite.' The clause headers of a\n" |
| 2140 | 'particular compound statement are all at the same indentation ' |
| 2141 | 'level.\n' |
| 2142 | 'Each clause header begins with a uniquely identifying keyword ' |
| 2143 | 'and ends\n' |
| 2144 | 'with a colon. A suite is a group of statements controlled by a\n' |
| 2145 | 'clause. A suite can be one or more semicolon-separated simple\n' |
| 2146 | 'statements on the same line as the header, following the ' |
| 2147 | "header's\n" |
| 2148 | 'colon, or it can be one or more indented statements on ' |
| 2149 | 'subsequent\n' |
| 2150 | 'lines. Only the latter form of suite can contain nested ' |
| 2151 | 'compound\n' |
| 2152 | "statements; the following is illegal, mostly because it wouldn't " |
| 2153 | 'be\n' |
| 2154 | 'clear to which "if" clause a following "else" clause would ' |
| 2155 | 'belong:\n' |
| 2156 | '\n' |
| 2157 | ' if test1: if test2: print x\n' |
| 2158 | '\n' |
| 2159 | 'Also note that the semicolon binds tighter than the colon in ' |
| 2160 | 'this\n' |
| 2161 | 'context, so that in the following example, either all or none of ' |
| 2162 | 'the\n' |
| 2163 | '"print" statements are executed:\n' |
| 2164 | '\n' |
| 2165 | ' if x < y < z: print x; print y; print z\n' |
| 2166 | '\n' |
| 2167 | 'Summarizing:\n' |
| 2168 | '\n' |
| 2169 | ' compound_stmt ::= if_stmt\n' |
| 2170 | ' | while_stmt\n' |
| 2171 | ' | for_stmt\n' |
| 2172 | ' | try_stmt\n' |
| 2173 | ' | with_stmt\n' |
| 2174 | ' | funcdef\n' |
| 2175 | ' | classdef\n' |
| 2176 | ' | decorated\n' |
| 2177 | ' suite ::= stmt_list NEWLINE | NEWLINE INDENT ' |
| 2178 | 'statement+ DEDENT\n' |
| 2179 | ' statement ::= stmt_list NEWLINE | compound_stmt\n' |
| 2180 | ' stmt_list ::= simple_stmt (";" simple_stmt)* [";"]\n' |
| 2181 | '\n' |
| 2182 | 'Note that statements always end in a "NEWLINE" possibly followed ' |
| 2183 | 'by a\n' |
| 2184 | '"DEDENT". Also note that optional continuation clauses always ' |
| 2185 | 'begin\n' |
| 2186 | 'with a keyword that cannot start a statement, thus there are no\n' |
| 2187 | 'ambiguities (the \'dangling "else"\' problem is solved in Python ' |
| 2188 | 'by\n' |
| 2189 | 'requiring nested "if" statements to be indented).\n' |
| 2190 | '\n' |
| 2191 | 'The formatting of the grammar rules in the following sections ' |
| 2192 | 'places\n' |
| 2193 | 'each clause on a separate line for clarity.\n' |
| 2194 | '\n' |
| 2195 | '\n' |
| 2196 | 'The "if" statement\n' |
| 2197 | '==================\n' |
| 2198 | '\n' |
| 2199 | 'The "if" statement is used for conditional execution:\n' |
| 2200 | '\n' |
| 2201 | ' if_stmt ::= "if" expression ":" suite\n' |
| 2202 | ' ( "elif" expression ":" suite )*\n' |
| 2203 | ' ["else" ":" suite]\n' |
| 2204 | '\n' |
| 2205 | 'It selects exactly one of the suites by evaluating the ' |
| 2206 | 'expressions one\n' |
| 2207 | 'by one until one is found to be true (see section Boolean ' |
| 2208 | 'operations\n' |
| 2209 | 'for the definition of true and false); then that suite is ' |
| 2210 | 'executed\n' |
| 2211 | '(and no other part of the "if" statement is executed or ' |
| 2212 | 'evaluated).\n' |
| 2213 | 'If all expressions are false, the suite of the "else" clause, ' |
| 2214 | 'if\n' |
| 2215 | 'present, is executed.\n' |
| 2216 | '\n' |
| 2217 | '\n' |
| 2218 | 'The "while" statement\n' |
| 2219 | '=====================\n' |
| 2220 | '\n' |
| 2221 | 'The "while" statement is used for repeated execution as long as ' |
| 2222 | 'an\n' |
| 2223 | 'expression is true:\n' |
| 2224 | '\n' |
| 2225 | ' while_stmt ::= "while" expression ":" suite\n' |
| 2226 | ' ["else" ":" suite]\n' |
| 2227 | '\n' |
| 2228 | 'This repeatedly tests the expression and, if it is true, ' |
| 2229 | 'executes the\n' |
| 2230 | 'first suite; if the expression is false (which may be the first ' |
| 2231 | 'time\n' |
| 2232 | 'it is tested) the suite of the "else" clause, if present, is ' |
| 2233 | 'executed\n' |
| 2234 | 'and the loop terminates.\n' |
| 2235 | '\n' |
| 2236 | 'A "break" statement executed in the first suite terminates the ' |
| 2237 | 'loop\n' |
| 2238 | 'without executing the "else" clause\'s suite. A "continue" ' |
| 2239 | 'statement\n' |
| 2240 | 'executed in the first suite skips the rest of the suite and goes ' |
| 2241 | 'back\n' |
| 2242 | 'to testing the expression.\n' |
| 2243 | '\n' |
| 2244 | '\n' |
| 2245 | 'The "for" statement\n' |
| 2246 | '===================\n' |
| 2247 | '\n' |
| 2248 | 'The "for" statement is used to iterate over the elements of a ' |
| 2249 | 'sequence\n' |
| 2250 | '(such as a string, tuple or list) or other iterable object:\n' |
| 2251 | '\n' |
| 2252 | ' for_stmt ::= "for" target_list "in" expression_list ":" ' |
| 2253 | 'suite\n' |
| 2254 | ' ["else" ":" suite]\n' |
| 2255 | '\n' |
| 2256 | 'The expression list is evaluated once; it should yield an ' |
| 2257 | 'iterable\n' |
| 2258 | 'object. An iterator is created for the result of the\n' |
| 2259 | '"expression_list". The suite is then executed once for each ' |
| 2260 | 'item\n' |
| 2261 | 'provided by the iterator, in the order of ascending indices. ' |
| 2262 | 'Each\n' |
| 2263 | 'item in turn is assigned to the target list using the standard ' |
| 2264 | 'rules\n' |
| 2265 | 'for assignments, and then the suite is executed. When the items ' |
| 2266 | 'are\n' |
| 2267 | 'exhausted (which is immediately when the sequence is empty), the ' |
| 2268 | 'suite\n' |
| 2269 | 'in the "else" clause, if present, is executed, and the loop\n' |
| 2270 | 'terminates.\n' |
| 2271 | '\n' |
| 2272 | 'A "break" statement executed in the first suite terminates the ' |
| 2273 | 'loop\n' |
| 2274 | 'without executing the "else" clause\'s suite. A "continue" ' |
| 2275 | 'statement\n' |
| 2276 | 'executed in the first suite skips the rest of the suite and ' |
| 2277 | 'continues\n' |
| 2278 | 'with the next item, or with the "else" clause if there was no ' |
| 2279 | 'next\n' |
| 2280 | 'item.\n' |
| 2281 | '\n' |
| 2282 | 'The suite may assign to the variable(s) in the target list; this ' |
| 2283 | 'does\n' |
| 2284 | 'not affect the next item assigned to it.\n' |
| 2285 | '\n' |
| 2286 | 'The target list is not deleted when the loop is finished, but if ' |
| 2287 | 'the\n' |
| 2288 | 'sequence is empty, it will not have been assigned to at all by ' |
| 2289 | 'the\n' |
| 2290 | 'loop. Hint: the built-in function "range()" returns a sequence ' |
| 2291 | 'of\n' |
| 2292 | 'integers suitable to emulate the effect of Pascal\'s "for i := a ' |
| 2293 | 'to b\n' |
| 2294 | 'do"; e.g., "range(3)" returns the list "[0, 1, 2]".\n' |
| 2295 | '\n' |
| 2296 | 'Note: There is a subtlety when the sequence is being modified by ' |
| 2297 | 'the\n' |
| 2298 | ' loop (this can only occur for mutable sequences, i.e. lists). ' |
| 2299 | 'An\n' |
| 2300 | ' internal counter is used to keep track of which item is used ' |
| 2301 | 'next,\n' |
| 2302 | ' and this is incremented on each iteration. When this counter ' |
| 2303 | 'has\n' |
| 2304 | ' reached the length of the sequence the loop terminates. This ' |
| 2305 | 'means\n' |
| 2306 | ' that if the suite deletes the current (or a previous) item ' |
| 2307 | 'from the\n' |
| 2308 | ' sequence, the next item will be skipped (since it gets the ' |
| 2309 | 'index of\n' |
| 2310 | ' the current item which has already been treated). Likewise, ' |
| 2311 | 'if the\n' |
| 2312 | ' suite inserts an item in the sequence before the current item, ' |
| 2313 | 'the\n' |
| 2314 | ' current item will be treated again the next time through the ' |
| 2315 | 'loop.\n' |
| 2316 | ' This can lead to nasty bugs that can be avoided by making a\n' |
| 2317 | ' temporary copy using a slice of the whole sequence, e.g.,\n' |
| 2318 | '\n' |
| 2319 | ' for x in a[:]:\n' |
| 2320 | ' if x < 0: a.remove(x)\n' |
| 2321 | '\n' |
| 2322 | '\n' |
| 2323 | 'The "try" statement\n' |
| 2324 | '===================\n' |
| 2325 | '\n' |
| 2326 | 'The "try" statement specifies exception handlers and/or cleanup ' |
| 2327 | 'code\n' |
| 2328 | 'for a group of statements:\n' |
| 2329 | '\n' |
| 2330 | ' try_stmt ::= try1_stmt | try2_stmt\n' |
| 2331 | ' try1_stmt ::= "try" ":" suite\n' |
| 2332 | ' ("except" [expression [("as" | ",") ' |
| 2333 | 'identifier]] ":" suite)+\n' |
| 2334 | ' ["else" ":" suite]\n' |
| 2335 | ' ["finally" ":" suite]\n' |
| 2336 | ' try2_stmt ::= "try" ":" suite\n' |
| 2337 | ' "finally" ":" suite\n' |
| 2338 | '\n' |
| 2339 | 'Changed in version 2.5: In previous versions of Python,\n' |
| 2340 | '"try"..."except"..."finally" did not work. "try"..."except" had ' |
| 2341 | 'to be\n' |
| 2342 | 'nested in "try"..."finally".\n' |
| 2343 | '\n' |
| 2344 | 'The "except" clause(s) specify one or more exception handlers. ' |
| 2345 | 'When no\n' |
| 2346 | 'exception occurs in the "try" clause, no exception handler is\n' |
| 2347 | 'executed. When an exception occurs in the "try" suite, a search ' |
| 2348 | 'for an\n' |
| 2349 | 'exception handler is started. This search inspects the except ' |
| 2350 | 'clauses\n' |
| 2351 | 'in turn until one is found that matches the exception. An ' |
| 2352 | 'expression-\n' |
| 2353 | 'less except clause, if present, must be last; it matches any\n' |
| 2354 | 'exception. For an except clause with an expression, that ' |
| 2355 | 'expression\n' |
| 2356 | 'is evaluated, and the clause matches the exception if the ' |
| 2357 | 'resulting\n' |
| 2358 | 'object is "compatible" with the exception. An object is ' |
| 2359 | 'compatible\n' |
| 2360 | 'with an exception if it is the class or a base class of the ' |
| 2361 | 'exception\n' |
| 2362 | 'object, or a tuple containing an item compatible with the ' |
| 2363 | 'exception.\n' |
| 2364 | '\n' |
| 2365 | 'If no except clause matches the exception, the search for an ' |
| 2366 | 'exception\n' |
| 2367 | 'handler continues in the surrounding code and on the invocation ' |
| 2368 | 'stack.\n' |
| 2369 | '[1]\n' |
| 2370 | '\n' |
| 2371 | 'If the evaluation of an expression in the header of an except ' |
| 2372 | 'clause\n' |
| 2373 | 'raises an exception, the original search for a handler is ' |
| 2374 | 'canceled and\n' |
| 2375 | 'a search starts for the new exception in the surrounding code ' |
| 2376 | 'and on\n' |
| 2377 | 'the call stack (it is treated as if the entire "try" statement ' |
| 2378 | 'raised\n' |
| 2379 | 'the exception).\n' |
| 2380 | '\n' |
| 2381 | 'When a matching except clause is found, the exception is ' |
| 2382 | 'assigned to\n' |
| 2383 | 'the target specified in that except clause, if present, and the ' |
| 2384 | 'except\n' |
| 2385 | "clause's suite is executed. All except clauses must have an\n" |
| 2386 | 'executable block. When the end of this block is reached, ' |
| 2387 | 'execution\n' |
| 2388 | 'continues normally after the entire try statement. (This means ' |
| 2389 | 'that\n' |
| 2390 | 'if two nested handlers exist for the same exception, and the ' |
| 2391 | 'exception\n' |
| 2392 | 'occurs in the try clause of the inner handler, the outer handler ' |
| 2393 | 'will\n' |
| 2394 | 'not handle the exception.)\n' |
| 2395 | '\n' |
| 2396 | "Before an except clause's suite is executed, details about the\n" |
| 2397 | 'exception are assigned to three variables in the "sys" module:\n' |
| 2398 | '"sys.exc_type" receives the object identifying the exception;\n' |
| 2399 | '"sys.exc_value" receives the exception\'s parameter;\n' |
| 2400 | '"sys.exc_traceback" receives a traceback object (see section ' |
| 2401 | 'The\n' |
| 2402 | 'standard type hierarchy) identifying the point in the program ' |
| 2403 | 'where\n' |
| 2404 | 'the exception occurred. These details are also available through ' |
| 2405 | 'the\n' |
| 2406 | '"sys.exc_info()" function, which returns a tuple "(exc_type,\n' |
| 2407 | 'exc_value, exc_traceback)". Use of the corresponding variables ' |
| 2408 | 'is\n' |
| 2409 | 'deprecated in favor of this function, since their use is unsafe ' |
| 2410 | 'in a\n' |
| 2411 | 'threaded program. As of Python 1.5, the variables are restored ' |
| 2412 | 'to\n' |
| 2413 | 'their previous values (before the call) when returning from a ' |
| 2414 | 'function\n' |
| 2415 | 'that handled an exception.\n' |
| 2416 | '\n' |
| 2417 | 'The optional "else" clause is executed if and when control flows ' |
| 2418 | 'off\n' |
| 2419 | 'the end of the "try" clause. [2] Exceptions in the "else" clause ' |
| 2420 | 'are\n' |
| 2421 | 'not handled by the preceding "except" clauses.\n' |
| 2422 | '\n' |
| 2423 | 'If "finally" is present, it specifies a \'cleanup\' handler. ' |
| 2424 | 'The "try"\n' |
| 2425 | 'clause is executed, including any "except" and "else" clauses. ' |
| 2426 | 'If an\n' |
| 2427 | 'exception occurs in any of the clauses and is not handled, the\n' |
| 2428 | 'exception is temporarily saved. The "finally" clause is ' |
| 2429 | 'executed. If\n' |
| 2430 | 'there is a saved exception, it is re-raised at the end of the\n' |
| 2431 | '"finally" clause. If the "finally" clause raises another ' |
| 2432 | 'exception or\n' |
| 2433 | 'executes a "return" or "break" statement, the saved exception ' |
| 2434 | 'is\n' |
| 2435 | 'discarded:\n' |
| 2436 | '\n' |
| 2437 | ' >>> def f():\n' |
| 2438 | ' ... try:\n' |
| 2439 | ' ... 1/0\n' |
| 2440 | ' ... finally:\n' |
| 2441 | ' ... return 42\n' |
| 2442 | ' ...\n' |
| 2443 | ' >>> f()\n' |
| 2444 | ' 42\n' |
| 2445 | '\n' |
| 2446 | 'The exception information is not available to the program ' |
| 2447 | 'during\n' |
| 2448 | 'execution of the "finally" clause.\n' |
| 2449 | '\n' |
| 2450 | 'When a "return", "break" or "continue" statement is executed in ' |
| 2451 | 'the\n' |
| 2452 | '"try" suite of a "try"..."finally" statement, the "finally" ' |
| 2453 | 'clause is\n' |
| 2454 | 'also executed \'on the way out.\' A "continue" statement is ' |
| 2455 | 'illegal in\n' |
| 2456 | 'the "finally" clause. (The reason is a problem with the current\n' |
| 2457 | 'implementation --- this restriction may be lifted in the ' |
| 2458 | 'future).\n' |
| 2459 | '\n' |
| 2460 | 'The return value of a function is determined by the last ' |
| 2461 | '"return"\n' |
| 2462 | 'statement executed. Since the "finally" clause always executes, ' |
| 2463 | 'a\n' |
| 2464 | '"return" statement executed in the "finally" clause will always ' |
| 2465 | 'be the\n' |
| 2466 | 'last one executed:\n' |
| 2467 | '\n' |
| 2468 | ' >>> def foo():\n' |
| 2469 | ' ... try:\n' |
| 2470 | " ... return 'try'\n" |
| 2471 | ' ... finally:\n' |
| 2472 | " ... return 'finally'\n" |
| 2473 | ' ...\n' |
| 2474 | ' >>> foo()\n' |
| 2475 | " 'finally'\n" |
| 2476 | '\n' |
| 2477 | 'Additional information on exceptions can be found in section\n' |
| 2478 | 'Exceptions, and information on using the "raise" statement to ' |
| 2479 | 'generate\n' |
| 2480 | 'exceptions may be found in section The raise statement.\n' |
| 2481 | '\n' |
| 2482 | '\n' |
| 2483 | 'The "with" statement\n' |
| 2484 | '====================\n' |
| 2485 | '\n' |
| 2486 | 'New in version 2.5.\n' |
| 2487 | '\n' |
| 2488 | 'The "with" statement is used to wrap the execution of a block ' |
| 2489 | 'with\n' |
| 2490 | 'methods defined by a context manager (see section With ' |
| 2491 | 'Statement\n' |
| 2492 | 'Context Managers). This allows common ' |
| 2493 | '"try"..."except"..."finally"\n' |
| 2494 | 'usage patterns to be encapsulated for convenient reuse.\n' |
| 2495 | '\n' |
| 2496 | ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n' |
| 2497 | ' with_item ::= expression ["as" target]\n' |
| 2498 | '\n' |
| 2499 | 'The execution of the "with" statement with one "item" proceeds ' |
| 2500 | 'as\n' |
| 2501 | 'follows:\n' |
| 2502 | '\n' |
| 2503 | '1. The context expression (the expression given in the ' |
| 2504 | '"with_item")\n' |
| 2505 | ' is evaluated to obtain a context manager.\n' |
| 2506 | '\n' |
| 2507 | '2. The context manager\'s "__exit__()" is loaded for later use.\n' |
| 2508 | '\n' |
| 2509 | '3. The context manager\'s "__enter__()" method is invoked.\n' |
| 2510 | '\n' |
| 2511 | '4. If a target was included in the "with" statement, the return\n' |
| 2512 | ' value from "__enter__()" is assigned to it.\n' |
| 2513 | '\n' |
| 2514 | ' Note: The "with" statement guarantees that if the ' |
| 2515 | '"__enter__()"\n' |
| 2516 | ' method returns without an error, then "__exit__()" will ' |
| 2517 | 'always be\n' |
| 2518 | ' called. Thus, if an error occurs during the assignment to ' |
| 2519 | 'the\n' |
| 2520 | ' target list, it will be treated the same as an error ' |
| 2521 | 'occurring\n' |
| 2522 | ' within the suite would be. See step 6 below.\n' |
| 2523 | '\n' |
| 2524 | '5. The suite is executed.\n' |
| 2525 | '\n' |
| 2526 | '6. The context manager\'s "__exit__()" method is invoked. If an\n' |
| 2527 | ' exception caused the suite to be exited, its type, value, ' |
| 2528 | 'and\n' |
| 2529 | ' traceback are passed as arguments to "__exit__()". Otherwise, ' |
| 2530 | 'three\n' |
| 2531 | ' "None" arguments are supplied.\n' |
| 2532 | '\n' |
| 2533 | ' If the suite was exited due to an exception, and the return ' |
| 2534 | 'value\n' |
| 2535 | ' from the "__exit__()" method was false, the exception is ' |
| 2536 | 'reraised.\n' |
| 2537 | ' If the return value was true, the exception is suppressed, ' |
| 2538 | 'and\n' |
| 2539 | ' execution continues with the statement following the "with"\n' |
| 2540 | ' statement.\n' |
| 2541 | '\n' |
| 2542 | ' If the suite was exited for any reason other than an ' |
| 2543 | 'exception, the\n' |
| 2544 | ' return value from "__exit__()" is ignored, and execution ' |
| 2545 | 'proceeds\n' |
| 2546 | ' at the normal location for the kind of exit that was taken.\n' |
| 2547 | '\n' |
| 2548 | 'With more than one item, the context managers are processed as ' |
| 2549 | 'if\n' |
| 2550 | 'multiple "with" statements were nested:\n' |
| 2551 | '\n' |
| 2552 | ' with A() as a, B() as b:\n' |
| 2553 | ' suite\n' |
| 2554 | '\n' |
| 2555 | 'is equivalent to\n' |
| 2556 | '\n' |
| 2557 | ' with A() as a:\n' |
| 2558 | ' with B() as b:\n' |
| 2559 | ' suite\n' |
| 2560 | '\n' |
| 2561 | 'Note: In Python 2.5, the "with" statement is only allowed when ' |
| 2562 | 'the\n' |
| 2563 | ' "with_statement" feature has been enabled. It is always ' |
| 2564 | 'enabled in\n' |
| 2565 | ' Python 2.6.\n' |
| 2566 | '\n' |
| 2567 | 'Changed in version 2.7: Support for multiple context ' |
| 2568 | 'expressions.\n' |
| 2569 | '\n' |
| 2570 | 'See also:\n' |
| 2571 | '\n' |
| 2572 | ' **PEP 343** - The "with" statement\n' |
| 2573 | ' The specification, background, and examples for the Python ' |
| 2574 | '"with"\n' |
| 2575 | ' statement.\n' |
| 2576 | '\n' |
| 2577 | '\n' |
| 2578 | 'Function definitions\n' |
| 2579 | '====================\n' |
| 2580 | '\n' |
| 2581 | 'A function definition defines a user-defined function object ' |
| 2582 | '(see\n' |
| 2583 | 'section The standard type hierarchy):\n' |
| 2584 | '\n' |
| 2585 | ' decorated ::= decorators (classdef | funcdef)\n' |
| 2586 | ' decorators ::= decorator+\n' |
| 2587 | ' decorator ::= "@" dotted_name ["(" [argument_list [","]] ' |
| 2588 | '")"] NEWLINE\n' |
| 2589 | ' funcdef ::= "def" funcname "(" [parameter_list] ")" ' |
| 2590 | '":" suite\n' |
| 2591 | ' dotted_name ::= identifier ("." identifier)*\n' |
| 2592 | ' parameter_list ::= (defparameter ",")*\n' |
| 2593 | ' ( "*" identifier ["," "**" identifier]\n' |
| 2594 | ' | "**" identifier\n' |
| 2595 | ' | defparameter [","] )\n' |
| 2596 | ' defparameter ::= parameter ["=" expression]\n' |
| 2597 | ' sublist ::= parameter ("," parameter)* [","]\n' |
| 2598 | ' parameter ::= identifier | "(" sublist ")"\n' |
| 2599 | ' funcname ::= identifier\n' |
| 2600 | '\n' |
| 2601 | 'A function definition is an executable statement. Its execution ' |
| 2602 | 'binds\n' |
| 2603 | 'the function name in the current local namespace to a function ' |
| 2604 | 'object\n' |
| 2605 | '(a wrapper around the executable code for the function). This\n' |
| 2606 | 'function object contains a reference to the current global ' |
| 2607 | 'namespace\n' |
| 2608 | 'as the global namespace to be used when the function is called.\n' |
| 2609 | '\n' |
| 2610 | 'The function definition does not execute the function body; this ' |
| 2611 | 'gets\n' |
| 2612 | 'executed only when the function is called. [3]\n' |
| 2613 | '\n' |
| 2614 | 'A function definition may be wrapped by one or more *decorator*\n' |
| 2615 | 'expressions. Decorator expressions are evaluated when the ' |
| 2616 | 'function is\n' |
| 2617 | 'defined, in the scope that contains the function definition. ' |
| 2618 | 'The\n' |
| 2619 | 'result must be a callable, which is invoked with the function ' |
| 2620 | 'object\n' |
| 2621 | 'as the only argument. The returned value is bound to the ' |
| 2622 | 'function name\n' |
| 2623 | 'instead of the function object. Multiple decorators are applied ' |
| 2624 | 'in\n' |
| 2625 | 'nested fashion. For example, the following code:\n' |
| 2626 | '\n' |
| 2627 | ' @f1(arg)\n' |
| 2628 | ' @f2\n' |
| 2629 | ' def func(): pass\n' |
| 2630 | '\n' |
| 2631 | 'is equivalent to:\n' |
| 2632 | '\n' |
| 2633 | ' def func(): pass\n' |
| 2634 | ' func = f1(arg)(f2(func))\n' |
| 2635 | '\n' |
| 2636 | 'When one or more top-level *parameters* have the form ' |
| 2637 | '*parameter* "="\n' |
| 2638 | '*expression*, the function is said to have "default parameter ' |
| 2639 | 'values."\n' |
| 2640 | 'For a parameter with a default value, the corresponding ' |
| 2641 | '*argument* may\n' |
| 2642 | "be omitted from a call, in which case the parameter's default " |
| 2643 | 'value is\n' |
| 2644 | 'substituted. If a parameter has a default value, all following\n' |
| 2645 | 'parameters must also have a default value --- this is a ' |
| 2646 | 'syntactic\n' |
| 2647 | 'restriction that is not expressed by the grammar.\n' |
| 2648 | '\n' |
| 2649 | '**Default parameter values are evaluated when the function ' |
| 2650 | 'definition\n' |
| 2651 | 'is executed.** This means that the expression is evaluated ' |
| 2652 | 'once, when\n' |
| 2653 | 'the function is defined, and that the same "pre-computed" value ' |
| 2654 | 'is\n' |
| 2655 | 'used for each call. This is especially important to understand ' |
| 2656 | 'when a\n' |
| 2657 | 'default parameter is a mutable object, such as a list or a ' |
| 2658 | 'dictionary:\n' |
| 2659 | 'if the function modifies the object (e.g. by appending an item ' |
| 2660 | 'to a\n' |
| 2661 | 'list), the default value is in effect modified. This is ' |
| 2662 | 'generally not\n' |
| 2663 | 'what was intended. A way around this is to use "None" as the\n' |
| 2664 | 'default, and explicitly test for it in the body of the function, ' |
| 2665 | 'e.g.:\n' |
| 2666 | '\n' |
| 2667 | ' def whats_on_the_telly(penguin=None):\n' |
| 2668 | ' if penguin is None:\n' |
| 2669 | ' penguin = []\n' |
| 2670 | ' penguin.append("property of the zoo")\n' |
| 2671 | ' return penguin\n' |
| 2672 | '\n' |
| 2673 | 'Function call semantics are described in more detail in section ' |
| 2674 | 'Calls.\n' |
| 2675 | 'A function call always assigns values to all parameters ' |
| 2676 | 'mentioned in\n' |
| 2677 | 'the parameter list, either from position arguments, from ' |
| 2678 | 'keyword\n' |
| 2679 | 'arguments, or from default values. If the form ""*identifier"" ' |
| 2680 | 'is\n' |
| 2681 | 'present, it is initialized to a tuple receiving any excess ' |
| 2682 | 'positional\n' |
| 2683 | 'parameters, defaulting to the empty tuple. If the form\n' |
| 2684 | '""**identifier"" is present, it is initialized to a new ' |
| 2685 | 'dictionary\n' |
| 2686 | 'receiving any excess keyword arguments, defaulting to a new ' |
| 2687 | 'empty\n' |
| 2688 | 'dictionary.\n' |
| 2689 | '\n' |
| 2690 | 'It is also possible to create anonymous functions (functions not ' |
| 2691 | 'bound\n' |
| 2692 | 'to a name), for immediate use in expressions. This uses lambda\n' |
| 2693 | 'expressions, described in section Lambdas. Note that the ' |
| 2694 | 'lambda\n' |
| 2695 | 'expression is merely a shorthand for a simplified function ' |
| 2696 | 'definition;\n' |
| 2697 | 'a function defined in a ""def"" statement can be passed around ' |
| 2698 | 'or\n' |
| 2699 | 'assigned to another name just like a function defined by a ' |
| 2700 | 'lambda\n' |
| 2701 | 'expression. The ""def"" form is actually more powerful since ' |
| 2702 | 'it\n' |
| 2703 | 'allows the execution of multiple statements.\n' |
| 2704 | '\n' |
| 2705 | "**Programmer's note:** Functions are first-class objects. A " |
| 2706 | '""def""\n' |
| 2707 | 'form executed inside a function definition defines a local ' |
| 2708 | 'function\n' |
| 2709 | 'that can be returned or passed around. Free variables used in ' |
| 2710 | 'the\n' |
| 2711 | 'nested function can access the local variables of the function\n' |
| 2712 | 'containing the def. See section Naming and binding for ' |
| 2713 | 'details.\n' |
| 2714 | '\n' |
| 2715 | '\n' |
| 2716 | 'Class definitions\n' |
| 2717 | '=================\n' |
| 2718 | '\n' |
| 2719 | 'A class definition defines a class object (see section The ' |
| 2720 | 'standard\n' |
| 2721 | 'type hierarchy):\n' |
| 2722 | '\n' |
| 2723 | ' classdef ::= "class" classname [inheritance] ":" suite\n' |
| 2724 | ' inheritance ::= "(" [expression_list] ")"\n' |
| 2725 | ' classname ::= identifier\n' |
| 2726 | '\n' |
| 2727 | 'A class definition is an executable statement. It first ' |
| 2728 | 'evaluates the\n' |
| 2729 | 'inheritance list, if present. Each item in the inheritance ' |
| 2730 | 'list\n' |
| 2731 | 'should evaluate to a class object or class type which allows\n' |
| 2732 | "subclassing. The class's suite is then executed in a new " |
| 2733 | 'execution\n' |
| 2734 | 'frame (see section Naming and binding), using a newly created ' |
| 2735 | 'local\n' |
| 2736 | 'namespace and the original global namespace. (Usually, the ' |
| 2737 | 'suite\n' |
| 2738 | "contains only function definitions.) When the class's suite " |
| 2739 | 'finishes\n' |
| 2740 | 'execution, its execution frame is discarded but its local ' |
| 2741 | 'namespace is\n' |
| 2742 | 'saved. [4] A class object is then created using the inheritance ' |
| 2743 | 'list\n' |
| 2744 | 'for the base classes and the saved local namespace for the ' |
| 2745 | 'attribute\n' |
| 2746 | 'dictionary. The class name is bound to this class object in ' |
| 2747 | 'the\n' |
| 2748 | 'original local namespace.\n' |
| 2749 | '\n' |
| 2750 | "**Programmer's note:** Variables defined in the class definition " |
| 2751 | 'are\n' |
| 2752 | 'class variables; they are shared by all instances. To create ' |
| 2753 | 'instance\n' |
| 2754 | 'variables, they can be set in a method with "self.name = ' |
| 2755 | 'value". Both\n' |
| 2756 | 'class and instance variables are accessible through the ' |
| 2757 | 'notation\n' |
| 2758 | '""self.name"", and an instance variable hides a class variable ' |
| 2759 | 'with\n' |
| 2760 | 'the same name when accessed in this way. Class variables can be ' |
| 2761 | 'used\n' |
| 2762 | 'as defaults for instance variables, but using mutable values ' |
| 2763 | 'there can\n' |
| 2764 | 'lead to unexpected results. For *new-style class*es, ' |
| 2765 | 'descriptors can\n' |
| 2766 | 'be used to create instance variables with different ' |
| 2767 | 'implementation\n' |
| 2768 | 'details.\n' |
| 2769 | '\n' |
| 2770 | 'Class definitions, like function definitions, may be wrapped by ' |
| 2771 | 'one or\n' |
| 2772 | 'more *decorator* expressions. The evaluation rules for the ' |
| 2773 | 'decorator\n' |
| 2774 | 'expressions are the same as for functions. The result must be a ' |
| 2775 | 'class\n' |
| 2776 | 'object, which is then bound to the class name.\n' |
| 2777 | '\n' |
| 2778 | '-[ Footnotes ]-\n' |
| 2779 | '\n' |
| 2780 | '[1] The exception is propagated to the invocation stack unless\n' |
| 2781 | ' there is a "finally" clause which happens to raise another\n' |
| 2782 | ' exception. That new exception causes the old one to be ' |
| 2783 | 'lost.\n' |
| 2784 | '\n' |
| 2785 | '[2] Currently, control "flows off the end" except in the case ' |
| 2786 | 'of\n' |
| 2787 | ' an exception or the execution of a "return", "continue", or\n' |
| 2788 | ' "break" statement.\n' |
| 2789 | '\n' |
| 2790 | '[3] A string literal appearing as the first statement in the\n' |
| 2791 | ' function body is transformed into the function\'s "__doc__"\n' |
| 2792 | " attribute and therefore the function's *docstring*.\n" |
| 2793 | '\n' |
| 2794 | '[4] A string literal appearing as the first statement in the ' |
| 2795 | 'class\n' |
| 2796 | ' body is transformed into the namespace\'s "__doc__" item ' |
| 2797 | 'and\n' |
| 2798 | " therefore the class's *docstring*.\n", |
| 2799 | 'context-managers': '\n' |
| 2800 | 'With Statement Context Managers\n' |
| 2801 | '*******************************\n' |
| 2802 | '\n' |
| 2803 | 'New in version 2.5.\n' |
| 2804 | '\n' |
| 2805 | 'A *context manager* is an object that defines the ' |
| 2806 | 'runtime context to\n' |
| 2807 | 'be established when executing a "with" statement. The ' |
| 2808 | 'context manager\n' |
| 2809 | 'handles the entry into, and the exit from, the desired ' |
| 2810 | 'runtime context\n' |
| 2811 | 'for the execution of the block of code. Context ' |
| 2812 | 'managers are normally\n' |
| 2813 | 'invoked using the "with" statement (described in section ' |
| 2814 | 'The with\n' |
| 2815 | 'statement), but can also be used by directly invoking ' |
| 2816 | 'their methods.\n' |
| 2817 | '\n' |
| 2818 | 'Typical uses of context managers include saving and ' |
| 2819 | 'restoring various\n' |
| 2820 | 'kinds of global state, locking and unlocking resources, ' |
| 2821 | 'closing opened\n' |
| 2822 | 'files, etc.\n' |
| 2823 | '\n' |
| 2824 | 'For more information on context managers, see Context ' |
| 2825 | 'Manager Types.\n' |
| 2826 | '\n' |
| 2827 | 'object.__enter__(self)\n' |
| 2828 | '\n' |
| 2829 | ' Enter the runtime context related to this object. The ' |
| 2830 | '"with"\n' |
| 2831 | " statement will bind this method's return value to the " |
| 2832 | 'target(s)\n' |
| 2833 | ' specified in the "as" clause of the statement, if ' |
| 2834 | 'any.\n' |
| 2835 | '\n' |
| 2836 | 'object.__exit__(self, exc_type, exc_value, traceback)\n' |
| 2837 | '\n' |
| 2838 | ' Exit the runtime context related to this object. The ' |
| 2839 | 'parameters\n' |
| 2840 | ' describe the exception that caused the context to be ' |
| 2841 | 'exited. If the\n' |
| 2842 | ' context was exited without an exception, all three ' |
| 2843 | 'arguments will\n' |
| 2844 | ' be "None".\n' |
| 2845 | '\n' |
| 2846 | ' If an exception is supplied, and the method wishes to ' |
| 2847 | 'suppress the\n' |
| 2848 | ' exception (i.e., prevent it from being propagated), ' |
| 2849 | 'it should\n' |
| 2850 | ' return a true value. Otherwise, the exception will be ' |
| 2851 | 'processed\n' |
| 2852 | ' normally upon exit from this method.\n' |
| 2853 | '\n' |
| 2854 | ' Note that "__exit__()" methods should not reraise the ' |
| 2855 | 'passed-in\n' |
| 2856 | " exception; this is the caller's responsibility.\n" |
| 2857 | '\n' |
| 2858 | 'See also:\n' |
| 2859 | '\n' |
| 2860 | ' **PEP 343** - The "with" statement\n' |
| 2861 | ' The specification, background, and examples for the ' |
| 2862 | 'Python "with"\n' |
| 2863 | ' statement.\n', |
| 2864 | 'continue': '\n' |
| 2865 | 'The "continue" statement\n' |
| 2866 | '************************\n' |
| 2867 | '\n' |
| 2868 | ' continue_stmt ::= "continue"\n' |
| 2869 | '\n' |
| 2870 | '"continue" may only occur syntactically nested in a "for" or ' |
| 2871 | '"while"\n' |
| 2872 | 'loop, but not nested in a function or class definition or ' |
| 2873 | '"finally"\n' |
| 2874 | 'clause within that loop. It continues with the next cycle of ' |
| 2875 | 'the\n' |
| 2876 | 'nearest enclosing loop.\n' |
| 2877 | '\n' |
| 2878 | 'When "continue" passes control out of a "try" statement with a\n' |
| 2879 | '"finally" clause, that "finally" clause is executed before ' |
| 2880 | 'really\n' |
| 2881 | 'starting the next loop cycle.\n', |
| 2882 | 'conversions': '\n' |
| 2883 | 'Arithmetic conversions\n' |
| 2884 | '**********************\n' |
| 2885 | '\n' |
| 2886 | 'When a description of an arithmetic operator below uses the ' |
| 2887 | 'phrase\n' |
| 2888 | '"the numeric arguments are converted to a common type," the ' |
| 2889 | 'arguments\n' |
| 2890 | 'are coerced using the coercion rules listed at Coercion ' |
| 2891 | 'rules. If\n' |
| 2892 | 'both arguments are standard numeric types, the following ' |
| 2893 | 'coercions are\n' |
| 2894 | 'applied:\n' |
| 2895 | '\n' |
| 2896 | '* If either argument is a complex number, the other is ' |
| 2897 | 'converted to\n' |
| 2898 | ' complex;\n' |
| 2899 | '\n' |
| 2900 | '* otherwise, if either argument is a floating point number, ' |
| 2901 | 'the\n' |
| 2902 | ' other is converted to floating point;\n' |
| 2903 | '\n' |
| 2904 | '* otherwise, if either argument is a long integer, the other ' |
| 2905 | 'is\n' |
| 2906 | ' converted to long integer;\n' |
| 2907 | '\n' |
| 2908 | '* otherwise, both must be plain integers and no conversion ' |
| 2909 | 'is\n' |
| 2910 | ' necessary.\n' |
| 2911 | '\n' |
| 2912 | 'Some additional rules apply for certain operators (e.g., a ' |
| 2913 | 'string left\n' |
| 2914 | "argument to the '%' operator). Extensions can define their " |
| 2915 | 'own\n' |
| 2916 | 'coercions.\n', |
| 2917 | 'customization': '\n' |
| 2918 | 'Basic customization\n' |
| 2919 | '*******************\n' |
| 2920 | '\n' |
| 2921 | 'object.__new__(cls[, ...])\n' |
| 2922 | '\n' |
| 2923 | ' Called to create a new instance of class *cls*. ' |
| 2924 | '"__new__()" is a\n' |
| 2925 | ' static method (special-cased so you need not declare it ' |
| 2926 | 'as such)\n' |
| 2927 | ' that takes the class of which an instance was requested ' |
| 2928 | 'as its\n' |
| 2929 | ' first argument. The remaining arguments are those ' |
| 2930 | 'passed to the\n' |
| 2931 | ' object constructor expression (the call to the class). ' |
| 2932 | 'The return\n' |
| 2933 | ' value of "__new__()" should be the new object instance ' |
| 2934 | '(usually an\n' |
| 2935 | ' instance of *cls*).\n' |
| 2936 | '\n' |
| 2937 | ' Typical implementations create a new instance of the ' |
| 2938 | 'class by\n' |
| 2939 | ' invoking the superclass\'s "__new__()" method using\n' |
| 2940 | ' "super(currentclass, cls).__new__(cls[, ...])" with ' |
| 2941 | 'appropriate\n' |
| 2942 | ' arguments and then modifying the newly-created instance ' |
| 2943 | 'as\n' |
| 2944 | ' necessary before returning it.\n' |
| 2945 | '\n' |
| 2946 | ' If "__new__()" returns an instance of *cls*, then the ' |
| 2947 | 'new\n' |
| 2948 | ' instance\'s "__init__()" method will be invoked like\n' |
| 2949 | ' "__init__(self[, ...])", where *self* is the new ' |
| 2950 | 'instance and the\n' |
| 2951 | ' remaining arguments are the same as were passed to ' |
| 2952 | '"__new__()".\n' |
| 2953 | '\n' |
| 2954 | ' If "__new__()" does not return an instance of *cls*, ' |
| 2955 | 'then the new\n' |
| 2956 | ' instance\'s "__init__()" method will not be invoked.\n' |
| 2957 | '\n' |
| 2958 | ' "__new__()" is intended mainly to allow subclasses of ' |
| 2959 | 'immutable\n' |
| 2960 | ' types (like int, str, or tuple) to customize instance ' |
| 2961 | 'creation. It\n' |
| 2962 | ' is also commonly overridden in custom metaclasses in ' |
| 2963 | 'order to\n' |
| 2964 | ' customize class creation.\n' |
| 2965 | '\n' |
| 2966 | 'object.__init__(self[, ...])\n' |
| 2967 | '\n' |
| 2968 | ' Called after the instance has been created (by ' |
| 2969 | '"__new__()"), but\n' |
| 2970 | ' before it is returned to the caller. The arguments are ' |
| 2971 | 'those\n' |
| 2972 | ' passed to the class constructor expression. If a base ' |
| 2973 | 'class has an\n' |
| 2974 | ' "__init__()" method, the derived class\'s "__init__()" ' |
| 2975 | 'method, if\n' |
| 2976 | ' any, must explicitly call it to ensure proper ' |
| 2977 | 'initialization of the\n' |
| 2978 | ' base class part of the instance; for example:\n' |
| 2979 | ' "BaseClass.__init__(self, [args...])".\n' |
| 2980 | '\n' |
| 2981 | ' Because "__new__()" and "__init__()" work together in ' |
| 2982 | 'constructing\n' |
| 2983 | ' objects ("__new__()" to create it, and "__init__()" to ' |
| 2984 | 'customise\n' |
| 2985 | ' it), no non-"None" value may be returned by ' |
| 2986 | '"__init__()"; doing so\n' |
| 2987 | ' will cause a "TypeError" to be raised at runtime.\n' |
| 2988 | '\n' |
| 2989 | 'object.__del__(self)\n' |
| 2990 | '\n' |
| 2991 | ' Called when the instance is about to be destroyed. This ' |
| 2992 | 'is also\n' |
| 2993 | ' called a destructor. If a base class has a "__del__()" ' |
| 2994 | 'method, the\n' |
| 2995 | ' derived class\'s "__del__()" method, if any, must ' |
| 2996 | 'explicitly call it\n' |
| 2997 | ' to ensure proper deletion of the base class part of the ' |
| 2998 | 'instance.\n' |
| 2999 | ' Note that it is possible (though not recommended!) for ' |
| 3000 | 'the\n' |
| 3001 | ' "__del__()" method to postpone destruction of the ' |
| 3002 | 'instance by\n' |
| 3003 | ' creating a new reference to it. It may then be called ' |
| 3004 | 'at a later\n' |
| 3005 | ' time when this new reference is deleted. It is not ' |
| 3006 | 'guaranteed that\n' |
| 3007 | ' "__del__()" methods are called for objects that still ' |
| 3008 | 'exist when\n' |
| 3009 | ' the interpreter exits.\n' |
| 3010 | '\n' |
| 3011 | ' Note: "del x" doesn\'t directly call "x.__del__()" --- ' |
| 3012 | 'the former\n' |
| 3013 | ' decrements the reference count for "x" by one, and the ' |
| 3014 | 'latter is\n' |
| 3015 | ' only called when "x"\'s reference count reaches zero. ' |
| 3016 | 'Some common\n' |
| 3017 | ' situations that may prevent the reference count of an ' |
| 3018 | 'object from\n' |
| 3019 | ' going to zero include: circular references between ' |
| 3020 | 'objects (e.g.,\n' |
| 3021 | ' a doubly-linked list or a tree data structure with ' |
| 3022 | 'parent and\n' |
| 3023 | ' child pointers); a reference to the object on the ' |
| 3024 | 'stack frame of\n' |
| 3025 | ' a function that caught an exception (the traceback ' |
| 3026 | 'stored in\n' |
| 3027 | ' "sys.exc_traceback" keeps the stack frame alive); or a ' |
| 3028 | 'reference\n' |
| 3029 | ' to the object on the stack frame that raised an ' |
| 3030 | 'unhandled\n' |
| 3031 | ' exception in interactive mode (the traceback stored ' |
| 3032 | 'in\n' |
| 3033 | ' "sys.last_traceback" keeps the stack frame alive). ' |
| 3034 | 'The first\n' |
| 3035 | ' situation can only be remedied by explicitly breaking ' |
| 3036 | 'the cycles;\n' |
| 3037 | ' the latter two situations can be resolved by storing ' |
| 3038 | '"None" in\n' |
| 3039 | ' "sys.exc_traceback" or "sys.last_traceback". Circular ' |
| 3040 | 'references\n' |
| 3041 | ' which are garbage are detected when the option cycle ' |
| 3042 | 'detector is\n' |
| 3043 | " enabled (it's on by default), but can only be cleaned " |
| 3044 | 'up if there\n' |
| 3045 | ' are no Python-level "__del__()" methods involved. ' |
| 3046 | 'Refer to the\n' |
| 3047 | ' documentation for the "gc" module for more information ' |
| 3048 | 'about how\n' |
| 3049 | ' "__del__()" methods are handled by the cycle ' |
| 3050 | 'detector,\n' |
| 3051 | ' particularly the description of the "garbage" value.\n' |
| 3052 | '\n' |
| 3053 | ' Warning: Due to the precarious circumstances under ' |
| 3054 | 'which\n' |
| 3055 | ' "__del__()" methods are invoked, exceptions that occur ' |
| 3056 | 'during\n' |
| 3057 | ' their execution are ignored, and a warning is printed ' |
| 3058 | 'to\n' |
| 3059 | ' "sys.stderr" instead. Also, when "__del__()" is ' |
| 3060 | 'invoked in\n' |
| 3061 | ' response to a module being deleted (e.g., when ' |
| 3062 | 'execution of the\n' |
| 3063 | ' program is done), other globals referenced by the ' |
| 3064 | '"__del__()"\n' |
| 3065 | ' method may already have been deleted or in the process ' |
| 3066 | 'of being\n' |
| 3067 | ' torn down (e.g. the import machinery shutting down). ' |
| 3068 | 'For this\n' |
| 3069 | ' reason, "__del__()" methods should do the absolute ' |
| 3070 | 'minimum needed\n' |
| 3071 | ' to maintain external invariants. Starting with ' |
| 3072 | 'version 1.5,\n' |
| 3073 | ' Python guarantees that globals whose name begins with ' |
| 3074 | 'a single\n' |
| 3075 | ' underscore are deleted from their module before other ' |
| 3076 | 'globals are\n' |
| 3077 | ' deleted; if no other references to such globals exist, ' |
| 3078 | 'this may\n' |
| 3079 | ' help in assuring that imported modules are still ' |
| 3080 | 'available at the\n' |
| 3081 | ' time when the "__del__()" method is called.\n' |
| 3082 | '\n' |
| 3083 | ' See also the "-R" command-line option.\n' |
| 3084 | '\n' |
| 3085 | 'object.__repr__(self)\n' |
| 3086 | '\n' |
| 3087 | ' Called by the "repr()" built-in function and by string ' |
| 3088 | 'conversions\n' |
| 3089 | ' (reverse quotes) to compute the "official" string ' |
| 3090 | 'representation of\n' |
| 3091 | ' an object. If at all possible, this should look like a ' |
| 3092 | 'valid\n' |
| 3093 | ' Python expression that could be used to recreate an ' |
| 3094 | 'object with the\n' |
| 3095 | ' same value (given an appropriate environment). If this ' |
| 3096 | 'is not\n' |
| 3097 | ' possible, a string of the form "<...some useful ' |
| 3098 | 'description...>"\n' |
| 3099 | ' should be returned. The return value must be a string ' |
| 3100 | 'object. If a\n' |
| 3101 | ' class defines "__repr__()" but not "__str__()", then ' |
| 3102 | '"__repr__()"\n' |
| 3103 | ' is also used when an "informal" string representation of ' |
| 3104 | 'instances\n' |
| 3105 | ' of that class is required.\n' |
| 3106 | '\n' |
| 3107 | ' This is typically used for debugging, so it is important ' |
| 3108 | 'that the\n' |
| 3109 | ' representation is information-rich and unambiguous.\n' |
| 3110 | '\n' |
| 3111 | 'object.__str__(self)\n' |
| 3112 | '\n' |
| 3113 | ' Called by the "str()" built-in function and by the ' |
| 3114 | '"print"\n' |
| 3115 | ' statement to compute the "informal" string ' |
| 3116 | 'representation of an\n' |
| 3117 | ' object. This differs from "__repr__()" in that it does ' |
| 3118 | 'not have to\n' |
| 3119 | ' be a valid Python expression: a more convenient or ' |
| 3120 | 'concise\n' |
| 3121 | ' representation may be used instead. The return value ' |
| 3122 | 'must be a\n' |
| 3123 | ' string object.\n' |
| 3124 | '\n' |
| 3125 | 'object.__lt__(self, other)\n' |
| 3126 | 'object.__le__(self, other)\n' |
| 3127 | 'object.__eq__(self, other)\n' |
| 3128 | 'object.__ne__(self, other)\n' |
| 3129 | 'object.__gt__(self, other)\n' |
| 3130 | 'object.__ge__(self, other)\n' |
| 3131 | '\n' |
| 3132 | ' New in version 2.1.\n' |
| 3133 | '\n' |
| 3134 | ' These are the so-called "rich comparison" methods, and ' |
| 3135 | 'are called\n' |
| 3136 | ' for comparison operators in preference to "__cmp__()" ' |
| 3137 | 'below. The\n' |
| 3138 | ' correspondence between operator symbols and method names ' |
| 3139 | 'is as\n' |
| 3140 | ' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls ' |
| 3141 | '"x.__le__(y)",\n' |
| 3142 | ' "x==y" calls "x.__eq__(y)", "x!=y" and "x<>y" call ' |
| 3143 | '"x.__ne__(y)",\n' |
| 3144 | ' "x>y" calls "x.__gt__(y)", and "x>=y" calls ' |
| 3145 | '"x.__ge__(y)".\n' |
| 3146 | '\n' |
| 3147 | ' A rich comparison method may return the singleton ' |
| 3148 | '"NotImplemented"\n' |
| 3149 | ' if it does not implement the operation for a given pair ' |
| 3150 | 'of\n' |
| 3151 | ' arguments. By convention, "False" and "True" are ' |
| 3152 | 'returned for a\n' |
| 3153 | ' successful comparison. However, these methods can return ' |
| 3154 | 'any value,\n' |
| 3155 | ' so if the comparison operator is used in a Boolean ' |
| 3156 | 'context (e.g.,\n' |
| 3157 | ' in the condition of an "if" statement), Python will call ' |
| 3158 | '"bool()"\n' |
| 3159 | ' on the value to determine if the result is true or ' |
| 3160 | 'false.\n' |
| 3161 | '\n' |
| 3162 | ' There are no implied relationships among the comparison ' |
| 3163 | 'operators.\n' |
| 3164 | ' The truth of "x==y" does not imply that "x!=y" is ' |
| 3165 | 'false.\n' |
| 3166 | ' Accordingly, when defining "__eq__()", one should also ' |
| 3167 | 'define\n' |
| 3168 | ' "__ne__()" so that the operators will behave as ' |
| 3169 | 'expected. See the\n' |
| 3170 | ' paragraph on "__hash__()" for some important notes on ' |
| 3171 | 'creating\n' |
| 3172 | ' *hashable* objects which support custom comparison ' |
| 3173 | 'operations and\n' |
| 3174 | ' are usable as dictionary keys.\n' |
| 3175 | '\n' |
| 3176 | ' There are no swapped-argument versions of these methods ' |
| 3177 | '(to be used\n' |
| 3178 | ' when the left argument does not support the operation ' |
| 3179 | 'but the right\n' |
| 3180 | ' argument does); rather, "__lt__()" and "__gt__()" are ' |
| 3181 | "each other's\n" |
| 3182 | ' reflection, "__le__()" and "__ge__()" are each other\'s ' |
| 3183 | 'reflection,\n' |
| 3184 | ' and "__eq__()" and "__ne__()" are their own reflection.\n' |
| 3185 | '\n' |
| 3186 | ' Arguments to rich comparison methods are never coerced.\n' |
| 3187 | '\n' |
| 3188 | ' To automatically generate ordering operations from a ' |
| 3189 | 'single root\n' |
| 3190 | ' operation, see "functools.total_ordering()".\n' |
| 3191 | '\n' |
| 3192 | 'object.__cmp__(self, other)\n' |
| 3193 | '\n' |
| 3194 | ' Called by comparison operations if rich comparison (see ' |
| 3195 | 'above) is\n' |
| 3196 | ' not defined. Should return a negative integer if "self ' |
| 3197 | '< other",\n' |
| 3198 | ' zero if "self == other", a positive integer if "self > ' |
| 3199 | 'other". If\n' |
| 3200 | ' no "__cmp__()", "__eq__()" or "__ne__()" operation is ' |
| 3201 | 'defined,\n' |
| 3202 | ' class instances are compared by object identity ' |
| 3203 | '("address"). See\n' |
| 3204 | ' also the description of "__hash__()" for some important ' |
| 3205 | 'notes on\n' |
| 3206 | ' creating *hashable* objects which support custom ' |
| 3207 | 'comparison\n' |
| 3208 | ' operations and are usable as dictionary keys. (Note: ' |
| 3209 | 'the\n' |
| 3210 | ' restriction that exceptions are not propagated by ' |
| 3211 | '"__cmp__()" has\n' |
| 3212 | ' been removed since Python 1.5.)\n' |
| 3213 | '\n' |
| 3214 | 'object.__rcmp__(self, other)\n' |
| 3215 | '\n' |
| 3216 | ' Changed in version 2.1: No longer supported.\n' |
| 3217 | '\n' |
| 3218 | 'object.__hash__(self)\n' |
| 3219 | '\n' |
| 3220 | ' Called by built-in function "hash()" and for operations ' |
| 3221 | 'on members\n' |
| 3222 | ' of hashed collections including "set", "frozenset", and ' |
| 3223 | '"dict".\n' |
| 3224 | ' "__hash__()" should return an integer. The only ' |
| 3225 | 'required property\n' |
| 3226 | ' is that objects which compare equal have the same hash ' |
| 3227 | 'value; it is\n' |
| 3228 | ' advised to mix together the hash values of the ' |
| 3229 | 'components of the\n' |
| 3230 | ' object that also play a part in comparison of objects by ' |
| 3231 | 'packing\n' |
| 3232 | ' them into a tuple and hashing the tuple. Example:\n' |
| 3233 | '\n' |
| 3234 | ' def __hash__(self):\n' |
| 3235 | ' return hash((self.name, self.nick, self.color))\n' |
| 3236 | '\n' |
| 3237 | ' If a class does not define a "__cmp__()" or "__eq__()" ' |
| 3238 | 'method it\n' |
| 3239 | ' should not define a "__hash__()" operation either; if it ' |
| 3240 | 'defines\n' |
| 3241 | ' "__cmp__()" or "__eq__()" but not "__hash__()", its ' |
| 3242 | 'instances will\n' |
| 3243 | ' not be usable in hashed collections. If a class defines ' |
| 3244 | 'mutable\n' |
| 3245 | ' objects and implements a "__cmp__()" or "__eq__()" ' |
| 3246 | 'method, it\n' |
| 3247 | ' should not implement "__hash__()", since hashable ' |
| 3248 | 'collection\n' |
| 3249 | " implementations require that an object's hash value is " |
| 3250 | 'immutable\n' |
| 3251 | " (if the object's hash value changes, it will be in the " |
| 3252 | 'wrong hash\n' |
| 3253 | ' bucket).\n' |
| 3254 | '\n' |
| 3255 | ' User-defined classes have "__cmp__()" and "__hash__()" ' |
| 3256 | 'methods by\n' |
| 3257 | ' default; with them, all objects compare unequal (except ' |
| 3258 | 'with\n' |
| 3259 | ' themselves) and "x.__hash__()" returns a result derived ' |
| 3260 | 'from\n' |
| 3261 | ' "id(x)".\n' |
| 3262 | '\n' |
| 3263 | ' Classes which inherit a "__hash__()" method from a ' |
| 3264 | 'parent class but\n' |
| 3265 | ' change the meaning of "__cmp__()" or "__eq__()" such ' |
| 3266 | 'that the hash\n' |
| 3267 | ' value returned is no longer appropriate (e.g. by ' |
| 3268 | 'switching to a\n' |
| 3269 | ' value-based concept of equality instead of the default ' |
| 3270 | 'identity\n' |
| 3271 | ' based equality) can explicitly flag themselves as being ' |
| 3272 | 'unhashable\n' |
| 3273 | ' by setting "__hash__ = None" in the class definition. ' |
| 3274 | 'Doing so\n' |
| 3275 | ' means that not only will instances of the class raise ' |
| 3276 | 'an\n' |
| 3277 | ' appropriate "TypeError" when a program attempts to ' |
| 3278 | 'retrieve their\n' |
| 3279 | ' hash value, but they will also be correctly identified ' |
| 3280 | 'as\n' |
| 3281 | ' unhashable when checking "isinstance(obj, ' |
| 3282 | 'collections.Hashable)"\n' |
| 3283 | ' (unlike classes which define their own "__hash__()" to ' |
| 3284 | 'explicitly\n' |
| 3285 | ' raise "TypeError").\n' |
| 3286 | '\n' |
| 3287 | ' Changed in version 2.5: "__hash__()" may now also return ' |
| 3288 | 'a long\n' |
| 3289 | ' integer object; the 32-bit integer is then derived from ' |
| 3290 | 'the hash of\n' |
| 3291 | ' that object.\n' |
| 3292 | '\n' |
| 3293 | ' Changed in version 2.6: "__hash__" may now be set to ' |
| 3294 | '"None" to\n' |
| 3295 | ' explicitly flag instances of a class as unhashable.\n' |
| 3296 | '\n' |
| 3297 | 'object.__nonzero__(self)\n' |
| 3298 | '\n' |
| 3299 | ' Called to implement truth value testing and the built-in ' |
| 3300 | 'operation\n' |
| 3301 | ' "bool()"; should return "False" or "True", or their ' |
| 3302 | 'integer\n' |
| 3303 | ' equivalents "0" or "1". When this method is not ' |
| 3304 | 'defined,\n' |
| 3305 | ' "__len__()" is called, if it is defined, and the object ' |
| 3306 | 'is\n' |
| 3307 | ' considered true if its result is nonzero. If a class ' |
| 3308 | 'defines\n' |
| 3309 | ' neither "__len__()" nor "__nonzero__()", all its ' |
| 3310 | 'instances are\n' |
| 3311 | ' considered true.\n' |
| 3312 | '\n' |
| 3313 | 'object.__unicode__(self)\n' |
| 3314 | '\n' |
| 3315 | ' Called to implement "unicode()" built-in; should return ' |
| 3316 | 'a Unicode\n' |
| 3317 | ' object. When this method is not defined, string ' |
| 3318 | 'conversion is\n' |
| 3319 | ' attempted, and the result of string conversion is ' |
| 3320 | 'converted to\n' |
| 3321 | ' Unicode using the system default encoding.\n', |
| 3322 | 'debugger': '\n' |
| 3323 | '"pdb" --- The Python Debugger\n' |
| 3324 | '*****************************\n' |
| 3325 | '\n' |
| 3326 | '**Source code:** Lib/pdb.py\n' |
| 3327 | '\n' |
| 3328 | '======================================================================\n' |
| 3329 | '\n' |
| 3330 | 'The module "pdb" defines an interactive source code debugger ' |
| 3331 | 'for\n' |
| 3332 | 'Python programs. It supports setting (conditional) breakpoints ' |
| 3333 | 'and\n' |
| 3334 | 'single stepping at the source line level, inspection of stack ' |
| 3335 | 'frames,\n' |
| 3336 | 'source code listing, and evaluation of arbitrary Python code in ' |
| 3337 | 'the\n' |
| 3338 | 'context of any stack frame. It also supports post-mortem ' |
| 3339 | 'debugging\n' |
| 3340 | 'and can be called under program control.\n' |
| 3341 | '\n' |
| 3342 | 'The debugger is extensible --- it is actually defined as the ' |
| 3343 | 'class\n' |
| 3344 | '"Pdb". This is currently undocumented but easily understood by ' |
| 3345 | 'reading\n' |
| 3346 | 'the source. The extension interface uses the modules "bdb" and ' |
| 3347 | '"cmd".\n' |
| 3348 | '\n' |
| 3349 | 'The debugger\'s prompt is "(Pdb)". Typical usage to run a ' |
| 3350 | 'program under\n' |
| 3351 | 'control of the debugger is:\n' |
| 3352 | '\n' |
| 3353 | ' >>> import pdb\n' |
| 3354 | ' >>> import mymodule\n' |
| 3355 | " >>> pdb.run('mymodule.test()')\n" |
| 3356 | ' > <string>(0)?()\n' |
| 3357 | ' (Pdb) continue\n' |
| 3358 | ' > <string>(1)?()\n' |
| 3359 | ' (Pdb) continue\n' |
| 3360 | " NameError: 'spam'\n" |
| 3361 | ' > <string>(1)?()\n' |
| 3362 | ' (Pdb)\n' |
| 3363 | '\n' |
| 3364 | '"pdb.py" can also be invoked as a script to debug other ' |
| 3365 | 'scripts. For\n' |
| 3366 | 'example:\n' |
| 3367 | '\n' |
| 3368 | ' python -m pdb myscript.py\n' |
| 3369 | '\n' |
| 3370 | 'When invoked as a script, pdb will automatically enter ' |
| 3371 | 'post-mortem\n' |
| 3372 | 'debugging if the program being debugged exits abnormally. After ' |
| 3373 | 'post-\n' |
| 3374 | 'mortem debugging (or after normal exit of the program), pdb ' |
| 3375 | 'will\n' |
| 3376 | "restart the program. Automatic restarting preserves pdb's state " |
| 3377 | '(such\n' |
| 3378 | 'as breakpoints) and in most cases is more useful than quitting ' |
| 3379 | 'the\n' |
| 3380 | "debugger upon program's exit.\n" |
| 3381 | '\n' |
| 3382 | 'New in version 2.4: Restarting post-mortem behavior added.\n' |
| 3383 | '\n' |
| 3384 | 'The typical usage to break into the debugger from a running ' |
| 3385 | 'program is\n' |
| 3386 | 'to insert\n' |
| 3387 | '\n' |
| 3388 | ' import pdb; pdb.set_trace()\n' |
| 3389 | '\n' |
| 3390 | 'at the location you want to break into the debugger. You can ' |
| 3391 | 'then\n' |
| 3392 | 'step through the code following this statement, and continue ' |
| 3393 | 'running\n' |
| 3394 | 'without the debugger using the "c" command.\n' |
| 3395 | '\n' |
| 3396 | 'The typical usage to inspect a crashed program is:\n' |
| 3397 | '\n' |
| 3398 | ' >>> import pdb\n' |
| 3399 | ' >>> import mymodule\n' |
| 3400 | ' >>> mymodule.test()\n' |
| 3401 | ' Traceback (most recent call last):\n' |
| 3402 | ' File "<stdin>", line 1, in <module>\n' |
| 3403 | ' File "./mymodule.py", line 4, in test\n' |
| 3404 | ' test2()\n' |
| 3405 | ' File "./mymodule.py", line 3, in test2\n' |
| 3406 | ' print spam\n' |
| 3407 | ' NameError: spam\n' |
| 3408 | ' >>> pdb.pm()\n' |
| 3409 | ' > ./mymodule.py(3)test2()\n' |
| 3410 | ' -> print spam\n' |
| 3411 | ' (Pdb)\n' |
| 3412 | '\n' |
| 3413 | 'The module defines the following functions; each enters the ' |
| 3414 | 'debugger\n' |
| 3415 | 'in a slightly different way:\n' |
| 3416 | '\n' |
| 3417 | 'pdb.run(statement[, globals[, locals]])\n' |
| 3418 | '\n' |
| 3419 | ' Execute the *statement* (given as a string) under debugger ' |
| 3420 | 'control.\n' |
| 3421 | ' The debugger prompt appears before any code is executed; you ' |
| 3422 | 'can\n' |
| 3423 | ' set breakpoints and type "continue", or you can step through ' |
| 3424 | 'the\n' |
| 3425 | ' statement using "step" or "next" (all these commands are ' |
| 3426 | 'explained\n' |
| 3427 | ' below). The optional *globals* and *locals* arguments ' |
| 3428 | 'specify the\n' |
| 3429 | ' environment in which the code is executed; by default the\n' |
| 3430 | ' dictionary of the module "__main__" is used. (See the ' |
| 3431 | 'explanation\n' |
| 3432 | ' of the "exec" statement or the "eval()" built-in function.)\n' |
| 3433 | '\n' |
| 3434 | 'pdb.runeval(expression[, globals[, locals]])\n' |
| 3435 | '\n' |
| 3436 | ' Evaluate the *expression* (given as a string) under debugger\n' |
| 3437 | ' control. When "runeval()" returns, it returns the value of ' |
| 3438 | 'the\n' |
| 3439 | ' expression. Otherwise this function is similar to "run()".\n' |
| 3440 | '\n' |
| 3441 | 'pdb.runcall(function[, argument, ...])\n' |
| 3442 | '\n' |
| 3443 | ' Call the *function* (a function or method object, not a ' |
| 3444 | 'string)\n' |
| 3445 | ' with the given arguments. When "runcall()" returns, it ' |
| 3446 | 'returns\n' |
| 3447 | ' whatever the function call returned. The debugger prompt ' |
| 3448 | 'appears\n' |
| 3449 | ' as soon as the function is entered.\n' |
| 3450 | '\n' |
| 3451 | 'pdb.set_trace()\n' |
| 3452 | '\n' |
| 3453 | ' Enter the debugger at the calling stack frame. This is ' |
| 3454 | 'useful to\n' |
| 3455 | ' hard-code a breakpoint at a given point in a program, even if ' |
| 3456 | 'the\n' |
| 3457 | ' code is not otherwise being debugged (e.g. when an assertion\n' |
| 3458 | ' fails).\n' |
| 3459 | '\n' |
| 3460 | 'pdb.post_mortem([traceback])\n' |
| 3461 | '\n' |
| 3462 | ' Enter post-mortem debugging of the given *traceback* object. ' |
| 3463 | 'If no\n' |
| 3464 | ' *traceback* is given, it uses the one of the exception that ' |
| 3465 | 'is\n' |
| 3466 | ' currently being handled (an exception must be being handled ' |
| 3467 | 'if the\n' |
| 3468 | ' default is to be used).\n' |
| 3469 | '\n' |
| 3470 | 'pdb.pm()\n' |
| 3471 | '\n' |
| 3472 | ' Enter post-mortem debugging of the traceback found in\n' |
| 3473 | ' "sys.last_traceback".\n' |
| 3474 | '\n' |
| 3475 | 'The "run*" functions and "set_trace()" are aliases for ' |
| 3476 | 'instantiating\n' |
| 3477 | 'the "Pdb" class and calling the method of the same name. If you ' |
| 3478 | 'want\n' |
| 3479 | 'to access further features, you have to do this yourself:\n' |
| 3480 | '\n' |
| 3481 | "class pdb.Pdb(completekey='tab', stdin=None, stdout=None, " |
| 3482 | 'skip=None)\n' |
| 3483 | '\n' |
| 3484 | ' "Pdb" is the debugger class.\n' |
| 3485 | '\n' |
| 3486 | ' The *completekey*, *stdin* and *stdout* arguments are passed ' |
| 3487 | 'to the\n' |
| 3488 | ' underlying "cmd.Cmd" class; see the description there.\n' |
| 3489 | '\n' |
| 3490 | ' The *skip* argument, if given, must be an iterable of ' |
| 3491 | 'glob-style\n' |
| 3492 | ' module name patterns. The debugger will not step into frames ' |
| 3493 | 'that\n' |
| 3494 | ' originate in a module that matches one of these patterns. ' |
| 3495 | '[1]\n' |
| 3496 | '\n' |
| 3497 | ' Example call to enable tracing with *skip*:\n' |
| 3498 | '\n' |
| 3499 | " import pdb; pdb.Pdb(skip=['django.*']).set_trace()\n" |
| 3500 | '\n' |
| 3501 | ' New in version 2.7: The *skip* argument.\n' |
| 3502 | '\n' |
| 3503 | ' run(statement[, globals[, locals]])\n' |
| 3504 | ' runeval(expression[, globals[, locals]])\n' |
| 3505 | ' runcall(function[, argument, ...])\n' |
| 3506 | ' set_trace()\n' |
| 3507 | '\n' |
| 3508 | ' See the documentation for the functions explained above.\n', |
| 3509 | 'del': '\n' |
| 3510 | 'The "del" statement\n' |
| 3511 | '*******************\n' |
| 3512 | '\n' |
| 3513 | ' del_stmt ::= "del" target_list\n' |
| 3514 | '\n' |
| 3515 | 'Deletion is recursively defined very similar to the way assignment ' |
| 3516 | 'is\n' |
| 3517 | 'defined. Rather than spelling it out in full details, here are some\n' |
| 3518 | 'hints.\n' |
| 3519 | '\n' |
| 3520 | 'Deletion of a target list recursively deletes each target, from left\n' |
| 3521 | 'to right.\n' |
| 3522 | '\n' |
| 3523 | 'Deletion of a name removes the binding of that name from the local ' |
| 3524 | 'or\n' |
| 3525 | 'global namespace, depending on whether the name occurs in a "global"\n' |
| 3526 | 'statement in the same code block. If the name is unbound, a\n' |
| 3527 | '"NameError" exception will be raised.\n' |
| 3528 | '\n' |
| 3529 | 'It is illegal to delete a name from the local namespace if it occurs\n' |
| 3530 | 'as a free variable in a nested block.\n' |
| 3531 | '\n' |
| 3532 | 'Deletion of attribute references, subscriptions and slicings is ' |
| 3533 | 'passed\n' |
| 3534 | 'to the primary object involved; deletion of a slicing is in general\n' |
| 3535 | 'equivalent to assignment of an empty slice of the right type (but ' |
| 3536 | 'even\n' |
| 3537 | 'this is determined by the sliced object).\n', |
| 3538 | 'dict': '\n' |
| 3539 | 'Dictionary displays\n' |
| 3540 | '*******************\n' |
| 3541 | '\n' |
| 3542 | 'A dictionary display is a possibly empty series of key/datum pairs\n' |
| 3543 | 'enclosed in curly braces:\n' |
| 3544 | '\n' |
| 3545 | ' dict_display ::= "{" [key_datum_list | dict_comprehension] ' |
| 3546 | '"}"\n' |
| 3547 | ' key_datum_list ::= key_datum ("," key_datum)* [","]\n' |
| 3548 | ' key_datum ::= expression ":" expression\n' |
| 3549 | ' dict_comprehension ::= expression ":" expression comp_for\n' |
| 3550 | '\n' |
| 3551 | 'A dictionary display yields a new dictionary object.\n' |
| 3552 | '\n' |
| 3553 | 'If a comma-separated sequence of key/datum pairs is given, they are\n' |
| 3554 | 'evaluated from left to right to define the entries of the ' |
| 3555 | 'dictionary:\n' |
| 3556 | 'each key object is used as a key into the dictionary to store the\n' |
| 3557 | 'corresponding datum. This means that you can specify the same key\n' |
| 3558 | "multiple times in the key/datum list, and the final dictionary's " |
| 3559 | 'value\n' |
| 3560 | 'for that key will be the last one given.\n' |
| 3561 | '\n' |
| 3562 | 'A dict comprehension, in contrast to list and set comprehensions,\n' |
| 3563 | 'needs two expressions separated with a colon followed by the usual\n' |
| 3564 | '"for" and "if" clauses. When the comprehension is run, the ' |
| 3565 | 'resulting\n' |
| 3566 | 'key and value elements are inserted in the new dictionary in the ' |
| 3567 | 'order\n' |
| 3568 | 'they are produced.\n' |
| 3569 | '\n' |
| 3570 | 'Restrictions on the types of the key values are listed earlier in\n' |
| 3571 | 'section The standard type hierarchy. (To summarize, the key type\n' |
| 3572 | 'should be *hashable*, which excludes all mutable objects.) Clashes\n' |
| 3573 | 'between duplicate keys are not detected; the last datum (textually\n' |
| 3574 | 'rightmost in the display) stored for a given key value prevails.\n', |
| 3575 | 'dynamic-features': '\n' |
| 3576 | 'Interaction with dynamic features\n' |
| 3577 | '*********************************\n' |
| 3578 | '\n' |
| 3579 | 'There are several cases where Python statements are ' |
| 3580 | 'illegal when used\n' |
| 3581 | 'in conjunction with nested scopes that contain free ' |
| 3582 | 'variables.\n' |
| 3583 | '\n' |
| 3584 | 'If a variable is referenced in an enclosing scope, it is ' |
| 3585 | 'illegal to\n' |
| 3586 | 'delete the name. An error will be reported at compile ' |
| 3587 | 'time.\n' |
| 3588 | '\n' |
| 3589 | 'If the wild card form of import --- "import *" --- is ' |
| 3590 | 'used in a\n' |
| 3591 | 'function and the function contains or is a nested block ' |
| 3592 | 'with free\n' |
| 3593 | 'variables, the compiler will raise a "SyntaxError".\n' |
| 3594 | '\n' |
| 3595 | 'If "exec" is used in a function and the function ' |
| 3596 | 'contains or is a\n' |
| 3597 | 'nested block with free variables, the compiler will ' |
| 3598 | 'raise a\n' |
| 3599 | '"SyntaxError" unless the exec explicitly specifies the ' |
| 3600 | 'local namespace\n' |
| 3601 | 'for the "exec". (In other words, "exec obj" would be ' |
| 3602 | 'illegal, but\n' |
| 3603 | '"exec obj in ns" would be legal.)\n' |
| 3604 | '\n' |
| 3605 | 'The "eval()", "execfile()", and "input()" functions and ' |
| 3606 | 'the "exec"\n' |
| 3607 | 'statement do not have access to the full environment for ' |
| 3608 | 'resolving\n' |
| 3609 | 'names. Names may be resolved in the local and global ' |
| 3610 | 'namespaces of\n' |
| 3611 | 'the caller. Free variables are not resolved in the ' |
| 3612 | 'nearest enclosing\n' |
| 3613 | 'namespace, but in the global namespace. [1] The "exec" ' |
| 3614 | 'statement and\n' |
| 3615 | 'the "eval()" and "execfile()" functions have optional ' |
| 3616 | 'arguments to\n' |
| 3617 | 'override the global and local namespace. If only one ' |
| 3618 | 'namespace is\n' |
| 3619 | 'specified, it is used for both.\n', |
| 3620 | 'else': '\n' |
| 3621 | 'The "if" statement\n' |
| 3622 | '******************\n' |
| 3623 | '\n' |
| 3624 | 'The "if" statement is used for conditional execution:\n' |
| 3625 | '\n' |
| 3626 | ' if_stmt ::= "if" expression ":" suite\n' |
| 3627 | ' ( "elif" expression ":" suite )*\n' |
| 3628 | ' ["else" ":" suite]\n' |
| 3629 | '\n' |
| 3630 | 'It selects exactly one of the suites by evaluating the expressions ' |
| 3631 | 'one\n' |
| 3632 | 'by one until one is found to be true (see section Boolean ' |
| 3633 | 'operations\n' |
| 3634 | 'for the definition of true and false); then that suite is executed\n' |
| 3635 | '(and no other part of the "if" statement is executed or evaluated).\n' |
| 3636 | 'If all expressions are false, the suite of the "else" clause, if\n' |
| 3637 | 'present, is executed.\n', |
| 3638 | 'exceptions': '\n' |
| 3639 | 'Exceptions\n' |
| 3640 | '**********\n' |
| 3641 | '\n' |
| 3642 | 'Exceptions are a means of breaking out of the normal flow of ' |
| 3643 | 'control\n' |
| 3644 | 'of a code block in order to handle errors or other ' |
| 3645 | 'exceptional\n' |
| 3646 | 'conditions. An exception is *raised* at the point where the ' |
| 3647 | 'error is\n' |
| 3648 | 'detected; it may be *handled* by the surrounding code block or ' |
| 3649 | 'by any\n' |
| 3650 | 'code block that directly or indirectly invoked the code block ' |
| 3651 | 'where\n' |
| 3652 | 'the error occurred.\n' |
| 3653 | '\n' |
| 3654 | 'The Python interpreter raises an exception when it detects a ' |
| 3655 | 'run-time\n' |
| 3656 | 'error (such as division by zero). A Python program can also\n' |
| 3657 | 'explicitly raise an exception with the "raise" statement. ' |
| 3658 | 'Exception\n' |
| 3659 | 'handlers are specified with the "try" ... "except" statement. ' |
| 3660 | 'The\n' |
| 3661 | '"finally" clause of such a statement can be used to specify ' |
| 3662 | 'cleanup\n' |
| 3663 | 'code which does not handle the exception, but is executed ' |
| 3664 | 'whether an\n' |
| 3665 | 'exception occurred or not in the preceding code.\n' |
| 3666 | '\n' |
| 3667 | 'Python uses the "termination" model of error handling: an ' |
| 3668 | 'exception\n' |
| 3669 | 'handler can find out what happened and continue execution at ' |
| 3670 | 'an outer\n' |
| 3671 | 'level, but it cannot repair the cause of the error and retry ' |
| 3672 | 'the\n' |
| 3673 | 'failing operation (except by re-entering the offending piece ' |
| 3674 | 'of code\n' |
| 3675 | 'from the top).\n' |
| 3676 | '\n' |
| 3677 | 'When an exception is not handled at all, the interpreter ' |
| 3678 | 'terminates\n' |
| 3679 | 'execution of the program, or returns to its interactive main ' |
| 3680 | 'loop. In\n' |
| 3681 | 'either case, it prints a stack backtrace, except when the ' |
| 3682 | 'exception is\n' |
| 3683 | '"SystemExit".\n' |
| 3684 | '\n' |
| 3685 | 'Exceptions are identified by class instances. The "except" ' |
| 3686 | 'clause is\n' |
| 3687 | 'selected depending on the class of the instance: it must ' |
| 3688 | 'reference the\n' |
| 3689 | 'class of the instance or a base class thereof. The instance ' |
| 3690 | 'can be\n' |
| 3691 | 'received by the handler and can carry additional information ' |
| 3692 | 'about the\n' |
| 3693 | 'exceptional condition.\n' |
| 3694 | '\n' |
| 3695 | 'Exceptions can also be identified by strings, in which case ' |
| 3696 | 'the\n' |
| 3697 | '"except" clause is selected by object identity. An arbitrary ' |
| 3698 | 'value\n' |
| 3699 | 'can be raised along with the identifying string which can be ' |
| 3700 | 'passed to\n' |
| 3701 | 'the handler.\n' |
| 3702 | '\n' |
| 3703 | 'Note: Messages to exceptions are not part of the Python API. ' |
| 3704 | 'Their\n' |
| 3705 | ' contents may change from one version of Python to the next ' |
| 3706 | 'without\n' |
| 3707 | ' warning and should not be relied on by code which will run ' |
| 3708 | 'under\n' |
| 3709 | ' multiple versions of the interpreter.\n' |
| 3710 | '\n' |
| 3711 | 'See also the description of the "try" statement in section The ' |
| 3712 | 'try\n' |
| 3713 | 'statement and "raise" statement in section The raise ' |
| 3714 | 'statement.\n' |
| 3715 | '\n' |
| 3716 | '-[ Footnotes ]-\n' |
| 3717 | '\n' |
| 3718 | '[1] This limitation occurs because the code that is executed ' |
| 3719 | 'by\n' |
| 3720 | ' these operations is not available at the time the module ' |
| 3721 | 'is\n' |
| 3722 | ' compiled.\n', |
| 3723 | 'exec': '\n' |
| 3724 | 'The "exec" statement\n' |
| 3725 | '********************\n' |
| 3726 | '\n' |
| 3727 | ' exec_stmt ::= "exec" or_expr ["in" expression ["," expression]]\n' |
| 3728 | '\n' |
| 3729 | 'This statement supports dynamic execution of Python code. The ' |
| 3730 | 'first\n' |
| 3731 | 'expression should evaluate to either a Unicode string, a *Latin-1*\n' |
| 3732 | 'encoded string, an open file object, a code object, or a tuple. If ' |
| 3733 | 'it\n' |
| 3734 | 'is a string, the string is parsed as a suite of Python statements\n' |
| 3735 | 'which is then executed (unless a syntax error occurs). [1] If it is ' |
| 3736 | 'an\n' |
| 3737 | 'open file, the file is parsed until EOF and executed. If it is a ' |
| 3738 | 'code\n' |
| 3739 | 'object, it is simply executed. For the interpretation of a tuple, ' |
| 3740 | 'see\n' |
| 3741 | "below. In all cases, the code that's executed is expected to be " |
| 3742 | 'valid\n' |
| 3743 | 'as file input (see section File input). Be aware that the "return"\n' |
| 3744 | 'and "yield" statements may not be used outside of function ' |
| 3745 | 'definitions\n' |
| 3746 | 'even within the context of code passed to the "exec" statement.\n' |
| 3747 | '\n' |
| 3748 | 'In all cases, if the optional parts are omitted, the code is ' |
| 3749 | 'executed\n' |
| 3750 | 'in the current scope. If only the first expression after "in" is\n' |
| 3751 | 'specified, it should be a dictionary, which will be used for both ' |
| 3752 | 'the\n' |
| 3753 | 'global and the local variables. If two expressions are given, they\n' |
| 3754 | 'are used for the global and local variables, respectively. If\n' |
| 3755 | 'provided, *locals* can be any mapping object. Remember that at ' |
| 3756 | 'module\n' |
| 3757 | 'level, globals and locals are the same dictionary. If two separate\n' |
| 3758 | 'objects are given as *globals* and *locals*, the code will be ' |
| 3759 | 'executed\n' |
| 3760 | 'as if it were embedded in a class definition.\n' |
| 3761 | '\n' |
| 3762 | 'The first expression may also be a tuple of length 2 or 3. In this\n' |
| 3763 | 'case, the optional parts must be omitted. The form "exec(expr,\n' |
| 3764 | 'globals)" is equivalent to "exec expr in globals", while the form\n' |
| 3765 | '"exec(expr, globals, locals)" is equivalent to "exec expr in ' |
| 3766 | 'globals,\n' |
| 3767 | 'locals". The tuple form of "exec" provides compatibility with ' |
| 3768 | 'Python\n' |
| 3769 | '3, where "exec" is a function rather than a statement.\n' |
| 3770 | '\n' |
| 3771 | 'Changed in version 2.4: Formerly, *locals* was required to be a\n' |
| 3772 | 'dictionary.\n' |
| 3773 | '\n' |
| 3774 | 'As a side effect, an implementation may insert additional keys into\n' |
| 3775 | 'the dictionaries given besides those corresponding to variable ' |
| 3776 | 'names\n' |
| 3777 | 'set by the executed code. For example, the current implementation ' |
| 3778 | 'may\n' |
| 3779 | 'add a reference to the dictionary of the built-in module ' |
| 3780 | '"__builtin__"\n' |
| 3781 | 'under the key "__builtins__" (!).\n' |
| 3782 | '\n' |
| 3783 | "**Programmer's hints:** dynamic evaluation of expressions is " |
| 3784 | 'supported\n' |
| 3785 | 'by the built-in function "eval()". The built-in functions ' |
| 3786 | '"globals()"\n' |
| 3787 | 'and "locals()" return the current global and local dictionary,\n' |
| 3788 | 'respectively, which may be useful to pass around for use by "exec".\n' |
| 3789 | '\n' |
| 3790 | '-[ Footnotes ]-\n' |
| 3791 | '\n' |
| 3792 | '[1] Note that the parser only accepts the Unix-style end of line\n' |
| 3793 | ' convention. If you are reading the code from a file, make sure ' |
| 3794 | 'to\n' |
| 3795 | ' use *universal newlines* mode to convert Windows or Mac-style\n' |
| 3796 | ' newlines.\n', |
| 3797 | 'execmodel': '\n' |
| 3798 | 'Execution model\n' |
| 3799 | '***************\n' |
| 3800 | '\n' |
| 3801 | '\n' |
| 3802 | 'Naming and binding\n' |
| 3803 | '==================\n' |
| 3804 | '\n' |
| 3805 | '*Names* refer to objects. Names are introduced by name ' |
| 3806 | 'binding\n' |
| 3807 | 'operations. Each occurrence of a name in the program text ' |
| 3808 | 'refers to\n' |
| 3809 | 'the *binding* of that name established in the innermost ' |
| 3810 | 'function block\n' |
| 3811 | 'containing the use.\n' |
| 3812 | '\n' |
| 3813 | 'A *block* is a piece of Python program text that is executed as ' |
| 3814 | 'a\n' |
| 3815 | 'unit. The following are blocks: a module, a function body, and ' |
| 3816 | 'a class\n' |
| 3817 | 'definition. Each command typed interactively is a block. A ' |
| 3818 | 'script\n' |
| 3819 | 'file (a file given as standard input to the interpreter or ' |
| 3820 | 'specified\n' |
| 3821 | 'on the interpreter command line the first argument) is a code ' |
| 3822 | 'block.\n' |
| 3823 | 'A script command (a command specified on the interpreter ' |
| 3824 | 'command line\n' |
| 3825 | "with the '**-c**' option) is a code block. The file read by " |
| 3826 | 'the\n' |
| 3827 | 'built-in function "execfile()" is a code block. The string ' |
| 3828 | 'argument\n' |
| 3829 | 'passed to the built-in function "eval()" and to the "exec" ' |
| 3830 | 'statement\n' |
| 3831 | 'is a code block. The expression read and evaluated by the ' |
| 3832 | 'built-in\n' |
| 3833 | 'function "input()" is a code block.\n' |
| 3834 | '\n' |
| 3835 | 'A code block is executed in an *execution frame*. A frame ' |
| 3836 | 'contains\n' |
| 3837 | 'some administrative information (used for debugging) and ' |
| 3838 | 'determines\n' |
| 3839 | "where and how execution continues after the code block's " |
| 3840 | 'execution has\n' |
| 3841 | 'completed.\n' |
| 3842 | '\n' |
| 3843 | 'A *scope* defines the visibility of a name within a block. If ' |
| 3844 | 'a local\n' |
| 3845 | 'variable is defined in a block, its scope includes that block. ' |
| 3846 | 'If the\n' |
| 3847 | 'definition occurs in a function block, the scope extends to any ' |
| 3848 | 'blocks\n' |
| 3849 | 'contained within the defining one, unless a contained block ' |
| 3850 | 'introduces\n' |
| 3851 | 'a different binding for the name. The scope of names defined ' |
| 3852 | 'in a\n' |
| 3853 | 'class block is limited to the class block; it does not extend ' |
| 3854 | 'to the\n' |
| 3855 | 'code blocks of methods -- this includes generator expressions ' |
| 3856 | 'since\n' |
| 3857 | 'they are implemented using a function scope. This means that ' |
| 3858 | 'the\n' |
| 3859 | 'following will fail:\n' |
| 3860 | '\n' |
| 3861 | ' class A:\n' |
| 3862 | ' a = 42\n' |
| 3863 | ' b = list(a + i for i in range(10))\n' |
| 3864 | '\n' |
| 3865 | 'When a name is used in a code block, it is resolved using the ' |
| 3866 | 'nearest\n' |
| 3867 | 'enclosing scope. The set of all such scopes visible to a code ' |
| 3868 | 'block\n' |
| 3869 | "is called the block's *environment*.\n" |
| 3870 | '\n' |
| 3871 | 'If a name is bound in a block, it is a local variable of that ' |
| 3872 | 'block.\n' |
| 3873 | 'If a name is bound at the module level, it is a global ' |
| 3874 | 'variable. (The\n' |
| 3875 | 'variables of the module code block are local and global.) If ' |
| 3876 | 'a\n' |
| 3877 | 'variable is used in a code block but not defined there, it is a ' |
| 3878 | '*free\n' |
| 3879 | 'variable*.\n' |
| 3880 | '\n' |
| 3881 | 'When a name is not found at all, a "NameError" exception is ' |
| 3882 | 'raised.\n' |
| 3883 | 'If the name refers to a local variable that has not been bound, ' |
| 3884 | 'a\n' |
| 3885 | '"UnboundLocalError" exception is raised. "UnboundLocalError" ' |
| 3886 | 'is a\n' |
| 3887 | 'subclass of "NameError".\n' |
| 3888 | '\n' |
| 3889 | 'The following constructs bind names: formal parameters to ' |
| 3890 | 'functions,\n' |
| 3891 | '"import" statements, class and function definitions (these bind ' |
| 3892 | 'the\n' |
| 3893 | 'class or function name in the defining block), and targets that ' |
| 3894 | 'are\n' |
| 3895 | 'identifiers if occurring in an assignment, "for" loop header, ' |
| 3896 | 'in the\n' |
| 3897 | 'second position of an "except" clause header or after "as" in a ' |
| 3898 | '"with"\n' |
| 3899 | 'statement. The "import" statement of the form "from ... import ' |
| 3900 | '*"\n' |
| 3901 | 'binds all names defined in the imported module, except those ' |
| 3902 | 'beginning\n' |
| 3903 | 'with an underscore. This form may only be used at the module ' |
| 3904 | 'level.\n' |
| 3905 | '\n' |
| 3906 | 'A target occurring in a "del" statement is also considered ' |
| 3907 | 'bound for\n' |
| 3908 | 'this purpose (though the actual semantics are to unbind the ' |
| 3909 | 'name). It\n' |
| 3910 | 'is illegal to unbind a name that is referenced by an enclosing ' |
| 3911 | 'scope;\n' |
| 3912 | 'the compiler will report a "SyntaxError".\n' |
| 3913 | '\n' |
| 3914 | 'Each assignment or import statement occurs within a block ' |
| 3915 | 'defined by a\n' |
| 3916 | 'class or function definition or at the module level (the ' |
| 3917 | 'top-level\n' |
| 3918 | 'code block).\n' |
| 3919 | '\n' |
| 3920 | 'If a name binding operation occurs anywhere within a code ' |
| 3921 | 'block, all\n' |
| 3922 | 'uses of the name within the block are treated as references to ' |
| 3923 | 'the\n' |
| 3924 | 'current block. This can lead to errors when a name is used ' |
| 3925 | 'within a\n' |
| 3926 | 'block before it is bound. This rule is subtle. Python lacks\n' |
| 3927 | 'declarations and allows name binding operations to occur ' |
| 3928 | 'anywhere\n' |
| 3929 | 'within a code block. The local variables of a code block can ' |
| 3930 | 'be\n' |
| 3931 | 'determined by scanning the entire text of the block for name ' |
| 3932 | 'binding\n' |
| 3933 | 'operations.\n' |
| 3934 | '\n' |
| 3935 | 'If the global statement occurs within a block, all uses of the ' |
| 3936 | 'name\n' |
| 3937 | 'specified in the statement refer to the binding of that name in ' |
| 3938 | 'the\n' |
| 3939 | 'top-level namespace. Names are resolved in the top-level ' |
| 3940 | 'namespace by\n' |
| 3941 | 'searching the global namespace, i.e. the namespace of the ' |
| 3942 | 'module\n' |
| 3943 | 'containing the code block, and the builtins namespace, the ' |
| 3944 | 'namespace\n' |
| 3945 | 'of the module "__builtin__". The global namespace is searched ' |
| 3946 | 'first.\n' |
| 3947 | 'If the name is not found there, the builtins namespace is ' |
| 3948 | 'searched.\n' |
| 3949 | 'The global statement must precede all uses of the name.\n' |
| 3950 | '\n' |
| 3951 | 'The builtins namespace associated with the execution of a code ' |
| 3952 | 'block\n' |
| 3953 | 'is actually found by looking up the name "__builtins__" in its ' |
| 3954 | 'global\n' |
| 3955 | 'namespace; this should be a dictionary or a module (in the ' |
| 3956 | 'latter case\n' |
| 3957 | "the module's dictionary is used). By default, when in the " |
| 3958 | '"__main__"\n' |
| 3959 | 'module, "__builtins__" is the built-in module "__builtin__" ' |
| 3960 | '(note: no\n' |
| 3961 | '\'s\'); when in any other module, "__builtins__" is an alias ' |
| 3962 | 'for the\n' |
| 3963 | 'dictionary of the "__builtin__" module itself. "__builtins__" ' |
| 3964 | 'can be\n' |
| 3965 | 'set to a user-created dictionary to create a weak form of ' |
| 3966 | 'restricted\n' |
| 3967 | 'execution.\n' |
| 3968 | '\n' |
| 3969 | '**CPython implementation detail:** Users should not touch\n' |
| 3970 | '"__builtins__"; it is strictly an implementation detail. ' |
| 3971 | 'Users\n' |
| 3972 | 'wanting to override values in the builtins namespace should ' |
| 3973 | '"import"\n' |
| 3974 | 'the "__builtin__" (no \'s\') module and modify its attributes\n' |
| 3975 | 'appropriately.\n' |
| 3976 | '\n' |
| 3977 | 'The namespace for a module is automatically created the first ' |
| 3978 | 'time a\n' |
| 3979 | 'module is imported. The main module for a script is always ' |
| 3980 | 'called\n' |
| 3981 | '"__main__".\n' |
| 3982 | '\n' |
| 3983 | 'The "global" statement has the same scope as a name binding ' |
| 3984 | 'operation\n' |
| 3985 | 'in the same block. If the nearest enclosing scope for a free ' |
| 3986 | 'variable\n' |
| 3987 | 'contains a global statement, the free variable is treated as a ' |
| 3988 | 'global.\n' |
| 3989 | '\n' |
| 3990 | 'A class definition is an executable statement that may use and ' |
| 3991 | 'define\n' |
| 3992 | 'names. These references follow the normal rules for name ' |
| 3993 | 'resolution.\n' |
| 3994 | 'The namespace of the class definition becomes the attribute ' |
| 3995 | 'dictionary\n' |
| 3996 | 'of the class. Names defined at the class scope are not visible ' |
| 3997 | 'in\n' |
| 3998 | 'methods.\n' |
| 3999 | '\n' |
| 4000 | '\n' |
| 4001 | 'Interaction with dynamic features\n' |
| 4002 | '---------------------------------\n' |
| 4003 | '\n' |
| 4004 | 'There are several cases where Python statements are illegal ' |
| 4005 | 'when used\n' |
| 4006 | 'in conjunction with nested scopes that contain free variables.\n' |
| 4007 | '\n' |
| 4008 | 'If a variable is referenced in an enclosing scope, it is ' |
| 4009 | 'illegal to\n' |
| 4010 | 'delete the name. An error will be reported at compile time.\n' |
| 4011 | '\n' |
| 4012 | 'If the wild card form of import --- "import *" --- is used in ' |
| 4013 | 'a\n' |
| 4014 | 'function and the function contains or is a nested block with ' |
| 4015 | 'free\n' |
| 4016 | 'variables, the compiler will raise a "SyntaxError".\n' |
| 4017 | '\n' |
| 4018 | 'If "exec" is used in a function and the function contains or is ' |
| 4019 | 'a\n' |
| 4020 | 'nested block with free variables, the compiler will raise a\n' |
| 4021 | '"SyntaxError" unless the exec explicitly specifies the local ' |
| 4022 | 'namespace\n' |
| 4023 | 'for the "exec". (In other words, "exec obj" would be illegal, ' |
| 4024 | 'but\n' |
| 4025 | '"exec obj in ns" would be legal.)\n' |
| 4026 | '\n' |
| 4027 | 'The "eval()", "execfile()", and "input()" functions and the ' |
| 4028 | '"exec"\n' |
| 4029 | 'statement do not have access to the full environment for ' |
| 4030 | 'resolving\n' |
| 4031 | 'names. Names may be resolved in the local and global ' |
| 4032 | 'namespaces of\n' |
| 4033 | 'the caller. Free variables are not resolved in the nearest ' |
| 4034 | 'enclosing\n' |
| 4035 | 'namespace, but in the global namespace. [1] The "exec" ' |
| 4036 | 'statement and\n' |
| 4037 | 'the "eval()" and "execfile()" functions have optional arguments ' |
| 4038 | 'to\n' |
| 4039 | 'override the global and local namespace. If only one namespace ' |
| 4040 | 'is\n' |
| 4041 | 'specified, it is used for both.\n' |
| 4042 | '\n' |
| 4043 | '\n' |
| 4044 | 'Exceptions\n' |
| 4045 | '==========\n' |
| 4046 | '\n' |
| 4047 | 'Exceptions are a means of breaking out of the normal flow of ' |
| 4048 | 'control\n' |
| 4049 | 'of a code block in order to handle errors or other exceptional\n' |
| 4050 | 'conditions. An exception is *raised* at the point where the ' |
| 4051 | 'error is\n' |
| 4052 | 'detected; it may be *handled* by the surrounding code block or ' |
| 4053 | 'by any\n' |
| 4054 | 'code block that directly or indirectly invoked the code block ' |
| 4055 | 'where\n' |
| 4056 | 'the error occurred.\n' |
| 4057 | '\n' |
| 4058 | 'The Python interpreter raises an exception when it detects a ' |
| 4059 | 'run-time\n' |
| 4060 | 'error (such as division by zero). A Python program can also\n' |
| 4061 | 'explicitly raise an exception with the "raise" statement. ' |
| 4062 | 'Exception\n' |
| 4063 | 'handlers are specified with the "try" ... "except" statement. ' |
| 4064 | 'The\n' |
| 4065 | '"finally" clause of such a statement can be used to specify ' |
| 4066 | 'cleanup\n' |
| 4067 | 'code which does not handle the exception, but is executed ' |
| 4068 | 'whether an\n' |
| 4069 | 'exception occurred or not in the preceding code.\n' |
| 4070 | '\n' |
| 4071 | 'Python uses the "termination" model of error handling: an ' |
| 4072 | 'exception\n' |
| 4073 | 'handler can find out what happened and continue execution at an ' |
| 4074 | 'outer\n' |
| 4075 | 'level, but it cannot repair the cause of the error and retry ' |
| 4076 | 'the\n' |
| 4077 | 'failing operation (except by re-entering the offending piece of ' |
| 4078 | 'code\n' |
| 4079 | 'from the top).\n' |
| 4080 | '\n' |
| 4081 | 'When an exception is not handled at all, the interpreter ' |
| 4082 | 'terminates\n' |
| 4083 | 'execution of the program, or returns to its interactive main ' |
| 4084 | 'loop. In\n' |
| 4085 | 'either case, it prints a stack backtrace, except when the ' |
| 4086 | 'exception is\n' |
| 4087 | '"SystemExit".\n' |
| 4088 | '\n' |
| 4089 | 'Exceptions are identified by class instances. The "except" ' |
| 4090 | 'clause is\n' |
| 4091 | 'selected depending on the class of the instance: it must ' |
| 4092 | 'reference the\n' |
| 4093 | 'class of the instance or a base class thereof. The instance ' |
| 4094 | 'can be\n' |
| 4095 | 'received by the handler and can carry additional information ' |
| 4096 | 'about the\n' |
| 4097 | 'exceptional condition.\n' |
| 4098 | '\n' |
| 4099 | 'Exceptions can also be identified by strings, in which case ' |
| 4100 | 'the\n' |
| 4101 | '"except" clause is selected by object identity. An arbitrary ' |
| 4102 | 'value\n' |
| 4103 | 'can be raised along with the identifying string which can be ' |
| 4104 | 'passed to\n' |
| 4105 | 'the handler.\n' |
| 4106 | '\n' |
| 4107 | 'Note: Messages to exceptions are not part of the Python API. ' |
| 4108 | 'Their\n' |
| 4109 | ' contents may change from one version of Python to the next ' |
| 4110 | 'without\n' |
| 4111 | ' warning and should not be relied on by code which will run ' |
| 4112 | 'under\n' |
| 4113 | ' multiple versions of the interpreter.\n' |
| 4114 | '\n' |
| 4115 | 'See also the description of the "try" statement in section The ' |
| 4116 | 'try\n' |
| 4117 | 'statement and "raise" statement in section The raise ' |
| 4118 | 'statement.\n' |
| 4119 | '\n' |
| 4120 | '-[ Footnotes ]-\n' |
| 4121 | '\n' |
| 4122 | '[1] This limitation occurs because the code that is executed ' |
| 4123 | 'by\n' |
| 4124 | ' these operations is not available at the time the module ' |
| 4125 | 'is\n' |
| 4126 | ' compiled.\n', |
| 4127 | 'exprlists': '\n' |
| 4128 | 'Expression lists\n' |
| 4129 | '****************\n' |
| 4130 | '\n' |
| 4131 | ' expression_list ::= expression ( "," expression )* [","]\n' |
| 4132 | '\n' |
| 4133 | 'An expression list containing at least one comma yields a ' |
| 4134 | 'tuple. The\n' |
| 4135 | 'length of the tuple is the number of expressions in the list. ' |
| 4136 | 'The\n' |
| 4137 | 'expressions are evaluated from left to right.\n' |
| 4138 | '\n' |
| 4139 | 'The trailing comma is required only to create a single tuple ' |
| 4140 | '(a.k.a. a\n' |
| 4141 | '*singleton*); it is optional in all other cases. A single ' |
| 4142 | 'expression\n' |
| 4143 | "without a trailing comma doesn't create a tuple, but rather " |
| 4144 | 'yields the\n' |
| 4145 | 'value of that expression. (To create an empty tuple, use an ' |
| 4146 | 'empty pair\n' |
| 4147 | 'of parentheses: "()".)\n', |
| 4148 | 'floating': '\n' |
| 4149 | 'Floating point literals\n' |
| 4150 | '***********************\n' |
| 4151 | '\n' |
| 4152 | 'Floating point literals are described by the following lexical\n' |
| 4153 | 'definitions:\n' |
| 4154 | '\n' |
| 4155 | ' floatnumber ::= pointfloat | exponentfloat\n' |
| 4156 | ' pointfloat ::= [intpart] fraction | intpart "."\n' |
| 4157 | ' exponentfloat ::= (intpart | pointfloat) exponent\n' |
| 4158 | ' intpart ::= digit+\n' |
| 4159 | ' fraction ::= "." digit+\n' |
| 4160 | ' exponent ::= ("e" | "E") ["+" | "-"] digit+\n' |
| 4161 | '\n' |
| 4162 | 'Note that the integer and exponent parts of floating point ' |
| 4163 | 'numbers can\n' |
| 4164 | 'look like octal integers, but are interpreted using radix 10. ' |
| 4165 | 'For\n' |
| 4166 | 'example, "077e010" is legal, and denotes the same number as ' |
| 4167 | '"77e10".\n' |
| 4168 | 'The allowed range of floating point literals is implementation-\n' |
| 4169 | 'dependent. Some examples of floating point literals:\n' |
| 4170 | '\n' |
| 4171 | ' 3.14 10. .001 1e100 3.14e-10 0e0\n' |
| 4172 | '\n' |
| 4173 | 'Note that numeric literals do not include a sign; a phrase like ' |
| 4174 | '"-1"\n' |
| 4175 | 'is actually an expression composed of the unary operator "-" and ' |
| 4176 | 'the\n' |
| 4177 | 'literal "1".\n', |
| 4178 | 'for': '\n' |
| 4179 | 'The "for" statement\n' |
| 4180 | '*******************\n' |
| 4181 | '\n' |
| 4182 | 'The "for" statement is used to iterate over the elements of a ' |
| 4183 | 'sequence\n' |
| 4184 | '(such as a string, tuple or list) or other iterable object:\n' |
| 4185 | '\n' |
| 4186 | ' for_stmt ::= "for" target_list "in" expression_list ":" suite\n' |
| 4187 | ' ["else" ":" suite]\n' |
| 4188 | '\n' |
| 4189 | 'The expression list is evaluated once; it should yield an iterable\n' |
| 4190 | 'object. An iterator is created for the result of the\n' |
| 4191 | '"expression_list". The suite is then executed once for each item\n' |
| 4192 | 'provided by the iterator, in the order of ascending indices. Each\n' |
| 4193 | 'item in turn is assigned to the target list using the standard rules\n' |
| 4194 | 'for assignments, and then the suite is executed. When the items are\n' |
| 4195 | 'exhausted (which is immediately when the sequence is empty), the ' |
| 4196 | 'suite\n' |
| 4197 | 'in the "else" clause, if present, is executed, and the loop\n' |
| 4198 | 'terminates.\n' |
| 4199 | '\n' |
| 4200 | 'A "break" statement executed in the first suite terminates the loop\n' |
| 4201 | 'without executing the "else" clause\'s suite. A "continue" ' |
| 4202 | 'statement\n' |
| 4203 | 'executed in the first suite skips the rest of the suite and ' |
| 4204 | 'continues\n' |
| 4205 | 'with the next item, or with the "else" clause if there was no next\n' |
| 4206 | 'item.\n' |
| 4207 | '\n' |
| 4208 | 'The suite may assign to the variable(s) in the target list; this ' |
| 4209 | 'does\n' |
| 4210 | 'not affect the next item assigned to it.\n' |
| 4211 | '\n' |
| 4212 | 'The target list is not deleted when the loop is finished, but if the\n' |
| 4213 | 'sequence is empty, it will not have been assigned to at all by the\n' |
| 4214 | 'loop. Hint: the built-in function "range()" returns a sequence of\n' |
| 4215 | 'integers suitable to emulate the effect of Pascal\'s "for i := a to ' |
| 4216 | 'b\n' |
| 4217 | 'do"; e.g., "range(3)" returns the list "[0, 1, 2]".\n' |
| 4218 | '\n' |
| 4219 | 'Note: There is a subtlety when the sequence is being modified by the\n' |
| 4220 | ' loop (this can only occur for mutable sequences, i.e. lists). An\n' |
| 4221 | ' internal counter is used to keep track of which item is used next,\n' |
| 4222 | ' and this is incremented on each iteration. When this counter has\n' |
| 4223 | ' reached the length of the sequence the loop terminates. This ' |
| 4224 | 'means\n' |
| 4225 | ' that if the suite deletes the current (or a previous) item from ' |
| 4226 | 'the\n' |
| 4227 | ' sequence, the next item will be skipped (since it gets the index ' |
| 4228 | 'of\n' |
| 4229 | ' the current item which has already been treated). Likewise, if ' |
| 4230 | 'the\n' |
| 4231 | ' suite inserts an item in the sequence before the current item, the\n' |
| 4232 | ' current item will be treated again the next time through the loop.\n' |
| 4233 | ' This can lead to nasty bugs that can be avoided by making a\n' |
| 4234 | ' temporary copy using a slice of the whole sequence, e.g.,\n' |
| 4235 | '\n' |
| 4236 | ' for x in a[:]:\n' |
| 4237 | ' if x < 0: a.remove(x)\n', |
| 4238 | 'formatstrings': '\n' |
| 4239 | 'Format String Syntax\n' |
| 4240 | '********************\n' |
| 4241 | '\n' |
| 4242 | 'The "str.format()" method and the "Formatter" class share ' |
| 4243 | 'the same\n' |
| 4244 | 'syntax for format strings (although in the case of ' |
| 4245 | '"Formatter",\n' |
| 4246 | 'subclasses can define their own format string syntax).\n' |
| 4247 | '\n' |
| 4248 | 'Format strings contain "replacement fields" surrounded by ' |
| 4249 | 'curly braces\n' |
| 4250 | '"{}". Anything that is not contained in braces is ' |
| 4251 | 'considered literal\n' |
| 4252 | 'text, which is copied unchanged to the output. If you need ' |
| 4253 | 'to include\n' |
| 4254 | 'a brace character in the literal text, it can be escaped by ' |
| 4255 | 'doubling:\n' |
| 4256 | '"{{" and "}}".\n' |
| 4257 | '\n' |
| 4258 | 'The grammar for a replacement field is as follows:\n' |
| 4259 | '\n' |
| 4260 | ' replacement_field ::= "{" [field_name] ["!" ' |
| 4261 | 'conversion] [":" format_spec] "}"\n' |
| 4262 | ' field_name ::= arg_name ("." attribute_name | ' |
| 4263 | '"[" element_index "]")*\n' |
| 4264 | ' arg_name ::= [identifier | integer]\n' |
| 4265 | ' attribute_name ::= identifier\n' |
| 4266 | ' element_index ::= integer | index_string\n' |
| 4267 | ' index_string ::= <any source character except ' |
| 4268 | '"]"> +\n' |
| 4269 | ' conversion ::= "r" | "s"\n' |
| 4270 | ' format_spec ::= <described in the next ' |
| 4271 | 'section>\n' |
| 4272 | '\n' |
| 4273 | 'In less formal terms, the replacement field can start with ' |
| 4274 | 'a\n' |
| 4275 | '*field_name* that specifies the object whose value is to be ' |
| 4276 | 'formatted\n' |
| 4277 | 'and inserted into the output instead of the replacement ' |
| 4278 | 'field. The\n' |
| 4279 | '*field_name* is optionally followed by a *conversion* ' |
| 4280 | 'field, which is\n' |
| 4281 | 'preceded by an exclamation point "\'!\'", and a ' |
| 4282 | '*format_spec*, which is\n' |
| 4283 | 'preceded by a colon "\':\'". These specify a non-default ' |
| 4284 | 'format for the\n' |
| 4285 | 'replacement value.\n' |
| 4286 | '\n' |
| 4287 | 'See also the Format Specification Mini-Language section.\n' |
| 4288 | '\n' |
| 4289 | 'The *field_name* itself begins with an *arg_name* that is ' |
| 4290 | 'either a\n' |
| 4291 | "number or a keyword. If it's a number, it refers to a " |
| 4292 | 'positional\n' |
| 4293 | "argument, and if it's a keyword, it refers to a named " |
| 4294 | 'keyword\n' |
| 4295 | 'argument. If the numerical arg_names in a format string ' |
| 4296 | 'are 0, 1, 2,\n' |
| 4297 | '... in sequence, they can all be omitted (not just some) ' |
| 4298 | 'and the\n' |
| 4299 | 'numbers 0, 1, 2, ... will be automatically inserted in that ' |
| 4300 | 'order.\n' |
| 4301 | 'Because *arg_name* is not quote-delimited, it is not ' |
| 4302 | 'possible to\n' |
| 4303 | 'specify arbitrary dictionary keys (e.g., the strings ' |
| 4304 | '"\'10\'" or\n' |
| 4305 | '"\':-]\'") within a format string. The *arg_name* can be ' |
| 4306 | 'followed by any\n' |
| 4307 | 'number of index or attribute expressions. An expression of ' |
| 4308 | 'the form\n' |
| 4309 | '"\'.name\'" selects the named attribute using "getattr()", ' |
| 4310 | 'while an\n' |
| 4311 | 'expression of the form "\'[index]\'" does an index lookup ' |
| 4312 | 'using\n' |
| 4313 | '"__getitem__()".\n' |
| 4314 | '\n' |
| 4315 | 'Changed in version 2.7: The positional argument specifiers ' |
| 4316 | 'can be\n' |
| 4317 | 'omitted, so "\'{} {}\'" is equivalent to "\'{0} {1}\'".\n' |
| 4318 | '\n' |
| 4319 | 'Some simple format string examples:\n' |
| 4320 | '\n' |
| 4321 | ' "First, thou shalt count to {0}" # References first ' |
| 4322 | 'positional argument\n' |
| 4323 | ' "Bring me a {}" # Implicitly ' |
| 4324 | 'references the first positional argument\n' |
| 4325 | ' "From {} to {}" # Same as "From {0} to ' |
| 4326 | '{1}"\n' |
| 4327 | ' "My quest is {name}" # References keyword ' |
| 4328 | "argument 'name'\n" |
| 4329 | ' "Weight in tons {0.weight}" # \'weight\' attribute ' |
| 4330 | 'of first positional arg\n' |
| 4331 | ' "Units destroyed: {players[0]}" # First element of ' |
| 4332 | "keyword argument 'players'.\n" |
| 4333 | '\n' |
| 4334 | 'The *conversion* field causes a type coercion before ' |
| 4335 | 'formatting.\n' |
| 4336 | 'Normally, the job of formatting a value is done by the ' |
| 4337 | '"__format__()"\n' |
| 4338 | 'method of the value itself. However, in some cases it is ' |
| 4339 | 'desirable to\n' |
| 4340 | 'force a type to be formatted as a string, overriding its ' |
| 4341 | 'own\n' |
| 4342 | 'definition of formatting. By converting the value to a ' |
| 4343 | 'string before\n' |
| 4344 | 'calling "__format__()", the normal formatting logic is ' |
| 4345 | 'bypassed.\n' |
| 4346 | '\n' |
| 4347 | 'Two conversion flags are currently supported: "\'!s\'" ' |
| 4348 | 'which calls\n' |
| 4349 | '"str()" on the value, and "\'!r\'" which calls "repr()".\n' |
| 4350 | '\n' |
| 4351 | 'Some examples:\n' |
| 4352 | '\n' |
| 4353 | ' "Harold\'s a clever {0!s}" # Calls str() on the ' |
| 4354 | 'argument first\n' |
| 4355 | ' "Bring out the holy {name!r}" # Calls repr() on the ' |
| 4356 | 'argument first\n' |
| 4357 | '\n' |
| 4358 | 'The *format_spec* field contains a specification of how the ' |
| 4359 | 'value\n' |
| 4360 | 'should be presented, including such details as field width, ' |
| 4361 | 'alignment,\n' |
| 4362 | 'padding, decimal precision and so on. Each value type can ' |
| 4363 | 'define its\n' |
| 4364 | 'own "formatting mini-language" or interpretation of the ' |
| 4365 | '*format_spec*.\n' |
| 4366 | '\n' |
| 4367 | 'Most built-in types support a common formatting ' |
| 4368 | 'mini-language, which\n' |
| 4369 | 'is described in the next section.\n' |
| 4370 | '\n' |
| 4371 | 'A *format_spec* field can also include nested replacement ' |
| 4372 | 'fields\n' |
| 4373 | 'within it. These nested replacement fields may contain a ' |
| 4374 | 'field name,\n' |
| 4375 | 'conversion flag and format specification, but deeper ' |
| 4376 | 'nesting is not\n' |
| 4377 | 'allowed. The replacement fields within the format_spec ' |
| 4378 | 'are\n' |
| 4379 | 'substituted before the *format_spec* string is interpreted. ' |
| 4380 | 'This\n' |
| 4381 | 'allows the formatting of a value to be dynamically ' |
| 4382 | 'specified.\n' |
| 4383 | '\n' |
| 4384 | 'See the Format examples section for some examples.\n' |
| 4385 | '\n' |
| 4386 | '\n' |
| 4387 | 'Format Specification Mini-Language\n' |
| 4388 | '==================================\n' |
| 4389 | '\n' |
| 4390 | '"Format specifications" are used within replacement fields ' |
| 4391 | 'contained\n' |
| 4392 | 'within a format string to define how individual values are ' |
| 4393 | 'presented\n' |
| 4394 | '(see Format String Syntax). They can also be passed ' |
| 4395 | 'directly to the\n' |
| 4396 | 'built-in "format()" function. Each formattable type may ' |
| 4397 | 'define how\n' |
| 4398 | 'the format specification is to be interpreted.\n' |
| 4399 | '\n' |
| 4400 | 'Most built-in types implement the following options for ' |
| 4401 | 'format\n' |
| 4402 | 'specifications, although some of the formatting options are ' |
| 4403 | 'only\n' |
| 4404 | 'supported by the numeric types.\n' |
| 4405 | '\n' |
| 4406 | 'A general convention is that an empty format string ("""") ' |
| 4407 | 'produces\n' |
| 4408 | 'the same result as if you had called "str()" on the value. ' |
| 4409 | 'A non-empty\n' |
| 4410 | 'format string typically modifies the result.\n' |
| 4411 | '\n' |
| 4412 | 'The general form of a *standard format specifier* is:\n' |
| 4413 | '\n' |
| 4414 | ' format_spec ::= ' |
| 4415 | '[[fill]align][sign][#][0][width][,][.precision][type]\n' |
| 4416 | ' fill ::= <any character>\n' |
| 4417 | ' align ::= "<" | ">" | "=" | "^"\n' |
| 4418 | ' sign ::= "+" | "-" | " "\n' |
| 4419 | ' width ::= integer\n' |
| 4420 | ' precision ::= integer\n' |
| 4421 | ' type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" ' |
| 4422 | '| "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"\n' |
| 4423 | '\n' |
| 4424 | 'If a valid *align* value is specified, it can be preceded ' |
| 4425 | 'by a *fill*\n' |
| 4426 | 'character that can be any character and defaults to a space ' |
| 4427 | 'if\n' |
| 4428 | 'omitted. It is not possible to use a literal curly brace ' |
| 4429 | '(""{"" or\n' |
| 4430 | '""}"") as the *fill* character when using the ' |
| 4431 | '"str.format()" method.\n' |
| 4432 | 'However, it is possible to insert a curly brace with a ' |
| 4433 | 'nested\n' |
| 4434 | "replacement field. This limitation doesn't affect the " |
| 4435 | '"format()"\n' |
| 4436 | 'function.\n' |
| 4437 | '\n' |
| 4438 | 'The meaning of the various alignment options is as ' |
| 4439 | 'follows:\n' |
| 4440 | '\n' |
| 4441 | ' ' |
| 4442 | '+-----------+------------------------------------------------------------+\n' |
| 4443 | ' | Option | ' |
| 4444 | 'Meaning ' |
| 4445 | '|\n' |
| 4446 | ' ' |
| 4447 | '+===========+============================================================+\n' |
| 4448 | ' | "\'<\'" | Forces the field to be left-aligned ' |
| 4449 | 'within the available |\n' |
| 4450 | ' | | space (this is the default for most ' |
| 4451 | 'objects). |\n' |
| 4452 | ' ' |
| 4453 | '+-----------+------------------------------------------------------------+\n' |
| 4454 | ' | "\'>\'" | Forces the field to be right-aligned ' |
| 4455 | 'within the available |\n' |
| 4456 | ' | | space (this is the default for ' |
| 4457 | 'numbers). |\n' |
| 4458 | ' ' |
| 4459 | '+-----------+------------------------------------------------------------+\n' |
| 4460 | ' | "\'=\'" | Forces the padding to be placed after ' |
| 4461 | 'the sign (if any) |\n' |
| 4462 | ' | | but before the digits. This is used for ' |
| 4463 | 'printing fields |\n' |
| 4464 | " | | in the form '+000000120'. This alignment " |
| 4465 | 'option is only |\n' |
| 4466 | ' | | valid for numeric types. It becomes the ' |
| 4467 | "default when '0' |\n" |
| 4468 | ' | | immediately precedes the field ' |
| 4469 | 'width. |\n' |
| 4470 | ' ' |
| 4471 | '+-----------+------------------------------------------------------------+\n' |
| 4472 | ' | "\'^\'" | Forces the field to be centered within ' |
| 4473 | 'the available |\n' |
| 4474 | ' | | ' |
| 4475 | 'space. ' |
| 4476 | '|\n' |
| 4477 | ' ' |
| 4478 | '+-----------+------------------------------------------------------------+\n' |
| 4479 | '\n' |
| 4480 | 'Note that unless a minimum field width is defined, the ' |
| 4481 | 'field width\n' |
| 4482 | 'will always be the same size as the data to fill it, so ' |
| 4483 | 'that the\n' |
| 4484 | 'alignment option has no meaning in this case.\n' |
| 4485 | '\n' |
| 4486 | 'The *sign* option is only valid for number types, and can ' |
| 4487 | 'be one of\n' |
| 4488 | 'the following:\n' |
| 4489 | '\n' |
| 4490 | ' ' |
| 4491 | '+-----------+------------------------------------------------------------+\n' |
| 4492 | ' | Option | ' |
| 4493 | 'Meaning ' |
| 4494 | '|\n' |
| 4495 | ' ' |
| 4496 | '+===========+============================================================+\n' |
| 4497 | ' | "\'+\'" | indicates that a sign should be used for ' |
| 4498 | 'both positive as |\n' |
| 4499 | ' | | well as negative ' |
| 4500 | 'numbers. |\n' |
| 4501 | ' ' |
| 4502 | '+-----------+------------------------------------------------------------+\n' |
| 4503 | ' | "\'-\'" | indicates that a sign should be used ' |
| 4504 | 'only for negative |\n' |
| 4505 | ' | | numbers (this is the default ' |
| 4506 | 'behavior). |\n' |
| 4507 | ' ' |
| 4508 | '+-----------+------------------------------------------------------------+\n' |
| 4509 | ' | space | indicates that a leading space should be ' |
| 4510 | 'used on positive |\n' |
| 4511 | ' | | numbers, and a minus sign on negative ' |
| 4512 | 'numbers. |\n' |
| 4513 | ' ' |
| 4514 | '+-----------+------------------------------------------------------------+\n' |
| 4515 | '\n' |
| 4516 | 'The "\'#\'" option is only valid for integers, and only for ' |
| 4517 | 'binary,\n' |
| 4518 | 'octal, or hexadecimal output. If present, it specifies ' |
| 4519 | 'that the\n' |
| 4520 | 'output will be prefixed by "\'0b\'", "\'0o\'", or "\'0x\'", ' |
| 4521 | 'respectively.\n' |
| 4522 | '\n' |
| 4523 | 'The "\',\'" option signals the use of a comma for a ' |
| 4524 | 'thousands separator.\n' |
| 4525 | 'For a locale aware separator, use the "\'n\'" integer ' |
| 4526 | 'presentation type\n' |
| 4527 | 'instead.\n' |
| 4528 | '\n' |
| 4529 | 'Changed in version 2.7: Added the "\',\'" option (see also ' |
| 4530 | '**PEP 378**).\n' |
| 4531 | '\n' |
| 4532 | '*width* is a decimal integer defining the minimum field ' |
| 4533 | 'width. If not\n' |
| 4534 | 'specified, then the field width will be determined by the ' |
| 4535 | 'content.\n' |
| 4536 | '\n' |
| 4537 | 'When no explicit alignment is given, preceding the *width* ' |
| 4538 | 'field by a\n' |
| 4539 | 'zero ("\'0\'") character enables sign-aware zero-padding ' |
| 4540 | 'for numeric\n' |
| 4541 | 'types. This is equivalent to a *fill* character of "\'0\'" ' |
| 4542 | 'with an\n' |
| 4543 | '*alignment* type of "\'=\'".\n' |
| 4544 | '\n' |
| 4545 | 'The *precision* is a decimal number indicating how many ' |
| 4546 | 'digits should\n' |
| 4547 | 'be displayed after the decimal point for a floating point ' |
| 4548 | 'value\n' |
| 4549 | 'formatted with "\'f\'" and "\'F\'", or before and after the ' |
| 4550 | 'decimal point\n' |
| 4551 | 'for a floating point value formatted with "\'g\'" or ' |
| 4552 | '"\'G\'". For non-\n' |
| 4553 | 'number types the field indicates the maximum field size - ' |
| 4554 | 'in other\n' |
| 4555 | 'words, how many characters will be used from the field ' |
| 4556 | 'content. The\n' |
| 4557 | '*precision* is not allowed for integer values.\n' |
| 4558 | '\n' |
| 4559 | 'Finally, the *type* determines how the data should be ' |
| 4560 | 'presented.\n' |
| 4561 | '\n' |
| 4562 | 'The available string presentation types are:\n' |
| 4563 | '\n' |
| 4564 | ' ' |
| 4565 | '+-----------+------------------------------------------------------------+\n' |
| 4566 | ' | Type | ' |
| 4567 | 'Meaning ' |
| 4568 | '|\n' |
| 4569 | ' ' |
| 4570 | '+===========+============================================================+\n' |
| 4571 | ' | "\'s\'" | String format. This is the default type ' |
| 4572 | 'for strings and |\n' |
| 4573 | ' | | may be ' |
| 4574 | 'omitted. |\n' |
| 4575 | ' ' |
| 4576 | '+-----------+------------------------------------------------------------+\n' |
| 4577 | ' | None | The same as ' |
| 4578 | '"\'s\'". |\n' |
| 4579 | ' ' |
| 4580 | '+-----------+------------------------------------------------------------+\n' |
| 4581 | '\n' |
| 4582 | 'The available integer presentation types are:\n' |
| 4583 | '\n' |
| 4584 | ' ' |
| 4585 | '+-----------+------------------------------------------------------------+\n' |
| 4586 | ' | Type | ' |
| 4587 | 'Meaning ' |
| 4588 | '|\n' |
| 4589 | ' ' |
| 4590 | '+===========+============================================================+\n' |
| 4591 | ' | "\'b\'" | Binary format. Outputs the number in ' |
| 4592 | 'base 2. |\n' |
| 4593 | ' ' |
| 4594 | '+-----------+------------------------------------------------------------+\n' |
| 4595 | ' | "\'c\'" | Character. Converts the integer to the ' |
| 4596 | 'corresponding |\n' |
| 4597 | ' | | unicode character before ' |
| 4598 | 'printing. |\n' |
| 4599 | ' ' |
| 4600 | '+-----------+------------------------------------------------------------+\n' |
| 4601 | ' | "\'d\'" | Decimal Integer. Outputs the number in ' |
| 4602 | 'base 10. |\n' |
| 4603 | ' ' |
| 4604 | '+-----------+------------------------------------------------------------+\n' |
| 4605 | ' | "\'o\'" | Octal format. Outputs the number in base ' |
| 4606 | '8. |\n' |
| 4607 | ' ' |
| 4608 | '+-----------+------------------------------------------------------------+\n' |
| 4609 | ' | "\'x\'" | Hex format. Outputs the number in base ' |
| 4610 | '16, using lower- |\n' |
| 4611 | ' | | case letters for the digits above ' |
| 4612 | '9. |\n' |
| 4613 | ' ' |
| 4614 | '+-----------+------------------------------------------------------------+\n' |
| 4615 | ' | "\'X\'" | Hex format. Outputs the number in base ' |
| 4616 | '16, using upper- |\n' |
| 4617 | ' | | case letters for the digits above ' |
| 4618 | '9. |\n' |
| 4619 | ' ' |
| 4620 | '+-----------+------------------------------------------------------------+\n' |
| 4621 | ' | "\'n\'" | Number. This is the same as "\'d\'", ' |
| 4622 | 'except that it uses the |\n' |
| 4623 | ' | | current locale setting to insert the ' |
| 4624 | 'appropriate number |\n' |
| 4625 | ' | | separator ' |
| 4626 | 'characters. |\n' |
| 4627 | ' ' |
| 4628 | '+-----------+------------------------------------------------------------+\n' |
| 4629 | ' | None | The same as ' |
| 4630 | '"\'d\'". |\n' |
| 4631 | ' ' |
| 4632 | '+-----------+------------------------------------------------------------+\n' |
| 4633 | '\n' |
| 4634 | 'In addition to the above presentation types, integers can ' |
| 4635 | 'be formatted\n' |
| 4636 | 'with the floating point presentation types listed below ' |
| 4637 | '(except "\'n\'"\n' |
| 4638 | 'and "None"). When doing so, "float()" is used to convert ' |
| 4639 | 'the integer\n' |
| 4640 | 'to a floating point number before formatting.\n' |
| 4641 | '\n' |
| 4642 | 'The available presentation types for floating point and ' |
| 4643 | 'decimal values\n' |
| 4644 | 'are:\n' |
| 4645 | '\n' |
| 4646 | ' ' |
| 4647 | '+-----------+------------------------------------------------------------+\n' |
| 4648 | ' | Type | ' |
| 4649 | 'Meaning ' |
| 4650 | '|\n' |
| 4651 | ' ' |
| 4652 | '+===========+============================================================+\n' |
| 4653 | ' | "\'e\'" | Exponent notation. Prints the number in ' |
| 4654 | 'scientific |\n' |
| 4655 | " | | notation using the letter 'e' to indicate " |
| 4656 | 'the exponent. |\n' |
| 4657 | ' | | The default precision is ' |
| 4658 | '"6". |\n' |
| 4659 | ' ' |
| 4660 | '+-----------+------------------------------------------------------------+\n' |
| 4661 | ' | "\'E\'" | Exponent notation. Same as "\'e\'" ' |
| 4662 | 'except it uses an upper |\n' |
| 4663 | " | | case 'E' as the separator " |
| 4664 | 'character. |\n' |
| 4665 | ' ' |
| 4666 | '+-----------+------------------------------------------------------------+\n' |
| 4667 | ' | "\'f\'" | Fixed point. Displays the number as a ' |
| 4668 | 'fixed-point number. |\n' |
| 4669 | ' | | The default precision is ' |
| 4670 | '"6". |\n' |
| 4671 | ' ' |
| 4672 | '+-----------+------------------------------------------------------------+\n' |
| 4673 | ' | "\'F\'" | Fixed point. Same as ' |
| 4674 | '"\'f\'". |\n' |
| 4675 | ' ' |
| 4676 | '+-----------+------------------------------------------------------------+\n' |
| 4677 | ' | "\'g\'" | General format. For a given precision ' |
| 4678 | '"p >= 1", this |\n' |
| 4679 | ' | | rounds the number to "p" significant ' |
| 4680 | 'digits and then |\n' |
| 4681 | ' | | formats the result in either fixed-point ' |
| 4682 | 'format or in |\n' |
| 4683 | ' | | scientific notation, depending on its ' |
| 4684 | 'magnitude. The |\n' |
| 4685 | ' | | precise rules are as follows: suppose that ' |
| 4686 | 'the result |\n' |
| 4687 | ' | | formatted with presentation type "\'e\'" ' |
| 4688 | 'and precision "p-1" |\n' |
| 4689 | ' | | would have exponent "exp". Then if "-4 <= ' |
| 4690 | 'exp < p", the |\n' |
| 4691 | ' | | number is formatted with presentation type ' |
| 4692 | '"\'f\'" and |\n' |
| 4693 | ' | | precision "p-1-exp". Otherwise, the ' |
| 4694 | 'number is formatted |\n' |
| 4695 | ' | | with presentation type "\'e\'" and ' |
| 4696 | 'precision "p-1". In both |\n' |
| 4697 | ' | | cases insignificant trailing zeros are ' |
| 4698 | 'removed from the |\n' |
| 4699 | ' | | significand, and the decimal point is also ' |
| 4700 | 'removed if |\n' |
| 4701 | ' | | there are no remaining digits following ' |
| 4702 | 'it. Positive and |\n' |
| 4703 | ' | | negative infinity, positive and negative ' |
| 4704 | 'zero, and nans, |\n' |
| 4705 | ' | | are formatted as "inf", "-inf", "0", "-0" ' |
| 4706 | 'and "nan" |\n' |
| 4707 | ' | | respectively, regardless of the ' |
| 4708 | 'precision. A precision of |\n' |
| 4709 | ' | | "0" is treated as equivalent to a ' |
| 4710 | 'precision of "1". The |\n' |
| 4711 | ' | | default precision is ' |
| 4712 | '"6". |\n' |
| 4713 | ' ' |
| 4714 | '+-----------+------------------------------------------------------------+\n' |
| 4715 | ' | "\'G\'" | General format. Same as "\'g\'" except ' |
| 4716 | 'switches to "\'E\'" if |\n' |
| 4717 | ' | | the number gets too large. The ' |
| 4718 | 'representations of infinity |\n' |
| 4719 | ' | | and NaN are uppercased, ' |
| 4720 | 'too. |\n' |
| 4721 | ' ' |
| 4722 | '+-----------+------------------------------------------------------------+\n' |
| 4723 | ' | "\'n\'" | Number. This is the same as "\'g\'", ' |
| 4724 | 'except that it uses the |\n' |
| 4725 | ' | | current locale setting to insert the ' |
| 4726 | 'appropriate number |\n' |
| 4727 | ' | | separator ' |
| 4728 | 'characters. |\n' |
| 4729 | ' ' |
| 4730 | '+-----------+------------------------------------------------------------+\n' |
| 4731 | ' | "\'%\'" | Percentage. Multiplies the number by 100 ' |
| 4732 | 'and displays in |\n' |
| 4733 | ' | | fixed ("\'f\'") format, followed by a ' |
| 4734 | 'percent sign. |\n' |
| 4735 | ' ' |
| 4736 | '+-----------+------------------------------------------------------------+\n' |
| 4737 | ' | None | The same as ' |
| 4738 | '"\'g\'". |\n' |
| 4739 | ' ' |
| 4740 | '+-----------+------------------------------------------------------------+\n' |
| 4741 | '\n' |
| 4742 | '\n' |
| 4743 | 'Format examples\n' |
| 4744 | '===============\n' |
| 4745 | '\n' |
| 4746 | 'This section contains examples of the "str.format()" syntax ' |
| 4747 | 'and\n' |
| 4748 | 'comparison with the old "%"-formatting.\n' |
| 4749 | '\n' |
| 4750 | 'In most of the cases the syntax is similar to the old ' |
| 4751 | '"%"-formatting,\n' |
| 4752 | 'with the addition of the "{}" and with ":" used instead of ' |
| 4753 | '"%". For\n' |
| 4754 | 'example, "\'%03.2f\'" can be translated to "\'{:03.2f}\'".\n' |
| 4755 | '\n' |
| 4756 | 'The new format syntax also supports new and different ' |
| 4757 | 'options, shown\n' |
| 4758 | 'in the follow examples.\n' |
| 4759 | '\n' |
| 4760 | 'Accessing arguments by position:\n' |
| 4761 | '\n' |
| 4762 | " >>> '{0}, {1}, {2}'.format('a', 'b', 'c')\n" |
| 4763 | " 'a, b, c'\n" |
| 4764 | " >>> '{}, {}, {}'.format('a', 'b', 'c') # 2.7+ only\n" |
| 4765 | " 'a, b, c'\n" |
| 4766 | " >>> '{2}, {1}, {0}'.format('a', 'b', 'c')\n" |
| 4767 | " 'c, b, a'\n" |
| 4768 | " >>> '{2}, {1}, {0}'.format(*'abc') # unpacking " |
| 4769 | 'argument sequence\n' |
| 4770 | " 'c, b, a'\n" |
| 4771 | " >>> '{0}{1}{0}'.format('abra', 'cad') # arguments' " |
| 4772 | 'indices can be repeated\n' |
| 4773 | " 'abracadabra'\n" |
| 4774 | '\n' |
| 4775 | 'Accessing arguments by name:\n' |
| 4776 | '\n' |
| 4777 | " >>> 'Coordinates: {latitude}, " |
| 4778 | "{longitude}'.format(latitude='37.24N', " |
| 4779 | "longitude='-115.81W')\n" |
| 4780 | " 'Coordinates: 37.24N, -115.81W'\n" |
| 4781 | " >>> coord = {'latitude': '37.24N', 'longitude': " |
| 4782 | "'-115.81W'}\n" |
| 4783 | " >>> 'Coordinates: {latitude}, " |
| 4784 | "{longitude}'.format(**coord)\n" |
| 4785 | " 'Coordinates: 37.24N, -115.81W'\n" |
| 4786 | '\n' |
| 4787 | "Accessing arguments' attributes:\n" |
| 4788 | '\n' |
| 4789 | ' >>> c = 3-5j\n' |
| 4790 | " >>> ('The complex number {0} is formed from the real " |
| 4791 | "part {0.real} '\n" |
| 4792 | " ... 'and the imaginary part {0.imag}.').format(c)\n" |
| 4793 | " 'The complex number (3-5j) is formed from the real part " |
| 4794 | "3.0 and the imaginary part -5.0.'\n" |
| 4795 | ' >>> class Point(object):\n' |
| 4796 | ' ... def __init__(self, x, y):\n' |
| 4797 | ' ... self.x, self.y = x, y\n' |
| 4798 | ' ... def __str__(self):\n' |
| 4799 | " ... return 'Point({self.x}, " |
| 4800 | "{self.y})'.format(self=self)\n" |
| 4801 | ' ...\n' |
| 4802 | ' >>> str(Point(4, 2))\n' |
| 4803 | " 'Point(4, 2)'\n" |
| 4804 | '\n' |
| 4805 | "Accessing arguments' items:\n" |
| 4806 | '\n' |
| 4807 | ' >>> coord = (3, 5)\n' |
| 4808 | " >>> 'X: {0[0]}; Y: {0[1]}'.format(coord)\n" |
| 4809 | " 'X: 3; Y: 5'\n" |
| 4810 | '\n' |
| 4811 | 'Replacing "%s" and "%r":\n' |
| 4812 | '\n' |
| 4813 | ' >>> "repr() shows quotes: {!r}; str() doesn\'t: ' |
| 4814 | '{!s}".format(\'test1\', \'test2\')\n' |
| 4815 | ' "repr() shows quotes: \'test1\'; str() doesn\'t: test2"\n' |
| 4816 | '\n' |
| 4817 | 'Aligning the text and specifying a width:\n' |
| 4818 | '\n' |
| 4819 | " >>> '{:<30}'.format('left aligned')\n" |
| 4820 | " 'left aligned '\n" |
| 4821 | " >>> '{:>30}'.format('right aligned')\n" |
| 4822 | " ' right aligned'\n" |
| 4823 | " >>> '{:^30}'.format('centered')\n" |
| 4824 | " ' centered '\n" |
| 4825 | " >>> '{:*^30}'.format('centered') # use '*' as a fill " |
| 4826 | 'char\n' |
| 4827 | " '***********centered***********'\n" |
| 4828 | '\n' |
| 4829 | 'Replacing "%+f", "%-f", and "% f" and specifying a sign:\n' |
| 4830 | '\n' |
| 4831 | " >>> '{:+f}; {:+f}'.format(3.14, -3.14) # show it " |
| 4832 | 'always\n' |
| 4833 | " '+3.140000; -3.140000'\n" |
| 4834 | " >>> '{: f}; {: f}'.format(3.14, -3.14) # show a space " |
| 4835 | 'for positive numbers\n' |
| 4836 | " ' 3.140000; -3.140000'\n" |
| 4837 | " >>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the " |
| 4838 | "minus -- same as '{:f}; {:f}'\n" |
| 4839 | " '3.140000; -3.140000'\n" |
| 4840 | '\n' |
| 4841 | 'Replacing "%x" and "%o" and converting the value to ' |
| 4842 | 'different bases:\n' |
| 4843 | '\n' |
| 4844 | ' >>> # format also supports binary numbers\n' |
| 4845 | ' >>> "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: ' |
| 4846 | '{0:b}".format(42)\n' |
| 4847 | " 'int: 42; hex: 2a; oct: 52; bin: 101010'\n" |
| 4848 | ' >>> # with 0x, 0o, or 0b as prefix:\n' |
| 4849 | ' >>> "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: ' |
| 4850 | '{0:#b}".format(42)\n' |
| 4851 | " 'int: 42; hex: 0x2a; oct: 0o52; bin: 0b101010'\n" |
| 4852 | '\n' |
| 4853 | 'Using the comma as a thousands separator:\n' |
| 4854 | '\n' |
| 4855 | " >>> '{:,}'.format(1234567890)\n" |
| 4856 | " '1,234,567,890'\n" |
| 4857 | '\n' |
| 4858 | 'Expressing a percentage:\n' |
| 4859 | '\n' |
| 4860 | ' >>> points = 19.5\n' |
| 4861 | ' >>> total = 22\n' |
| 4862 | " >>> 'Correct answers: {:.2%}'.format(points/total)\n" |
| 4863 | " 'Correct answers: 88.64%'\n" |
| 4864 | '\n' |
| 4865 | 'Using type-specific formatting:\n' |
| 4866 | '\n' |
| 4867 | ' >>> import datetime\n' |
| 4868 | ' >>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)\n' |
| 4869 | " >>> '{:%Y-%m-%d %H:%M:%S}'.format(d)\n" |
| 4870 | " '2010-07-04 12:15:58'\n" |
| 4871 | '\n' |
| 4872 | 'Nesting arguments and more complex examples:\n' |
| 4873 | '\n' |
| 4874 | " >>> for align, text in zip('<^>', ['left', 'center', " |
| 4875 | "'right']):\n" |
| 4876 | " ... '{0:{fill}{align}16}'.format(text, fill=align, " |
| 4877 | 'align=align)\n' |
| 4878 | ' ...\n' |
| 4879 | " 'left<<<<<<<<<<<<'\n" |
| 4880 | " '^^^^^center^^^^^'\n" |
| 4881 | " '>>>>>>>>>>>right'\n" |
| 4882 | ' >>>\n' |
| 4883 | ' >>> octets = [192, 168, 0, 1]\n' |
| 4884 | " >>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)\n" |
| 4885 | " 'C0A80001'\n" |
| 4886 | ' >>> int(_, 16)\n' |
| 4887 | ' 3232235521\n' |
| 4888 | ' >>>\n' |
| 4889 | ' >>> width = 5\n' |
| 4890 | ' >>> for num in range(5,12):\n' |
| 4891 | " ... for base in 'dXob':\n" |
| 4892 | " ... print '{0:{width}{base}}'.format(num, " |
| 4893 | 'base=base, width=width),\n' |
| 4894 | ' ... print\n' |
| 4895 | ' ...\n' |
| 4896 | ' 5 5 5 101\n' |
| 4897 | ' 6 6 6 110\n' |
| 4898 | ' 7 7 7 111\n' |
| 4899 | ' 8 8 10 1000\n' |
| 4900 | ' 9 9 11 1001\n' |
| 4901 | ' 10 A 12 1010\n' |
| 4902 | ' 11 B 13 1011\n', |
| 4903 | 'function': '\n' |
| 4904 | 'Function definitions\n' |
| 4905 | '********************\n' |
| 4906 | '\n' |
| 4907 | 'A function definition defines a user-defined function object ' |
| 4908 | '(see\n' |
| 4909 | 'section The standard type hierarchy):\n' |
| 4910 | '\n' |
| 4911 | ' decorated ::= decorators (classdef | funcdef)\n' |
| 4912 | ' decorators ::= decorator+\n' |
| 4913 | ' decorator ::= "@" dotted_name ["(" [argument_list [","]] ' |
| 4914 | '")"] NEWLINE\n' |
| 4915 | ' funcdef ::= "def" funcname "(" [parameter_list] ")" ' |
| 4916 | '":" suite\n' |
| 4917 | ' dotted_name ::= identifier ("." identifier)*\n' |
| 4918 | ' parameter_list ::= (defparameter ",")*\n' |
| 4919 | ' ( "*" identifier ["," "**" identifier]\n' |
| 4920 | ' | "**" identifier\n' |
| 4921 | ' | defparameter [","] )\n' |
| 4922 | ' defparameter ::= parameter ["=" expression]\n' |
| 4923 | ' sublist ::= parameter ("," parameter)* [","]\n' |
| 4924 | ' parameter ::= identifier | "(" sublist ")"\n' |
| 4925 | ' funcname ::= identifier\n' |
| 4926 | '\n' |
| 4927 | 'A function definition is an executable statement. Its execution ' |
| 4928 | 'binds\n' |
| 4929 | 'the function name in the current local namespace to a function ' |
| 4930 | 'object\n' |
| 4931 | '(a wrapper around the executable code for the function). This\n' |
| 4932 | 'function object contains a reference to the current global ' |
| 4933 | 'namespace\n' |
| 4934 | 'as the global namespace to be used when the function is called.\n' |
| 4935 | '\n' |
| 4936 | 'The function definition does not execute the function body; this ' |
| 4937 | 'gets\n' |
| 4938 | 'executed only when the function is called. [3]\n' |
| 4939 | '\n' |
| 4940 | 'A function definition may be wrapped by one or more *decorator*\n' |
| 4941 | 'expressions. Decorator expressions are evaluated when the ' |
| 4942 | 'function is\n' |
| 4943 | 'defined, in the scope that contains the function definition. ' |
| 4944 | 'The\n' |
| 4945 | 'result must be a callable, which is invoked with the function ' |
| 4946 | 'object\n' |
| 4947 | 'as the only argument. The returned value is bound to the ' |
| 4948 | 'function name\n' |
| 4949 | 'instead of the function object. Multiple decorators are applied ' |
| 4950 | 'in\n' |
| 4951 | 'nested fashion. For example, the following code:\n' |
| 4952 | '\n' |
| 4953 | ' @f1(arg)\n' |
| 4954 | ' @f2\n' |
| 4955 | ' def func(): pass\n' |
| 4956 | '\n' |
| 4957 | 'is equivalent to:\n' |
| 4958 | '\n' |
| 4959 | ' def func(): pass\n' |
| 4960 | ' func = f1(arg)(f2(func))\n' |
| 4961 | '\n' |
| 4962 | 'When one or more top-level *parameters* have the form ' |
| 4963 | '*parameter* "="\n' |
| 4964 | '*expression*, the function is said to have "default parameter ' |
| 4965 | 'values."\n' |
| 4966 | 'For a parameter with a default value, the corresponding ' |
| 4967 | '*argument* may\n' |
| 4968 | "be omitted from a call, in which case the parameter's default " |
| 4969 | 'value is\n' |
| 4970 | 'substituted. If a parameter has a default value, all following\n' |
| 4971 | 'parameters must also have a default value --- this is a ' |
| 4972 | 'syntactic\n' |
| 4973 | 'restriction that is not expressed by the grammar.\n' |
| 4974 | '\n' |
| 4975 | '**Default parameter values are evaluated when the function ' |
| 4976 | 'definition\n' |
| 4977 | 'is executed.** This means that the expression is evaluated ' |
| 4978 | 'once, when\n' |
| 4979 | 'the function is defined, and that the same "pre-computed" value ' |
| 4980 | 'is\n' |
| 4981 | 'used for each call. This is especially important to understand ' |
| 4982 | 'when a\n' |
| 4983 | 'default parameter is a mutable object, such as a list or a ' |
| 4984 | 'dictionary:\n' |
| 4985 | 'if the function modifies the object (e.g. by appending an item ' |
| 4986 | 'to a\n' |
| 4987 | 'list), the default value is in effect modified. This is ' |
| 4988 | 'generally not\n' |
| 4989 | 'what was intended. A way around this is to use "None" as the\n' |
| 4990 | 'default, and explicitly test for it in the body of the function, ' |
| 4991 | 'e.g.:\n' |
| 4992 | '\n' |
| 4993 | ' def whats_on_the_telly(penguin=None):\n' |
| 4994 | ' if penguin is None:\n' |
| 4995 | ' penguin = []\n' |
| 4996 | ' penguin.append("property of the zoo")\n' |
| 4997 | ' return penguin\n' |
| 4998 | '\n' |
| 4999 | 'Function call semantics are described in more detail in section ' |
| 5000 | 'Calls.\n' |
| 5001 | 'A function call always assigns values to all parameters ' |
| 5002 | 'mentioned in\n' |
| 5003 | 'the parameter list, either from position arguments, from ' |
| 5004 | 'keyword\n' |
| 5005 | 'arguments, or from default values. If the form ""*identifier"" ' |
| 5006 | 'is\n' |
| 5007 | 'present, it is initialized to a tuple receiving any excess ' |
| 5008 | 'positional\n' |
| 5009 | 'parameters, defaulting to the empty tuple. If the form\n' |
| 5010 | '""**identifier"" is present, it is initialized to a new ' |
| 5011 | 'dictionary\n' |
| 5012 | 'receiving any excess keyword arguments, defaulting to a new ' |
| 5013 | 'empty\n' |
| 5014 | 'dictionary.\n' |
| 5015 | '\n' |
| 5016 | 'It is also possible to create anonymous functions (functions not ' |
| 5017 | 'bound\n' |
| 5018 | 'to a name), for immediate use in expressions. This uses lambda\n' |
| 5019 | 'expressions, described in section Lambdas. Note that the ' |
| 5020 | 'lambda\n' |
| 5021 | 'expression is merely a shorthand for a simplified function ' |
| 5022 | 'definition;\n' |
| 5023 | 'a function defined in a ""def"" statement can be passed around ' |
| 5024 | 'or\n' |
| 5025 | 'assigned to another name just like a function defined by a ' |
| 5026 | 'lambda\n' |
| 5027 | 'expression. The ""def"" form is actually more powerful since ' |
| 5028 | 'it\n' |
| 5029 | 'allows the execution of multiple statements.\n' |
| 5030 | '\n' |
| 5031 | "**Programmer's note:** Functions are first-class objects. A " |
| 5032 | '""def""\n' |
| 5033 | 'form executed inside a function definition defines a local ' |
| 5034 | 'function\n' |
| 5035 | 'that can be returned or passed around. Free variables used in ' |
| 5036 | 'the\n' |
| 5037 | 'nested function can access the local variables of the function\n' |
| 5038 | 'containing the def. See section Naming and binding for ' |
| 5039 | 'details.\n', |
| 5040 | 'global': '\n' |
| 5041 | 'The "global" statement\n' |
| 5042 | '**********************\n' |
| 5043 | '\n' |
| 5044 | ' global_stmt ::= "global" identifier ("," identifier)*\n' |
| 5045 | '\n' |
| 5046 | 'The "global" statement is a declaration which holds for the ' |
| 5047 | 'entire\n' |
| 5048 | 'current code block. It means that the listed identifiers are to ' |
| 5049 | 'be\n' |
| 5050 | 'interpreted as globals. It would be impossible to assign to a ' |
| 5051 | 'global\n' |
| 5052 | 'variable without "global", although free variables may refer to\n' |
| 5053 | 'globals without being declared global.\n' |
| 5054 | '\n' |
| 5055 | 'Names listed in a "global" statement must not be used in the same ' |
| 5056 | 'code\n' |
| 5057 | 'block textually preceding that "global" statement.\n' |
| 5058 | '\n' |
| 5059 | 'Names listed in a "global" statement must not be defined as ' |
| 5060 | 'formal\n' |
| 5061 | 'parameters or in a "for" loop control target, "class" definition,\n' |
| 5062 | 'function definition, or "import" statement.\n' |
| 5063 | '\n' |
| 5064 | '**CPython implementation detail:** The current implementation does ' |
| 5065 | 'not\n' |
| 5066 | 'enforce the latter two restrictions, but programs should not ' |
| 5067 | 'abuse\n' |
| 5068 | 'this freedom, as future implementations may enforce them or ' |
| 5069 | 'silently\n' |
| 5070 | 'change the meaning of the program.\n' |
| 5071 | '\n' |
| 5072 | '**Programmer\'s note:** "global" is a directive to the parser. ' |
| 5073 | 'It\n' |
| 5074 | 'applies only to code parsed at the same time as the "global"\n' |
| 5075 | 'statement. In particular, a "global" statement contained in an ' |
| 5076 | '"exec"\n' |
| 5077 | 'statement does not affect the code block *containing* the "exec"\n' |
| 5078 | 'statement, and code contained in an "exec" statement is unaffected ' |
| 5079 | 'by\n' |
| 5080 | '"global" statements in the code containing the "exec" statement. ' |
| 5081 | 'The\n' |
| 5082 | 'same applies to the "eval()", "execfile()" and "compile()" ' |
| 5083 | 'functions.\n', |
| 5084 | 'id-classes': '\n' |
| 5085 | 'Reserved classes of identifiers\n' |
| 5086 | '*******************************\n' |
| 5087 | '\n' |
| 5088 | 'Certain classes of identifiers (besides keywords) have ' |
| 5089 | 'special\n' |
| 5090 | 'meanings. These classes are identified by the patterns of ' |
| 5091 | 'leading and\n' |
| 5092 | 'trailing underscore characters:\n' |
| 5093 | '\n' |
| 5094 | '"_*"\n' |
| 5095 | ' Not imported by "from module import *". The special ' |
| 5096 | 'identifier "_"\n' |
| 5097 | ' is used in the interactive interpreter to store the result ' |
| 5098 | 'of the\n' |
| 5099 | ' last evaluation; it is stored in the "__builtin__" module. ' |
| 5100 | 'When\n' |
| 5101 | ' not in interactive mode, "_" has no special meaning and is ' |
| 5102 | 'not\n' |
| 5103 | ' defined. See section The import statement.\n' |
| 5104 | '\n' |
| 5105 | ' Note: The name "_" is often used in conjunction with\n' |
| 5106 | ' internationalization; refer to the documentation for the\n' |
| 5107 | ' "gettext" module for more information on this ' |
| 5108 | 'convention.\n' |
| 5109 | '\n' |
| 5110 | '"__*__"\n' |
| 5111 | ' System-defined names. These names are defined by the ' |
| 5112 | 'interpreter\n' |
| 5113 | ' and its implementation (including the standard library). ' |
| 5114 | 'Current\n' |
| 5115 | ' system names are discussed in the Special method names ' |
| 5116 | 'section and\n' |
| 5117 | ' elsewhere. More will likely be defined in future versions ' |
| 5118 | 'of\n' |
| 5119 | ' Python. *Any* use of "__*__" names, in any context, that ' |
| 5120 | 'does not\n' |
| 5121 | ' follow explicitly documented use, is subject to breakage ' |
| 5122 | 'without\n' |
| 5123 | ' warning.\n' |
| 5124 | '\n' |
| 5125 | '"__*"\n' |
| 5126 | ' Class-private names. Names in this category, when used ' |
| 5127 | 'within the\n' |
| 5128 | ' context of a class definition, are re-written to use a ' |
| 5129 | 'mangled form\n' |
| 5130 | ' to help avoid name clashes between "private" attributes of ' |
| 5131 | 'base and\n' |
| 5132 | ' derived classes. See section Identifiers (Names).\n', |
| 5133 | 'identifiers': '\n' |
| 5134 | 'Identifiers and keywords\n' |
| 5135 | '************************\n' |
| 5136 | '\n' |
| 5137 | 'Identifiers (also referred to as *names*) are described by ' |
| 5138 | 'the\n' |
| 5139 | 'following lexical definitions:\n' |
| 5140 | '\n' |
| 5141 | ' identifier ::= (letter|"_") (letter | digit | "_")*\n' |
| 5142 | ' letter ::= lowercase | uppercase\n' |
| 5143 | ' lowercase ::= "a"..."z"\n' |
| 5144 | ' uppercase ::= "A"..."Z"\n' |
| 5145 | ' digit ::= "0"..."9"\n' |
| 5146 | '\n' |
| 5147 | 'Identifiers are unlimited in length. Case is significant.\n' |
| 5148 | '\n' |
| 5149 | '\n' |
| 5150 | 'Keywords\n' |
| 5151 | '========\n' |
| 5152 | '\n' |
| 5153 | 'The following identifiers are used as reserved words, or ' |
| 5154 | '*keywords* of\n' |
| 5155 | 'the language, and cannot be used as ordinary identifiers. ' |
| 5156 | 'They must\n' |
| 5157 | 'be spelled exactly as written here:\n' |
| 5158 | '\n' |
| 5159 | ' and del from not while\n' |
| 5160 | ' as elif global or with\n' |
| 5161 | ' assert else if pass yield\n' |
| 5162 | ' break except import print\n' |
| 5163 | ' class exec in raise\n' |
| 5164 | ' continue finally is return\n' |
| 5165 | ' def for lambda try\n' |
| 5166 | '\n' |
| 5167 | 'Changed in version 2.4: "None" became a constant and is now ' |
| 5168 | 'recognized\n' |
| 5169 | 'by the compiler as a name for the built-in object "None". ' |
| 5170 | 'Although it\n' |
| 5171 | 'is not a keyword, you cannot assign a different object to ' |
| 5172 | 'it.\n' |
| 5173 | '\n' |
| 5174 | 'Changed in version 2.5: Using "as" and "with" as identifiers ' |
| 5175 | 'triggers\n' |
| 5176 | 'a warning. To use them as keywords, enable the ' |
| 5177 | '"with_statement"\n' |
| 5178 | 'future feature .\n' |
| 5179 | '\n' |
| 5180 | 'Changed in version 2.6: "as" and "with" are full keywords.\n' |
| 5181 | '\n' |
| 5182 | '\n' |
| 5183 | 'Reserved classes of identifiers\n' |
| 5184 | '===============================\n' |
| 5185 | '\n' |
| 5186 | 'Certain classes of identifiers (besides keywords) have ' |
| 5187 | 'special\n' |
| 5188 | 'meanings. These classes are identified by the patterns of ' |
| 5189 | 'leading and\n' |
| 5190 | 'trailing underscore characters:\n' |
| 5191 | '\n' |
| 5192 | '"_*"\n' |
| 5193 | ' Not imported by "from module import *". The special ' |
| 5194 | 'identifier "_"\n' |
| 5195 | ' is used in the interactive interpreter to store the result ' |
| 5196 | 'of the\n' |
| 5197 | ' last evaluation; it is stored in the "__builtin__" ' |
| 5198 | 'module. When\n' |
| 5199 | ' not in interactive mode, "_" has no special meaning and is ' |
| 5200 | 'not\n' |
| 5201 | ' defined. See section The import statement.\n' |
| 5202 | '\n' |
| 5203 | ' Note: The name "_" is often used in conjunction with\n' |
| 5204 | ' internationalization; refer to the documentation for ' |
| 5205 | 'the\n' |
| 5206 | ' "gettext" module for more information on this ' |
| 5207 | 'convention.\n' |
| 5208 | '\n' |
| 5209 | '"__*__"\n' |
| 5210 | ' System-defined names. These names are defined by the ' |
| 5211 | 'interpreter\n' |
| 5212 | ' and its implementation (including the standard library). ' |
| 5213 | 'Current\n' |
| 5214 | ' system names are discussed in the Special method names ' |
| 5215 | 'section and\n' |
| 5216 | ' elsewhere. More will likely be defined in future versions ' |
| 5217 | 'of\n' |
| 5218 | ' Python. *Any* use of "__*__" names, in any context, that ' |
| 5219 | 'does not\n' |
| 5220 | ' follow explicitly documented use, is subject to breakage ' |
| 5221 | 'without\n' |
| 5222 | ' warning.\n' |
| 5223 | '\n' |
| 5224 | '"__*"\n' |
| 5225 | ' Class-private names. Names in this category, when used ' |
| 5226 | 'within the\n' |
| 5227 | ' context of a class definition, are re-written to use a ' |
| 5228 | 'mangled form\n' |
| 5229 | ' to help avoid name clashes between "private" attributes of ' |
| 5230 | 'base and\n' |
| 5231 | ' derived classes. See section Identifiers (Names).\n', |
| 5232 | 'if': '\n' |
| 5233 | 'The "if" statement\n' |
| 5234 | '******************\n' |
| 5235 | '\n' |
| 5236 | 'The "if" statement is used for conditional execution:\n' |
| 5237 | '\n' |
| 5238 | ' if_stmt ::= "if" expression ":" suite\n' |
| 5239 | ' ( "elif" expression ":" suite )*\n' |
| 5240 | ' ["else" ":" suite]\n' |
| 5241 | '\n' |
| 5242 | 'It selects exactly one of the suites by evaluating the expressions ' |
| 5243 | 'one\n' |
| 5244 | 'by one until one is found to be true (see section Boolean operations\n' |
| 5245 | 'for the definition of true and false); then that suite is executed\n' |
| 5246 | '(and no other part of the "if" statement is executed or evaluated).\n' |
| 5247 | 'If all expressions are false, the suite of the "else" clause, if\n' |
| 5248 | 'present, is executed.\n', |
| 5249 | 'imaginary': '\n' |
| 5250 | 'Imaginary literals\n' |
| 5251 | '******************\n' |
| 5252 | '\n' |
| 5253 | 'Imaginary literals are described by the following lexical ' |
| 5254 | 'definitions:\n' |
| 5255 | '\n' |
| 5256 | ' imagnumber ::= (floatnumber | intpart) ("j" | "J")\n' |
| 5257 | '\n' |
| 5258 | 'An imaginary literal yields a complex number with a real part ' |
| 5259 | 'of 0.0.\n' |
| 5260 | 'Complex numbers are represented as a pair of floating point ' |
| 5261 | 'numbers\n' |
| 5262 | 'and have the same restrictions on their range. To create a ' |
| 5263 | 'complex\n' |
| 5264 | 'number with a nonzero real part, add a floating point number to ' |
| 5265 | 'it,\n' |
| 5266 | 'e.g., "(3+4j)". Some examples of imaginary literals:\n' |
| 5267 | '\n' |
| 5268 | ' 3.14j 10.j 10j .001j 1e100j 3.14e-10j\n', |
| 5269 | 'import': '\n' |
| 5270 | 'The "import" statement\n' |
| 5271 | '**********************\n' |
| 5272 | '\n' |
| 5273 | ' import_stmt ::= "import" module ["as" name] ( "," module ' |
| 5274 | '["as" name] )*\n' |
| 5275 | ' | "from" relative_module "import" identifier ' |
| 5276 | '["as" name]\n' |
| 5277 | ' ( "," identifier ["as" name] )*\n' |
| 5278 | ' | "from" relative_module "import" "(" ' |
| 5279 | 'identifier ["as" name]\n' |
| 5280 | ' ( "," identifier ["as" name] )* [","] ")"\n' |
| 5281 | ' | "from" module "import" "*"\n' |
| 5282 | ' module ::= (identifier ".")* identifier\n' |
| 5283 | ' relative_module ::= "."* module | "."+\n' |
| 5284 | ' name ::= identifier\n' |
| 5285 | '\n' |
| 5286 | 'Import statements are executed in two steps: (1) find a module, ' |
| 5287 | 'and\n' |
| 5288 | 'initialize it if necessary; (2) define a name or names in the ' |
| 5289 | 'local\n' |
| 5290 | 'namespace (of the scope where the "import" statement occurs). The\n' |
| 5291 | 'statement comes in two forms differing on whether it uses the ' |
| 5292 | '"from"\n' |
| 5293 | 'keyword. The first form (without "from") repeats these steps for ' |
| 5294 | 'each\n' |
| 5295 | 'identifier in the list. The form with "from" performs step (1) ' |
| 5296 | 'once,\n' |
| 5297 | 'and then performs step (2) repeatedly.\n' |
| 5298 | '\n' |
| 5299 | 'To understand how step (1) occurs, one must first understand how\n' |
| 5300 | 'Python handles hierarchical naming of modules. To help organize\n' |
| 5301 | 'modules and provide a hierarchy in naming, Python has a concept ' |
| 5302 | 'of\n' |
| 5303 | 'packages. A package can contain other packages and modules while\n' |
| 5304 | 'modules cannot contain other modules or packages. From a file ' |
| 5305 | 'system\n' |
| 5306 | 'perspective, packages are directories and modules are files.\n' |
| 5307 | '\n' |
| 5308 | 'Once the name of the module is known (unless otherwise specified, ' |
| 5309 | 'the\n' |
| 5310 | 'term "module" will refer to both packages and modules), searching ' |
| 5311 | 'for\n' |
| 5312 | 'the module or package can begin. The first place checked is\n' |
| 5313 | '"sys.modules", the cache of all modules that have been imported\n' |
| 5314 | 'previously. If the module is found there then it is used in step ' |
| 5315 | '(2)\n' |
| 5316 | 'of import.\n' |
| 5317 | '\n' |
| 5318 | 'If the module is not found in the cache, then "sys.meta_path" is\n' |
| 5319 | 'searched (the specification for "sys.meta_path" can be found in ' |
| 5320 | '**PEP\n' |
| 5321 | '302**). The object is a list of *finder* objects which are queried ' |
| 5322 | 'in\n' |
| 5323 | 'order as to whether they know how to load the module by calling ' |
| 5324 | 'their\n' |
| 5325 | '"find_module()" method with the name of the module. If the module\n' |
| 5326 | 'happens to be contained within a package (as denoted by the ' |
| 5327 | 'existence\n' |
| 5328 | 'of a dot in the name), then a second argument to "find_module()" ' |
| 5329 | 'is\n' |
| 5330 | 'given as the value of the "__path__" attribute from the parent ' |
| 5331 | 'package\n' |
| 5332 | '(everything up to the last dot in the name of the module being\n' |
| 5333 | 'imported). If a finder can find the module it returns a *loader*\n' |
| 5334 | '(discussed later) or returns "None".\n' |
| 5335 | '\n' |
| 5336 | 'If none of the finders on "sys.meta_path" are able to find the ' |
| 5337 | 'module\n' |
| 5338 | 'then some implicitly defined finders are queried. Implementations ' |
| 5339 | 'of\n' |
| 5340 | 'Python vary in what implicit meta path finders are defined. The ' |
| 5341 | 'one\n' |
| 5342 | 'they all do define, though, is one that handles "sys.path_hooks",\n' |
| 5343 | '"sys.path_importer_cache", and "sys.path".\n' |
| 5344 | '\n' |
| 5345 | 'The implicit finder searches for the requested module in the ' |
| 5346 | '"paths"\n' |
| 5347 | 'specified in one of two places ("paths" do not have to be file ' |
| 5348 | 'system\n' |
| 5349 | 'paths). If the module being imported is supposed to be contained\n' |
| 5350 | 'within a package then the second argument passed to ' |
| 5351 | '"find_module()",\n' |
| 5352 | '"__path__" on the parent package, is used as the source of paths. ' |
| 5353 | 'If\n' |
| 5354 | 'the module is not contained in a package then "sys.path" is used ' |
| 5355 | 'as\n' |
| 5356 | 'the source of paths.\n' |
| 5357 | '\n' |
| 5358 | 'Once the source of paths is chosen it is iterated over to find a\n' |
| 5359 | 'finder that can handle that path. The dict at\n' |
| 5360 | '"sys.path_importer_cache" caches finders for paths and is checked ' |
| 5361 | 'for\n' |
| 5362 | 'a finder. If the path does not have a finder cached then\n' |
| 5363 | '"sys.path_hooks" is searched by calling each object in the list ' |
| 5364 | 'with a\n' |
| 5365 | 'single argument of the path, returning a finder or raises\n' |
| 5366 | '"ImportError". If a finder is returned then it is cached in\n' |
| 5367 | '"sys.path_importer_cache" and then used for that path entry. If ' |
| 5368 | 'no\n' |
| 5369 | 'finder can be found but the path exists then a value of "None" is\n' |
| 5370 | 'stored in "sys.path_importer_cache" to signify that an implicit, ' |
| 5371 | 'file-\n' |
| 5372 | 'based finder that handles modules stored as individual files ' |
| 5373 | 'should be\n' |
| 5374 | 'used for that path. If the path does not exist then a finder ' |
| 5375 | 'which\n' |
| 5376 | 'always returns "None" is placed in the cache for the path.\n' |
| 5377 | '\n' |
| 5378 | 'If no finder can find the module then "ImportError" is raised.\n' |
| 5379 | 'Otherwise some finder returned a loader whose "load_module()" ' |
| 5380 | 'method\n' |
| 5381 | 'is called with the name of the module to load (see **PEP 302** for ' |
| 5382 | 'the\n' |
| 5383 | 'original definition of loaders). A loader has several ' |
| 5384 | 'responsibilities\n' |
| 5385 | 'to perform on a module it loads. First, if the module already ' |
| 5386 | 'exists\n' |
| 5387 | 'in "sys.modules" (a possibility if the loader is called outside of ' |
| 5388 | 'the\n' |
| 5389 | 'import machinery) then it is to use that module for initialization ' |
| 5390 | 'and\n' |
| 5391 | 'not a new module. But if the module does not exist in ' |
| 5392 | '"sys.modules"\n' |
| 5393 | 'then it is to be added to that dict before initialization begins. ' |
| 5394 | 'If\n' |
| 5395 | 'an error occurs during loading of the module and it was added to\n' |
| 5396 | '"sys.modules" it is to be removed from the dict. If an error ' |
| 5397 | 'occurs\n' |
| 5398 | 'but the module was already in "sys.modules" it is left in the ' |
| 5399 | 'dict.\n' |
| 5400 | '\n' |
| 5401 | 'The loader must set several attributes on the module. "__name__" ' |
| 5402 | 'is to\n' |
| 5403 | 'be set to the name of the module. "__file__" is to be the "path" ' |
| 5404 | 'to\n' |
| 5405 | 'the file unless the module is built-in (and thus listed in\n' |
| 5406 | '"sys.builtin_module_names") in which case the attribute is not ' |
| 5407 | 'set. If\n' |
| 5408 | 'what is being imported is a package then "__path__" is to be set ' |
| 5409 | 'to a\n' |
| 5410 | 'list of paths to be searched when looking for modules and ' |
| 5411 | 'packages\n' |
| 5412 | 'contained within the package being imported. "__package__" is ' |
| 5413 | 'optional\n' |
| 5414 | 'but should be set to the name of package that contains the module ' |
| 5415 | 'or\n' |
| 5416 | 'package (the empty string is used for module not contained in a\n' |
| 5417 | 'package). "__loader__" is also optional but should be set to the\n' |
| 5418 | 'loader object that is loading the module.\n' |
| 5419 | '\n' |
| 5420 | 'If an error occurs during loading then the loader raises ' |
| 5421 | '"ImportError"\n' |
| 5422 | 'if some other exception is not already being propagated. Otherwise ' |
| 5423 | 'the\n' |
| 5424 | 'loader returns the module that was loaded and initialized.\n' |
| 5425 | '\n' |
| 5426 | 'When step (1) finishes without raising an exception, step (2) can\n' |
| 5427 | 'begin.\n' |
| 5428 | '\n' |
| 5429 | 'The first form of "import" statement binds the module name in the\n' |
| 5430 | 'local namespace to the module object, and then goes on to import ' |
| 5431 | 'the\n' |
| 5432 | 'next identifier, if any. If the module name is followed by "as", ' |
| 5433 | 'the\n' |
| 5434 | 'name following "as" is used as the local name for the module.\n' |
| 5435 | '\n' |
| 5436 | 'The "from" form does not bind the module name: it goes through ' |
| 5437 | 'the\n' |
| 5438 | 'list of identifiers, looks each one of them up in the module found ' |
| 5439 | 'in\n' |
| 5440 | 'step (1), and binds the name in the local namespace to the object ' |
| 5441 | 'thus\n' |
| 5442 | 'found. As with the first form of "import", an alternate local ' |
| 5443 | 'name\n' |
| 5444 | 'can be supplied by specifying ""as" localname". If a name is not\n' |
| 5445 | 'found, "ImportError" is raised. If the list of identifiers is\n' |
| 5446 | 'replaced by a star ("\'*\'"), all public names defined in the ' |
| 5447 | 'module are\n' |
| 5448 | 'bound in the local namespace of the "import" statement..\n' |
| 5449 | '\n' |
| 5450 | 'The *public names* defined by a module are determined by checking ' |
| 5451 | 'the\n' |
| 5452 | 'module\'s namespace for a variable named "__all__"; if defined, it ' |
| 5453 | 'must\n' |
| 5454 | 'be a sequence of strings which are names defined or imported by ' |
| 5455 | 'that\n' |
| 5456 | 'module. The names given in "__all__" are all considered public ' |
| 5457 | 'and\n' |
| 5458 | 'are required to exist. If "__all__" is not defined, the set of ' |
| 5459 | 'public\n' |
| 5460 | "names includes all names found in the module's namespace which do " |
| 5461 | 'not\n' |
| 5462 | 'begin with an underscore character ("\'_\'"). "__all__" should ' |
| 5463 | 'contain\n' |
| 5464 | 'the entire public API. It is intended to avoid accidentally ' |
| 5465 | 'exporting\n' |
| 5466 | 'items that are not part of the API (such as library modules which ' |
| 5467 | 'were\n' |
| 5468 | 'imported and used within the module).\n' |
| 5469 | '\n' |
| 5470 | 'The "from" form with "*" may only occur in a module scope. If ' |
| 5471 | 'the\n' |
| 5472 | 'wild card form of import --- "import *" --- is used in a function ' |
| 5473 | 'and\n' |
| 5474 | 'the function contains or is a nested block with free variables, ' |
| 5475 | 'the\n' |
| 5476 | 'compiler will raise a "SyntaxError".\n' |
| 5477 | '\n' |
| 5478 | 'When specifying what module to import you do not have to specify ' |
| 5479 | 'the\n' |
| 5480 | 'absolute name of the module. When a module or package is ' |
| 5481 | 'contained\n' |
| 5482 | 'within another package it is possible to make a relative import ' |
| 5483 | 'within\n' |
| 5484 | 'the same top package without having to mention the package name. ' |
| 5485 | 'By\n' |
| 5486 | 'using leading dots in the specified module or package after "from" ' |
| 5487 | 'you\n' |
| 5488 | 'can specify how high to traverse up the current package hierarchy\n' |
| 5489 | 'without specifying exact names. One leading dot means the current\n' |
| 5490 | 'package where the module making the import exists. Two dots means ' |
| 5491 | 'up\n' |
| 5492 | 'one package level. Three dots is up two levels, etc. So if you ' |
| 5493 | 'execute\n' |
| 5494 | '"from . import mod" from a module in the "pkg" package then you ' |
| 5495 | 'will\n' |
| 5496 | 'end up importing "pkg.mod". If you execute "from ..subpkg2 import ' |
| 5497 | 'mod"\n' |
| 5498 | 'from within "pkg.subpkg1" you will import "pkg.subpkg2.mod". The\n' |
| 5499 | 'specification for relative imports is contained within **PEP ' |
| 5500 | '328**.\n' |
| 5501 | '\n' |
| 5502 | '"importlib.import_module()" is provided to support applications ' |
| 5503 | 'that\n' |
| 5504 | 'determine which modules need to be loaded dynamically.\n' |
| 5505 | '\n' |
| 5506 | '\n' |
| 5507 | 'Future statements\n' |
| 5508 | '=================\n' |
| 5509 | '\n' |
| 5510 | 'A *future statement* is a directive to the compiler that a ' |
| 5511 | 'particular\n' |
| 5512 | 'module should be compiled using syntax or semantics that will be\n' |
| 5513 | 'available in a specified future release of Python. The future\n' |
| 5514 | 'statement is intended to ease migration to future versions of ' |
| 5515 | 'Python\n' |
| 5516 | 'that introduce incompatible changes to the language. It allows ' |
| 5517 | 'use of\n' |
| 5518 | 'the new features on a per-module basis before the release in which ' |
| 5519 | 'the\n' |
| 5520 | 'feature becomes standard.\n' |
| 5521 | '\n' |
| 5522 | ' future_statement ::= "from" "__future__" "import" feature ["as" ' |
| 5523 | 'name]\n' |
| 5524 | ' ("," feature ["as" name])*\n' |
| 5525 | ' | "from" "__future__" "import" "(" feature ' |
| 5526 | '["as" name]\n' |
| 5527 | ' ("," feature ["as" name])* [","] ")"\n' |
| 5528 | ' feature ::= identifier\n' |
| 5529 | ' name ::= identifier\n' |
| 5530 | '\n' |
| 5531 | 'A future statement must appear near the top of the module. The ' |
| 5532 | 'only\n' |
| 5533 | 'lines that can appear before a future statement are:\n' |
| 5534 | '\n' |
| 5535 | '* the module docstring (if any),\n' |
| 5536 | '\n' |
| 5537 | '* comments,\n' |
| 5538 | '\n' |
| 5539 | '* blank lines, and\n' |
| 5540 | '\n' |
| 5541 | '* other future statements.\n' |
| 5542 | '\n' |
| 5543 | 'The features recognized by Python 2.6 are "unicode_literals",\n' |
| 5544 | '"print_function", "absolute_import", "division", "generators",\n' |
| 5545 | '"nested_scopes" and "with_statement". "generators", ' |
| 5546 | '"with_statement",\n' |
| 5547 | '"nested_scopes" are redundant in Python version 2.6 and above ' |
| 5548 | 'because\n' |
| 5549 | 'they are always enabled.\n' |
| 5550 | '\n' |
| 5551 | 'A future statement is recognized and treated specially at compile\n' |
| 5552 | 'time: Changes to the semantics of core constructs are often\n' |
| 5553 | 'implemented by generating different code. It may even be the ' |
| 5554 | 'case\n' |
| 5555 | 'that a new feature introduces new incompatible syntax (such as a ' |
| 5556 | 'new\n' |
| 5557 | 'reserved word), in which case the compiler may need to parse the\n' |
| 5558 | 'module differently. Such decisions cannot be pushed off until\n' |
| 5559 | 'runtime.\n' |
| 5560 | '\n' |
| 5561 | 'For any given release, the compiler knows which feature names ' |
| 5562 | 'have\n' |
| 5563 | 'been defined, and raises a compile-time error if a future ' |
| 5564 | 'statement\n' |
| 5565 | 'contains a feature not known to it.\n' |
| 5566 | '\n' |
| 5567 | 'The direct runtime semantics are the same as for any import ' |
| 5568 | 'statement:\n' |
| 5569 | 'there is a standard module "__future__", described later, and it ' |
| 5570 | 'will\n' |
| 5571 | 'be imported in the usual way at the time the future statement is\n' |
| 5572 | 'executed.\n' |
| 5573 | '\n' |
| 5574 | 'The interesting runtime semantics depend on the specific feature\n' |
| 5575 | 'enabled by the future statement.\n' |
| 5576 | '\n' |
| 5577 | 'Note that there is nothing special about the statement:\n' |
| 5578 | '\n' |
| 5579 | ' import __future__ [as name]\n' |
| 5580 | '\n' |
| 5581 | "That is not a future statement; it's an ordinary import statement " |
| 5582 | 'with\n' |
| 5583 | 'no special semantics or syntax restrictions.\n' |
| 5584 | '\n' |
| 5585 | 'Code compiled by an "exec" statement or calls to the built-in\n' |
| 5586 | 'functions "compile()" and "execfile()" that occur in a module "M"\n' |
| 5587 | 'containing a future statement will, by default, use the new ' |
| 5588 | 'syntax or\n' |
| 5589 | 'semantics associated with the future statement. This can, ' |
| 5590 | 'starting\n' |
| 5591 | 'with Python 2.2 be controlled by optional arguments to "compile()" ' |
| 5592 | '---\n' |
| 5593 | 'see the documentation of that function for details.\n' |
| 5594 | '\n' |
| 5595 | 'A future statement typed at an interactive interpreter prompt ' |
| 5596 | 'will\n' |
| 5597 | 'take effect for the rest of the interpreter session. If an\n' |
| 5598 | 'interpreter is started with the "-i" option, is passed a script ' |
| 5599 | 'name\n' |
| 5600 | 'to execute, and the script includes a future statement, it will be ' |
| 5601 | 'in\n' |
| 5602 | 'effect in the interactive session started after the script is\n' |
| 5603 | 'executed.\n' |
| 5604 | '\n' |
| 5605 | 'See also:\n' |
| 5606 | '\n' |
| 5607 | ' **PEP 236** - Back to the __future__\n' |
| 5608 | ' The original proposal for the __future__ mechanism.\n', |
| 5609 | 'in': '\n' |
| 5610 | 'Membership test operations\n' |
| 5611 | '**************************\n' |
| 5612 | '\n' |
| 5613 | 'The operators "in" and "not in" test for membership. "x in s"\n' |
| 5614 | 'evaluates to "True" if *x* is a member of *s*, and "False" otherwise.\n' |
| 5615 | '"x not in s" returns the negation of "x in s". All built-in ' |
| 5616 | 'sequences\n' |
| 5617 | 'and set types support this as well as dictionary, for which "in" ' |
| 5618 | 'tests\n' |
| 5619 | 'whether the dictionary has a given key. For container types such as\n' |
| 5620 | 'list, tuple, set, frozenset, dict, or collections.deque, the\n' |
| 5621 | 'expression "x in y" is equivalent to "any(x is e or x == e for e in\n' |
| 5622 | 'y)".\n' |
| 5623 | '\n' |
| 5624 | 'For the string and bytes types, "x in y" is "True" if and only if *x*\n' |
| 5625 | 'is a substring of *y*. An equivalent test is "y.find(x) != -1".\n' |
| 5626 | 'Empty strings are always considered to be a substring of any other\n' |
| 5627 | 'string, so """ in "abc"" will return "True".\n' |
| 5628 | '\n' |
| 5629 | 'For user-defined classes which define the "__contains__()" method, "x\n' |
| 5630 | 'in y" returns "True" if "y.__contains__(x)" returns a true value, and\n' |
| 5631 | '"False" otherwise.\n' |
| 5632 | '\n' |
| 5633 | 'For user-defined classes which do not define "__contains__()" but do\n' |
| 5634 | 'define "__iter__()", "x in y" is "True" if some value "z" with "x ==\n' |
| 5635 | 'z" is produced while iterating over "y". If an exception is raised\n' |
| 5636 | 'during the iteration, it is as if "in" raised that exception.\n' |
| 5637 | '\n' |
| 5638 | 'Lastly, the old-style iteration protocol is tried: if a class defines\n' |
| 5639 | '"__getitem__()", "x in y" is "True" if and only if there is a non-\n' |
| 5640 | 'negative integer index *i* such that "x == y[i]", and all lower\n' |
| 5641 | 'integer indices do not raise "IndexError" exception. (If any other\n' |
| 5642 | 'exception is raised, it is as if "in" raised that exception).\n' |
| 5643 | '\n' |
| 5644 | 'The operator "not in" is defined to have the inverse true value of\n' |
| 5645 | '"in".\n', |
| 5646 | 'integers': '\n' |
| 5647 | 'Integer and long integer literals\n' |
| 5648 | '*********************************\n' |
| 5649 | '\n' |
| 5650 | 'Integer and long integer literals are described by the ' |
| 5651 | 'following\n' |
| 5652 | 'lexical definitions:\n' |
| 5653 | '\n' |
| 5654 | ' longinteger ::= integer ("l" | "L")\n' |
| 5655 | ' integer ::= decimalinteger | octinteger | hexinteger | ' |
| 5656 | 'bininteger\n' |
| 5657 | ' decimalinteger ::= nonzerodigit digit* | "0"\n' |
| 5658 | ' octinteger ::= "0" ("o" | "O") octdigit+ | "0" octdigit+\n' |
| 5659 | ' hexinteger ::= "0" ("x" | "X") hexdigit+\n' |
| 5660 | ' bininteger ::= "0" ("b" | "B") bindigit+\n' |
| 5661 | ' nonzerodigit ::= "1"..."9"\n' |
| 5662 | ' octdigit ::= "0"..."7"\n' |
| 5663 | ' bindigit ::= "0" | "1"\n' |
| 5664 | ' hexdigit ::= digit | "a"..."f" | "A"..."F"\n' |
| 5665 | '\n' |
| 5666 | 'Although both lower case "\'l\'" and upper case "\'L\'" are ' |
| 5667 | 'allowed as\n' |
| 5668 | 'suffix for long integers, it is strongly recommended to always ' |
| 5669 | 'use\n' |
| 5670 | '"\'L\'", since the letter "\'l\'" looks too much like the digit ' |
| 5671 | '"\'1\'".\n' |
| 5672 | '\n' |
| 5673 | 'Plain integer literals that are above the largest representable ' |
| 5674 | 'plain\n' |
| 5675 | 'integer (e.g., 2147483647 when using 32-bit arithmetic) are ' |
| 5676 | 'accepted\n' |
| 5677 | 'as if they were long integers instead. [1] There is no limit ' |
| 5678 | 'for long\n' |
| 5679 | 'integer literals apart from what can be stored in available ' |
| 5680 | 'memory.\n' |
| 5681 | '\n' |
| 5682 | 'Some examples of plain integer literals (first row) and long ' |
| 5683 | 'integer\n' |
| 5684 | 'literals (second and third rows):\n' |
| 5685 | '\n' |
| 5686 | ' 7 2147483647 0177\n' |
| 5687 | ' 3L 79228162514264337593543950336L 0377L 0x100000000L\n' |
| 5688 | ' 79228162514264337593543950336 0xdeadbeef\n', |
| 5689 | 'lambda': '\n' |
| 5690 | 'Lambdas\n' |
| 5691 | '*******\n' |
| 5692 | '\n' |
| 5693 | ' lambda_expr ::= "lambda" [parameter_list]: expression\n' |
| 5694 | ' old_lambda_expr ::= "lambda" [parameter_list]: old_expression\n' |
| 5695 | '\n' |
| 5696 | 'Lambda expressions (sometimes called lambda forms) have the same\n' |
| 5697 | 'syntactic position as expressions. They are a shorthand to ' |
| 5698 | 'create\n' |
| 5699 | 'anonymous functions; the expression "lambda arguments: ' |
| 5700 | 'expression"\n' |
| 5701 | 'yields a function object. The unnamed object behaves like a ' |
| 5702 | 'function\n' |
| 5703 | 'object defined with\n' |
| 5704 | '\n' |
| 5705 | ' def name(arguments):\n' |
| 5706 | ' return expression\n' |
| 5707 | '\n' |
| 5708 | 'See section Function definitions for the syntax of parameter ' |
| 5709 | 'lists.\n' |
| 5710 | 'Note that functions created with lambda expressions cannot ' |
| 5711 | 'contain\n' |
| 5712 | 'statements.\n', |
| 5713 | 'lists': '\n' |
| 5714 | 'List displays\n' |
| 5715 | '*************\n' |
| 5716 | '\n' |
| 5717 | 'A list display is a possibly empty series of expressions enclosed ' |
| 5718 | 'in\n' |
| 5719 | 'square brackets:\n' |
| 5720 | '\n' |
| 5721 | ' list_display ::= "[" [expression_list | ' |
| 5722 | 'list_comprehension] "]"\n' |
| 5723 | ' list_comprehension ::= expression list_for\n' |
| 5724 | ' list_for ::= "for" target_list "in" ' |
| 5725 | 'old_expression_list [list_iter]\n' |
| 5726 | ' old_expression_list ::= old_expression [("," old_expression)+ ' |
| 5727 | '[","]]\n' |
| 5728 | ' old_expression ::= or_test | old_lambda_expr\n' |
| 5729 | ' list_iter ::= list_for | list_if\n' |
| 5730 | ' list_if ::= "if" old_expression [list_iter]\n' |
| 5731 | '\n' |
| 5732 | 'A list display yields a new list object. Its contents are ' |
| 5733 | 'specified\n' |
| 5734 | 'by providing either a list of expressions or a list comprehension.\n' |
| 5735 | 'When a comma-separated list of expressions is supplied, its ' |
| 5736 | 'elements\n' |
| 5737 | 'are evaluated from left to right and placed into the list object ' |
| 5738 | 'in\n' |
| 5739 | 'that order. When a list comprehension is supplied, it consists of ' |
| 5740 | 'a\n' |
| 5741 | 'single expression followed by at least one "for" clause and zero ' |
| 5742 | 'or\n' |
| 5743 | 'more "for" or "if" clauses. In this case, the elements of the new\n' |
| 5744 | 'list are those that would be produced by considering each of the ' |
| 5745 | '"for"\n' |
| 5746 | 'or "if" clauses a block, nesting from left to right, and ' |
| 5747 | 'evaluating\n' |
| 5748 | 'the expression to produce a list element each time the innermost ' |
| 5749 | 'block\n' |
| 5750 | 'is reached [1].\n', |
| 5751 | 'naming': '\n' |
| 5752 | 'Naming and binding\n' |
| 5753 | '******************\n' |
| 5754 | '\n' |
| 5755 | '*Names* refer to objects. Names are introduced by name binding\n' |
| 5756 | 'operations. Each occurrence of a name in the program text refers ' |
| 5757 | 'to\n' |
| 5758 | 'the *binding* of that name established in the innermost function ' |
| 5759 | 'block\n' |
| 5760 | 'containing the use.\n' |
| 5761 | '\n' |
| 5762 | 'A *block* is a piece of Python program text that is executed as a\n' |
| 5763 | 'unit. The following are blocks: a module, a function body, and a ' |
| 5764 | 'class\n' |
| 5765 | 'definition. Each command typed interactively is a block. A ' |
| 5766 | 'script\n' |
| 5767 | 'file (a file given as standard input to the interpreter or ' |
| 5768 | 'specified\n' |
| 5769 | 'on the interpreter command line the first argument) is a code ' |
| 5770 | 'block.\n' |
| 5771 | 'A script command (a command specified on the interpreter command ' |
| 5772 | 'line\n' |
| 5773 | "with the '**-c**' option) is a code block. The file read by the\n" |
| 5774 | 'built-in function "execfile()" is a code block. The string ' |
| 5775 | 'argument\n' |
| 5776 | 'passed to the built-in function "eval()" and to the "exec" ' |
| 5777 | 'statement\n' |
| 5778 | 'is a code block. The expression read and evaluated by the ' |
| 5779 | 'built-in\n' |
| 5780 | 'function "input()" is a code block.\n' |
| 5781 | '\n' |
| 5782 | 'A code block is executed in an *execution frame*. A frame ' |
| 5783 | 'contains\n' |
| 5784 | 'some administrative information (used for debugging) and ' |
| 5785 | 'determines\n' |
| 5786 | "where and how execution continues after the code block's execution " |
| 5787 | 'has\n' |
| 5788 | 'completed.\n' |
| 5789 | '\n' |
| 5790 | 'A *scope* defines the visibility of a name within a block. If a ' |
| 5791 | 'local\n' |
| 5792 | 'variable is defined in a block, its scope includes that block. If ' |
| 5793 | 'the\n' |
| 5794 | 'definition occurs in a function block, the scope extends to any ' |
| 5795 | 'blocks\n' |
| 5796 | 'contained within the defining one, unless a contained block ' |
| 5797 | 'introduces\n' |
| 5798 | 'a different binding for the name. The scope of names defined in ' |
| 5799 | 'a\n' |
| 5800 | 'class block is limited to the class block; it does not extend to ' |
| 5801 | 'the\n' |
| 5802 | 'code blocks of methods -- this includes generator expressions ' |
| 5803 | 'since\n' |
| 5804 | 'they are implemented using a function scope. This means that the\n' |
| 5805 | 'following will fail:\n' |
| 5806 | '\n' |
| 5807 | ' class A:\n' |
| 5808 | ' a = 42\n' |
| 5809 | ' b = list(a + i for i in range(10))\n' |
| 5810 | '\n' |
| 5811 | 'When a name is used in a code block, it is resolved using the ' |
| 5812 | 'nearest\n' |
| 5813 | 'enclosing scope. The set of all such scopes visible to a code ' |
| 5814 | 'block\n' |
| 5815 | "is called the block's *environment*.\n" |
| 5816 | '\n' |
| 5817 | 'If a name is bound in a block, it is a local variable of that ' |
| 5818 | 'block.\n' |
| 5819 | 'If a name is bound at the module level, it is a global variable. ' |
| 5820 | '(The\n' |
| 5821 | 'variables of the module code block are local and global.) If a\n' |
| 5822 | 'variable is used in a code block but not defined there, it is a ' |
| 5823 | '*free\n' |
| 5824 | 'variable*.\n' |
| 5825 | '\n' |
| 5826 | 'When a name is not found at all, a "NameError" exception is ' |
| 5827 | 'raised.\n' |
| 5828 | 'If the name refers to a local variable that has not been bound, a\n' |
| 5829 | '"UnboundLocalError" exception is raised. "UnboundLocalError" is ' |
| 5830 | 'a\n' |
| 5831 | 'subclass of "NameError".\n' |
| 5832 | '\n' |
| 5833 | 'The following constructs bind names: formal parameters to ' |
| 5834 | 'functions,\n' |
| 5835 | '"import" statements, class and function definitions (these bind ' |
| 5836 | 'the\n' |
| 5837 | 'class or function name in the defining block), and targets that ' |
| 5838 | 'are\n' |
| 5839 | 'identifiers if occurring in an assignment, "for" loop header, in ' |
| 5840 | 'the\n' |
| 5841 | 'second position of an "except" clause header or after "as" in a ' |
| 5842 | '"with"\n' |
| 5843 | 'statement. The "import" statement of the form "from ... import ' |
| 5844 | '*"\n' |
| 5845 | 'binds all names defined in the imported module, except those ' |
| 5846 | 'beginning\n' |
| 5847 | 'with an underscore. This form may only be used at the module ' |
| 5848 | 'level.\n' |
| 5849 | '\n' |
| 5850 | 'A target occurring in a "del" statement is also considered bound ' |
| 5851 | 'for\n' |
| 5852 | 'this purpose (though the actual semantics are to unbind the ' |
| 5853 | 'name). It\n' |
| 5854 | 'is illegal to unbind a name that is referenced by an enclosing ' |
| 5855 | 'scope;\n' |
| 5856 | 'the compiler will report a "SyntaxError".\n' |
| 5857 | '\n' |
| 5858 | 'Each assignment or import statement occurs within a block defined ' |
| 5859 | 'by a\n' |
| 5860 | 'class or function definition or at the module level (the ' |
| 5861 | 'top-level\n' |
| 5862 | 'code block).\n' |
| 5863 | '\n' |
| 5864 | 'If a name binding operation occurs anywhere within a code block, ' |
| 5865 | 'all\n' |
| 5866 | 'uses of the name within the block are treated as references to ' |
| 5867 | 'the\n' |
| 5868 | 'current block. This can lead to errors when a name is used within ' |
| 5869 | 'a\n' |
| 5870 | 'block before it is bound. This rule is subtle. Python lacks\n' |
| 5871 | 'declarations and allows name binding operations to occur anywhere\n' |
| 5872 | 'within a code block. The local variables of a code block can be\n' |
| 5873 | 'determined by scanning the entire text of the block for name ' |
| 5874 | 'binding\n' |
| 5875 | 'operations.\n' |
| 5876 | '\n' |
| 5877 | 'If the global statement occurs within a block, all uses of the ' |
| 5878 | 'name\n' |
| 5879 | 'specified in the statement refer to the binding of that name in ' |
| 5880 | 'the\n' |
| 5881 | 'top-level namespace. Names are resolved in the top-level namespace ' |
| 5882 | 'by\n' |
| 5883 | 'searching the global namespace, i.e. the namespace of the module\n' |
| 5884 | 'containing the code block, and the builtins namespace, the ' |
| 5885 | 'namespace\n' |
| 5886 | 'of the module "__builtin__". The global namespace is searched ' |
| 5887 | 'first.\n' |
| 5888 | 'If the name is not found there, the builtins namespace is ' |
| 5889 | 'searched.\n' |
| 5890 | 'The global statement must precede all uses of the name.\n' |
| 5891 | '\n' |
| 5892 | 'The builtins namespace associated with the execution of a code ' |
| 5893 | 'block\n' |
| 5894 | 'is actually found by looking up the name "__builtins__" in its ' |
| 5895 | 'global\n' |
| 5896 | 'namespace; this should be a dictionary or a module (in the latter ' |
| 5897 | 'case\n' |
| 5898 | "the module's dictionary is used). By default, when in the " |
| 5899 | '"__main__"\n' |
| 5900 | 'module, "__builtins__" is the built-in module "__builtin__" (note: ' |
| 5901 | 'no\n' |
| 5902 | '\'s\'); when in any other module, "__builtins__" is an alias for ' |
| 5903 | 'the\n' |
| 5904 | 'dictionary of the "__builtin__" module itself. "__builtins__" can ' |
| 5905 | 'be\n' |
| 5906 | 'set to a user-created dictionary to create a weak form of ' |
| 5907 | 'restricted\n' |
| 5908 | 'execution.\n' |
| 5909 | '\n' |
| 5910 | '**CPython implementation detail:** Users should not touch\n' |
| 5911 | '"__builtins__"; it is strictly an implementation detail. Users\n' |
| 5912 | 'wanting to override values in the builtins namespace should ' |
| 5913 | '"import"\n' |
| 5914 | 'the "__builtin__" (no \'s\') module and modify its attributes\n' |
| 5915 | 'appropriately.\n' |
| 5916 | '\n' |
| 5917 | 'The namespace for a module is automatically created the first time ' |
| 5918 | 'a\n' |
| 5919 | 'module is imported. The main module for a script is always ' |
| 5920 | 'called\n' |
| 5921 | '"__main__".\n' |
| 5922 | '\n' |
| 5923 | 'The "global" statement has the same scope as a name binding ' |
| 5924 | 'operation\n' |
| 5925 | 'in the same block. If the nearest enclosing scope for a free ' |
| 5926 | 'variable\n' |
| 5927 | 'contains a global statement, the free variable is treated as a ' |
| 5928 | 'global.\n' |
| 5929 | '\n' |
| 5930 | 'A class definition is an executable statement that may use and ' |
| 5931 | 'define\n' |
| 5932 | 'names. These references follow the normal rules for name ' |
| 5933 | 'resolution.\n' |
| 5934 | 'The namespace of the class definition becomes the attribute ' |
| 5935 | 'dictionary\n' |
| 5936 | 'of the class. Names defined at the class scope are not visible ' |
| 5937 | 'in\n' |
| 5938 | 'methods.\n' |
| 5939 | '\n' |
| 5940 | '\n' |
| 5941 | 'Interaction with dynamic features\n' |
| 5942 | '=================================\n' |
| 5943 | '\n' |
| 5944 | 'There are several cases where Python statements are illegal when ' |
| 5945 | 'used\n' |
| 5946 | 'in conjunction with nested scopes that contain free variables.\n' |
| 5947 | '\n' |
| 5948 | 'If a variable is referenced in an enclosing scope, it is illegal ' |
| 5949 | 'to\n' |
| 5950 | 'delete the name. An error will be reported at compile time.\n' |
| 5951 | '\n' |
| 5952 | 'If the wild card form of import --- "import *" --- is used in a\n' |
| 5953 | 'function and the function contains or is a nested block with free\n' |
| 5954 | 'variables, the compiler will raise a "SyntaxError".\n' |
| 5955 | '\n' |
| 5956 | 'If "exec" is used in a function and the function contains or is a\n' |
| 5957 | 'nested block with free variables, the compiler will raise a\n' |
| 5958 | '"SyntaxError" unless the exec explicitly specifies the local ' |
| 5959 | 'namespace\n' |
| 5960 | 'for the "exec". (In other words, "exec obj" would be illegal, ' |
| 5961 | 'but\n' |
| 5962 | '"exec obj in ns" would be legal.)\n' |
| 5963 | '\n' |
| 5964 | 'The "eval()", "execfile()", and "input()" functions and the ' |
| 5965 | '"exec"\n' |
| 5966 | 'statement do not have access to the full environment for ' |
| 5967 | 'resolving\n' |
| 5968 | 'names. Names may be resolved in the local and global namespaces ' |
| 5969 | 'of\n' |
| 5970 | 'the caller. Free variables are not resolved in the nearest ' |
| 5971 | 'enclosing\n' |
| 5972 | 'namespace, but in the global namespace. [1] The "exec" statement ' |
| 5973 | 'and\n' |
| 5974 | 'the "eval()" and "execfile()" functions have optional arguments ' |
| 5975 | 'to\n' |
| 5976 | 'override the global and local namespace. If only one namespace ' |
| 5977 | 'is\n' |
| 5978 | 'specified, it is used for both.\n', |
| 5979 | 'numbers': '\n' |
| 5980 | 'Numeric literals\n' |
| 5981 | '****************\n' |
| 5982 | '\n' |
| 5983 | 'There are four types of numeric literals: plain integers, long\n' |
| 5984 | 'integers, floating point numbers, and imaginary numbers. There ' |
| 5985 | 'are no\n' |
| 5986 | 'complex literals (complex numbers can be formed by adding a real\n' |
| 5987 | 'number and an imaginary number).\n' |
| 5988 | '\n' |
| 5989 | 'Note that numeric literals do not include a sign; a phrase like ' |
| 5990 | '"-1"\n' |
| 5991 | 'is actually an expression composed of the unary operator \'"-"\' ' |
| 5992 | 'and the\n' |
| 5993 | 'literal "1".\n', |
| 5994 | 'numeric-types': '\n' |
| 5995 | 'Emulating numeric types\n' |
| 5996 | '***********************\n' |
| 5997 | '\n' |
| 5998 | 'The following methods can be defined to emulate numeric ' |
| 5999 | 'objects.\n' |
| 6000 | 'Methods corresponding to operations that are not supported ' |
| 6001 | 'by the\n' |
| 6002 | 'particular kind of number implemented (e.g., bitwise ' |
| 6003 | 'operations for\n' |
| 6004 | 'non-integral numbers) should be left undefined.\n' |
| 6005 | '\n' |
| 6006 | 'object.__add__(self, other)\n' |
| 6007 | 'object.__sub__(self, other)\n' |
| 6008 | 'object.__mul__(self, other)\n' |
| 6009 | 'object.__floordiv__(self, other)\n' |
| 6010 | 'object.__mod__(self, other)\n' |
| 6011 | 'object.__divmod__(self, other)\n' |
| 6012 | 'object.__pow__(self, other[, modulo])\n' |
| 6013 | 'object.__lshift__(self, other)\n' |
| 6014 | 'object.__rshift__(self, other)\n' |
| 6015 | 'object.__and__(self, other)\n' |
| 6016 | 'object.__xor__(self, other)\n' |
| 6017 | 'object.__or__(self, other)\n' |
| 6018 | '\n' |
| 6019 | ' These methods are called to implement the binary ' |
| 6020 | 'arithmetic\n' |
| 6021 | ' operations ("+", "-", "*", "//", "%", "divmod()", ' |
| 6022 | '"pow()", "**",\n' |
| 6023 | ' "<<", ">>", "&", "^", "|"). For instance, to evaluate ' |
| 6024 | 'the\n' |
| 6025 | ' expression "x + y", where *x* is an instance of a class ' |
| 6026 | 'that has an\n' |
| 6027 | ' "__add__()" method, "x.__add__(y)" is called. The ' |
| 6028 | '"__divmod__()"\n' |
| 6029 | ' method should be the equivalent to using ' |
| 6030 | '"__floordiv__()" and\n' |
| 6031 | ' "__mod__()"; it should not be related to "__truediv__()" ' |
| 6032 | '(described\n' |
| 6033 | ' below). Note that "__pow__()" should be defined to ' |
| 6034 | 'accept an\n' |
| 6035 | ' optional third argument if the ternary version of the ' |
| 6036 | 'built-in\n' |
| 6037 | ' "pow()" function is to be supported.\n' |
| 6038 | '\n' |
| 6039 | ' If one of those methods does not support the operation ' |
| 6040 | 'with the\n' |
| 6041 | ' supplied arguments, it should return "NotImplemented".\n' |
| 6042 | '\n' |
| 6043 | 'object.__div__(self, other)\n' |
| 6044 | 'object.__truediv__(self, other)\n' |
| 6045 | '\n' |
| 6046 | ' The division operator ("/") is implemented by these ' |
| 6047 | 'methods. The\n' |
| 6048 | ' "__truediv__()" method is used when ' |
| 6049 | '"__future__.division" is in\n' |
| 6050 | ' effect, otherwise "__div__()" is used. If only one of ' |
| 6051 | 'these two\n' |
| 6052 | ' methods is defined, the object will not support division ' |
| 6053 | 'in the\n' |
| 6054 | ' alternate context; "TypeError" will be raised instead.\n' |
| 6055 | '\n' |
| 6056 | 'object.__radd__(self, other)\n' |
| 6057 | 'object.__rsub__(self, other)\n' |
| 6058 | 'object.__rmul__(self, other)\n' |
| 6059 | 'object.__rdiv__(self, other)\n' |
| 6060 | 'object.__rtruediv__(self, other)\n' |
| 6061 | 'object.__rfloordiv__(self, other)\n' |
| 6062 | 'object.__rmod__(self, other)\n' |
| 6063 | 'object.__rdivmod__(self, other)\n' |
| 6064 | 'object.__rpow__(self, other)\n' |
| 6065 | 'object.__rlshift__(self, other)\n' |
| 6066 | 'object.__rrshift__(self, other)\n' |
| 6067 | 'object.__rand__(self, other)\n' |
| 6068 | 'object.__rxor__(self, other)\n' |
| 6069 | 'object.__ror__(self, other)\n' |
| 6070 | '\n' |
| 6071 | ' These methods are called to implement the binary ' |
| 6072 | 'arithmetic\n' |
| 6073 | ' operations ("+", "-", "*", "/", "%", "divmod()", ' |
| 6074 | '"pow()", "**",\n' |
| 6075 | ' "<<", ">>", "&", "^", "|") with reflected (swapped) ' |
| 6076 | 'operands.\n' |
| 6077 | ' These functions are only called if the left operand does ' |
| 6078 | 'not\n' |
| 6079 | ' support the corresponding operation and the operands are ' |
| 6080 | 'of\n' |
| 6081 | ' different types. [2] For instance, to evaluate the ' |
| 6082 | 'expression "x -\n' |
| 6083 | ' y", where *y* is an instance of a class that has an ' |
| 6084 | '"__rsub__()"\n' |
| 6085 | ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" ' |
| 6086 | 'returns\n' |
| 6087 | ' *NotImplemented*.\n' |
| 6088 | '\n' |
| 6089 | ' Note that ternary "pow()" will not try calling ' |
| 6090 | '"__rpow__()" (the\n' |
| 6091 | ' coercion rules would become too complicated).\n' |
| 6092 | '\n' |
| 6093 | " Note: If the right operand's type is a subclass of the " |
| 6094 | 'left\n' |
| 6095 | " operand's type and that subclass provides the " |
| 6096 | 'reflected method\n' |
| 6097 | ' for the operation, this method will be called before ' |
| 6098 | 'the left\n' |
| 6099 | " operand's non-reflected method. This behavior allows " |
| 6100 | 'subclasses\n' |
| 6101 | " to override their ancestors' operations.\n" |
| 6102 | '\n' |
| 6103 | 'object.__iadd__(self, other)\n' |
| 6104 | 'object.__isub__(self, other)\n' |
| 6105 | 'object.__imul__(self, other)\n' |
| 6106 | 'object.__idiv__(self, other)\n' |
| 6107 | 'object.__itruediv__(self, other)\n' |
| 6108 | 'object.__ifloordiv__(self, other)\n' |
| 6109 | 'object.__imod__(self, other)\n' |
| 6110 | 'object.__ipow__(self, other[, modulo])\n' |
| 6111 | 'object.__ilshift__(self, other)\n' |
| 6112 | 'object.__irshift__(self, other)\n' |
| 6113 | 'object.__iand__(self, other)\n' |
| 6114 | 'object.__ixor__(self, other)\n' |
| 6115 | 'object.__ior__(self, other)\n' |
| 6116 | '\n' |
| 6117 | ' These methods are called to implement the augmented ' |
| 6118 | 'arithmetic\n' |
| 6119 | ' assignments ("+=", "-=", "*=", "/=", "//=", "%=", "**=", ' |
| 6120 | '"<<=",\n' |
| 6121 | ' ">>=", "&=", "^=", "|="). These methods should attempt ' |
| 6122 | 'to do the\n' |
| 6123 | ' operation in-place (modifying *self*) and return the ' |
| 6124 | 'result (which\n' |
| 6125 | ' could be, but does not have to be, *self*). If a ' |
| 6126 | 'specific method\n' |
| 6127 | ' is not defined, the augmented assignment falls back to ' |
| 6128 | 'the normal\n' |
| 6129 | ' methods. For instance, to execute the statement "x += ' |
| 6130 | 'y", where\n' |
| 6131 | ' *x* is an instance of a class that has an "__iadd__()" ' |
| 6132 | 'method,\n' |
| 6133 | ' "x.__iadd__(y)" is called. If *x* is an instance of a ' |
| 6134 | 'class that\n' |
| 6135 | ' does not define a "__iadd__()" method, "x.__add__(y)" ' |
| 6136 | 'and\n' |
| 6137 | ' "y.__radd__(x)" are considered, as with the evaluation ' |
| 6138 | 'of "x + y".\n' |
| 6139 | '\n' |
| 6140 | 'object.__neg__(self)\n' |
| 6141 | 'object.__pos__(self)\n' |
| 6142 | 'object.__abs__(self)\n' |
| 6143 | 'object.__invert__(self)\n' |
| 6144 | '\n' |
| 6145 | ' Called to implement the unary arithmetic operations ' |
| 6146 | '("-", "+",\n' |
| 6147 | ' "abs()" and "~").\n' |
| 6148 | '\n' |
| 6149 | 'object.__complex__(self)\n' |
| 6150 | 'object.__int__(self)\n' |
| 6151 | 'object.__long__(self)\n' |
| 6152 | 'object.__float__(self)\n' |
| 6153 | '\n' |
| 6154 | ' Called to implement the built-in functions "complex()", ' |
| 6155 | '"int()",\n' |
| 6156 | ' "long()", and "float()". Should return a value of the ' |
| 6157 | 'appropriate\n' |
| 6158 | ' type.\n' |
| 6159 | '\n' |
| 6160 | 'object.__oct__(self)\n' |
| 6161 | 'object.__hex__(self)\n' |
| 6162 | '\n' |
| 6163 | ' Called to implement the built-in functions "oct()" and ' |
| 6164 | '"hex()".\n' |
| 6165 | ' Should return a string value.\n' |
| 6166 | '\n' |
| 6167 | 'object.__index__(self)\n' |
| 6168 | '\n' |
| 6169 | ' Called to implement "operator.index()". Also called ' |
| 6170 | 'whenever\n' |
| 6171 | ' Python needs an integer object (such as in slicing). ' |
| 6172 | 'Must return\n' |
| 6173 | ' an integer (int or long).\n' |
| 6174 | '\n' |
| 6175 | ' New in version 2.5.\n' |
| 6176 | '\n' |
| 6177 | 'object.__coerce__(self, other)\n' |
| 6178 | '\n' |
| 6179 | ' Called to implement "mixed-mode" numeric arithmetic. ' |
| 6180 | 'Should either\n' |
| 6181 | ' return a 2-tuple containing *self* and *other* converted ' |
| 6182 | 'to a\n' |
| 6183 | ' common numeric type, or "None" if conversion is ' |
| 6184 | 'impossible. When\n' |
| 6185 | ' the common type would be the type of "other", it is ' |
| 6186 | 'sufficient to\n' |
| 6187 | ' return "None", since the interpreter will also ask the ' |
| 6188 | 'other object\n' |
| 6189 | ' to attempt a coercion (but sometimes, if the ' |
| 6190 | 'implementation of the\n' |
| 6191 | ' other type cannot be changed, it is useful to do the ' |
| 6192 | 'conversion to\n' |
| 6193 | ' the other type here). A return value of ' |
| 6194 | '"NotImplemented" is\n' |
| 6195 | ' equivalent to returning "None".\n', |
| 6196 | 'objects': '\n' |
| 6197 | 'Objects, values and types\n' |
| 6198 | '*************************\n' |
| 6199 | '\n' |
| 6200 | "*Objects* are Python's abstraction for data. All data in a " |
| 6201 | 'Python\n' |
| 6202 | 'program is represented by objects or by relations between ' |
| 6203 | 'objects. (In\n' |
| 6204 | 'a sense, and in conformance to Von Neumann\'s model of a "stored\n' |
| 6205 | 'program computer," code is also represented by objects.)\n' |
| 6206 | '\n' |
| 6207 | "Every object has an identity, a type and a value. An object's\n" |
| 6208 | '*identity* never changes once it has been created; you may think ' |
| 6209 | 'of it\n' |
| 6210 | 'as the object\'s address in memory. The \'"is"\' operator ' |
| 6211 | 'compares the\n' |
| 6212 | 'identity of two objects; the "id()" function returns an integer\n' |
| 6213 | 'representing its identity (currently implemented as its address). ' |
| 6214 | 'An\n' |
| 6215 | "object's *type* is also unchangeable. [1] An object's type " |
| 6216 | 'determines\n' |
| 6217 | 'the operations that the object supports (e.g., "does it have a\n' |
| 6218 | 'length?") and also defines the possible values for objects of ' |
| 6219 | 'that\n' |
| 6220 | 'type. The "type()" function returns an object\'s type (which is ' |
| 6221 | 'an\n' |
| 6222 | 'object itself). The *value* of some objects can change. ' |
| 6223 | 'Objects\n' |
| 6224 | 'whose value can change are said to be *mutable*; objects whose ' |
| 6225 | 'value\n' |
| 6226 | 'is unchangeable once they are created are called *immutable*. ' |
| 6227 | '(The\n' |
| 6228 | 'value of an immutable container object that contains a reference ' |
| 6229 | 'to a\n' |
| 6230 | "mutable object can change when the latter's value is changed; " |
| 6231 | 'however\n' |
| 6232 | 'the container is still considered immutable, because the ' |
| 6233 | 'collection of\n' |
| 6234 | 'objects it contains cannot be changed. So, immutability is not\n' |
| 6235 | 'strictly the same as having an unchangeable value, it is more ' |
| 6236 | 'subtle.)\n' |
| 6237 | "An object's mutability is determined by its type; for instance,\n" |
| 6238 | 'numbers, strings and tuples are immutable, while dictionaries ' |
| 6239 | 'and\n' |
| 6240 | 'lists are mutable.\n' |
| 6241 | '\n' |
| 6242 | 'Objects are never explicitly destroyed; however, when they ' |
| 6243 | 'become\n' |
| 6244 | 'unreachable they may be garbage-collected. An implementation is\n' |
| 6245 | 'allowed to postpone garbage collection or omit it altogether --- ' |
| 6246 | 'it is\n' |
| 6247 | 'a matter of implementation quality how garbage collection is\n' |
| 6248 | 'implemented, as long as no objects are collected that are still\n' |
| 6249 | 'reachable.\n' |
| 6250 | '\n' |
| 6251 | '**CPython implementation detail:** CPython currently uses a ' |
| 6252 | 'reference-\n' |
| 6253 | 'counting scheme with (optional) delayed detection of cyclically ' |
| 6254 | 'linked\n' |
| 6255 | 'garbage, which collects most objects as soon as they become\n' |
| 6256 | 'unreachable, but is not guaranteed to collect garbage containing\n' |
| 6257 | 'circular references. See the documentation of the "gc" module ' |
| 6258 | 'for\n' |
| 6259 | 'information on controlling the collection of cyclic garbage. ' |
| 6260 | 'Other\n' |
| 6261 | 'implementations act differently and CPython may change. Do not ' |
| 6262 | 'depend\n' |
| 6263 | 'on immediate finalization of objects when they become unreachable ' |
| 6264 | '(ex:\n' |
| 6265 | 'always close files).\n' |
| 6266 | '\n' |
| 6267 | "Note that the use of the implementation's tracing or debugging\n" |
| 6268 | 'facilities may keep objects alive that would normally be ' |
| 6269 | 'collectable.\n' |
| 6270 | 'Also note that catching an exception with a \'"try"..."except"\'\n' |
| 6271 | 'statement may keep objects alive.\n' |
| 6272 | '\n' |
| 6273 | 'Some objects contain references to "external" resources such as ' |
| 6274 | 'open\n' |
| 6275 | 'files or windows. It is understood that these resources are ' |
| 6276 | 'freed\n' |
| 6277 | 'when the object is garbage-collected, but since garbage ' |
| 6278 | 'collection is\n' |
| 6279 | 'not guaranteed to happen, such objects also provide an explicit ' |
| 6280 | 'way to\n' |
| 6281 | 'release the external resource, usually a "close()" method. ' |
| 6282 | 'Programs\n' |
| 6283 | 'are strongly recommended to explicitly close such objects. The\n' |
| 6284 | '\'"try"..."finally"\' statement provides a convenient way to do ' |
| 6285 | 'this.\n' |
| 6286 | '\n' |
| 6287 | 'Some objects contain references to other objects; these are ' |
| 6288 | 'called\n' |
| 6289 | '*containers*. Examples of containers are tuples, lists and\n' |
| 6290 | "dictionaries. The references are part of a container's value. " |
| 6291 | 'In\n' |
| 6292 | 'most cases, when we talk about the value of a container, we imply ' |
| 6293 | 'the\n' |
| 6294 | 'values, not the identities of the contained objects; however, ' |
| 6295 | 'when we\n' |
| 6296 | 'talk about the mutability of a container, only the identities of ' |
| 6297 | 'the\n' |
| 6298 | 'immediately contained objects are implied. So, if an immutable\n' |
| 6299 | 'container (like a tuple) contains a reference to a mutable ' |
| 6300 | 'object, its\n' |
| 6301 | 'value changes if that mutable object is changed.\n' |
| 6302 | '\n' |
| 6303 | 'Types affect almost all aspects of object behavior. Even the\n' |
| 6304 | 'importance of object identity is affected in some sense: for ' |
| 6305 | 'immutable\n' |
| 6306 | 'types, operations that compute new values may actually return a\n' |
| 6307 | 'reference to any existing object with the same type and value, ' |
| 6308 | 'while\n' |
| 6309 | 'for mutable objects this is not allowed. E.g., after "a = 1; b = ' |
| 6310 | '1",\n' |
| 6311 | '"a" and "b" may or may not refer to the same object with the ' |
| 6312 | 'value\n' |
| 6313 | 'one, depending on the implementation, but after "c = []; d = []", ' |
| 6314 | '"c"\n' |
| 6315 | 'and "d" are guaranteed to refer to two different, unique, newly\n' |
| 6316 | 'created empty lists. (Note that "c = d = []" assigns the same ' |
| 6317 | 'object\n' |
| 6318 | 'to both "c" and "d".)\n', |
| 6319 | 'operator-summary': '\n' |
| 6320 | 'Operator precedence\n' |
| 6321 | '*******************\n' |
| 6322 | '\n' |
| 6323 | 'The following table summarizes the operator precedences ' |
| 6324 | 'in Python,\n' |
| 6325 | 'from lowest precedence (least binding) to highest ' |
| 6326 | 'precedence (most\n' |
| 6327 | 'binding). Operators in the same box have the same ' |
| 6328 | 'precedence. Unless\n' |
| 6329 | 'the syntax is explicitly given, operators are binary. ' |
| 6330 | 'Operators in\n' |
| 6331 | 'the same box group left to right (except for ' |
| 6332 | 'comparisons, including\n' |
| 6333 | 'tests, which all have the same precedence and chain from ' |
| 6334 | 'left to right\n' |
| 6335 | '--- see section Comparisons --- and exponentiation, ' |
| 6336 | 'which groups from\n' |
| 6337 | 'right to left).\n' |
| 6338 | '\n' |
| 6339 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6340 | '| Operator | ' |
| 6341 | 'Description |\n' |
| 6342 | '+=================================================+=======================================+\n' |
| 6343 | '| "lambda" | ' |
| 6344 | 'Lambda expression |\n' |
| 6345 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6346 | '| "if" -- "else" | ' |
| 6347 | 'Conditional expression |\n' |
| 6348 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6349 | '| "or" | ' |
| 6350 | 'Boolean OR |\n' |
| 6351 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6352 | '| "and" | ' |
| 6353 | 'Boolean AND |\n' |
| 6354 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6355 | '| "not" "x" | ' |
| 6356 | 'Boolean NOT |\n' |
| 6357 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6358 | '| "in", "not in", "is", "is not", "<", "<=", ">", | ' |
| 6359 | 'Comparisons, including membership |\n' |
| 6360 | '| ">=", "<>", "!=", "==" | ' |
| 6361 | 'tests and identity tests |\n' |
| 6362 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6363 | '| "|" | ' |
| 6364 | 'Bitwise OR |\n' |
| 6365 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6366 | '| "^" | ' |
| 6367 | 'Bitwise XOR |\n' |
| 6368 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6369 | '| "&" | ' |
| 6370 | 'Bitwise AND |\n' |
| 6371 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6372 | '| "<<", ">>" | ' |
| 6373 | 'Shifts |\n' |
| 6374 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6375 | '| "+", "-" | ' |
| 6376 | 'Addition and subtraction |\n' |
| 6377 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6378 | '| "*", "/", "//", "%" | ' |
| 6379 | 'Multiplication, division, remainder |\n' |
| 6380 | '| | ' |
| 6381 | '[7] |\n' |
| 6382 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6383 | '| "+x", "-x", "~x" | ' |
| 6384 | 'Positive, negative, bitwise NOT |\n' |
| 6385 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6386 | '| "**" | ' |
| 6387 | 'Exponentiation [8] |\n' |
| 6388 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6389 | '| "x[index]", "x[index:index]", | ' |
| 6390 | 'Subscription, slicing, call, |\n' |
| 6391 | '| "x(arguments...)", "x.attribute" | ' |
| 6392 | 'attribute reference |\n' |
| 6393 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6394 | '| "(expressions...)", "[expressions...]", "{key: | ' |
| 6395 | 'Binding or tuple display, list |\n' |
| 6396 | '| value...}", "`expressions...`" | ' |
| 6397 | 'display, dictionary display, string |\n' |
| 6398 | '| | ' |
| 6399 | 'conversion |\n' |
| 6400 | '+-------------------------------------------------+---------------------------------------+\n' |
| 6401 | '\n' |
| 6402 | '-[ Footnotes ]-\n' |
| 6403 | '\n' |
| 6404 | '[1] In Python 2.3 and later releases, a list ' |
| 6405 | 'comprehension "leaks"\n' |
| 6406 | ' the control variables of each "for" it contains into ' |
| 6407 | 'the\n' |
| 6408 | ' containing scope. However, this behavior is ' |
| 6409 | 'deprecated, and\n' |
| 6410 | ' relying on it will not work in Python 3.\n' |
| 6411 | '\n' |
| 6412 | '[2] While "abs(x%y) < abs(y)" is true mathematically, ' |
| 6413 | 'for floats\n' |
| 6414 | ' it may not be true numerically due to roundoff. For ' |
| 6415 | 'example, and\n' |
| 6416 | ' assuming a platform on which a Python float is an ' |
| 6417 | 'IEEE 754 double-\n' |
| 6418 | ' precision number, in order that "-1e-100 % 1e100" ' |
| 6419 | 'have the same\n' |
| 6420 | ' sign as "1e100", the computed result is "-1e-100 + ' |
| 6421 | '1e100", which\n' |
| 6422 | ' is numerically exactly equal to "1e100". The ' |
| 6423 | 'function\n' |
| 6424 | ' "math.fmod()" returns a result whose sign matches ' |
| 6425 | 'the sign of the\n' |
| 6426 | ' first argument instead, and so returns "-1e-100" in ' |
| 6427 | 'this case.\n' |
| 6428 | ' Which approach is more appropriate depends on the ' |
| 6429 | 'application.\n' |
| 6430 | '\n' |
| 6431 | '[3] If x is very close to an exact integer multiple of ' |
| 6432 | "y, it's\n" |
| 6433 | ' possible for "floor(x/y)" to be one larger than ' |
| 6434 | '"(x-x%y)/y" due to\n' |
| 6435 | ' rounding. In such cases, Python returns the latter ' |
| 6436 | 'result, in\n' |
| 6437 | ' order to preserve that "divmod(x,y)[0] * y + x % y" ' |
| 6438 | 'be very close\n' |
| 6439 | ' to "x".\n' |
| 6440 | '\n' |
| 6441 | '[4] The Unicode standard distinguishes between *code ' |
| 6442 | 'points* (e.g.\n' |
| 6443 | ' U+0041) and *abstract characters* (e.g. "LATIN ' |
| 6444 | 'CAPITAL LETTER A").\n' |
| 6445 | ' While most abstract characters in Unicode are only ' |
| 6446 | 'represented\n' |
| 6447 | ' using one code point, there is a number of abstract ' |
| 6448 | 'characters\n' |
| 6449 | ' that can in addition be represented using a sequence ' |
| 6450 | 'of more than\n' |
| 6451 | ' one code point. For example, the abstract character ' |
| 6452 | '"LATIN\n' |
| 6453 | ' CAPITAL LETTER C WITH CEDILLA" can be represented as ' |
| 6454 | 'a single\n' |
| 6455 | ' *precomposed character* at code position U+00C7, or ' |
| 6456 | 'as a sequence\n' |
| 6457 | ' of a *base character* at code position U+0043 (LATIN ' |
| 6458 | 'CAPITAL\n' |
| 6459 | ' LETTER C), followed by a *combining character* at ' |
| 6460 | 'code position\n' |
| 6461 | ' U+0327 (COMBINING CEDILLA).\n' |
| 6462 | '\n' |
| 6463 | ' The comparison operators on unicode strings compare ' |
| 6464 | 'at the level\n' |
| 6465 | ' of Unicode code points. This may be ' |
| 6466 | 'counter-intuitive to humans.\n' |
| 6467 | ' For example, "u"\\u00C7" == u"\\u0043\\u0327"" is ' |
| 6468 | '"False", even\n' |
| 6469 | ' though both strings represent the same abstract ' |
| 6470 | 'character "LATIN\n' |
| 6471 | ' CAPITAL LETTER C WITH CEDILLA".\n' |
| 6472 | '\n' |
| 6473 | ' To compare strings at the level of abstract ' |
| 6474 | 'characters (that is,\n' |
| 6475 | ' in a way intuitive to humans), use ' |
| 6476 | '"unicodedata.normalize()".\n' |
| 6477 | '\n' |
| 6478 | '[5] Earlier versions of Python used lexicographic ' |
| 6479 | 'comparison of\n' |
| 6480 | ' the sorted (key, value) lists, but this was very ' |
| 6481 | 'expensive for the\n' |
| 6482 | ' common case of comparing for equality. An even ' |
| 6483 | 'earlier version of\n' |
| 6484 | ' Python compared dictionaries by identity only, but ' |
| 6485 | 'this caused\n' |
| 6486 | ' surprises because people expected to be able to test ' |
| 6487 | 'a dictionary\n' |
| 6488 | ' for emptiness by comparing it to "{}".\n' |
| 6489 | '\n' |
| 6490 | '[6] Due to automatic garbage-collection, free lists, and ' |
| 6491 | 'the\n' |
| 6492 | ' dynamic nature of descriptors, you may notice ' |
| 6493 | 'seemingly unusual\n' |
| 6494 | ' behaviour in certain uses of the "is" operator, like ' |
| 6495 | 'those\n' |
| 6496 | ' involving comparisons between instance methods, or ' |
| 6497 | 'constants.\n' |
| 6498 | ' Check their documentation for more info.\n' |
| 6499 | '\n' |
| 6500 | '[7] The "%" operator is also used for string formatting; ' |
| 6501 | 'the same\n' |
| 6502 | ' precedence applies.\n' |
| 6503 | '\n' |
| 6504 | '[8] The power operator "**" binds less tightly than an ' |
| 6505 | 'arithmetic\n' |
| 6506 | ' or bitwise unary operator on its right, that is, ' |
| 6507 | '"2**-1" is "0.5".\n', |
| 6508 | 'pass': '\n' |
| 6509 | 'The "pass" statement\n' |
| 6510 | '********************\n' |
| 6511 | '\n' |
| 6512 | ' pass_stmt ::= "pass"\n' |
| 6513 | '\n' |
| 6514 | '"pass" is a null operation --- when it is executed, nothing ' |
| 6515 | 'happens.\n' |
| 6516 | 'It is useful as a placeholder when a statement is required\n' |
| 6517 | 'syntactically, but no code needs to be executed, for example:\n' |
| 6518 | '\n' |
| 6519 | ' def f(arg): pass # a function that does nothing (yet)\n' |
| 6520 | '\n' |
| 6521 | ' class C: pass # a class with no methods (yet)\n', |
| 6522 | 'power': '\n' |
| 6523 | 'The power operator\n' |
| 6524 | '******************\n' |
| 6525 | '\n' |
| 6526 | 'The power operator binds more tightly than unary operators on its\n' |
| 6527 | 'left; it binds less tightly than unary operators on its right. ' |
| 6528 | 'The\n' |
| 6529 | 'syntax is:\n' |
| 6530 | '\n' |
| 6531 | ' power ::= primary ["**" u_expr]\n' |
| 6532 | '\n' |
| 6533 | 'Thus, in an unparenthesized sequence of power and unary operators, ' |
| 6534 | 'the\n' |
| 6535 | 'operators are evaluated from right to left (this does not ' |
| 6536 | 'constrain\n' |
| 6537 | 'the evaluation order for the operands): "-1**2" results in "-1".\n' |
| 6538 | '\n' |
| 6539 | 'The power operator has the same semantics as the built-in "pow()"\n' |
| 6540 | 'function, when called with two arguments: it yields its left ' |
| 6541 | 'argument\n' |
| 6542 | 'raised to the power of its right argument. The numeric arguments ' |
| 6543 | 'are\n' |
| 6544 | 'first converted to a common type. The result type is that of the\n' |
| 6545 | 'arguments after coercion.\n' |
| 6546 | '\n' |
| 6547 | 'With mixed operand types, the coercion rules for binary arithmetic\n' |
| 6548 | 'operators apply. For int and long int operands, the result has the\n' |
| 6549 | 'same type as the operands (after coercion) unless the second ' |
| 6550 | 'argument\n' |
| 6551 | 'is negative; in that case, all arguments are converted to float and ' |
| 6552 | 'a\n' |
| 6553 | 'float result is delivered. For example, "10**2" returns "100", but\n' |
| 6554 | '"10**-2" returns "0.01". (This last feature was added in Python ' |
| 6555 | '2.2.\n' |
| 6556 | 'In Python 2.1 and before, if both arguments were of integer types ' |
| 6557 | 'and\n' |
| 6558 | 'the second argument was negative, an exception was raised).\n' |
| 6559 | '\n' |
| 6560 | 'Raising "0.0" to a negative power results in a ' |
| 6561 | '"ZeroDivisionError".\n' |
| 6562 | 'Raising a negative number to a fractional power results in a\n' |
| 6563 | '"ValueError".\n', |
| 6564 | 'print': '\n' |
| 6565 | 'The "print" statement\n' |
| 6566 | '*********************\n' |
| 6567 | '\n' |
| 6568 | ' print_stmt ::= "print" ([expression ("," expression)* [","]]\n' |
| 6569 | ' | ">>" expression [("," expression)+ [","]])\n' |
| 6570 | '\n' |
| 6571 | '"print" evaluates each expression in turn and writes the resulting\n' |
| 6572 | 'object to standard output (see below). If an object is not a ' |
| 6573 | 'string,\n' |
| 6574 | 'it is first converted to a string using the rules for string\n' |
| 6575 | 'conversions. The (resulting or original) string is then written. ' |
| 6576 | 'A\n' |
| 6577 | 'space is written before each object is (converted and) written, ' |
| 6578 | 'unless\n' |
| 6579 | 'the output system believes it is positioned at the beginning of a\n' |
| 6580 | 'line. This is the case (1) when no characters have yet been ' |
| 6581 | 'written\n' |
| 6582 | 'to standard output, (2) when the last character written to ' |
| 6583 | 'standard\n' |
| 6584 | 'output is a whitespace character except "\' \'", or (3) when the ' |
| 6585 | 'last\n' |
| 6586 | 'write operation on standard output was not a "print" statement. ' |
| 6587 | '(In\n' |
| 6588 | 'some cases it may be functional to write an empty string to ' |
| 6589 | 'standard\n' |
| 6590 | 'output for this reason.)\n' |
| 6591 | '\n' |
| 6592 | 'Note: Objects which act like file objects but which are not the\n' |
| 6593 | ' built-in file objects often do not properly emulate this aspect ' |
| 6594 | 'of\n' |
| 6595 | " the file object's behavior, so it is best not to rely on this.\n" |
| 6596 | '\n' |
| 6597 | 'A "\'\\n\'" character is written at the end, unless the "print" ' |
| 6598 | 'statement\n' |
| 6599 | 'ends with a comma. This is the only action if the statement ' |
| 6600 | 'contains\n' |
| 6601 | 'just the keyword "print".\n' |
| 6602 | '\n' |
| 6603 | 'Standard output is defined as the file object named "stdout" in ' |
| 6604 | 'the\n' |
| 6605 | 'built-in module "sys". If no such object exists, or if it does ' |
| 6606 | 'not\n' |
| 6607 | 'have a "write()" method, a "RuntimeError" exception is raised.\n' |
| 6608 | '\n' |
| 6609 | '"print" also has an extended form, defined by the second portion ' |
| 6610 | 'of\n' |
| 6611 | 'the syntax described above. This form is sometimes referred to as\n' |
| 6612 | '""print" chevron." In this form, the first expression after the ' |
| 6613 | '">>"\n' |
| 6614 | 'must evaluate to a "file-like" object, specifically an object that ' |
| 6615 | 'has\n' |
| 6616 | 'a "write()" method as described above. With this extended form, ' |
| 6617 | 'the\n' |
| 6618 | 'subsequent expressions are printed to this file object. If the ' |
| 6619 | 'first\n' |
| 6620 | 'expression evaluates to "None", then "sys.stdout" is used as the ' |
| 6621 | 'file\n' |
| 6622 | 'for output.\n', |
| 6623 | 'raise': '\n' |
| 6624 | 'The "raise" statement\n' |
| 6625 | '*********************\n' |
| 6626 | '\n' |
| 6627 | ' raise_stmt ::= "raise" [expression ["," expression ["," ' |
| 6628 | 'expression]]]\n' |
| 6629 | '\n' |
| 6630 | 'If no expressions are present, "raise" re-raises the last ' |
| 6631 | 'exception\n' |
| 6632 | 'that was active in the current scope. If no exception is active ' |
| 6633 | 'in\n' |
| 6634 | 'the current scope, a "TypeError" exception is raised indicating ' |
| 6635 | 'that\n' |
| 6636 | 'this is an error (if running under IDLE, a "Queue.Empty" exception ' |
| 6637 | 'is\n' |
| 6638 | 'raised instead).\n' |
| 6639 | '\n' |
| 6640 | 'Otherwise, "raise" evaluates the expressions to get three objects,\n' |
| 6641 | 'using "None" as the value of omitted expressions. The first two\n' |
| 6642 | 'objects are used to determine the *type* and *value* of the ' |
| 6643 | 'exception.\n' |
| 6644 | '\n' |
| 6645 | 'If the first object is an instance, the type of the exception is ' |
| 6646 | 'the\n' |
| 6647 | 'class of the instance, the instance itself is the value, and the\n' |
| 6648 | 'second object must be "None".\n' |
| 6649 | '\n' |
| 6650 | 'If the first object is a class, it becomes the type of the ' |
| 6651 | 'exception.\n' |
| 6652 | 'The second object is used to determine the exception value: If it ' |
| 6653 | 'is\n' |
| 6654 | 'an instance of the class, the instance becomes the exception value. ' |
| 6655 | 'If\n' |
| 6656 | 'the second object is a tuple, it is used as the argument list for ' |
| 6657 | 'the\n' |
| 6658 | 'class constructor; if it is "None", an empty argument list is ' |
| 6659 | 'used,\n' |
| 6660 | 'and any other object is treated as a single argument to the\n' |
| 6661 | 'constructor. The instance so created by calling the constructor ' |
| 6662 | 'is\n' |
| 6663 | 'used as the exception value.\n' |
| 6664 | '\n' |
| 6665 | 'If a third object is present and not "None", it must be a ' |
| 6666 | 'traceback\n' |
| 6667 | 'object (see section The standard type hierarchy), and it is\n' |
| 6668 | 'substituted instead of the current location as the place where the\n' |
| 6669 | 'exception occurred. If the third object is present and not a\n' |
| 6670 | 'traceback object or "None", a "TypeError" exception is raised. ' |
| 6671 | 'The\n' |
| 6672 | 'three-expression form of "raise" is useful to re-raise an ' |
| 6673 | 'exception\n' |
| 6674 | 'transparently in an except clause, but "raise" with no expressions\n' |
| 6675 | 'should be preferred if the exception to be re-raised was the most\n' |
| 6676 | 'recently active exception in the current scope.\n' |
| 6677 | '\n' |
| 6678 | 'Additional information on exceptions can be found in section\n' |
| 6679 | 'Exceptions, and information about handling exceptions is in ' |
| 6680 | 'section\n' |
| 6681 | 'The try statement.\n', |
| 6682 | 'return': '\n' |
| 6683 | 'The "return" statement\n' |
| 6684 | '**********************\n' |
| 6685 | '\n' |
| 6686 | ' return_stmt ::= "return" [expression_list]\n' |
| 6687 | '\n' |
| 6688 | '"return" may only occur syntactically nested in a function ' |
| 6689 | 'definition,\n' |
| 6690 | 'not within a nested class definition.\n' |
| 6691 | '\n' |
| 6692 | 'If an expression list is present, it is evaluated, else "None" is\n' |
| 6693 | 'substituted.\n' |
| 6694 | '\n' |
| 6695 | '"return" leaves the current function call with the expression list ' |
| 6696 | '(or\n' |
| 6697 | '"None") as return value.\n' |
| 6698 | '\n' |
| 6699 | 'When "return" passes control out of a "try" statement with a ' |
| 6700 | '"finally"\n' |
| 6701 | 'clause, that "finally" clause is executed before really leaving ' |
| 6702 | 'the\n' |
| 6703 | 'function.\n' |
| 6704 | '\n' |
| 6705 | 'In a generator function, the "return" statement is not allowed to\n' |
| 6706 | 'include an "expression_list". In that context, a bare "return"\n' |
| 6707 | 'indicates that the generator is done and will cause ' |
| 6708 | '"StopIteration" to\n' |
| 6709 | 'be raised.\n', |
| 6710 | 'sequence-types': '\n' |
| 6711 | 'Emulating container types\n' |
| 6712 | '*************************\n' |
| 6713 | '\n' |
| 6714 | 'The following methods can be defined to implement ' |
| 6715 | 'container objects.\n' |
| 6716 | 'Containers usually are sequences (such as lists or tuples) ' |
| 6717 | 'or mappings\n' |
| 6718 | '(like dictionaries), but can represent other containers as ' |
| 6719 | 'well. The\n' |
| 6720 | 'first set of methods is used either to emulate a sequence ' |
| 6721 | 'or to\n' |
| 6722 | 'emulate a mapping; the difference is that for a sequence, ' |
| 6723 | 'the\n' |
| 6724 | 'allowable keys should be the integers *k* for which "0 <= ' |
| 6725 | 'k < N" where\n' |
| 6726 | '*N* is the length of the sequence, or slice objects, which ' |
| 6727 | 'define a\n' |
| 6728 | 'range of items. (For backwards compatibility, the method\n' |
| 6729 | '"__getslice__()" (see below) can also be defined to handle ' |
| 6730 | 'simple, but\n' |
| 6731 | 'not extended slices.) It is also recommended that mappings ' |
| 6732 | 'provide the\n' |
| 6733 | 'methods "keys()", "values()", "items()", "has_key()", ' |
| 6734 | '"get()",\n' |
| 6735 | '"clear()", "setdefault()", "iterkeys()", "itervalues()",\n' |
| 6736 | '"iteritems()", "pop()", "popitem()", "copy()", and ' |
| 6737 | '"update()" behaving\n' |
| 6738 | "similar to those for Python's standard dictionary " |
| 6739 | 'objects. The\n' |
| 6740 | '"UserDict" module provides a "DictMixin" class to help ' |
| 6741 | 'create those\n' |
| 6742 | 'methods from a base set of "__getitem__()", ' |
| 6743 | '"__setitem__()",\n' |
| 6744 | '"__delitem__()", and "keys()". Mutable sequences should ' |
| 6745 | 'provide\n' |
| 6746 | 'methods "append()", "count()", "index()", "extend()", ' |
| 6747 | '"insert()",\n' |
| 6748 | '"pop()", "remove()", "reverse()" and "sort()", like Python ' |
| 6749 | 'standard\n' |
| 6750 | 'list objects. Finally, sequence types should implement ' |
| 6751 | 'addition\n' |
| 6752 | '(meaning concatenation) and multiplication (meaning ' |
| 6753 | 'repetition) by\n' |
| 6754 | 'defining the methods "__add__()", "__radd__()", ' |
| 6755 | '"__iadd__()",\n' |
| 6756 | '"__mul__()", "__rmul__()" and "__imul__()" described ' |
| 6757 | 'below; they\n' |
| 6758 | 'should not define "__coerce__()" or other numerical ' |
| 6759 | 'operators. It is\n' |
| 6760 | 'recommended that both mappings and sequences implement ' |
| 6761 | 'the\n' |
| 6762 | '"__contains__()" method to allow efficient use of the "in" ' |
| 6763 | 'operator;\n' |
| 6764 | 'for mappings, "in" should be equivalent of "has_key()"; ' |
| 6765 | 'for sequences,\n' |
| 6766 | 'it should search through the values. It is further ' |
| 6767 | 'recommended that\n' |
| 6768 | 'both mappings and sequences implement the "__iter__()" ' |
| 6769 | 'method to allow\n' |
| 6770 | 'efficient iteration through the container; for mappings, ' |
| 6771 | '"__iter__()"\n' |
| 6772 | 'should be the same as "iterkeys()"; for sequences, it ' |
| 6773 | 'should iterate\n' |
| 6774 | 'through the values.\n' |
| 6775 | '\n' |
| 6776 | 'object.__len__(self)\n' |
| 6777 | '\n' |
| 6778 | ' Called to implement the built-in function "len()". ' |
| 6779 | 'Should return\n' |
| 6780 | ' the length of the object, an integer ">=" 0. Also, an ' |
| 6781 | 'object that\n' |
| 6782 | ' doesn\'t define a "__nonzero__()" method and whose ' |
| 6783 | '"__len__()"\n' |
| 6784 | ' method returns zero is considered to be false in a ' |
| 6785 | 'Boolean context.\n' |
| 6786 | '\n' |
| 6787 | ' **CPython implementation detail:** In CPython, the ' |
| 6788 | 'length is\n' |
| 6789 | ' required to be at most "sys.maxsize". If the length is ' |
| 6790 | 'larger than\n' |
| 6791 | ' "sys.maxsize" some features (such as "len()") may ' |
| 6792 | 'raise\n' |
| 6793 | ' "OverflowError". To prevent raising "OverflowError" by ' |
| 6794 | 'truth value\n' |
| 6795 | ' testing, an object must define a "__nonzero__()" ' |
| 6796 | 'method.\n' |
| 6797 | '\n' |
| 6798 | 'object.__getitem__(self, key)\n' |
| 6799 | '\n' |
| 6800 | ' Called to implement evaluation of "self[key]". For ' |
| 6801 | 'sequence types,\n' |
| 6802 | ' the accepted keys should be integers and slice ' |
| 6803 | 'objects. Note that\n' |
| 6804 | ' the special interpretation of negative indexes (if the ' |
| 6805 | 'class wishes\n' |
| 6806 | ' to emulate a sequence type) is up to the ' |
| 6807 | '"__getitem__()" method. If\n' |
| 6808 | ' *key* is of an inappropriate type, "TypeError" may be ' |
| 6809 | 'raised; if of\n' |
| 6810 | ' a value outside the set of indexes for the sequence ' |
| 6811 | '(after any\n' |
| 6812 | ' special interpretation of negative values), ' |
| 6813 | '"IndexError" should be\n' |
| 6814 | ' raised. For mapping types, if *key* is missing (not in ' |
| 6815 | 'the\n' |
| 6816 | ' container), "KeyError" should be raised.\n' |
| 6817 | '\n' |
| 6818 | ' Note: "for" loops expect that an "IndexError" will be ' |
| 6819 | 'raised for\n' |
| 6820 | ' illegal indexes to allow proper detection of the end ' |
| 6821 | 'of the\n' |
| 6822 | ' sequence.\n' |
| 6823 | '\n' |
| 6824 | 'object.__missing__(self, key)\n' |
| 6825 | '\n' |
| 6826 | ' Called by "dict"."__getitem__()" to implement ' |
| 6827 | '"self[key]" for dict\n' |
| 6828 | ' subclasses when key is not in the dictionary.\n' |
| 6829 | '\n' |
| 6830 | 'object.__setitem__(self, key, value)\n' |
| 6831 | '\n' |
| 6832 | ' Called to implement assignment to "self[key]". Same ' |
| 6833 | 'note as for\n' |
| 6834 | ' "__getitem__()". This should only be implemented for ' |
| 6835 | 'mappings if\n' |
| 6836 | ' the objects support changes to the values for keys, or ' |
| 6837 | 'if new keys\n' |
| 6838 | ' can be added, or for sequences if elements can be ' |
| 6839 | 'replaced. The\n' |
| 6840 | ' same exceptions should be raised for improper *key* ' |
| 6841 | 'values as for\n' |
| 6842 | ' the "__getitem__()" method.\n' |
| 6843 | '\n' |
| 6844 | 'object.__delitem__(self, key)\n' |
| 6845 | '\n' |
| 6846 | ' Called to implement deletion of "self[key]". Same note ' |
| 6847 | 'as for\n' |
| 6848 | ' "__getitem__()". This should only be implemented for ' |
| 6849 | 'mappings if\n' |
| 6850 | ' the objects support removal of keys, or for sequences ' |
| 6851 | 'if elements\n' |
| 6852 | ' can be removed from the sequence. The same exceptions ' |
| 6853 | 'should be\n' |
| 6854 | ' raised for improper *key* values as for the ' |
| 6855 | '"__getitem__()" method.\n' |
| 6856 | '\n' |
| 6857 | 'object.__iter__(self)\n' |
| 6858 | '\n' |
| 6859 | ' This method is called when an iterator is required for ' |
| 6860 | 'a container.\n' |
| 6861 | ' This method should return a new iterator object that ' |
| 6862 | 'can iterate\n' |
| 6863 | ' over all the objects in the container. For mappings, ' |
| 6864 | 'it should\n' |
| 6865 | ' iterate over the keys of the container, and should also ' |
| 6866 | 'be made\n' |
| 6867 | ' available as the method "iterkeys()".\n' |
| 6868 | '\n' |
| 6869 | ' Iterator objects also need to implement this method; ' |
| 6870 | 'they are\n' |
| 6871 | ' required to return themselves. For more information on ' |
| 6872 | 'iterator\n' |
| 6873 | ' objects, see Iterator Types.\n' |
| 6874 | '\n' |
| 6875 | 'object.__reversed__(self)\n' |
| 6876 | '\n' |
| 6877 | ' Called (if present) by the "reversed()" built-in to ' |
| 6878 | 'implement\n' |
| 6879 | ' reverse iteration. It should return a new iterator ' |
| 6880 | 'object that\n' |
| 6881 | ' iterates over all the objects in the container in ' |
| 6882 | 'reverse order.\n' |
| 6883 | '\n' |
| 6884 | ' If the "__reversed__()" method is not provided, the ' |
| 6885 | '"reversed()"\n' |
| 6886 | ' built-in will fall back to using the sequence protocol ' |
| 6887 | '("__len__()"\n' |
| 6888 | ' and "__getitem__()"). Objects that support the ' |
| 6889 | 'sequence protocol\n' |
| 6890 | ' should only provide "__reversed__()" if they can ' |
| 6891 | 'provide an\n' |
| 6892 | ' implementation that is more efficient than the one ' |
| 6893 | 'provided by\n' |
| 6894 | ' "reversed()".\n' |
| 6895 | '\n' |
| 6896 | ' New in version 2.6.\n' |
| 6897 | '\n' |
| 6898 | 'The membership test operators ("in" and "not in") are ' |
| 6899 | 'normally\n' |
| 6900 | 'implemented as an iteration through a sequence. However, ' |
| 6901 | 'container\n' |
| 6902 | 'objects can supply the following special method with a ' |
| 6903 | 'more efficient\n' |
| 6904 | 'implementation, which also does not require the object be ' |
| 6905 | 'a sequence.\n' |
| 6906 | '\n' |
| 6907 | 'object.__contains__(self, item)\n' |
| 6908 | '\n' |
| 6909 | ' Called to implement membership test operators. Should ' |
| 6910 | 'return true\n' |
| 6911 | ' if *item* is in *self*, false otherwise. For mapping ' |
| 6912 | 'objects, this\n' |
| 6913 | ' should consider the keys of the mapping rather than the ' |
| 6914 | 'values or\n' |
| 6915 | ' the key-item pairs.\n' |
| 6916 | '\n' |
| 6917 | ' For objects that don\'t define "__contains__()", the ' |
| 6918 | 'membership test\n' |
| 6919 | ' first tries iteration via "__iter__()", then the old ' |
| 6920 | 'sequence\n' |
| 6921 | ' iteration protocol via "__getitem__()", see this ' |
| 6922 | 'section in the\n' |
| 6923 | ' language reference.\n', |
| 6924 | 'shifting': '\n' |
| 6925 | 'Shifting operations\n' |
| 6926 | '*******************\n' |
| 6927 | '\n' |
| 6928 | 'The shifting operations have lower priority than the arithmetic\n' |
| 6929 | 'operations:\n' |
| 6930 | '\n' |
| 6931 | ' shift_expr ::= a_expr | shift_expr ( "<<" | ">>" ) a_expr\n' |
| 6932 | '\n' |
| 6933 | 'These operators accept plain or long integers as arguments. ' |
| 6934 | 'The\n' |
| 6935 | 'arguments are converted to a common type. They shift the first\n' |
| 6936 | 'argument to the left or right by the number of bits given by ' |
| 6937 | 'the\n' |
| 6938 | 'second argument.\n' |
| 6939 | '\n' |
| 6940 | 'A right shift by *n* bits is defined as division by "pow(2, ' |
| 6941 | 'n)". A\n' |
| 6942 | 'left shift by *n* bits is defined as multiplication with "pow(2, ' |
| 6943 | 'n)".\n' |
| 6944 | 'Negative shift counts raise a "ValueError" exception.\n' |
| 6945 | '\n' |
| 6946 | 'Note: In the current implementation, the right-hand operand is\n' |
| 6947 | ' required to be at most "sys.maxsize". If the right-hand ' |
| 6948 | 'operand is\n' |
| 6949 | ' larger than "sys.maxsize" an "OverflowError" exception is ' |
| 6950 | 'raised.\n', |
| 6951 | 'slicings': '\n' |
| 6952 | 'Slicings\n' |
| 6953 | '********\n' |
| 6954 | '\n' |
| 6955 | 'A slicing selects a range of items in a sequence object (e.g., ' |
| 6956 | 'a\n' |
| 6957 | 'string, tuple or list). Slicings may be used as expressions or ' |
| 6958 | 'as\n' |
| 6959 | 'targets in assignment or "del" statements. The syntax for a ' |
| 6960 | 'slicing:\n' |
| 6961 | '\n' |
| 6962 | ' slicing ::= simple_slicing | extended_slicing\n' |
| 6963 | ' simple_slicing ::= primary "[" short_slice "]"\n' |
| 6964 | ' extended_slicing ::= primary "[" slice_list "]"\n' |
| 6965 | ' slice_list ::= slice_item ("," slice_item)* [","]\n' |
| 6966 | ' slice_item ::= expression | proper_slice | ellipsis\n' |
| 6967 | ' proper_slice ::= short_slice | long_slice\n' |
| 6968 | ' short_slice ::= [lower_bound] ":" [upper_bound]\n' |
| 6969 | ' long_slice ::= short_slice ":" [stride]\n' |
| 6970 | ' lower_bound ::= expression\n' |
| 6971 | ' upper_bound ::= expression\n' |
| 6972 | ' stride ::= expression\n' |
| 6973 | ' ellipsis ::= "..."\n' |
| 6974 | '\n' |
| 6975 | 'There is ambiguity in the formal syntax here: anything that ' |
| 6976 | 'looks like\n' |
| 6977 | 'an expression list also looks like a slice list, so any ' |
| 6978 | 'subscription\n' |
| 6979 | 'can be interpreted as a slicing. Rather than further ' |
| 6980 | 'complicating the\n' |
| 6981 | 'syntax, this is disambiguated by defining that in this case the\n' |
| 6982 | 'interpretation as a subscription takes priority over the\n' |
| 6983 | 'interpretation as a slicing (this is the case if the slice list\n' |
| 6984 | 'contains no proper slice nor ellipses). Similarly, when the ' |
| 6985 | 'slice\n' |
| 6986 | 'list has exactly one short slice and no trailing comma, the\n' |
| 6987 | 'interpretation as a simple slicing takes priority over that as ' |
| 6988 | 'an\n' |
| 6989 | 'extended slicing.\n' |
| 6990 | '\n' |
| 6991 | 'The semantics for a simple slicing are as follows. The primary ' |
| 6992 | 'must\n' |
| 6993 | 'evaluate to a sequence object. The lower and upper bound ' |
| 6994 | 'expressions,\n' |
| 6995 | 'if present, must evaluate to plain integers; defaults are zero ' |
| 6996 | 'and the\n' |
| 6997 | '"sys.maxint", respectively. If either bound is negative, the\n' |
| 6998 | "sequence's length is added to it. The slicing now selects all " |
| 6999 | 'items\n' |
| 7000 | 'with index *k* such that "i <= k < j" where *i* and *j* are the\n' |
| 7001 | 'specified lower and upper bounds. This may be an empty ' |
| 7002 | 'sequence. It\n' |
| 7003 | 'is not an error if *i* or *j* lie outside the range of valid ' |
| 7004 | 'indexes\n' |
| 7005 | "(such items don't exist so they aren't selected).\n" |
| 7006 | '\n' |
| 7007 | 'The semantics for an extended slicing are as follows. The ' |
| 7008 | 'primary\n' |
| 7009 | 'must evaluate to a mapping object, and it is indexed with a key ' |
| 7010 | 'that\n' |
| 7011 | 'is constructed from the slice list, as follows. If the slice ' |
| 7012 | 'list\n' |
| 7013 | 'contains at least one comma, the key is a tuple containing the\n' |
| 7014 | 'conversion of the slice items; otherwise, the conversion of the ' |
| 7015 | 'lone\n' |
| 7016 | 'slice item is the key. The conversion of a slice item that is ' |
| 7017 | 'an\n' |
| 7018 | 'expression is that expression. The conversion of an ellipsis ' |
| 7019 | 'slice\n' |
| 7020 | 'item is the built-in "Ellipsis" object. The conversion of a ' |
| 7021 | 'proper\n' |
| 7022 | 'slice is a slice object (see section The standard type ' |
| 7023 | 'hierarchy)\n' |
| 7024 | 'whose "start", "stop" and "step" attributes are the values of ' |
| 7025 | 'the\n' |
| 7026 | 'expressions given as lower bound, upper bound and stride,\n' |
| 7027 | 'respectively, substituting "None" for missing expressions.\n', |
| 7028 | 'specialattrs': '\n' |
| 7029 | 'Special Attributes\n' |
| 7030 | '******************\n' |
| 7031 | '\n' |
| 7032 | 'The implementation adds a few special read-only attributes ' |
| 7033 | 'to several\n' |
| 7034 | 'object types, where they are relevant. Some of these are ' |
| 7035 | 'not reported\n' |
| 7036 | 'by the "dir()" built-in function.\n' |
| 7037 | '\n' |
| 7038 | 'object.__dict__\n' |
| 7039 | '\n' |
| 7040 | ' A dictionary or other mapping object used to store an ' |
| 7041 | "object's\n" |
| 7042 | ' (writable) attributes.\n' |
| 7043 | '\n' |
| 7044 | 'object.__methods__\n' |
| 7045 | '\n' |
| 7046 | ' Deprecated since version 2.2: Use the built-in function ' |
| 7047 | '"dir()" to\n' |
| 7048 | " get a list of an object's attributes. This attribute is " |
| 7049 | 'no longer\n' |
| 7050 | ' available.\n' |
| 7051 | '\n' |
| 7052 | 'object.__members__\n' |
| 7053 | '\n' |
| 7054 | ' Deprecated since version 2.2: Use the built-in function ' |
| 7055 | '"dir()" to\n' |
| 7056 | " get a list of an object's attributes. This attribute is " |
| 7057 | 'no longer\n' |
| 7058 | ' available.\n' |
| 7059 | '\n' |
| 7060 | 'instance.__class__\n' |
| 7061 | '\n' |
| 7062 | ' The class to which a class instance belongs.\n' |
| 7063 | '\n' |
| 7064 | 'class.__bases__\n' |
| 7065 | '\n' |
| 7066 | ' The tuple of base classes of a class object.\n' |
| 7067 | '\n' |
| 7068 | 'definition.__name__\n' |
| 7069 | '\n' |
| 7070 | ' The name of the class, type, function, method, ' |
| 7071 | 'descriptor, or\n' |
| 7072 | ' generator instance.\n' |
| 7073 | '\n' |
| 7074 | 'The following attributes are only supported by *new-style ' |
| 7075 | 'class*es.\n' |
| 7076 | '\n' |
| 7077 | 'class.__mro__\n' |
| 7078 | '\n' |
| 7079 | ' This attribute is a tuple of classes that are considered ' |
| 7080 | 'when\n' |
| 7081 | ' looking for base classes during method resolution.\n' |
| 7082 | '\n' |
| 7083 | 'class.mro()\n' |
| 7084 | '\n' |
| 7085 | ' This method can be overridden by a metaclass to customize ' |
| 7086 | 'the\n' |
| 7087 | ' method resolution order for its instances. It is called ' |
| 7088 | 'at class\n' |
| 7089 | ' instantiation, and its result is stored in "__mro__".\n' |
| 7090 | '\n' |
| 7091 | 'class.__subclasses__()\n' |
| 7092 | '\n' |
| 7093 | ' Each new-style class keeps a list of weak references to ' |
| 7094 | 'its\n' |
| 7095 | ' immediate subclasses. This method returns a list of all ' |
| 7096 | 'those\n' |
| 7097 | ' references still alive. Example:\n' |
| 7098 | '\n' |
| 7099 | ' >>> int.__subclasses__()\n' |
| 7100 | " [<type 'bool'>]\n" |
| 7101 | '\n' |
| 7102 | '-[ Footnotes ]-\n' |
| 7103 | '\n' |
| 7104 | '[1] Additional information on these special methods may be ' |
| 7105 | 'found\n' |
| 7106 | ' in the Python Reference Manual (Basic customization).\n' |
| 7107 | '\n' |
| 7108 | '[2] As a consequence, the list "[1, 2]" is considered equal ' |
| 7109 | 'to\n' |
| 7110 | ' "[1.0, 2.0]", and similarly for tuples.\n' |
| 7111 | '\n' |
| 7112 | "[3] They must have since the parser can't tell the type of " |
| 7113 | 'the\n' |
| 7114 | ' operands.\n' |
| 7115 | '\n' |
| 7116 | '[4] Cased characters are those with general category ' |
| 7117 | 'property\n' |
| 7118 | ' being one of "Lu" (Letter, uppercase), "Ll" (Letter, ' |
| 7119 | 'lowercase),\n' |
| 7120 | ' or "Lt" (Letter, titlecase).\n' |
| 7121 | '\n' |
| 7122 | '[5] To format only a tuple you should therefore provide a\n' |
| 7123 | ' singleton tuple whose only element is the tuple to be ' |
| 7124 | 'formatted.\n' |
| 7125 | '\n' |
| 7126 | '[6] The advantage of leaving the newline on is that ' |
| 7127 | 'returning an\n' |
| 7128 | ' empty string is then an unambiguous EOF indication. It ' |
| 7129 | 'is also\n' |
| 7130 | ' possible (in cases where it might matter, for example, ' |
| 7131 | 'if you want\n' |
| 7132 | ' to make an exact copy of a file while scanning its ' |
| 7133 | 'lines) to tell\n' |
| 7134 | ' whether the last line of a file ended in a newline or ' |
| 7135 | 'not (yes\n' |
| 7136 | ' this happens!).\n', |
| 7137 | 'specialnames': '\n' |
| 7138 | 'Special method names\n' |
| 7139 | '********************\n' |
| 7140 | '\n' |
| 7141 | 'A class can implement certain operations that are invoked by ' |
| 7142 | 'special\n' |
| 7143 | 'syntax (such as arithmetic operations or subscripting and ' |
| 7144 | 'slicing) by\n' |
| 7145 | "defining methods with special names. This is Python's " |
| 7146 | 'approach to\n' |
| 7147 | '*operator overloading*, allowing classes to define their own ' |
| 7148 | 'behavior\n' |
| 7149 | 'with respect to language operators. For instance, if a ' |
| 7150 | 'class defines\n' |
| 7151 | 'a method named "__getitem__()", and "x" is an instance of ' |
| 7152 | 'this class,\n' |
| 7153 | 'then "x[i]" is roughly equivalent to "x.__getitem__(i)" for ' |
| 7154 | 'old-style\n' |
| 7155 | 'classes and "type(x).__getitem__(x, i)" for new-style ' |
| 7156 | 'classes. Except\n' |
| 7157 | 'where mentioned, attempts to execute an operation raise an ' |
| 7158 | 'exception\n' |
| 7159 | 'when no appropriate method is defined (typically ' |
| 7160 | '"AttributeError" or\n' |
| 7161 | '"TypeError").\n' |
| 7162 | '\n' |
| 7163 | 'When implementing a class that emulates any built-in type, ' |
| 7164 | 'it is\n' |
| 7165 | 'important that the emulation only be implemented to the ' |
| 7166 | 'degree that it\n' |
| 7167 | 'makes sense for the object being modelled. For example, ' |
| 7168 | 'some\n' |
| 7169 | 'sequences may work well with retrieval of individual ' |
| 7170 | 'elements, but\n' |
| 7171 | 'extracting a slice may not make sense. (One example of this ' |
| 7172 | 'is the\n' |
| 7173 | '"NodeList" interface in the W3C\'s Document Object Model.)\n' |
| 7174 | '\n' |
| 7175 | '\n' |
| 7176 | 'Basic customization\n' |
| 7177 | '===================\n' |
| 7178 | '\n' |
| 7179 | 'object.__new__(cls[, ...])\n' |
| 7180 | '\n' |
| 7181 | ' Called to create a new instance of class *cls*. ' |
| 7182 | '"__new__()" is a\n' |
| 7183 | ' static method (special-cased so you need not declare it ' |
| 7184 | 'as such)\n' |
| 7185 | ' that takes the class of which an instance was requested ' |
| 7186 | 'as its\n' |
| 7187 | ' first argument. The remaining arguments are those passed ' |
| 7188 | 'to the\n' |
| 7189 | ' object constructor expression (the call to the class). ' |
| 7190 | 'The return\n' |
| 7191 | ' value of "__new__()" should be the new object instance ' |
| 7192 | '(usually an\n' |
| 7193 | ' instance of *cls*).\n' |
| 7194 | '\n' |
| 7195 | ' Typical implementations create a new instance of the ' |
| 7196 | 'class by\n' |
| 7197 | ' invoking the superclass\'s "__new__()" method using\n' |
| 7198 | ' "super(currentclass, cls).__new__(cls[, ...])" with ' |
| 7199 | 'appropriate\n' |
| 7200 | ' arguments and then modifying the newly-created instance ' |
| 7201 | 'as\n' |
| 7202 | ' necessary before returning it.\n' |
| 7203 | '\n' |
| 7204 | ' If "__new__()" returns an instance of *cls*, then the ' |
| 7205 | 'new\n' |
| 7206 | ' instance\'s "__init__()" method will be invoked like\n' |
| 7207 | ' "__init__(self[, ...])", where *self* is the new instance ' |
| 7208 | 'and the\n' |
| 7209 | ' remaining arguments are the same as were passed to ' |
| 7210 | '"__new__()".\n' |
| 7211 | '\n' |
| 7212 | ' If "__new__()" does not return an instance of *cls*, then ' |
| 7213 | 'the new\n' |
| 7214 | ' instance\'s "__init__()" method will not be invoked.\n' |
| 7215 | '\n' |
| 7216 | ' "__new__()" is intended mainly to allow subclasses of ' |
| 7217 | 'immutable\n' |
| 7218 | ' types (like int, str, or tuple) to customize instance ' |
| 7219 | 'creation. It\n' |
| 7220 | ' is also commonly overridden in custom metaclasses in ' |
| 7221 | 'order to\n' |
| 7222 | ' customize class creation.\n' |
| 7223 | '\n' |
| 7224 | 'object.__init__(self[, ...])\n' |
| 7225 | '\n' |
| 7226 | ' Called after the instance has been created (by ' |
| 7227 | '"__new__()"), but\n' |
| 7228 | ' before it is returned to the caller. The arguments are ' |
| 7229 | 'those\n' |
| 7230 | ' passed to the class constructor expression. If a base ' |
| 7231 | 'class has an\n' |
| 7232 | ' "__init__()" method, the derived class\'s "__init__()" ' |
| 7233 | 'method, if\n' |
| 7234 | ' any, must explicitly call it to ensure proper ' |
| 7235 | 'initialization of the\n' |
| 7236 | ' base class part of the instance; for example:\n' |
| 7237 | ' "BaseClass.__init__(self, [args...])".\n' |
| 7238 | '\n' |
| 7239 | ' Because "__new__()" and "__init__()" work together in ' |
| 7240 | 'constructing\n' |
| 7241 | ' objects ("__new__()" to create it, and "__init__()" to ' |
| 7242 | 'customise\n' |
| 7243 | ' it), no non-"None" value may be returned by "__init__()"; ' |
| 7244 | 'doing so\n' |
| 7245 | ' will cause a "TypeError" to be raised at runtime.\n' |
| 7246 | '\n' |
| 7247 | 'object.__del__(self)\n' |
| 7248 | '\n' |
| 7249 | ' Called when the instance is about to be destroyed. This ' |
| 7250 | 'is also\n' |
| 7251 | ' called a destructor. If a base class has a "__del__()" ' |
| 7252 | 'method, the\n' |
| 7253 | ' derived class\'s "__del__()" method, if any, must ' |
| 7254 | 'explicitly call it\n' |
| 7255 | ' to ensure proper deletion of the base class part of the ' |
| 7256 | 'instance.\n' |
| 7257 | ' Note that it is possible (though not recommended!) for ' |
| 7258 | 'the\n' |
| 7259 | ' "__del__()" method to postpone destruction of the ' |
| 7260 | 'instance by\n' |
| 7261 | ' creating a new reference to it. It may then be called at ' |
| 7262 | 'a later\n' |
| 7263 | ' time when this new reference is deleted. It is not ' |
| 7264 | 'guaranteed that\n' |
| 7265 | ' "__del__()" methods are called for objects that still ' |
| 7266 | 'exist when\n' |
| 7267 | ' the interpreter exits.\n' |
| 7268 | '\n' |
| 7269 | ' Note: "del x" doesn\'t directly call "x.__del__()" --- ' |
| 7270 | 'the former\n' |
| 7271 | ' decrements the reference count for "x" by one, and the ' |
| 7272 | 'latter is\n' |
| 7273 | ' only called when "x"\'s reference count reaches zero. ' |
| 7274 | 'Some common\n' |
| 7275 | ' situations that may prevent the reference count of an ' |
| 7276 | 'object from\n' |
| 7277 | ' going to zero include: circular references between ' |
| 7278 | 'objects (e.g.,\n' |
| 7279 | ' a doubly-linked list or a tree data structure with ' |
| 7280 | 'parent and\n' |
| 7281 | ' child pointers); a reference to the object on the stack ' |
| 7282 | 'frame of\n' |
| 7283 | ' a function that caught an exception (the traceback ' |
| 7284 | 'stored in\n' |
| 7285 | ' "sys.exc_traceback" keeps the stack frame alive); or a ' |
| 7286 | 'reference\n' |
| 7287 | ' to the object on the stack frame that raised an ' |
| 7288 | 'unhandled\n' |
| 7289 | ' exception in interactive mode (the traceback stored in\n' |
| 7290 | ' "sys.last_traceback" keeps the stack frame alive). The ' |
| 7291 | 'first\n' |
| 7292 | ' situation can only be remedied by explicitly breaking ' |
| 7293 | 'the cycles;\n' |
| 7294 | ' the latter two situations can be resolved by storing ' |
| 7295 | '"None" in\n' |
| 7296 | ' "sys.exc_traceback" or "sys.last_traceback". Circular ' |
| 7297 | 'references\n' |
| 7298 | ' which are garbage are detected when the option cycle ' |
| 7299 | 'detector is\n' |
| 7300 | " enabled (it's on by default), but can only be cleaned " |
| 7301 | 'up if there\n' |
| 7302 | ' are no Python-level "__del__()" methods involved. Refer ' |
| 7303 | 'to the\n' |
| 7304 | ' documentation for the "gc" module for more information ' |
| 7305 | 'about how\n' |
| 7306 | ' "__del__()" methods are handled by the cycle detector,\n' |
| 7307 | ' particularly the description of the "garbage" value.\n' |
| 7308 | '\n' |
| 7309 | ' Warning: Due to the precarious circumstances under which\n' |
| 7310 | ' "__del__()" methods are invoked, exceptions that occur ' |
| 7311 | 'during\n' |
| 7312 | ' their execution are ignored, and a warning is printed ' |
| 7313 | 'to\n' |
| 7314 | ' "sys.stderr" instead. Also, when "__del__()" is invoked ' |
| 7315 | 'in\n' |
| 7316 | ' response to a module being deleted (e.g., when ' |
| 7317 | 'execution of the\n' |
| 7318 | ' program is done), other globals referenced by the ' |
| 7319 | '"__del__()"\n' |
| 7320 | ' method may already have been deleted or in the process ' |
| 7321 | 'of being\n' |
| 7322 | ' torn down (e.g. the import machinery shutting down). ' |
| 7323 | 'For this\n' |
| 7324 | ' reason, "__del__()" methods should do the absolute ' |
| 7325 | 'minimum needed\n' |
| 7326 | ' to maintain external invariants. Starting with version ' |
| 7327 | '1.5,\n' |
| 7328 | ' Python guarantees that globals whose name begins with a ' |
| 7329 | 'single\n' |
| 7330 | ' underscore are deleted from their module before other ' |
| 7331 | 'globals are\n' |
| 7332 | ' deleted; if no other references to such globals exist, ' |
| 7333 | 'this may\n' |
| 7334 | ' help in assuring that imported modules are still ' |
| 7335 | 'available at the\n' |
| 7336 | ' time when the "__del__()" method is called.\n' |
| 7337 | '\n' |
| 7338 | ' See also the "-R" command-line option.\n' |
| 7339 | '\n' |
| 7340 | 'object.__repr__(self)\n' |
| 7341 | '\n' |
| 7342 | ' Called by the "repr()" built-in function and by string ' |
| 7343 | 'conversions\n' |
| 7344 | ' (reverse quotes) to compute the "official" string ' |
| 7345 | 'representation of\n' |
| 7346 | ' an object. If at all possible, this should look like a ' |
| 7347 | 'valid\n' |
| 7348 | ' Python expression that could be used to recreate an ' |
| 7349 | 'object with the\n' |
| 7350 | ' same value (given an appropriate environment). If this ' |
| 7351 | 'is not\n' |
| 7352 | ' possible, a string of the form "<...some useful ' |
| 7353 | 'description...>"\n' |
| 7354 | ' should be returned. The return value must be a string ' |
| 7355 | 'object. If a\n' |
| 7356 | ' class defines "__repr__()" but not "__str__()", then ' |
| 7357 | '"__repr__()"\n' |
| 7358 | ' is also used when an "informal" string representation of ' |
| 7359 | 'instances\n' |
| 7360 | ' of that class is required.\n' |
| 7361 | '\n' |
| 7362 | ' This is typically used for debugging, so it is important ' |
| 7363 | 'that the\n' |
| 7364 | ' representation is information-rich and unambiguous.\n' |
| 7365 | '\n' |
| 7366 | 'object.__str__(self)\n' |
| 7367 | '\n' |
| 7368 | ' Called by the "str()" built-in function and by the ' |
| 7369 | '"print"\n' |
| 7370 | ' statement to compute the "informal" string representation ' |
| 7371 | 'of an\n' |
| 7372 | ' object. This differs from "__repr__()" in that it does ' |
| 7373 | 'not have to\n' |
| 7374 | ' be a valid Python expression: a more convenient or ' |
| 7375 | 'concise\n' |
| 7376 | ' representation may be used instead. The return value must ' |
| 7377 | 'be a\n' |
| 7378 | ' string object.\n' |
| 7379 | '\n' |
| 7380 | 'object.__lt__(self, other)\n' |
| 7381 | 'object.__le__(self, other)\n' |
| 7382 | 'object.__eq__(self, other)\n' |
| 7383 | 'object.__ne__(self, other)\n' |
| 7384 | 'object.__gt__(self, other)\n' |
| 7385 | 'object.__ge__(self, other)\n' |
| 7386 | '\n' |
| 7387 | ' New in version 2.1.\n' |
| 7388 | '\n' |
| 7389 | ' These are the so-called "rich comparison" methods, and ' |
| 7390 | 'are called\n' |
| 7391 | ' for comparison operators in preference to "__cmp__()" ' |
| 7392 | 'below. The\n' |
| 7393 | ' correspondence between operator symbols and method names ' |
| 7394 | 'is as\n' |
| 7395 | ' follows: "x<y" calls "x.__lt__(y)", "x<=y" calls ' |
| 7396 | '"x.__le__(y)",\n' |
| 7397 | ' "x==y" calls "x.__eq__(y)", "x!=y" and "x<>y" call ' |
| 7398 | '"x.__ne__(y)",\n' |
| 7399 | ' "x>y" calls "x.__gt__(y)", and "x>=y" calls ' |
| 7400 | '"x.__ge__(y)".\n' |
| 7401 | '\n' |
| 7402 | ' A rich comparison method may return the singleton ' |
| 7403 | '"NotImplemented"\n' |
| 7404 | ' if it does not implement the operation for a given pair ' |
| 7405 | 'of\n' |
| 7406 | ' arguments. By convention, "False" and "True" are returned ' |
| 7407 | 'for a\n' |
| 7408 | ' successful comparison. However, these methods can return ' |
| 7409 | 'any value,\n' |
| 7410 | ' so if the comparison operator is used in a Boolean ' |
| 7411 | 'context (e.g.,\n' |
| 7412 | ' in the condition of an "if" statement), Python will call ' |
| 7413 | '"bool()"\n' |
| 7414 | ' on the value to determine if the result is true or ' |
| 7415 | 'false.\n' |
| 7416 | '\n' |
| 7417 | ' There are no implied relationships among the comparison ' |
| 7418 | 'operators.\n' |
| 7419 | ' The truth of "x==y" does not imply that "x!=y" is false.\n' |
| 7420 | ' Accordingly, when defining "__eq__()", one should also ' |
| 7421 | 'define\n' |
| 7422 | ' "__ne__()" so that the operators will behave as ' |
| 7423 | 'expected. See the\n' |
| 7424 | ' paragraph on "__hash__()" for some important notes on ' |
| 7425 | 'creating\n' |
| 7426 | ' *hashable* objects which support custom comparison ' |
| 7427 | 'operations and\n' |
| 7428 | ' are usable as dictionary keys.\n' |
| 7429 | '\n' |
| 7430 | ' There are no swapped-argument versions of these methods ' |
| 7431 | '(to be used\n' |
| 7432 | ' when the left argument does not support the operation but ' |
| 7433 | 'the right\n' |
| 7434 | ' argument does); rather, "__lt__()" and "__gt__()" are ' |
| 7435 | "each other's\n" |
| 7436 | ' reflection, "__le__()" and "__ge__()" are each other\'s ' |
| 7437 | 'reflection,\n' |
| 7438 | ' and "__eq__()" and "__ne__()" are their own reflection.\n' |
| 7439 | '\n' |
| 7440 | ' Arguments to rich comparison methods are never coerced.\n' |
| 7441 | '\n' |
| 7442 | ' To automatically generate ordering operations from a ' |
| 7443 | 'single root\n' |
| 7444 | ' operation, see "functools.total_ordering()".\n' |
| 7445 | '\n' |
| 7446 | 'object.__cmp__(self, other)\n' |
| 7447 | '\n' |
| 7448 | ' Called by comparison operations if rich comparison (see ' |
| 7449 | 'above) is\n' |
| 7450 | ' not defined. Should return a negative integer if "self < ' |
| 7451 | 'other",\n' |
| 7452 | ' zero if "self == other", a positive integer if "self > ' |
| 7453 | 'other". If\n' |
| 7454 | ' no "__cmp__()", "__eq__()" or "__ne__()" operation is ' |
| 7455 | 'defined,\n' |
| 7456 | ' class instances are compared by object identity ' |
| 7457 | '("address"). See\n' |
| 7458 | ' also the description of "__hash__()" for some important ' |
| 7459 | 'notes on\n' |
| 7460 | ' creating *hashable* objects which support custom ' |
| 7461 | 'comparison\n' |
| 7462 | ' operations and are usable as dictionary keys. (Note: the\n' |
| 7463 | ' restriction that exceptions are not propagated by ' |
| 7464 | '"__cmp__()" has\n' |
| 7465 | ' been removed since Python 1.5.)\n' |
| 7466 | '\n' |
| 7467 | 'object.__rcmp__(self, other)\n' |
| 7468 | '\n' |
| 7469 | ' Changed in version 2.1: No longer supported.\n' |
| 7470 | '\n' |
| 7471 | 'object.__hash__(self)\n' |
| 7472 | '\n' |
| 7473 | ' Called by built-in function "hash()" and for operations ' |
| 7474 | 'on members\n' |
| 7475 | ' of hashed collections including "set", "frozenset", and ' |
| 7476 | '"dict".\n' |
| 7477 | ' "__hash__()" should return an integer. The only required ' |
| 7478 | 'property\n' |
| 7479 | ' is that objects which compare equal have the same hash ' |
| 7480 | 'value; it is\n' |
| 7481 | ' advised to mix together the hash values of the components ' |
| 7482 | 'of the\n' |
| 7483 | ' object that also play a part in comparison of objects by ' |
| 7484 | 'packing\n' |
| 7485 | ' them into a tuple and hashing the tuple. Example:\n' |
| 7486 | '\n' |
| 7487 | ' def __hash__(self):\n' |
| 7488 | ' return hash((self.name, self.nick, self.color))\n' |
| 7489 | '\n' |
| 7490 | ' If a class does not define a "__cmp__()" or "__eq__()" ' |
| 7491 | 'method it\n' |
| 7492 | ' should not define a "__hash__()" operation either; if it ' |
| 7493 | 'defines\n' |
| 7494 | ' "__cmp__()" or "__eq__()" but not "__hash__()", its ' |
| 7495 | 'instances will\n' |
| 7496 | ' not be usable in hashed collections. If a class defines ' |
| 7497 | 'mutable\n' |
| 7498 | ' objects and implements a "__cmp__()" or "__eq__()" ' |
| 7499 | 'method, it\n' |
| 7500 | ' should not implement "__hash__()", since hashable ' |
| 7501 | 'collection\n' |
| 7502 | " implementations require that an object's hash value is " |
| 7503 | 'immutable\n' |
| 7504 | " (if the object's hash value changes, it will be in the " |
| 7505 | 'wrong hash\n' |
| 7506 | ' bucket).\n' |
| 7507 | '\n' |
| 7508 | ' User-defined classes have "__cmp__()" and "__hash__()" ' |
| 7509 | 'methods by\n' |
| 7510 | ' default; with them, all objects compare unequal (except ' |
| 7511 | 'with\n' |
| 7512 | ' themselves) and "x.__hash__()" returns a result derived ' |
| 7513 | 'from\n' |
| 7514 | ' "id(x)".\n' |
| 7515 | '\n' |
| 7516 | ' Classes which inherit a "__hash__()" method from a parent ' |
| 7517 | 'class but\n' |
| 7518 | ' change the meaning of "__cmp__()" or "__eq__()" such that ' |
| 7519 | 'the hash\n' |
| 7520 | ' value returned is no longer appropriate (e.g. by ' |
| 7521 | 'switching to a\n' |
| 7522 | ' value-based concept of equality instead of the default ' |
| 7523 | 'identity\n' |
| 7524 | ' based equality) can explicitly flag themselves as being ' |
| 7525 | 'unhashable\n' |
| 7526 | ' by setting "__hash__ = None" in the class definition. ' |
| 7527 | 'Doing so\n' |
| 7528 | ' means that not only will instances of the class raise an\n' |
| 7529 | ' appropriate "TypeError" when a program attempts to ' |
| 7530 | 'retrieve their\n' |
| 7531 | ' hash value, but they will also be correctly identified ' |
| 7532 | 'as\n' |
| 7533 | ' unhashable when checking "isinstance(obj, ' |
| 7534 | 'collections.Hashable)"\n' |
| 7535 | ' (unlike classes which define their own "__hash__()" to ' |
| 7536 | 'explicitly\n' |
| 7537 | ' raise "TypeError").\n' |
| 7538 | '\n' |
| 7539 | ' Changed in version 2.5: "__hash__()" may now also return ' |
| 7540 | 'a long\n' |
| 7541 | ' integer object; the 32-bit integer is then derived from ' |
| 7542 | 'the hash of\n' |
| 7543 | ' that object.\n' |
| 7544 | '\n' |
| 7545 | ' Changed in version 2.6: "__hash__" may now be set to ' |
| 7546 | '"None" to\n' |
| 7547 | ' explicitly flag instances of a class as unhashable.\n' |
| 7548 | '\n' |
| 7549 | 'object.__nonzero__(self)\n' |
| 7550 | '\n' |
| 7551 | ' Called to implement truth value testing and the built-in ' |
| 7552 | 'operation\n' |
| 7553 | ' "bool()"; should return "False" or "True", or their ' |
| 7554 | 'integer\n' |
| 7555 | ' equivalents "0" or "1". When this method is not ' |
| 7556 | 'defined,\n' |
| 7557 | ' "__len__()" is called, if it is defined, and the object ' |
| 7558 | 'is\n' |
| 7559 | ' considered true if its result is nonzero. If a class ' |
| 7560 | 'defines\n' |
| 7561 | ' neither "__len__()" nor "__nonzero__()", all its ' |
| 7562 | 'instances are\n' |
| 7563 | ' considered true.\n' |
| 7564 | '\n' |
| 7565 | 'object.__unicode__(self)\n' |
| 7566 | '\n' |
| 7567 | ' Called to implement "unicode()" built-in; should return a ' |
| 7568 | 'Unicode\n' |
| 7569 | ' object. When this method is not defined, string ' |
| 7570 | 'conversion is\n' |
| 7571 | ' attempted, and the result of string conversion is ' |
| 7572 | 'converted to\n' |
| 7573 | ' Unicode using the system default encoding.\n' |
| 7574 | '\n' |
| 7575 | '\n' |
| 7576 | 'Customizing attribute access\n' |
| 7577 | '============================\n' |
| 7578 | '\n' |
| 7579 | 'The following methods can be defined to customize the ' |
| 7580 | 'meaning of\n' |
| 7581 | 'attribute access (use of, assignment to, or deletion of ' |
| 7582 | '"x.name") for\n' |
| 7583 | 'class instances.\n' |
| 7584 | '\n' |
| 7585 | 'object.__getattr__(self, name)\n' |
| 7586 | '\n' |
| 7587 | ' Called when an attribute lookup has not found the ' |
| 7588 | 'attribute in the\n' |
| 7589 | ' usual places (i.e. it is not an instance attribute nor is ' |
| 7590 | 'it found\n' |
| 7591 | ' in the class tree for "self"). "name" is the attribute ' |
| 7592 | 'name. This\n' |
| 7593 | ' method should return the (computed) attribute value or ' |
| 7594 | 'raise an\n' |
| 7595 | ' "AttributeError" exception.\n' |
| 7596 | '\n' |
| 7597 | ' Note that if the attribute is found through the normal ' |
| 7598 | 'mechanism,\n' |
| 7599 | ' "__getattr__()" is not called. (This is an intentional ' |
| 7600 | 'asymmetry\n' |
| 7601 | ' between "__getattr__()" and "__setattr__()".) This is ' |
| 7602 | 'done both for\n' |
| 7603 | ' efficiency reasons and because otherwise "__getattr__()" ' |
| 7604 | 'would have\n' |
| 7605 | ' no way to access other attributes of the instance. Note ' |
| 7606 | 'that at\n' |
| 7607 | ' least for instance variables, you can fake total control ' |
| 7608 | 'by not\n' |
| 7609 | ' inserting any values in the instance attribute dictionary ' |
| 7610 | '(but\n' |
| 7611 | ' instead inserting them in another object). See the\n' |
| 7612 | ' "__getattribute__()" method below for a way to actually ' |
| 7613 | 'get total\n' |
| 7614 | ' control in new-style classes.\n' |
| 7615 | '\n' |
| 7616 | 'object.__setattr__(self, name, value)\n' |
| 7617 | '\n' |
| 7618 | ' Called when an attribute assignment is attempted. This ' |
| 7619 | 'is called\n' |
| 7620 | ' instead of the normal mechanism (i.e. store the value in ' |
| 7621 | 'the\n' |
| 7622 | ' instance dictionary). *name* is the attribute name, ' |
| 7623 | '*value* is the\n' |
| 7624 | ' value to be assigned to it.\n' |
| 7625 | '\n' |
| 7626 | ' If "__setattr__()" wants to assign to an instance ' |
| 7627 | 'attribute, it\n' |
| 7628 | ' should not simply execute "self.name = value" --- this ' |
| 7629 | 'would cause\n' |
| 7630 | ' a recursive call to itself. Instead, it should insert ' |
| 7631 | 'the value in\n' |
| 7632 | ' the dictionary of instance attributes, e.g., ' |
| 7633 | '"self.__dict__[name] =\n' |
| 7634 | ' value". For new-style classes, rather than accessing the ' |
| 7635 | 'instance\n' |
| 7636 | ' dictionary, it should call the base class method with the ' |
| 7637 | 'same\n' |
| 7638 | ' name, for example, "object.__setattr__(self, name, ' |
| 7639 | 'value)".\n' |
| 7640 | '\n' |
| 7641 | 'object.__delattr__(self, name)\n' |
| 7642 | '\n' |
| 7643 | ' Like "__setattr__()" but for attribute deletion instead ' |
| 7644 | 'of\n' |
| 7645 | ' assignment. This should only be implemented if "del ' |
| 7646 | 'obj.name" is\n' |
| 7647 | ' meaningful for the object.\n' |
| 7648 | '\n' |
| 7649 | '\n' |
| 7650 | 'More attribute access for new-style classes\n' |
| 7651 | '-------------------------------------------\n' |
| 7652 | '\n' |
| 7653 | 'The following methods only apply to new-style classes.\n' |
| 7654 | '\n' |
| 7655 | 'object.__getattribute__(self, name)\n' |
| 7656 | '\n' |
| 7657 | ' Called unconditionally to implement attribute accesses ' |
| 7658 | 'for\n' |
| 7659 | ' instances of the class. If the class also defines ' |
| 7660 | '"__getattr__()",\n' |
| 7661 | ' the latter will not be called unless "__getattribute__()" ' |
| 7662 | 'either\n' |
| 7663 | ' calls it explicitly or raises an "AttributeError". This ' |
| 7664 | 'method\n' |
| 7665 | ' should return the (computed) attribute value or raise an\n' |
| 7666 | ' "AttributeError" exception. In order to avoid infinite ' |
| 7667 | 'recursion in\n' |
| 7668 | ' this method, its implementation should always call the ' |
| 7669 | 'base class\n' |
| 7670 | ' method with the same name to access any attributes it ' |
| 7671 | 'needs, for\n' |
| 7672 | ' example, "object.__getattribute__(self, name)".\n' |
| 7673 | '\n' |
| 7674 | ' Note: This method may still be bypassed when looking up ' |
| 7675 | 'special\n' |
| 7676 | ' methods as the result of implicit invocation via ' |
| 7677 | 'language syntax\n' |
| 7678 | ' or built-in functions. See Special method lookup for ' |
| 7679 | 'new-style\n' |
| 7680 | ' classes.\n' |
| 7681 | '\n' |
| 7682 | '\n' |
| 7683 | 'Implementing Descriptors\n' |
| 7684 | '------------------------\n' |
| 7685 | '\n' |
| 7686 | 'The following methods only apply when an instance of the ' |
| 7687 | 'class\n' |
| 7688 | 'containing the method (a so-called *descriptor* class) ' |
| 7689 | 'appears in an\n' |
| 7690 | "*owner* class (the descriptor must be in either the owner's " |
| 7691 | 'class\n' |
| 7692 | 'dictionary or in the class dictionary for one of its ' |
| 7693 | 'parents). In the\n' |
| 7694 | 'examples below, "the attribute" refers to the attribute ' |
| 7695 | 'whose name is\n' |
| 7696 | 'the key of the property in the owner class\' "__dict__".\n' |
| 7697 | '\n' |
| 7698 | 'object.__get__(self, instance, owner)\n' |
| 7699 | '\n' |
| 7700 | ' Called to get the attribute of the owner class (class ' |
| 7701 | 'attribute\n' |
| 7702 | ' access) or of an instance of that class (instance ' |
| 7703 | 'attribute\n' |
| 7704 | ' access). *owner* is always the owner class, while ' |
| 7705 | '*instance* is the\n' |
| 7706 | ' instance that the attribute was accessed through, or ' |
| 7707 | '"None" when\n' |
| 7708 | ' the attribute is accessed through the *owner*. This ' |
| 7709 | 'method should\n' |
| 7710 | ' return the (computed) attribute value or raise an ' |
| 7711 | '"AttributeError"\n' |
| 7712 | ' exception.\n' |
| 7713 | '\n' |
| 7714 | 'object.__set__(self, instance, value)\n' |
| 7715 | '\n' |
| 7716 | ' Called to set the attribute on an instance *instance* of ' |
| 7717 | 'the owner\n' |
| 7718 | ' class to a new value, *value*.\n' |
| 7719 | '\n' |
| 7720 | 'object.__delete__(self, instance)\n' |
| 7721 | '\n' |
| 7722 | ' Called to delete the attribute on an instance *instance* ' |
| 7723 | 'of the\n' |
| 7724 | ' owner class.\n' |
| 7725 | '\n' |
| 7726 | '\n' |
| 7727 | 'Invoking Descriptors\n' |
| 7728 | '--------------------\n' |
| 7729 | '\n' |
| 7730 | 'In general, a descriptor is an object attribute with ' |
| 7731 | '"binding\n' |
| 7732 | 'behavior", one whose attribute access has been overridden by ' |
| 7733 | 'methods\n' |
| 7734 | 'in the descriptor protocol: "__get__()", "__set__()", and\n' |
| 7735 | '"__delete__()". If any of those methods are defined for an ' |
| 7736 | 'object, it\n' |
| 7737 | 'is said to be a descriptor.\n' |
| 7738 | '\n' |
| 7739 | 'The default behavior for attribute access is to get, set, or ' |
| 7740 | 'delete\n' |
| 7741 | "the attribute from an object's dictionary. For instance, " |
| 7742 | '"a.x" has a\n' |
| 7743 | 'lookup chain starting with "a.__dict__[\'x\']", then\n' |
| 7744 | '"type(a).__dict__[\'x\']", and continuing through the base ' |
| 7745 | 'classes of\n' |
| 7746 | '"type(a)" excluding metaclasses.\n' |
| 7747 | '\n' |
| 7748 | 'However, if the looked-up value is an object defining one of ' |
| 7749 | 'the\n' |
| 7750 | 'descriptor methods, then Python may override the default ' |
| 7751 | 'behavior and\n' |
| 7752 | 'invoke the descriptor method instead. Where this occurs in ' |
| 7753 | 'the\n' |
| 7754 | 'precedence chain depends on which descriptor methods were ' |
| 7755 | 'defined and\n' |
| 7756 | 'how they were called. Note that descriptors are only ' |
| 7757 | 'invoked for new\n' |
| 7758 | 'style objects or classes (ones that subclass "object()" or ' |
| 7759 | '"type()").\n' |
| 7760 | '\n' |
| 7761 | 'The starting point for descriptor invocation is a binding, ' |
| 7762 | '"a.x". How\n' |
| 7763 | 'the arguments are assembled depends on "a":\n' |
| 7764 | '\n' |
| 7765 | 'Direct Call\n' |
| 7766 | ' The simplest and least common call is when user code ' |
| 7767 | 'directly\n' |
| 7768 | ' invokes a descriptor method: "x.__get__(a)".\n' |
| 7769 | '\n' |
| 7770 | 'Instance Binding\n' |
| 7771 | ' If binding to a new-style object instance, "a.x" is ' |
| 7772 | 'transformed\n' |
| 7773 | ' into the call: "type(a).__dict__[\'x\'].__get__(a, ' |
| 7774 | 'type(a))".\n' |
| 7775 | '\n' |
| 7776 | 'Class Binding\n' |
| 7777 | ' If binding to a new-style class, "A.x" is transformed ' |
| 7778 | 'into the\n' |
| 7779 | ' call: "A.__dict__[\'x\'].__get__(None, A)".\n' |
| 7780 | '\n' |
| 7781 | 'Super Binding\n' |
| 7782 | ' If "a" is an instance of "super", then the binding ' |
| 7783 | '"super(B,\n' |
| 7784 | ' obj).m()" searches "obj.__class__.__mro__" for the base ' |
| 7785 | 'class "A"\n' |
| 7786 | ' immediately preceding "B" and then invokes the descriptor ' |
| 7787 | 'with the\n' |
| 7788 | ' call: "A.__dict__[\'m\'].__get__(obj, obj.__class__)".\n' |
| 7789 | '\n' |
| 7790 | 'For instance bindings, the precedence of descriptor ' |
| 7791 | 'invocation depends\n' |
| 7792 | 'on the which descriptor methods are defined. A descriptor ' |
| 7793 | 'can define\n' |
| 7794 | 'any combination of "__get__()", "__set__()" and ' |
| 7795 | '"__delete__()". If it\n' |
| 7796 | 'does not define "__get__()", then accessing the attribute ' |
| 7797 | 'will return\n' |
| 7798 | 'the descriptor object itself unless there is a value in the ' |
| 7799 | "object's\n" |
| 7800 | 'instance dictionary. If the descriptor defines "__set__()" ' |
| 7801 | 'and/or\n' |
| 7802 | '"__delete__()", it is a data descriptor; if it defines ' |
| 7803 | 'neither, it is\n' |
| 7804 | 'a non-data descriptor. Normally, data descriptors define ' |
| 7805 | 'both\n' |
| 7806 | '"__get__()" and "__set__()", while non-data descriptors have ' |
| 7807 | 'just the\n' |
| 7808 | '"__get__()" method. Data descriptors with "__set__()" and ' |
| 7809 | '"__get__()"\n' |
| 7810 | 'defined always override a redefinition in an instance ' |
| 7811 | 'dictionary. In\n' |
| 7812 | 'contrast, non-data descriptors can be overridden by ' |
| 7813 | 'instances.\n' |
| 7814 | '\n' |
| 7815 | 'Python methods (including "staticmethod()" and ' |
| 7816 | '"classmethod()") are\n' |
| 7817 | 'implemented as non-data descriptors. Accordingly, instances ' |
| 7818 | 'can\n' |
| 7819 | 'redefine and override methods. This allows individual ' |
| 7820 | 'instances to\n' |
| 7821 | 'acquire behaviors that differ from other instances of the ' |
| 7822 | 'same class.\n' |
| 7823 | '\n' |
| 7824 | 'The "property()" function is implemented as a data ' |
| 7825 | 'descriptor.\n' |
| 7826 | 'Accordingly, instances cannot override the behavior of a ' |
| 7827 | 'property.\n' |
| 7828 | '\n' |
| 7829 | '\n' |
| 7830 | '__slots__\n' |
| 7831 | '---------\n' |
| 7832 | '\n' |
| 7833 | 'By default, instances of both old and new-style classes have ' |
| 7834 | 'a\n' |
| 7835 | 'dictionary for attribute storage. This wastes space for ' |
| 7836 | 'objects\n' |
| 7837 | 'having very few instance variables. The space consumption ' |
| 7838 | 'can become\n' |
| 7839 | 'acute when creating large numbers of instances.\n' |
| 7840 | '\n' |
| 7841 | 'The default can be overridden by defining *__slots__* in a ' |
| 7842 | 'new-style\n' |
| 7843 | 'class definition. The *__slots__* declaration takes a ' |
| 7844 | 'sequence of\n' |
| 7845 | 'instance variables and reserves just enough space in each ' |
| 7846 | 'instance to\n' |
| 7847 | 'hold a value for each variable. Space is saved because ' |
| 7848 | '*__dict__* is\n' |
| 7849 | 'not created for each instance.\n' |
| 7850 | '\n' |
| 7851 | '__slots__\n' |
| 7852 | '\n' |
| 7853 | ' This class variable can be assigned a string, iterable, ' |
| 7854 | 'or sequence\n' |
| 7855 | ' of strings with variable names used by instances. If ' |
| 7856 | 'defined in a\n' |
| 7857 | ' new-style class, *__slots__* reserves space for the ' |
| 7858 | 'declared\n' |
| 7859 | ' variables and prevents the automatic creation of ' |
| 7860 | '*__dict__* and\n' |
| 7861 | ' *__weakref__* for each instance.\n' |
| 7862 | '\n' |
| 7863 | ' New in version 2.2.\n' |
| 7864 | '\n' |
| 7865 | 'Notes on using *__slots__*\n' |
| 7866 | '\n' |
| 7867 | '* When inheriting from a class without *__slots__*, the ' |
| 7868 | '*__dict__*\n' |
| 7869 | ' attribute of that class will always be accessible, so a ' |
| 7870 | '*__slots__*\n' |
| 7871 | ' definition in the subclass is meaningless.\n' |
| 7872 | '\n' |
| 7873 | '* Without a *__dict__* variable, instances cannot be ' |
| 7874 | 'assigned new\n' |
| 7875 | ' variables not listed in the *__slots__* definition. ' |
| 7876 | 'Attempts to\n' |
| 7877 | ' assign to an unlisted variable name raises ' |
| 7878 | '"AttributeError". If\n' |
| 7879 | ' dynamic assignment of new variables is desired, then add\n' |
| 7880 | ' "\'__dict__\'" to the sequence of strings in the ' |
| 7881 | '*__slots__*\n' |
| 7882 | ' declaration.\n' |
| 7883 | '\n' |
| 7884 | ' Changed in version 2.3: Previously, adding "\'__dict__\'" ' |
| 7885 | 'to the\n' |
| 7886 | ' *__slots__* declaration would not enable the assignment of ' |
| 7887 | 'new\n' |
| 7888 | ' attributes not specifically listed in the sequence of ' |
| 7889 | 'instance\n' |
| 7890 | ' variable names.\n' |
| 7891 | '\n' |
| 7892 | '* Without a *__weakref__* variable for each instance, ' |
| 7893 | 'classes\n' |
| 7894 | ' defining *__slots__* do not support weak references to ' |
| 7895 | 'its\n' |
| 7896 | ' instances. If weak reference support is needed, then add\n' |
| 7897 | ' "\'__weakref__\'" to the sequence of strings in the ' |
| 7898 | '*__slots__*\n' |
| 7899 | ' declaration.\n' |
| 7900 | '\n' |
| 7901 | ' Changed in version 2.3: Previously, adding ' |
| 7902 | '"\'__weakref__\'" to the\n' |
| 7903 | ' *__slots__* declaration would not enable support for weak\n' |
| 7904 | ' references.\n' |
| 7905 | '\n' |
| 7906 | '* *__slots__* are implemented at the class level by ' |
| 7907 | 'creating\n' |
| 7908 | ' descriptors (Implementing Descriptors) for each variable ' |
| 7909 | 'name. As a\n' |
| 7910 | ' result, class attributes cannot be used to set default ' |
| 7911 | 'values for\n' |
| 7912 | ' instance variables defined by *__slots__*; otherwise, the ' |
| 7913 | 'class\n' |
| 7914 | ' attribute would overwrite the descriptor assignment.\n' |
| 7915 | '\n' |
| 7916 | '* The action of a *__slots__* declaration is limited to the ' |
| 7917 | 'class\n' |
| 7918 | ' where it is defined. As a result, subclasses will have a ' |
| 7919 | '*__dict__*\n' |
| 7920 | ' unless they also define *__slots__* (which must only ' |
| 7921 | 'contain names\n' |
| 7922 | ' of any *additional* slots).\n' |
| 7923 | '\n' |
| 7924 | '* If a class defines a slot also defined in a base class, ' |
| 7925 | 'the\n' |
| 7926 | ' instance variable defined by the base class slot is ' |
| 7927 | 'inaccessible\n' |
| 7928 | ' (except by retrieving its descriptor directly from the ' |
| 7929 | 'base class).\n' |
| 7930 | ' This renders the meaning of the program undefined. In the ' |
| 7931 | 'future, a\n' |
| 7932 | ' check may be added to prevent this.\n' |
| 7933 | '\n' |
| 7934 | '* Nonempty *__slots__* does not work for classes derived ' |
| 7935 | 'from\n' |
| 7936 | ' "variable-length" built-in types such as "long", "str" and ' |
| 7937 | '"tuple".\n' |
| 7938 | '\n' |
| 7939 | '* Any non-string iterable may be assigned to *__slots__*. ' |
| 7940 | 'Mappings\n' |
| 7941 | ' may also be used; however, in the future, special meaning ' |
| 7942 | 'may be\n' |
| 7943 | ' assigned to the values corresponding to each key.\n' |
| 7944 | '\n' |
| 7945 | '* *__class__* assignment works only if both classes have the ' |
| 7946 | 'same\n' |
| 7947 | ' *__slots__*.\n' |
| 7948 | '\n' |
| 7949 | ' Changed in version 2.6: Previously, *__class__* assignment ' |
| 7950 | 'raised an\n' |
| 7951 | ' error if either new or old class had *__slots__*.\n' |
| 7952 | '\n' |
| 7953 | '\n' |
| 7954 | 'Customizing class creation\n' |
| 7955 | '==========================\n' |
| 7956 | '\n' |
| 7957 | 'By default, new-style classes are constructed using ' |
| 7958 | '"type()". A class\n' |
| 7959 | 'definition is read into a separate namespace and the value ' |
| 7960 | 'of class\n' |
| 7961 | 'name is bound to the result of "type(name, bases, dict)".\n' |
| 7962 | '\n' |
| 7963 | 'When the class definition is read, if *__metaclass__* is ' |
| 7964 | 'defined then\n' |
| 7965 | 'the callable assigned to it will be called instead of ' |
| 7966 | '"type()". This\n' |
| 7967 | 'allows classes or functions to be written which monitor or ' |
| 7968 | 'alter the\n' |
| 7969 | 'class creation process:\n' |
| 7970 | '\n' |
| 7971 | '* Modifying the class dictionary prior to the class being ' |
| 7972 | 'created.\n' |
| 7973 | '\n' |
| 7974 | '* Returning an instance of another class -- essentially ' |
| 7975 | 'performing\n' |
| 7976 | ' the role of a factory function.\n' |
| 7977 | '\n' |
| 7978 | "These steps will have to be performed in the metaclass's " |
| 7979 | '"__new__()"\n' |
| 7980 | 'method -- "type.__new__()" can then be called from this ' |
| 7981 | 'method to\n' |
| 7982 | 'create a class with different properties. This example adds ' |
| 7983 | 'a new\n' |
| 7984 | 'element to the class dictionary before creating the class:\n' |
| 7985 | '\n' |
| 7986 | ' class metacls(type):\n' |
| 7987 | ' def __new__(mcs, name, bases, dict):\n' |
| 7988 | " dict['foo'] = 'metacls was here'\n" |
| 7989 | ' return type.__new__(mcs, name, bases, dict)\n' |
| 7990 | '\n' |
| 7991 | 'You can of course also override other class methods (or add ' |
| 7992 | 'new\n' |
| 7993 | 'methods); for example defining a custom "__call__()" method ' |
| 7994 | 'in the\n' |
| 7995 | 'metaclass allows custom behavior when the class is called, ' |
| 7996 | 'e.g. not\n' |
| 7997 | 'always creating a new instance.\n' |
| 7998 | '\n' |
| 7999 | '__metaclass__\n' |
| 8000 | '\n' |
| 8001 | ' This variable can be any callable accepting arguments for ' |
| 8002 | '"name",\n' |
| 8003 | ' "bases", and "dict". Upon class creation, the callable ' |
| 8004 | 'is used\n' |
| 8005 | ' instead of the built-in "type()".\n' |
| 8006 | '\n' |
| 8007 | ' New in version 2.2.\n' |
| 8008 | '\n' |
| 8009 | 'The appropriate metaclass is determined by the following ' |
| 8010 | 'precedence\n' |
| 8011 | 'rules:\n' |
| 8012 | '\n' |
| 8013 | '* If "dict[\'__metaclass__\']" exists, it is used.\n' |
| 8014 | '\n' |
| 8015 | '* Otherwise, if there is at least one base class, its ' |
| 8016 | 'metaclass is\n' |
| 8017 | ' used (this looks for a *__class__* attribute first and if ' |
| 8018 | 'not found,\n' |
| 8019 | ' uses its type).\n' |
| 8020 | '\n' |
| 8021 | '* Otherwise, if a global variable named __metaclass__ ' |
| 8022 | 'exists, it is\n' |
| 8023 | ' used.\n' |
| 8024 | '\n' |
| 8025 | '* Otherwise, the old-style, classic metaclass ' |
| 8026 | '(types.ClassType) is\n' |
| 8027 | ' used.\n' |
| 8028 | '\n' |
| 8029 | 'The potential uses for metaclasses are boundless. Some ideas ' |
| 8030 | 'that have\n' |
| 8031 | 'been explored including logging, interface checking, ' |
| 8032 | 'automatic\n' |
| 8033 | 'delegation, automatic property creation, proxies, ' |
| 8034 | 'frameworks, and\n' |
| 8035 | 'automatic resource locking/synchronization.\n' |
| 8036 | '\n' |
| 8037 | '\n' |
| 8038 | 'Customizing instance and subclass checks\n' |
| 8039 | '========================================\n' |
| 8040 | '\n' |
| 8041 | 'New in version 2.6.\n' |
| 8042 | '\n' |
| 8043 | 'The following methods are used to override the default ' |
| 8044 | 'behavior of the\n' |
| 8045 | '"isinstance()" and "issubclass()" built-in functions.\n' |
| 8046 | '\n' |
| 8047 | 'In particular, the metaclass "abc.ABCMeta" implements these ' |
| 8048 | 'methods in\n' |
| 8049 | 'order to allow the addition of Abstract Base Classes (ABCs) ' |
| 8050 | 'as\n' |
| 8051 | '"virtual base classes" to any class or type (including ' |
| 8052 | 'built-in\n' |
| 8053 | 'types), including other ABCs.\n' |
| 8054 | '\n' |
| 8055 | 'class.__instancecheck__(self, instance)\n' |
| 8056 | '\n' |
| 8057 | ' Return true if *instance* should be considered a (direct ' |
| 8058 | 'or\n' |
| 8059 | ' indirect) instance of *class*. If defined, called to ' |
| 8060 | 'implement\n' |
| 8061 | ' "isinstance(instance, class)".\n' |
| 8062 | '\n' |
| 8063 | 'class.__subclasscheck__(self, subclass)\n' |
| 8064 | '\n' |
| 8065 | ' Return true if *subclass* should be considered a (direct ' |
| 8066 | 'or\n' |
| 8067 | ' indirect) subclass of *class*. If defined, called to ' |
| 8068 | 'implement\n' |
| 8069 | ' "issubclass(subclass, class)".\n' |
| 8070 | '\n' |
| 8071 | 'Note that these methods are looked up on the type ' |
| 8072 | '(metaclass) of a\n' |
| 8073 | 'class. They cannot be defined as class methods in the ' |
| 8074 | 'actual class.\n' |
| 8075 | 'This is consistent with the lookup of special methods that ' |
| 8076 | 'are called\n' |
| 8077 | 'on instances, only in this case the instance is itself a ' |
| 8078 | 'class.\n' |
| 8079 | '\n' |
| 8080 | 'See also:\n' |
| 8081 | '\n' |
| 8082 | ' **PEP 3119** - Introducing Abstract Base Classes\n' |
| 8083 | ' Includes the specification for customizing ' |
| 8084 | '"isinstance()" and\n' |
| 8085 | ' "issubclass()" behavior through "__instancecheck__()" ' |
| 8086 | 'and\n' |
| 8087 | ' "__subclasscheck__()", with motivation for this ' |
| 8088 | 'functionality in\n' |
| 8089 | ' the context of adding Abstract Base Classes (see the ' |
| 8090 | '"abc"\n' |
| 8091 | ' module) to the language.\n' |
| 8092 | '\n' |
| 8093 | '\n' |
| 8094 | 'Emulating callable objects\n' |
| 8095 | '==========================\n' |
| 8096 | '\n' |
| 8097 | 'object.__call__(self[, args...])\n' |
| 8098 | '\n' |
| 8099 | ' Called when the instance is "called" as a function; if ' |
| 8100 | 'this method\n' |
| 8101 | ' is defined, "x(arg1, arg2, ...)" is a shorthand for\n' |
| 8102 | ' "x.__call__(arg1, arg2, ...)".\n' |
| 8103 | '\n' |
| 8104 | '\n' |
| 8105 | 'Emulating container types\n' |
| 8106 | '=========================\n' |
| 8107 | '\n' |
| 8108 | 'The following methods can be defined to implement container ' |
| 8109 | 'objects.\n' |
| 8110 | 'Containers usually are sequences (such as lists or tuples) ' |
| 8111 | 'or mappings\n' |
| 8112 | '(like dictionaries), but can represent other containers as ' |
| 8113 | 'well. The\n' |
| 8114 | 'first set of methods is used either to emulate a sequence or ' |
| 8115 | 'to\n' |
| 8116 | 'emulate a mapping; the difference is that for a sequence, ' |
| 8117 | 'the\n' |
| 8118 | 'allowable keys should be the integers *k* for which "0 <= k ' |
| 8119 | '< N" where\n' |
| 8120 | '*N* is the length of the sequence, or slice objects, which ' |
| 8121 | 'define a\n' |
| 8122 | 'range of items. (For backwards compatibility, the method\n' |
| 8123 | '"__getslice__()" (see below) can also be defined to handle ' |
| 8124 | 'simple, but\n' |
| 8125 | 'not extended slices.) It is also recommended that mappings ' |
| 8126 | 'provide the\n' |
| 8127 | 'methods "keys()", "values()", "items()", "has_key()", ' |
| 8128 | '"get()",\n' |
| 8129 | '"clear()", "setdefault()", "iterkeys()", "itervalues()",\n' |
| 8130 | '"iteritems()", "pop()", "popitem()", "copy()", and ' |
| 8131 | '"update()" behaving\n' |
| 8132 | "similar to those for Python's standard dictionary objects. " |
| 8133 | 'The\n' |
| 8134 | '"UserDict" module provides a "DictMixin" class to help ' |
| 8135 | 'create those\n' |
| 8136 | 'methods from a base set of "__getitem__()", ' |
| 8137 | '"__setitem__()",\n' |
| 8138 | '"__delitem__()", and "keys()". Mutable sequences should ' |
| 8139 | 'provide\n' |
| 8140 | 'methods "append()", "count()", "index()", "extend()", ' |
| 8141 | '"insert()",\n' |
| 8142 | '"pop()", "remove()", "reverse()" and "sort()", like Python ' |
| 8143 | 'standard\n' |
| 8144 | 'list objects. Finally, sequence types should implement ' |
| 8145 | 'addition\n' |
| 8146 | '(meaning concatenation) and multiplication (meaning ' |
| 8147 | 'repetition) by\n' |
| 8148 | 'defining the methods "__add__()", "__radd__()", ' |
| 8149 | '"__iadd__()",\n' |
| 8150 | '"__mul__()", "__rmul__()" and "__imul__()" described below; ' |
| 8151 | 'they\n' |
| 8152 | 'should not define "__coerce__()" or other numerical ' |
| 8153 | 'operators. It is\n' |
| 8154 | 'recommended that both mappings and sequences implement the\n' |
| 8155 | '"__contains__()" method to allow efficient use of the "in" ' |
| 8156 | 'operator;\n' |
| 8157 | 'for mappings, "in" should be equivalent of "has_key()"; for ' |
| 8158 | 'sequences,\n' |
| 8159 | 'it should search through the values. It is further ' |
| 8160 | 'recommended that\n' |
| 8161 | 'both mappings and sequences implement the "__iter__()" ' |
| 8162 | 'method to allow\n' |
| 8163 | 'efficient iteration through the container; for mappings, ' |
| 8164 | '"__iter__()"\n' |
| 8165 | 'should be the same as "iterkeys()"; for sequences, it should ' |
| 8166 | 'iterate\n' |
| 8167 | 'through the values.\n' |
| 8168 | '\n' |
| 8169 | 'object.__len__(self)\n' |
| 8170 | '\n' |
| 8171 | ' Called to implement the built-in function "len()". ' |
| 8172 | 'Should return\n' |
| 8173 | ' the length of the object, an integer ">=" 0. Also, an ' |
| 8174 | 'object that\n' |
| 8175 | ' doesn\'t define a "__nonzero__()" method and whose ' |
| 8176 | '"__len__()"\n' |
| 8177 | ' method returns zero is considered to be false in a ' |
| 8178 | 'Boolean context.\n' |
| 8179 | '\n' |
| 8180 | ' **CPython implementation detail:** In CPython, the length ' |
| 8181 | 'is\n' |
| 8182 | ' required to be at most "sys.maxsize". If the length is ' |
| 8183 | 'larger than\n' |
| 8184 | ' "sys.maxsize" some features (such as "len()") may raise\n' |
| 8185 | ' "OverflowError". To prevent raising "OverflowError" by ' |
| 8186 | 'truth value\n' |
| 8187 | ' testing, an object must define a "__nonzero__()" method.\n' |
| 8188 | '\n' |
| 8189 | 'object.__getitem__(self, key)\n' |
| 8190 | '\n' |
| 8191 | ' Called to implement evaluation of "self[key]". For ' |
| 8192 | 'sequence types,\n' |
| 8193 | ' the accepted keys should be integers and slice objects. ' |
| 8194 | 'Note that\n' |
| 8195 | ' the special interpretation of negative indexes (if the ' |
| 8196 | 'class wishes\n' |
| 8197 | ' to emulate a sequence type) is up to the "__getitem__()" ' |
| 8198 | 'method. If\n' |
| 8199 | ' *key* is of an inappropriate type, "TypeError" may be ' |
| 8200 | 'raised; if of\n' |
| 8201 | ' a value outside the set of indexes for the sequence ' |
| 8202 | '(after any\n' |
| 8203 | ' special interpretation of negative values), "IndexError" ' |
| 8204 | 'should be\n' |
| 8205 | ' raised. For mapping types, if *key* is missing (not in ' |
| 8206 | 'the\n' |
| 8207 | ' container), "KeyError" should be raised.\n' |
| 8208 | '\n' |
| 8209 | ' Note: "for" loops expect that an "IndexError" will be ' |
| 8210 | 'raised for\n' |
| 8211 | ' illegal indexes to allow proper detection of the end of ' |
| 8212 | 'the\n' |
| 8213 | ' sequence.\n' |
| 8214 | '\n' |
| 8215 | 'object.__missing__(self, key)\n' |
| 8216 | '\n' |
| 8217 | ' Called by "dict"."__getitem__()" to implement "self[key]" ' |
| 8218 | 'for dict\n' |
| 8219 | ' subclasses when key is not in the dictionary.\n' |
| 8220 | '\n' |
| 8221 | 'object.__setitem__(self, key, value)\n' |
| 8222 | '\n' |
| 8223 | ' Called to implement assignment to "self[key]". Same note ' |
| 8224 | 'as for\n' |
| 8225 | ' "__getitem__()". This should only be implemented for ' |
| 8226 | 'mappings if\n' |
| 8227 | ' the objects support changes to the values for keys, or if ' |
| 8228 | 'new keys\n' |
| 8229 | ' can be added, or for sequences if elements can be ' |
| 8230 | 'replaced. The\n' |
| 8231 | ' same exceptions should be raised for improper *key* ' |
| 8232 | 'values as for\n' |
| 8233 | ' the "__getitem__()" method.\n' |
| 8234 | '\n' |
| 8235 | 'object.__delitem__(self, key)\n' |
| 8236 | '\n' |
| 8237 | ' Called to implement deletion of "self[key]". Same note ' |
| 8238 | 'as for\n' |
| 8239 | ' "__getitem__()". This should only be implemented for ' |
| 8240 | 'mappings if\n' |
| 8241 | ' the objects support removal of keys, or for sequences if ' |
| 8242 | 'elements\n' |
| 8243 | ' can be removed from the sequence. The same exceptions ' |
| 8244 | 'should be\n' |
| 8245 | ' raised for improper *key* values as for the ' |
| 8246 | '"__getitem__()" method.\n' |
| 8247 | '\n' |
| 8248 | 'object.__iter__(self)\n' |
| 8249 | '\n' |
| 8250 | ' This method is called when an iterator is required for a ' |
| 8251 | 'container.\n' |
| 8252 | ' This method should return a new iterator object that can ' |
| 8253 | 'iterate\n' |
| 8254 | ' over all the objects in the container. For mappings, it ' |
| 8255 | 'should\n' |
| 8256 | ' iterate over the keys of the container, and should also ' |
| 8257 | 'be made\n' |
| 8258 | ' available as the method "iterkeys()".\n' |
| 8259 | '\n' |
| 8260 | ' Iterator objects also need to implement this method; they ' |
| 8261 | 'are\n' |
| 8262 | ' required to return themselves. For more information on ' |
| 8263 | 'iterator\n' |
| 8264 | ' objects, see Iterator Types.\n' |
| 8265 | '\n' |
| 8266 | 'object.__reversed__(self)\n' |
| 8267 | '\n' |
| 8268 | ' Called (if present) by the "reversed()" built-in to ' |
| 8269 | 'implement\n' |
| 8270 | ' reverse iteration. It should return a new iterator ' |
| 8271 | 'object that\n' |
| 8272 | ' iterates over all the objects in the container in reverse ' |
| 8273 | 'order.\n' |
| 8274 | '\n' |
| 8275 | ' If the "__reversed__()" method is not provided, the ' |
| 8276 | '"reversed()"\n' |
| 8277 | ' built-in will fall back to using the sequence protocol ' |
| 8278 | '("__len__()"\n' |
| 8279 | ' and "__getitem__()"). Objects that support the sequence ' |
| 8280 | 'protocol\n' |
| 8281 | ' should only provide "__reversed__()" if they can provide ' |
| 8282 | 'an\n' |
| 8283 | ' implementation that is more efficient than the one ' |
| 8284 | 'provided by\n' |
| 8285 | ' "reversed()".\n' |
| 8286 | '\n' |
| 8287 | ' New in version 2.6.\n' |
| 8288 | '\n' |
| 8289 | 'The membership test operators ("in" and "not in") are ' |
| 8290 | 'normally\n' |
| 8291 | 'implemented as an iteration through a sequence. However, ' |
| 8292 | 'container\n' |
| 8293 | 'objects can supply the following special method with a more ' |
| 8294 | 'efficient\n' |
| 8295 | 'implementation, which also does not require the object be a ' |
| 8296 | 'sequence.\n' |
| 8297 | '\n' |
| 8298 | 'object.__contains__(self, item)\n' |
| 8299 | '\n' |
| 8300 | ' Called to implement membership test operators. Should ' |
| 8301 | 'return true\n' |
| 8302 | ' if *item* is in *self*, false otherwise. For mapping ' |
| 8303 | 'objects, this\n' |
| 8304 | ' should consider the keys of the mapping rather than the ' |
| 8305 | 'values or\n' |
| 8306 | ' the key-item pairs.\n' |
| 8307 | '\n' |
| 8308 | ' For objects that don\'t define "__contains__()", the ' |
| 8309 | 'membership test\n' |
| 8310 | ' first tries iteration via "__iter__()", then the old ' |
| 8311 | 'sequence\n' |
| 8312 | ' iteration protocol via "__getitem__()", see this section ' |
| 8313 | 'in the\n' |
| 8314 | ' language reference.\n' |
| 8315 | '\n' |
| 8316 | '\n' |
| 8317 | 'Additional methods for emulation of sequence types\n' |
| 8318 | '==================================================\n' |
| 8319 | '\n' |
| 8320 | 'The following optional methods can be defined to further ' |
| 8321 | 'emulate\n' |
| 8322 | 'sequence objects. Immutable sequences methods should at ' |
| 8323 | 'most only\n' |
| 8324 | 'define "__getslice__()"; mutable sequences might define all ' |
| 8325 | 'three\n' |
| 8326 | 'methods.\n' |
| 8327 | '\n' |
| 8328 | 'object.__getslice__(self, i, j)\n' |
| 8329 | '\n' |
| 8330 | ' Deprecated since version 2.0: Support slice objects as ' |
| 8331 | 'parameters\n' |
| 8332 | ' to the "__getitem__()" method. (However, built-in types ' |
| 8333 | 'in CPython\n' |
| 8334 | ' currently still implement "__getslice__()". Therefore, ' |
| 8335 | 'you have to\n' |
| 8336 | ' override it in derived classes when implementing ' |
| 8337 | 'slicing.)\n' |
| 8338 | '\n' |
| 8339 | ' Called to implement evaluation of "self[i:j]". The ' |
| 8340 | 'returned object\n' |
| 8341 | ' should be of the same type as *self*. Note that missing ' |
| 8342 | '*i* or *j*\n' |
| 8343 | ' in the slice expression are replaced by zero or ' |
| 8344 | '"sys.maxsize",\n' |
| 8345 | ' respectively. If negative indexes are used in the slice, ' |
| 8346 | 'the\n' |
| 8347 | ' length of the sequence is added to that index. If the ' |
| 8348 | 'instance does\n' |
| 8349 | ' not implement the "__len__()" method, an "AttributeError" ' |
| 8350 | 'is\n' |
| 8351 | ' raised. No guarantee is made that indexes adjusted this ' |
| 8352 | 'way are not\n' |
| 8353 | ' still negative. Indexes which are greater than the ' |
| 8354 | 'length of the\n' |
| 8355 | ' sequence are not modified. If no "__getslice__()" is ' |
| 8356 | 'found, a slice\n' |
| 8357 | ' object is created instead, and passed to "__getitem__()" ' |
| 8358 | 'instead.\n' |
| 8359 | '\n' |
| 8360 | 'object.__setslice__(self, i, j, sequence)\n' |
| 8361 | '\n' |
| 8362 | ' Called to implement assignment to "self[i:j]". Same notes ' |
| 8363 | 'for *i*\n' |
| 8364 | ' and *j* as for "__getslice__()".\n' |
| 8365 | '\n' |
| 8366 | ' This method is deprecated. If no "__setslice__()" is ' |
| 8367 | 'found, or for\n' |
| 8368 | ' extended slicing of the form "self[i:j:k]", a slice ' |
| 8369 | 'object is\n' |
| 8370 | ' created, and passed to "__setitem__()", instead of ' |
| 8371 | '"__setslice__()"\n' |
| 8372 | ' being called.\n' |
| 8373 | '\n' |
| 8374 | 'object.__delslice__(self, i, j)\n' |
| 8375 | '\n' |
| 8376 | ' Called to implement deletion of "self[i:j]". Same notes ' |
| 8377 | 'for *i* and\n' |
| 8378 | ' *j* as for "__getslice__()". This method is deprecated. ' |
| 8379 | 'If no\n' |
| 8380 | ' "__delslice__()" is found, or for extended slicing of the ' |
| 8381 | 'form\n' |
| 8382 | ' "self[i:j:k]", a slice object is created, and passed to\n' |
| 8383 | ' "__delitem__()", instead of "__delslice__()" being ' |
| 8384 | 'called.\n' |
| 8385 | '\n' |
| 8386 | 'Notice that these methods are only invoked when a single ' |
| 8387 | 'slice with a\n' |
| 8388 | 'single colon is used, and the slice method is available. ' |
| 8389 | 'For slice\n' |
| 8390 | 'operations involving extended slice notation, or in absence ' |
| 8391 | 'of the\n' |
| 8392 | 'slice methods, "__getitem__()", "__setitem__()" or ' |
| 8393 | '"__delitem__()" is\n' |
| 8394 | 'called with a slice object as argument.\n' |
| 8395 | '\n' |
| 8396 | 'The following example demonstrate how to make your program ' |
| 8397 | 'or module\n' |
| 8398 | 'compatible with earlier versions of Python (assuming that ' |
| 8399 | 'methods\n' |
| 8400 | '"__getitem__()", "__setitem__()" and "__delitem__()" support ' |
| 8401 | 'slice\n' |
| 8402 | 'objects as arguments):\n' |
| 8403 | '\n' |
| 8404 | ' class MyClass:\n' |
| 8405 | ' ...\n' |
| 8406 | ' def __getitem__(self, index):\n' |
| 8407 | ' ...\n' |
| 8408 | ' def __setitem__(self, index, value):\n' |
| 8409 | ' ...\n' |
| 8410 | ' def __delitem__(self, index):\n' |
| 8411 | ' ...\n' |
| 8412 | '\n' |
| 8413 | ' if sys.version_info < (2, 0):\n' |
| 8414 | " # They won't be defined if version is at least " |
| 8415 | '2.0 final\n' |
| 8416 | '\n' |
| 8417 | ' def __getslice__(self, i, j):\n' |
| 8418 | ' return self[max(0, i):max(0, j):]\n' |
| 8419 | ' def __setslice__(self, i, j, seq):\n' |
| 8420 | ' self[max(0, i):max(0, j):] = seq\n' |
| 8421 | ' def __delslice__(self, i, j):\n' |
| 8422 | ' del self[max(0, i):max(0, j):]\n' |
| 8423 | ' ...\n' |
| 8424 | '\n' |
| 8425 | 'Note the calls to "max()"; these are necessary because of ' |
| 8426 | 'the handling\n' |
| 8427 | 'of negative indices before the "__*slice__()" methods are ' |
| 8428 | 'called.\n' |
| 8429 | 'When negative indexes are used, the "__*item__()" methods ' |
| 8430 | 'receive them\n' |
| 8431 | 'as provided, but the "__*slice__()" methods get a "cooked" ' |
| 8432 | 'form of the\n' |
| 8433 | 'index values. For each negative index value, the length of ' |
| 8434 | 'the\n' |
| 8435 | 'sequence is added to the index before calling the method ' |
| 8436 | '(which may\n' |
| 8437 | 'still result in a negative index); this is the customary ' |
| 8438 | 'handling of\n' |
| 8439 | 'negative indexes by the built-in sequence types, and the ' |
| 8440 | '"__*item__()"\n' |
| 8441 | 'methods are expected to do this as well. However, since ' |
| 8442 | 'they should\n' |
| 8443 | 'already be doing that, negative indexes cannot be passed in; ' |
| 8444 | 'they must\n' |
| 8445 | 'be constrained to the bounds of the sequence before being ' |
| 8446 | 'passed to\n' |
| 8447 | 'the "__*item__()" methods. Calling "max(0, i)" conveniently ' |
| 8448 | 'returns\n' |
| 8449 | 'the proper value.\n' |
| 8450 | '\n' |
| 8451 | '\n' |
| 8452 | 'Emulating numeric types\n' |
| 8453 | '=======================\n' |
| 8454 | '\n' |
| 8455 | 'The following methods can be defined to emulate numeric ' |
| 8456 | 'objects.\n' |
| 8457 | 'Methods corresponding to operations that are not supported ' |
| 8458 | 'by the\n' |
| 8459 | 'particular kind of number implemented (e.g., bitwise ' |
| 8460 | 'operations for\n' |
| 8461 | 'non-integral numbers) should be left undefined.\n' |
| 8462 | '\n' |
| 8463 | 'object.__add__(self, other)\n' |
| 8464 | 'object.__sub__(self, other)\n' |
| 8465 | 'object.__mul__(self, other)\n' |
| 8466 | 'object.__floordiv__(self, other)\n' |
| 8467 | 'object.__mod__(self, other)\n' |
| 8468 | 'object.__divmod__(self, other)\n' |
| 8469 | 'object.__pow__(self, other[, modulo])\n' |
| 8470 | 'object.__lshift__(self, other)\n' |
| 8471 | 'object.__rshift__(self, other)\n' |
| 8472 | 'object.__and__(self, other)\n' |
| 8473 | 'object.__xor__(self, other)\n' |
| 8474 | 'object.__or__(self, other)\n' |
| 8475 | '\n' |
| 8476 | ' These methods are called to implement the binary ' |
| 8477 | 'arithmetic\n' |
| 8478 | ' operations ("+", "-", "*", "//", "%", "divmod()", ' |
| 8479 | '"pow()", "**",\n' |
| 8480 | ' "<<", ">>", "&", "^", "|"). For instance, to evaluate ' |
| 8481 | 'the\n' |
| 8482 | ' expression "x + y", where *x* is an instance of a class ' |
| 8483 | 'that has an\n' |
| 8484 | ' "__add__()" method, "x.__add__(y)" is called. The ' |
| 8485 | '"__divmod__()"\n' |
| 8486 | ' method should be the equivalent to using "__floordiv__()" ' |
| 8487 | 'and\n' |
| 8488 | ' "__mod__()"; it should not be related to "__truediv__()" ' |
| 8489 | '(described\n' |
| 8490 | ' below). Note that "__pow__()" should be defined to ' |
| 8491 | 'accept an\n' |
| 8492 | ' optional third argument if the ternary version of the ' |
| 8493 | 'built-in\n' |
| 8494 | ' "pow()" function is to be supported.\n' |
| 8495 | '\n' |
| 8496 | ' If one of those methods does not support the operation ' |
| 8497 | 'with the\n' |
| 8498 | ' supplied arguments, it should return "NotImplemented".\n' |
| 8499 | '\n' |
| 8500 | 'object.__div__(self, other)\n' |
| 8501 | 'object.__truediv__(self, other)\n' |
| 8502 | '\n' |
| 8503 | ' The division operator ("/") is implemented by these ' |
| 8504 | 'methods. The\n' |
| 8505 | ' "__truediv__()" method is used when "__future__.division" ' |
| 8506 | 'is in\n' |
| 8507 | ' effect, otherwise "__div__()" is used. If only one of ' |
| 8508 | 'these two\n' |
| 8509 | ' methods is defined, the object will not support division ' |
| 8510 | 'in the\n' |
| 8511 | ' alternate context; "TypeError" will be raised instead.\n' |
| 8512 | '\n' |
| 8513 | 'object.__radd__(self, other)\n' |
| 8514 | 'object.__rsub__(self, other)\n' |
| 8515 | 'object.__rmul__(self, other)\n' |
| 8516 | 'object.__rdiv__(self, other)\n' |
| 8517 | 'object.__rtruediv__(self, other)\n' |
| 8518 | 'object.__rfloordiv__(self, other)\n' |
| 8519 | 'object.__rmod__(self, other)\n' |
| 8520 | 'object.__rdivmod__(self, other)\n' |
| 8521 | 'object.__rpow__(self, other)\n' |
| 8522 | 'object.__rlshift__(self, other)\n' |
| 8523 | 'object.__rrshift__(self, other)\n' |
| 8524 | 'object.__rand__(self, other)\n' |
| 8525 | 'object.__rxor__(self, other)\n' |
| 8526 | 'object.__ror__(self, other)\n' |
| 8527 | '\n' |
| 8528 | ' These methods are called to implement the binary ' |
| 8529 | 'arithmetic\n' |
| 8530 | ' operations ("+", "-", "*", "/", "%", "divmod()", "pow()", ' |
| 8531 | '"**",\n' |
| 8532 | ' "<<", ">>", "&", "^", "|") with reflected (swapped) ' |
| 8533 | 'operands.\n' |
| 8534 | ' These functions are only called if the left operand does ' |
| 8535 | 'not\n' |
| 8536 | ' support the corresponding operation and the operands are ' |
| 8537 | 'of\n' |
| 8538 | ' different types. [2] For instance, to evaluate the ' |
| 8539 | 'expression "x -\n' |
| 8540 | ' y", where *y* is an instance of a class that has an ' |
| 8541 | '"__rsub__()"\n' |
| 8542 | ' method, "y.__rsub__(x)" is called if "x.__sub__(y)" ' |
| 8543 | 'returns\n' |
| 8544 | ' *NotImplemented*.\n' |
| 8545 | '\n' |
| 8546 | ' Note that ternary "pow()" will not try calling ' |
| 8547 | '"__rpow__()" (the\n' |
| 8548 | ' coercion rules would become too complicated).\n' |
| 8549 | '\n' |
| 8550 | " Note: If the right operand's type is a subclass of the " |
| 8551 | 'left\n' |
| 8552 | " operand's type and that subclass provides the reflected " |
| 8553 | 'method\n' |
| 8554 | ' for the operation, this method will be called before ' |
| 8555 | 'the left\n' |
| 8556 | " operand's non-reflected method. This behavior allows " |
| 8557 | 'subclasses\n' |
| 8558 | " to override their ancestors' operations.\n" |
| 8559 | '\n' |
| 8560 | 'object.__iadd__(self, other)\n' |
| 8561 | 'object.__isub__(self, other)\n' |
| 8562 | 'object.__imul__(self, other)\n' |
| 8563 | 'object.__idiv__(self, other)\n' |
| 8564 | 'object.__itruediv__(self, other)\n' |
| 8565 | 'object.__ifloordiv__(self, other)\n' |
| 8566 | 'object.__imod__(self, other)\n' |
| 8567 | 'object.__ipow__(self, other[, modulo])\n' |
| 8568 | 'object.__ilshift__(self, other)\n' |
| 8569 | 'object.__irshift__(self, other)\n' |
| 8570 | 'object.__iand__(self, other)\n' |
| 8571 | 'object.__ixor__(self, other)\n' |
| 8572 | 'object.__ior__(self, other)\n' |
| 8573 | '\n' |
| 8574 | ' These methods are called to implement the augmented ' |
| 8575 | 'arithmetic\n' |
| 8576 | ' assignments ("+=", "-=", "*=", "/=", "//=", "%=", "**=", ' |
| 8577 | '"<<=",\n' |
| 8578 | ' ">>=", "&=", "^=", "|="). These methods should attempt ' |
| 8579 | 'to do the\n' |
| 8580 | ' operation in-place (modifying *self*) and return the ' |
| 8581 | 'result (which\n' |
| 8582 | ' could be, but does not have to be, *self*). If a ' |
| 8583 | 'specific method\n' |
| 8584 | ' is not defined, the augmented assignment falls back to ' |
| 8585 | 'the normal\n' |
| 8586 | ' methods. For instance, to execute the statement "x += ' |
| 8587 | 'y", where\n' |
| 8588 | ' *x* is an instance of a class that has an "__iadd__()" ' |
| 8589 | 'method,\n' |
| 8590 | ' "x.__iadd__(y)" is called. If *x* is an instance of a ' |
| 8591 | 'class that\n' |
| 8592 | ' does not define a "__iadd__()" method, "x.__add__(y)" ' |
| 8593 | 'and\n' |
| 8594 | ' "y.__radd__(x)" are considered, as with the evaluation of ' |
| 8595 | '"x + y".\n' |
| 8596 | '\n' |
| 8597 | 'object.__neg__(self)\n' |
| 8598 | 'object.__pos__(self)\n' |
| 8599 | 'object.__abs__(self)\n' |
| 8600 | 'object.__invert__(self)\n' |
| 8601 | '\n' |
| 8602 | ' Called to implement the unary arithmetic operations ("-", ' |
| 8603 | '"+",\n' |
| 8604 | ' "abs()" and "~").\n' |
| 8605 | '\n' |
| 8606 | 'object.__complex__(self)\n' |
| 8607 | 'object.__int__(self)\n' |
| 8608 | 'object.__long__(self)\n' |
| 8609 | 'object.__float__(self)\n' |
| 8610 | '\n' |
| 8611 | ' Called to implement the built-in functions "complex()", ' |
| 8612 | '"int()",\n' |
| 8613 | ' "long()", and "float()". Should return a value of the ' |
| 8614 | 'appropriate\n' |
| 8615 | ' type.\n' |
| 8616 | '\n' |
| 8617 | 'object.__oct__(self)\n' |
| 8618 | 'object.__hex__(self)\n' |
| 8619 | '\n' |
| 8620 | ' Called to implement the built-in functions "oct()" and ' |
| 8621 | '"hex()".\n' |
| 8622 | ' Should return a string value.\n' |
| 8623 | '\n' |
| 8624 | 'object.__index__(self)\n' |
| 8625 | '\n' |
| 8626 | ' Called to implement "operator.index()". Also called ' |
| 8627 | 'whenever\n' |
| 8628 | ' Python needs an integer object (such as in slicing). ' |
| 8629 | 'Must return\n' |
| 8630 | ' an integer (int or long).\n' |
| 8631 | '\n' |
| 8632 | ' New in version 2.5.\n' |
| 8633 | '\n' |
| 8634 | 'object.__coerce__(self, other)\n' |
| 8635 | '\n' |
| 8636 | ' Called to implement "mixed-mode" numeric arithmetic. ' |
| 8637 | 'Should either\n' |
| 8638 | ' return a 2-tuple containing *self* and *other* converted ' |
| 8639 | 'to a\n' |
| 8640 | ' common numeric type, or "None" if conversion is ' |
| 8641 | 'impossible. When\n' |
| 8642 | ' the common type would be the type of "other", it is ' |
| 8643 | 'sufficient to\n' |
| 8644 | ' return "None", since the interpreter will also ask the ' |
| 8645 | 'other object\n' |
| 8646 | ' to attempt a coercion (but sometimes, if the ' |
| 8647 | 'implementation of the\n' |
| 8648 | ' other type cannot be changed, it is useful to do the ' |
| 8649 | 'conversion to\n' |
| 8650 | ' the other type here). A return value of "NotImplemented" ' |
| 8651 | 'is\n' |
| 8652 | ' equivalent to returning "None".\n' |
| 8653 | '\n' |
| 8654 | '\n' |
| 8655 | 'Coercion rules\n' |
| 8656 | '==============\n' |
| 8657 | '\n' |
| 8658 | 'This section used to document the rules for coercion. As ' |
| 8659 | 'the language\n' |
| 8660 | 'has evolved, the coercion rules have become hard to ' |
| 8661 | 'document\n' |
| 8662 | 'precisely; documenting what one version of one particular\n' |
| 8663 | 'implementation does is undesirable. Instead, here are some ' |
| 8664 | 'informal\n' |
| 8665 | 'guidelines regarding coercion. In Python 3, coercion will ' |
| 8666 | 'not be\n' |
| 8667 | 'supported.\n' |
| 8668 | '\n' |
| 8669 | '* If the left operand of a % operator is a string or Unicode ' |
| 8670 | 'object,\n' |
| 8671 | ' no coercion takes place and the string formatting ' |
| 8672 | 'operation is\n' |
| 8673 | ' invoked instead.\n' |
| 8674 | '\n' |
| 8675 | '* It is no longer recommended to define a coercion ' |
| 8676 | 'operation. Mixed-\n' |
| 8677 | " mode operations on types that don't define coercion pass " |
| 8678 | 'the\n' |
| 8679 | ' original arguments to the operation.\n' |
| 8680 | '\n' |
| 8681 | '* New-style classes (those derived from "object") never ' |
| 8682 | 'invoke the\n' |
| 8683 | ' "__coerce__()" method in response to a binary operator; ' |
| 8684 | 'the only\n' |
| 8685 | ' time "__coerce__()" is invoked is when the built-in ' |
| 8686 | 'function\n' |
| 8687 | ' "coerce()" is called.\n' |
| 8688 | '\n' |
| 8689 | '* For most intents and purposes, an operator that returns\n' |
| 8690 | ' "NotImplemented" is treated the same as one that is not ' |
| 8691 | 'implemented\n' |
| 8692 | ' at all.\n' |
| 8693 | '\n' |
| 8694 | '* Below, "__op__()" and "__rop__()" are used to signify the ' |
| 8695 | 'generic\n' |
| 8696 | ' method names corresponding to an operator; "__iop__()" is ' |
| 8697 | 'used for\n' |
| 8698 | ' the corresponding in-place operator. For example, for the ' |
| 8699 | 'operator\n' |
| 8700 | ' \'"+"\', "__add__()" and "__radd__()" are used for the ' |
| 8701 | 'left and right\n' |
| 8702 | ' variant of the binary operator, and "__iadd__()" for the ' |
| 8703 | 'in-place\n' |
| 8704 | ' variant.\n' |
| 8705 | '\n' |
| 8706 | '* For objects *x* and *y*, first "x.__op__(y)" is tried. If ' |
| 8707 | 'this is\n' |
| 8708 | ' not implemented or returns "NotImplemented", ' |
| 8709 | '"y.__rop__(x)" is\n' |
| 8710 | ' tried. If this is also not implemented or returns ' |
| 8711 | '"NotImplemented",\n' |
| 8712 | ' a "TypeError" exception is raised. But see the following ' |
| 8713 | 'exception:\n' |
| 8714 | '\n' |
| 8715 | '* Exception to the previous item: if the left operand is an ' |
| 8716 | 'instance\n' |
| 8717 | ' of a built-in type or a new-style class, and the right ' |
| 8718 | 'operand is an\n' |
| 8719 | ' instance of a proper subclass of that type or class and ' |
| 8720 | 'overrides\n' |
| 8721 | ' the base\'s "__rop__()" method, the right operand\'s ' |
| 8722 | '"__rop__()"\n' |
| 8723 | ' method is tried *before* the left operand\'s "__op__()" ' |
| 8724 | 'method.\n' |
| 8725 | '\n' |
| 8726 | ' This is done so that a subclass can completely override ' |
| 8727 | 'binary\n' |
| 8728 | ' operators. Otherwise, the left operand\'s "__op__()" ' |
| 8729 | 'method would\n' |
| 8730 | ' always accept the right operand: when an instance of a ' |
| 8731 | 'given class\n' |
| 8732 | ' is expected, an instance of a subclass of that class is ' |
| 8733 | 'always\n' |
| 8734 | ' acceptable.\n' |
| 8735 | '\n' |
| 8736 | '* When either operand type defines a coercion, this coercion ' |
| 8737 | 'is\n' |
| 8738 | ' called before that type\'s "__op__()" or "__rop__()" ' |
| 8739 | 'method is\n' |
| 8740 | ' called, but no sooner. If the coercion returns an object ' |
| 8741 | 'of a\n' |
| 8742 | ' different type for the operand whose coercion is invoked, ' |
| 8743 | 'part of\n' |
| 8744 | ' the process is redone using the new object.\n' |
| 8745 | '\n' |
| 8746 | '* When an in-place operator (like \'"+="\') is used, if the ' |
| 8747 | 'left\n' |
| 8748 | ' operand implements "__iop__()", it is invoked without any ' |
| 8749 | 'coercion.\n' |
| 8750 | ' When the operation falls back to "__op__()" and/or ' |
| 8751 | '"__rop__()", the\n' |
| 8752 | ' normal coercion rules apply.\n' |
| 8753 | '\n' |
| 8754 | '* In "x + y", if *x* is a sequence that implements sequence\n' |
| 8755 | ' concatenation, sequence concatenation is invoked.\n' |
| 8756 | '\n' |
| 8757 | '* In "x * y", if one operand is a sequence that implements ' |
| 8758 | 'sequence\n' |
| 8759 | ' repetition, and the other is an integer ("int" or "long"), ' |
| 8760 | 'sequence\n' |
| 8761 | ' repetition is invoked.\n' |
| 8762 | '\n' |
| 8763 | '* Rich comparisons (implemented by methods "__eq__()" and so ' |
| 8764 | 'on)\n' |
| 8765 | ' never use coercion. Three-way comparison (implemented by\n' |
| 8766 | ' "__cmp__()") does use coercion under the same conditions ' |
| 8767 | 'as other\n' |
| 8768 | ' binary operations use it.\n' |
| 8769 | '\n' |
| 8770 | '* In the current implementation, the built-in numeric types ' |
| 8771 | '"int",\n' |
| 8772 | ' "long", "float", and "complex" do not use coercion. All ' |
| 8773 | 'these types\n' |
| 8774 | ' implement a "__coerce__()" method, for use by the ' |
| 8775 | 'built-in\n' |
| 8776 | ' "coerce()" function.\n' |
| 8777 | '\n' |
| 8778 | ' Changed in version 2.7: The complex type no longer makes ' |
| 8779 | 'implicit\n' |
| 8780 | ' calls to the "__coerce__()" method for mixed-type binary ' |
| 8781 | 'arithmetic\n' |
| 8782 | ' operations.\n' |
| 8783 | '\n' |
| 8784 | '\n' |
| 8785 | 'With Statement Context Managers\n' |
| 8786 | '===============================\n' |
| 8787 | '\n' |
| 8788 | 'New in version 2.5.\n' |
| 8789 | '\n' |
| 8790 | 'A *context manager* is an object that defines the runtime ' |
| 8791 | 'context to\n' |
| 8792 | 'be established when executing a "with" statement. The ' |
| 8793 | 'context manager\n' |
| 8794 | 'handles the entry into, and the exit from, the desired ' |
| 8795 | 'runtime context\n' |
| 8796 | 'for the execution of the block of code. Context managers ' |
| 8797 | 'are normally\n' |
| 8798 | 'invoked using the "with" statement (described in section The ' |
| 8799 | 'with\n' |
| 8800 | 'statement), but can also be used by directly invoking their ' |
| 8801 | 'methods.\n' |
| 8802 | '\n' |
| 8803 | 'Typical uses of context managers include saving and ' |
| 8804 | 'restoring various\n' |
| 8805 | 'kinds of global state, locking and unlocking resources, ' |
| 8806 | 'closing opened\n' |
| 8807 | 'files, etc.\n' |
| 8808 | '\n' |
| 8809 | 'For more information on context managers, see Context ' |
| 8810 | 'Manager Types.\n' |
| 8811 | '\n' |
| 8812 | 'object.__enter__(self)\n' |
| 8813 | '\n' |
| 8814 | ' Enter the runtime context related to this object. The ' |
| 8815 | '"with"\n' |
| 8816 | " statement will bind this method's return value to the " |
| 8817 | 'target(s)\n' |
| 8818 | ' specified in the "as" clause of the statement, if any.\n' |
| 8819 | '\n' |
| 8820 | 'object.__exit__(self, exc_type, exc_value, traceback)\n' |
| 8821 | '\n' |
| 8822 | ' Exit the runtime context related to this object. The ' |
| 8823 | 'parameters\n' |
| 8824 | ' describe the exception that caused the context to be ' |
| 8825 | 'exited. If the\n' |
| 8826 | ' context was exited without an exception, all three ' |
| 8827 | 'arguments will\n' |
| 8828 | ' be "None".\n' |
| 8829 | '\n' |
| 8830 | ' If an exception is supplied, and the method wishes to ' |
| 8831 | 'suppress the\n' |
| 8832 | ' exception (i.e., prevent it from being propagated), it ' |
| 8833 | 'should\n' |
| 8834 | ' return a true value. Otherwise, the exception will be ' |
| 8835 | 'processed\n' |
| 8836 | ' normally upon exit from this method.\n' |
| 8837 | '\n' |
| 8838 | ' Note that "__exit__()" methods should not reraise the ' |
| 8839 | 'passed-in\n' |
| 8840 | " exception; this is the caller's responsibility.\n" |
| 8841 | '\n' |
| 8842 | 'See also:\n' |
| 8843 | '\n' |
| 8844 | ' **PEP 343** - The "with" statement\n' |
| 8845 | ' The specification, background, and examples for the ' |
| 8846 | 'Python "with"\n' |
| 8847 | ' statement.\n' |
| 8848 | '\n' |
| 8849 | '\n' |
| 8850 | 'Special method lookup for old-style classes\n' |
| 8851 | '===========================================\n' |
| 8852 | '\n' |
| 8853 | 'For old-style classes, special methods are always looked up ' |
| 8854 | 'in exactly\n' |
| 8855 | 'the same way as any other method or attribute. This is the ' |
| 8856 | 'case\n' |
| 8857 | 'regardless of whether the method is being looked up ' |
| 8858 | 'explicitly as in\n' |
| 8859 | '"x.__getitem__(i)" or implicitly as in "x[i]".\n' |
| 8860 | '\n' |
| 8861 | 'This behaviour means that special methods may exhibit ' |
| 8862 | 'different\n' |
| 8863 | 'behaviour for different instances of a single old-style ' |
| 8864 | 'class if the\n' |
| 8865 | 'appropriate special attributes are set differently:\n' |
| 8866 | '\n' |
| 8867 | ' >>> class C:\n' |
| 8868 | ' ... pass\n' |
| 8869 | ' ...\n' |
| 8870 | ' >>> c1 = C()\n' |
| 8871 | ' >>> c2 = C()\n' |
| 8872 | ' >>> c1.__len__ = lambda: 5\n' |
| 8873 | ' >>> c2.__len__ = lambda: 9\n' |
| 8874 | ' >>> len(c1)\n' |
| 8875 | ' 5\n' |
| 8876 | ' >>> len(c2)\n' |
| 8877 | ' 9\n' |
| 8878 | '\n' |
| 8879 | '\n' |
| 8880 | 'Special method lookup for new-style classes\n' |
| 8881 | '===========================================\n' |
| 8882 | '\n' |
| 8883 | 'For new-style classes, implicit invocations of special ' |
| 8884 | 'methods are\n' |
| 8885 | "only guaranteed to work correctly if defined on an object's " |
| 8886 | 'type, not\n' |
| 8887 | "in the object's instance dictionary. That behaviour is the " |
| 8888 | 'reason why\n' |
| 8889 | 'the following code raises an exception (unlike the ' |
| 8890 | 'equivalent example\n' |
| 8891 | 'with old-style classes):\n' |
| 8892 | '\n' |
| 8893 | ' >>> class C(object):\n' |
| 8894 | ' ... pass\n' |
| 8895 | ' ...\n' |
| 8896 | ' >>> c = C()\n' |
| 8897 | ' >>> c.__len__ = lambda: 5\n' |
| 8898 | ' >>> len(c)\n' |
| 8899 | ' Traceback (most recent call last):\n' |
| 8900 | ' File "<stdin>", line 1, in <module>\n' |
| 8901 | " TypeError: object of type 'C' has no len()\n" |
| 8902 | '\n' |
| 8903 | 'The rationale behind this behaviour lies with a number of ' |
| 8904 | 'special\n' |
| 8905 | 'methods such as "__hash__()" and "__repr__()" that are ' |
| 8906 | 'implemented by\n' |
| 8907 | 'all objects, including type objects. If the implicit lookup ' |
| 8908 | 'of these\n' |
| 8909 | 'methods used the conventional lookup process, they would ' |
| 8910 | 'fail when\n' |
| 8911 | 'invoked on the type object itself:\n' |
| 8912 | '\n' |
| 8913 | ' >>> 1 .__hash__() == hash(1)\n' |
| 8914 | ' True\n' |
| 8915 | ' >>> int.__hash__() == hash(int)\n' |
| 8916 | ' Traceback (most recent call last):\n' |
| 8917 | ' File "<stdin>", line 1, in <module>\n' |
| 8918 | " TypeError: descriptor '__hash__' of 'int' object needs an " |
| 8919 | 'argument\n' |
| 8920 | '\n' |
| 8921 | 'Incorrectly attempting to invoke an unbound method of a ' |
| 8922 | 'class in this\n' |
| 8923 | "way is sometimes referred to as 'metaclass confusion', and " |
| 8924 | 'is avoided\n' |
| 8925 | 'by bypassing the instance when looking up special methods:\n' |
| 8926 | '\n' |
| 8927 | ' >>> type(1).__hash__(1) == hash(1)\n' |
| 8928 | ' True\n' |
| 8929 | ' >>> type(int).__hash__(int) == hash(int)\n' |
| 8930 | ' True\n' |
| 8931 | '\n' |
| 8932 | 'In addition to bypassing any instance attributes in the ' |
| 8933 | 'interest of\n' |
| 8934 | 'correctness, implicit special method lookup generally also ' |
| 8935 | 'bypasses\n' |
| 8936 | 'the "__getattribute__()" method even of the object\'s ' |
| 8937 | 'metaclass:\n' |
| 8938 | '\n' |
| 8939 | ' >>> class Meta(type):\n' |
| 8940 | ' ... def __getattribute__(*args):\n' |
| 8941 | ' ... print "Metaclass getattribute invoked"\n' |
| 8942 | ' ... return type.__getattribute__(*args)\n' |
| 8943 | ' ...\n' |
| 8944 | ' >>> class C(object):\n' |
| 8945 | ' ... __metaclass__ = Meta\n' |
| 8946 | ' ... def __len__(self):\n' |
| 8947 | ' ... return 10\n' |
| 8948 | ' ... def __getattribute__(*args):\n' |
| 8949 | ' ... print "Class getattribute invoked"\n' |
| 8950 | ' ... return object.__getattribute__(*args)\n' |
| 8951 | ' ...\n' |
| 8952 | ' >>> c = C()\n' |
| 8953 | ' >>> c.__len__() # Explicit lookup via ' |
| 8954 | 'instance\n' |
| 8955 | ' Class getattribute invoked\n' |
| 8956 | ' 10\n' |
| 8957 | ' >>> type(c).__len__(c) # Explicit lookup via ' |
| 8958 | 'type\n' |
| 8959 | ' Metaclass getattribute invoked\n' |
| 8960 | ' 10\n' |
| 8961 | ' >>> len(c) # Implicit lookup\n' |
| 8962 | ' 10\n' |
| 8963 | '\n' |
| 8964 | 'Bypassing the "__getattribute__()" machinery in this fashion ' |
| 8965 | 'provides\n' |
| 8966 | 'significant scope for speed optimisations within the ' |
| 8967 | 'interpreter, at\n' |
| 8968 | 'the cost of some flexibility in the handling of special ' |
| 8969 | 'methods (the\n' |
| 8970 | 'special method *must* be set on the class object itself in ' |
| 8971 | 'order to be\n' |
| 8972 | 'consistently invoked by the interpreter).\n' |
| 8973 | '\n' |
| 8974 | '-[ Footnotes ]-\n' |
| 8975 | '\n' |
| 8976 | "[1] It *is* possible in some cases to change an object's " |
| 8977 | 'type,\n' |
| 8978 | " under certain controlled conditions. It generally isn't " |
| 8979 | 'a good\n' |
| 8980 | ' idea though, since it can lead to some very strange ' |
| 8981 | 'behaviour if\n' |
| 8982 | ' it is handled incorrectly.\n' |
| 8983 | '\n' |
| 8984 | '[2] For operands of the same type, it is assumed that if the ' |
| 8985 | 'non-\n' |
| 8986 | ' reflected method (such as "__add__()") fails the ' |
| 8987 | 'operation is not\n' |
| 8988 | ' supported, which is why the reflected method is not ' |
| 8989 | 'called.\n', |
| 8990 | 'string-methods': '\n' |
| 8991 | 'String Methods\n' |
| 8992 | '**************\n' |
| 8993 | '\n' |
| 8994 | 'Below are listed the string methods which both 8-bit ' |
| 8995 | 'strings and\n' |
| 8996 | 'Unicode objects support. Some of them are also available ' |
| 8997 | 'on\n' |
| 8998 | '"bytearray" objects.\n' |
| 8999 | '\n' |
| 9000 | "In addition, Python's strings support the sequence type " |
| 9001 | 'methods\n' |
| 9002 | 'described in the Sequence Types --- str, unicode, list, ' |
| 9003 | 'tuple,\n' |
| 9004 | 'bytearray, buffer, xrange section. To output formatted ' |
| 9005 | 'strings use\n' |
| 9006 | 'template strings or the "%" operator described in the ' |
| 9007 | 'String\n' |
| 9008 | 'Formatting Operations section. Also, see the "re" module ' |
| 9009 | 'for string\n' |
| 9010 | 'functions based on regular expressions.\n' |
| 9011 | '\n' |
| 9012 | 'str.capitalize()\n' |
| 9013 | '\n' |
| 9014 | ' Return a copy of the string with its first character ' |
| 9015 | 'capitalized\n' |
| 9016 | ' and the rest lowercased.\n' |
| 9017 | '\n' |
| 9018 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9019 | '\n' |
| 9020 | 'str.center(width[, fillchar])\n' |
| 9021 | '\n' |
| 9022 | ' Return centered in a string of length *width*. Padding ' |
| 9023 | 'is done\n' |
| 9024 | ' using the specified *fillchar* (default is a space).\n' |
| 9025 | '\n' |
| 9026 | ' Changed in version 2.4: Support for the *fillchar* ' |
| 9027 | 'argument.\n' |
| 9028 | '\n' |
| 9029 | 'str.count(sub[, start[, end]])\n' |
| 9030 | '\n' |
| 9031 | ' Return the number of non-overlapping occurrences of ' |
| 9032 | 'substring *sub*\n' |
| 9033 | ' in the range [*start*, *end*]. Optional arguments ' |
| 9034 | '*start* and\n' |
| 9035 | ' *end* are interpreted as in slice notation.\n' |
| 9036 | '\n' |
| 9037 | 'str.decode([encoding[, errors]])\n' |
| 9038 | '\n' |
| 9039 | ' Decodes the string using the codec registered for ' |
| 9040 | '*encoding*.\n' |
| 9041 | ' *encoding* defaults to the default string encoding. ' |
| 9042 | '*errors* may\n' |
| 9043 | ' be given to set a different error handling scheme. The ' |
| 9044 | 'default is\n' |
| 9045 | ' "\'strict\'", meaning that encoding errors raise ' |
| 9046 | '"UnicodeError".\n' |
| 9047 | ' Other possible values are "\'ignore\'", "\'replace\'" ' |
| 9048 | 'and any other\n' |
| 9049 | ' name registered via "codecs.register_error()", see ' |
| 9050 | 'section Codec\n' |
| 9051 | ' Base Classes.\n' |
| 9052 | '\n' |
| 9053 | ' New in version 2.2.\n' |
| 9054 | '\n' |
| 9055 | ' Changed in version 2.3: Support for other error ' |
| 9056 | 'handling schemes\n' |
| 9057 | ' added.\n' |
| 9058 | '\n' |
| 9059 | ' Changed in version 2.7: Support for keyword arguments ' |
| 9060 | 'added.\n' |
| 9061 | '\n' |
| 9062 | 'str.encode([encoding[, errors]])\n' |
| 9063 | '\n' |
| 9064 | ' Return an encoded version of the string. Default ' |
| 9065 | 'encoding is the\n' |
| 9066 | ' current default string encoding. *errors* may be given ' |
| 9067 | 'to set a\n' |
| 9068 | ' different error handling scheme. The default for ' |
| 9069 | '*errors* is\n' |
| 9070 | ' "\'strict\'", meaning that encoding errors raise a ' |
| 9071 | '"UnicodeError".\n' |
| 9072 | ' Other possible values are "\'ignore\'", "\'replace\'",\n' |
| 9073 | ' "\'xmlcharrefreplace\'", "\'backslashreplace\'" and any ' |
| 9074 | 'other name\n' |
| 9075 | ' registered via "codecs.register_error()", see section ' |
| 9076 | 'Codec Base\n' |
| 9077 | ' Classes. For a list of possible encodings, see section ' |
| 9078 | 'Standard\n' |
| 9079 | ' Encodings.\n' |
| 9080 | '\n' |
| 9081 | ' New in version 2.0.\n' |
| 9082 | '\n' |
| 9083 | ' Changed in version 2.3: Support for ' |
| 9084 | '"\'xmlcharrefreplace\'" and\n' |
| 9085 | ' "\'backslashreplace\'" and other error handling schemes ' |
| 9086 | 'added.\n' |
| 9087 | '\n' |
| 9088 | ' Changed in version 2.7: Support for keyword arguments ' |
| 9089 | 'added.\n' |
| 9090 | '\n' |
| 9091 | 'str.endswith(suffix[, start[, end]])\n' |
| 9092 | '\n' |
| 9093 | ' Return "True" if the string ends with the specified ' |
| 9094 | '*suffix*,\n' |
| 9095 | ' otherwise return "False". *suffix* can also be a tuple ' |
| 9096 | 'of suffixes\n' |
| 9097 | ' to look for. With optional *start*, test beginning at ' |
| 9098 | 'that\n' |
| 9099 | ' position. With optional *end*, stop comparing at that ' |
| 9100 | 'position.\n' |
| 9101 | '\n' |
| 9102 | ' Changed in version 2.5: Accept tuples as *suffix*.\n' |
| 9103 | '\n' |
| 9104 | 'str.expandtabs([tabsize])\n' |
| 9105 | '\n' |
| 9106 | ' Return a copy of the string where all tab characters ' |
| 9107 | 'are replaced\n' |
| 9108 | ' by one or more spaces, depending on the current column ' |
| 9109 | 'and the\n' |
| 9110 | ' given tab size. Tab positions occur every *tabsize* ' |
| 9111 | 'characters\n' |
| 9112 | ' (default is 8, giving tab positions at columns 0, 8, 16 ' |
| 9113 | 'and so on).\n' |
| 9114 | ' To expand the string, the current column is set to zero ' |
| 9115 | 'and the\n' |
| 9116 | ' string is examined character by character. If the ' |
| 9117 | 'character is a\n' |
| 9118 | ' tab ("\\t"), one or more space characters are inserted ' |
| 9119 | 'in the result\n' |
| 9120 | ' until the current column is equal to the next tab ' |
| 9121 | 'position. (The\n' |
| 9122 | ' tab character itself is not copied.) If the character ' |
| 9123 | 'is a newline\n' |
| 9124 | ' ("\\n") or return ("\\r"), it is copied and the current ' |
| 9125 | 'column is\n' |
| 9126 | ' reset to zero. Any other character is copied unchanged ' |
| 9127 | 'and the\n' |
| 9128 | ' current column is incremented by one regardless of how ' |
| 9129 | 'the\n' |
| 9130 | ' character is represented when printed.\n' |
| 9131 | '\n' |
| 9132 | " >>> '01\\t012\\t0123\\t01234'.expandtabs()\n" |
| 9133 | " '01 012 0123 01234'\n" |
| 9134 | " >>> '01\\t012\\t0123\\t01234'.expandtabs(4)\n" |
| 9135 | " '01 012 0123 01234'\n" |
| 9136 | '\n' |
| 9137 | 'str.find(sub[, start[, end]])\n' |
| 9138 | '\n' |
| 9139 | ' Return the lowest index in the string where substring ' |
| 9140 | '*sub* is\n' |
| 9141 | ' found within the slice "s[start:end]". Optional ' |
| 9142 | 'arguments *start*\n' |
| 9143 | ' and *end* are interpreted as in slice notation. Return ' |
| 9144 | '"-1" if\n' |
| 9145 | ' *sub* is not found.\n' |
| 9146 | '\n' |
| 9147 | ' Note: The "find()" method should be used only if you ' |
| 9148 | 'need to know\n' |
| 9149 | ' the position of *sub*. To check if *sub* is a ' |
| 9150 | 'substring or not,\n' |
| 9151 | ' use the "in" operator:\n' |
| 9152 | '\n' |
| 9153 | " >>> 'Py' in 'Python'\n" |
| 9154 | ' True\n' |
| 9155 | '\n' |
| 9156 | 'str.format(*args, **kwargs)\n' |
| 9157 | '\n' |
| 9158 | ' Perform a string formatting operation. The string on ' |
| 9159 | 'which this\n' |
| 9160 | ' method is called can contain literal text or ' |
| 9161 | 'replacement fields\n' |
| 9162 | ' delimited by braces "{}". Each replacement field ' |
| 9163 | 'contains either\n' |
| 9164 | ' the numeric index of a positional argument, or the name ' |
| 9165 | 'of a\n' |
| 9166 | ' keyword argument. Returns a copy of the string where ' |
| 9167 | 'each\n' |
| 9168 | ' replacement field is replaced with the string value of ' |
| 9169 | 'the\n' |
| 9170 | ' corresponding argument.\n' |
| 9171 | '\n' |
| 9172 | ' >>> "The sum of 1 + 2 is {0}".format(1+2)\n' |
| 9173 | " 'The sum of 1 + 2 is 3'\n" |
| 9174 | '\n' |
| 9175 | ' See Format String Syntax for a description of the ' |
| 9176 | 'various\n' |
| 9177 | ' formatting options that can be specified in format ' |
| 9178 | 'strings.\n' |
| 9179 | '\n' |
| 9180 | ' This method of string formatting is the new standard in ' |
| 9181 | 'Python 3,\n' |
| 9182 | ' and should be preferred to the "%" formatting described ' |
| 9183 | 'in String\n' |
| 9184 | ' Formatting Operations in new code.\n' |
| 9185 | '\n' |
| 9186 | ' New in version 2.6.\n' |
| 9187 | '\n' |
| 9188 | 'str.index(sub[, start[, end]])\n' |
| 9189 | '\n' |
| 9190 | ' Like "find()", but raise "ValueError" when the ' |
| 9191 | 'substring is not\n' |
| 9192 | ' found.\n' |
| 9193 | '\n' |
| 9194 | 'str.isalnum()\n' |
| 9195 | '\n' |
| 9196 | ' Return true if all characters in the string are ' |
| 9197 | 'alphanumeric and\n' |
| 9198 | ' there is at least one character, false otherwise.\n' |
| 9199 | '\n' |
| 9200 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9201 | '\n' |
| 9202 | 'str.isalpha()\n' |
| 9203 | '\n' |
| 9204 | ' Return true if all characters in the string are ' |
| 9205 | 'alphabetic and\n' |
| 9206 | ' there is at least one character, false otherwise.\n' |
| 9207 | '\n' |
| 9208 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9209 | '\n' |
| 9210 | 'str.isdigit()\n' |
| 9211 | '\n' |
| 9212 | ' Return true if all characters in the string are digits ' |
| 9213 | 'and there is\n' |
| 9214 | ' at least one character, false otherwise.\n' |
| 9215 | '\n' |
| 9216 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9217 | '\n' |
| 9218 | 'str.islower()\n' |
| 9219 | '\n' |
| 9220 | ' Return true if all cased characters [4] in the string ' |
| 9221 | 'are lowercase\n' |
| 9222 | ' and there is at least one cased character, false ' |
| 9223 | 'otherwise.\n' |
| 9224 | '\n' |
| 9225 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9226 | '\n' |
| 9227 | 'str.isspace()\n' |
| 9228 | '\n' |
| 9229 | ' Return true if there are only whitespace characters in ' |
| 9230 | 'the string\n' |
| 9231 | ' and there is at least one character, false otherwise.\n' |
| 9232 | '\n' |
| 9233 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9234 | '\n' |
| 9235 | 'str.istitle()\n' |
| 9236 | '\n' |
| 9237 | ' Return true if the string is a titlecased string and ' |
| 9238 | 'there is at\n' |
| 9239 | ' least one character, for example uppercase characters ' |
| 9240 | 'may only\n' |
| 9241 | ' follow uncased characters and lowercase characters only ' |
| 9242 | 'cased ones.\n' |
| 9243 | ' Return false otherwise.\n' |
| 9244 | '\n' |
| 9245 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9246 | '\n' |
| 9247 | 'str.isupper()\n' |
| 9248 | '\n' |
| 9249 | ' Return true if all cased characters [4] in the string ' |
| 9250 | 'are uppercase\n' |
| 9251 | ' and there is at least one cased character, false ' |
| 9252 | 'otherwise.\n' |
| 9253 | '\n' |
| 9254 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9255 | '\n' |
| 9256 | 'str.join(iterable)\n' |
| 9257 | '\n' |
| 9258 | ' Return a string which is the concatenation of the ' |
| 9259 | 'strings in\n' |
| 9260 | ' *iterable*. A "TypeError" will be raised if there are ' |
| 9261 | 'any non-\n' |
| 9262 | ' string values in *iterable*, including "bytes" ' |
| 9263 | 'objects. The\n' |
| 9264 | ' separator between elements is the string providing this ' |
| 9265 | 'method.\n' |
| 9266 | '\n' |
| 9267 | 'str.ljust(width[, fillchar])\n' |
| 9268 | '\n' |
| 9269 | ' Return the string left justified in a string of length ' |
| 9270 | '*width*.\n' |
| 9271 | ' Padding is done using the specified *fillchar* (default ' |
| 9272 | 'is a\n' |
| 9273 | ' space). The original string is returned if *width* is ' |
| 9274 | 'less than or\n' |
| 9275 | ' equal to "len(s)".\n' |
| 9276 | '\n' |
| 9277 | ' Changed in version 2.4: Support for the *fillchar* ' |
| 9278 | 'argument.\n' |
| 9279 | '\n' |
| 9280 | 'str.lower()\n' |
| 9281 | '\n' |
| 9282 | ' Return a copy of the string with all the cased ' |
| 9283 | 'characters [4]\n' |
| 9284 | ' converted to lowercase.\n' |
| 9285 | '\n' |
| 9286 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9287 | '\n' |
| 9288 | 'str.lstrip([chars])\n' |
| 9289 | '\n' |
| 9290 | ' Return a copy of the string with leading characters ' |
| 9291 | 'removed. The\n' |
| 9292 | ' *chars* argument is a string specifying the set of ' |
| 9293 | 'characters to be\n' |
| 9294 | ' removed. If omitted or "None", the *chars* argument ' |
| 9295 | 'defaults to\n' |
| 9296 | ' removing whitespace. The *chars* argument is not a ' |
| 9297 | 'prefix; rather,\n' |
| 9298 | ' all combinations of its values are stripped:\n' |
| 9299 | '\n' |
| 9300 | " >>> ' spacious '.lstrip()\n" |
| 9301 | " 'spacious '\n" |
| 9302 | " >>> 'www.example.com'.lstrip('cmowz.')\n" |
| 9303 | " 'example.com'\n" |
| 9304 | '\n' |
| 9305 | ' Changed in version 2.2.2: Support for the *chars* ' |
| 9306 | 'argument.\n' |
| 9307 | '\n' |
| 9308 | 'str.partition(sep)\n' |
| 9309 | '\n' |
| 9310 | ' Split the string at the first occurrence of *sep*, and ' |
| 9311 | 'return a\n' |
| 9312 | ' 3-tuple containing the part before the separator, the ' |
| 9313 | 'separator\n' |
| 9314 | ' itself, and the part after the separator. If the ' |
| 9315 | 'separator is not\n' |
| 9316 | ' found, return a 3-tuple containing the string itself, ' |
| 9317 | 'followed by\n' |
| 9318 | ' two empty strings.\n' |
| 9319 | '\n' |
| 9320 | ' New in version 2.5.\n' |
| 9321 | '\n' |
| 9322 | 'str.replace(old, new[, count])\n' |
| 9323 | '\n' |
| 9324 | ' Return a copy of the string with all occurrences of ' |
| 9325 | 'substring *old*\n' |
| 9326 | ' replaced by *new*. If the optional argument *count* is ' |
| 9327 | 'given, only\n' |
| 9328 | ' the first *count* occurrences are replaced.\n' |
| 9329 | '\n' |
| 9330 | 'str.rfind(sub[, start[, end]])\n' |
| 9331 | '\n' |
| 9332 | ' Return the highest index in the string where substring ' |
| 9333 | '*sub* is\n' |
| 9334 | ' found, such that *sub* is contained within ' |
| 9335 | '"s[start:end]".\n' |
| 9336 | ' Optional arguments *start* and *end* are interpreted as ' |
| 9337 | 'in slice\n' |
| 9338 | ' notation. Return "-1" on failure.\n' |
| 9339 | '\n' |
| 9340 | 'str.rindex(sub[, start[, end]])\n' |
| 9341 | '\n' |
| 9342 | ' Like "rfind()" but raises "ValueError" when the ' |
| 9343 | 'substring *sub* is\n' |
| 9344 | ' not found.\n' |
| 9345 | '\n' |
| 9346 | 'str.rjust(width[, fillchar])\n' |
| 9347 | '\n' |
| 9348 | ' Return the string right justified in a string of length ' |
| 9349 | '*width*.\n' |
| 9350 | ' Padding is done using the specified *fillchar* (default ' |
| 9351 | 'is a\n' |
| 9352 | ' space). The original string is returned if *width* is ' |
| 9353 | 'less than or\n' |
| 9354 | ' equal to "len(s)".\n' |
| 9355 | '\n' |
| 9356 | ' Changed in version 2.4: Support for the *fillchar* ' |
| 9357 | 'argument.\n' |
| 9358 | '\n' |
| 9359 | 'str.rpartition(sep)\n' |
| 9360 | '\n' |
| 9361 | ' Split the string at the last occurrence of *sep*, and ' |
| 9362 | 'return a\n' |
| 9363 | ' 3-tuple containing the part before the separator, the ' |
| 9364 | 'separator\n' |
| 9365 | ' itself, and the part after the separator. If the ' |
| 9366 | 'separator is not\n' |
| 9367 | ' found, return a 3-tuple containing two empty strings, ' |
| 9368 | 'followed by\n' |
| 9369 | ' the string itself.\n' |
| 9370 | '\n' |
| 9371 | ' New in version 2.5.\n' |
| 9372 | '\n' |
| 9373 | 'str.rsplit([sep[, maxsplit]])\n' |
| 9374 | '\n' |
| 9375 | ' Return a list of the words in the string, using *sep* ' |
| 9376 | 'as the\n' |
| 9377 | ' delimiter string. If *maxsplit* is given, at most ' |
| 9378 | '*maxsplit* splits\n' |
| 9379 | ' are done, the *rightmost* ones. If *sep* is not ' |
| 9380 | 'specified or\n' |
| 9381 | ' "None", any whitespace string is a separator. Except ' |
| 9382 | 'for splitting\n' |
| 9383 | ' from the right, "rsplit()" behaves like "split()" which ' |
| 9384 | 'is\n' |
| 9385 | ' described in detail below.\n' |
| 9386 | '\n' |
| 9387 | ' New in version 2.4.\n' |
| 9388 | '\n' |
| 9389 | 'str.rstrip([chars])\n' |
| 9390 | '\n' |
| 9391 | ' Return a copy of the string with trailing characters ' |
| 9392 | 'removed. The\n' |
| 9393 | ' *chars* argument is a string specifying the set of ' |
| 9394 | 'characters to be\n' |
| 9395 | ' removed. If omitted or "None", the *chars* argument ' |
| 9396 | 'defaults to\n' |
| 9397 | ' removing whitespace. The *chars* argument is not a ' |
| 9398 | 'suffix; rather,\n' |
| 9399 | ' all combinations of its values are stripped:\n' |
| 9400 | '\n' |
| 9401 | " >>> ' spacious '.rstrip()\n" |
| 9402 | " ' spacious'\n" |
| 9403 | " >>> 'mississippi'.rstrip('ipz')\n" |
| 9404 | " 'mississ'\n" |
| 9405 | '\n' |
| 9406 | ' Changed in version 2.2.2: Support for the *chars* ' |
| 9407 | 'argument.\n' |
| 9408 | '\n' |
| 9409 | 'str.split([sep[, maxsplit]])\n' |
| 9410 | '\n' |
| 9411 | ' Return a list of the words in the string, using *sep* ' |
| 9412 | 'as the\n' |
| 9413 | ' delimiter string. If *maxsplit* is given, at most ' |
| 9414 | '*maxsplit*\n' |
| 9415 | ' splits are done (thus, the list will have at most ' |
| 9416 | '"maxsplit+1"\n' |
| 9417 | ' elements). If *maxsplit* is not specified or "-1", ' |
| 9418 | 'then there is\n' |
| 9419 | ' no limit on the number of splits (all possible splits ' |
| 9420 | 'are made).\n' |
| 9421 | '\n' |
| 9422 | ' If *sep* is given, consecutive delimiters are not ' |
| 9423 | 'grouped together\n' |
| 9424 | ' and are deemed to delimit empty strings (for example,\n' |
| 9425 | ' "\'1,,2\'.split(\',\')" returns "[\'1\', \'\', ' |
| 9426 | '\'2\']"). The *sep* argument\n' |
| 9427 | ' may consist of multiple characters (for example,\n' |
| 9428 | ' "\'1<>2<>3\'.split(\'<>\')" returns "[\'1\', \'2\', ' |
| 9429 | '\'3\']"). Splitting an\n' |
| 9430 | ' empty string with a specified separator returns ' |
| 9431 | '"[\'\']".\n' |
| 9432 | '\n' |
| 9433 | ' If *sep* is not specified or is "None", a different ' |
| 9434 | 'splitting\n' |
| 9435 | ' algorithm is applied: runs of consecutive whitespace ' |
| 9436 | 'are regarded\n' |
| 9437 | ' as a single separator, and the result will contain no ' |
| 9438 | 'empty strings\n' |
| 9439 | ' at the start or end if the string has leading or ' |
| 9440 | 'trailing\n' |
| 9441 | ' whitespace. Consequently, splitting an empty string or ' |
| 9442 | 'a string\n' |
| 9443 | ' consisting of just whitespace with a "None" separator ' |
| 9444 | 'returns "[]".\n' |
| 9445 | '\n' |
| 9446 | ' For example, "\' 1 2 3 \'.split()" returns "[\'1\', ' |
| 9447 | '\'2\', \'3\']", and\n' |
| 9448 | ' "\' 1 2 3 \'.split(None, 1)" returns "[\'1\', ' |
| 9449 | '\'2 3 \']".\n' |
| 9450 | '\n' |
| 9451 | 'str.splitlines([keepends])\n' |
| 9452 | '\n' |
| 9453 | ' Return a list of the lines in the string, breaking at ' |
| 9454 | 'line\n' |
| 9455 | ' boundaries. This method uses the *universal newlines* ' |
| 9456 | 'approach to\n' |
| 9457 | ' splitting lines. Line breaks are not included in the ' |
| 9458 | 'resulting list\n' |
| 9459 | ' unless *keepends* is given and true.\n' |
| 9460 | '\n' |
| 9461 | ' Python recognizes ""\\r"", ""\\n"", and ""\\r\\n"" as ' |
| 9462 | 'line boundaries\n' |
| 9463 | ' for 8-bit strings.\n' |
| 9464 | '\n' |
| 9465 | ' For example:\n' |
| 9466 | '\n' |
| 9467 | " >>> 'ab c\\n\\nde fg\\rkl\\r\\n'.splitlines()\n" |
| 9468 | " ['ab c', '', 'de fg', 'kl']\n" |
| 9469 | " >>> 'ab c\\n\\nde fg\\rkl\\r\\n'.splitlines(True)\n" |
| 9470 | " ['ab c\\n', '\\n', 'de fg\\r', 'kl\\r\\n']\n" |
| 9471 | '\n' |
| 9472 | ' Unlike "split()" when a delimiter string *sep* is ' |
| 9473 | 'given, this\n' |
| 9474 | ' method returns an empty list for the empty string, and ' |
| 9475 | 'a terminal\n' |
| 9476 | ' line break does not result in an extra line:\n' |
| 9477 | '\n' |
| 9478 | ' >>> "".splitlines()\n' |
| 9479 | ' []\n' |
| 9480 | ' >>> "One line\\n".splitlines()\n' |
| 9481 | " ['One line']\n" |
| 9482 | '\n' |
| 9483 | ' For comparison, "split(\'\\n\')" gives:\n' |
| 9484 | '\n' |
| 9485 | " >>> ''.split('\\n')\n" |
| 9486 | " ['']\n" |
| 9487 | " >>> 'Two lines\\n'.split('\\n')\n" |
| 9488 | " ['Two lines', '']\n" |
| 9489 | '\n' |
| 9490 | 'unicode.splitlines([keepends])\n' |
| 9491 | '\n' |
| 9492 | ' Return a list of the lines in the string, like ' |
| 9493 | '"str.splitlines()".\n' |
| 9494 | ' However, the Unicode method splits on the following ' |
| 9495 | 'line\n' |
| 9496 | ' boundaries, which are a superset of the *universal ' |
| 9497 | 'newlines*\n' |
| 9498 | ' recognized for 8-bit strings.\n' |
| 9499 | '\n' |
| 9500 | ' ' |
| 9501 | '+-------------------------+-------------------------------+\n' |
| 9502 | ' | Representation | ' |
| 9503 | 'Description |\n' |
| 9504 | ' ' |
| 9505 | '+=========================+===============================+\n' |
| 9506 | ' | "\\n" | Line ' |
| 9507 | 'Feed |\n' |
| 9508 | ' ' |
| 9509 | '+-------------------------+-------------------------------+\n' |
| 9510 | ' | "\\r" | Carriage ' |
| 9511 | 'Return |\n' |
| 9512 | ' ' |
| 9513 | '+-------------------------+-------------------------------+\n' |
| 9514 | ' | "\\r\\n" | Carriage Return + Line ' |
| 9515 | 'Feed |\n' |
| 9516 | ' ' |
| 9517 | '+-------------------------+-------------------------------+\n' |
| 9518 | ' | "\\v" or "\\x0b" | Line ' |
| 9519 | 'Tabulation |\n' |
| 9520 | ' ' |
| 9521 | '+-------------------------+-------------------------------+\n' |
| 9522 | ' | "\\f" or "\\x0c" | Form ' |
| 9523 | 'Feed |\n' |
| 9524 | ' ' |
| 9525 | '+-------------------------+-------------------------------+\n' |
| 9526 | ' | "\\x1c" | File ' |
| 9527 | 'Separator |\n' |
| 9528 | ' ' |
| 9529 | '+-------------------------+-------------------------------+\n' |
| 9530 | ' | "\\x1d" | Group ' |
| 9531 | 'Separator |\n' |
| 9532 | ' ' |
| 9533 | '+-------------------------+-------------------------------+\n' |
| 9534 | ' | "\\x1e" | Record ' |
| 9535 | 'Separator |\n' |
| 9536 | ' ' |
| 9537 | '+-------------------------+-------------------------------+\n' |
| 9538 | ' | "\\x85" | Next Line (C1 Control ' |
| 9539 | 'Code) |\n' |
| 9540 | ' ' |
| 9541 | '+-------------------------+-------------------------------+\n' |
| 9542 | ' | "\\u2028" | Line ' |
| 9543 | 'Separator |\n' |
| 9544 | ' ' |
| 9545 | '+-------------------------+-------------------------------+\n' |
| 9546 | ' | "\\u2029" | Paragraph ' |
| 9547 | 'Separator |\n' |
| 9548 | ' ' |
| 9549 | '+-------------------------+-------------------------------+\n' |
| 9550 | '\n' |
| 9551 | ' Changed in version 2.7: "\\v" and "\\f" added to list ' |
| 9552 | 'of line\n' |
| 9553 | ' boundaries.\n' |
| 9554 | '\n' |
| 9555 | 'str.startswith(prefix[, start[, end]])\n' |
| 9556 | '\n' |
| 9557 | ' Return "True" if string starts with the *prefix*, ' |
| 9558 | 'otherwise return\n' |
| 9559 | ' "False". *prefix* can also be a tuple of prefixes to ' |
| 9560 | 'look for.\n' |
| 9561 | ' With optional *start*, test string beginning at that ' |
| 9562 | 'position.\n' |
| 9563 | ' With optional *end*, stop comparing string at that ' |
| 9564 | 'position.\n' |
| 9565 | '\n' |
| 9566 | ' Changed in version 2.5: Accept tuples as *prefix*.\n' |
| 9567 | '\n' |
| 9568 | 'str.strip([chars])\n' |
| 9569 | '\n' |
| 9570 | ' Return a copy of the string with the leading and ' |
| 9571 | 'trailing\n' |
| 9572 | ' characters removed. The *chars* argument is a string ' |
| 9573 | 'specifying the\n' |
| 9574 | ' set of characters to be removed. If omitted or "None", ' |
| 9575 | 'the *chars*\n' |
| 9576 | ' argument defaults to removing whitespace. The *chars* ' |
| 9577 | 'argument is\n' |
| 9578 | ' not a prefix or suffix; rather, all combinations of its ' |
| 9579 | 'values are\n' |
| 9580 | ' stripped:\n' |
| 9581 | '\n' |
| 9582 | " >>> ' spacious '.strip()\n" |
| 9583 | " 'spacious'\n" |
| 9584 | " >>> 'www.example.com'.strip('cmowz.')\n" |
| 9585 | " 'example'\n" |
| 9586 | '\n' |
| 9587 | ' Changed in version 2.2.2: Support for the *chars* ' |
| 9588 | 'argument.\n' |
| 9589 | '\n' |
| 9590 | 'str.swapcase()\n' |
| 9591 | '\n' |
| 9592 | ' Return a copy of the string with uppercase characters ' |
| 9593 | 'converted to\n' |
| 9594 | ' lowercase and vice versa.\n' |
| 9595 | '\n' |
| 9596 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9597 | '\n' |
| 9598 | 'str.title()\n' |
| 9599 | '\n' |
| 9600 | ' Return a titlecased version of the string where words ' |
| 9601 | 'start with an\n' |
| 9602 | ' uppercase character and the remaining characters are ' |
| 9603 | 'lowercase.\n' |
| 9604 | '\n' |
| 9605 | ' The algorithm uses a simple language-independent ' |
| 9606 | 'definition of a\n' |
| 9607 | ' word as groups of consecutive letters. The definition ' |
| 9608 | 'works in\n' |
| 9609 | ' many contexts but it means that apostrophes in ' |
| 9610 | 'contractions and\n' |
| 9611 | ' possessives form word boundaries, which may not be the ' |
| 9612 | 'desired\n' |
| 9613 | ' result:\n' |
| 9614 | '\n' |
| 9615 | ' >>> "they\'re bill\'s friends from the UK".title()\n' |
| 9616 | ' "They\'Re Bill\'S Friends From The Uk"\n' |
| 9617 | '\n' |
| 9618 | ' A workaround for apostrophes can be constructed using ' |
| 9619 | 'regular\n' |
| 9620 | ' expressions:\n' |
| 9621 | '\n' |
| 9622 | ' >>> import re\n' |
| 9623 | ' >>> def titlecase(s):\n' |
| 9624 | ' ... return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n' |
| 9625 | ' ... lambda mo: ' |
| 9626 | 'mo.group(0)[0].upper() +\n' |
| 9627 | ' ... ' |
| 9628 | 'mo.group(0)[1:].lower(),\n' |
| 9629 | ' ... s)\n' |
| 9630 | ' ...\n' |
| 9631 | ' >>> titlecase("they\'re bill\'s friends.")\n' |
| 9632 | ' "They\'re Bill\'s Friends."\n' |
| 9633 | '\n' |
| 9634 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9635 | '\n' |
| 9636 | 'str.translate(table[, deletechars])\n' |
| 9637 | '\n' |
| 9638 | ' Return a copy of the string where all characters ' |
| 9639 | 'occurring in the\n' |
| 9640 | ' optional argument *deletechars* are removed, and the ' |
| 9641 | 'remaining\n' |
| 9642 | ' characters have been mapped through the given ' |
| 9643 | 'translation table,\n' |
| 9644 | ' which must be a string of length 256.\n' |
| 9645 | '\n' |
| 9646 | ' You can use the "maketrans()" helper function in the ' |
| 9647 | '"string"\n' |
| 9648 | ' module to create a translation table. For string ' |
| 9649 | 'objects, set the\n' |
| 9650 | ' *table* argument to "None" for translations that only ' |
| 9651 | 'delete\n' |
| 9652 | ' characters:\n' |
| 9653 | '\n' |
| 9654 | " >>> 'read this short text'.translate(None, 'aeiou')\n" |
| 9655 | " 'rd ths shrt txt'\n" |
| 9656 | '\n' |
| 9657 | ' New in version 2.6: Support for a "None" *table* ' |
| 9658 | 'argument.\n' |
| 9659 | '\n' |
| 9660 | ' For Unicode objects, the "translate()" method does not ' |
| 9661 | 'accept the\n' |
| 9662 | ' optional *deletechars* argument. Instead, it returns a ' |
| 9663 | 'copy of the\n' |
| 9664 | ' *s* where all characters have been mapped through the ' |
| 9665 | 'given\n' |
| 9666 | ' translation table which must be a mapping of Unicode ' |
| 9667 | 'ordinals to\n' |
| 9668 | ' Unicode ordinals, Unicode strings or "None". Unmapped ' |
| 9669 | 'characters\n' |
| 9670 | ' are left untouched. Characters mapped to "None" are ' |
| 9671 | 'deleted. Note,\n' |
| 9672 | ' a more flexible approach is to create a custom ' |
| 9673 | 'character mapping\n' |
| 9674 | ' codec using the "codecs" module (see "encodings.cp1251" ' |
| 9675 | 'for an\n' |
| 9676 | ' example).\n' |
| 9677 | '\n' |
| 9678 | 'str.upper()\n' |
| 9679 | '\n' |
| 9680 | ' Return a copy of the string with all the cased ' |
| 9681 | 'characters [4]\n' |
| 9682 | ' converted to uppercase. Note that ' |
| 9683 | '"str.upper().isupper()" might be\n' |
| 9684 | ' "False" if "s" contains uncased characters or if the ' |
| 9685 | 'Unicode\n' |
| 9686 | ' category of the resulting character(s) is not "Lu" ' |
| 9687 | '(Letter,\n' |
| 9688 | ' uppercase), but e.g. "Lt" (Letter, titlecase).\n' |
| 9689 | '\n' |
| 9690 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 9691 | '\n' |
| 9692 | 'str.zfill(width)\n' |
| 9693 | '\n' |
| 9694 | ' Return the numeric string left filled with zeros in a ' |
| 9695 | 'string of\n' |
| 9696 | ' length *width*. A sign prefix is handled correctly. ' |
| 9697 | 'The original\n' |
| 9698 | ' string is returned if *width* is less than or equal to ' |
| 9699 | '"len(s)".\n' |
| 9700 | '\n' |
| 9701 | ' New in version 2.2.2.\n' |
| 9702 | '\n' |
| 9703 | 'The following methods are present only on unicode ' |
| 9704 | 'objects:\n' |
| 9705 | '\n' |
| 9706 | 'unicode.isnumeric()\n' |
| 9707 | '\n' |
| 9708 | ' Return "True" if there are only numeric characters in ' |
| 9709 | 'S, "False"\n' |
| 9710 | ' otherwise. Numeric characters include digit characters, ' |
| 9711 | 'and all\n' |
| 9712 | ' characters that have the Unicode numeric value ' |
| 9713 | 'property, e.g.\n' |
| 9714 | ' U+2155, VULGAR FRACTION ONE FIFTH.\n' |
| 9715 | '\n' |
| 9716 | 'unicode.isdecimal()\n' |
| 9717 | '\n' |
| 9718 | ' Return "True" if there are only decimal characters in ' |
| 9719 | 'S, "False"\n' |
| 9720 | ' otherwise. Decimal characters include digit characters, ' |
| 9721 | 'and all\n' |
| 9722 | ' characters that can be used to form decimal-radix ' |
| 9723 | 'numbers, e.g.\n' |
| 9724 | ' U+0660, ARABIC-INDIC DIGIT ZERO.\n', |
| 9725 | 'strings': '\n' |
| 9726 | 'String literals\n' |
| 9727 | '***************\n' |
| 9728 | '\n' |
| 9729 | 'String literals are described by the following lexical ' |
| 9730 | 'definitions:\n' |
| 9731 | '\n' |
| 9732 | ' stringliteral ::= [stringprefix](shortstring | longstring)\n' |
| 9733 | ' stringprefix ::= "r" | "u" | "ur" | "R" | "U" | "UR" | "Ur" ' |
| 9734 | '| "uR"\n' |
| 9735 | ' | "b" | "B" | "br" | "Br" | "bR" | "BR"\n' |
| 9736 | ' shortstring ::= "\'" shortstringitem* "\'" | \'"\' ' |
| 9737 | 'shortstringitem* \'"\'\n' |
| 9738 | ' longstring ::= "\'\'\'" longstringitem* "\'\'\'"\n' |
| 9739 | ' | \'"""\' longstringitem* \'"""\'\n' |
| 9740 | ' shortstringitem ::= shortstringchar | escapeseq\n' |
| 9741 | ' longstringitem ::= longstringchar | escapeseq\n' |
| 9742 | ' shortstringchar ::= <any source character except "\\" or ' |
| 9743 | 'newline or the quote>\n' |
| 9744 | ' longstringchar ::= <any source character except "\\">\n' |
| 9745 | ' escapeseq ::= "\\" <any ASCII character>\n' |
| 9746 | '\n' |
| 9747 | 'One syntactic restriction not indicated by these productions is ' |
| 9748 | 'that\n' |
| 9749 | 'whitespace is not allowed between the "stringprefix" and the rest ' |
| 9750 | 'of\n' |
| 9751 | 'the string literal. The source character set is defined by the\n' |
| 9752 | 'encoding declaration; it is ASCII if no encoding declaration is ' |
| 9753 | 'given\n' |
| 9754 | 'in the source file; see section Encoding declarations.\n' |
| 9755 | '\n' |
| 9756 | 'In plain English: String literals can be enclosed in matching ' |
| 9757 | 'single\n' |
| 9758 | 'quotes ("\'") or double quotes ("""). They can also be enclosed ' |
| 9759 | 'in\n' |
| 9760 | 'matching groups of three single or double quotes (these are ' |
| 9761 | 'generally\n' |
| 9762 | 'referred to as *triple-quoted strings*). The backslash ("\\")\n' |
| 9763 | 'character is used to escape characters that otherwise have a ' |
| 9764 | 'special\n' |
| 9765 | 'meaning, such as newline, backslash itself, or the quote ' |
| 9766 | 'character.\n' |
| 9767 | 'String literals may optionally be prefixed with a letter "\'r\'" ' |
| 9768 | 'or\n' |
| 9769 | '"\'R\'"; such strings are called *raw strings* and use different ' |
| 9770 | 'rules\n' |
| 9771 | 'for interpreting backslash escape sequences. A prefix of "\'u\'" ' |
| 9772 | 'or\n' |
| 9773 | '"\'U\'" makes the string a Unicode string. Unicode strings use ' |
| 9774 | 'the\n' |
| 9775 | 'Unicode character set as defined by the Unicode Consortium and ' |
| 9776 | 'ISO\n' |
| 9777 | '10646. Some additional escape sequences, described below, are\n' |
| 9778 | 'available in Unicode strings. A prefix of "\'b\'" or "\'B\'" is ' |
| 9779 | 'ignored in\n' |
| 9780 | 'Python 2; it indicates that the literal should become a bytes ' |
| 9781 | 'literal\n' |
| 9782 | 'in Python 3 (e.g. when code is automatically converted with ' |
| 9783 | '2to3). A\n' |
| 9784 | '"\'u\'" or "\'b\'" prefix may be followed by an "\'r\'" prefix.\n' |
| 9785 | '\n' |
| 9786 | 'In triple-quoted strings, unescaped newlines and quotes are ' |
| 9787 | 'allowed\n' |
| 9788 | '(and are retained), except that three unescaped quotes in a row\n' |
| 9789 | 'terminate the string. (A "quote" is the character used to open ' |
| 9790 | 'the\n' |
| 9791 | 'string, i.e. either "\'" or """.)\n' |
| 9792 | '\n' |
| 9793 | 'Unless an "\'r\'" or "\'R\'" prefix is present, escape sequences ' |
| 9794 | 'in\n' |
| 9795 | 'strings are interpreted according to rules similar to those used ' |
| 9796 | 'by\n' |
| 9797 | 'Standard C. The recognized escape sequences are:\n' |
| 9798 | '\n' |
| 9799 | '+-------------------+-----------------------------------+---------+\n' |
| 9800 | '| Escape Sequence | Meaning | Notes ' |
| 9801 | '|\n' |
| 9802 | '+===================+===================================+=========+\n' |
| 9803 | '| "\\newline" | Ignored ' |
| 9804 | '| |\n' |
| 9805 | '+-------------------+-----------------------------------+---------+\n' |
| 9806 | '| "\\\\" | Backslash ("\\") ' |
| 9807 | '| |\n' |
| 9808 | '+-------------------+-----------------------------------+---------+\n' |
| 9809 | '| "\\\'" | Single quote ("\'") ' |
| 9810 | '| |\n' |
| 9811 | '+-------------------+-----------------------------------+---------+\n' |
| 9812 | '| "\\"" | Double quote (""") ' |
| 9813 | '| |\n' |
| 9814 | '+-------------------+-----------------------------------+---------+\n' |
| 9815 | '| "\\a" | ASCII Bell (BEL) ' |
| 9816 | '| |\n' |
| 9817 | '+-------------------+-----------------------------------+---------+\n' |
| 9818 | '| "\\b" | ASCII Backspace (BS) ' |
| 9819 | '| |\n' |
| 9820 | '+-------------------+-----------------------------------+---------+\n' |
| 9821 | '| "\\f" | ASCII Formfeed (FF) ' |
| 9822 | '| |\n' |
| 9823 | '+-------------------+-----------------------------------+---------+\n' |
| 9824 | '| "\\n" | ASCII Linefeed (LF) ' |
| 9825 | '| |\n' |
| 9826 | '+-------------------+-----------------------------------+---------+\n' |
| 9827 | '| "\\N{name}" | Character named *name* in the ' |
| 9828 | '| |\n' |
| 9829 | '| | Unicode database (Unicode only) | ' |
| 9830 | '|\n' |
| 9831 | '+-------------------+-----------------------------------+---------+\n' |
| 9832 | '| "\\r" | ASCII Carriage Return (CR) ' |
| 9833 | '| |\n' |
| 9834 | '+-------------------+-----------------------------------+---------+\n' |
| 9835 | '| "\\t" | ASCII Horizontal Tab (TAB) ' |
| 9836 | '| |\n' |
| 9837 | '+-------------------+-----------------------------------+---------+\n' |
| 9838 | '| "\\uxxxx" | Character with 16-bit hex value | ' |
| 9839 | '(1) |\n' |
| 9840 | '| | *xxxx* (Unicode only) | ' |
| 9841 | '|\n' |
| 9842 | '+-------------------+-----------------------------------+---------+\n' |
| 9843 | '| "\\Uxxxxxxxx" | Character with 32-bit hex value | ' |
| 9844 | '(2) |\n' |
| 9845 | '| | *xxxxxxxx* (Unicode only) | ' |
| 9846 | '|\n' |
| 9847 | '+-------------------+-----------------------------------+---------+\n' |
| 9848 | '| "\\v" | ASCII Vertical Tab (VT) ' |
| 9849 | '| |\n' |
| 9850 | '+-------------------+-----------------------------------+---------+\n' |
| 9851 | '| "\\ooo" | Character with octal value *ooo* | ' |
| 9852 | '(3,5) |\n' |
| 9853 | '+-------------------+-----------------------------------+---------+\n' |
| 9854 | '| "\\xhh" | Character with hex value *hh* | ' |
| 9855 | '(4,5) |\n' |
| 9856 | '+-------------------+-----------------------------------+---------+\n' |
| 9857 | '\n' |
| 9858 | 'Notes:\n' |
| 9859 | '\n' |
| 9860 | '1. Individual code units which form parts of a surrogate pair ' |
| 9861 | 'can\n' |
| 9862 | ' be encoded using this escape sequence.\n' |
| 9863 | '\n' |
| 9864 | '2. Any Unicode character can be encoded this way, but characters\n' |
| 9865 | ' outside the Basic Multilingual Plane (BMP) will be encoded ' |
| 9866 | 'using a\n' |
| 9867 | ' surrogate pair if Python is compiled to use 16-bit code units ' |
| 9868 | '(the\n' |
| 9869 | ' default).\n' |
| 9870 | '\n' |
| 9871 | '3. As in Standard C, up to three octal digits are accepted.\n' |
| 9872 | '\n' |
| 9873 | '4. Unlike in Standard C, exactly two hex digits are required.\n' |
| 9874 | '\n' |
| 9875 | '5. In a string literal, hexadecimal and octal escapes denote the\n' |
| 9876 | ' byte with the given value; it is not necessary that the byte\n' |
| 9877 | ' encodes a character in the source character set. In a Unicode\n' |
| 9878 | ' literal, these escapes denote a Unicode character with the ' |
| 9879 | 'given\n' |
| 9880 | ' value.\n' |
| 9881 | '\n' |
| 9882 | 'Unlike Standard C, all unrecognized escape sequences are left in ' |
| 9883 | 'the\n' |
| 9884 | 'string unchanged, i.e., *the backslash is left in the string*. ' |
| 9885 | '(This\n' |
| 9886 | 'behavior is useful when debugging: if an escape sequence is ' |
| 9887 | 'mistyped,\n' |
| 9888 | 'the resulting output is more easily recognized as broken.) It is ' |
| 9889 | 'also\n' |
| 9890 | 'important to note that the escape sequences marked as "(Unicode ' |
| 9891 | 'only)"\n' |
| 9892 | 'in the table above fall into the category of unrecognized escapes ' |
| 9893 | 'for\n' |
| 9894 | 'non-Unicode string literals.\n' |
| 9895 | '\n' |
| 9896 | 'When an "\'r\'" or "\'R\'" prefix is present, a character ' |
| 9897 | 'following a\n' |
| 9898 | 'backslash is included in the string without change, and *all\n' |
| 9899 | 'backslashes are left in the string*. For example, the string ' |
| 9900 | 'literal\n' |
| 9901 | '"r"\\n"" consists of two characters: a backslash and a lowercase ' |
| 9902 | '"\'n\'".\n' |
| 9903 | 'String quotes can be escaped with a backslash, but the backslash\n' |
| 9904 | 'remains in the string; for example, "r"\\""" is a valid string ' |
| 9905 | 'literal\n' |
| 9906 | 'consisting of two characters: a backslash and a double quote; ' |
| 9907 | '"r"\\""\n' |
| 9908 | 'is not a valid string literal (even a raw string cannot end in an ' |
| 9909 | 'odd\n' |
| 9910 | 'number of backslashes). Specifically, *a raw string cannot end ' |
| 9911 | 'in a\n' |
| 9912 | 'single backslash* (since the backslash would escape the ' |
| 9913 | 'following\n' |
| 9914 | 'quote character). Note also that a single backslash followed by ' |
| 9915 | 'a\n' |
| 9916 | 'newline is interpreted as those two characters as part of the ' |
| 9917 | 'string,\n' |
| 9918 | '*not* as a line continuation.\n' |
| 9919 | '\n' |
| 9920 | 'When an "\'r\'" or "\'R\'" prefix is used in conjunction with a ' |
| 9921 | '"\'u\'" or\n' |
| 9922 | '"\'U\'" prefix, then the "\\uXXXX" and "\\UXXXXXXXX" escape ' |
| 9923 | 'sequences are\n' |
| 9924 | 'processed while *all other backslashes are left in the string*. ' |
| 9925 | 'For\n' |
| 9926 | 'example, the string literal "ur"\\u0062\\n"" consists of three ' |
| 9927 | 'Unicode\n' |
| 9928 | "characters: 'LATIN SMALL LETTER B', 'REVERSE SOLIDUS', and " |
| 9929 | "'LATIN\n" |
| 9930 | "SMALL LETTER N'. Backslashes can be escaped with a preceding\n" |
| 9931 | 'backslash; however, both remain in the string. As a result, ' |
| 9932 | '"\\uXXXX"\n' |
| 9933 | 'escape sequences are only recognized when there are an odd number ' |
| 9934 | 'of\n' |
| 9935 | 'backslashes.\n', |
| 9936 | 'subscriptions': '\n' |
| 9937 | 'Subscriptions\n' |
| 9938 | '*************\n' |
| 9939 | '\n' |
| 9940 | 'A subscription selects an item of a sequence (string, tuple ' |
| 9941 | 'or list)\n' |
| 9942 | 'or mapping (dictionary) object:\n' |
| 9943 | '\n' |
| 9944 | ' subscription ::= primary "[" expression_list "]"\n' |
| 9945 | '\n' |
| 9946 | 'The primary must evaluate to an object of a sequence or ' |
| 9947 | 'mapping type.\n' |
| 9948 | '\n' |
| 9949 | 'If the primary is a mapping, the expression list must ' |
| 9950 | 'evaluate to an\n' |
| 9951 | 'object whose value is one of the keys of the mapping, and ' |
| 9952 | 'the\n' |
| 9953 | 'subscription selects the value in the mapping that ' |
| 9954 | 'corresponds to that\n' |
| 9955 | 'key. (The expression list is a tuple except if it has ' |
| 9956 | 'exactly one\n' |
| 9957 | 'item.)\n' |
| 9958 | '\n' |
| 9959 | 'If the primary is a sequence, the expression (list) must ' |
| 9960 | 'evaluate to a\n' |
| 9961 | 'plain integer. If this value is negative, the length of ' |
| 9962 | 'the sequence\n' |
| 9963 | 'is added to it (so that, e.g., "x[-1]" selects the last ' |
| 9964 | 'item of "x".)\n' |
| 9965 | 'The resulting value must be a nonnegative integer less than ' |
| 9966 | 'the number\n' |
| 9967 | 'of items in the sequence, and the subscription selects the ' |
| 9968 | 'item whose\n' |
| 9969 | 'index is that value (counting from zero).\n' |
| 9970 | '\n' |
| 9971 | "A string's items are characters. A character is not a " |
| 9972 | 'separate data\n' |
| 9973 | 'type but a string of exactly one character.\n', |
| 9974 | 'truth': '\n' |
| 9975 | 'Truth Value Testing\n' |
| 9976 | '*******************\n' |
| 9977 | '\n' |
| 9978 | 'Any object can be tested for truth value, for use in an "if" or\n' |
| 9979 | '"while" condition or as operand of the Boolean operations below. ' |
| 9980 | 'The\n' |
| 9981 | 'following values are considered false:\n' |
| 9982 | '\n' |
| 9983 | '* "None"\n' |
| 9984 | '\n' |
| 9985 | '* "False"\n' |
| 9986 | '\n' |
| 9987 | '* zero of any numeric type, for example, "0", "0L", "0.0", "0j".\n' |
| 9988 | '\n' |
| 9989 | '* any empty sequence, for example, "\'\'", "()", "[]".\n' |
| 9990 | '\n' |
| 9991 | '* any empty mapping, for example, "{}".\n' |
| 9992 | '\n' |
| 9993 | '* instances of user-defined classes, if the class defines a\n' |
| 9994 | ' "__nonzero__()" or "__len__()" method, when that method returns ' |
| 9995 | 'the\n' |
| 9996 | ' integer zero or "bool" value "False". [1]\n' |
| 9997 | '\n' |
| 9998 | 'All other values are considered true --- so objects of many types ' |
| 9999 | 'are\n' |
| 10000 | 'always true.\n' |
| 10001 | '\n' |
| 10002 | 'Operations and built-in functions that have a Boolean result ' |
| 10003 | 'always\n' |
| 10004 | 'return "0" or "False" for false and "1" or "True" for true, unless\n' |
| 10005 | 'otherwise stated. (Important exception: the Boolean operations ' |
| 10006 | '"or"\n' |
| 10007 | 'and "and" always return one of their operands.)\n', |
| 10008 | 'try': '\n' |
| 10009 | 'The "try" statement\n' |
| 10010 | '*******************\n' |
| 10011 | '\n' |
| 10012 | 'The "try" statement specifies exception handlers and/or cleanup code\n' |
| 10013 | 'for a group of statements:\n' |
| 10014 | '\n' |
| 10015 | ' try_stmt ::= try1_stmt | try2_stmt\n' |
| 10016 | ' try1_stmt ::= "try" ":" suite\n' |
| 10017 | ' ("except" [expression [("as" | ",") identifier]] ":" ' |
| 10018 | 'suite)+\n' |
| 10019 | ' ["else" ":" suite]\n' |
| 10020 | ' ["finally" ":" suite]\n' |
| 10021 | ' try2_stmt ::= "try" ":" suite\n' |
| 10022 | ' "finally" ":" suite\n' |
| 10023 | '\n' |
| 10024 | 'Changed in version 2.5: In previous versions of Python,\n' |
| 10025 | '"try"..."except"..."finally" did not work. "try"..."except" had to ' |
| 10026 | 'be\n' |
| 10027 | 'nested in "try"..."finally".\n' |
| 10028 | '\n' |
| 10029 | 'The "except" clause(s) specify one or more exception handlers. When ' |
| 10030 | 'no\n' |
| 10031 | 'exception occurs in the "try" clause, no exception handler is\n' |
| 10032 | 'executed. When an exception occurs in the "try" suite, a search for ' |
| 10033 | 'an\n' |
| 10034 | 'exception handler is started. This search inspects the except ' |
| 10035 | 'clauses\n' |
| 10036 | 'in turn until one is found that matches the exception. An ' |
| 10037 | 'expression-\n' |
| 10038 | 'less except clause, if present, must be last; it matches any\n' |
| 10039 | 'exception. For an except clause with an expression, that expression\n' |
| 10040 | 'is evaluated, and the clause matches the exception if the resulting\n' |
| 10041 | 'object is "compatible" with the exception. An object is compatible\n' |
| 10042 | 'with an exception if it is the class or a base class of the ' |
| 10043 | 'exception\n' |
| 10044 | 'object, or a tuple containing an item compatible with the exception.\n' |
| 10045 | '\n' |
| 10046 | 'If no except clause matches the exception, the search for an ' |
| 10047 | 'exception\n' |
| 10048 | 'handler continues in the surrounding code and on the invocation ' |
| 10049 | 'stack.\n' |
| 10050 | '[1]\n' |
| 10051 | '\n' |
| 10052 | 'If the evaluation of an expression in the header of an except clause\n' |
| 10053 | 'raises an exception, the original search for a handler is canceled ' |
| 10054 | 'and\n' |
| 10055 | 'a search starts for the new exception in the surrounding code and on\n' |
| 10056 | 'the call stack (it is treated as if the entire "try" statement ' |
| 10057 | 'raised\n' |
| 10058 | 'the exception).\n' |
| 10059 | '\n' |
| 10060 | 'When a matching except clause is found, the exception is assigned to\n' |
| 10061 | 'the target specified in that except clause, if present, and the ' |
| 10062 | 'except\n' |
| 10063 | "clause's suite is executed. All except clauses must have an\n" |
| 10064 | 'executable block. When the end of this block is reached, execution\n' |
| 10065 | 'continues normally after the entire try statement. (This means that\n' |
| 10066 | 'if two nested handlers exist for the same exception, and the ' |
| 10067 | 'exception\n' |
| 10068 | 'occurs in the try clause of the inner handler, the outer handler ' |
| 10069 | 'will\n' |
| 10070 | 'not handle the exception.)\n' |
| 10071 | '\n' |
| 10072 | "Before an except clause's suite is executed, details about the\n" |
| 10073 | 'exception are assigned to three variables in the "sys" module:\n' |
| 10074 | '"sys.exc_type" receives the object identifying the exception;\n' |
| 10075 | '"sys.exc_value" receives the exception\'s parameter;\n' |
| 10076 | '"sys.exc_traceback" receives a traceback object (see section The\n' |
| 10077 | 'standard type hierarchy) identifying the point in the program where\n' |
| 10078 | 'the exception occurred. These details are also available through the\n' |
| 10079 | '"sys.exc_info()" function, which returns a tuple "(exc_type,\n' |
| 10080 | 'exc_value, exc_traceback)". Use of the corresponding variables is\n' |
| 10081 | 'deprecated in favor of this function, since their use is unsafe in a\n' |
| 10082 | 'threaded program. As of Python 1.5, the variables are restored to\n' |
| 10083 | 'their previous values (before the call) when returning from a ' |
| 10084 | 'function\n' |
| 10085 | 'that handled an exception.\n' |
| 10086 | '\n' |
| 10087 | 'The optional "else" clause is executed if and when control flows off\n' |
| 10088 | 'the end of the "try" clause. [2] Exceptions in the "else" clause are\n' |
| 10089 | 'not handled by the preceding "except" clauses.\n' |
| 10090 | '\n' |
| 10091 | 'If "finally" is present, it specifies a \'cleanup\' handler. The ' |
| 10092 | '"try"\n' |
| 10093 | 'clause is executed, including any "except" and "else" clauses. If ' |
| 10094 | 'an\n' |
| 10095 | 'exception occurs in any of the clauses and is not handled, the\n' |
| 10096 | 'exception is temporarily saved. The "finally" clause is executed. ' |
| 10097 | 'If\n' |
| 10098 | 'there is a saved exception, it is re-raised at the end of the\n' |
| 10099 | '"finally" clause. If the "finally" clause raises another exception ' |
| 10100 | 'or\n' |
| 10101 | 'executes a "return" or "break" statement, the saved exception is\n' |
| 10102 | 'discarded:\n' |
| 10103 | '\n' |
| 10104 | ' >>> def f():\n' |
| 10105 | ' ... try:\n' |
| 10106 | ' ... 1/0\n' |
| 10107 | ' ... finally:\n' |
| 10108 | ' ... return 42\n' |
| 10109 | ' ...\n' |
| 10110 | ' >>> f()\n' |
| 10111 | ' 42\n' |
| 10112 | '\n' |
| 10113 | 'The exception information is not available to the program during\n' |
| 10114 | 'execution of the "finally" clause.\n' |
| 10115 | '\n' |
| 10116 | 'When a "return", "break" or "continue" statement is executed in the\n' |
| 10117 | '"try" suite of a "try"..."finally" statement, the "finally" clause ' |
| 10118 | 'is\n' |
| 10119 | 'also executed \'on the way out.\' A "continue" statement is illegal ' |
| 10120 | 'in\n' |
| 10121 | 'the "finally" clause. (The reason is a problem with the current\n' |
| 10122 | 'implementation --- this restriction may be lifted in the future).\n' |
| 10123 | '\n' |
| 10124 | 'The return value of a function is determined by the last "return"\n' |
| 10125 | 'statement executed. Since the "finally" clause always executes, a\n' |
| 10126 | '"return" statement executed in the "finally" clause will always be ' |
| 10127 | 'the\n' |
| 10128 | 'last one executed:\n' |
| 10129 | '\n' |
| 10130 | ' >>> def foo():\n' |
| 10131 | ' ... try:\n' |
| 10132 | " ... return 'try'\n" |
| 10133 | ' ... finally:\n' |
| 10134 | " ... return 'finally'\n" |
| 10135 | ' ...\n' |
| 10136 | ' >>> foo()\n' |
| 10137 | " 'finally'\n" |
| 10138 | '\n' |
| 10139 | 'Additional information on exceptions can be found in section\n' |
| 10140 | 'Exceptions, and information on using the "raise" statement to ' |
| 10141 | 'generate\n' |
| 10142 | 'exceptions may be found in section The raise statement.\n', |
| 10143 | 'types': '\n' |
| 10144 | 'The standard type hierarchy\n' |
| 10145 | '***************************\n' |
| 10146 | '\n' |
| 10147 | 'Below is a list of the types that are built into Python. ' |
| 10148 | 'Extension\n' |
| 10149 | 'modules (written in C, Java, or other languages, depending on the\n' |
| 10150 | 'implementation) can define additional types. Future versions of\n' |
| 10151 | 'Python may add types to the type hierarchy (e.g., rational ' |
| 10152 | 'numbers,\n' |
| 10153 | 'efficiently stored arrays of integers, etc.).\n' |
| 10154 | '\n' |
| 10155 | 'Some of the type descriptions below contain a paragraph listing\n' |
| 10156 | "'special attributes.' These are attributes that provide access to " |
| 10157 | 'the\n' |
| 10158 | 'implementation and are not intended for general use. Their ' |
| 10159 | 'definition\n' |
| 10160 | 'may change in the future.\n' |
| 10161 | '\n' |
| 10162 | 'None\n' |
| 10163 | ' This type has a single value. There is a single object with ' |
| 10164 | 'this\n' |
| 10165 | ' value. This object is accessed through the built-in name "None". ' |
| 10166 | 'It\n' |
| 10167 | ' is used to signify the absence of a value in many situations, ' |
| 10168 | 'e.g.,\n' |
| 10169 | " it is returned from functions that don't explicitly return\n" |
| 10170 | ' anything. Its truth value is false.\n' |
| 10171 | '\n' |
| 10172 | 'NotImplemented\n' |
| 10173 | ' This type has a single value. There is a single object with ' |
| 10174 | 'this\n' |
| 10175 | ' value. This object is accessed through the built-in name\n' |
| 10176 | ' "NotImplemented". Numeric methods and rich comparison methods ' |
| 10177 | 'may\n' |
| 10178 | ' return this value if they do not implement the operation for ' |
| 10179 | 'the\n' |
| 10180 | ' operands provided. (The interpreter will then try the ' |
| 10181 | 'reflected\n' |
| 10182 | ' operation, or some other fallback, depending on the operator.) ' |
| 10183 | 'Its\n' |
| 10184 | ' truth value is true.\n' |
| 10185 | '\n' |
| 10186 | 'Ellipsis\n' |
| 10187 | ' This type has a single value. There is a single object with ' |
| 10188 | 'this\n' |
| 10189 | ' value. This object is accessed through the built-in name\n' |
| 10190 | ' "Ellipsis". It is used to indicate the presence of the "..." ' |
| 10191 | 'syntax\n' |
| 10192 | ' in a slice. Its truth value is true.\n' |
| 10193 | '\n' |
| 10194 | '"numbers.Number"\n' |
| 10195 | ' These are created by numeric literals and returned as results ' |
| 10196 | 'by\n' |
| 10197 | ' arithmetic operators and arithmetic built-in functions. ' |
| 10198 | 'Numeric\n' |
| 10199 | ' objects are immutable; once created their value never changes.\n' |
| 10200 | ' Python numbers are of course strongly related to mathematical\n' |
| 10201 | ' numbers, but subject to the limitations of numerical ' |
| 10202 | 'representation\n' |
| 10203 | ' in computers.\n' |
| 10204 | '\n' |
| 10205 | ' Python distinguishes between integers, floating point numbers, ' |
| 10206 | 'and\n' |
| 10207 | ' complex numbers:\n' |
| 10208 | '\n' |
| 10209 | ' "numbers.Integral"\n' |
| 10210 | ' These represent elements from the mathematical set of ' |
| 10211 | 'integers\n' |
| 10212 | ' (positive and negative).\n' |
| 10213 | '\n' |
| 10214 | ' There are three types of integers:\n' |
| 10215 | '\n' |
| 10216 | ' Plain integers\n' |
| 10217 | ' These represent numbers in the range -2147483648 through\n' |
| 10218 | ' 2147483647. (The range may be larger on machines with a\n' |
| 10219 | ' larger natural word size, but not smaller.) When the ' |
| 10220 | 'result\n' |
| 10221 | ' of an operation would fall outside this range, the result ' |
| 10222 | 'is\n' |
| 10223 | ' normally returned as a long integer (in some cases, the\n' |
| 10224 | ' exception "OverflowError" is raised instead). For the\n' |
| 10225 | ' purpose of shift and mask operations, integers are assumed ' |
| 10226 | 'to\n' |
| 10227 | " have a binary, 2's complement notation using 32 or more " |
| 10228 | 'bits,\n' |
| 10229 | ' and hiding no bits from the user (i.e., all 4294967296\n' |
| 10230 | ' different bit patterns correspond to different values).\n' |
| 10231 | '\n' |
| 10232 | ' Long integers\n' |
| 10233 | ' These represent numbers in an unlimited range, subject to\n' |
| 10234 | ' available (virtual) memory only. For the purpose of ' |
| 10235 | 'shift\n' |
| 10236 | ' and mask operations, a binary representation is assumed, ' |
| 10237 | 'and\n' |
| 10238 | " negative numbers are represented in a variant of 2's\n" |
| 10239 | ' complement which gives the illusion of an infinite string ' |
| 10240 | 'of\n' |
| 10241 | ' sign bits extending to the left.\n' |
| 10242 | '\n' |
| 10243 | ' Booleans\n' |
| 10244 | ' These represent the truth values False and True. The two\n' |
| 10245 | ' objects representing the values "False" and "True" are ' |
| 10246 | 'the\n' |
| 10247 | ' only Boolean objects. The Boolean type is a subtype of ' |
| 10248 | 'plain\n' |
| 10249 | ' integers, and Boolean values behave like the values 0 and ' |
| 10250 | '1,\n' |
| 10251 | ' respectively, in almost all contexts, the exception being\n' |
| 10252 | ' that when converted to a string, the strings ""False"" or\n' |
| 10253 | ' ""True"" are returned, respectively.\n' |
| 10254 | '\n' |
| 10255 | ' The rules for integer representation are intended to give ' |
| 10256 | 'the\n' |
| 10257 | ' most meaningful interpretation of shift and mask operations\n' |
| 10258 | ' involving negative integers and the least surprises when\n' |
| 10259 | ' switching between the plain and long integer domains. Any\n' |
| 10260 | ' operation, if it yields a result in the plain integer ' |
| 10261 | 'domain,\n' |
| 10262 | ' will yield the same result in the long integer domain or ' |
| 10263 | 'when\n' |
| 10264 | ' using mixed operands. The switch between domains is ' |
| 10265 | 'transparent\n' |
| 10266 | ' to the programmer.\n' |
| 10267 | '\n' |
| 10268 | ' "numbers.Real" ("float")\n' |
| 10269 | ' These represent machine-level double precision floating ' |
| 10270 | 'point\n' |
| 10271 | ' numbers. You are at the mercy of the underlying machine\n' |
| 10272 | ' architecture (and C or Java implementation) for the accepted\n' |
| 10273 | ' range and handling of overflow. Python does not support ' |
| 10274 | 'single-\n' |
| 10275 | ' precision floating point numbers; the savings in processor ' |
| 10276 | 'and\n' |
| 10277 | ' memory usage that are usually the reason for using these are\n' |
| 10278 | ' dwarfed by the overhead of using objects in Python, so there ' |
| 10279 | 'is\n' |
| 10280 | ' no reason to complicate the language with two kinds of ' |
| 10281 | 'floating\n' |
| 10282 | ' point numbers.\n' |
| 10283 | '\n' |
| 10284 | ' "numbers.Complex"\n' |
| 10285 | ' These represent complex numbers as a pair of machine-level\n' |
| 10286 | ' double precision floating point numbers. The same caveats ' |
| 10287 | 'apply\n' |
| 10288 | ' as for floating point numbers. The real and imaginary parts ' |
| 10289 | 'of a\n' |
| 10290 | ' complex number "z" can be retrieved through the read-only\n' |
| 10291 | ' attributes "z.real" and "z.imag".\n' |
| 10292 | '\n' |
| 10293 | 'Sequences\n' |
| 10294 | ' These represent finite ordered sets indexed by non-negative\n' |
| 10295 | ' numbers. The built-in function "len()" returns the number of ' |
| 10296 | 'items\n' |
| 10297 | ' of a sequence. When the length of a sequence is *n*, the index ' |
| 10298 | 'set\n' |
| 10299 | ' contains the numbers 0, 1, ..., *n*-1. Item *i* of sequence *a* ' |
| 10300 | 'is\n' |
| 10301 | ' selected by "a[i]".\n' |
| 10302 | '\n' |
| 10303 | ' Sequences also support slicing: "a[i:j]" selects all items with\n' |
| 10304 | ' index *k* such that *i* "<=" *k* "<" *j*. When used as an\n' |
| 10305 | ' expression, a slice is a sequence of the same type. This ' |
| 10306 | 'implies\n' |
| 10307 | ' that the index set is renumbered so that it starts at 0.\n' |
| 10308 | '\n' |
| 10309 | ' Some sequences also support "extended slicing" with a third ' |
| 10310 | '"step"\n' |
| 10311 | ' parameter: "a[i:j:k]" selects all items of *a* with index *x* ' |
| 10312 | 'where\n' |
| 10313 | ' "x = i + n*k", *n* ">=" "0" and *i* "<=" *x* "<" *j*.\n' |
| 10314 | '\n' |
| 10315 | ' Sequences are distinguished according to their mutability:\n' |
| 10316 | '\n' |
| 10317 | ' Immutable sequences\n' |
| 10318 | ' An object of an immutable sequence type cannot change once it ' |
| 10319 | 'is\n' |
| 10320 | ' created. (If the object contains references to other ' |
| 10321 | 'objects,\n' |
| 10322 | ' these other objects may be mutable and may be changed; ' |
| 10323 | 'however,\n' |
| 10324 | ' the collection of objects directly referenced by an ' |
| 10325 | 'immutable\n' |
| 10326 | ' object cannot change.)\n' |
| 10327 | '\n' |
| 10328 | ' The following types are immutable sequences:\n' |
| 10329 | '\n' |
| 10330 | ' Strings\n' |
| 10331 | ' The items of a string are characters. There is no ' |
| 10332 | 'separate\n' |
| 10333 | ' character type; a character is represented by a string of ' |
| 10334 | 'one\n' |
| 10335 | ' item. Characters represent (at least) 8-bit bytes. The\n' |
| 10336 | ' built-in functions "chr()" and "ord()" convert between\n' |
| 10337 | ' characters and nonnegative integers representing the byte\n' |
| 10338 | ' values. Bytes with the values 0--127 usually represent ' |
| 10339 | 'the\n' |
| 10340 | ' corresponding ASCII values, but the interpretation of ' |
| 10341 | 'values\n' |
| 10342 | ' is up to the program. The string data type is also used ' |
| 10343 | 'to\n' |
| 10344 | ' represent arrays of bytes, e.g., to hold data read from a\n' |
| 10345 | ' file.\n' |
| 10346 | '\n' |
| 10347 | ' (On systems whose native character set is not ASCII, ' |
| 10348 | 'strings\n' |
| 10349 | ' may use EBCDIC in their internal representation, provided ' |
| 10350 | 'the\n' |
| 10351 | ' functions "chr()" and "ord()" implement a mapping between\n' |
| 10352 | ' ASCII and EBCDIC, and string comparison preserves the ' |
| 10353 | 'ASCII\n' |
| 10354 | ' order. Or perhaps someone can propose a better rule?)\n' |
| 10355 | '\n' |
| 10356 | ' Unicode\n' |
| 10357 | ' The items of a Unicode object are Unicode code units. A\n' |
| 10358 | ' Unicode code unit is represented by a Unicode object of ' |
| 10359 | 'one\n' |
| 10360 | ' item and can hold either a 16-bit or 32-bit value\n' |
| 10361 | ' representing a Unicode ordinal (the maximum value for the\n' |
| 10362 | ' ordinal is given in "sys.maxunicode", and depends on how\n' |
| 10363 | ' Python is configured at compile time). Surrogate pairs ' |
| 10364 | 'may\n' |
| 10365 | ' be present in the Unicode object, and will be reported as ' |
| 10366 | 'two\n' |
| 10367 | ' separate items. The built-in functions "unichr()" and\n' |
| 10368 | ' "ord()" convert between code units and nonnegative ' |
| 10369 | 'integers\n' |
| 10370 | ' representing the Unicode ordinals as defined in the ' |
| 10371 | 'Unicode\n' |
| 10372 | ' Standard 3.0. Conversion from and to other encodings are\n' |
| 10373 | ' possible through the Unicode method "encode()" and the ' |
| 10374 | 'built-\n' |
| 10375 | ' in function "unicode()".\n' |
| 10376 | '\n' |
| 10377 | ' Tuples\n' |
| 10378 | ' The items of a tuple are arbitrary Python objects. Tuples ' |
| 10379 | 'of\n' |
| 10380 | ' two or more items are formed by comma-separated lists of\n' |
| 10381 | " expressions. A tuple of one item (a 'singleton') can be\n" |
| 10382 | ' formed by affixing a comma to an expression (an expression ' |
| 10383 | 'by\n' |
| 10384 | ' itself does not create a tuple, since parentheses must be\n' |
| 10385 | ' usable for grouping of expressions). An empty tuple can ' |
| 10386 | 'be\n' |
| 10387 | ' formed by an empty pair of parentheses.\n' |
| 10388 | '\n' |
| 10389 | ' Mutable sequences\n' |
| 10390 | ' Mutable sequences can be changed after they are created. ' |
| 10391 | 'The\n' |
| 10392 | ' subscription and slicing notations can be used as the target ' |
| 10393 | 'of\n' |
| 10394 | ' assignment and "del" (delete) statements.\n' |
| 10395 | '\n' |
| 10396 | ' There are currently two intrinsic mutable sequence types:\n' |
| 10397 | '\n' |
| 10398 | ' Lists\n' |
| 10399 | ' The items of a list are arbitrary Python objects. Lists ' |
| 10400 | 'are\n' |
| 10401 | ' formed by placing a comma-separated list of expressions ' |
| 10402 | 'in\n' |
| 10403 | ' square brackets. (Note that there are no special cases ' |
| 10404 | 'needed\n' |
| 10405 | ' to form lists of length 0 or 1.)\n' |
| 10406 | '\n' |
| 10407 | ' Byte Arrays\n' |
| 10408 | ' A bytearray object is a mutable array. They are created ' |
| 10409 | 'by\n' |
| 10410 | ' the built-in "bytearray()" constructor. Aside from being\n' |
| 10411 | ' mutable (and hence unhashable), byte arrays otherwise ' |
| 10412 | 'provide\n' |
| 10413 | ' the same interface and functionality as immutable bytes\n' |
| 10414 | ' objects.\n' |
| 10415 | '\n' |
| 10416 | ' The extension module "array" provides an additional example ' |
| 10417 | 'of a\n' |
| 10418 | ' mutable sequence type.\n' |
| 10419 | '\n' |
| 10420 | 'Set types\n' |
| 10421 | ' These represent unordered, finite sets of unique, immutable\n' |
| 10422 | ' objects. As such, they cannot be indexed by any subscript. ' |
| 10423 | 'However,\n' |
| 10424 | ' they can be iterated over, and the built-in function "len()"\n' |
| 10425 | ' returns the number of items in a set. Common uses for sets are ' |
| 10426 | 'fast\n' |
| 10427 | ' membership testing, removing duplicates from a sequence, and\n' |
| 10428 | ' computing mathematical operations such as intersection, union,\n' |
| 10429 | ' difference, and symmetric difference.\n' |
| 10430 | '\n' |
| 10431 | ' For set elements, the same immutability rules apply as for\n' |
| 10432 | ' dictionary keys. Note that numeric types obey the normal rules ' |
| 10433 | 'for\n' |
| 10434 | ' numeric comparison: if two numbers compare equal (e.g., "1" and\n' |
| 10435 | ' "1.0"), only one of them can be contained in a set.\n' |
| 10436 | '\n' |
| 10437 | ' There are currently two intrinsic set types:\n' |
| 10438 | '\n' |
| 10439 | ' Sets\n' |
| 10440 | ' These represent a mutable set. They are created by the ' |
| 10441 | 'built-in\n' |
| 10442 | ' "set()" constructor and can be modified afterwards by ' |
| 10443 | 'several\n' |
| 10444 | ' methods, such as "add()".\n' |
| 10445 | '\n' |
| 10446 | ' Frozen sets\n' |
| 10447 | ' These represent an immutable set. They are created by the\n' |
| 10448 | ' built-in "frozenset()" constructor. As a frozenset is ' |
| 10449 | 'immutable\n' |
| 10450 | ' and *hashable*, it can be used again as an element of ' |
| 10451 | 'another\n' |
| 10452 | ' set, or as a dictionary key.\n' |
| 10453 | '\n' |
| 10454 | 'Mappings\n' |
| 10455 | ' These represent finite sets of objects indexed by arbitrary ' |
| 10456 | 'index\n' |
| 10457 | ' sets. The subscript notation "a[k]" selects the item indexed by ' |
| 10458 | '"k"\n' |
| 10459 | ' from the mapping "a"; this can be used in expressions and as ' |
| 10460 | 'the\n' |
| 10461 | ' target of assignments or "del" statements. The built-in ' |
| 10462 | 'function\n' |
| 10463 | ' "len()" returns the number of items in a mapping.\n' |
| 10464 | '\n' |
| 10465 | ' There is currently a single intrinsic mapping type:\n' |
| 10466 | '\n' |
| 10467 | ' Dictionaries\n' |
| 10468 | ' These represent finite sets of objects indexed by nearly\n' |
| 10469 | ' arbitrary values. The only types of values not acceptable ' |
| 10470 | 'as\n' |
| 10471 | ' keys are values containing lists or dictionaries or other\n' |
| 10472 | ' mutable types that are compared by value rather than by ' |
| 10473 | 'object\n' |
| 10474 | ' identity, the reason being that the efficient implementation ' |
| 10475 | 'of\n' |
| 10476 | " dictionaries requires a key's hash value to remain constant.\n" |
| 10477 | ' Numeric types used for keys obey the normal rules for ' |
| 10478 | 'numeric\n' |
| 10479 | ' comparison: if two numbers compare equal (e.g., "1" and ' |
| 10480 | '"1.0")\n' |
| 10481 | ' then they can be used interchangeably to index the same\n' |
| 10482 | ' dictionary entry.\n' |
| 10483 | '\n' |
| 10484 | ' Dictionaries are mutable; they can be created by the "{...}"\n' |
| 10485 | ' notation (see section Dictionary displays).\n' |
| 10486 | '\n' |
| 10487 | ' The extension modules "dbm", "gdbm", and "bsddb" provide\n' |
| 10488 | ' additional examples of mapping types.\n' |
| 10489 | '\n' |
| 10490 | 'Callable types\n' |
| 10491 | ' These are the types to which the function call operation (see\n' |
| 10492 | ' section Calls) can be applied:\n' |
| 10493 | '\n' |
| 10494 | ' User-defined functions\n' |
| 10495 | ' A user-defined function object is created by a function\n' |
| 10496 | ' definition (see section Function definitions). It should be\n' |
| 10497 | ' called with an argument list containing the same number of ' |
| 10498 | 'items\n' |
| 10499 | " as the function's formal parameter list.\n" |
| 10500 | '\n' |
| 10501 | ' Special attributes:\n' |
| 10502 | '\n' |
| 10503 | ' ' |
| 10504 | '+-------------------------+---------------------------------+-------------+\n' |
| 10505 | ' | Attribute | Meaning ' |
| 10506 | '| |\n' |
| 10507 | ' ' |
| 10508 | '+=========================+=================================+=============+\n' |
| 10509 | ' | "__doc__" "func_doc" | The function\'s documentation ' |
| 10510 | '| Writable |\n' |
| 10511 | ' | | string, or "None" if ' |
| 10512 | '| |\n' |
| 10513 | ' | | unavailable. ' |
| 10514 | '| |\n' |
| 10515 | ' ' |
| 10516 | '+-------------------------+---------------------------------+-------------+\n' |
| 10517 | ' | "__name__" "func_name" | The function\'s name ' |
| 10518 | '| Writable |\n' |
| 10519 | ' ' |
| 10520 | '+-------------------------+---------------------------------+-------------+\n' |
| 10521 | ' | "__module__" | The name of the module the | ' |
| 10522 | 'Writable |\n' |
| 10523 | ' | | function was defined in, or ' |
| 10524 | '| |\n' |
| 10525 | ' | | "None" if unavailable. ' |
| 10526 | '| |\n' |
| 10527 | ' ' |
| 10528 | '+-------------------------+---------------------------------+-------------+\n' |
| 10529 | ' | "__defaults__" | A tuple containing default | ' |
| 10530 | 'Writable |\n' |
| 10531 | ' | "func_defaults" | argument values for those ' |
| 10532 | '| |\n' |
| 10533 | ' | | arguments that have defaults, ' |
| 10534 | '| |\n' |
| 10535 | ' | | or "None" if no arguments have ' |
| 10536 | '| |\n' |
| 10537 | ' | | a default value. ' |
| 10538 | '| |\n' |
| 10539 | ' ' |
| 10540 | '+-------------------------+---------------------------------+-------------+\n' |
| 10541 | ' | "__code__" "func_code" | The code object representing | ' |
| 10542 | 'Writable |\n' |
| 10543 | ' | | the compiled function body. ' |
| 10544 | '| |\n' |
| 10545 | ' ' |
| 10546 | '+-------------------------+---------------------------------+-------------+\n' |
| 10547 | ' | "__globals__" | A reference to the dictionary | ' |
| 10548 | 'Read-only |\n' |
| 10549 | ' | "func_globals" | that holds the function\'s ' |
| 10550 | '| |\n' |
| 10551 | ' | | global variables --- the global ' |
| 10552 | '| |\n' |
| 10553 | ' | | namespace of the module in ' |
| 10554 | '| |\n' |
| 10555 | ' | | which the function was defined. ' |
| 10556 | '| |\n' |
| 10557 | ' ' |
| 10558 | '+-------------------------+---------------------------------+-------------+\n' |
| 10559 | ' | "__dict__" "func_dict" | The namespace supporting | ' |
| 10560 | 'Writable |\n' |
| 10561 | ' | | arbitrary function attributes. ' |
| 10562 | '| |\n' |
| 10563 | ' ' |
| 10564 | '+-------------------------+---------------------------------+-------------+\n' |
| 10565 | ' | "__closure__" | "None" or a tuple of cells that | ' |
| 10566 | 'Read-only |\n' |
| 10567 | ' | "func_closure" | contain bindings for the ' |
| 10568 | '| |\n' |
| 10569 | " | | function's free variables. " |
| 10570 | '| |\n' |
| 10571 | ' ' |
| 10572 | '+-------------------------+---------------------------------+-------------+\n' |
| 10573 | '\n' |
| 10574 | ' Most of the attributes labelled "Writable" check the type of ' |
| 10575 | 'the\n' |
| 10576 | ' assigned value.\n' |
| 10577 | '\n' |
| 10578 | ' Changed in version 2.4: "func_name" is now writable.\n' |
| 10579 | '\n' |
| 10580 | ' Changed in version 2.6: The double-underscore attributes\n' |
| 10581 | ' "__closure__", "__code__", "__defaults__", and "__globals__"\n' |
| 10582 | ' were introduced as aliases for the corresponding "func_*"\n' |
| 10583 | ' attributes for forwards compatibility with Python 3.\n' |
| 10584 | '\n' |
| 10585 | ' Function objects also support getting and setting arbitrary\n' |
| 10586 | ' attributes, which can be used, for example, to attach ' |
| 10587 | 'metadata\n' |
| 10588 | ' to functions. Regular attribute dot-notation is used to get ' |
| 10589 | 'and\n' |
| 10590 | ' set such attributes. *Note that the current implementation ' |
| 10591 | 'only\n' |
| 10592 | ' supports function attributes on user-defined functions. ' |
| 10593 | 'Function\n' |
| 10594 | ' attributes on built-in functions may be supported in the\n' |
| 10595 | ' future.*\n' |
| 10596 | '\n' |
| 10597 | " Additional information about a function's definition can be\n" |
| 10598 | ' retrieved from its code object; see the description of ' |
| 10599 | 'internal\n' |
| 10600 | ' types below.\n' |
| 10601 | '\n' |
| 10602 | ' User-defined methods\n' |
| 10603 | ' A user-defined method object combines a class, a class ' |
| 10604 | 'instance\n' |
| 10605 | ' (or "None") and any callable object (normally a user-defined\n' |
| 10606 | ' function).\n' |
| 10607 | '\n' |
| 10608 | ' Special read-only attributes: "im_self" is the class ' |
| 10609 | 'instance\n' |
| 10610 | ' object, "im_func" is the function object; "im_class" is the\n' |
| 10611 | ' class of "im_self" for bound methods or the class that asked ' |
| 10612 | 'for\n' |
| 10613 | ' the method for unbound methods; "__doc__" is the method\'s\n' |
| 10614 | ' documentation (same as "im_func.__doc__"); "__name__" is the\n' |
| 10615 | ' method name (same as "im_func.__name__"); "__module__" is ' |
| 10616 | 'the\n' |
| 10617 | ' name of the module the method was defined in, or "None" if\n' |
| 10618 | ' unavailable.\n' |
| 10619 | '\n' |
| 10620 | ' Changed in version 2.2: "im_self" used to refer to the class\n' |
| 10621 | ' that defined the method.\n' |
| 10622 | '\n' |
| 10623 | ' Changed in version 2.6: For Python 3 forward-compatibility,\n' |
| 10624 | ' "im_func" is also available as "__func__", and "im_self" as\n' |
| 10625 | ' "__self__".\n' |
| 10626 | '\n' |
| 10627 | ' Methods also support accessing (but not setting) the ' |
| 10628 | 'arbitrary\n' |
| 10629 | ' function attributes on the underlying function object.\n' |
| 10630 | '\n' |
| 10631 | ' User-defined method objects may be created when getting an\n' |
| 10632 | ' attribute of a class (perhaps via an instance of that class), ' |
| 10633 | 'if\n' |
| 10634 | ' that attribute is a user-defined function object, an unbound\n' |
| 10635 | ' user-defined method object, or a class method object. When ' |
| 10636 | 'the\n' |
| 10637 | ' attribute is a user-defined method object, a new method ' |
| 10638 | 'object\n' |
| 10639 | ' is only created if the class from which it is being retrieved ' |
| 10640 | 'is\n' |
| 10641 | ' the same as, or a derived class of, the class stored in the\n' |
| 10642 | ' original method object; otherwise, the original method object ' |
| 10643 | 'is\n' |
| 10644 | ' used as it is.\n' |
| 10645 | '\n' |
| 10646 | ' When a user-defined method object is created by retrieving a\n' |
| 10647 | ' user-defined function object from a class, its "im_self"\n' |
| 10648 | ' attribute is "None" and the method object is said to be ' |
| 10649 | 'unbound.\n' |
| 10650 | ' When one is created by retrieving a user-defined function ' |
| 10651 | 'object\n' |
| 10652 | ' from a class via one of its instances, its "im_self" ' |
| 10653 | 'attribute\n' |
| 10654 | ' is the instance, and the method object is said to be bound. ' |
| 10655 | 'In\n' |
| 10656 | ' either case, the new method\'s "im_class" attribute is the ' |
| 10657 | 'class\n' |
| 10658 | ' from which the retrieval takes place, and its "im_func"\n' |
| 10659 | ' attribute is the original function object.\n' |
| 10660 | '\n' |
| 10661 | ' When a user-defined method object is created by retrieving\n' |
| 10662 | ' another method object from a class or instance, the behaviour ' |
| 10663 | 'is\n' |
| 10664 | ' the same as for a function object, except that the "im_func"\n' |
| 10665 | ' attribute of the new instance is not the original method ' |
| 10666 | 'object\n' |
| 10667 | ' but its "im_func" attribute.\n' |
| 10668 | '\n' |
| 10669 | ' When a user-defined method object is created by retrieving a\n' |
| 10670 | ' class method object from a class or instance, its "im_self"\n' |
| 10671 | ' attribute is the class itself, and its "im_func" attribute ' |
| 10672 | 'is\n' |
| 10673 | ' the function object underlying the class method.\n' |
| 10674 | '\n' |
| 10675 | ' When an unbound user-defined method object is called, the\n' |
| 10676 | ' underlying function ("im_func") is called, with the ' |
| 10677 | 'restriction\n' |
| 10678 | ' that the first argument must be an instance of the proper ' |
| 10679 | 'class\n' |
| 10680 | ' ("im_class") or of a derived class thereof.\n' |
| 10681 | '\n' |
| 10682 | ' When a bound user-defined method object is called, the\n' |
| 10683 | ' underlying function ("im_func") is called, inserting the ' |
| 10684 | 'class\n' |
| 10685 | ' instance ("im_self") in front of the argument list. For\n' |
| 10686 | ' instance, when "C" is a class which contains a definition for ' |
| 10687 | 'a\n' |
| 10688 | ' function "f()", and "x" is an instance of "C", calling ' |
| 10689 | '"x.f(1)"\n' |
| 10690 | ' is equivalent to calling "C.f(x, 1)".\n' |
| 10691 | '\n' |
| 10692 | ' When a user-defined method object is derived from a class ' |
| 10693 | 'method\n' |
| 10694 | ' object, the "class instance" stored in "im_self" will ' |
| 10695 | 'actually\n' |
| 10696 | ' be the class itself, so that calling either "x.f(1)" or ' |
| 10697 | '"C.f(1)"\n' |
| 10698 | ' is equivalent to calling "f(C,1)" where "f" is the ' |
| 10699 | 'underlying\n' |
| 10700 | ' function.\n' |
| 10701 | '\n' |
| 10702 | ' Note that the transformation from function object to (unbound ' |
| 10703 | 'or\n' |
| 10704 | ' bound) method object happens each time the attribute is\n' |
| 10705 | ' retrieved from the class or instance. In some cases, a ' |
| 10706 | 'fruitful\n' |
| 10707 | ' optimization is to assign the attribute to a local variable ' |
| 10708 | 'and\n' |
| 10709 | ' call that local variable. Also notice that this ' |
| 10710 | 'transformation\n' |
| 10711 | ' only happens for user-defined functions; other callable ' |
| 10712 | 'objects\n' |
| 10713 | ' (and all non-callable objects) are retrieved without\n' |
| 10714 | ' transformation. It is also important to note that ' |
| 10715 | 'user-defined\n' |
| 10716 | ' functions which are attributes of a class instance are not\n' |
| 10717 | ' converted to bound methods; this *only* happens when the\n' |
| 10718 | ' function is an attribute of the class.\n' |
| 10719 | '\n' |
| 10720 | ' Generator functions\n' |
| 10721 | ' A function or method which uses the "yield" statement (see\n' |
| 10722 | ' section The yield statement) is called a *generator ' |
| 10723 | 'function*.\n' |
| 10724 | ' Such a function, when called, always returns an iterator ' |
| 10725 | 'object\n' |
| 10726 | ' which can be used to execute the body of the function: ' |
| 10727 | 'calling\n' |
| 10728 | ' the iterator\'s "next()" method will cause the function to\n' |
| 10729 | ' execute until it provides a value using the "yield" ' |
| 10730 | 'statement.\n' |
| 10731 | ' When the function executes a "return" statement or falls off ' |
| 10732 | 'the\n' |
| 10733 | ' end, a "StopIteration" exception is raised and the iterator ' |
| 10734 | 'will\n' |
| 10735 | ' have reached the end of the set of values to be returned.\n' |
| 10736 | '\n' |
| 10737 | ' Built-in functions\n' |
| 10738 | ' A built-in function object is a wrapper around a C function.\n' |
| 10739 | ' Examples of built-in functions are "len()" and "math.sin()"\n' |
| 10740 | ' ("math" is a standard built-in module). The number and type ' |
| 10741 | 'of\n' |
| 10742 | ' the arguments are determined by the C function. Special ' |
| 10743 | 'read-\n' |
| 10744 | ' only attributes: "__doc__" is the function\'s documentation\n' |
| 10745 | ' string, or "None" if unavailable; "__name__" is the ' |
| 10746 | "function's\n" |
| 10747 | ' name; "__self__" is set to "None" (but see the next item);\n' |
| 10748 | ' "__module__" is the name of the module the function was ' |
| 10749 | 'defined\n' |
| 10750 | ' in or "None" if unavailable.\n' |
| 10751 | '\n' |
| 10752 | ' Built-in methods\n' |
| 10753 | ' This is really a different disguise of a built-in function, ' |
| 10754 | 'this\n' |
| 10755 | ' time containing an object passed to the C function as an\n' |
| 10756 | ' implicit extra argument. An example of a built-in method is\n' |
| 10757 | ' "alist.append()", assuming *alist* is a list object. In this\n' |
| 10758 | ' case, the special read-only attribute "__self__" is set to ' |
| 10759 | 'the\n' |
| 10760 | ' object denoted by *alist*.\n' |
| 10761 | '\n' |
| 10762 | ' Class Types\n' |
| 10763 | ' Class types, or "new-style classes," are callable. These\n' |
| 10764 | ' objects normally act as factories for new instances of\n' |
| 10765 | ' themselves, but variations are possible for class types that\n' |
| 10766 | ' override "__new__()". The arguments of the call are passed ' |
| 10767 | 'to\n' |
| 10768 | ' "__new__()" and, in the typical case, to "__init__()" to\n' |
| 10769 | ' initialize the new instance.\n' |
| 10770 | '\n' |
| 10771 | ' Classic Classes\n' |
| 10772 | ' Class objects are described below. When a class object is\n' |
| 10773 | ' called, a new class instance (also described below) is ' |
| 10774 | 'created\n' |
| 10775 | " and returned. This implies a call to the class's " |
| 10776 | '"__init__()"\n' |
| 10777 | ' method if it has one. Any arguments are passed on to the\n' |
| 10778 | ' "__init__()" method. If there is no "__init__()" method, ' |
| 10779 | 'the\n' |
| 10780 | ' class must be called without arguments.\n' |
| 10781 | '\n' |
| 10782 | ' Class instances\n' |
| 10783 | ' Class instances are described below. Class instances are\n' |
| 10784 | ' callable only when the class has a "__call__()" method;\n' |
| 10785 | ' "x(arguments)" is a shorthand for "x.__call__(arguments)".\n' |
| 10786 | '\n' |
| 10787 | 'Modules\n' |
| 10788 | ' Modules are imported by the "import" statement (see section The\n' |
| 10789 | ' import statement). A module object has a namespace implemented ' |
| 10790 | 'by a\n' |
| 10791 | ' dictionary object (this is the dictionary referenced by the\n' |
| 10792 | ' func_globals attribute of functions defined in the module).\n' |
| 10793 | ' Attribute references are translated to lookups in this ' |
| 10794 | 'dictionary,\n' |
| 10795 | ' e.g., "m.x" is equivalent to "m.__dict__["x"]". A module object\n' |
| 10796 | ' does not contain the code object used to initialize the module\n' |
| 10797 | " (since it isn't needed once the initialization is done).\n" |
| 10798 | '\n' |
| 10799 | " Attribute assignment updates the module's namespace dictionary,\n" |
| 10800 | ' e.g., "m.x = 1" is equivalent to "m.__dict__["x"] = 1".\n' |
| 10801 | '\n' |
| 10802 | ' Special read-only attribute: "__dict__" is the module\'s ' |
| 10803 | 'namespace\n' |
| 10804 | ' as a dictionary object.\n' |
| 10805 | '\n' |
| 10806 | ' **CPython implementation detail:** Because of the way CPython\n' |
| 10807 | ' clears module dictionaries, the module dictionary will be ' |
| 10808 | 'cleared\n' |
| 10809 | ' when the module falls out of scope even if the dictionary still ' |
| 10810 | 'has\n' |
| 10811 | ' live references. To avoid this, copy the dictionary or keep ' |
| 10812 | 'the\n' |
| 10813 | ' module around while using its dictionary directly.\n' |
| 10814 | '\n' |
| 10815 | ' Predefined (writable) attributes: "__name__" is the module\'s ' |
| 10816 | 'name;\n' |
| 10817 | ' "__doc__" is the module\'s documentation string, or "None" if\n' |
| 10818 | ' unavailable; "__file__" is the pathname of the file from which ' |
| 10819 | 'the\n' |
| 10820 | ' module was loaded, if it was loaded from a file. The "__file__"\n' |
| 10821 | ' attribute is not present for C modules that are statically ' |
| 10822 | 'linked\n' |
| 10823 | ' into the interpreter; for extension modules loaded dynamically ' |
| 10824 | 'from\n' |
| 10825 | ' a shared library, it is the pathname of the shared library ' |
| 10826 | 'file.\n' |
| 10827 | '\n' |
| 10828 | 'Classes\n' |
| 10829 | ' Both class types (new-style classes) and class objects (old-\n' |
| 10830 | ' style/classic classes) are typically created by class ' |
| 10831 | 'definitions\n' |
| 10832 | ' (see section Class definitions). A class has a namespace\n' |
| 10833 | ' implemented by a dictionary object. Class attribute references ' |
| 10834 | 'are\n' |
| 10835 | ' translated to lookups in this dictionary, e.g., "C.x" is ' |
| 10836 | 'translated\n' |
| 10837 | ' to "C.__dict__["x"]" (although for new-style classes in ' |
| 10838 | 'particular\n' |
| 10839 | ' there are a number of hooks which allow for other means of ' |
| 10840 | 'locating\n' |
| 10841 | ' attributes). When the attribute name is not found there, the\n' |
| 10842 | ' attribute search continues in the base classes. For old-style\n' |
| 10843 | ' classes, the search is depth-first, left-to-right in the order ' |
| 10844 | 'of\n' |
| 10845 | ' occurrence in the base class list. New-style classes use the ' |
| 10846 | 'more\n' |
| 10847 | ' complex C3 method resolution order which behaves correctly even ' |
| 10848 | 'in\n' |
| 10849 | " the presence of 'diamond' inheritance structures where there " |
| 10850 | 'are\n' |
| 10851 | ' multiple inheritance paths leading back to a common ancestor.\n' |
| 10852 | ' Additional details on the C3 MRO used by new-style classes can ' |
| 10853 | 'be\n' |
| 10854 | ' found in the documentation accompanying the 2.3 release at\n' |
| 10855 | ' https://www.python.org/download/releases/2.3/mro/.\n' |
| 10856 | '\n' |
| 10857 | ' When a class attribute reference (for class "C", say) would ' |
| 10858 | 'yield a\n' |
| 10859 | ' user-defined function object or an unbound user-defined method\n' |
| 10860 | ' object whose associated class is either "C" or one of its base\n' |
| 10861 | ' classes, it is transformed into an unbound user-defined method\n' |
| 10862 | ' object whose "im_class" attribute is "C". When it would yield a\n' |
| 10863 | ' class method object, it is transformed into a bound ' |
| 10864 | 'user-defined\n' |
| 10865 | ' method object whose "im_self" attribute is "C". When it would\n' |
| 10866 | ' yield a static method object, it is transformed into the object\n' |
| 10867 | ' wrapped by the static method object. See section Implementing\n' |
| 10868 | ' Descriptors for another way in which attributes retrieved from ' |
| 10869 | 'a\n' |
| 10870 | ' class may differ from those actually contained in its ' |
| 10871 | '"__dict__"\n' |
| 10872 | ' (note that only new-style classes support descriptors).\n' |
| 10873 | '\n' |
| 10874 | " Class attribute assignments update the class's dictionary, " |
| 10875 | 'never\n' |
| 10876 | ' the dictionary of a base class.\n' |
| 10877 | '\n' |
| 10878 | ' A class object can be called (see above) to yield a class ' |
| 10879 | 'instance\n' |
| 10880 | ' (see below).\n' |
| 10881 | '\n' |
| 10882 | ' Special attributes: "__name__" is the class name; "__module__" ' |
| 10883 | 'is\n' |
| 10884 | ' the module name in which the class was defined; "__dict__" is ' |
| 10885 | 'the\n' |
| 10886 | ' dictionary containing the class\'s namespace; "__bases__" is a ' |
| 10887 | 'tuple\n' |
| 10888 | ' (possibly empty or a singleton) containing the base classes, in ' |
| 10889 | 'the\n' |
| 10890 | ' order of their occurrence in the base class list; "__doc__" is ' |
| 10891 | 'the\n' |
| 10892 | ' class\'s documentation string, or "None" if undefined.\n' |
| 10893 | '\n' |
| 10894 | 'Class instances\n' |
| 10895 | ' A class instance is created by calling a class object (see ' |
| 10896 | 'above).\n' |
| 10897 | ' A class instance has a namespace implemented as a dictionary ' |
| 10898 | 'which\n' |
| 10899 | ' is the first place in which attribute references are searched.\n' |
| 10900 | " When an attribute is not found there, and the instance's class " |
| 10901 | 'has\n' |
| 10902 | ' an attribute by that name, the search continues with the class\n' |
| 10903 | ' attributes. If a class attribute is found that is a ' |
| 10904 | 'user-defined\n' |
| 10905 | ' function object or an unbound user-defined method object whose\n' |
| 10906 | ' associated class is the class (call it "C") of the instance for\n' |
| 10907 | ' which the attribute reference was initiated or one of its bases, ' |
| 10908 | 'it\n' |
| 10909 | ' is transformed into a bound user-defined method object whose\n' |
| 10910 | ' "im_class" attribute is "C" and whose "im_self" attribute is ' |
| 10911 | 'the\n' |
| 10912 | ' instance. Static method and class method objects are also\n' |
| 10913 | ' transformed, as if they had been retrieved from class "C"; see\n' |
| 10914 | ' above under "Classes". See section Implementing Descriptors for\n' |
| 10915 | ' another way in which attributes of a class retrieved via its\n' |
| 10916 | ' instances may differ from the objects actually stored in the\n' |
| 10917 | ' class\'s "__dict__". If no class attribute is found, and the\n' |
| 10918 | ' object\'s class has a "__getattr__()" method, that is called to\n' |
| 10919 | ' satisfy the lookup.\n' |
| 10920 | '\n' |
| 10921 | " Attribute assignments and deletions update the instance's\n" |
| 10922 | " dictionary, never a class's dictionary. If the class has a\n" |
| 10923 | ' "__setattr__()" or "__delattr__()" method, this is called ' |
| 10924 | 'instead\n' |
| 10925 | ' of updating the instance dictionary directly.\n' |
| 10926 | '\n' |
| 10927 | ' Class instances can pretend to be numbers, sequences, or ' |
| 10928 | 'mappings\n' |
| 10929 | ' if they have methods with certain special names. See section\n' |
| 10930 | ' Special method names.\n' |
| 10931 | '\n' |
| 10932 | ' Special attributes: "__dict__" is the attribute dictionary;\n' |
| 10933 | ' "__class__" is the instance\'s class.\n' |
| 10934 | '\n' |
| 10935 | 'Files\n' |
| 10936 | ' A file object represents an open file. File objects are created ' |
| 10937 | 'by\n' |
| 10938 | ' the "open()" built-in function, and also by "os.popen()",\n' |
| 10939 | ' "os.fdopen()", and the "makefile()" method of socket objects ' |
| 10940 | '(and\n' |
| 10941 | ' perhaps by other functions or methods provided by extension\n' |
| 10942 | ' modules). The objects "sys.stdin", "sys.stdout" and ' |
| 10943 | '"sys.stderr"\n' |
| 10944 | ' are initialized to file objects corresponding to the ' |
| 10945 | "interpreter's\n" |
| 10946 | ' standard input, output and error streams. See File Objects for\n' |
| 10947 | ' complete documentation of file objects.\n' |
| 10948 | '\n' |
| 10949 | 'Internal types\n' |
| 10950 | ' A few types used internally by the interpreter are exposed to ' |
| 10951 | 'the\n' |
| 10952 | ' user. Their definitions may change with future versions of the\n' |
| 10953 | ' interpreter, but they are mentioned here for completeness.\n' |
| 10954 | '\n' |
| 10955 | ' Code objects\n' |
| 10956 | ' Code objects represent *byte-compiled* executable Python ' |
| 10957 | 'code,\n' |
| 10958 | ' or *bytecode*. The difference between a code object and a\n' |
| 10959 | ' function object is that the function object contains an ' |
| 10960 | 'explicit\n' |
| 10961 | " reference to the function's globals (the module in which it " |
| 10962 | 'was\n' |
| 10963 | ' defined), while a code object contains no context; also the\n' |
| 10964 | ' default argument values are stored in the function object, ' |
| 10965 | 'not\n' |
| 10966 | ' in the code object (because they represent values calculated ' |
| 10967 | 'at\n' |
| 10968 | ' run-time). Unlike function objects, code objects are ' |
| 10969 | 'immutable\n' |
| 10970 | ' and contain no references (directly or indirectly) to ' |
| 10971 | 'mutable\n' |
| 10972 | ' objects.\n' |
| 10973 | '\n' |
| 10974 | ' Special read-only attributes: "co_name" gives the function ' |
| 10975 | 'name;\n' |
| 10976 | ' "co_argcount" is the number of positional arguments ' |
| 10977 | '(including\n' |
| 10978 | ' arguments with default values); "co_nlocals" is the number ' |
| 10979 | 'of\n' |
| 10980 | ' local variables used by the function (including arguments);\n' |
| 10981 | ' "co_varnames" is a tuple containing the names of the local\n' |
| 10982 | ' variables (starting with the argument names); "co_cellvars" ' |
| 10983 | 'is a\n' |
| 10984 | ' tuple containing the names of local variables that are\n' |
| 10985 | ' referenced by nested functions; "co_freevars" is a tuple\n' |
| 10986 | ' containing the names of free variables; "co_code" is a ' |
| 10987 | 'string\n' |
| 10988 | ' representing the sequence of bytecode instructions; ' |
| 10989 | '"co_consts"\n' |
| 10990 | ' is a tuple containing the literals used by the bytecode;\n' |
| 10991 | ' "co_names" is a tuple containing the names used by the ' |
| 10992 | 'bytecode;\n' |
| 10993 | ' "co_filename" is the filename from which the code was ' |
| 10994 | 'compiled;\n' |
| 10995 | ' "co_firstlineno" is the first line number of the function;\n' |
| 10996 | ' "co_lnotab" is a string encoding the mapping from bytecode\n' |
| 10997 | ' offsets to line numbers (for details see the source code of ' |
| 10998 | 'the\n' |
| 10999 | ' interpreter); "co_stacksize" is the required stack size\n' |
| 11000 | ' (including local variables); "co_flags" is an integer ' |
| 11001 | 'encoding a\n' |
| 11002 | ' number of flags for the interpreter.\n' |
| 11003 | '\n' |
| 11004 | ' The following flag bits are defined for "co_flags": bit ' |
| 11005 | '"0x04"\n' |
| 11006 | ' is set if the function uses the "*arguments" syntax to accept ' |
| 11007 | 'an\n' |
| 11008 | ' arbitrary number of positional arguments; bit "0x08" is set ' |
| 11009 | 'if\n' |
| 11010 | ' the function uses the "**keywords" syntax to accept ' |
| 11011 | 'arbitrary\n' |
| 11012 | ' keyword arguments; bit "0x20" is set if the function is a\n' |
| 11013 | ' generator.\n' |
| 11014 | '\n' |
| 11015 | ' Future feature declarations ("from __future__ import ' |
| 11016 | 'division")\n' |
| 11017 | ' also use bits in "co_flags" to indicate whether a code ' |
| 11018 | 'object\n' |
| 11019 | ' was compiled with a particular feature enabled: bit "0x2000" ' |
| 11020 | 'is\n' |
| 11021 | ' set if the function was compiled with future division ' |
| 11022 | 'enabled;\n' |
| 11023 | ' bits "0x10" and "0x1000" were used in earlier versions of\n' |
| 11024 | ' Python.\n' |
| 11025 | '\n' |
| 11026 | ' Other bits in "co_flags" are reserved for internal use.\n' |
| 11027 | '\n' |
| 11028 | ' If a code object represents a function, the first item in\n' |
| 11029 | ' "co_consts" is the documentation string of the function, or\n' |
| 11030 | ' "None" if undefined.\n' |
| 11031 | '\n' |
| 11032 | ' Frame objects\n' |
| 11033 | ' Frame objects represent execution frames. They may occur in\n' |
| 11034 | ' traceback objects (see below).\n' |
| 11035 | '\n' |
| 11036 | ' Special read-only attributes: "f_back" is to the previous ' |
| 11037 | 'stack\n' |
| 11038 | ' frame (towards the caller), or "None" if this is the bottom\n' |
| 11039 | ' stack frame; "f_code" is the code object being executed in ' |
| 11040 | 'this\n' |
| 11041 | ' frame; "f_locals" is the dictionary used to look up local\n' |
| 11042 | ' variables; "f_globals" is used for global variables;\n' |
| 11043 | ' "f_builtins" is used for built-in (intrinsic) names;\n' |
| 11044 | ' "f_restricted" is a flag indicating whether the function is\n' |
| 11045 | ' executing in restricted execution mode; "f_lasti" gives the\n' |
| 11046 | ' precise instruction (this is an index into the bytecode ' |
| 11047 | 'string\n' |
| 11048 | ' of the code object).\n' |
| 11049 | '\n' |
| 11050 | ' Special writable attributes: "f_trace", if not "None", is a\n' |
| 11051 | ' function called at the start of each source code line (this ' |
| 11052 | 'is\n' |
| 11053 | ' used by the debugger); "f_exc_type", "f_exc_value",\n' |
| 11054 | ' "f_exc_traceback" represent the last exception raised in the\n' |
| 11055 | ' parent frame provided another exception was ever raised in ' |
| 11056 | 'the\n' |
| 11057 | ' current frame (in all other cases they are "None"); ' |
| 11058 | '"f_lineno"\n' |
| 11059 | ' is the current line number of the frame --- writing to this ' |
| 11060 | 'from\n' |
| 11061 | ' within a trace function jumps to the given line (only for ' |
| 11062 | 'the\n' |
| 11063 | ' bottom-most frame). A debugger can implement a Jump command\n' |
| 11064 | ' (aka Set Next Statement) by writing to f_lineno.\n' |
| 11065 | '\n' |
| 11066 | ' Traceback objects\n' |
| 11067 | ' Traceback objects represent a stack trace of an exception. ' |
| 11068 | 'A\n' |
| 11069 | ' traceback object is created when an exception occurs. When ' |
| 11070 | 'the\n' |
| 11071 | ' search for an exception handler unwinds the execution stack, ' |
| 11072 | 'at\n' |
| 11073 | ' each unwound level a traceback object is inserted in front ' |
| 11074 | 'of\n' |
| 11075 | ' the current traceback. When an exception handler is ' |
| 11076 | 'entered,\n' |
| 11077 | ' the stack trace is made available to the program. (See ' |
| 11078 | 'section\n' |
| 11079 | ' The try statement.) It is accessible as "sys.exc_traceback", ' |
| 11080 | 'and\n' |
| 11081 | ' also as the third item of the tuple returned by\n' |
| 11082 | ' "sys.exc_info()". The latter is the preferred interface, ' |
| 11083 | 'since\n' |
| 11084 | ' it works correctly when the program is using multiple ' |
| 11085 | 'threads.\n' |
| 11086 | ' When the program contains no suitable handler, the stack ' |
| 11087 | 'trace\n' |
| 11088 | ' is written (nicely formatted) to the standard error stream; ' |
| 11089 | 'if\n' |
| 11090 | ' the interpreter is interactive, it is also made available to ' |
| 11091 | 'the\n' |
| 11092 | ' user as "sys.last_traceback".\n' |
| 11093 | '\n' |
| 11094 | ' Special read-only attributes: "tb_next" is the next level in ' |
| 11095 | 'the\n' |
| 11096 | ' stack trace (towards the frame where the exception occurred), ' |
| 11097 | 'or\n' |
| 11098 | ' "None" if there is no next level; "tb_frame" points to the\n' |
| 11099 | ' execution frame of the current level; "tb_lineno" gives the ' |
| 11100 | 'line\n' |
| 11101 | ' number where the exception occurred; "tb_lasti" indicates ' |
| 11102 | 'the\n' |
| 11103 | ' precise instruction. The line number and last instruction ' |
| 11104 | 'in\n' |
| 11105 | ' the traceback may differ from the line number of its frame\n' |
| 11106 | ' object if the exception occurred in a "try" statement with ' |
| 11107 | 'no\n' |
| 11108 | ' matching except clause or with a finally clause.\n' |
| 11109 | '\n' |
| 11110 | ' Slice objects\n' |
| 11111 | ' Slice objects are used to represent slices when *extended ' |
| 11112 | 'slice\n' |
| 11113 | ' syntax* is used. This is a slice using two colons, or ' |
| 11114 | 'multiple\n' |
| 11115 | ' slices or ellipses separated by commas, e.g., "a[i:j:step]",\n' |
| 11116 | ' "a[i:j, k:l]", or "a[..., i:j]". They are also created by ' |
| 11117 | 'the\n' |
| 11118 | ' built-in "slice()" function.\n' |
| 11119 | '\n' |
| 11120 | ' Special read-only attributes: "start" is the lower bound; ' |
| 11121 | '"stop"\n' |
| 11122 | ' is the upper bound; "step" is the step value; each is "None" ' |
| 11123 | 'if\n' |
| 11124 | ' omitted. These attributes can have any type.\n' |
| 11125 | '\n' |
| 11126 | ' Slice objects support one method:\n' |
| 11127 | '\n' |
| 11128 | ' slice.indices(self, length)\n' |
| 11129 | '\n' |
| 11130 | ' This method takes a single integer argument *length* and\n' |
| 11131 | ' computes information about the extended slice that the ' |
| 11132 | 'slice\n' |
| 11133 | ' object would describe if applied to a sequence of ' |
| 11134 | '*length*\n' |
| 11135 | ' items. It returns a tuple of three integers; ' |
| 11136 | 'respectively\n' |
| 11137 | ' these are the *start* and *stop* indices and the *step* ' |
| 11138 | 'or\n' |
| 11139 | ' stride length of the slice. Missing or out-of-bounds ' |
| 11140 | 'indices\n' |
| 11141 | ' are handled in a manner consistent with regular slices.\n' |
| 11142 | '\n' |
| 11143 | ' New in version 2.3.\n' |
| 11144 | '\n' |
| 11145 | ' Static method objects\n' |
| 11146 | ' Static method objects provide a way of defeating the\n' |
| 11147 | ' transformation of function objects to method objects ' |
| 11148 | 'described\n' |
| 11149 | ' above. A static method object is a wrapper around any other\n' |
| 11150 | ' object, usually a user-defined method object. When a static\n' |
| 11151 | ' method object is retrieved from a class or a class instance, ' |
| 11152 | 'the\n' |
| 11153 | ' object actually returned is the wrapped object, which is not\n' |
| 11154 | ' subject to any further transformation. Static method objects ' |
| 11155 | 'are\n' |
| 11156 | ' not themselves callable, although the objects they wrap ' |
| 11157 | 'usually\n' |
| 11158 | ' are. Static method objects are created by the built-in\n' |
| 11159 | ' "staticmethod()" constructor.\n' |
| 11160 | '\n' |
| 11161 | ' Class method objects\n' |
| 11162 | ' A class method object, like a static method object, is a ' |
| 11163 | 'wrapper\n' |
| 11164 | ' around another object that alters the way in which that ' |
| 11165 | 'object\n' |
| 11166 | ' is retrieved from classes and class instances. The behaviour ' |
| 11167 | 'of\n' |
| 11168 | ' class method objects upon such retrieval is described above,\n' |
| 11169 | ' under "User-defined methods". Class method objects are ' |
| 11170 | 'created\n' |
| 11171 | ' by the built-in "classmethod()" constructor.\n', |
| 11172 | 'typesfunctions': '\n' |
| 11173 | 'Functions\n' |
| 11174 | '*********\n' |
| 11175 | '\n' |
| 11176 | 'Function objects are created by function definitions. The ' |
| 11177 | 'only\n' |
| 11178 | 'operation on a function object is to call it: ' |
| 11179 | '"func(argument-list)".\n' |
| 11180 | '\n' |
| 11181 | 'There are really two flavors of function objects: built-in ' |
| 11182 | 'functions\n' |
| 11183 | 'and user-defined functions. Both support the same ' |
| 11184 | 'operation (to call\n' |
| 11185 | 'the function), but the implementation is different, hence ' |
| 11186 | 'the\n' |
| 11187 | 'different object types.\n' |
| 11188 | '\n' |
| 11189 | 'See Function definitions for more information.\n', |
| 11190 | 'typesmapping': '\n' |
| 11191 | 'Mapping Types --- "dict"\n' |
| 11192 | '************************\n' |
| 11193 | '\n' |
| 11194 | 'A *mapping* object maps *hashable* values to arbitrary ' |
| 11195 | 'objects.\n' |
| 11196 | 'Mappings are mutable objects. There is currently only one ' |
| 11197 | 'standard\n' |
| 11198 | 'mapping type, the *dictionary*. (For other containers see ' |
| 11199 | 'the built\n' |
| 11200 | 'in "list", "set", and "tuple" classes, and the "collections" ' |
| 11201 | 'module.)\n' |
| 11202 | '\n' |
| 11203 | "A dictionary's keys are *almost* arbitrary values. Values " |
| 11204 | 'that are\n' |
| 11205 | 'not *hashable*, that is, values containing lists, ' |
| 11206 | 'dictionaries or\n' |
| 11207 | 'other mutable types (that are compared by value rather than ' |
| 11208 | 'by object\n' |
| 11209 | 'identity) may not be used as keys. Numeric types used for ' |
| 11210 | 'keys obey\n' |
| 11211 | 'the normal rules for numeric comparison: if two numbers ' |
| 11212 | 'compare equal\n' |
| 11213 | '(such as "1" and "1.0") then they can be used ' |
| 11214 | 'interchangeably to index\n' |
| 11215 | 'the same dictionary entry. (Note however, that since ' |
| 11216 | 'computers store\n' |
| 11217 | 'floating-point numbers as approximations it is usually ' |
| 11218 | 'unwise to use\n' |
| 11219 | 'them as dictionary keys.)\n' |
| 11220 | '\n' |
| 11221 | 'Dictionaries can be created by placing a comma-separated ' |
| 11222 | 'list of "key:\n' |
| 11223 | 'value" pairs within braces, for example: "{\'jack\': 4098, ' |
| 11224 | "'sjoerd':\n" |
| 11225 | '4127}" or "{4098: \'jack\', 4127: \'sjoerd\'}", or by the ' |
| 11226 | '"dict"\n' |
| 11227 | 'constructor.\n' |
| 11228 | '\n' |
| 11229 | 'class dict(**kwarg)\n' |
| 11230 | 'class dict(mapping, **kwarg)\n' |
| 11231 | 'class dict(iterable, **kwarg)\n' |
| 11232 | '\n' |
| 11233 | ' Return a new dictionary initialized from an optional ' |
| 11234 | 'positional\n' |
| 11235 | ' argument and a possibly empty set of keyword arguments.\n' |
| 11236 | '\n' |
| 11237 | ' If no positional argument is given, an empty dictionary ' |
| 11238 | 'is created.\n' |
| 11239 | ' If a positional argument is given and it is a mapping ' |
| 11240 | 'object, a\n' |
| 11241 | ' dictionary is created with the same key-value pairs as ' |
| 11242 | 'the mapping\n' |
| 11243 | ' object. Otherwise, the positional argument must be an ' |
| 11244 | '*iterable*\n' |
| 11245 | ' object. Each item in the iterable must itself be an ' |
| 11246 | 'iterable with\n' |
| 11247 | ' exactly two objects. The first object of each item ' |
| 11248 | 'becomes a key\n' |
| 11249 | ' in the new dictionary, and the second object the ' |
| 11250 | 'corresponding\n' |
| 11251 | ' value. If a key occurs more than once, the last value ' |
| 11252 | 'for that key\n' |
| 11253 | ' becomes the corresponding value in the new dictionary.\n' |
| 11254 | '\n' |
| 11255 | ' If keyword arguments are given, the keyword arguments and ' |
| 11256 | 'their\n' |
| 11257 | ' values are added to the dictionary created from the ' |
| 11258 | 'positional\n' |
| 11259 | ' argument. If a key being added is already present, the ' |
| 11260 | 'value from\n' |
| 11261 | ' the keyword argument replaces the value from the ' |
| 11262 | 'positional\n' |
| 11263 | ' argument.\n' |
| 11264 | '\n' |
| 11265 | ' To illustrate, the following examples all return a ' |
| 11266 | 'dictionary equal\n' |
| 11267 | ' to "{"one": 1, "two": 2, "three": 3}":\n' |
| 11268 | '\n' |
| 11269 | ' >>> a = dict(one=1, two=2, three=3)\n' |
| 11270 | " >>> b = {'one': 1, 'two': 2, 'three': 3}\n" |
| 11271 | " >>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))\n" |
| 11272 | " >>> d = dict([('two', 2), ('one', 1), ('three', 3)])\n" |
| 11273 | " >>> e = dict({'three': 3, 'one': 1, 'two': 2})\n" |
| 11274 | ' >>> a == b == c == d == e\n' |
| 11275 | ' True\n' |
| 11276 | '\n' |
| 11277 | ' Providing keyword arguments as in the first example only ' |
| 11278 | 'works for\n' |
| 11279 | ' keys that are valid Python identifiers. Otherwise, any ' |
| 11280 | 'valid keys\n' |
| 11281 | ' can be used.\n' |
| 11282 | '\n' |
| 11283 | ' New in version 2.2.\n' |
| 11284 | '\n' |
| 11285 | ' Changed in version 2.3: Support for building a dictionary ' |
| 11286 | 'from\n' |
| 11287 | ' keyword arguments added.\n' |
| 11288 | '\n' |
| 11289 | ' These are the operations that dictionaries support (and ' |
| 11290 | 'therefore,\n' |
| 11291 | ' custom mapping types should support too):\n' |
| 11292 | '\n' |
| 11293 | ' len(d)\n' |
| 11294 | '\n' |
| 11295 | ' Return the number of items in the dictionary *d*.\n' |
| 11296 | '\n' |
| 11297 | ' d[key]\n' |
| 11298 | '\n' |
| 11299 | ' Return the item of *d* with key *key*. Raises a ' |
| 11300 | '"KeyError" if\n' |
| 11301 | ' *key* is not in the map.\n' |
| 11302 | '\n' |
| 11303 | ' If a subclass of dict defines a method "__missing__()" ' |
| 11304 | 'and *key*\n' |
| 11305 | ' is not present, the "d[key]" operation calls that ' |
| 11306 | 'method with\n' |
| 11307 | ' the key *key* as argument. The "d[key]" operation ' |
| 11308 | 'then returns\n' |
| 11309 | ' or raises whatever is returned or raised by the\n' |
| 11310 | ' "__missing__(key)" call. No other operations or ' |
| 11311 | 'methods invoke\n' |
| 11312 | ' "__missing__()". If "__missing__()" is not defined, ' |
| 11313 | '"KeyError"\n' |
| 11314 | ' is raised. "__missing__()" must be a method; it cannot ' |
| 11315 | 'be an\n' |
| 11316 | ' instance variable:\n' |
| 11317 | '\n' |
| 11318 | ' >>> class Counter(dict):\n' |
| 11319 | ' ... def __missing__(self, key):\n' |
| 11320 | ' ... return 0\n' |
| 11321 | ' >>> c = Counter()\n' |
| 11322 | " >>> c['red']\n" |
| 11323 | ' 0\n' |
| 11324 | " >>> c['red'] += 1\n" |
| 11325 | " >>> c['red']\n" |
| 11326 | ' 1\n' |
| 11327 | '\n' |
| 11328 | ' The example above shows part of the implementation of\n' |
| 11329 | ' "collections.Counter". A different "__missing__" ' |
| 11330 | 'method is used\n' |
| 11331 | ' by "collections.defaultdict".\n' |
| 11332 | '\n' |
| 11333 | ' New in version 2.5: Recognition of __missing__ methods ' |
| 11334 | 'of dict\n' |
| 11335 | ' subclasses.\n' |
| 11336 | '\n' |
| 11337 | ' d[key] = value\n' |
| 11338 | '\n' |
| 11339 | ' Set "d[key]" to *value*.\n' |
| 11340 | '\n' |
| 11341 | ' del d[key]\n' |
| 11342 | '\n' |
| 11343 | ' Remove "d[key]" from *d*. Raises a "KeyError" if ' |
| 11344 | '*key* is not\n' |
| 11345 | ' in the map.\n' |
| 11346 | '\n' |
| 11347 | ' key in d\n' |
| 11348 | '\n' |
| 11349 | ' Return "True" if *d* has a key *key*, else "False".\n' |
| 11350 | '\n' |
| 11351 | ' New in version 2.2.\n' |
| 11352 | '\n' |
| 11353 | ' key not in d\n' |
| 11354 | '\n' |
| 11355 | ' Equivalent to "not key in d".\n' |
| 11356 | '\n' |
| 11357 | ' New in version 2.2.\n' |
| 11358 | '\n' |
| 11359 | ' iter(d)\n' |
| 11360 | '\n' |
| 11361 | ' Return an iterator over the keys of the dictionary. ' |
| 11362 | 'This is a\n' |
| 11363 | ' shortcut for "iterkeys()".\n' |
| 11364 | '\n' |
| 11365 | ' clear()\n' |
| 11366 | '\n' |
| 11367 | ' Remove all items from the dictionary.\n' |
| 11368 | '\n' |
| 11369 | ' copy()\n' |
| 11370 | '\n' |
| 11371 | ' Return a shallow copy of the dictionary.\n' |
| 11372 | '\n' |
| 11373 | ' fromkeys(seq[, value])\n' |
| 11374 | '\n' |
| 11375 | ' Create a new dictionary with keys from *seq* and ' |
| 11376 | 'values set to\n' |
| 11377 | ' *value*.\n' |
| 11378 | '\n' |
| 11379 | ' "fromkeys()" is a class method that returns a new ' |
| 11380 | 'dictionary.\n' |
| 11381 | ' *value* defaults to "None".\n' |
| 11382 | '\n' |
| 11383 | ' New in version 2.3.\n' |
| 11384 | '\n' |
| 11385 | ' get(key[, default])\n' |
| 11386 | '\n' |
| 11387 | ' Return the value for *key* if *key* is in the ' |
| 11388 | 'dictionary, else\n' |
| 11389 | ' *default*. If *default* is not given, it defaults to ' |
| 11390 | '"None", so\n' |
| 11391 | ' that this method never raises a "KeyError".\n' |
| 11392 | '\n' |
| 11393 | ' has_key(key)\n' |
| 11394 | '\n' |
| 11395 | ' Test for the presence of *key* in the dictionary. ' |
| 11396 | '"has_key()"\n' |
| 11397 | ' is deprecated in favor of "key in d".\n' |
| 11398 | '\n' |
| 11399 | ' items()\n' |
| 11400 | '\n' |
| 11401 | ' Return a copy of the dictionary\'s list of "(key, ' |
| 11402 | 'value)" pairs.\n' |
| 11403 | '\n' |
| 11404 | ' **CPython implementation detail:** Keys and values are ' |
| 11405 | 'listed in\n' |
| 11406 | ' an arbitrary order which is non-random, varies across ' |
| 11407 | 'Python\n' |
| 11408 | " implementations, and depends on the dictionary's " |
| 11409 | 'history of\n' |
| 11410 | ' insertions and deletions.\n' |
| 11411 | '\n' |
| 11412 | ' If "items()", "keys()", "values()", "iteritems()", ' |
| 11413 | '"iterkeys()",\n' |
| 11414 | ' and "itervalues()" are called with no intervening ' |
| 11415 | 'modifications\n' |
| 11416 | ' to the dictionary, the lists will directly ' |
| 11417 | 'correspond. This\n' |
| 11418 | ' allows the creation of "(value, key)" pairs using ' |
| 11419 | '"zip()":\n' |
| 11420 | ' "pairs = zip(d.values(), d.keys())". The same ' |
| 11421 | 'relationship\n' |
| 11422 | ' holds for the "iterkeys()" and "itervalues()" methods: ' |
| 11423 | '"pairs =\n' |
| 11424 | ' zip(d.itervalues(), d.iterkeys())" provides the same ' |
| 11425 | 'value for\n' |
| 11426 | ' "pairs". Another way to create the same list is "pairs ' |
| 11427 | '= [(v, k)\n' |
| 11428 | ' for (k, v) in d.iteritems()]".\n' |
| 11429 | '\n' |
| 11430 | ' iteritems()\n' |
| 11431 | '\n' |
| 11432 | ' Return an iterator over the dictionary\'s "(key, ' |
| 11433 | 'value)" pairs.\n' |
| 11434 | ' See the note for "dict.items()".\n' |
| 11435 | '\n' |
| 11436 | ' Using "iteritems()" while adding or deleting entries ' |
| 11437 | 'in the\n' |
| 11438 | ' dictionary may raise a "RuntimeError" or fail to ' |
| 11439 | 'iterate over\n' |
| 11440 | ' all entries.\n' |
| 11441 | '\n' |
| 11442 | ' New in version 2.2.\n' |
| 11443 | '\n' |
| 11444 | ' iterkeys()\n' |
| 11445 | '\n' |
| 11446 | " Return an iterator over the dictionary's keys. See " |
| 11447 | 'the note for\n' |
| 11448 | ' "dict.items()".\n' |
| 11449 | '\n' |
| 11450 | ' Using "iterkeys()" while adding or deleting entries in ' |
| 11451 | 'the\n' |
| 11452 | ' dictionary may raise a "RuntimeError" or fail to ' |
| 11453 | 'iterate over\n' |
| 11454 | ' all entries.\n' |
| 11455 | '\n' |
| 11456 | ' New in version 2.2.\n' |
| 11457 | '\n' |
| 11458 | ' itervalues()\n' |
| 11459 | '\n' |
| 11460 | " Return an iterator over the dictionary's values. See " |
| 11461 | 'the note\n' |
| 11462 | ' for "dict.items()".\n' |
| 11463 | '\n' |
| 11464 | ' Using "itervalues()" while adding or deleting entries ' |
| 11465 | 'in the\n' |
| 11466 | ' dictionary may raise a "RuntimeError" or fail to ' |
| 11467 | 'iterate over\n' |
| 11468 | ' all entries.\n' |
| 11469 | '\n' |
| 11470 | ' New in version 2.2.\n' |
| 11471 | '\n' |
| 11472 | ' keys()\n' |
| 11473 | '\n' |
| 11474 | " Return a copy of the dictionary's list of keys. See " |
| 11475 | 'the note\n' |
| 11476 | ' for "dict.items()".\n' |
| 11477 | '\n' |
| 11478 | ' pop(key[, default])\n' |
| 11479 | '\n' |
| 11480 | ' If *key* is in the dictionary, remove it and return ' |
| 11481 | 'its value,\n' |
| 11482 | ' else return *default*. If *default* is not given and ' |
| 11483 | '*key* is\n' |
| 11484 | ' not in the dictionary, a "KeyError" is raised.\n' |
| 11485 | '\n' |
| 11486 | ' New in version 2.3.\n' |
| 11487 | '\n' |
| 11488 | ' popitem()\n' |
| 11489 | '\n' |
| 11490 | ' Remove and return an arbitrary "(key, value)" pair ' |
| 11491 | 'from the\n' |
| 11492 | ' dictionary.\n' |
| 11493 | '\n' |
| 11494 | ' "popitem()" is useful to destructively iterate over a\n' |
| 11495 | ' dictionary, as often used in set algorithms. If the ' |
| 11496 | 'dictionary\n' |
| 11497 | ' is empty, calling "popitem()" raises a "KeyError".\n' |
| 11498 | '\n' |
| 11499 | ' setdefault(key[, default])\n' |
| 11500 | '\n' |
| 11501 | ' If *key* is in the dictionary, return its value. If ' |
| 11502 | 'not, insert\n' |
| 11503 | ' *key* with a value of *default* and return *default*. ' |
| 11504 | '*default*\n' |
| 11505 | ' defaults to "None".\n' |
| 11506 | '\n' |
| 11507 | ' update([other])\n' |
| 11508 | '\n' |
| 11509 | ' Update the dictionary with the key/value pairs from ' |
| 11510 | '*other*,\n' |
| 11511 | ' overwriting existing keys. Return "None".\n' |
| 11512 | '\n' |
| 11513 | ' "update()" accepts either another dictionary object or ' |
| 11514 | 'an\n' |
| 11515 | ' iterable of key/value pairs (as tuples or other ' |
| 11516 | 'iterables of\n' |
| 11517 | ' length two). If keyword arguments are specified, the ' |
| 11518 | 'dictionary\n' |
| 11519 | ' is then updated with those key/value pairs: ' |
| 11520 | '"d.update(red=1,\n' |
| 11521 | ' blue=2)".\n' |
| 11522 | '\n' |
| 11523 | ' Changed in version 2.4: Allowed the argument to be an ' |
| 11524 | 'iterable\n' |
| 11525 | ' of key/value pairs and allowed keyword arguments.\n' |
| 11526 | '\n' |
| 11527 | ' values()\n' |
| 11528 | '\n' |
| 11529 | " Return a copy of the dictionary's list of values. See " |
| 11530 | 'the note\n' |
| 11531 | ' for "dict.items()".\n' |
| 11532 | '\n' |
| 11533 | ' viewitems()\n' |
| 11534 | '\n' |
| 11535 | ' Return a new view of the dictionary\'s items ("(key, ' |
| 11536 | 'value)"\n' |
| 11537 | ' pairs). See below for documentation of view objects.\n' |
| 11538 | '\n' |
| 11539 | ' New in version 2.7.\n' |
| 11540 | '\n' |
| 11541 | ' viewkeys()\n' |
| 11542 | '\n' |
| 11543 | " Return a new view of the dictionary's keys. See below " |
| 11544 | 'for\n' |
| 11545 | ' documentation of view objects.\n' |
| 11546 | '\n' |
| 11547 | ' New in version 2.7.\n' |
| 11548 | '\n' |
| 11549 | ' viewvalues()\n' |
| 11550 | '\n' |
| 11551 | " Return a new view of the dictionary's values. See " |
| 11552 | 'below for\n' |
| 11553 | ' documentation of view objects.\n' |
| 11554 | '\n' |
| 11555 | ' New in version 2.7.\n' |
| 11556 | '\n' |
| 11557 | ' Dictionaries compare equal if and only if they have the ' |
| 11558 | 'same "(key,\n' |
| 11559 | ' value)" pairs.\n' |
| 11560 | '\n' |
| 11561 | '\n' |
| 11562 | 'Dictionary view objects\n' |
| 11563 | '=======================\n' |
| 11564 | '\n' |
| 11565 | 'The objects returned by "dict.viewkeys()", ' |
| 11566 | '"dict.viewvalues()" and\n' |
| 11567 | '"dict.viewitems()" are *view objects*. They provide a ' |
| 11568 | 'dynamic view on\n' |
| 11569 | "the dictionary's entries, which means that when the " |
| 11570 | 'dictionary\n' |
| 11571 | 'changes, the view reflects these changes.\n' |
| 11572 | '\n' |
| 11573 | 'Dictionary views can be iterated over to yield their ' |
| 11574 | 'respective data,\n' |
| 11575 | 'and support membership tests:\n' |
| 11576 | '\n' |
| 11577 | 'len(dictview)\n' |
| 11578 | '\n' |
| 11579 | ' Return the number of entries in the dictionary.\n' |
| 11580 | '\n' |
| 11581 | 'iter(dictview)\n' |
| 11582 | '\n' |
| 11583 | ' Return an iterator over the keys, values or items ' |
| 11584 | '(represented as\n' |
| 11585 | ' tuples of "(key, value)") in the dictionary.\n' |
| 11586 | '\n' |
| 11587 | ' Keys and values are iterated over in an arbitrary order ' |
| 11588 | 'which is\n' |
| 11589 | ' non-random, varies across Python implementations, and ' |
| 11590 | 'depends on\n' |
| 11591 | " the dictionary's history of insertions and deletions. If " |
| 11592 | 'keys,\n' |
| 11593 | ' values and items views are iterated over with no ' |
| 11594 | 'intervening\n' |
| 11595 | ' modifications to the dictionary, the order of items will ' |
| 11596 | 'directly\n' |
| 11597 | ' correspond. This allows the creation of "(value, key)" ' |
| 11598 | 'pairs using\n' |
| 11599 | ' "zip()": "pairs = zip(d.values(), d.keys())". Another ' |
| 11600 | 'way to\n' |
| 11601 | ' create the same list is "pairs = [(v, k) for (k, v) in ' |
| 11602 | 'd.items()]".\n' |
| 11603 | '\n' |
| 11604 | ' Iterating views while adding or deleting entries in the ' |
| 11605 | 'dictionary\n' |
| 11606 | ' may raise a "RuntimeError" or fail to iterate over all ' |
| 11607 | 'entries.\n' |
| 11608 | '\n' |
| 11609 | 'x in dictview\n' |
| 11610 | '\n' |
| 11611 | ' Return "True" if *x* is in the underlying dictionary\'s ' |
| 11612 | 'keys, values\n' |
| 11613 | ' or items (in the latter case, *x* should be a "(key, ' |
| 11614 | 'value)"\n' |
| 11615 | ' tuple).\n' |
| 11616 | '\n' |
| 11617 | 'Keys views are set-like since their entries are unique and ' |
| 11618 | 'hashable.\n' |
| 11619 | 'If all values are hashable, so that (key, value) pairs are ' |
| 11620 | 'unique and\n' |
| 11621 | 'hashable, then the items view is also set-like. (Values ' |
| 11622 | 'views are not\n' |
| 11623 | 'treated as set-like since the entries are generally not ' |
| 11624 | 'unique.) Then\n' |
| 11625 | 'these set operations are available ("other" refers either to ' |
| 11626 | 'another\n' |
| 11627 | 'view or a set):\n' |
| 11628 | '\n' |
| 11629 | 'dictview & other\n' |
| 11630 | '\n' |
| 11631 | ' Return the intersection of the dictview and the other ' |
| 11632 | 'object as a\n' |
| 11633 | ' new set.\n' |
| 11634 | '\n' |
| 11635 | 'dictview | other\n' |
| 11636 | '\n' |
| 11637 | ' Return the union of the dictview and the other object as ' |
| 11638 | 'a new set.\n' |
| 11639 | '\n' |
| 11640 | 'dictview - other\n' |
| 11641 | '\n' |
| 11642 | ' Return the difference between the dictview and the other ' |
| 11643 | 'object\n' |
| 11644 | " (all elements in *dictview* that aren't in *other*) as a " |
| 11645 | 'new set.\n' |
| 11646 | '\n' |
| 11647 | 'dictview ^ other\n' |
| 11648 | '\n' |
| 11649 | ' Return the symmetric difference (all elements either in ' |
| 11650 | '*dictview*\n' |
| 11651 | ' or *other*, but not in both) of the dictview and the ' |
| 11652 | 'other object\n' |
| 11653 | ' as a new set.\n' |
| 11654 | '\n' |
| 11655 | 'An example of dictionary view usage:\n' |
| 11656 | '\n' |
| 11657 | " >>> dishes = {'eggs': 2, 'sausage': 1, 'bacon': 1, " |
| 11658 | "'spam': 500}\n" |
| 11659 | ' >>> keys = dishes.viewkeys()\n' |
| 11660 | ' >>> values = dishes.viewvalues()\n' |
| 11661 | '\n' |
| 11662 | ' >>> # iteration\n' |
| 11663 | ' >>> n = 0\n' |
| 11664 | ' >>> for val in values:\n' |
| 11665 | ' ... n += val\n' |
| 11666 | ' >>> print(n)\n' |
| 11667 | ' 504\n' |
| 11668 | '\n' |
| 11669 | ' >>> # keys and values are iterated over in the same ' |
| 11670 | 'order\n' |
| 11671 | ' >>> list(keys)\n' |
| 11672 | " ['eggs', 'bacon', 'sausage', 'spam']\n" |
| 11673 | ' >>> list(values)\n' |
| 11674 | ' [2, 1, 1, 500]\n' |
| 11675 | '\n' |
| 11676 | ' >>> # view objects are dynamic and reflect dict changes\n' |
| 11677 | " >>> del dishes['eggs']\n" |
| 11678 | " >>> del dishes['sausage']\n" |
| 11679 | ' >>> list(keys)\n' |
| 11680 | " ['spam', 'bacon']\n" |
| 11681 | '\n' |
| 11682 | ' >>> # set operations\n' |
| 11683 | " >>> keys & {'eggs', 'bacon', 'salad'}\n" |
| 11684 | " {'bacon'}\n", |
| 11685 | 'typesmethods': '\n' |
| 11686 | 'Methods\n' |
| 11687 | '*******\n' |
| 11688 | '\n' |
| 11689 | 'Methods are functions that are called using the attribute ' |
| 11690 | 'notation.\n' |
| 11691 | 'There are two flavors: built-in methods (such as "append()" ' |
| 11692 | 'on lists)\n' |
| 11693 | 'and class instance methods. Built-in methods are described ' |
| 11694 | 'with the\n' |
| 11695 | 'types that support them.\n' |
| 11696 | '\n' |
| 11697 | 'The implementation adds two special read-only attributes to ' |
| 11698 | 'class\n' |
| 11699 | 'instance methods: "m.im_self" is the object on which the ' |
| 11700 | 'method\n' |
| 11701 | 'operates, and "m.im_func" is the function implementing the ' |
| 11702 | 'method.\n' |
| 11703 | 'Calling "m(arg-1, arg-2, ..., arg-n)" is completely ' |
| 11704 | 'equivalent to\n' |
| 11705 | 'calling "m.im_func(m.im_self, arg-1, arg-2, ..., arg-n)".\n' |
| 11706 | '\n' |
| 11707 | 'Class instance methods are either *bound* or *unbound*, ' |
| 11708 | 'referring to\n' |
| 11709 | 'whether the method was accessed through an instance or a ' |
| 11710 | 'class,\n' |
| 11711 | 'respectively. When a method is unbound, its "im_self" ' |
| 11712 | 'attribute will\n' |
| 11713 | 'be "None" and if called, an explicit "self" object must be ' |
| 11714 | 'passed as\n' |
| 11715 | 'the first argument. In this case, "self" must be an ' |
| 11716 | 'instance of the\n' |
| 11717 | "unbound method's class (or a subclass of that class), " |
| 11718 | 'otherwise a\n' |
| 11719 | '"TypeError" is raised.\n' |
| 11720 | '\n' |
| 11721 | 'Like function objects, methods objects support getting ' |
| 11722 | 'arbitrary\n' |
| 11723 | 'attributes. However, since method attributes are actually ' |
| 11724 | 'stored on\n' |
| 11725 | 'the underlying function object ("meth.im_func"), setting ' |
| 11726 | 'method\n' |
| 11727 | 'attributes on either bound or unbound methods is ' |
| 11728 | 'disallowed.\n' |
| 11729 | 'Attempting to set an attribute on a method results in an\n' |
| 11730 | '"AttributeError" being raised. In order to set a method ' |
| 11731 | 'attribute,\n' |
| 11732 | 'you need to explicitly set it on the underlying function ' |
| 11733 | 'object:\n' |
| 11734 | '\n' |
| 11735 | ' >>> class C:\n' |
| 11736 | ' ... def method(self):\n' |
| 11737 | ' ... pass\n' |
| 11738 | ' ...\n' |
| 11739 | ' >>> c = C()\n' |
| 11740 | " >>> c.method.whoami = 'my name is method' # can't set on " |
| 11741 | 'the method\n' |
| 11742 | ' Traceback (most recent call last):\n' |
| 11743 | ' File "<stdin>", line 1, in <module>\n' |
| 11744 | " AttributeError: 'instancemethod' object has no attribute " |
| 11745 | "'whoami'\n" |
| 11746 | " >>> c.method.im_func.whoami = 'my name is method'\n" |
| 11747 | ' >>> c.method.whoami\n' |
| 11748 | " 'my name is method'\n" |
| 11749 | '\n' |
| 11750 | 'See The standard type hierarchy for more information.\n', |
| 11751 | 'typesmodules': '\n' |
| 11752 | 'Modules\n' |
| 11753 | '*******\n' |
| 11754 | '\n' |
| 11755 | 'The only special operation on a module is attribute access: ' |
| 11756 | '"m.name",\n' |
| 11757 | 'where *m* is a module and *name* accesses a name defined in ' |
| 11758 | "*m*'s\n" |
| 11759 | 'symbol table. Module attributes can be assigned to. (Note ' |
| 11760 | 'that the\n' |
| 11761 | '"import" statement is not, strictly speaking, an operation ' |
| 11762 | 'on a module\n' |
| 11763 | 'object; "import foo" does not require a module object named ' |
| 11764 | '*foo* to\n' |
| 11765 | 'exist, rather it requires an (external) *definition* for a ' |
| 11766 | 'module\n' |
| 11767 | 'named *foo* somewhere.)\n' |
| 11768 | '\n' |
| 11769 | 'A special attribute of every module is "__dict__". This is ' |
| 11770 | 'the\n' |
| 11771 | "dictionary containing the module's symbol table. Modifying " |
| 11772 | 'this\n' |
| 11773 | "dictionary will actually change the module's symbol table, " |
| 11774 | 'but direct\n' |
| 11775 | 'assignment to the "__dict__" attribute is not possible (you ' |
| 11776 | 'can write\n' |
| 11777 | '"m.__dict__[\'a\'] = 1", which defines "m.a" to be "1", but ' |
| 11778 | "you can't\n" |
| 11779 | 'write "m.__dict__ = {}"). Modifying "__dict__" directly is ' |
| 11780 | 'not\n' |
| 11781 | 'recommended.\n' |
| 11782 | '\n' |
| 11783 | 'Modules built into the interpreter are written like this: ' |
| 11784 | '"<module\n' |
| 11785 | '\'sys\' (built-in)>". If loaded from a file, they are ' |
| 11786 | 'written as\n' |
| 11787 | '"<module \'os\' from ' |
| 11788 | '\'/usr/local/lib/pythonX.Y/os.pyc\'>".\n', |
| 11789 | 'typesseq': '\n' |
| 11790 | 'Sequence Types --- "str", "unicode", "list", "tuple", ' |
| 11791 | '"bytearray", "buffer", "xrange"\n' |
| 11792 | '*************************************************************************************\n' |
| 11793 | '\n' |
| 11794 | 'There are seven sequence types: strings, Unicode strings, ' |
| 11795 | 'lists,\n' |
| 11796 | 'tuples, bytearrays, buffers, and xrange objects.\n' |
| 11797 | '\n' |
| 11798 | 'For other containers see the built in "dict" and "set" classes, ' |
| 11799 | 'and\n' |
| 11800 | 'the "collections" module.\n' |
| 11801 | '\n' |
| 11802 | 'String literals are written in single or double quotes: ' |
| 11803 | '"\'xyzzy\'",\n' |
| 11804 | '""frobozz"". See String literals for more about string ' |
| 11805 | 'literals.\n' |
| 11806 | 'Unicode strings are much like strings, but are specified in the ' |
| 11807 | 'syntax\n' |
| 11808 | 'using a preceding "\'u\'" character: "u\'abc\'", "u"def"". In ' |
| 11809 | 'addition to\n' |
| 11810 | 'the functionality described here, there are also ' |
| 11811 | 'string-specific\n' |
| 11812 | 'methods described in the String Methods section. Lists are ' |
| 11813 | 'constructed\n' |
| 11814 | 'with square brackets, separating items with commas: "[a, b, ' |
| 11815 | 'c]".\n' |
| 11816 | 'Tuples are constructed by the comma operator (not within square\n' |
| 11817 | 'brackets), with or without enclosing parentheses, but an empty ' |
| 11818 | 'tuple\n' |
| 11819 | 'must have the enclosing parentheses, such as "a, b, c" or "()". ' |
| 11820 | 'A\n' |
| 11821 | 'single item tuple must have a trailing comma, such as "(d,)".\n' |
| 11822 | '\n' |
| 11823 | 'Bytearray objects are created with the built-in function\n' |
| 11824 | '"bytearray()".\n' |
| 11825 | '\n' |
| 11826 | 'Buffer objects are not directly supported by Python syntax, but ' |
| 11827 | 'can be\n' |
| 11828 | 'created by calling the built-in function "buffer()". They ' |
| 11829 | "don't\n" |
| 11830 | 'support concatenation or repetition.\n' |
| 11831 | '\n' |
| 11832 | 'Objects of type xrange are similar to buffers in that there is ' |
| 11833 | 'no\n' |
| 11834 | 'specific syntax to create them, but they are created using the\n' |
| 11835 | '"xrange()" function. They don\'t support slicing, concatenation ' |
| 11836 | 'or\n' |
| 11837 | 'repetition, and using "in", "not in", "min()" or "max()" on them ' |
| 11838 | 'is\n' |
| 11839 | 'inefficient.\n' |
| 11840 | '\n' |
| 11841 | 'Most sequence types support the following operations. The "in" ' |
| 11842 | 'and\n' |
| 11843 | '"not in" operations have the same priorities as the comparison\n' |
| 11844 | 'operations. The "+" and "*" operations have the same priority ' |
| 11845 | 'as the\n' |
| 11846 | 'corresponding numeric operations. [3] Additional methods are ' |
| 11847 | 'provided\n' |
| 11848 | 'for Mutable Sequence Types.\n' |
| 11849 | '\n' |
| 11850 | 'This table lists the sequence operations sorted in ascending ' |
| 11851 | 'priority.\n' |
| 11852 | 'In the table, *s* and *t* are sequences of the same type; *n*, ' |
| 11853 | '*i* and\n' |
| 11854 | '*j* are integers:\n' |
| 11855 | '\n' |
| 11856 | '+--------------------+----------------------------------+------------+\n' |
| 11857 | '| Operation | Result | ' |
| 11858 | 'Notes |\n' |
| 11859 | '+====================+==================================+============+\n' |
| 11860 | '| "x in s" | "True" if an item of *s* is | ' |
| 11861 | '(1) |\n' |
| 11862 | '| | equal to *x*, else "False" ' |
| 11863 | '| |\n' |
| 11864 | '+--------------------+----------------------------------+------------+\n' |
| 11865 | '| "x not in s" | "False" if an item of *s* is | ' |
| 11866 | '(1) |\n' |
| 11867 | '| | equal to *x*, else "True" ' |
| 11868 | '| |\n' |
| 11869 | '+--------------------+----------------------------------+------------+\n' |
| 11870 | '| "s + t" | the concatenation of *s* and *t* | ' |
| 11871 | '(6) |\n' |
| 11872 | '+--------------------+----------------------------------+------------+\n' |
| 11873 | '| "s * n, n * s" | equivalent to adding *s* to | ' |
| 11874 | '(2) |\n' |
| 11875 | '| | itself *n* times ' |
| 11876 | '| |\n' |
| 11877 | '+--------------------+----------------------------------+------------+\n' |
| 11878 | '| "s[i]" | *i*th item of *s*, origin 0 | ' |
| 11879 | '(3) |\n' |
| 11880 | '+--------------------+----------------------------------+------------+\n' |
| 11881 | '| "s[i:j]" | slice of *s* from *i* to *j* | ' |
| 11882 | '(3)(4) |\n' |
| 11883 | '+--------------------+----------------------------------+------------+\n' |
| 11884 | '| "s[i:j:k]" | slice of *s* from *i* to *j* | ' |
| 11885 | '(3)(5) |\n' |
| 11886 | '| | with step *k* ' |
| 11887 | '| |\n' |
| 11888 | '+--------------------+----------------------------------+------------+\n' |
| 11889 | '| "len(s)" | length of *s* ' |
| 11890 | '| |\n' |
| 11891 | '+--------------------+----------------------------------+------------+\n' |
| 11892 | '| "min(s)" | smallest item of *s* ' |
| 11893 | '| |\n' |
| 11894 | '+--------------------+----------------------------------+------------+\n' |
| 11895 | '| "max(s)" | largest item of *s* ' |
| 11896 | '| |\n' |
| 11897 | '+--------------------+----------------------------------+------------+\n' |
| 11898 | '| "s.index(x)" | index of the first occurrence of ' |
| 11899 | '| |\n' |
| 11900 | '| | *x* in *s* ' |
| 11901 | '| |\n' |
| 11902 | '+--------------------+----------------------------------+------------+\n' |
| 11903 | '| "s.count(x)" | total number of occurrences of ' |
| 11904 | '| |\n' |
| 11905 | '| | *x* in *s* ' |
| 11906 | '| |\n' |
| 11907 | '+--------------------+----------------------------------+------------+\n' |
| 11908 | '\n' |
| 11909 | 'Sequence types also support comparisons. In particular, tuples ' |
| 11910 | 'and\n' |
| 11911 | 'lists are compared lexicographically by comparing corresponding\n' |
| 11912 | 'elements. This means that to compare equal, every element must ' |
| 11913 | 'compare\n' |
| 11914 | 'equal and the two sequences must be of the same type and have ' |
| 11915 | 'the same\n' |
| 11916 | 'length. (For full details see Comparisons in the language ' |
| 11917 | 'reference.)\n' |
| 11918 | '\n' |
| 11919 | 'Notes:\n' |
| 11920 | '\n' |
| 11921 | '1. When *s* is a string or Unicode string object the "in" and ' |
| 11922 | '"not\n' |
| 11923 | ' in" operations act like a substring test. In Python ' |
| 11924 | 'versions\n' |
| 11925 | ' before 2.3, *x* had to be a string of length 1. In Python 2.3 ' |
| 11926 | 'and\n' |
| 11927 | ' beyond, *x* may be a string of any length.\n' |
| 11928 | '\n' |
| 11929 | '2. Values of *n* less than "0" are treated as "0" (which yields ' |
| 11930 | 'an\n' |
| 11931 | ' empty sequence of the same type as *s*). Note that items in ' |
| 11932 | 'the\n' |
| 11933 | ' sequence *s* are not copied; they are referenced multiple ' |
| 11934 | 'times.\n' |
| 11935 | ' This often haunts new Python programmers; consider:\n' |
| 11936 | '\n' |
| 11937 | ' >>> lists = [[]] * 3\n' |
| 11938 | ' >>> lists\n' |
| 11939 | ' [[], [], []]\n' |
| 11940 | ' >>> lists[0].append(3)\n' |
| 11941 | ' >>> lists\n' |
| 11942 | ' [[3], [3], [3]]\n' |
| 11943 | '\n' |
| 11944 | ' What has happened is that "[[]]" is a one-element list ' |
| 11945 | 'containing\n' |
| 11946 | ' an empty list, so all three elements of "[[]] * 3" are ' |
| 11947 | 'references\n' |
| 11948 | ' to this single empty list. Modifying any of the elements of\n' |
| 11949 | ' "lists" modifies this single list. You can create a list of\n' |
| 11950 | ' different lists this way:\n' |
| 11951 | '\n' |
| 11952 | ' >>> lists = [[] for i in range(3)]\n' |
| 11953 | ' >>> lists[0].append(3)\n' |
| 11954 | ' >>> lists[1].append(5)\n' |
| 11955 | ' >>> lists[2].append(7)\n' |
| 11956 | ' >>> lists\n' |
| 11957 | ' [[3], [5], [7]]\n' |
| 11958 | '\n' |
| 11959 | ' Further explanation is available in the FAQ entry How do I ' |
| 11960 | 'create a\n' |
| 11961 | ' multidimensional list?.\n' |
| 11962 | '\n' |
| 11963 | '3. If *i* or *j* is negative, the index is relative to the end ' |
| 11964 | 'of\n' |
| 11965 | ' sequence *s*: "len(s) + i" or "len(s) + j" is substituted. ' |
| 11966 | 'But\n' |
| 11967 | ' note that "-0" is still "0".\n' |
| 11968 | '\n' |
| 11969 | '4. The slice of *s* from *i* to *j* is defined as the sequence ' |
| 11970 | 'of\n' |
| 11971 | ' items with index *k* such that "i <= k < j". If *i* or *j* ' |
| 11972 | 'is\n' |
| 11973 | ' greater than "len(s)", use "len(s)". If *i* is omitted or ' |
| 11974 | '"None",\n' |
| 11975 | ' use "0". If *j* is omitted or "None", use "len(s)". If *i* ' |
| 11976 | 'is\n' |
| 11977 | ' greater than or equal to *j*, the slice is empty.\n' |
| 11978 | '\n' |
| 11979 | '5. The slice of *s* from *i* to *j* with step *k* is defined as ' |
| 11980 | 'the\n' |
| 11981 | ' sequence of items with index "x = i + n*k" such that "0 <= n ' |
| 11982 | '<\n' |
| 11983 | ' (j-i)/k". In other words, the indices are "i", "i+k", ' |
| 11984 | '"i+2*k",\n' |
| 11985 | ' "i+3*k" and so on, stopping when *j* is reached (but never\n' |
| 11986 | ' including *j*). When *k* is positive, *i* and *j* are ' |
| 11987 | 'reduced to\n' |
| 11988 | ' "len(s)" if they are greater. When *k* is negative, *i* and ' |
| 11989 | '*j* are\n' |
| 11990 | ' reduced to "len(s) - 1" if they are greater. If *i* or *j* ' |
| 11991 | 'are\n' |
| 11992 | ' omitted or "None", they become "end" values (which end ' |
| 11993 | 'depends on\n' |
| 11994 | ' the sign of *k*). Note, *k* cannot be zero. If *k* is ' |
| 11995 | '"None", it\n' |
| 11996 | ' is treated like "1".\n' |
| 11997 | '\n' |
| 11998 | '6. **CPython implementation detail:** If *s* and *t* are both\n' |
| 11999 | ' strings, some Python implementations such as CPython can ' |
| 12000 | 'usually\n' |
| 12001 | ' perform an in-place optimization for assignments of the form ' |
| 12002 | '"s = s\n' |
| 12003 | ' + t" or "s += t". When applicable, this optimization makes\n' |
| 12004 | ' quadratic run-time much less likely. This optimization is ' |
| 12005 | 'both\n' |
| 12006 | ' version and implementation dependent. For performance ' |
| 12007 | 'sensitive\n' |
| 12008 | ' code, it is preferable to use the "str.join()" method which ' |
| 12009 | 'assures\n' |
| 12010 | ' consistent linear concatenation performance across versions ' |
| 12011 | 'and\n' |
| 12012 | ' implementations.\n' |
| 12013 | '\n' |
| 12014 | ' Changed in version 2.4: Formerly, string concatenation never\n' |
| 12015 | ' occurred in-place.\n' |
| 12016 | '\n' |
| 12017 | '\n' |
| 12018 | 'String Methods\n' |
| 12019 | '==============\n' |
| 12020 | '\n' |
| 12021 | 'Below are listed the string methods which both 8-bit strings ' |
| 12022 | 'and\n' |
| 12023 | 'Unicode objects support. Some of them are also available on\n' |
| 12024 | '"bytearray" objects.\n' |
| 12025 | '\n' |
| 12026 | "In addition, Python's strings support the sequence type methods\n" |
| 12027 | 'described in the Sequence Types --- str, unicode, list, tuple,\n' |
| 12028 | 'bytearray, buffer, xrange section. To output formatted strings ' |
| 12029 | 'use\n' |
| 12030 | 'template strings or the "%" operator described in the String\n' |
| 12031 | 'Formatting Operations section. Also, see the "re" module for ' |
| 12032 | 'string\n' |
| 12033 | 'functions based on regular expressions.\n' |
| 12034 | '\n' |
| 12035 | 'str.capitalize()\n' |
| 12036 | '\n' |
| 12037 | ' Return a copy of the string with its first character ' |
| 12038 | 'capitalized\n' |
| 12039 | ' and the rest lowercased.\n' |
| 12040 | '\n' |
| 12041 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12042 | '\n' |
| 12043 | 'str.center(width[, fillchar])\n' |
| 12044 | '\n' |
| 12045 | ' Return centered in a string of length *width*. Padding is ' |
| 12046 | 'done\n' |
| 12047 | ' using the specified *fillchar* (default is a space).\n' |
| 12048 | '\n' |
| 12049 | ' Changed in version 2.4: Support for the *fillchar* argument.\n' |
| 12050 | '\n' |
| 12051 | 'str.count(sub[, start[, end]])\n' |
| 12052 | '\n' |
| 12053 | ' Return the number of non-overlapping occurrences of substring ' |
| 12054 | '*sub*\n' |
| 12055 | ' in the range [*start*, *end*]. Optional arguments *start* ' |
| 12056 | 'and\n' |
| 12057 | ' *end* are interpreted as in slice notation.\n' |
| 12058 | '\n' |
| 12059 | 'str.decode([encoding[, errors]])\n' |
| 12060 | '\n' |
| 12061 | ' Decodes the string using the codec registered for ' |
| 12062 | '*encoding*.\n' |
| 12063 | ' *encoding* defaults to the default string encoding. *errors* ' |
| 12064 | 'may\n' |
| 12065 | ' be given to set a different error handling scheme. The ' |
| 12066 | 'default is\n' |
| 12067 | ' "\'strict\'", meaning that encoding errors raise ' |
| 12068 | '"UnicodeError".\n' |
| 12069 | ' Other possible values are "\'ignore\'", "\'replace\'" and any ' |
| 12070 | 'other\n' |
| 12071 | ' name registered via "codecs.register_error()", see section ' |
| 12072 | 'Codec\n' |
| 12073 | ' Base Classes.\n' |
| 12074 | '\n' |
| 12075 | ' New in version 2.2.\n' |
| 12076 | '\n' |
| 12077 | ' Changed in version 2.3: Support for other error handling ' |
| 12078 | 'schemes\n' |
| 12079 | ' added.\n' |
| 12080 | '\n' |
| 12081 | ' Changed in version 2.7: Support for keyword arguments added.\n' |
| 12082 | '\n' |
| 12083 | 'str.encode([encoding[, errors]])\n' |
| 12084 | '\n' |
| 12085 | ' Return an encoded version of the string. Default encoding is ' |
| 12086 | 'the\n' |
| 12087 | ' current default string encoding. *errors* may be given to ' |
| 12088 | 'set a\n' |
| 12089 | ' different error handling scheme. The default for *errors* ' |
| 12090 | 'is\n' |
| 12091 | ' "\'strict\'", meaning that encoding errors raise a ' |
| 12092 | '"UnicodeError".\n' |
| 12093 | ' Other possible values are "\'ignore\'", "\'replace\'",\n' |
| 12094 | ' "\'xmlcharrefreplace\'", "\'backslashreplace\'" and any other ' |
| 12095 | 'name\n' |
| 12096 | ' registered via "codecs.register_error()", see section Codec ' |
| 12097 | 'Base\n' |
| 12098 | ' Classes. For a list of possible encodings, see section ' |
| 12099 | 'Standard\n' |
| 12100 | ' Encodings.\n' |
| 12101 | '\n' |
| 12102 | ' New in version 2.0.\n' |
| 12103 | '\n' |
| 12104 | ' Changed in version 2.3: Support for "\'xmlcharrefreplace\'" ' |
| 12105 | 'and\n' |
| 12106 | ' "\'backslashreplace\'" and other error handling schemes ' |
| 12107 | 'added.\n' |
| 12108 | '\n' |
| 12109 | ' Changed in version 2.7: Support for keyword arguments added.\n' |
| 12110 | '\n' |
| 12111 | 'str.endswith(suffix[, start[, end]])\n' |
| 12112 | '\n' |
| 12113 | ' Return "True" if the string ends with the specified ' |
| 12114 | '*suffix*,\n' |
| 12115 | ' otherwise return "False". *suffix* can also be a tuple of ' |
| 12116 | 'suffixes\n' |
| 12117 | ' to look for. With optional *start*, test beginning at that\n' |
| 12118 | ' position. With optional *end*, stop comparing at that ' |
| 12119 | 'position.\n' |
| 12120 | '\n' |
| 12121 | ' Changed in version 2.5: Accept tuples as *suffix*.\n' |
| 12122 | '\n' |
| 12123 | 'str.expandtabs([tabsize])\n' |
| 12124 | '\n' |
| 12125 | ' Return a copy of the string where all tab characters are ' |
| 12126 | 'replaced\n' |
| 12127 | ' by one or more spaces, depending on the current column and ' |
| 12128 | 'the\n' |
| 12129 | ' given tab size. Tab positions occur every *tabsize* ' |
| 12130 | 'characters\n' |
| 12131 | ' (default is 8, giving tab positions at columns 0, 8, 16 and ' |
| 12132 | 'so on).\n' |
| 12133 | ' To expand the string, the current column is set to zero and ' |
| 12134 | 'the\n' |
| 12135 | ' string is examined character by character. If the character ' |
| 12136 | 'is a\n' |
| 12137 | ' tab ("\\t"), one or more space characters are inserted in the ' |
| 12138 | 'result\n' |
| 12139 | ' until the current column is equal to the next tab position. ' |
| 12140 | '(The\n' |
| 12141 | ' tab character itself is not copied.) If the character is a ' |
| 12142 | 'newline\n' |
| 12143 | ' ("\\n") or return ("\\r"), it is copied and the current ' |
| 12144 | 'column is\n' |
| 12145 | ' reset to zero. Any other character is copied unchanged and ' |
| 12146 | 'the\n' |
| 12147 | ' current column is incremented by one regardless of how the\n' |
| 12148 | ' character is represented when printed.\n' |
| 12149 | '\n' |
| 12150 | " >>> '01\\t012\\t0123\\t01234'.expandtabs()\n" |
| 12151 | " '01 012 0123 01234'\n" |
| 12152 | " >>> '01\\t012\\t0123\\t01234'.expandtabs(4)\n" |
| 12153 | " '01 012 0123 01234'\n" |
| 12154 | '\n' |
| 12155 | 'str.find(sub[, start[, end]])\n' |
| 12156 | '\n' |
| 12157 | ' Return the lowest index in the string where substring *sub* ' |
| 12158 | 'is\n' |
| 12159 | ' found within the slice "s[start:end]". Optional arguments ' |
| 12160 | '*start*\n' |
| 12161 | ' and *end* are interpreted as in slice notation. Return "-1" ' |
| 12162 | 'if\n' |
| 12163 | ' *sub* is not found.\n' |
| 12164 | '\n' |
| 12165 | ' Note: The "find()" method should be used only if you need to ' |
| 12166 | 'know\n' |
| 12167 | ' the position of *sub*. To check if *sub* is a substring or ' |
| 12168 | 'not,\n' |
| 12169 | ' use the "in" operator:\n' |
| 12170 | '\n' |
| 12171 | " >>> 'Py' in 'Python'\n" |
| 12172 | ' True\n' |
| 12173 | '\n' |
| 12174 | 'str.format(*args, **kwargs)\n' |
| 12175 | '\n' |
| 12176 | ' Perform a string formatting operation. The string on which ' |
| 12177 | 'this\n' |
| 12178 | ' method is called can contain literal text or replacement ' |
| 12179 | 'fields\n' |
| 12180 | ' delimited by braces "{}". Each replacement field contains ' |
| 12181 | 'either\n' |
| 12182 | ' the numeric index of a positional argument, or the name of a\n' |
| 12183 | ' keyword argument. Returns a copy of the string where each\n' |
| 12184 | ' replacement field is replaced with the string value of the\n' |
| 12185 | ' corresponding argument.\n' |
| 12186 | '\n' |
| 12187 | ' >>> "The sum of 1 + 2 is {0}".format(1+2)\n' |
| 12188 | " 'The sum of 1 + 2 is 3'\n" |
| 12189 | '\n' |
| 12190 | ' See Format String Syntax for a description of the various\n' |
| 12191 | ' formatting options that can be specified in format strings.\n' |
| 12192 | '\n' |
| 12193 | ' This method of string formatting is the new standard in ' |
| 12194 | 'Python 3,\n' |
| 12195 | ' and should be preferred to the "%" formatting described in ' |
| 12196 | 'String\n' |
| 12197 | ' Formatting Operations in new code.\n' |
| 12198 | '\n' |
| 12199 | ' New in version 2.6.\n' |
| 12200 | '\n' |
| 12201 | 'str.index(sub[, start[, end]])\n' |
| 12202 | '\n' |
| 12203 | ' Like "find()", but raise "ValueError" when the substring is ' |
| 12204 | 'not\n' |
| 12205 | ' found.\n' |
| 12206 | '\n' |
| 12207 | 'str.isalnum()\n' |
| 12208 | '\n' |
| 12209 | ' Return true if all characters in the string are alphanumeric ' |
| 12210 | 'and\n' |
| 12211 | ' there is at least one character, false otherwise.\n' |
| 12212 | '\n' |
| 12213 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12214 | '\n' |
| 12215 | 'str.isalpha()\n' |
| 12216 | '\n' |
| 12217 | ' Return true if all characters in the string are alphabetic ' |
| 12218 | 'and\n' |
| 12219 | ' there is at least one character, false otherwise.\n' |
| 12220 | '\n' |
| 12221 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12222 | '\n' |
| 12223 | 'str.isdigit()\n' |
| 12224 | '\n' |
| 12225 | ' Return true if all characters in the string are digits and ' |
| 12226 | 'there is\n' |
| 12227 | ' at least one character, false otherwise.\n' |
| 12228 | '\n' |
| 12229 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12230 | '\n' |
| 12231 | 'str.islower()\n' |
| 12232 | '\n' |
| 12233 | ' Return true if all cased characters [4] in the string are ' |
| 12234 | 'lowercase\n' |
| 12235 | ' and there is at least one cased character, false otherwise.\n' |
| 12236 | '\n' |
| 12237 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12238 | '\n' |
| 12239 | 'str.isspace()\n' |
| 12240 | '\n' |
| 12241 | ' Return true if there are only whitespace characters in the ' |
| 12242 | 'string\n' |
| 12243 | ' and there is at least one character, false otherwise.\n' |
| 12244 | '\n' |
| 12245 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12246 | '\n' |
| 12247 | 'str.istitle()\n' |
| 12248 | '\n' |
| 12249 | ' Return true if the string is a titlecased string and there is ' |
| 12250 | 'at\n' |
| 12251 | ' least one character, for example uppercase characters may ' |
| 12252 | 'only\n' |
| 12253 | ' follow uncased characters and lowercase characters only cased ' |
| 12254 | 'ones.\n' |
| 12255 | ' Return false otherwise.\n' |
| 12256 | '\n' |
| 12257 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12258 | '\n' |
| 12259 | 'str.isupper()\n' |
| 12260 | '\n' |
| 12261 | ' Return true if all cased characters [4] in the string are ' |
| 12262 | 'uppercase\n' |
| 12263 | ' and there is at least one cased character, false otherwise.\n' |
| 12264 | '\n' |
| 12265 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12266 | '\n' |
| 12267 | 'str.join(iterable)\n' |
| 12268 | '\n' |
| 12269 | ' Return a string which is the concatenation of the strings in\n' |
| 12270 | ' *iterable*. A "TypeError" will be raised if there are any ' |
| 12271 | 'non-\n' |
| 12272 | ' string values in *iterable*, including "bytes" objects. The\n' |
| 12273 | ' separator between elements is the string providing this ' |
| 12274 | 'method.\n' |
| 12275 | '\n' |
| 12276 | 'str.ljust(width[, fillchar])\n' |
| 12277 | '\n' |
| 12278 | ' Return the string left justified in a string of length ' |
| 12279 | '*width*.\n' |
| 12280 | ' Padding is done using the specified *fillchar* (default is a\n' |
| 12281 | ' space). The original string is returned if *width* is less ' |
| 12282 | 'than or\n' |
| 12283 | ' equal to "len(s)".\n' |
| 12284 | '\n' |
| 12285 | ' Changed in version 2.4: Support for the *fillchar* argument.\n' |
| 12286 | '\n' |
| 12287 | 'str.lower()\n' |
| 12288 | '\n' |
| 12289 | ' Return a copy of the string with all the cased characters ' |
| 12290 | '[4]\n' |
| 12291 | ' converted to lowercase.\n' |
| 12292 | '\n' |
| 12293 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12294 | '\n' |
| 12295 | 'str.lstrip([chars])\n' |
| 12296 | '\n' |
| 12297 | ' Return a copy of the string with leading characters removed. ' |
| 12298 | 'The\n' |
| 12299 | ' *chars* argument is a string specifying the set of characters ' |
| 12300 | 'to be\n' |
| 12301 | ' removed. If omitted or "None", the *chars* argument defaults ' |
| 12302 | 'to\n' |
| 12303 | ' removing whitespace. The *chars* argument is not a prefix; ' |
| 12304 | 'rather,\n' |
| 12305 | ' all combinations of its values are stripped:\n' |
| 12306 | '\n' |
| 12307 | " >>> ' spacious '.lstrip()\n" |
| 12308 | " 'spacious '\n" |
| 12309 | " >>> 'www.example.com'.lstrip('cmowz.')\n" |
| 12310 | " 'example.com'\n" |
| 12311 | '\n' |
| 12312 | ' Changed in version 2.2.2: Support for the *chars* argument.\n' |
| 12313 | '\n' |
| 12314 | 'str.partition(sep)\n' |
| 12315 | '\n' |
| 12316 | ' Split the string at the first occurrence of *sep*, and return ' |
| 12317 | 'a\n' |
| 12318 | ' 3-tuple containing the part before the separator, the ' |
| 12319 | 'separator\n' |
| 12320 | ' itself, and the part after the separator. If the separator ' |
| 12321 | 'is not\n' |
| 12322 | ' found, return a 3-tuple containing the string itself, ' |
| 12323 | 'followed by\n' |
| 12324 | ' two empty strings.\n' |
| 12325 | '\n' |
| 12326 | ' New in version 2.5.\n' |
| 12327 | '\n' |
| 12328 | 'str.replace(old, new[, count])\n' |
| 12329 | '\n' |
| 12330 | ' Return a copy of the string with all occurrences of substring ' |
| 12331 | '*old*\n' |
| 12332 | ' replaced by *new*. If the optional argument *count* is ' |
| 12333 | 'given, only\n' |
| 12334 | ' the first *count* occurrences are replaced.\n' |
| 12335 | '\n' |
| 12336 | 'str.rfind(sub[, start[, end]])\n' |
| 12337 | '\n' |
| 12338 | ' Return the highest index in the string where substring *sub* ' |
| 12339 | 'is\n' |
| 12340 | ' found, such that *sub* is contained within "s[start:end]".\n' |
| 12341 | ' Optional arguments *start* and *end* are interpreted as in ' |
| 12342 | 'slice\n' |
| 12343 | ' notation. Return "-1" on failure.\n' |
| 12344 | '\n' |
| 12345 | 'str.rindex(sub[, start[, end]])\n' |
| 12346 | '\n' |
| 12347 | ' Like "rfind()" but raises "ValueError" when the substring ' |
| 12348 | '*sub* is\n' |
| 12349 | ' not found.\n' |
| 12350 | '\n' |
| 12351 | 'str.rjust(width[, fillchar])\n' |
| 12352 | '\n' |
| 12353 | ' Return the string right justified in a string of length ' |
| 12354 | '*width*.\n' |
| 12355 | ' Padding is done using the specified *fillchar* (default is a\n' |
| 12356 | ' space). The original string is returned if *width* is less ' |
| 12357 | 'than or\n' |
| 12358 | ' equal to "len(s)".\n' |
| 12359 | '\n' |
| 12360 | ' Changed in version 2.4: Support for the *fillchar* argument.\n' |
| 12361 | '\n' |
| 12362 | 'str.rpartition(sep)\n' |
| 12363 | '\n' |
| 12364 | ' Split the string at the last occurrence of *sep*, and return ' |
| 12365 | 'a\n' |
| 12366 | ' 3-tuple containing the part before the separator, the ' |
| 12367 | 'separator\n' |
| 12368 | ' itself, and the part after the separator. If the separator ' |
| 12369 | 'is not\n' |
| 12370 | ' found, return a 3-tuple containing two empty strings, ' |
| 12371 | 'followed by\n' |
| 12372 | ' the string itself.\n' |
| 12373 | '\n' |
| 12374 | ' New in version 2.5.\n' |
| 12375 | '\n' |
| 12376 | 'str.rsplit([sep[, maxsplit]])\n' |
| 12377 | '\n' |
| 12378 | ' Return a list of the words in the string, using *sep* as the\n' |
| 12379 | ' delimiter string. If *maxsplit* is given, at most *maxsplit* ' |
| 12380 | 'splits\n' |
| 12381 | ' are done, the *rightmost* ones. If *sep* is not specified ' |
| 12382 | 'or\n' |
| 12383 | ' "None", any whitespace string is a separator. Except for ' |
| 12384 | 'splitting\n' |
| 12385 | ' from the right, "rsplit()" behaves like "split()" which is\n' |
| 12386 | ' described in detail below.\n' |
| 12387 | '\n' |
| 12388 | ' New in version 2.4.\n' |
| 12389 | '\n' |
| 12390 | 'str.rstrip([chars])\n' |
| 12391 | '\n' |
| 12392 | ' Return a copy of the string with trailing characters ' |
| 12393 | 'removed. The\n' |
| 12394 | ' *chars* argument is a string specifying the set of characters ' |
| 12395 | 'to be\n' |
| 12396 | ' removed. If omitted or "None", the *chars* argument defaults ' |
| 12397 | 'to\n' |
| 12398 | ' removing whitespace. The *chars* argument is not a suffix; ' |
| 12399 | 'rather,\n' |
| 12400 | ' all combinations of its values are stripped:\n' |
| 12401 | '\n' |
| 12402 | " >>> ' spacious '.rstrip()\n" |
| 12403 | " ' spacious'\n" |
| 12404 | " >>> 'mississippi'.rstrip('ipz')\n" |
| 12405 | " 'mississ'\n" |
| 12406 | '\n' |
| 12407 | ' Changed in version 2.2.2: Support for the *chars* argument.\n' |
| 12408 | '\n' |
| 12409 | 'str.split([sep[, maxsplit]])\n' |
| 12410 | '\n' |
| 12411 | ' Return a list of the words in the string, using *sep* as the\n' |
| 12412 | ' delimiter string. If *maxsplit* is given, at most ' |
| 12413 | '*maxsplit*\n' |
| 12414 | ' splits are done (thus, the list will have at most ' |
| 12415 | '"maxsplit+1"\n' |
| 12416 | ' elements). If *maxsplit* is not specified or "-1", then ' |
| 12417 | 'there is\n' |
| 12418 | ' no limit on the number of splits (all possible splits are ' |
| 12419 | 'made).\n' |
| 12420 | '\n' |
| 12421 | ' If *sep* is given, consecutive delimiters are not grouped ' |
| 12422 | 'together\n' |
| 12423 | ' and are deemed to delimit empty strings (for example,\n' |
| 12424 | ' "\'1,,2\'.split(\',\')" returns "[\'1\', \'\', \'2\']"). The ' |
| 12425 | '*sep* argument\n' |
| 12426 | ' may consist of multiple characters (for example,\n' |
| 12427 | ' "\'1<>2<>3\'.split(\'<>\')" returns "[\'1\', \'2\', \'3\']"). ' |
| 12428 | 'Splitting an\n' |
| 12429 | ' empty string with a specified separator returns "[\'\']".\n' |
| 12430 | '\n' |
| 12431 | ' If *sep* is not specified or is "None", a different ' |
| 12432 | 'splitting\n' |
| 12433 | ' algorithm is applied: runs of consecutive whitespace are ' |
| 12434 | 'regarded\n' |
| 12435 | ' as a single separator, and the result will contain no empty ' |
| 12436 | 'strings\n' |
| 12437 | ' at the start or end if the string has leading or trailing\n' |
| 12438 | ' whitespace. Consequently, splitting an empty string or a ' |
| 12439 | 'string\n' |
| 12440 | ' consisting of just whitespace with a "None" separator returns ' |
| 12441 | '"[]".\n' |
| 12442 | '\n' |
| 12443 | ' For example, "\' 1 2 3 \'.split()" returns "[\'1\', ' |
| 12444 | '\'2\', \'3\']", and\n' |
| 12445 | ' "\' 1 2 3 \'.split(None, 1)" returns "[\'1\', \'2 3 ' |
| 12446 | '\']".\n' |
| 12447 | '\n' |
| 12448 | 'str.splitlines([keepends])\n' |
| 12449 | '\n' |
| 12450 | ' Return a list of the lines in the string, breaking at line\n' |
| 12451 | ' boundaries. This method uses the *universal newlines* ' |
| 12452 | 'approach to\n' |
| 12453 | ' splitting lines. Line breaks are not included in the ' |
| 12454 | 'resulting list\n' |
| 12455 | ' unless *keepends* is given and true.\n' |
| 12456 | '\n' |
| 12457 | ' Python recognizes ""\\r"", ""\\n"", and ""\\r\\n"" as line ' |
| 12458 | 'boundaries\n' |
| 12459 | ' for 8-bit strings.\n' |
| 12460 | '\n' |
| 12461 | ' For example:\n' |
| 12462 | '\n' |
| 12463 | " >>> 'ab c\\n\\nde fg\\rkl\\r\\n'.splitlines()\n" |
| 12464 | " ['ab c', '', 'de fg', 'kl']\n" |
| 12465 | " >>> 'ab c\\n\\nde fg\\rkl\\r\\n'.splitlines(True)\n" |
| 12466 | " ['ab c\\n', '\\n', 'de fg\\r', 'kl\\r\\n']\n" |
| 12467 | '\n' |
| 12468 | ' Unlike "split()" when a delimiter string *sep* is given, ' |
| 12469 | 'this\n' |
| 12470 | ' method returns an empty list for the empty string, and a ' |
| 12471 | 'terminal\n' |
| 12472 | ' line break does not result in an extra line:\n' |
| 12473 | '\n' |
| 12474 | ' >>> "".splitlines()\n' |
| 12475 | ' []\n' |
| 12476 | ' >>> "One line\\n".splitlines()\n' |
| 12477 | " ['One line']\n" |
| 12478 | '\n' |
| 12479 | ' For comparison, "split(\'\\n\')" gives:\n' |
| 12480 | '\n' |
| 12481 | " >>> ''.split('\\n')\n" |
| 12482 | " ['']\n" |
| 12483 | " >>> 'Two lines\\n'.split('\\n')\n" |
| 12484 | " ['Two lines', '']\n" |
| 12485 | '\n' |
| 12486 | 'unicode.splitlines([keepends])\n' |
| 12487 | '\n' |
| 12488 | ' Return a list of the lines in the string, like ' |
| 12489 | '"str.splitlines()".\n' |
| 12490 | ' However, the Unicode method splits on the following line\n' |
| 12491 | ' boundaries, which are a superset of the *universal newlines*\n' |
| 12492 | ' recognized for 8-bit strings.\n' |
| 12493 | '\n' |
| 12494 | ' +-------------------------+-------------------------------+\n' |
| 12495 | ' | Representation | Description |\n' |
| 12496 | ' +=========================+===============================+\n' |
| 12497 | ' | "\\n" | Line Feed |\n' |
| 12498 | ' +-------------------------+-------------------------------+\n' |
| 12499 | ' | "\\r" | Carriage Return |\n' |
| 12500 | ' +-------------------------+-------------------------------+\n' |
| 12501 | ' | "\\r\\n" | Carriage Return + Line Feed ' |
| 12502 | '|\n' |
| 12503 | ' +-------------------------+-------------------------------+\n' |
| 12504 | ' | "\\v" or "\\x0b" | Line Tabulation ' |
| 12505 | '|\n' |
| 12506 | ' +-------------------------+-------------------------------+\n' |
| 12507 | ' | "\\f" or "\\x0c" | Form Feed ' |
| 12508 | '|\n' |
| 12509 | ' +-------------------------+-------------------------------+\n' |
| 12510 | ' | "\\x1c" | File Separator |\n' |
| 12511 | ' +-------------------------+-------------------------------+\n' |
| 12512 | ' | "\\x1d" | Group Separator |\n' |
| 12513 | ' +-------------------------+-------------------------------+\n' |
| 12514 | ' | "\\x1e" | Record Separator |\n' |
| 12515 | ' +-------------------------+-------------------------------+\n' |
| 12516 | ' | "\\x85" | Next Line (C1 Control Code) |\n' |
| 12517 | ' +-------------------------+-------------------------------+\n' |
| 12518 | ' | "\\u2028" | Line Separator |\n' |
| 12519 | ' +-------------------------+-------------------------------+\n' |
| 12520 | ' | "\\u2029" | Paragraph Separator |\n' |
| 12521 | ' +-------------------------+-------------------------------+\n' |
| 12522 | '\n' |
| 12523 | ' Changed in version 2.7: "\\v" and "\\f" added to list of ' |
| 12524 | 'line\n' |
| 12525 | ' boundaries.\n' |
| 12526 | '\n' |
| 12527 | 'str.startswith(prefix[, start[, end]])\n' |
| 12528 | '\n' |
| 12529 | ' Return "True" if string starts with the *prefix*, otherwise ' |
| 12530 | 'return\n' |
| 12531 | ' "False". *prefix* can also be a tuple of prefixes to look ' |
| 12532 | 'for.\n' |
| 12533 | ' With optional *start*, test string beginning at that ' |
| 12534 | 'position.\n' |
| 12535 | ' With optional *end*, stop comparing string at that position.\n' |
| 12536 | '\n' |
| 12537 | ' Changed in version 2.5: Accept tuples as *prefix*.\n' |
| 12538 | '\n' |
| 12539 | 'str.strip([chars])\n' |
| 12540 | '\n' |
| 12541 | ' Return a copy of the string with the leading and trailing\n' |
| 12542 | ' characters removed. The *chars* argument is a string ' |
| 12543 | 'specifying the\n' |
| 12544 | ' set of characters to be removed. If omitted or "None", the ' |
| 12545 | '*chars*\n' |
| 12546 | ' argument defaults to removing whitespace. The *chars* ' |
| 12547 | 'argument is\n' |
| 12548 | ' not a prefix or suffix; rather, all combinations of its ' |
| 12549 | 'values are\n' |
| 12550 | ' stripped:\n' |
| 12551 | '\n' |
| 12552 | " >>> ' spacious '.strip()\n" |
| 12553 | " 'spacious'\n" |
| 12554 | " >>> 'www.example.com'.strip('cmowz.')\n" |
| 12555 | " 'example'\n" |
| 12556 | '\n' |
| 12557 | ' Changed in version 2.2.2: Support for the *chars* argument.\n' |
| 12558 | '\n' |
| 12559 | 'str.swapcase()\n' |
| 12560 | '\n' |
| 12561 | ' Return a copy of the string with uppercase characters ' |
| 12562 | 'converted to\n' |
| 12563 | ' lowercase and vice versa.\n' |
| 12564 | '\n' |
| 12565 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12566 | '\n' |
| 12567 | 'str.title()\n' |
| 12568 | '\n' |
| 12569 | ' Return a titlecased version of the string where words start ' |
| 12570 | 'with an\n' |
| 12571 | ' uppercase character and the remaining characters are ' |
| 12572 | 'lowercase.\n' |
| 12573 | '\n' |
| 12574 | ' The algorithm uses a simple language-independent definition ' |
| 12575 | 'of a\n' |
| 12576 | ' word as groups of consecutive letters. The definition works ' |
| 12577 | 'in\n' |
| 12578 | ' many contexts but it means that apostrophes in contractions ' |
| 12579 | 'and\n' |
| 12580 | ' possessives form word boundaries, which may not be the ' |
| 12581 | 'desired\n' |
| 12582 | ' result:\n' |
| 12583 | '\n' |
| 12584 | ' >>> "they\'re bill\'s friends from the UK".title()\n' |
| 12585 | ' "They\'Re Bill\'S Friends From The Uk"\n' |
| 12586 | '\n' |
| 12587 | ' A workaround for apostrophes can be constructed using ' |
| 12588 | 'regular\n' |
| 12589 | ' expressions:\n' |
| 12590 | '\n' |
| 12591 | ' >>> import re\n' |
| 12592 | ' >>> def titlecase(s):\n' |
| 12593 | ' ... return re.sub(r"[A-Za-z]+(\'[A-Za-z]+)?",\n' |
| 12594 | ' ... lambda mo: mo.group(0)[0].upper() +\n' |
| 12595 | ' ... mo.group(0)[1:].lower(),\n' |
| 12596 | ' ... s)\n' |
| 12597 | ' ...\n' |
| 12598 | ' >>> titlecase("they\'re bill\'s friends.")\n' |
| 12599 | ' "They\'re Bill\'s Friends."\n' |
| 12600 | '\n' |
| 12601 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12602 | '\n' |
| 12603 | 'str.translate(table[, deletechars])\n' |
| 12604 | '\n' |
| 12605 | ' Return a copy of the string where all characters occurring in ' |
| 12606 | 'the\n' |
| 12607 | ' optional argument *deletechars* are removed, and the ' |
| 12608 | 'remaining\n' |
| 12609 | ' characters have been mapped through the given translation ' |
| 12610 | 'table,\n' |
| 12611 | ' which must be a string of length 256.\n' |
| 12612 | '\n' |
| 12613 | ' You can use the "maketrans()" helper function in the ' |
| 12614 | '"string"\n' |
| 12615 | ' module to create a translation table. For string objects, set ' |
| 12616 | 'the\n' |
| 12617 | ' *table* argument to "None" for translations that only delete\n' |
| 12618 | ' characters:\n' |
| 12619 | '\n' |
| 12620 | " >>> 'read this short text'.translate(None, 'aeiou')\n" |
| 12621 | " 'rd ths shrt txt'\n" |
| 12622 | '\n' |
| 12623 | ' New in version 2.6: Support for a "None" *table* argument.\n' |
| 12624 | '\n' |
| 12625 | ' For Unicode objects, the "translate()" method does not accept ' |
| 12626 | 'the\n' |
| 12627 | ' optional *deletechars* argument. Instead, it returns a copy ' |
| 12628 | 'of the\n' |
| 12629 | ' *s* where all characters have been mapped through the given\n' |
| 12630 | ' translation table which must be a mapping of Unicode ordinals ' |
| 12631 | 'to\n' |
| 12632 | ' Unicode ordinals, Unicode strings or "None". Unmapped ' |
| 12633 | 'characters\n' |
| 12634 | ' are left untouched. Characters mapped to "None" are deleted. ' |
| 12635 | 'Note,\n' |
| 12636 | ' a more flexible approach is to create a custom character ' |
| 12637 | 'mapping\n' |
| 12638 | ' codec using the "codecs" module (see "encodings.cp1251" for ' |
| 12639 | 'an\n' |
| 12640 | ' example).\n' |
| 12641 | '\n' |
| 12642 | 'str.upper()\n' |
| 12643 | '\n' |
| 12644 | ' Return a copy of the string with all the cased characters ' |
| 12645 | '[4]\n' |
| 12646 | ' converted to uppercase. Note that "str.upper().isupper()" ' |
| 12647 | 'might be\n' |
| 12648 | ' "False" if "s" contains uncased characters or if the Unicode\n' |
| 12649 | ' category of the resulting character(s) is not "Lu" (Letter,\n' |
| 12650 | ' uppercase), but e.g. "Lt" (Letter, titlecase).\n' |
| 12651 | '\n' |
| 12652 | ' For 8-bit strings, this method is locale-dependent.\n' |
| 12653 | '\n' |
| 12654 | 'str.zfill(width)\n' |
| 12655 | '\n' |
| 12656 | ' Return the numeric string left filled with zeros in a string ' |
| 12657 | 'of\n' |
| 12658 | ' length *width*. A sign prefix is handled correctly. The ' |
| 12659 | 'original\n' |
| 12660 | ' string is returned if *width* is less than or equal to ' |
| 12661 | '"len(s)".\n' |
| 12662 | '\n' |
| 12663 | ' New in version 2.2.2.\n' |
| 12664 | '\n' |
| 12665 | 'The following methods are present only on unicode objects:\n' |
| 12666 | '\n' |
| 12667 | 'unicode.isnumeric()\n' |
| 12668 | '\n' |
| 12669 | ' Return "True" if there are only numeric characters in S, ' |
| 12670 | '"False"\n' |
| 12671 | ' otherwise. Numeric characters include digit characters, and ' |
| 12672 | 'all\n' |
| 12673 | ' characters that have the Unicode numeric value property, ' |
| 12674 | 'e.g.\n' |
| 12675 | ' U+2155, VULGAR FRACTION ONE FIFTH.\n' |
| 12676 | '\n' |
| 12677 | 'unicode.isdecimal()\n' |
| 12678 | '\n' |
| 12679 | ' Return "True" if there are only decimal characters in S, ' |
| 12680 | '"False"\n' |
| 12681 | ' otherwise. Decimal characters include digit characters, and ' |
| 12682 | 'all\n' |
| 12683 | ' characters that can be used to form decimal-radix numbers, ' |
| 12684 | 'e.g.\n' |
| 12685 | ' U+0660, ARABIC-INDIC DIGIT ZERO.\n' |
| 12686 | '\n' |
| 12687 | '\n' |
| 12688 | 'String Formatting Operations\n' |
| 12689 | '============================\n' |
| 12690 | '\n' |
| 12691 | 'String and Unicode objects have one unique built-in operation: ' |
| 12692 | 'the "%"\n' |
| 12693 | 'operator (modulo). This is also known as the string ' |
| 12694 | '*formatting* or\n' |
| 12695 | '*interpolation* operator. Given "format % values" (where ' |
| 12696 | '*format* is\n' |
| 12697 | 'a string or Unicode object), "%" conversion specifications in ' |
| 12698 | '*format*\n' |
| 12699 | 'are replaced with zero or more elements of *values*. The effect ' |
| 12700 | 'is\n' |
| 12701 | 'similar to the using "sprintf()" in the C language. If *format* ' |
| 12702 | 'is a\n' |
| 12703 | 'Unicode object, or if any of the objects being converted using ' |
| 12704 | 'the\n' |
| 12705 | '"%s" conversion are Unicode objects, the result will also be a ' |
| 12706 | 'Unicode\n' |
| 12707 | 'object.\n' |
| 12708 | '\n' |
| 12709 | 'If *format* requires a single argument, *values* may be a single ' |
| 12710 | 'non-\n' |
| 12711 | 'tuple object. [5] Otherwise, *values* must be a tuple with ' |
| 12712 | 'exactly\n' |
| 12713 | 'the number of items specified by the format string, or a single\n' |
| 12714 | 'mapping object (for example, a dictionary).\n' |
| 12715 | '\n' |
| 12716 | 'A conversion specifier contains two or more characters and has ' |
| 12717 | 'the\n' |
| 12718 | 'following components, which must occur in this order:\n' |
| 12719 | '\n' |
| 12720 | '1. The "\'%\'" character, which marks the start of the ' |
| 12721 | 'specifier.\n' |
| 12722 | '\n' |
| 12723 | '2. Mapping key (optional), consisting of a parenthesised ' |
| 12724 | 'sequence\n' |
| 12725 | ' of characters (for example, "(somename)").\n' |
| 12726 | '\n' |
| 12727 | '3. Conversion flags (optional), which affect the result of some\n' |
| 12728 | ' conversion types.\n' |
| 12729 | '\n' |
| 12730 | '4. Minimum field width (optional). If specified as an "\'*\'"\n' |
| 12731 | ' (asterisk), the actual width is read from the next element of ' |
| 12732 | 'the\n' |
| 12733 | ' tuple in *values*, and the object to convert comes after the\n' |
| 12734 | ' minimum field width and optional precision.\n' |
| 12735 | '\n' |
| 12736 | '5. Precision (optional), given as a "\'.\'" (dot) followed by ' |
| 12737 | 'the\n' |
| 12738 | ' precision. If specified as "\'*\'" (an asterisk), the actual ' |
| 12739 | 'width\n' |
| 12740 | ' is read from the next element of the tuple in *values*, and ' |
| 12741 | 'the\n' |
| 12742 | ' value to convert comes after the precision.\n' |
| 12743 | '\n' |
| 12744 | '6. Length modifier (optional).\n' |
| 12745 | '\n' |
| 12746 | '7. Conversion type.\n' |
| 12747 | '\n' |
| 12748 | 'When the right argument is a dictionary (or other mapping type), ' |
| 12749 | 'then\n' |
| 12750 | 'the formats in the string *must* include a parenthesised mapping ' |
| 12751 | 'key\n' |
| 12752 | 'into that dictionary inserted immediately after the "\'%\'" ' |
| 12753 | 'character.\n' |
| 12754 | 'The mapping key selects the value to be formatted from the ' |
| 12755 | 'mapping.\n' |
| 12756 | 'For example:\n' |
| 12757 | '\n' |
| 12758 | ">>> print '%(language)s has %(number)03d quote types.' % \\\n" |
| 12759 | '... {"language": "Python", "number": 2}\n' |
| 12760 | 'Python has 002 quote types.\n' |
| 12761 | '\n' |
| 12762 | 'In this case no "*" specifiers may occur in a format (since ' |
| 12763 | 'they\n' |
| 12764 | 'require a sequential parameter list).\n' |
| 12765 | '\n' |
| 12766 | 'The conversion flag characters are:\n' |
| 12767 | '\n' |
| 12768 | '+-----------+-----------------------------------------------------------------------+\n' |
| 12769 | '| Flag | ' |
| 12770 | 'Meaning ' |
| 12771 | '|\n' |
| 12772 | '+===========+=======================================================================+\n' |
| 12773 | '| "\'#\'" | The value conversion will use the "alternate ' |
| 12774 | 'form" (where defined |\n' |
| 12775 | '| | ' |
| 12776 | 'below). ' |
| 12777 | '|\n' |
| 12778 | '+-----------+-----------------------------------------------------------------------+\n' |
| 12779 | '| "\'0\'" | The conversion will be zero padded for numeric ' |
| 12780 | 'values. |\n' |
| 12781 | '+-----------+-----------------------------------------------------------------------+\n' |
| 12782 | '| "\'-\'" | The converted value is left adjusted (overrides ' |
| 12783 | 'the "\'0\'" conversion |\n' |
| 12784 | '| | if both are ' |
| 12785 | 'given). |\n' |
| 12786 | '+-----------+-----------------------------------------------------------------------+\n' |
| 12787 | '| "\' \'" | (a space) A blank should be left before a ' |
| 12788 | 'positive number (or empty |\n' |
| 12789 | '| | string) produced by a signed ' |
| 12790 | 'conversion. |\n' |
| 12791 | '+-----------+-----------------------------------------------------------------------+\n' |
| 12792 | '| "\'+\'" | A sign character ("\'+\'" or "\'-\'") will ' |
| 12793 | 'precede the conversion |\n' |
| 12794 | '| | (overrides a "space" ' |
| 12795 | 'flag). |\n' |
| 12796 | '+-----------+-----------------------------------------------------------------------+\n' |
| 12797 | '\n' |
| 12798 | 'A length modifier ("h", "l", or "L") may be present, but is ' |
| 12799 | 'ignored as\n' |
| 12800 | 'it is not necessary for Python -- so e.g. "%ld" is identical to ' |
| 12801 | '"%d".\n' |
| 12802 | '\n' |
| 12803 | 'The conversion types are:\n' |
| 12804 | '\n' |
| 12805 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12806 | '| Conversion | ' |
| 12807 | 'Meaning | Notes ' |
| 12808 | '|\n' |
| 12809 | '+==============+=======================================================+=========+\n' |
| 12810 | '| "\'d\'" | Signed integer ' |
| 12811 | 'decimal. | |\n' |
| 12812 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12813 | '| "\'i\'" | Signed integer ' |
| 12814 | 'decimal. | |\n' |
| 12815 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12816 | '| "\'o\'" | Signed octal ' |
| 12817 | 'value. | (1) |\n' |
| 12818 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12819 | '| "\'u\'" | Obsolete type -- it is identical to ' |
| 12820 | '"\'d\'". | (7) |\n' |
| 12821 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12822 | '| "\'x\'" | Signed hexadecimal ' |
| 12823 | '(lowercase). | (2) |\n' |
| 12824 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12825 | '| "\'X\'" | Signed hexadecimal ' |
| 12826 | '(uppercase). | (2) |\n' |
| 12827 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12828 | '| "\'e\'" | Floating point exponential format ' |
| 12829 | '(lowercase). | (3) |\n' |
| 12830 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12831 | '| "\'E\'" | Floating point exponential format ' |
| 12832 | '(uppercase). | (3) |\n' |
| 12833 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12834 | '| "\'f\'" | Floating point decimal ' |
| 12835 | 'format. | (3) |\n' |
| 12836 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12837 | '| "\'F\'" | Floating point decimal ' |
| 12838 | 'format. | (3) |\n' |
| 12839 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12840 | '| "\'g\'" | Floating point format. Uses lowercase ' |
| 12841 | 'exponential | (4) |\n' |
| 12842 | '| | format if exponent is less than -4 or not less ' |
| 12843 | 'than | |\n' |
| 12844 | '| | precision, decimal format ' |
| 12845 | 'otherwise. | |\n' |
| 12846 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12847 | '| "\'G\'" | Floating point format. Uses uppercase ' |
| 12848 | 'exponential | (4) |\n' |
| 12849 | '| | format if exponent is less than -4 or not less ' |
| 12850 | 'than | |\n' |
| 12851 | '| | precision, decimal format ' |
| 12852 | 'otherwise. | |\n' |
| 12853 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12854 | '| "\'c\'" | Single character (accepts integer or single ' |
| 12855 | 'character | |\n' |
| 12856 | '| | ' |
| 12857 | 'string). | ' |
| 12858 | '|\n' |
| 12859 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12860 | '| "\'r\'" | String (converts any Python object using ' |
| 12861 | 'repr()). | (5) |\n' |
| 12862 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12863 | '| "\'s\'" | String (converts any Python object using ' |
| 12864 | '"str()"). | (6) |\n' |
| 12865 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12866 | '| "\'%\'" | No argument is converted, results in a ' |
| 12867 | '"\'%\'" | |\n' |
| 12868 | '| | character in the ' |
| 12869 | 'result. | |\n' |
| 12870 | '+--------------+-------------------------------------------------------+---------+\n' |
| 12871 | '\n' |
| 12872 | 'Notes:\n' |
| 12873 | '\n' |
| 12874 | '1. The alternate form causes a leading zero ("\'0\'") to be ' |
| 12875 | 'inserted\n' |
| 12876 | ' between left-hand padding and the formatting of the number if ' |
| 12877 | 'the\n' |
| 12878 | ' leading character of the result is not already a zero.\n' |
| 12879 | '\n' |
| 12880 | '2. The alternate form causes a leading "\'0x\'" or "\'0X\'" ' |
| 12881 | '(depending\n' |
| 12882 | ' on whether the "\'x\'" or "\'X\'" format was used) to be ' |
| 12883 | 'inserted\n' |
| 12884 | ' before the first digit.\n' |
| 12885 | '\n' |
| 12886 | '3. The alternate form causes the result to always contain a ' |
| 12887 | 'decimal\n' |
| 12888 | ' point, even if no digits follow it.\n' |
| 12889 | '\n' |
| 12890 | ' The precision determines the number of digits after the ' |
| 12891 | 'decimal\n' |
| 12892 | ' point and defaults to 6.\n' |
| 12893 | '\n' |
| 12894 | '4. The alternate form causes the result to always contain a ' |
| 12895 | 'decimal\n' |
| 12896 | ' point, and trailing zeroes are not removed as they would ' |
| 12897 | 'otherwise\n' |
| 12898 | ' be.\n' |
| 12899 | '\n' |
| 12900 | ' The precision determines the number of significant digits ' |
| 12901 | 'before\n' |
| 12902 | ' and after the decimal point and defaults to 6.\n' |
| 12903 | '\n' |
| 12904 | '5. The "%r" conversion was added in Python 2.0.\n' |
| 12905 | '\n' |
| 12906 | ' The precision determines the maximal number of characters ' |
| 12907 | 'used.\n' |
| 12908 | '\n' |
| 12909 | '6. If the object or format provided is a "unicode" string, the\n' |
| 12910 | ' resulting string will also be "unicode".\n' |
| 12911 | '\n' |
| 12912 | ' The precision determines the maximal number of characters ' |
| 12913 | 'used.\n' |
| 12914 | '\n' |
| 12915 | '7. See **PEP 237**.\n' |
| 12916 | '\n' |
| 12917 | 'Since Python strings have an explicit length, "%s" conversions ' |
| 12918 | 'do not\n' |
| 12919 | 'assume that "\'\\0\'" is the end of the string.\n' |
| 12920 | '\n' |
| 12921 | 'Changed in version 2.7: "%f" conversions for numbers whose ' |
| 12922 | 'absolute\n' |
| 12923 | 'value is over 1e50 are no longer replaced by "%g" conversions.\n' |
| 12924 | '\n' |
| 12925 | 'Additional string operations are defined in standard modules ' |
| 12926 | '"string"\n' |
| 12927 | 'and "re".\n' |
| 12928 | '\n' |
| 12929 | '\n' |
| 12930 | 'XRange Type\n' |
| 12931 | '===========\n' |
| 12932 | '\n' |
| 12933 | 'The "xrange" type is an immutable sequence which is commonly ' |
| 12934 | 'used for\n' |
| 12935 | 'looping. The advantage of the "xrange" type is that an ' |
| 12936 | '"xrange"\n' |
| 12937 | 'object will always take the same amount of memory, no matter the ' |
| 12938 | 'size\n' |
| 12939 | 'of the range it represents. There are no consistent ' |
| 12940 | 'performance\n' |
| 12941 | 'advantages.\n' |
| 12942 | '\n' |
| 12943 | 'XRange objects have very little behavior: they only support ' |
| 12944 | 'indexing,\n' |
| 12945 | 'iteration, and the "len()" function.\n' |
| 12946 | '\n' |
| 12947 | '\n' |
| 12948 | 'Mutable Sequence Types\n' |
| 12949 | '======================\n' |
| 12950 | '\n' |
| 12951 | 'List and "bytearray" objects support additional operations that ' |
| 12952 | 'allow\n' |
| 12953 | 'in-place modification of the object. Other mutable sequence ' |
| 12954 | 'types\n' |
| 12955 | '(when added to the language) should also support these ' |
| 12956 | 'operations.\n' |
| 12957 | 'Strings and tuples are immutable sequence types: such objects ' |
| 12958 | 'cannot\n' |
| 12959 | 'be modified once created. The following operations are defined ' |
| 12960 | 'on\n' |
| 12961 | 'mutable sequence types (where *x* is an arbitrary object):\n' |
| 12962 | '\n' |
| 12963 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12964 | '| Operation | ' |
| 12965 | 'Result | Notes |\n' |
| 12966 | '+================================+==================================+=======================+\n' |
| 12967 | '| "s[i] = x" | item *i* of *s* is replaced ' |
| 12968 | 'by | |\n' |
| 12969 | '| | ' |
| 12970 | '*x* | |\n' |
| 12971 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12972 | '| "s[i:j] = t" | slice of *s* from *i* to *j* ' |
| 12973 | 'is | |\n' |
| 12974 | '| | replaced by the contents of ' |
| 12975 | 'the | |\n' |
| 12976 | '| | iterable ' |
| 12977 | '*t* | |\n' |
| 12978 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12979 | '| "del s[i:j]" | same as "s[i:j] = ' |
| 12980 | '[]" | |\n' |
| 12981 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12982 | '| "s[i:j:k] = t" | the elements of "s[i:j:k]" ' |
| 12983 | 'are | (1) |\n' |
| 12984 | '| | replaced by those of ' |
| 12985 | '*t* | |\n' |
| 12986 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12987 | '| "del s[i:j:k]" | removes the elements ' |
| 12988 | 'of | |\n' |
| 12989 | '| | "s[i:j:k]" from the ' |
| 12990 | 'list | |\n' |
| 12991 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12992 | '| "s.append(x)" | same as "s[len(s):len(s)] = ' |
| 12993 | '[x]" | (2) |\n' |
| 12994 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 12995 | '| "s.extend(t)" or "s += t" | for the most part the same ' |
| 12996 | 'as | (3) |\n' |
| 12997 | '| | "s[len(s):len(s)] = ' |
| 12998 | 't" | |\n' |
| 12999 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13000 | '| "s *= n" | updates *s* with its ' |
| 13001 | 'contents | (11) |\n' |
| 13002 | '| | repeated *n* ' |
| 13003 | 'times | |\n' |
| 13004 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13005 | '| "s.count(x)" | return number of *i*\'s for ' |
| 13006 | 'which | |\n' |
| 13007 | '| | "s[i] == ' |
| 13008 | 'x" | |\n' |
| 13009 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13010 | '| "s.index(x[, i[, j]])" | return smallest *k* such ' |
| 13011 | 'that | (4) |\n' |
| 13012 | '| | "s[k] == x" and "i <= k < ' |
| 13013 | 'j" | |\n' |
| 13014 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13015 | '| "s.insert(i, x)" | same as "s[i:i] = ' |
| 13016 | '[x]" | (5) |\n' |
| 13017 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13018 | '| "s.pop([i])" | same as "x = s[i]; del ' |
| 13019 | 's[i]; | (6) |\n' |
| 13020 | '| | return ' |
| 13021 | 'x" | |\n' |
| 13022 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13023 | '| "s.remove(x)" | same as "del ' |
| 13024 | 's[s.index(x)]" | (4) |\n' |
| 13025 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13026 | '| "s.reverse()" | reverses the items of *s* ' |
| 13027 | 'in | (7) |\n' |
| 13028 | '| | ' |
| 13029 | 'place | |\n' |
| 13030 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13031 | '| "s.sort([cmp[, key[, | sort the items of *s* in ' |
| 13032 | 'place | (7)(8)(9)(10) |\n' |
| 13033 | '| reverse]]])" ' |
| 13034 | '| | |\n' |
| 13035 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13036 | '\n' |
| 13037 | 'Notes:\n' |
| 13038 | '\n' |
| 13039 | '1. *t* must have the same length as the slice it is replacing.\n' |
| 13040 | '\n' |
| 13041 | '2. The C implementation of Python has historically accepted\n' |
| 13042 | ' multiple parameters and implicitly joined them into a tuple; ' |
| 13043 | 'this\n' |
| 13044 | ' no longer works in Python 2.0. Use of this misfeature has ' |
| 13045 | 'been\n' |
| 13046 | ' deprecated since Python 1.4.\n' |
| 13047 | '\n' |
| 13048 | '3. *t* can be any iterable object.\n' |
| 13049 | '\n' |
| 13050 | '4. Raises "ValueError" when *x* is not found in *s*. When a\n' |
| 13051 | ' negative index is passed as the second or third parameter to ' |
| 13052 | 'the\n' |
| 13053 | ' "index()" method, the list length is added, as for slice ' |
| 13054 | 'indices.\n' |
| 13055 | ' If it is still negative, it is truncated to zero, as for ' |
| 13056 | 'slice\n' |
| 13057 | ' indices.\n' |
| 13058 | '\n' |
| 13059 | ' Changed in version 2.3: Previously, "index()" didn\'t have ' |
| 13060 | 'arguments\n' |
| 13061 | ' for specifying start and stop positions.\n' |
| 13062 | '\n' |
| 13063 | '5. When a negative index is passed as the first parameter to ' |
| 13064 | 'the\n' |
| 13065 | ' "insert()" method, the list length is added, as for slice ' |
| 13066 | 'indices.\n' |
| 13067 | ' If it is still negative, it is truncated to zero, as for ' |
| 13068 | 'slice\n' |
| 13069 | ' indices.\n' |
| 13070 | '\n' |
| 13071 | ' Changed in version 2.3: Previously, all negative indices ' |
| 13072 | 'were\n' |
| 13073 | ' truncated to zero.\n' |
| 13074 | '\n' |
| 13075 | '6. The "pop()" method\'s optional argument *i* defaults to "-1", ' |
| 13076 | 'so\n' |
| 13077 | ' that by default the last item is removed and returned.\n' |
| 13078 | '\n' |
| 13079 | '7. The "sort()" and "reverse()" methods modify the list in ' |
| 13080 | 'place\n' |
| 13081 | ' for economy of space when sorting or reversing a large list. ' |
| 13082 | 'To\n' |
| 13083 | " remind you that they operate by side effect, they don't " |
| 13084 | 'return the\n' |
| 13085 | ' sorted or reversed list.\n' |
| 13086 | '\n' |
| 13087 | '8. The "sort()" method takes optional arguments for controlling ' |
| 13088 | 'the\n' |
| 13089 | ' comparisons.\n' |
| 13090 | '\n' |
| 13091 | ' *cmp* specifies a custom comparison function of two arguments ' |
| 13092 | '(list\n' |
| 13093 | ' items) which should return a negative, zero or positive ' |
| 13094 | 'number\n' |
| 13095 | ' depending on whether the first argument is considered smaller ' |
| 13096 | 'than,\n' |
| 13097 | ' equal to, or larger than the second argument: "cmp=lambda ' |
| 13098 | 'x,y:\n' |
| 13099 | ' cmp(x.lower(), y.lower())". The default value is "None".\n' |
| 13100 | '\n' |
| 13101 | ' *key* specifies a function of one argument that is used to ' |
| 13102 | 'extract\n' |
| 13103 | ' a comparison key from each list element: "key=str.lower". ' |
| 13104 | 'The\n' |
| 13105 | ' default value is "None".\n' |
| 13106 | '\n' |
| 13107 | ' *reverse* is a boolean value. If set to "True", then the ' |
| 13108 | 'list\n' |
| 13109 | ' elements are sorted as if each comparison were reversed.\n' |
| 13110 | '\n' |
| 13111 | ' In general, the *key* and *reverse* conversion processes are ' |
| 13112 | 'much\n' |
| 13113 | ' faster than specifying an equivalent *cmp* function. This ' |
| 13114 | 'is\n' |
| 13115 | ' because *cmp* is called multiple times for each list element ' |
| 13116 | 'while\n' |
| 13117 | ' *key* and *reverse* touch each element only once. Use\n' |
| 13118 | ' "functools.cmp_to_key()" to convert an old-style *cmp* ' |
| 13119 | 'function to\n' |
| 13120 | ' a *key* function.\n' |
| 13121 | '\n' |
| 13122 | ' Changed in version 2.3: Support for "None" as an equivalent ' |
| 13123 | 'to\n' |
| 13124 | ' omitting *cmp* was added.\n' |
| 13125 | '\n' |
| 13126 | ' Changed in version 2.4: Support for *key* and *reverse* was ' |
| 13127 | 'added.\n' |
| 13128 | '\n' |
| 13129 | '9. Starting with Python 2.3, the "sort()" method is guaranteed ' |
| 13130 | 'to\n' |
| 13131 | ' be stable. A sort is stable if it guarantees not to change ' |
| 13132 | 'the\n' |
| 13133 | ' relative order of elements that compare equal --- this is ' |
| 13134 | 'helpful\n' |
| 13135 | ' for sorting in multiple passes (for example, sort by ' |
| 13136 | 'department,\n' |
| 13137 | ' then by salary grade).\n' |
| 13138 | '\n' |
| 13139 | '10. **CPython implementation detail:** While a list is being\n' |
| 13140 | ' sorted, the effect of attempting to mutate, or even inspect, ' |
| 13141 | 'the\n' |
| 13142 | ' list is undefined. The C implementation of Python 2.3 and ' |
| 13143 | 'newer\n' |
| 13144 | ' makes the list appear empty for the duration, and raises\n' |
| 13145 | ' "ValueError" if it can detect that the list has been ' |
| 13146 | 'mutated\n' |
| 13147 | ' during a sort.\n' |
| 13148 | '\n' |
| 13149 | '11. The value *n* is an integer, or an object implementing\n' |
| 13150 | ' "__index__()". Zero and negative values of *n* clear the\n' |
| 13151 | ' sequence. Items in the sequence are not copied; they are\n' |
| 13152 | ' referenced multiple times, as explained for "s * n" under ' |
| 13153 | 'Sequence\n' |
| 13154 | ' Types --- str, unicode, list, tuple, bytearray, buffer, ' |
| 13155 | 'xrange.\n', |
| 13156 | 'typesseq-mutable': '\n' |
| 13157 | 'Mutable Sequence Types\n' |
| 13158 | '**********************\n' |
| 13159 | '\n' |
| 13160 | 'List and "bytearray" objects support additional ' |
| 13161 | 'operations that allow\n' |
| 13162 | 'in-place modification of the object. Other mutable ' |
| 13163 | 'sequence types\n' |
| 13164 | '(when added to the language) should also support these ' |
| 13165 | 'operations.\n' |
| 13166 | 'Strings and tuples are immutable sequence types: such ' |
| 13167 | 'objects cannot\n' |
| 13168 | 'be modified once created. The following operations are ' |
| 13169 | 'defined on\n' |
| 13170 | 'mutable sequence types (where *x* is an arbitrary ' |
| 13171 | 'object):\n' |
| 13172 | '\n' |
| 13173 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13174 | '| Operation | ' |
| 13175 | 'Result | Notes ' |
| 13176 | '|\n' |
| 13177 | '+================================+==================================+=======================+\n' |
| 13178 | '| "s[i] = x" | item *i* of *s* is ' |
| 13179 | 'replaced by | |\n' |
| 13180 | '| | ' |
| 13181 | '*x* | ' |
| 13182 | '|\n' |
| 13183 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13184 | '| "s[i:j] = t" | slice of *s* from *i* ' |
| 13185 | 'to *j* is | |\n' |
| 13186 | '| | replaced by the ' |
| 13187 | 'contents of the | |\n' |
| 13188 | '| | iterable ' |
| 13189 | '*t* | |\n' |
| 13190 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13191 | '| "del s[i:j]" | same as "s[i:j] = ' |
| 13192 | '[]" | |\n' |
| 13193 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13194 | '| "s[i:j:k] = t" | the elements of ' |
| 13195 | '"s[i:j:k]" are | (1) |\n' |
| 13196 | '| | replaced by those of ' |
| 13197 | '*t* | |\n' |
| 13198 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13199 | '| "del s[i:j:k]" | removes the elements ' |
| 13200 | 'of | |\n' |
| 13201 | '| | "s[i:j:k]" from the ' |
| 13202 | 'list | |\n' |
| 13203 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13204 | '| "s.append(x)" | same as ' |
| 13205 | '"s[len(s):len(s)] = [x]" | (2) |\n' |
| 13206 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13207 | '| "s.extend(t)" or "s += t" | for the most part the ' |
| 13208 | 'same as | (3) |\n' |
| 13209 | '| | "s[len(s):len(s)] = ' |
| 13210 | 't" | |\n' |
| 13211 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13212 | '| "s *= n" | updates *s* with its ' |
| 13213 | 'contents | (11) |\n' |
| 13214 | '| | repeated *n* ' |
| 13215 | 'times | |\n' |
| 13216 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13217 | '| "s.count(x)" | return number of ' |
| 13218 | "*i*'s for which | |\n" |
| 13219 | '| | "s[i] == ' |
| 13220 | 'x" | |\n' |
| 13221 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13222 | '| "s.index(x[, i[, j]])" | return smallest *k* ' |
| 13223 | 'such that | (4) |\n' |
| 13224 | '| | "s[k] == x" and "i <= ' |
| 13225 | 'k < j" | |\n' |
| 13226 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13227 | '| "s.insert(i, x)" | same as "s[i:i] = ' |
| 13228 | '[x]" | (5) |\n' |
| 13229 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13230 | '| "s.pop([i])" | same as "x = s[i]; ' |
| 13231 | 'del s[i]; | (6) |\n' |
| 13232 | '| | return ' |
| 13233 | 'x" | |\n' |
| 13234 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13235 | '| "s.remove(x)" | same as "del ' |
| 13236 | 's[s.index(x)]" | (4) |\n' |
| 13237 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13238 | '| "s.reverse()" | reverses the items of ' |
| 13239 | '*s* in | (7) |\n' |
| 13240 | '| | ' |
| 13241 | 'place | ' |
| 13242 | '|\n' |
| 13243 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13244 | '| "s.sort([cmp[, key[, | sort the items of *s* ' |
| 13245 | 'in place | (7)(8)(9)(10) |\n' |
| 13246 | '| reverse]]])" ' |
| 13247 | '| ' |
| 13248 | '| |\n' |
| 13249 | '+--------------------------------+----------------------------------+-----------------------+\n' |
| 13250 | '\n' |
| 13251 | 'Notes:\n' |
| 13252 | '\n' |
| 13253 | '1. *t* must have the same length as the slice it is ' |
| 13254 | 'replacing.\n' |
| 13255 | '\n' |
| 13256 | '2. The C implementation of Python has historically ' |
| 13257 | 'accepted\n' |
| 13258 | ' multiple parameters and implicitly joined them into a ' |
| 13259 | 'tuple; this\n' |
| 13260 | ' no longer works in Python 2.0. Use of this ' |
| 13261 | 'misfeature has been\n' |
| 13262 | ' deprecated since Python 1.4.\n' |
| 13263 | '\n' |
| 13264 | '3. *t* can be any iterable object.\n' |
| 13265 | '\n' |
| 13266 | '4. Raises "ValueError" when *x* is not found in *s*. ' |
| 13267 | 'When a\n' |
| 13268 | ' negative index is passed as the second or third ' |
| 13269 | 'parameter to the\n' |
| 13270 | ' "index()" method, the list length is added, as for ' |
| 13271 | 'slice indices.\n' |
| 13272 | ' If it is still negative, it is truncated to zero, as ' |
| 13273 | 'for slice\n' |
| 13274 | ' indices.\n' |
| 13275 | '\n' |
| 13276 | ' Changed in version 2.3: Previously, "index()" didn\'t ' |
| 13277 | 'have arguments\n' |
| 13278 | ' for specifying start and stop positions.\n' |
| 13279 | '\n' |
| 13280 | '5. When a negative index is passed as the first ' |
| 13281 | 'parameter to the\n' |
| 13282 | ' "insert()" method, the list length is added, as for ' |
| 13283 | 'slice indices.\n' |
| 13284 | ' If it is still negative, it is truncated to zero, as ' |
| 13285 | 'for slice\n' |
| 13286 | ' indices.\n' |
| 13287 | '\n' |
| 13288 | ' Changed in version 2.3: Previously, all negative ' |
| 13289 | 'indices were\n' |
| 13290 | ' truncated to zero.\n' |
| 13291 | '\n' |
| 13292 | '6. The "pop()" method\'s optional argument *i* defaults ' |
| 13293 | 'to "-1", so\n' |
| 13294 | ' that by default the last item is removed and ' |
| 13295 | 'returned.\n' |
| 13296 | '\n' |
| 13297 | '7. The "sort()" and "reverse()" methods modify the list ' |
| 13298 | 'in place\n' |
| 13299 | ' for economy of space when sorting or reversing a ' |
| 13300 | 'large list. To\n' |
| 13301 | ' remind you that they operate by side effect, they ' |
| 13302 | "don't return the\n" |
| 13303 | ' sorted or reversed list.\n' |
| 13304 | '\n' |
| 13305 | '8. The "sort()" method takes optional arguments for ' |
| 13306 | 'controlling the\n' |
| 13307 | ' comparisons.\n' |
| 13308 | '\n' |
| 13309 | ' *cmp* specifies a custom comparison function of two ' |
| 13310 | 'arguments (list\n' |
| 13311 | ' items) which should return a negative, zero or ' |
| 13312 | 'positive number\n' |
| 13313 | ' depending on whether the first argument is considered ' |
| 13314 | 'smaller than,\n' |
| 13315 | ' equal to, or larger than the second argument: ' |
| 13316 | '"cmp=lambda x,y:\n' |
| 13317 | ' cmp(x.lower(), y.lower())". The default value is ' |
| 13318 | '"None".\n' |
| 13319 | '\n' |
| 13320 | ' *key* specifies a function of one argument that is ' |
| 13321 | 'used to extract\n' |
| 13322 | ' a comparison key from each list element: ' |
| 13323 | '"key=str.lower". The\n' |
| 13324 | ' default value is "None".\n' |
| 13325 | '\n' |
| 13326 | ' *reverse* is a boolean value. If set to "True", then ' |
| 13327 | 'the list\n' |
| 13328 | ' elements are sorted as if each comparison were ' |
| 13329 | 'reversed.\n' |
| 13330 | '\n' |
| 13331 | ' In general, the *key* and *reverse* conversion ' |
| 13332 | 'processes are much\n' |
| 13333 | ' faster than specifying an equivalent *cmp* function. ' |
| 13334 | 'This is\n' |
| 13335 | ' because *cmp* is called multiple times for each list ' |
| 13336 | 'element while\n' |
| 13337 | ' *key* and *reverse* touch each element only once. ' |
| 13338 | 'Use\n' |
| 13339 | ' "functools.cmp_to_key()" to convert an old-style ' |
| 13340 | '*cmp* function to\n' |
| 13341 | ' a *key* function.\n' |
| 13342 | '\n' |
| 13343 | ' Changed in version 2.3: Support for "None" as an ' |
| 13344 | 'equivalent to\n' |
| 13345 | ' omitting *cmp* was added.\n' |
| 13346 | '\n' |
| 13347 | ' Changed in version 2.4: Support for *key* and ' |
| 13348 | '*reverse* was added.\n' |
| 13349 | '\n' |
| 13350 | '9. Starting with Python 2.3, the "sort()" method is ' |
| 13351 | 'guaranteed to\n' |
| 13352 | ' be stable. A sort is stable if it guarantees not to ' |
| 13353 | 'change the\n' |
| 13354 | ' relative order of elements that compare equal --- ' |
| 13355 | 'this is helpful\n' |
| 13356 | ' for sorting in multiple passes (for example, sort by ' |
| 13357 | 'department,\n' |
| 13358 | ' then by salary grade).\n' |
| 13359 | '\n' |
| 13360 | '10. **CPython implementation detail:** While a list is ' |
| 13361 | 'being\n' |
| 13362 | ' sorted, the effect of attempting to mutate, or even ' |
| 13363 | 'inspect, the\n' |
| 13364 | ' list is undefined. The C implementation of Python ' |
| 13365 | '2.3 and newer\n' |
| 13366 | ' makes the list appear empty for the duration, and ' |
| 13367 | 'raises\n' |
| 13368 | ' "ValueError" if it can detect that the list has been ' |
| 13369 | 'mutated\n' |
| 13370 | ' during a sort.\n' |
| 13371 | '\n' |
| 13372 | '11. The value *n* is an integer, or an object ' |
| 13373 | 'implementing\n' |
| 13374 | ' "__index__()". Zero and negative values of *n* ' |
| 13375 | 'clear the\n' |
| 13376 | ' sequence. Items in the sequence are not copied; ' |
| 13377 | 'they are\n' |
| 13378 | ' referenced multiple times, as explained for "s * n" ' |
| 13379 | 'under Sequence\n' |
| 13380 | ' Types --- str, unicode, list, tuple, bytearray, ' |
| 13381 | 'buffer, xrange.\n', |
| 13382 | 'unary': '\n' |
| 13383 | 'Unary arithmetic and bitwise operations\n' |
| 13384 | '***************************************\n' |
| 13385 | '\n' |
| 13386 | 'All unary arithmetic and bitwise operations have the same ' |
| 13387 | 'priority:\n' |
| 13388 | '\n' |
| 13389 | ' u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr\n' |
| 13390 | '\n' |
| 13391 | 'The unary "-" (minus) operator yields the negation of its numeric\n' |
| 13392 | 'argument.\n' |
| 13393 | '\n' |
| 13394 | 'The unary "+" (plus) operator yields its numeric argument ' |
| 13395 | 'unchanged.\n' |
| 13396 | '\n' |
| 13397 | 'The unary "~" (invert) operator yields the bitwise inversion of ' |
| 13398 | 'its\n' |
| 13399 | 'plain or long integer argument. The bitwise inversion of "x" is\n' |
| 13400 | 'defined as "-(x+1)". It only applies to integral numbers.\n' |
| 13401 | '\n' |
| 13402 | 'In all three cases, if the argument does not have the proper type, ' |
| 13403 | 'a\n' |
| 13404 | '"TypeError" exception is raised.\n', |
| 13405 | 'while': '\n' |
| 13406 | 'The "while" statement\n' |
| 13407 | '*********************\n' |
| 13408 | '\n' |
| 13409 | 'The "while" statement is used for repeated execution as long as an\n' |
| 13410 | 'expression is true:\n' |
| 13411 | '\n' |
| 13412 | ' while_stmt ::= "while" expression ":" suite\n' |
| 13413 | ' ["else" ":" suite]\n' |
| 13414 | '\n' |
| 13415 | 'This repeatedly tests the expression and, if it is true, executes ' |
| 13416 | 'the\n' |
| 13417 | 'first suite; if the expression is false (which may be the first ' |
| 13418 | 'time\n' |
| 13419 | 'it is tested) the suite of the "else" clause, if present, is ' |
| 13420 | 'executed\n' |
| 13421 | 'and the loop terminates.\n' |
| 13422 | '\n' |
| 13423 | 'A "break" statement executed in the first suite terminates the ' |
| 13424 | 'loop\n' |
| 13425 | 'without executing the "else" clause\'s suite. A "continue" ' |
| 13426 | 'statement\n' |
| 13427 | 'executed in the first suite skips the rest of the suite and goes ' |
| 13428 | 'back\n' |
| 13429 | 'to testing the expression.\n', |
| 13430 | 'with': '\n' |
| 13431 | 'The "with" statement\n' |
| 13432 | '********************\n' |
| 13433 | '\n' |
| 13434 | 'New in version 2.5.\n' |
| 13435 | '\n' |
| 13436 | 'The "with" statement is used to wrap the execution of a block with\n' |
| 13437 | 'methods defined by a context manager (see section With Statement\n' |
| 13438 | 'Context Managers). This allows common "try"..."except"..."finally"\n' |
| 13439 | 'usage patterns to be encapsulated for convenient reuse.\n' |
| 13440 | '\n' |
| 13441 | ' with_stmt ::= "with" with_item ("," with_item)* ":" suite\n' |
| 13442 | ' with_item ::= expression ["as" target]\n' |
| 13443 | '\n' |
| 13444 | 'The execution of the "with" statement with one "item" proceeds as\n' |
| 13445 | 'follows:\n' |
| 13446 | '\n' |
| 13447 | '1. The context expression (the expression given in the "with_item")\n' |
| 13448 | ' is evaluated to obtain a context manager.\n' |
| 13449 | '\n' |
| 13450 | '2. The context manager\'s "__exit__()" is loaded for later use.\n' |
| 13451 | '\n' |
| 13452 | '3. The context manager\'s "__enter__()" method is invoked.\n' |
| 13453 | '\n' |
| 13454 | '4. If a target was included in the "with" statement, the return\n' |
| 13455 | ' value from "__enter__()" is assigned to it.\n' |
| 13456 | '\n' |
| 13457 | ' Note: The "with" statement guarantees that if the "__enter__()"\n' |
| 13458 | ' method returns without an error, then "__exit__()" will always ' |
| 13459 | 'be\n' |
| 13460 | ' called. Thus, if an error occurs during the assignment to the\n' |
| 13461 | ' target list, it will be treated the same as an error occurring\n' |
| 13462 | ' within the suite would be. See step 6 below.\n' |
| 13463 | '\n' |
| 13464 | '5. The suite is executed.\n' |
| 13465 | '\n' |
| 13466 | '6. The context manager\'s "__exit__()" method is invoked. If an\n' |
| 13467 | ' exception caused the suite to be exited, its type, value, and\n' |
| 13468 | ' traceback are passed as arguments to "__exit__()". Otherwise, ' |
| 13469 | 'three\n' |
| 13470 | ' "None" arguments are supplied.\n' |
| 13471 | '\n' |
| 13472 | ' If the suite was exited due to an exception, and the return ' |
| 13473 | 'value\n' |
| 13474 | ' from the "__exit__()" method was false, the exception is ' |
| 13475 | 'reraised.\n' |
| 13476 | ' If the return value was true, the exception is suppressed, and\n' |
| 13477 | ' execution continues with the statement following the "with"\n' |
| 13478 | ' statement.\n' |
| 13479 | '\n' |
| 13480 | ' If the suite was exited for any reason other than an exception, ' |
| 13481 | 'the\n' |
| 13482 | ' return value from "__exit__()" is ignored, and execution ' |
| 13483 | 'proceeds\n' |
| 13484 | ' at the normal location for the kind of exit that was taken.\n' |
| 13485 | '\n' |
| 13486 | 'With more than one item, the context managers are processed as if\n' |
| 13487 | 'multiple "with" statements were nested:\n' |
| 13488 | '\n' |
| 13489 | ' with A() as a, B() as b:\n' |
| 13490 | ' suite\n' |
| 13491 | '\n' |
| 13492 | 'is equivalent to\n' |
| 13493 | '\n' |
| 13494 | ' with A() as a:\n' |
| 13495 | ' with B() as b:\n' |
| 13496 | ' suite\n' |
| 13497 | '\n' |
| 13498 | 'Note: In Python 2.5, the "with" statement is only allowed when the\n' |
| 13499 | ' "with_statement" feature has been enabled. It is always enabled ' |
| 13500 | 'in\n' |
| 13501 | ' Python 2.6.\n' |
| 13502 | '\n' |
| 13503 | 'Changed in version 2.7: Support for multiple context expressions.\n' |
| 13504 | '\n' |
| 13505 | 'See also:\n' |
| 13506 | '\n' |
| 13507 | ' **PEP 343** - The "with" statement\n' |
| 13508 | ' The specification, background, and examples for the Python ' |
| 13509 | '"with"\n' |
| 13510 | ' statement.\n', |
| 13511 | 'yield': '\n' |
| 13512 | 'The "yield" statement\n' |
| 13513 | '*********************\n' |
| 13514 | '\n' |
| 13515 | ' yield_stmt ::= yield_expression\n' |
| 13516 | '\n' |
| 13517 | 'The "yield" statement is only used when defining a generator ' |
| 13518 | 'function,\n' |
| 13519 | 'and is only used in the body of the generator function. Using a\n' |
| 13520 | '"yield" statement in a function definition is sufficient to cause ' |
| 13521 | 'that\n' |
| 13522 | 'definition to create a generator function instead of a normal\n' |
| 13523 | 'function.\n' |
| 13524 | '\n' |
| 13525 | 'When a generator function is called, it returns an iterator known ' |
| 13526 | 'as a\n' |
| 13527 | 'generator iterator, or more commonly, a generator. The body of ' |
| 13528 | 'the\n' |
| 13529 | "generator function is executed by calling the generator's " |
| 13530 | '"next()"\n' |
| 13531 | 'method repeatedly until it raises an exception.\n' |
| 13532 | '\n' |
| 13533 | 'When a "yield" statement is executed, the state of the generator ' |
| 13534 | 'is\n' |
| 13535 | 'frozen and the value of "expression_list" is returned to ' |
| 13536 | '"next()"\'s\n' |
| 13537 | 'caller. By "frozen" we mean that all local state is retained,\n' |
| 13538 | 'including the current bindings of local variables, the instruction\n' |
| 13539 | 'pointer, and the internal evaluation stack: enough information is\n' |
| 13540 | 'saved so that the next time "next()" is invoked, the function can\n' |
| 13541 | 'proceed exactly as if the "yield" statement were just another ' |
| 13542 | 'external\n' |
| 13543 | 'call.\n' |
| 13544 | '\n' |
| 13545 | 'As of Python version 2.5, the "yield" statement is now allowed in ' |
| 13546 | 'the\n' |
| 13547 | '"try" clause of a "try" ... "finally" construct. If the generator ' |
| 13548 | 'is\n' |
| 13549 | 'not resumed before it is finalized (by reaching a zero reference ' |
| 13550 | 'count\n' |
| 13551 | "or by being garbage collected), the generator-iterator's " |
| 13552 | '"close()"\n' |
| 13553 | 'method will be called, allowing any pending "finally" clauses to\n' |
| 13554 | 'execute.\n' |
| 13555 | '\n' |
| 13556 | 'For full details of "yield" semantics, refer to the Yield ' |
| 13557 | 'expressions\n' |
| 13558 | 'section.\n' |
| 13559 | '\n' |
| 13560 | 'Note: In Python 2.2, the "yield" statement was only allowed when ' |
| 13561 | 'the\n' |
| 13562 | ' "generators" feature has been enabled. This "__future__" import\n' |
| 13563 | ' statement was used to enable the feature:\n' |
| 13564 | '\n' |
| 13565 | ' from __future__ import generators\n' |
| 13566 | '\n' |
| 13567 | 'See also:\n' |
| 13568 | '\n' |
| 13569 | ' **PEP 255** - Simple Generators\n' |
| 13570 | ' The proposal for adding generators and the "yield" statement ' |
| 13571 | 'to\n' |
| 13572 | ' Python.\n' |
| 13573 | '\n' |
| 13574 | ' **PEP 342** - Coroutines via Enhanced Generators\n' |
| 13575 | ' The proposal that, among other generator enhancements, ' |
| 13576 | 'proposed\n' |
| 13577 | ' allowing "yield" to appear inside a "try" ... "finally" ' |
| 13578 | 'block.\n'} |