Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 1 | \chapter{Future statements and nested scopes \label{futures}} |
| 2 | \sectionauthor{Jeremy Hylton}{jeremy@alum.mit.edu} |
| 3 | |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 4 | |
| 5 | The semantics of Python's static scoping will change in version 2.2 to |
| 6 | support resolution of unbound local names in enclosing functions' |
| 7 | namespaces. The new semantics will be available in Python 2.1 through |
Fred Drake | a9d0450 | 2001-03-23 16:47:11 +0000 | [diff] [blame] | 8 | the use of a future statement. This appendix documents these two |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 9 | features for Python 2.1; it will be removed in Python 2.2 and the |
| 10 | features will be documented in the main sections of this manual. |
| 11 | |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 12 | |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 13 | \section{Future statements \label{future-statements}} |
| 14 | |
| 15 | A \dfn{future statement}\indexii{future}{statement} is a directive to |
| 16 | the compiler that a particular module should be compiled using syntax |
| 17 | or semantics that will be available in a specified future release of |
| 18 | Python. The future statement is intended to ease migration to future |
| 19 | versions of Python that introduce incompatible changes to the |
| 20 | language. It allows use of the new features on a per-module basis |
| 21 | before the release in which the feature becomes standard. |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 22 | |
| 23 | \begin{verbatim} |
| 24 | future_statement: "from" "__future__" "import" feature ["as" name] |
| 25 | ("," feature ["as" name])* |
| 26 | |
| 27 | feature: identifier |
| 28 | name: identifier |
| 29 | \end{verbatim} |
| 30 | |
| 31 | A future statement must appear near the top of the module. The only |
| 32 | lines that can appear before a future statement are: |
| 33 | |
| 34 | \begin{itemize} |
| 35 | |
| 36 | \item the module docstring (if any), |
| 37 | \item comments, |
| 38 | \item blank lines, and |
| 39 | \item other future statements. |
| 40 | |
| 41 | \end{itemize} |
| 42 | |
| 43 | The only feature recognized by Python 2.1 is \samp{nested_scopes}. |
| 44 | |
| 45 | A future statement is recognized and treated specially at compile time: |
| 46 | Changes to the semantics of core constructs are often implemented by |
| 47 | generating different code. It may even be the case that a new feature |
| 48 | introduces new incompatible syntax (such as a new reserved word), in |
| 49 | which case the compiler may need to parse the module differently. Such |
| 50 | decisions cannot be pushed off until runtime. |
| 51 | |
| 52 | For any given release, the compiler knows which feature names have been |
| 53 | defined, and raises a compile-time error if a future statement contains |
| 54 | a feature not known to it. |
| 55 | |
| 56 | The direct runtime semantics are the same as for any import statement: |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 57 | there is a standard module \module{__future__}, described later, and |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 58 | it will be imported in the usual way at the time the future statement |
| 59 | is executed. |
| 60 | |
| 61 | The interesting runtime semantics depend on the specific feature |
| 62 | enabled by the future statement. |
| 63 | |
| 64 | Note that there is nothing special about the statement: |
| 65 | |
| 66 | \begin{verbatim} |
| 67 | import __future__ [as name] |
| 68 | \end{verbatim} |
| 69 | |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 70 | That is not a future statement; it's an ordinary import statement with |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 71 | no special semantics or syntax restrictions. |
| 72 | |
| 73 | Code compiled by an exec statement or calls to the builtin functions |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 74 | \function{compile()} and \function{execfile()} that occur in a module |
| 75 | \module{M} containing a future statement will use the new syntax or |
| 76 | semantics associated with the future statement. |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 77 | |
| 78 | A future statement typed at an interactive interpreter prompt will |
| 79 | take effect for the rest of the interpreter session. If an |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 80 | interpreter is started with the \programopt{-i} option, is passed a |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 81 | script name to execute, and the script includes a future statement, it |
| 82 | will be in effect in the interactive session started after the script |
| 83 | is executed. |
| 84 | |
| 85 | \section{\module{__future__} --- |
| 86 | Future statement definitions} |
| 87 | |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 88 | \declaremodule[future]{standard}{__future__} |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 89 | \modulesynopsis{Future statement definitions} |
| 90 | |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 91 | \module{__future__} is a real module, and serves three purposes: |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 92 | |
| 93 | \begin{itemize} |
| 94 | |
| 95 | \item To avoid confusing existing tools that analyze import statements |
| 96 | and expect to find the modules they're importing. |
| 97 | |
| 98 | \item To ensure that future_statements run under releases prior to 2.1 |
| 99 | at least yield runtime exceptions (the import of |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 100 | \module{__future__} will fail, because there was no module of |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 101 | that name prior to 2.1). |
| 102 | |
| 103 | \item To document when incompatible changes were introduced, and when they |
| 104 | will be --- or were --- made mandatory. This is a form of executable |
| 105 | documentation, and can be inspected programatically via importing |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 106 | \module{__future__} and examining its contents. |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 107 | |
| 108 | \end{itemize} |
| 109 | |
| 110 | Each statment in \file{__future__.py} is of the form: |
| 111 | |
| 112 | \begin{verbatim} |
| 113 | FeatureName = "_Feature(" OptionalRelease "," MandatoryRelease ")" |
| 114 | \end{verbatim} |
| 115 | |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 116 | where, normally, OptionalRelease is less then MandatoryRelease, and |
| 117 | both are 5-tuples of the same form as \code{sys.version_info}: |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 118 | |
| 119 | \begin{verbatim} |
| 120 | (PY_MAJOR_VERSION, # the 2 in 2.1.0a3; an int |
| 121 | PY_MINOR_VERSION, # the 1; an int |
| 122 | PY_MICRO_VERSION, # the 0; an int |
| 123 | PY_RELEASE_LEVEL, # "alpha", "beta", "candidate" or "final"; string |
| 124 | PY_RELEASE_SERIAL # the 3; an int |
| 125 | ) |
| 126 | \end{verbatim} |
| 127 | |
| 128 | OptionalRelease records the first release in which the feature was |
| 129 | accepted. |
| 130 | |
| 131 | In the case of MandatoryReleases that have not yet occurred, |
| 132 | MandatoryRelease predicts the release in which the feature will become |
| 133 | part of the language. |
| 134 | |
| 135 | Else MandatoryRelease records when the feature became part of the |
| 136 | language; in releases at or after that, modules no longer need a |
| 137 | future statement to use the feature in question, but may continue to |
| 138 | use such imports. |
| 139 | |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 140 | MandatoryRelease may also be \code{None}, meaning that a planned |
| 141 | feature got dropped. |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 142 | |
| 143 | Instances of class \class{_Feature} have two corresponding methods, |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 144 | \method{getOptionalRelease()} and \method{getMandatoryRelease()}. |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 145 | |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 146 | No feature description will ever be deleted from \module{__future__}. |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 147 | |
Fred Drake | 39778b7 | 2001-03-23 16:20:46 +0000 | [diff] [blame] | 148 | \section{Nested scopes \label{nested-scopes}} |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 149 | \indexii{nested}{scopes} |
| 150 | |
Jeremy Hylton | aa90adc | 2001-03-23 17:23:50 +0000 | [diff] [blame] | 151 | This section defines the new scoping semantics that will be introduced |
| 152 | in Python 2.2. They are available in Python 2.1 by using the future |
| 153 | statement \samp{nested_scopes}. This section begins with a bit of |
| 154 | terminology. |
| 155 | |
| 156 | \subsection{Definitions and rules \label{defintions}} |
| 157 | |
| 158 | \dfn{Names} refer to objects. Names are introduced by name binding |
| 159 | operations. Each occurrence of a name in the program text refers to |
| 160 | the binding of that name established in the innermost function block |
| 161 | containing the use. |
| 162 | |
| 163 | A \dfn{block} is a pice of Python program text that can is executed as |
| 164 | a unit. The following are blocks: a module, a function body, and a |
| 165 | class defintion. |
| 166 | |
| 167 | A \dfn{scope} defines the visibility of a name within a block. If a |
| 168 | local variable is defined in a block, it's scope includes that block. |
| 169 | If the definition occurs in a function block, the scope extends to any |
| 170 | blocks contained within the defining one, unless a contained block |
| 171 | introduces a different binding for the name. The scope of names |
| 172 | defined in a class block is limited to the class block; it does not |
| 173 | extend to the code blocks of methods. |
| 174 | |
| 175 | When a name is used in a code block, it is resolved using the nearest |
| 176 | enclosing scope. The set of all such scopes visible to a code block |
| 177 | is called the block's \dfn{environment}. |
| 178 | |
| 179 | If a name is bound in a block, it is a local variable of that block. |
| 180 | If a name is bound at the module level, it is a global variable. (The |
Fred Drake | e264577 | 2001-03-28 16:55:53 +0000 | [diff] [blame] | 181 | variables of the module code block are local and global.) If a |
Jeremy Hylton | aa90adc | 2001-03-23 17:23:50 +0000 | [diff] [blame] | 182 | variable is used in a code block but not defined there, it is a |
| 183 | \dfn{free variable}. |
| 184 | |
| 185 | The name binding operations are assignment, class and function |
| 186 | definition, import statements, for statements, and except statements. |
| 187 | Each assignment or import statement occurs within a block defined by a |
| 188 | class or function definition or at the module level (the top-level |
| 189 | code block). |
| 190 | |
| 191 | If a name binding operation occurs anywhere within a code block, all |
| 192 | uses of the name within the block are treated as references to the |
| 193 | current block. This can lead to errors when a name is used within a |
| 194 | block before it is bound. |
| 195 | |
| 196 | The previous rule is a subtle. Python lacks declarations and allows |
| 197 | name binding operations to occur anywhere within a code block. The |
| 198 | local variables of a code block can be determined by scanning the |
| 199 | entire text of the block for name binding operations. |
| 200 | |
| 201 | If the global statement occurs within a block, all uses of the name |
| 202 | specified in the statement refer to the binding of that name in the |
| 203 | top-level namespace. Names are resolved in the top-level namespace by |
| 204 | searching the global namespace, i.e. the namespace of the module |
| 205 | containing the code block, and the builtin namespace, the namespace of |
| 206 | the module \module{__builtin__}. The global namespace is searched |
| 207 | first. If the name is not found there, the builtin namespace is |
| 208 | searched. The global statement must precede all uses of the name. |
| 209 | |
| 210 | The global statement has the same scope as a name binding operation |
| 211 | in the same block. If the nearest enclosing scope for a free variable |
| 212 | contains a global statement, the free variable is treated as a global. |
| 213 | |
| 214 | A class definition is an executable statement that may use and define |
| 215 | names. These references follow the normal rules for name resolution. |
| 216 | The namespace of the class definition becomes the attribute dictionary |
| 217 | of the class. Names defined at the class scope are not visible in |
| 218 | methods. |
| 219 | |
| 220 | \subsection{Interaction with dynamic features \label{dynamic-features}} |
| 221 | |
| 222 | There are several cases where Python statements are illegal when |
| 223 | used in conjunction with nested scopes that contain free |
| 224 | variables. |
| 225 | |
| 226 | If a variable is referenced in an enclosing scope, it is illegal |
| 227 | to delete the name. An error will be reported at compile time. |
| 228 | |
| 229 | If the wild card form of import --- \samp{import *} --- is used in a |
| 230 | function and the function contains or is a nested block with free |
| 231 | variables, the compiler will raise a SyntaxError. |
| 232 | |
| 233 | If exec is used in a function and the function contains or is a nested |
| 234 | block with free variables, the compiler will raise a SyntaxError |
| 235 | unless the exec explicitly specifies the local namespace for the exec. |
| 236 | (In other words, "exec obj" would be illegal, but "exec obj in ns" |
| 237 | would be legal.) |
| 238 | |
| 239 | The builtin functions \function{eval()} and \function{input()} can not |
| 240 | access free variables unless the variables are also referenced by the |
| 241 | program text of the block that contains the call to \function{eval()} |
| 242 | or \function{input()}. |
| 243 | |
| 244 | \emph{Compatibility note}: The compiler for Python 2.1 will issue |
| 245 | warnings for uses of nested functions that will behave differently |
| 246 | with nested scopes. The warnings will not be issued if nested scopes |
| 247 | are enabled via a future statement. If a name bound in a function |
| 248 | scope and the function contains a nested function scope that uses the |
| 249 | name, the compiler will issue a warning. The name resolution rules |
| 250 | will result in different bindings under Python 2.1 than under Python |
| 251 | 2.2. The warning indicates that the program may not run correctly |
| 252 | with all versions of Python. |