Changeset 388 for python/vendor/current/Doc/reference
- Timestamp:
- Mar 19, 2014, 11:11:30 AM (11 years ago)
- Location:
- python/vendor/current/Doc/reference
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
python/vendor/current/Doc/reference/compound_stmts.rst
r2 r388 239 239 if the resulting object is "compatible" with the exception. An object is 240 240 compatible with an exception if it is the class or a base class of the exception 241 object, a tuple containing an item compatible with the exception, or, in the 242 (deprecated) case of string exceptions, is the raised string itself (note that 243 the object identities must match, i.e. it must be the same string object, not 244 just a string with the same value). 241 object, or a tuple containing an item compatible with the exception. 245 242 246 243 If no except clause matches the exception, the search for an exception handler … … 298 295 :keyword:`finally` clause. If the :keyword:`finally` clause raises another 299 296 exception or executes a :keyword:`return` or :keyword:`break` statement, the 300 saved exception is lost. The exception information is not available to the 301 program during execution of the :keyword:`finally` clause. 297 saved exception is discarded:: 298 299 def f(): 300 try: 301 1/0 302 finally: 303 return 42 304 305 >>> f() 306 42 307 308 The exception information is not available to the program during execution of 309 the :keyword:`finally` clause. 302 310 303 311 .. index:: … … 334 342 335 343 .. productionlist:: 336 with_stmt: "with" `expression` ["as" `target`] ":" `suite` 337 338 The execution of the :keyword:`with` statement proceeds as follows: 339 340 #. The context expression is evaluated to obtain a context manager. 344 with_stmt: "with" with_item ("," with_item)* ":" `suite` 345 with_item: `expression` ["as" `target`] 346 347 The execution of the :keyword:`with` statement with one "item" proceeds as follows: 348 349 #. The context expression (the expression given in the :token:`with_item`) is 350 evaluated to obtain a context manager. 351 352 #. The context manager's :meth:`__exit__` is loaded for later use. 341 353 342 354 #. The context manager's :meth:`__enter__` method is invoked. … … 350 362 returns without an error, then :meth:`__exit__` will always be called. Thus, if 351 363 an error occurs during the assignment to the target list, it will be treated the 352 same as an error occurring within the suite would be. See step 5below.364 same as an error occurring within the suite would be. See step 6 below. 353 365 354 366 #. The suite is executed. … … 368 380 for the kind of exit that was taken. 369 381 382 With more than one item, the context managers are processed as if multiple 383 :keyword:`with` statements were nested:: 384 385 with A() as a, B() as b: 386 suite 387 388 is equivalent to :: 389 390 with A() as a: 391 with B() as b: 392 suite 393 370 394 .. note:: 371 395 … … 374 398 Python 2.6. 375 399 400 .. versionchanged:: 2.7 401 Support for multiple context expressions. 402 376 403 .. seealso:: 377 404 … … 380 407 statement. 381 408 409 410 .. index:: 411 single: parameter; function definition 382 412 383 413 .. _function: … … 405 435 dotted_name: `identifier` ("." `identifier`)* 406 436 parameter_list: (`defparameter` ",")* 407 : ( "*" `identifier` [ ,"**" `identifier`]437 : ( "*" `identifier` ["," "**" `identifier`] 408 438 : | "**" `identifier` 409 439 : | `defparameter` [","] ) … … 441 471 func = f1(arg)(f2(func)) 442 472 443 .. index:: triple: default; parameter; value 444 445 When one or more top-level parameters have the form *parameter* ``=`` 446 *expression*, the function is said to have "default parameter values." For a 447 parameter with a default value, the corresponding argument may be omitted from a 448 call, in which case the parameter's default value is substituted. If a 473 .. index:: 474 triple: default; parameter; value 475 single: argument; function definition 476 477 When one or more top-level :term:`parameters <parameter>` have the form 478 *parameter* ``=`` *expression*, the function is said to have "default parameter 479 values." For a parameter with a default value, the corresponding 480 :term:`argument` may be omitted from a call, in which 481 case the parameter's default value is substituted. If a 449 482 parameter has a default value, all following parameters must also have a default 450 483 value --- this is a syntactic restriction that is not expressed by the grammar. … … 452 485 **Default parameter values are evaluated when the function definition is 453 486 executed.** This means that the expression is evaluated once, when the function 454 is defined, and that th atsame "pre-computed" value is used for each call. This487 is defined, and that the same "pre-computed" value is used for each call. This 455 488 is especially important to understand when a default parameter is a mutable 456 489 object, such as a list or a dictionary: if the function modifies the object … … 477 510 receiving any excess keyword arguments, defaulting to a new empty dictionary. 478 511 479 .. index:: pair: lambda; form512 .. index:: pair: lambda; expression 480 513 481 514 It is also possible to create anonymous functions (functions not bound to a 482 name), for immediate use in expressions. This uses lambda forms, described in483 section :ref:`lambda`. Note that the lambda formis merely a shorthand for a515 name), for immediate use in expressions. This uses lambda expressions, described in 516 section :ref:`lambda`. Note that the lambda expression is merely a shorthand for a 484 517 simplified function definition; a function defined in a ":keyword:`def`" 485 518 statement can be passed around or assigned to another name just like a function 486 defined by a lambda form. The ":keyword:`def`" form is actually more powerful519 defined by a lambda expression. The ":keyword:`def`" form is actually more powerful 487 520 since it allows the execution of multiple statements. 488 521 … … 544 577 .. rubric:: Footnotes 545 578 546 .. [#] The exception is propagated to the invocation stack only if there is no 547 :keyword:`finally` clause that negates the exception. 579 .. [#] The exception is propagated to the invocation stack unless 580 there is a :keyword:`finally` clause which happens to raise another 581 exception. That new exception causes the old one to be lost. 548 582 549 583 .. [#] Currently, control "flows off the end" except in the case of an exception or the -
python/vendor/current/Doc/reference/datamodel.rst
r2 r388 67 67 module for information on controlling the collection of cyclic garbage. 68 68 Other implementations act differently and CPython may change. 69 Do not depend on immediate finalization of objects when they become 70 unreachable (ex: always close files). 69 71 70 72 Note that the use of the implementation's tracing or debugging facilities may … … 410 412 These represent a mutable set. They are created by the built-in :func:`set` 411 413 constructor and can be modified afterwards by several methods, such as 412 :meth:` add`.414 :meth:`~set.add`. 413 415 414 416 Frozen sets … … 479 481 480 482 Special attributes: 483 484 .. tabularcolumns:: |l|L|l| 481 485 482 486 +-----------------------+-------------------------------+-----------+ … … 572 576 573 577 .. versionchanged:: 2.6 574 For 3.0forward-compatibility, :attr:`im_func` is also available as578 For Python 3 forward-compatibility, :attr:`im_func` is also available as 575 579 :attr:`__func__`, and :attr:`im_self` as :attr:`__self__`. 576 580 … … 620 624 621 625 When a user-defined method object is created by retrieving a class method object 622 from a class or instance, its :attr:`im_self` attribute is the class itself (the 623 same as the :attr:`im_class` attribute), and its :attr:`im_func` attribute is 624 the function object underlying the class method. 626 from a class or instance, its :attr:`im_self` attribute is the class itself, and 627 its :attr:`im_func` attribute is the function object underlying the class method. 625 628 626 629 When an unbound user-defined method object is called, the underlying function … … 659 662 function`. Such a function, when called, always returns an iterator object 660 663 which can be used to execute the body of the function: calling the iterator's 661 :meth:`next` method will cause the function to execute until it provides a value 664 :meth:`~iterator.next` method will cause the function to execute until 665 it provides a value 662 666 using the :keyword:`yield` statement. When the function executes a 663 667 :keyword:`return` statement or falls off the end, a :exc:`StopIteration` … … 690 694 a built-in method is ``alist.append()``, assuming *alist* is a list object. In 691 695 this case, the special read-only attribute :attr:`__self__` is set to the object 692 denoted by * list*.696 denoted by *alist*. 693 697 694 698 Class Types … … 739 743 Special read-only attribute: :attr:`__dict__` is the module's namespace as a 740 744 dictionary object. 745 746 .. impl-detail:: 747 748 Because of the way CPython clears module dictionaries, the module 749 dictionary will be cleared when the module falls out of scope even if the 750 dictionary still has live references. To avoid this, copy the dictionary 751 or keep the module around while using its dictionary directly. 741 752 742 753 .. index:: … … 787 798 transformed into an unbound user-defined method object whose :attr:`im_class` 788 799 attribute is :class:`C`. When it would yield a class method object, it is 789 transformed into a bound user-defined method object whose :attr:`im_class`790 and :attr:`im_self` attributes are both:class:`C`. When it would yield a800 transformed into a bound user-defined method object whose 801 :attr:`im_self` attribute is :class:`C`. When it would yield a 791 802 static method object, it is transformed into the object wrapped by the static 792 803 method object. See section :ref:`descriptors` for another way in which … … 812 823 Special attributes: :attr:`__name__` is the class name; :attr:`__module__` is 813 824 the module name in which the class was defined; :attr:`__dict__` is the 814 dictionary containing the class's namespace; :attr:` __bases__` is a tuple815 (possibly empty or a singleton) containing the base classes, in the order of816 their occurrence in the base class list; :attr:`__doc__` is the class's817 documentation string, or None if undefined.825 dictionary containing the class's namespace; :attr:`~class.__bases__` is a 826 tuple (possibly empty or a singleton) containing the base classes, in the 827 order of their occurrence in the base class list; :attr:`__doc__` is the 828 class's documentation string, or None if undefined. 818 829 819 830 Class instances … … 860 871 single: __class__ (instance attribute) 861 872 862 Special attributes: :attr:` __dict__` is the attribute dictionary;863 :attr:` __class__` is the instance's class.873 Special attributes: :attr:`~object.__dict__` is the attribute dictionary; 874 :attr:`~instance.__class__` is the instance's class. 864 875 865 876 Files … … 908 919 objects, code objects are immutable and contain no references (directly or 909 920 indirectly) to mutable objects. 921 922 .. index:: 923 single: co_argcount (code object attribute) 924 single: co_code (code object attribute) 925 single: co_consts (code object attribute) 926 single: co_filename (code object attribute) 927 single: co_firstlineno (code object attribute) 928 single: co_flags (code object attribute) 929 single: co_lnotab (code object attribute) 930 single: co_name (code object attribute) 931 single: co_names (code object attribute) 932 single: co_nlocals (code object attribute) 933 single: co_stacksize (code object attribute) 934 single: co_varnames (code object attribute) 935 single: co_cellvars (code object attribute) 936 single: co_freevars (code object attribute) 910 937 911 938 Special read-only attributes: :attr:`co_name` gives the function name; … … 926 953 :attr:`co_flags` is an integer encoding a number of flags for the interpreter. 927 954 928 .. index::929 single: co_argcount (code object attribute)930 single: co_code (code object attribute)931 single: co_consts (code object attribute)932 single: co_filename (code object attribute)933 single: co_firstlineno (code object attribute)934 single: co_flags (code object attribute)935 single: co_lnotab (code object attribute)936 single: co_name (code object attribute)937 single: co_names (code object attribute)938 single: co_nlocals (code object attribute)939 single: co_stacksize (code object attribute)940 single: co_varnames (code object attribute)941 single: co_cellvars (code object attribute)942 single: co_freevars (code object attribute)943 944 955 .. index:: object: generator 945 956 … … 1060 1071 single: step (slice object attribute) 1061 1072 1062 Special read-only attributes: :attr:` start` is the lower bound; :attr:`stop` is1063 the upper bound; :attr:`step` is the step value; each is ``None`` if omitted.1064 These attributes can have any type.1073 Special read-only attributes: :attr:`~slice.start` is the lower bound; 1074 :attr:`~slice.stop` is the upper bound; :attr:`~slice.step` is the step 1075 value; each is ``None`` if omitted. These attributes can have any type. 1065 1076 1066 1077 Slice objects support one method: … … 1141 1152 single: class; old-style 1142 1153 1143 Old-style classes are removed in Python 3 .0, leaving only the semantics of1154 Old-style classes are removed in Python 3, leaving only the semantics of 1144 1155 new-style classes. 1145 1156 … … 1169 1180 object being modelled. For example, some sequences may work well with retrieval 1170 1181 of individual elements, but extracting a slice may not make sense. (One example 1171 of this is the :class:`NodeList` interface in the W3C's Document Object Model.) 1182 of this is the :class:`~xml.dom.NodeList` interface in the W3C's Document 1183 Object Model.) 1172 1184 1173 1185 … … 1274 1286 called. 1275 1287 1288 See also the :option:`-R` command-line option. 1289 1276 1290 1277 1291 .. method:: object.__repr__(self) … … 1354 1368 1355 1369 To automatically generate ordering operations from a single root operation, 1356 see the `Total Ordering recipe in the ASPN cookbook 1357 <http://code.activestate.com/recipes/576529/>`_\. 1370 see :func:`functools.total_ordering`. 1358 1371 1359 1372 .. method:: object.__cmp__(self, other) … … 1532 1545 1533 1546 The following methods only apply when an instance of the class containing the 1534 method (a so-called *descriptor* class) appears in the class dictionary of1535 another new-style class, known as the *owner* class. In the examples below, "the 1536 attribute" refers to the attribute whose name is the key of the property in the 1537 owner class' ``__dict__``. Descriptors can only be implemented as new-style 1538 class es themselves.1547 method (a so-called *descriptor* class) appears in an *owner* class (the 1548 descriptor must be in either the owner's class dictionary or in the class 1549 dictionary for one of its parents). In the examples below, "the attribute" 1550 refers to the attribute whose name is the key of the property in the owner 1551 class' :attr:`__dict__`. 1539 1552 1540 1553 … … 1601 1614 obj).m()`` searches ``obj.__class__.__mro__`` for the base class ``A`` 1602 1615 immediately preceding ``B`` and then invokes the descriptor with the call: 1603 ``A.__dict__['m'].__get__(obj, A)``.1616 ``A.__dict__['m'].__get__(obj, obj.__class__)``. 1604 1617 1605 1618 For instance bindings, the precedence of descriptor invocation depends on the 1606 which descriptor methods are defined. Normally, data descriptors define both 1607 :meth:`__get__` and :meth:`__set__`, while non-data descriptors have just the 1608 :meth:`__get__` method. Data descriptors always override a redefinition in an 1619 which descriptor methods are defined. A descriptor can define any combination 1620 of :meth:`__get__`, :meth:`__set__` and :meth:`__delete__`. If it does not 1621 define :meth:`__get__`, then accessing the attribute will return the descriptor 1622 object itself unless there is a value in the object's instance dictionary. If 1623 the descriptor defines :meth:`__set__` and/or :meth:`__delete__`, it is a data 1624 descriptor; if it defines neither, it is a non-data descriptor. Normally, data 1625 descriptors define both :meth:`__get__` and :meth:`__set__`, while non-data 1626 descriptors have just the :meth:`__get__` method. Data descriptors with 1627 :meth:`__set__` and :meth:`__get__` defined always override a redefinition in an 1609 1628 instance dictionary. In contrast, non-data descriptors can be overridden by 1610 instances. [#]_1629 instances. 1611 1630 1612 1631 Python methods (including :func:`staticmethod` and :func:`classmethod`) are … … 1756 1775 property creation, proxies, frameworks, and automatic resource 1757 1776 locking/synchronization. 1777 1778 1779 Customizing instance and subclass checks 1780 ---------------------------------------- 1781 1782 .. versionadded:: 2.6 1783 1784 The following methods are used to override the default behavior of the 1785 :func:`isinstance` and :func:`issubclass` built-in functions. 1786 1787 In particular, the metaclass :class:`abc.ABCMeta` implements these methods in 1788 order to allow the addition of Abstract Base Classes (ABCs) as "virtual base 1789 classes" to any class or type (including built-in types), including other 1790 ABCs. 1791 1792 .. method:: class.__instancecheck__(self, instance) 1793 1794 Return true if *instance* should be considered a (direct or indirect) 1795 instance of *class*. If defined, called to implement ``isinstance(instance, 1796 class)``. 1797 1798 1799 .. method:: class.__subclasscheck__(self, subclass) 1800 1801 Return true if *subclass* should be considered a (direct or indirect) 1802 subclass of *class*. If defined, called to implement ``issubclass(subclass, 1803 class)``. 1804 1805 1806 Note that these methods are looked up on the type (metaclass) of a class. They 1807 cannot be defined as class methods in the actual class. This is consistent with 1808 the lookup of special methods that are called on instances, only in this 1809 case the instance is itself a class. 1810 1811 .. seealso:: 1812 1813 :pep:`3119` - Introducing Abstract Base Classes 1814 Includes the specification for customizing :func:`isinstance` and 1815 :func:`issubclass` behavior through :meth:`~class.__instancecheck__` and 1816 :meth:`~class.__subclasscheck__`, with motivation for this functionality 1817 in the context of adding Abstract Base Classes (see the :mod:`abc` 1818 module) to the language. 1758 1819 1759 1820 … … 1788 1849 :meth:`values`, :meth:`items`, :meth:`has_key`, :meth:`get`, :meth:`clear`, 1789 1850 :meth:`setdefault`, :meth:`iterkeys`, :meth:`itervalues`, :meth:`iteritems`, 1790 :meth:`pop`, :meth:`popitem`, :meth:` copy`, and :meth:`update` behaving similar1851 :meth:`pop`, :meth:`popitem`, :meth:`!copy`, and :meth:`update` behaving similar 1791 1852 to those for Python's standard dictionary objects. The :mod:`UserDict` module 1792 1853 provides a :class:`DictMixin` class to help create those methods from a base set … … 2178 2239 evolved, the coercion rules have become hard to document precisely; documenting 2179 2240 what one version of one particular implementation does is undesirable. Instead, 2180 here are some informal guidelines regarding coercion. In Python 3 .0, coercion2241 here are some informal guidelines regarding coercion. In Python 3, coercion 2181 2242 will not be supported. 2182 2243 … … 2253 2314 * 2254 2315 2255 In ``x * y``, if one opera toris a sequence that implements sequence2316 In ``x * y``, if one operand is a sequence that implements sequence 2256 2317 repetition, and the other is an integer (:class:`int` or :class:`long`), 2257 2318 sequence repetition is invoked. … … 2266 2327 2267 2328 In the current implementation, the built-in numeric types :class:`int`, 2268 :class:`long` and :class:`float` do not use coercion; the type :class:`complex` 2269 however does use coercion for binary operators and rich comparisons, despite 2270 the above rules. The difference can become apparent when subclassing these 2271 types. Over time, the type :class:`complex` may be fixed to avoid coercion. 2329 :class:`long`, :class:`float`, and :class:`complex` do not use coercion. 2272 2330 All these types implement a :meth:`__coerce__` method, for use by the built-in 2273 2331 :func:`coerce` function. 2332 2333 .. versionchanged:: 2.7 2334 2335 The complex type no longer makes implicit calls to the :meth:`__coerce__` 2336 method for mixed-type binary arithmetic operations. 2274 2337 2275 2338 … … 2435 2498 lead to some very strange behaviour if it is handled incorrectly. 2436 2499 2437 .. [#] A descriptor can define any combination of :meth:`__get__`,2438 :meth:`__set__` and :meth:`__delete__`. If it does not define :meth:`__get__`,2439 then accessing the attribute even on an instance will return the descriptor2440 object itself. If the descriptor defines :meth:`__set__` and/or2441 :meth:`__delete__`, it is a data descriptor; if it defines neither, it is a2442 non-data descriptor.2443 2444 2500 .. [#] For operands of the same type, it is assumed that if the non-reflected method 2445 2501 (such as :meth:`__add__`) fails the operation is not supported, which is why the -
python/vendor/current/Doc/reference/executionmodel.rst
r2 r388 120 120 .. index:: pair: restricted; execution 121 121 122 The built -innamespace associated with the execution of a code block is actually122 The builtins namespace associated with the execution of a code block is actually 123 123 found by looking up the name ``__builtins__`` in its global namespace; this 124 124 should be a dictionary or a module (in the latter case the module's dictionary … … 132 132 133 133 Users should not touch ``__builtins__``; it is strictly an implementation 134 detail. Users wanting to override values in the built -innamespace should134 detail. Users wanting to override values in the builtins namespace should 135 135 :keyword:`import` the :mod:`__builtin__` (no 's') module and modify its 136 136 attributes appropriately. … … 141 141 imported. The main module for a script is always called :mod:`__main__`. 142 142 143 The global statement has the same scope as a name binding operation in the same144 block. If the nearest enclosing scope for a free variable contains a global 145 statement, the free variable is treated as a global.143 The :keyword:`global` statement has the same scope as a name binding operation 144 in the same block. If the nearest enclosing scope for a free variable contains 145 a global statement, the free variable is treated as a global. 146 146 147 147 A class definition is an executable statement that may use and define names. -
python/vendor/current/Doc/reference/expressions.rst
r2 r388 66 66 atom: `identifier` | `literal` | `enclosure` 67 67 enclosure: `parenth_form` | `list_display` 68 : | `generator_expression` | `dict_display` 68 : | `generator_expression` | `dict_display` | `set_display` 69 69 : | `string_conversion` | `yield_atom` 70 70 … … 97 97 or more underscores, it is considered a :dfn:`private name` of that class. 98 98 Private names are transformed to a longer form before code is generated for 99 them. The transformation inserts the class name in front of the name, with 100 leading underscores removed, and a single underscore inserted in front of the 101 class name. For example, the identifier ``__spam`` occurring in a class named 102 ``Ham`` will be transformed to ``_Ham__spam``. This transformation is 103 independent of the syntactical context in which the identifier is used. If the 104 transformed name is extremely long (longer than 255 characters), implementation 105 defined truncation may happen. If the class name consists only of underscores, 106 no transformation is done. 99 them. The transformation inserts the class name, with leading underscores 100 removed and a single underscore inserted, in front of the name. For example, 101 the identifier ``__spam`` occurring in a class named ``Ham`` will be transformed 102 to ``_Ham__spam``. This transformation is independent of the syntactical 103 context in which the identifier is used. If the transformed name is extremely 104 long (longer than 255 characters), implementation defined truncation may happen. 105 If the class name consists only of underscores, no transformation is done. 107 106 108 107 … … 186 185 list_for: "for" `target_list` "in" `old_expression_list` [`list_iter`] 187 186 old_expression_list: `old_expression` [("," `old_expression`)+ [","]] 187 old_expression: `or_test` | `old_lambda_expr` 188 188 list_iter: `list_for` | `list_if` 189 189 list_if: "if" `old_expression` [`list_iter`] … … 206 206 207 207 208 .. _comprehensions: 209 210 Displays for sets and dictionaries 211 ---------------------------------- 212 213 For constructing a set or a dictionary Python provides special syntax 214 called "displays", each of them in two flavors: 215 216 * either the container contents are listed explicitly, or 217 218 * they are computed via a set of looping and filtering instructions, called a 219 :dfn:`comprehension`. 220 221 Common syntax elements for comprehensions are: 222 223 .. productionlist:: 224 comprehension: `expression` `comp_for` 225 comp_for: "for" `target_list` "in" `or_test` [`comp_iter`] 226 comp_iter: `comp_for` | `comp_if` 227 comp_if: "if" `expression_nocond` [`comp_iter`] 228 229 The comprehension consists of a single expression followed by at least one 230 :keyword:`for` clause and zero or more :keyword:`for` or :keyword:`if` clauses. 231 In this case, the elements of the new container are those that would be produced 232 by considering each of the :keyword:`for` or :keyword:`if` clauses a block, 233 nesting from left to right, and evaluating the expression to produce an element 234 each time the innermost block is reached. 235 236 Note that the comprehension is executed in a separate scope, so names assigned 237 to in the target list don't "leak" in the enclosing scope. 238 239 208 240 .. _genexpr: 209 241 … … 212 244 213 245 .. index:: pair: generator; expression 246 object: generator 214 247 215 248 A generator expression is a compact generator notation in parentheses: 216 249 217 250 .. productionlist:: 218 generator_expression: "(" `expression` `genexpr_for` ")" 219 genexpr_for: "for" `target_list` "in" `or_test` [`genexpr_iter`] 220 genexpr_iter: `genexpr_for` | `genexpr_if` 221 genexpr_if: "if" `old_expression` [`genexpr_iter`] 222 223 .. index:: object: generator 224 225 A generator expression yields a new generator object. It consists of a single 226 expression followed by at least one :keyword:`for` clause and zero or more 227 :keyword:`for` or :keyword:`if` clauses. The iterating values of the new 228 generator are those that would be produced by considering each of the 229 :keyword:`for` or :keyword:`if` clauses a block, nesting from left to right, and 230 evaluating the expression to yield a value that is reached the innermost block 231 for each iteration. 232 233 Variables used in the generator expression are evaluated lazily in a separate 234 scope when the :meth:`next` method is called for the generator object (in the 235 same fashion as for normal generators). However, the :keyword:`in` expression 236 of the leftmost :keyword:`for` clause is immediately evaluated in the current 237 scope so that an error produced by it can be seen before any other possible 251 generator_expression: "(" `expression` `comp_for` ")" 252 253 A generator expression yields a new generator object. Its syntax is the same as 254 for comprehensions, except that it is enclosed in parentheses instead of 255 brackets or curly braces. 256 257 Variables used in the generator expression are evaluated lazily when the 258 :meth:`__next__` method is called for generator object (in the same fashion as 259 normal generators). However, the leftmost :keyword:`for` clause is immediately 260 evaluated, so that an error produced by it can be seen before any other possible 238 261 error in the code that handles the generator expression. Subsequent 239 :keyword:`for` and :keyword:`if` clauses cannot be evaluated immediately since240 the y may depend on the previous :keyword:`for` loop. For example:241 ``(x*y for x in range(10) for yin bar(x))``.242 243 The parentheses can be omitted on calls with only one argument. See section262 :keyword:`for` clauses cannot be evaluated immediately since they may depend on 263 the previous :keyword:`for` loop. For example: ``(x*y for x in range(10) for y 264 in bar(x))``. 265 266 The parentheses can be omitted on calls with only one argument. See section 244 267 :ref:`calls` for the detail. 245 246 268 247 269 .. _dict: … … 251 273 252 274 .. index:: pair: dictionary; display 253 254 .. index:: 255 single: key 256 single: datum 257 single: key/datum pair 275 key, datum, key/datum pair 276 object: dictionary 258 277 259 278 A dictionary display is a possibly empty series of key/datum pairs enclosed in … … 261 280 262 281 .. productionlist:: 263 dict_display: "{" [`key_datum_list` ] "}"282 dict_display: "{" [`key_datum_list` | `dict_comprehension`] "}" 264 283 key_datum_list: `key_datum` ("," `key_datum`)* [","] 265 284 key_datum: `expression` ":" `expression` 266 267 .. index:: object: dictionary 285 dict_comprehension: `expression` ":" `expression` `comp_for` 268 286 269 287 A dictionary display yields a new dictionary object. 270 288 271 The key/datum pairs are evaluated from left to right to define the entries of 272 the dictionary: each key object is used as a key into the dictionary to store 273 the corresponding datum. 289 If a comma-separated sequence of key/datum pairs is given, they are evaluated 290 from left to right to define the entries of the dictionary: each key object is 291 used as a key into the dictionary to store the corresponding datum. This means 292 that you can specify the same key multiple times in the key/datum list, and the 293 final dictionary's value for that key will be the last one given. 294 295 A dict comprehension, in contrast to list and set comprehensions, needs two 296 expressions separated with a colon followed by the usual "for" and "if" clauses. 297 When the comprehension is run, the resulting key and value elements are inserted 298 in the new dictionary in the order they are produced. 274 299 275 300 .. index:: pair: immutable; object 301 hashable 276 302 277 303 Restrictions on the types of the key values are listed earlier in section … … 282 308 283 309 310 .. _set: 311 312 Set displays 313 ------------ 314 315 .. index:: pair: set; display 316 object: set 317 318 A set display is denoted by curly braces and distinguishable from dictionary 319 displays by the lack of colons separating keys and values: 320 321 .. productionlist:: 322 set_display: "{" (`expression_list` | `comprehension`) "}" 323 324 A set display yields a new mutable set object, the contents being specified by 325 either a sequence of expressions or a comprehension. When a comma-separated 326 list of expressions is supplied, its elements are evaluated from left to right 327 and added to the set object. When a comprehension is supplied, the set is 328 constructed from the elements resulting from the comprehension. 329 330 An empty set cannot be constructed with ``{}``; this literal constructs an empty 331 dictionary. 332 333 284 334 .. _string-conversions: 285 335 … … 297 347 298 348 .. productionlist:: 299 string_conversion: " '" `expression_list` "'"349 string_conversion: "`" `expression_list` "`" 300 350 301 351 A string conversion evaluates the contained expression list and converts the … … 367 417 suspended. The only difference is that a generator function cannot control 368 418 where should the execution continue after it yields; the control is always 369 transfer ed to the generator's caller.419 transferred to the generator's caller. 370 420 371 421 .. index:: object: generator 372 422 373 The following generator's methods can be used to control the execution of a 374 generator function: 423 424 Generator-iterator methods 425 ^^^^^^^^^^^^^^^^^^^^^^^^^^ 426 427 This subsection describes the methods of a generator iterator. They can 428 be used to control the execution of a generator function. 429 430 Note that calling any of the generator methods below when the generator 431 is already executing raises a :exc:`ValueError` exception. 375 432 376 433 .. index:: exception: StopIteration 434 .. class:: generator 377 435 378 436 … … 388 446 raised. 389 447 448 .. class:: . 390 449 391 450 .. method:: generator.send(value) … … 604 663 that is an expression is that expression. The conversion of an ellipsis slice 605 664 item is the built-in ``Ellipsis`` object. The conversion of a proper slice is a 606 slice object (see section :ref:`types`) whose :attr:`start`, :attr:`stop` and 607 :attr:`step` attributes are the values of the expressions given as lower bound, 608 upper bound and stride, respectively, substituting ``None`` for missing 609 expressions. 610 665 slice object (see section :ref:`types`) whose :attr:`~slice.start`, 666 :attr:`~slice.stop` and :attr:`~slice.step` attributes are the values of the 667 expressions given as lower bound, upper bound and stride, respectively, 668 substituting ``None`` for missing expressions. 669 670 671 .. index:: 672 object: callable 673 single: call 674 single: argument; call semantics 611 675 612 676 .. _calls: … … 615 679 ----- 616 680 617 .. index:: single: call 618 619 .. index:: object: callable 620 621 A call calls a callable object (e.g., a function) with a possibly empty series 622 of arguments: 681 A call calls a callable object (e.g., a :term:`function`) with a possibly empty 682 series of :term:`arguments <argument>`: 623 683 624 684 .. productionlist:: … … 639 699 does not affect the semantics. 640 700 701 .. index:: 702 single: parameter; call semantics 703 641 704 The primary must evaluate to a callable object (user-defined functions, built-in 642 705 functions, methods of built-in objects, class objects, methods of class … … 644 707 define additional callable object types). All argument expressions are 645 708 evaluated before the call is attempted. Please refer to section :ref:`function` 646 for the syntax of formal parameterlists.709 for the syntax of formal :term:`parameter` lists. 647 710 648 711 If keyword arguments are present, they are first converted to positional … … 669 732 do not have names, even if they are 'named' for the purpose of documentation, 670 733 and which therefore cannot be supplied by keyword. In CPython, this is the 671 case for functions implemented in C that use :c func:`PyArg_ParseTuple` to734 case for functions implemented in C that use :c:func:`PyArg_ParseTuple` to 672 735 parse their arguments. 673 736 … … 685 748 there were no excess keyword arguments. 686 749 750 .. index:: 751 single: *; in function calls 752 687 753 If the syntax ``*expression`` appears in the function call, ``expression`` must 688 evaluate to a sequence. Elements from this sequence are treated as if they were689 additional positional arguments; if there are positional arguments *x1*,..., 690 *x N*, and ``expression`` evaluates to a sequence *y1*, ..., *yM*, thisis691 equivalent to a call with M+N positional arguments *x1*, ..., *xN*, *y1*, ...,692 *yM*.754 evaluate to an iterable. Elements from this iterable are treated as if they 755 were additional positional arguments; if there are positional arguments 756 *x1*, ..., *xN*, and ``expression`` evaluates to a sequence *y1*, ..., *yM*, this 757 is equivalent to a call with M+N positional arguments *x1*, ..., *xN*, *y1*, 758 ..., *yM*. 693 759 694 760 A consequence of this is that although the ``*expression`` syntax may appear … … 711 777 used in the same call, so in practice this confusion does not arise. 712 778 779 .. index:: 780 single: **; in function calls 781 713 782 If the syntax ``**expression`` appears in the function call, ``expression`` must 714 783 evaluate to a mapping, the contents of which are treated as additional keyword … … 950 1019 counts raise a :exc:`ValueError` exception. 951 1020 1021 .. note:: 1022 1023 In the current implementation, the right-hand operand is required 1024 to be at most :attr:`sys.maxsize`. If the right-hand operand is larger than 1025 :attr:`sys.maxsize` an :exc:`OverflowError` exception is raised. 952 1026 953 1027 .. _bitwise: … … 987 1061 .. _comparisons: 988 1062 .. _is: 989 .. _is not:1063 .. _is not: 990 1064 .. _in: 991 .. _not in:1065 .. _not in: 992 1066 993 1067 Comparisons … … 1137 1211 pair: Boolean; operation 1138 1212 1139 Boolean operations have the lowest priority of all Python operations: 1140 1141 .. productionlist:: 1142 expression: `conditional_expression` | `lambda_form` 1143 old_expression: `or_test` | `old_lambda_form` 1144 conditional_expression: `or_test` ["if" `or_test` "else" `expression`] 1213 .. productionlist:: 1145 1214 or_test: `and_test` | `or_test` "or" `and_test` 1146 1215 and_test: `not_test` | `and_test` "and" `not_test` … … 1158 1227 The operator :keyword:`not` yields ``True`` if its argument is false, ``False`` 1159 1228 otherwise. 1160 1161 The expression ``x if C else y`` first evaluates *C* (*not* *x*); if *C* is1162 true, *x* is evaluated and its value is returned; otherwise, *y* is evaluated1163 and its value is returned.1164 1165 .. versionadded:: 2.51166 1229 1167 1230 .. index:: operator: and … … 1184 1247 1185 1248 1249 Conditional Expressions 1250 ======================= 1251 1252 .. versionadded:: 2.5 1253 1254 .. index:: 1255 pair: conditional; expression 1256 pair: ternary; operator 1257 1258 .. productionlist:: 1259 conditional_expression: `or_test` ["if" `or_test` "else" `expression`] 1260 expression: `conditional_expression` | `lambda_expr` 1261 1262 Conditional expressions (sometimes called a "ternary operator") have the lowest 1263 priority of all Python operations. 1264 1265 The expression ``x if C else y`` first evaluates the condition, *C* (*not* *x*); 1266 if *C* is true, *x* is evaluated and its value is returned; otherwise, *y* is 1267 evaluated and its value is returned. 1268 1269 See :pep:`308` for more details about conditional expressions. 1270 1271 1186 1272 .. _lambdas: 1187 1273 .. _lambda: … … 1192 1278 .. index:: 1193 1279 pair: lambda; expression 1194 pair: lambda; form1195 1280 pair: anonymous; function 1196 1281 1197 1282 .. productionlist:: 1198 lambda_ form: "lambda" [`parameter_list`]: `expression`1199 old_lambda_ form: "lambda" [`parameter_list`]: `old_expression`1200 1201 Lambda forms (lambda expressions) have the same syntactic position as1283 lambda_expr: "lambda" [`parameter_list`]: `expression` 1284 old_lambda_expr: "lambda" [`parameter_list`]: `old_expression` 1285 1286 Lambda expressions (sometimes called lambda forms) have the same syntactic position as 1202 1287 expressions. They are a shorthand to create anonymous functions; the expression 1203 1288 ``lambda arguments: expression`` yields a function object. The unnamed object … … 1208 1293 1209 1294 See section :ref:`function` for the syntax of parameter lists. Note that 1210 functions created with lambda forms cannot contain statements.1295 functions created with lambda expressions cannot contain statements. 1211 1296 1212 1297 … … 1259 1344 .. _operator-summary: 1260 1345 1261 Summary 1262 ======= 1346 Operator precedence 1347 =================== 1263 1348 1264 1349 .. index:: pair: operator; precedence … … 1277 1362 | :keyword:`lambda` | Lambda expression | 1278 1363 +-----------------------------------------------+-------------------------------------+ 1364 | :keyword:`if` -- :keyword:`else` | Conditional expression | 1365 +-----------------------------------------------+-------------------------------------+ 1279 1366 | :keyword:`or` | Boolean OR | 1280 1367 +-----------------------------------------------+-------------------------------------+ 1281 1368 | :keyword:`and` | Boolean AND | 1282 1369 +-----------------------------------------------+-------------------------------------+ 1283 | :keyword:`not` *x*| Boolean NOT |1284 +-----------------------------------------------+-------------------------------------+ 1285 | :keyword:`in`, :keyword:`not ` :keyword:`in`,| Comparisons, including membership |1286 | :keyword:`is`, :keyword:`is not`, ``<``, | tests and identity tests ,|1370 | :keyword:`not` ``x`` | Boolean NOT | 1371 +-----------------------------------------------+-------------------------------------+ 1372 | :keyword:`in`, :keyword:`not in`, | Comparisons, including membership | 1373 | :keyword:`is`, :keyword:`is not`, ``<``, | tests and identity tests | 1287 1374 | ``<=``, ``>``, ``>=``, ``<>``, ``!=``, ``==`` | | 1288 1375 +-----------------------------------------------+-------------------------------------+ … … 1298 1385 +-----------------------------------------------+-------------------------------------+ 1299 1386 | ``*``, ``/``, ``//``, ``%`` | Multiplication, division, remainder | 1387 | | [#]_ | 1300 1388 +-----------------------------------------------+-------------------------------------+ 1301 1389 | ``+x``, ``-x``, ``~x`` | Positive, negative, bitwise NOT | … … 1308 1396 | ``(expressions...)``, | Binding or tuple display, | 1309 1397 | ``[expressions...]``, | list display, | 1310 | ``{key: datum...}``,| dictionary display, |1398 | ``{key: value...}``, | dictionary display, | 1311 1399 | ```expressions...``` | string conversion | 1312 1400 +-----------------------------------------------+-------------------------------------+ … … 1316 1404 .. [#] In Python 2.3 and later releases, a list comprehension "leaks" the control 1317 1405 variables of each ``for`` it contains into the containing scope. However, this 1318 behavior is deprecated, and relying on it will not work in Python 3. 01406 behavior is deprecated, and relying on it will not work in Python 3. 1319 1407 1320 1408 .. [#] While ``abs(x%y) < abs(y)`` is true mathematically, for floats it may not be … … 1322 1410 a Python float is an IEEE 754 double-precision number, in order that ``-1e-100 % 1323 1411 1e100`` have the same sign as ``1e100``, the computed result is ``-1e-100 + 1324 1e100``, which is numerically exactly equal to ``1e100``. Function :func:`fmod`1325 in the :mod:`math` modulereturns a result whose sign matches the sign of the1412 1e100``, which is numerically exactly equal to ``1e100``. The function 1413 :func:`math.fmod` returns a result whose sign matches the sign of the 1326 1414 first argument instead, and so returns ``-1e-100`` in this case. Which approach 1327 1415 is more appropriate depends on the application. … … 1336 1424 strings ``u"\u00C7"`` and ``u"\u0043\u0327"`` compare differently, 1337 1425 even though they both represent the same unicode character (LATIN 1338 CAP TITAL LETTER C WITH CEDILLA). To compare strings in a human1426 CAPITAL LETTER C WITH CEDILLA). To compare strings in a human 1339 1427 recognizable way, compare using :func:`unicodedata.normalize`. 1340 1428 … … 1353 1441 methods, or constants. Check their documentation for more info. 1354 1442 1443 .. [#] The ``%`` operator is also used for string formatting; the same 1444 precedence applies. 1445 1355 1446 .. [#] The power operator ``**`` binds less tightly than an arithmetic or 1356 1447 bitwise unary operator on its right, that is, ``2**-1`` is ``0.5``. -
python/vendor/current/Doc/reference/index.rst
r2 r388 4 4 The Python Language Reference 5 5 ################################# 6 7 :Release: |version|8 :Date: |today|9 6 10 7 This reference manual describes the syntax and "core semantics" of the -
python/vendor/current/Doc/reference/introduction.rst
r2 r388 66 66 implementation that generates IL, and compiles Python code directly to .NET 67 67 assemblies. It was created by Jim Hugunin, the original creator of Jython. For 68 more information, see `the IronPython website <http://www.ironpython. com/>`_.68 more information, see `the IronPython website <http://www.ironpython.net/>`_. 69 69 70 70 PyPy 71 An implementation of Python written in Python; even the bytecode interpreter is72 written in Python. This is executed using CPython as the underlying73 interpreter. One of the goals of the project is to encourage experimentation74 with the language itself by making it easier to modify the interpreter (since it75 i s written in Python). Additional information is available on `the PyPy76 project's home page <http://codespeak.net/pypy/>`_.71 An implementation of Python written completely in Python. It supports several 72 advanced features not found in other implementations like stackless support 73 and a Just in Time compiler. One of the goals of the project is to encourage 74 experimentation with the language itself by making it easier to modify the 75 interpreter (since it is written in Python). Additional information is 76 available on `the PyPy project's home page <http://pypy.org/>`_. 77 77 78 78 Each of these implementations varies in some way from the language as documented -
python/vendor/current/Doc/reference/lexical_analysis.rst
r2 r388 358 358 359 359 .. versionchanged:: 2.5 360 Both :keyword:`as` and :keyword:`with` are only recognized when the361 ``with_statement`` future feature has been enabled. It will always be enabled in362 Python 2.6. See section :ref:`with` for details. Note that using :keyword:`as` 363 and :keyword:`with` as identifiers will always issue a warning, even when the 364 ``with_statement`` future directive is not in effect.360 Using :keyword:`as` and :keyword:`with` as identifiers triggers a warning. To 361 use them as keywords, enable the ``with_statement`` future feature . 362 363 .. versionchanged:: 2.6 364 :keyword:`as` and :keyword:`with` are full keywords. 365 365 366 366 … … 387 387 388 388 ``__*__`` 389 System-defined names. These names are defined by the interpreter and its 390 implementation (including the standard library); applications should not expect 391 to define additional names using this convention. The set of names of this 392 class defined by Python may be extended in future versions. See section 393 :ref:`specialnames`. 389 System-defined names. These names are defined by the interpreter and its 390 implementation (including the standard library). Current system names are 391 discussed in the :ref:`specialnames` section and elsewhere. More will likely 392 be defined in future versions of Python. *Any* use of ``__*__`` names, in 393 any context, that does not follow explicitly documented use, is subject to 394 breakage without warning. 394 395 395 396 ``__*`` … … 426 427 stringliteral: [`stringprefix`](`shortstring` | `longstring`) 427 428 stringprefix: "r" | "u" | "ur" | "R" | "U" | "UR" | "Ur" | "uR" 429 : | "b" | "B" | "br" | "Br" | "bR" | "BR" 428 430 shortstring: "'" `shortstringitem`* "'" | '"' `shortstringitem`* '"' 429 431 longstring: "'''" `longstringitem`* "'''" … … 458 460 Unicode character set as defined by the Unicode Consortium and ISO 10646. Some 459 461 additional escape sequences, described below, are available in Unicode strings. 460 The two prefix characters may be combined; in this case, ``'u'`` must appear 461 before ``'r'``. 462 A prefix of ``'b'`` or ``'B'`` is ignored in Python 2; it indicates that the 463 literal should become a bytes literal in Python 3 (e.g. when code is 464 automatically converted with 2to3). A ``'u'`` or ``'b'`` prefix may be followed 465 by an ``'r'`` prefix. 462 466 463 467 In triple-quoted strings, unescaped newlines and quotes are allowed (and are … … 526 530 Any Unicode character can be encoded this way, but characters outside the Basic 527 531 Multilingual Plane (BMP) will be encoded using a surrogate pair if Python is 528 compiled to use 16-bit code units (the default). Individual code units which 529 form parts of a surrogate pair can be encoded using this escape sequence. 532 compiled to use 16-bit code units (the default). 530 533 531 534 (3) -
python/vendor/current/Doc/reference/simple_stmts.rst
r2 r388 122 122 iterable with the same number of items as there are targets in the target list, 123 123 and the items are assigned, from left to right, to the corresponding targets. 124 (This rule is relaxed as of Python 1.5; in earlier versions, the object had to125 be a tuple. Since strings are sequences, an assignment like ``a, b = "xy"`` is126 now legal as long as the string has the right length.)127 124 128 125 Assignment of an object to a single target is recursively defined as follows. … … 356 353 357 354 Deletion is recursively defined very similar to the way assignment is defined. 358 Rather tha tspelling it out in full details, here are some hints.355 Rather than spelling it out in full details, here are some hints. 359 356 360 357 Deletion of a target list recursively deletes each target, from left to right. … … 515 512 clauses to execute. 516 513 514 For full details of :keyword:`yield` semantics, refer to the :ref:`yieldexpr` 515 section. 516 517 517 .. note:: 518 518 … … 710 710 :attr:`__path__` attribute from the parent package (everything up to the last 711 711 dot in the name of the module being imported). If a finder can find the module 712 it returns a :term:`loader` (discussed later) or returns :keyword:`None`.712 it returns a :term:`loader` (discussed later) or returns ``None``. 713 713 714 714 .. index:: … … 737 737 :exc:`ImportError`. If a finder is returned then it is cached in 738 738 :data:`sys.path_importer_cache` and then used for that path entry. If no finder 739 can be found but the path exists then a value of :keyword:`None` is739 can be found but the path exists then a value of ``None`` is 740 740 stored in :data:`sys.path_importer_cache` to signify that an implicit, 741 741 file-based finder that handles modules stored as individual files should be 742 742 used for that path. If the path does not exist then a finder which always 743 returns :keyword:`None` is placed in the cache for the path.743 returns ``None`` is placed in the cache for the path. 744 744 745 745 .. index:: … … 837 837 So if you execute ``from . import mod`` from a module in the ``pkg`` package 838 838 then you will end up importing ``pkg.mod``. If you execute ``from ..subpkg2 839 imp rt mod`` from within ``pkg.subpkg1`` you will import ``pkg.subpkg2.mod``.839 import mod`` from within ``pkg.subpkg1`` you will import ``pkg.subpkg2.mod``. 840 840 The specification for relative imports is contained within :pep:`328`. 841 841 842 843 .. index:: builtin: __import__ 844 845 The built-in function :func:`__import__` is provided to support applications 846 that determine which modules need to be loaded dynamically; refer to 847 :ref:`built-in-funcs` for additional information. 842 :func:`importlib.import_module` is provided to support applications that 843 determine which modules need to be loaded dynamically. 848 844 849 845 … … 986 982 987 983 This statement supports dynamic execution of Python code. The first expression 988 should evaluate to either a string, an open file object, or a code object. If 989 it is a string, the string is parsed as a suite of Python statements which is 990 then executed (unless a syntax error occurs). [#]_ If it is an open file, the file 991 is parsed until EOF and executed. If it is a code object, it is simply 992 executed. In all cases, the code that's executed is expected to be valid as 993 file input (see section :ref:`file-input`). Be aware that the 984 should evaluate to either a Unicode string, a *Latin-1* encoded string, an open 985 file object, a code object, or a tuple. If it is a string, the string is parsed 986 as a suite of Python statements which is then executed (unless a syntax error 987 occurs). [#]_ If it is an open file, the file is parsed until EOF and executed. 988 If it is a code object, it is simply executed. For the interpretation of a 989 tuple, see below. In all cases, the code that's executed is expected to be 990 valid as file input (see section :ref:`file-input`). Be aware that the 994 991 :keyword:`return` and :keyword:`yield` statements may not be used outside of 995 992 function definitions even within the context of code passed to the … … 997 994 998 995 In all cases, if the optional parts are omitted, the code is executed in the 999 current scope. If only the first expression after :keyword:`in` is specified,996 current scope. If only the first expression after ``in`` is specified, 1000 997 it should be a dictionary, which will be used for both the global and the local 1001 998 variables. If two expressions are given, they are used for the global and local 1002 999 variables, respectively. If provided, *locals* can be any mapping object. 1000 Remember that at module level, globals and locals are the same dictionary. If 1001 two separate objects are given as *globals* and *locals*, the code will be 1002 executed as if it were embedded in a class definition. 1003 1004 The first expression may also be a tuple of length 2 or 3. In this case, the 1005 optional parts must be omitted. The form ``exec(expr, globals)`` is equivalent 1006 to ``exec expr in globals``, while the form ``exec(expr, globals, locals)`` is 1007 equivalent to ``exec expr in globals, locals``. The tuple form of ``exec`` 1008 provides compatibility with Python 3, where ``exec`` is a function rather than 1009 a statement. 1003 1010 1004 1011 .. versionchanged:: 2.4 … … 1029 1036 1030 1037 .. [#] Note that the parser only accepts the Unix-style end of line convention. 1031 If you are reading the code from a file, make sure to use universal1032 newlinemode to convert Windows or Mac-style newlines.1038 If you are reading the code from a file, make sure to use 1039 :term:`universal newlines` mode to convert Windows or Mac-style newlines.
Note:
See TracChangeset
for help on using the changeset viewer.