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 | |
| 149 | \section{Nested scopes \label{nested-scopes}} |
| 150 | |
Jeremy Hylton | 324cc6e | 2001-03-23 15:29:54 +0000 | [diff] [blame] | 151 | \indexii{nested}{scopes} |
| 152 | |
| 153 | Nested scopes are left as an exercise for the reader. |