| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 44fa8f6 commit 96593ed
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -5,6 +5,11 @@ Built-in Constants | |||
| 5 | 5 | A small number of constants live in the built-in namespace. They are: | |
| 6 | 6 | ||
| 7 | 7 | ||
| 8 | + .. note:: | ||
| 9 | + | ||
| 10 | + :data:`None`, :data:`False`, :data:`True` and :data:`__debug__` cannot be | ||
| 11 | + reassigned, so they can be considered "true" constants. | ||
| 12 | + | ||
| 8 | 13 | .. XXX False, True, None are keywords too | |
| 9 | 14 | ||
| 10 | 15 | .. data:: False | |
@@ -37,3 +42,10 @@ A small number of constants live in the built-in namespace. They are: | |||
| 37 | 42 | slicing syntax for user-defined container data types, as in :: | |
| 38 | 43 | ||
| 39 | 44 | val = container[1:5, 7:10, ...] | |
| 45 | + | ||
| 46 | + | ||
| 47 | + .. data:: __debug__ | ||
| 48 | + | ||
| 49 | + A boolean value that is :data:`True` if Python was not started with the | ||
| 50 | + ``-O`` command line option. Its value is used indirectly by the | ||
| 51 | + :keyword:`assert` statement, but it can also be used directly in code. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,10 +19,10 @@ The typical use is:: | |||
| 19 | 19 | This iterates over the lines of all files listed in ``sys.argv[1:]``, defaulting | |
| 20 | 20 | to ``sys.stdin`` if the list is empty. If a filename is ``'-'``, it is also | |
| 21 | 21 | replaced by ``sys.stdin``. To specify an alternative list of filenames, pass it | |
| 22 | - as the first argument to :func:`input`. A single file name is also allowed. | ||
| 22 | + as the first argument to :func:`.input`. A single file name is also allowed. | ||
| 23 | 23 | ||
| 24 | 24 | All files are opened in text mode by default, but you can override this by | |
| 25 | - specifying the *mode* parameter in the call to :func:`input` or | ||
| 25 | + specifying the *mode* parameter in the call to :func:`.input` or | ||
| 26 | 26 | :class:`FileInput()`. If an I/O error occurs during opening or reading a file, | |
| 27 | 27 | :exc:`IOError` is raised. | |
| 28 | 28 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -50,4 +50,4 @@ The :mod:`new` module defines the following functions: | |||
| 50 | 50 | ||
| 51 | 51 | This function returns a new class object, with name *name*, derived from | |
| 52 | 52 | *baseclasses* (which should be a tuple of classes) and with namespace *dict*. | |
| 53 | - | ||
| 53 | + Alias for the built-in :class:`type`. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -12,8 +12,8 @@ The :mod:`readline` module defines a number of functions to facilitate | |||
| 12 | 12 | completion and reading/writing of history files from the Python interpreter. | |
| 13 | 13 | This module can be used directly or via the :mod:`rlcompleter` module. Settings | |
| 14 | 14 | made using this module affect the behaviour of both the interpreter's | |
| 15 | - interactive prompt and the prompts offered by the :func:`raw_input` and | ||
| 16 | - :func:`input` built-in functions. | ||
| 15 | + interactive prompt and the prompts offered by the built-in :func:`input` | ||
| 16 | + function. | ||
| 17 | 17 | ||
| 18 | 18 | The :mod:`readline` module defines the following functions: | |
| 19 | 19 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,25 +26,24 @@ Naming and binding | |||
| 26 | 26 | Each occurrence of a name in the program text refers to the :dfn:`binding` of | |
| 27 | 27 | that name established in the innermost function block containing the use. | |
| 28 | 28 | ||
| 29 | - .. index:: single: block | ||
| 29 | + .. index:: block | ||
| 30 | 30 | ||
| 31 | 31 | A :dfn:`block` is a piece of Python program text that is executed as a unit. | |
| 32 | 32 | The following are blocks: a module, a function body, and a class definition. | |
| 33 | 33 | Each command typed interactively is a block. A script file (a file given as | |
| 34 | 34 | standard input to the interpreter or specified on the interpreter command line | |
| 35 | 35 | the first argument) is a code block. A script command (a command specified on | |
| 36 | - the interpreter command line with the '**-c**' option) is a code block. The string | ||
| 37 | - argument passed to the built-in functions :func:`eval` and :func:`exec` is a | ||
| 38 | - code block. The expression read and evaluated by the built-in function | ||
| 39 | - :func:`input` is a code block. | ||
| 36 | + the interpreter command line with the '**-c**' option) is a code block. The | ||
| 37 | + string argument passed to the built-in functions :func:`eval` and :func:`exec` | ||
| 38 | + is a code block. | ||
| 40 | 39 | ||
| 41 | 40 | .. index:: pair: execution; frame | |
| 42 | 41 | ||
| 43 | 42 | A code block is executed in an :dfn:`execution frame`. A frame contains some | |
| 44 | 43 | administrative information (used for debugging) and determines where and how | |
| 45 | 44 | execution continues after the code block's execution has completed. | |
| 46 | 45 | ||
| 47 | - .. index:: single: scope | ||
| 46 | + .. index:: scope | ||
| 48 | 47 | ||
| 49 | 48 | A :dfn:`scope` defines the visibility of a name within a block. If a local | |
| 50 | 49 | variable is defined in a block, its scope includes that block. If the | |
@@ -61,10 +60,11 @@ scope. The set of all such scopes visible to a code block is called the block's | |||
| 61 | 60 | ||
| 62 | 61 | .. index:: pair: free; variable | |
| 63 | 62 | ||
| 64 | - If a name is bound in a block, it is a local variable of that block. If a name | ||
| 65 | - is bound at the module level, it is a global variable. (The variables of the | ||
| 66 | - module code block are local and global.) If a variable is used in a code block | ||
| 67 | - but not defined there, it is a :dfn:`free variable`. | ||
| 63 | + If a name is bound in a block, it is a local variable of that block, unless | ||
| 64 | + declared as :keyword:`nonlocal`. If a name is bound at the module level, it is | ||
| 65 | + a global variable. (The variables of the module code block are local and | ||
| 66 | + global.) If a variable is used in a code block but not defined there, it is a | ||
| 67 | + :dfn:`free variable`. | ||
| 68 | 68 | ||
| 69 | 69 | .. index:: | |
| 70 | 70 | single: NameError (built-in exception) | |
@@ -96,18 +96,20 @@ function definition or at the module level (the top-level code block). | |||
| 96 | 96 | ||
| 97 | 97 | If a name binding operation occurs anywhere within a code block, all uses of the | |
| 98 | 98 | name within the block are treated as references to the current block. This can | |
| 99 | - lead to errors when a name is used within a block before it is bound. This rule | ||
| 99 | + lead to errors when a name is used within a block before it is bound. This rule | ||
| 100 | 100 | is subtle. Python lacks declarations and allows name binding operations to | |
| 101 | 101 | occur anywhere within a code block. The local variables of a code block can be | |
| 102 | 102 | determined by scanning the entire text of the block for name binding operations. | |
| 103 | 103 | ||
| 104 | - If the global statement occurs within a block, all uses of the name specified in | ||
| 105 | - the statement refer to the binding of that name in the top-level namespace. | ||
| 106 | - Names are resolved in the top-level namespace by searching the global namespace, | ||
| 107 | - i.e. the namespace of the module containing the code block, and the builtin | ||
| 108 | - namespace, the namespace of the module :mod:`__builtin__`. The global namespace | ||
| 109 | - is searched first. If the name is not found there, the builtin namespace is | ||
| 110 | - searched. The global statement must precede all uses of the name. | ||
| 104 | + If the :keyword:`global` statement occurs within a block, all uses of the name | ||
| 105 | + specified in the statement refer to the binding of that name in the top-level | ||
| 106 | + namespace. Names are resolved in the top-level namespace by searching the | ||
| 107 | + global namespace, i.e. the namespace of the module containing the code block, | ||
| 108 | + and the builtin namespace, the namespace of the module :mod:`__builtin__`. The | ||
| 109 | + global namespace is searched first. If the name is not found there, the builtin | ||
| 110 | + namespace is searched. The global statement must precede all uses of the name. | ||
| 111 | + | ||
| 112 | + .. XXX document "nonlocal" semantics here | ||
| 111 | 113 | ||
| 112 | 114 | .. index:: pair: restricted; execution | |
| 113 | 115 | ||
@@ -137,7 +139,7 @@ block. If the nearest enclosing scope for a free variable contains a global | |||
| 137 | 139 | statement, the free variable is treated as a global. | |
| 138 | 140 | ||
| 139 | 141 | A class definition is an executable statement that may use and define names. | |
| 140 | - These references follow the normal rules for name resolution. The namespace of | ||
| 142 | + These references follow the normal rules for name resolution. The namespace of | ||
| 141 | 143 | the class definition becomes the attribute dictionary of the class. Names | |
| 142 | 144 | defined at the class scope are not visible in methods. | |
| 143 | 145 | ||
@@ -157,13 +159,14 @@ If the wild card form of import --- ``import *`` --- is used in a function and | |||
| 157 | 159 | the function contains or is a nested block with free variables, the compiler | |
| 158 | 160 | will raise a :exc:`SyntaxError`. | |
| 159 | 161 | ||
| 160 | - The :func:`eval` and :func:`exec` functions do | ||
| 161 | - not have access to the full environment for resolving names. Names may be | ||
| 162 | - resolved in the local and global namespaces of the caller. Free variables are | ||
| 163 | - not resolved in the nearest enclosing namespace, but in the global namespace. | ||
| 164 | - [#]_ The :func:`exec` and :func:`eval` functions have optional | ||
| 165 | - arguments to override the global and local namespace. If only one namespace is | ||
| 166 | - specified, it is used for both. | ||
| 162 | + .. XXX from * also invalid with relative imports (at least currently) | ||
| 163 | + | ||
| 164 | + The :func:`eval` and :func:`exec` functions do not have access to the full | ||
| 165 | + environment for resolving names. Names may be resolved in the local and global | ||
| 166 | + namespaces of the caller. Free variables are not resolved in the nearest | ||
| 167 | + enclosing namespace, but in the global namespace. [#]_ The :func:`exec` and | ||
| 168 | + :func:`eval` functions have optional arguments to override the global and local | ||
| 169 | + namespace. If only one namespace is specified, it is used for both. | ||
| 167 | 170 | ||
| 168 | 171 | ||
| 169 | 172 | .. _exceptions: | |
@@ -205,28 +208,24 @@ re-entering the offending piece of code from the top). | |||
| 205 | 208 | ||
| 206 | 209 | When an exception is not handled at all, the interpreter terminates execution of | |
| 207 | 210 | the program, or returns to its interactive main loop. In either case, it prints | |
| 208 | - a stack backtrace, except when the exception is :exc:`SystemExit`. | ||
| 211 | + a stack backtrace, except when the exception is :exc:`SystemExit`. | ||
| 209 | 212 | ||
| 210 | 213 | Exceptions are identified by class instances. The :keyword:`except` clause is | |
| 211 | 214 | selected depending on the class of the instance: it must reference the class of | |
| 212 | 215 | the instance or a base class thereof. The instance can be received by the | |
| 213 | 216 | handler and can carry additional information about the exceptional condition. | |
| 214 | 217 | ||
| 215 | - Exceptions can also be identified by strings, in which case the | ||
| 216 | - :keyword:`except` clause is selected by object identity. An arbitrary value can | ||
| 217 | - be raised along with the identifying string which can be passed to the handler. | ||
| 218 | - | ||
| 219 | 218 | .. warning:: | |
| 220 | 219 | ||
| 221 | - Messages to exceptions are not part of the Python API. Their contents may | ||
| 222 | - change from one version of Python to the next without warning and should not be | ||
| 220 | + Exception messages are not part of the Python API. Their contents may change | ||
| 221 | + from one version of Python to the next without warning and should not be | ||
| 223 | 222 | relied on by code which will run under multiple versions of the interpreter. | |
| 224 | 223 | ||
| 225 | 224 | See also the description of the :keyword:`try` statement in section :ref:`try` | |
| 226 | 225 | and :keyword:`raise` statement in section :ref:`raise`. | |
| 227 | 226 | ||
| 228 | 227 | .. rubric:: Footnotes | |
| 229 | 228 | ||
| 230 | - .. [#] This limitation occurs because the code that is executed by these operations is | ||
| 231 | - not available at the time the module is compiled. | ||
| 229 | + .. [#] This limitation occurs because the code that is executed by these operations | ||
| 230 | + is not available at the time the module is compiled. | ||
| 232 | 231 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments